diff --git a/src/NadekoBot/Modules/Games/Commands/Hangman/HangmanGame.cs b/src/NadekoBot/Modules/Games/Commands/Hangman/HangmanGame.cs index d096b452..58071453 100644 --- a/src/NadekoBot/Modules/Games/Commands/Hangman/HangmanGame.cs +++ b/src/NadekoBot/Modules/Games/Commands/Hangman/HangmanGame.cs @@ -12,37 +12,15 @@ using System.Threading.Tasks; namespace NadekoBot.Modules.Games.Commands.Hangman { - public class HangmanModel - { - public List All { get; set; } - public List Animals { get; set; } - public List Countries { get; set; } - public List Movies { get; set; } - public List Things { get; set; } - } - public class HangmanTermPool { - public enum HangmanTermType - { - All, - Animals, - Countries, - Movies, - Things - } - const string termsPath = "data/hangman.json"; - public static HangmanModel data { get; } + public static IReadOnlyDictionary data { get; } static HangmanTermPool() { try { - data = JsonConvert.DeserializeObject(File.ReadAllText(termsPath)); - data.All = data.Animals.Concat(data.Countries) - .Concat(data.Movies) - .Concat(data.Things) - .ToList(); + data = JsonConvert.DeserializeObject>(File.ReadAllText(termsPath)); } catch (Exception ex) { @@ -50,23 +28,27 @@ namespace NadekoBot.Modules.Games.Commands.Hangman } } - public static HangmanObject GetTerm(HangmanTermType type) + public static HangmanObject GetTerm(string type) { + if (string.IsNullOrWhiteSpace(type)) + throw new ArgumentNullException(nameof(type)); + + type = type.Trim(); + var rng = new NadekoRandom(); - switch (type) - { - case HangmanTermType.Animals: - return data.Animals[rng.Next(0, data.Animals.Count)]; - case HangmanTermType.Countries: - return data.Countries[rng.Next(0, data.Countries.Count)]; - case HangmanTermType.Movies: - return data.Movies[rng.Next(0, data.Movies.Count)]; - case HangmanTermType.Things: - return data.Things[rng.Next(0, data.Things.Count)]; - default: - return data.All[rng.Next(0, data.All.Count)]; + + if (type == "All") { + var keys = data.Keys.ToArray(); + type = keys[rng.Next(0, keys.Length)]; } + HangmanObject[] termTypes; + data.TryGetValue(type, out termTypes); + + if (termTypes.Length == 0) + return null; + + return termTypes[rng.Next(0, termTypes.Length)]; } } @@ -95,20 +77,23 @@ namespace NadekoBot.Modules.Games.Commands.Hangman public bool GuessedAll => Guesses.IsSupersetOf(Term.Word.ToUpperInvariant() .Where(c => char.IsLetter(c) || char.IsDigit(c))); - public HangmanTermPool.HangmanTermType TermType { get; } + public string TermType { get; } public event Action OnEnded; - public HangmanGame(IMessageChannel channel, HangmanTermPool.HangmanTermType type) + public HangmanGame(IMessageChannel channel, string type) { _log = LogManager.GetCurrentClassLogger(); this.GameChannel = channel; - this.TermType = type; + this.TermType = type.ToTitleCase(); } public void Start() { this.Term = HangmanTermPool.GetTerm(TermType); + + if (this.Term == null) + throw new KeyNotFoundException("Can't find a term with that type. Use hangmanlist command."); // start listening for answers when game starts NadekoBot.Client.MessageReceived += PotentialGuess; } diff --git a/src/NadekoBot/Modules/Games/Commands/HangmanCommands.cs b/src/NadekoBot/Modules/Games/Commands/HangmanCommands.cs index eba11193..0827ab94 100644 --- a/src/NadekoBot/Modules/Games/Commands/HangmanCommands.cs +++ b/src/NadekoBot/Modules/Games/Commands/HangmanCommands.cs @@ -23,7 +23,7 @@ namespace NadekoBot.Modules.Games static HangmanCommands() { _log = LogManager.GetCurrentClassLogger(); - typesStr = $"`List of \"{NadekoBot.ModulePrefixes[typeof(Games).Name]}hangman\" term types:`\n" + String.Join(", ", Enum.GetNames(typeof(HangmanTermPool.HangmanTermType))); + typesStr = $"`List of \"{NadekoBot.ModulePrefixes[typeof(Games).Name]}hangman\" term types:`\n" + String.Join(", ", HangmanTermPool.data.Keys); } [NadekoCommand, Usage, Description, Aliases] @@ -33,7 +33,7 @@ namespace NadekoBot.Modules.Games } [NadekoCommand, Usage, Description, Aliases] - public async Task Hangman(HangmanTermPool.HangmanTermType type = HangmanTermPool.HangmanTermType.All) + public async Task Hangman([Remainder]string type = "All") { var hm = new HangmanGame(Context.Channel, type); @@ -48,7 +48,14 @@ namespace NadekoBot.Modules.Games HangmanGame throwaway; HangmanGames.TryRemove(g.GameChannel.Id, out throwaway); }; - hm.Start(); + try + { + hm.Start(); + } + catch (Exception ex) { + try { await Context.Channel.SendErrorAsync($"Starting errored: {ex.Message}").ConfigureAwait(false); } catch { } + return; + } await Context.Channel.SendConfirmAsync("Hangman game started", hm.ScrambledWord + "\n" + hm.GetHangman() + "\n" + hm.ScrambledWord); }