NadekoBot/src/NadekoBot/Modules/Games/Commands/HangmanCommands.cs

61 lines
2.1 KiB
C#
Raw Normal View History

2016-12-17 00:16:14 +00:00
using Discord.Commands;
2016-12-14 23:56:13 +00:00
using NadekoBot.Attributes;
using NadekoBot.Extensions;
using NadekoBot.Modules.Games.Commands.Hangman;
using NLog;
using System;
using System.Collections.Concurrent;
using System.Threading.Tasks;
2017-02-18 20:07:36 +00:00
using NadekoBot.Modules.Games.Hangman;
using Discord;
2016-12-14 23:56:13 +00:00
namespace NadekoBot.Modules.Games
{
public partial class Games
{
[Group]
2017-02-19 14:18:40 +00:00
public class HangmanCommands : NadekoSubmodule
2016-12-14 23:56:13 +00:00
{
//channelId, game
public static ConcurrentDictionary<ulong, HangmanGame> HangmanGames { get; } = new ConcurrentDictionary<ulong, HangmanGame>();
[NadekoCommand, Usage, Description, Aliases]
2016-12-16 18:43:57 +00:00
public async Task Hangmanlist()
2016-12-14 23:56:13 +00:00
{
await Context.Channel.SendConfirmAsync(Format.Code(GetText("hangman_types", Prefix)) + "\n" + String.Join(", ", HangmanTermPool.data.Keys));
2016-12-14 23:56:13 +00:00
}
2017-01-08 07:57:26 +00:00
2016-12-14 23:56:13 +00:00
[NadekoCommand, Usage, Description, Aliases]
public async Task Hangman([Remainder]string type = "All")
2016-12-14 23:56:13 +00:00
{
2016-12-16 18:43:57 +00:00
var hm = new HangmanGame(Context.Channel, type);
2016-12-14 23:56:13 +00:00
2016-12-16 18:43:57 +00:00
if (!HangmanGames.TryAdd(Context.Channel.Id, hm))
2016-12-14 23:56:13 +00:00
{
await ReplyErrorLocalized("hangman_running").ConfigureAwait(false);
2016-12-14 23:56:13 +00:00
return;
}
hm.OnEnded += (g) =>
{
HangmanGame throwaway;
HangmanGames.TryRemove(g.GameChannel.Id, out throwaway);
};
try
{
hm.Start();
}
2017-01-11 12:54:25 +00:00
catch (Exception ex)
{
try { await Context.Channel.SendErrorAsync(GetText("hangman_start_errored") + " " + ex.Message).ConfigureAwait(false); } catch { }
2017-01-11 12:54:25 +00:00
HangmanGame throwaway;
HangmanGames.TryRemove(Context.Channel.Id, out throwaway);
throwaway.Dispose();
return;
}
2016-12-14 23:56:13 +00:00
await Context.Channel.SendConfirmAsync(GetText("hangman_game_started"), hm.ScrambledWord + "\n" + hm.GetHangman());
2016-12-14 23:56:13 +00:00
}
}
}
}