2016-12-17 01:16:14 +01:00
using Discord.Commands ;
2016-12-15 00:56:13 +01:00
using NadekoBot.Extensions ;
using System ;
using System.Threading.Tasks ;
2017-02-21 18:46:17 +01:00
using Discord ;
2017-05-27 10:19:27 +02:00
using Discord.WebSocket ;
2017-07-17 21:42:36 +02:00
using NadekoBot.Common.Attributes ;
using NadekoBot.Modules.Games.Common.Hangman ;
2017-10-05 00:51:12 +02:00
using NadekoBot.Modules.Games.Services ;
2016-12-15 00:56:13 +01:00
namespace NadekoBot.Modules.Games
{
public partial class Games
{
[Group]
2017-10-05 00:51:12 +02:00
public class HangmanCommands : NadekoSubmodule < GamesService >
2016-12-15 00:56:13 +01:00
{
2017-06-19 15:42:10 +02:00
private readonly DiscordSocketClient _client ;
2017-05-27 10:19:27 +02:00
2017-06-19 15:42:10 +02:00
public HangmanCommands ( DiscordSocketClient client )
2017-05-27 10:19:27 +02:00
{
_client = client ;
}
2016-12-15 00:56:13 +01:00
[NadekoCommand, Usage, Description, Aliases]
2017-06-28 03:42:02 +02:00
[RequireContext(ContextType.Guild)]
2016-12-16 19:43:57 +01:00
public async Task Hangmanlist ( )
2016-12-15 00:56:13 +01:00
{
2017-10-09 02:52:46 +02:00
await Context . Channel . SendConfirmAsync ( Format . Code ( GetText ( "hangman_types" , Prefix ) ) + "\n" + string . Join ( "\n" , _service . TermPool . Data . Keys ) ) ;
2016-12-15 00:56:13 +01:00
}
2017-01-08 08:57:26 +01:00
2016-12-15 00:56:13 +01:00
[NadekoCommand, Usage, Description, Aliases]
2017-06-28 03:42:02 +02:00
[RequireContext(ContextType.Guild)]
2017-09-29 16:37:26 +02:00
public async Task Hangman ( [ Remainder ] string type = "random" )
2016-12-15 00:56:13 +01:00
{
2017-10-09 02:52:46 +02:00
var hm = new Hangman ( type , _service . TermPool ) ;
2016-12-15 00:56:13 +01:00
2017-10-05 00:51:12 +02:00
if ( ! _service . HangmanGames . TryAdd ( Context . Channel . Id , hm ) )
2016-12-15 00:56:13 +01:00
{
2017-07-28 12:28:08 +02:00
hm . Dispose ( ) ;
2017-02-21 18:46:17 +01:00
await ReplyErrorLocalized ( "hangman_running" ) . ConfigureAwait ( false ) ;
2016-12-15 00:56:13 +01:00
return ;
}
2017-07-28 12:28:08 +02:00
hm . OnGameEnded + = Hm_OnGameEnded ;
hm . OnGuessFailed + = Hm_OnGuessFailed ;
hm . OnGuessSucceeded + = Hm_OnGuessSucceeded ;
hm . OnLetterAlreadyUsed + = Hm_OnLetterAlreadyUsed ;
_client . MessageReceived + = _client_MessageReceived ;
2016-12-15 00:56:13 +01:00
2017-01-08 09:48:35 +01:00
try
{
2017-07-28 12:28:08 +02:00
await Context . Channel . SendConfirmAsync ( GetText ( "hangman_game_started" ) + $" ({hm.TermType})" ,
hm . ScrambledWord + "\n" + hm . GetHangman ( ) )
. ConfigureAwait ( false ) ;
2017-01-08 09:48:35 +01:00
}
2017-07-28 12:28:08 +02:00
catch { }
await hm . EndedTask . ConfigureAwait ( false ) ;
2017-07-28 12:38:08 +02:00
_client . MessageReceived - = _client_MessageReceived ;
2017-10-05 00:51:12 +02:00
_service . HangmanGames . TryRemove ( Context . Channel . Id , out _ ) ;
2017-07-28 12:38:08 +02:00
hm . Dispose ( ) ;
2017-07-28 12:28:08 +02:00
Task _client_MessageReceived ( SocketMessage msg )
2017-01-11 13:54:25 +01:00
{
2017-07-28 12:28:08 +02: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 ;
2017-01-08 09:48:35 +01:00
}
2017-07-28 12:28:08 +02:00
}
2016-12-15 00:56:13 +01:00
2017-07-28 12:28:08 +02: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 )
{
2017-09-10 23:48:34 +02:00
return Context . Channel . SendConfirmAsync ( $"Hangman Game ({game.TermType})" , $"{user} guessed a letter `{guess}`!\n" + game . ScrambledWord + "\n" + game . GetHangman ( ) ,
footer : string . Join ( " " , game . PreviousGuesses ) ) ;
2017-07-28 12:28:08 +02:00
}
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-15 00:56:13 +01:00
}
2017-06-28 03:42:02 +02:00
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
public async Task HangmanStop ( )
{
2017-10-05 00:51:12 +02:00
if ( _service . HangmanGames . TryRemove ( Context . Channel . Id , out var removed ) )
2017-06-28 03:42:02 +02:00
{
2017-07-28 12:38:08 +02:00
await removed . Stop ( ) . ConfigureAwait ( false ) ;
2017-06-28 03:42:02 +02:00
await ReplyConfirmLocalized ( "hangman_stopped" ) . ConfigureAwait ( false ) ;
}
}
2016-12-15 00:56:13 +01:00
}
}
2017-07-28 12:28:08 +02:00
}