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.Collections.Concurrent ;
using System.Threading.Tasks ;
2017-02-21 17:46:17 +00:00
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 ;
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
{
2017-06-19 13:42:10 +00:00
private readonly DiscordSocketClient _client ;
2017-05-27 08:19:27 +00:00
2017-06-19 13:42:10 +00:00
public HangmanCommands ( DiscordSocketClient client )
2017-05-27 08:19:27 +00:00
{
_client = client ;
}
2016-12-14 23:56:13 +00:00
//channelId, game
2017-07-28 10:28:08 +00:00
public static ConcurrentDictionary < ulong , Hangman > HangmanGames { get ; } = new ConcurrentDictionary < ulong , Hangman > ( ) ;
2016-12-14 23:56:13 +00:00
[NadekoCommand, Usage, Description, Aliases]
2017-06-28 01:42:02 +00:00
[RequireContext(ContextType.Guild)]
2016-12-16 18:43:57 +00:00
public async Task Hangmanlist ( )
2016-12-14 23:56:13 +00:00
{
2017-09-29 14:37:26 +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]
2017-06-28 01:42:02 +00:00
[RequireContext(ContextType.Guild)]
2017-09-29 14:37:26 +00:00
public async Task Hangman ( [ Remainder ] string type = "random" )
2016-12-14 23:56:13 +00:00
{
2017-07-28 10:28:08 +00:00
var hm = new Hangman ( 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
{
2017-07-28 10:28:08 +00:00
hm . Dispose ( ) ;
2017-02-21 17:46:17 +00:00
await ReplyErrorLocalized ( "hangman_running" ) . ConfigureAwait ( false ) ;
2016-12-14 23:56:13 +00:00
return ;
}
2017-07-28 10:28:08 +00:00
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
2017-01-08 08:48:35 +00:00
try
{
2017-07-28 10:28:08 +00:00
await Context . Channel . SendConfirmAsync ( GetText ( "hangman_game_started" ) + $" ({hm.TermType})" ,
hm . ScrambledWord + "\n" + hm . GetHangman ( ) )
. ConfigureAwait ( false ) ;
2017-01-08 08:48:35 +00:00
}
2017-07-28 10:28:08 +00:00
catch { }
await hm . EndedTask . ConfigureAwait ( false ) ;
2017-07-28 10:38:08 +00:00
_client . MessageReceived - = _client_MessageReceived ;
HangmanGames . TryRemove ( Context . Channel . Id , out _ ) ;
hm . Dispose ( ) ;
2017-07-28 10:28:08 +00:00
Task _client_MessageReceived ( SocketMessage msg )
2017-01-11 12:54:25 +00:00
{
2017-07-28 10:28:08 +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 ;
2017-01-08 08:48:35 +00:00
}
2017-07-28 10:28:08 +00:00
}
2016-12-14 23:56:13 +00:00
2017-07-28 10:28:08 +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 )
{
2017-09-10 21:48:34 +00: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 10:28:08 +00: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-14 23:56:13 +00:00
}
2017-06-28 01:42:02 +00:00
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
public async Task HangmanStop ( )
{
2017-07-25 16:31:30 +00:00
if ( HangmanGames . TryRemove ( Context . Channel . Id , out var removed ) )
2017-06-28 01:42:02 +00:00
{
2017-07-28 10:38:08 +00:00
await removed . Stop ( ) . ConfigureAwait ( false ) ;
2017-06-28 01:42:02 +00:00
await ReplyConfirmLocalized ( "hangman_stopped" ) . ConfigureAwait ( false ) ;
}
}
2016-12-14 23:56:13 +00:00
}
}
2017-07-28 10:28:08 +00:00
}