added hangman category to hangman output - regardless of whether you've selected a category or got a random one.
This commit is contained in:
parent
015724a150
commit
d910683d78
6 changed files with 40 additions and 24 deletions
src/EllieBot/Modules/Games/Hangman
|
@ -44,7 +44,7 @@ public sealed class DefaultHangmanSource : IHangmanSource
|
|||
public IReadOnlyCollection<string> GetCategories()
|
||||
=> termsDict.Keys.ToList();
|
||||
|
||||
public bool GetTerm(string? category, [NotNullWhen(true)] out HangmanTerm? term)
|
||||
public bool GetTerm(string? category, [NotNullWhen(true)] out (HangmanTerm Term, string Category)? term)
|
||||
{
|
||||
if (category is null)
|
||||
{
|
||||
|
@ -54,7 +54,7 @@ public sealed class DefaultHangmanSource : IHangmanSource
|
|||
|
||||
if (termsDict.TryGetValue(category, out var terms))
|
||||
{
|
||||
term = terms[_rng.Next(0, terms.Length)];
|
||||
term = (terms[_rng.Next(0, terms.Length)], category);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
@ -10,7 +10,8 @@ public partial class Games
|
|||
[Cmd]
|
||||
[RequireContext(ContextType.Guild)]
|
||||
public async Task Hangmanlist()
|
||||
=> await Response().Confirm(GetText(strs.hangman_types(prefix)), _service.GetHangmanTypes().Join('\n'))
|
||||
=> await Response()
|
||||
.Confirm(GetText(strs.hangman_types(prefix)), _service.GetHangmanTypes().Join('\n'))
|
||||
.SendAsync();
|
||||
|
||||
private static string Draw(HangmanGame.State state)
|
||||
|
@ -35,29 +36,25 @@ public partial class Games
|
|||
|
||||
public static EmbedBuilder GetEmbed(IMessageSenderService sender, HangmanGame.State state)
|
||||
{
|
||||
var eb = sender.CreateEmbed()
|
||||
.WithOkColor()
|
||||
.AddField("Hangman", Draw(state))
|
||||
.AddField("Guess", Format.Code(state.Word));
|
||||
|
||||
if (state.Phase == HangmanGame.Phase.Running)
|
||||
{
|
||||
return sender.CreateEmbed()
|
||||
.WithOkColor()
|
||||
.AddField("Hangman", Draw(state))
|
||||
.AddField("Guess", Format.Code(state.Word))
|
||||
.WithFooter(state.MissedLetters.Join(' '));
|
||||
return eb
|
||||
.WithFooter(state.MissedLetters.Join(' '))
|
||||
.WithAuthor(state.Category);
|
||||
}
|
||||
|
||||
if (state.Phase == HangmanGame.Phase.Ended && state.Failed)
|
||||
{
|
||||
return sender.CreateEmbed()
|
||||
.WithErrorColor()
|
||||
.AddField("Hangman", Draw(state))
|
||||
.AddField("Guess", Format.Code(state.Word))
|
||||
return eb
|
||||
.WithFooter(state.MissedLetters.Join(' '));
|
||||
}
|
||||
|
||||
return sender.CreateEmbed()
|
||||
.WithOkColor()
|
||||
.AddField("Hangman", Draw(state))
|
||||
.AddField("Guess", Format.Code(state.Word))
|
||||
.WithFooter(state.MissedLetters.Join(' '));
|
||||
return eb.WithFooter(state.MissedLetters.Join(' '));
|
||||
}
|
||||
|
||||
[Cmd]
|
||||
|
|
|
@ -4,9 +4,20 @@ namespace EllieBot.Modules.Games.Hangman;
|
|||
|
||||
public sealed class HangmanGame
|
||||
{
|
||||
public enum GuessResult { NoAction, AlreadyTried, Incorrect, Guess, Win }
|
||||
public enum GuessResult
|
||||
{
|
||||
NoAction,
|
||||
AlreadyTried,
|
||||
Incorrect,
|
||||
Guess,
|
||||
Win
|
||||
}
|
||||
|
||||
public enum Phase { Running, Ended }
|
||||
public enum Phase
|
||||
{
|
||||
Running,
|
||||
Ended
|
||||
}
|
||||
|
||||
private Phase CurrentPhase { get; set; }
|
||||
|
||||
|
@ -17,16 +28,21 @@ public sealed class HangmanGame
|
|||
private readonly string _word;
|
||||
private readonly string _imageUrl;
|
||||
|
||||
public HangmanGame(HangmanTerm term)
|
||||
public string Category { get; }
|
||||
|
||||
public HangmanGame(HangmanTerm term, string cat)
|
||||
{
|
||||
_word = term.Word;
|
||||
_imageUrl = term.ImageUrl;
|
||||
Category = cat;
|
||||
|
||||
_remaining = _word.ToLowerInvariant().Where(x => char.IsLetter(x)).Select(char.ToLowerInvariant).ToHashSet();
|
||||
}
|
||||
|
||||
public State GetState(GuessResult guessResult = GuessResult.NoAction)
|
||||
=> new(_incorrect.Count,
|
||||
=> new(
|
||||
Category,
|
||||
_incorrect.Count,
|
||||
CurrentPhase,
|
||||
CurrentPhase == Phase.Ended ? _word : GetScrambledWord(),
|
||||
guessResult,
|
||||
|
@ -99,6 +115,7 @@ public sealed class HangmanGame
|
|||
}
|
||||
|
||||
public record State(
|
||||
string Category,
|
||||
int Errors,
|
||||
Phase Phase,
|
||||
string Word,
|
||||
|
|
|
@ -36,10 +36,10 @@ public sealed class HangmanService : IHangmanService, IExecNoCommand
|
|||
public bool StartHangman(ulong channelId, string? category, [NotNullWhen(true)] out HangmanGame.State? state)
|
||||
{
|
||||
state = null;
|
||||
if (!_source.GetTerm(category, out var term))
|
||||
if (!_source.GetTerm(category, out var termData))
|
||||
return false;
|
||||
|
||||
var game = new HangmanGame(term);
|
||||
var game = new HangmanGame(termData.Value.Term, termData.Value.Category);
|
||||
lock (_locker)
|
||||
{
|
||||
var hc = _hangmanGames.GetOrAdd(channelId, game);
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
#nullable disable
|
||||
using YamlDotNet.Serialization;
|
||||
|
||||
namespace EllieBot.Modules.Games.Hangman;
|
||||
|
||||
public sealed class HangmanTerm
|
||||
|
|
|
@ -6,5 +6,5 @@ public interface IHangmanSource : IEService
|
|||
{
|
||||
public IReadOnlyCollection<string> GetCategories();
|
||||
public void Reload();
|
||||
public bool GetTerm(string? category, [NotNullWhen(true)] out HangmanTerm? term);
|
||||
public bool GetTerm(string? category, [NotNullWhen(true)] out (HangmanTerm Term, string Category)? term);
|
||||
}
|
Loading…
Add table
Reference in a new issue