NadekoBot/NadekoBot.Modules.Games/HangmanCommands.cs

135 lines
5.9 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.Extensions;
using System;
using System.Threading.Tasks;
using Discord;
2017-05-27 08:19:27 +00:00
using Discord.WebSocket;
2017-07-17 19:42:36 +00:00
using NadekoBot.Common.Attributes;
using NadekoBot.Modules.Games.Common.Hangman;
using NadekoBot.Modules.Games.Services;
2016-12-14 23:56:13 +00:00
namespace NadekoBot.Modules.Games
{
public partial class Games
{
[Group]
public class HangmanCommands : NadekoSubmodule<GamesService>
2016-12-14 23:56:13 +00:00
{
private readonly DiscordSocketClient _client;
2017-05-27 08:19:27 +00:00
public HangmanCommands(DiscordSocketClient client)
2017-05-27 08:19:27 +00:00
{
_client = client;
}
2016-12-14 23:56:13 +00:00
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
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("\n", TermPool.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]
[RequireContext(ContextType.Guild)]
public async Task Hangman([Remainder]string type = "random")
2016-12-14 23:56:13 +00:00
{
var hm = new Hangman(type);
2016-12-14 23:56:13 +00:00
if (!_service.HangmanGames.TryAdd(Context.Channel.Id, hm))
2016-12-14 23:56:13 +00:00
{
hm.Dispose();
await ReplyErrorLocalized("hangman_running").ConfigureAwait(false);
2016-12-14 23:56:13 +00:00
return;
}
hm.OnGameEnded += Hm_OnGameEnded;
hm.OnGuessFailed += Hm_OnGuessFailed;
hm.OnGuessSucceeded += Hm_OnGuessSucceeded;
hm.OnLetterAlreadyUsed += Hm_OnLetterAlreadyUsed;
_client.MessageReceived += _client_MessageReceived;
2016-12-14 23:56:13 +00:00
try
{
await Context.Channel.SendConfirmAsync(GetText("hangman_game_started") + $" ({hm.TermType})",
hm.ScrambledWord + "\n" + hm.GetHangman())
.ConfigureAwait(false);
}
catch { }
await hm.EndedTask.ConfigureAwait(false);
2017-07-28 10:38:08 +00:00
_client.MessageReceived -= _client_MessageReceived;
_service.HangmanGames.TryRemove(Context.Channel.Id, out _);
2017-07-28 10:38:08 +00:00
hm.Dispose();
Task _client_MessageReceived(SocketMessage msg)
2017-01-11 12:54:25 +00:00
{
var _ = Task.Run(() =>
{
if (Context.Channel.Id == msg.Channel.Id)
return hm.Input(msg.Author.Id, msg.Author.ToString(), msg.Content);
else
return Task.CompletedTask;
});
return Task.CompletedTask;
}
}
2016-12-14 23:56:13 +00:00
Task Hm_OnGameEnded(Hangman game, string winner)
{
if (winner == null)
{
var loseEmbed = new EmbedBuilder().WithTitle($"Hangman Game ({game.TermType}) - Ended")
.WithDescription(Format.Bold("You lose."))
.AddField(efb => efb.WithName("It was").WithValue(game.Term.Word.ToTitleCase()))
.WithFooter(efb => efb.WithText(string.Join(" ", game.PreviousGuesses)))
.WithErrorColor();
if (Uri.IsWellFormedUriString(game.Term.ImageUrl, UriKind.Absolute))
loseEmbed.WithImageUrl(game.Term.ImageUrl);
return Context.Channel.EmbedAsync(loseEmbed);
}
var winEmbed = new EmbedBuilder().WithTitle($"Hangman Game ({game.TermType}) - Ended")
.WithDescription(Format.Bold($"{winner} Won."))
.AddField(efb => efb.WithName("It was").WithValue(game.Term.Word.ToTitleCase()))
.WithFooter(efb => efb.WithText(string.Join(" ", game.PreviousGuesses)))
.WithOkColor();
if (Uri.IsWellFormedUriString(game.Term.ImageUrl, UriKind.Absolute))
winEmbed.WithImageUrl(game.Term.ImageUrl);
return Context.Channel.EmbedAsync(winEmbed);
}
private Task Hm_OnLetterAlreadyUsed(Hangman game, string user, char guess)
{
return Context.Channel.SendErrorAsync($"Hangman Game ({game.TermType})", $"{user} Letter `{guess}` has already been used. You can guess again in 3 seconds.\n" + game.ScrambledWord + "\n" + game.GetHangman(),
footer: string.Join(" ", game.PreviousGuesses));
}
private Task Hm_OnGuessSucceeded(Hangman game, string user, char guess)
{
return Context.Channel.SendConfirmAsync($"Hangman Game ({game.TermType})", $"{user} guessed a letter `{guess}`!\n" + game.ScrambledWord + "\n" + game.GetHangman(),
footer: string.Join(" ", game.PreviousGuesses));
}
private Task Hm_OnGuessFailed(Hangman game, string user, char guess)
{
return Context.Channel.SendErrorAsync($"Hangman Game ({game.TermType})", $"{user} Letter `{guess}` does not exist. You can guess again in 3 seconds.\n" + game.ScrambledWord + "\n" + game.GetHangman(),
footer: string.Join(" ", game.PreviousGuesses));
2016-12-14 23:56:13 +00:00
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
public async Task HangmanStop()
{
if (_service.HangmanGames.TryRemove(Context.Channel.Id, out var removed))
{
2017-07-28 10:38:08 +00:00
await removed.Stop().ConfigureAwait(false);
await ReplyConfirmLocalized("hangman_stopped").ConfigureAwait(false);
}
}
2016-12-14 23:56:13 +00:00
}
}
}