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()
|
public IReadOnlyCollection<string> GetCategories()
|
||||||
=> termsDict.Keys.ToList();
|
=> 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)
|
if (category is null)
|
||||||
{
|
{
|
||||||
|
@ -54,7 +54,7 @@ public sealed class DefaultHangmanSource : IHangmanSource
|
||||||
|
|
||||||
if (termsDict.TryGetValue(category, out var terms))
|
if (termsDict.TryGetValue(category, out var terms))
|
||||||
{
|
{
|
||||||
term = terms[_rng.Next(0, terms.Length)];
|
term = (terms[_rng.Next(0, terms.Length)], category);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -10,7 +10,8 @@ public partial class Games
|
||||||
[Cmd]
|
[Cmd]
|
||||||
[RequireContext(ContextType.Guild)]
|
[RequireContext(ContextType.Guild)]
|
||||||
public async Task Hangmanlist()
|
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();
|
.SendAsync();
|
||||||
|
|
||||||
private static string Draw(HangmanGame.State state)
|
private static string Draw(HangmanGame.State state)
|
||||||
|
@ -35,29 +36,25 @@ public partial class Games
|
||||||
|
|
||||||
public static EmbedBuilder GetEmbed(IMessageSenderService sender, HangmanGame.State state)
|
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)
|
if (state.Phase == HangmanGame.Phase.Running)
|
||||||
{
|
{
|
||||||
return sender.CreateEmbed()
|
return eb
|
||||||
.WithOkColor()
|
.WithFooter(state.MissedLetters.Join(' '))
|
||||||
.AddField("Hangman", Draw(state))
|
.WithAuthor(state.Category);
|
||||||
.AddField("Guess", Format.Code(state.Word))
|
|
||||||
.WithFooter(state.MissedLetters.Join(' '));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (state.Phase == HangmanGame.Phase.Ended && state.Failed)
|
if (state.Phase == HangmanGame.Phase.Ended && state.Failed)
|
||||||
{
|
{
|
||||||
return sender.CreateEmbed()
|
return eb
|
||||||
.WithErrorColor()
|
|
||||||
.AddField("Hangman", Draw(state))
|
|
||||||
.AddField("Guess", Format.Code(state.Word))
|
|
||||||
.WithFooter(state.MissedLetters.Join(' '));
|
.WithFooter(state.MissedLetters.Join(' '));
|
||||||
}
|
}
|
||||||
|
|
||||||
return sender.CreateEmbed()
|
return eb.WithFooter(state.MissedLetters.Join(' '));
|
||||||
.WithOkColor()
|
|
||||||
.AddField("Hangman", Draw(state))
|
|
||||||
.AddField("Guess", Format.Code(state.Word))
|
|
||||||
.WithFooter(state.MissedLetters.Join(' '));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[Cmd]
|
[Cmd]
|
||||||
|
|
|
@ -4,9 +4,20 @@ namespace EllieBot.Modules.Games.Hangman;
|
||||||
|
|
||||||
public sealed class HangmanGame
|
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; }
|
private Phase CurrentPhase { get; set; }
|
||||||
|
|
||||||
|
@ -17,16 +28,21 @@ public sealed class HangmanGame
|
||||||
private readonly string _word;
|
private readonly string _word;
|
||||||
private readonly string _imageUrl;
|
private readonly string _imageUrl;
|
||||||
|
|
||||||
public HangmanGame(HangmanTerm term)
|
public string Category { get; }
|
||||||
|
|
||||||
|
public HangmanGame(HangmanTerm term, string cat)
|
||||||
{
|
{
|
||||||
_word = term.Word;
|
_word = term.Word;
|
||||||
_imageUrl = term.ImageUrl;
|
_imageUrl = term.ImageUrl;
|
||||||
|
Category = cat;
|
||||||
|
|
||||||
_remaining = _word.ToLowerInvariant().Where(x => char.IsLetter(x)).Select(char.ToLowerInvariant).ToHashSet();
|
_remaining = _word.ToLowerInvariant().Where(x => char.IsLetter(x)).Select(char.ToLowerInvariant).ToHashSet();
|
||||||
}
|
}
|
||||||
|
|
||||||
public State GetState(GuessResult guessResult = GuessResult.NoAction)
|
public State GetState(GuessResult guessResult = GuessResult.NoAction)
|
||||||
=> new(_incorrect.Count,
|
=> new(
|
||||||
|
Category,
|
||||||
|
_incorrect.Count,
|
||||||
CurrentPhase,
|
CurrentPhase,
|
||||||
CurrentPhase == Phase.Ended ? _word : GetScrambledWord(),
|
CurrentPhase == Phase.Ended ? _word : GetScrambledWord(),
|
||||||
guessResult,
|
guessResult,
|
||||||
|
@ -99,6 +115,7 @@ public sealed class HangmanGame
|
||||||
}
|
}
|
||||||
|
|
||||||
public record State(
|
public record State(
|
||||||
|
string Category,
|
||||||
int Errors,
|
int Errors,
|
||||||
Phase Phase,
|
Phase Phase,
|
||||||
string Word,
|
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)
|
public bool StartHangman(ulong channelId, string? category, [NotNullWhen(true)] out HangmanGame.State? state)
|
||||||
{
|
{
|
||||||
state = null;
|
state = null;
|
||||||
if (!_source.GetTerm(category, out var term))
|
if (!_source.GetTerm(category, out var termData))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
var game = new HangmanGame(term);
|
var game = new HangmanGame(termData.Value.Term, termData.Value.Category);
|
||||||
lock (_locker)
|
lock (_locker)
|
||||||
{
|
{
|
||||||
var hc = _hangmanGames.GetOrAdd(channelId, game);
|
var hc = _hangmanGames.GetOrAdd(channelId, game);
|
||||||
|
|
|
@ -1,4 +1,6 @@
|
||||||
#nullable disable
|
#nullable disable
|
||||||
|
using YamlDotNet.Serialization;
|
||||||
|
|
||||||
namespace EllieBot.Modules.Games.Hangman;
|
namespace EllieBot.Modules.Games.Hangman;
|
||||||
|
|
||||||
public sealed class HangmanTerm
|
public sealed class HangmanTerm
|
||||||
|
|
|
@ -6,5 +6,5 @@ public interface IHangmanSource : IEService
|
||||||
{
|
{
|
||||||
public IReadOnlyCollection<string> GetCategories();
|
public IReadOnlyCollection<string> GetCategories();
|
||||||
public void Reload();
|
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
Add a link
Reference in a new issue