NadekoBot/NadekoBot.Modules.Games/TicTacToeCommands.cs

62 lines
1.9 KiB
C#
Raw Normal View History

2017-02-08 20:44:32 +00:00
using Discord;
using Discord.Commands;
2017-05-27 08:19:27 +00:00
using Discord.WebSocket;
2017-02-08 20:44:32 +00:00
using NadekoBot.Extensions;
using System;
using System.Text;
using System.Threading;
2017-02-08 20:44:32 +00:00
using System.Threading.Tasks;
2017-07-17 19:42:36 +00:00
using NadekoBot.Common.Attributes;
using NadekoBot.Services.Impl;
using NadekoBot.Modules.Games.Services;
using NadekoBot.Modules.Games.Common;
2017-02-08 20:44:32 +00:00
namespace NadekoBot.Modules.Games
{
public partial class Games
{
[Group]
public class TicTacToeCommands : NadekoSubmodule<GamesService>
2017-02-08 20:44:32 +00:00
{
private readonly SemaphoreSlim _sem = new SemaphoreSlim(1, 1);
private readonly DiscordSocketClient _client;
2017-05-27 08:19:27 +00:00
public TicTacToeCommands(DiscordSocketClient client)
2017-05-27 08:19:27 +00:00
{
_client = client;
}
2017-02-08 20:44:32 +00:00
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
public async Task TicTacToe()
2017-02-08 20:44:32 +00:00
{
var channel = (ITextChannel)Context.Channel;
await _sem.WaitAsync(1000);
try
2017-02-08 20:44:32 +00:00
{
if (_service.TicTacToeGames.TryGetValue(channel.Id, out TicTacToe game))
2017-02-08 20:44:32 +00:00
{
var _ = Task.Run(async () =>
{
await game.Start((IGuildUser)Context.User);
});
2017-02-08 20:44:32 +00:00
return;
}
game = new TicTacToe(base._strings, this._client, channel, (IGuildUser)Context.User);
_service.TicTacToeGames.Add(channel.Id, game);
await ReplyConfirmLocalized("ttt_created").ConfigureAwait(false);
game.OnEnded += (g) =>
{
_service.TicTacToeGames.Remove(channel.Id);
};
2017-02-08 20:44:32 +00:00
}
finally
2017-02-08 20:44:32 +00:00
{
_sem.Release();
2017-02-08 20:44:32 +00:00
}
}
}
}
}