2016-09-01 01:12:08 +00:00
|
|
|
|
using Discord;
|
|
|
|
|
using Discord.Commands;
|
2017-01-01 12:26:17 +00:00
|
|
|
|
using Discord.WebSocket;
|
2016-09-01 01:12:08 +00:00
|
|
|
|
using NadekoBot.Attributes;
|
|
|
|
|
using NadekoBot.Extensions;
|
2017-05-27 08:19:27 +00:00
|
|
|
|
using NadekoBot.Modules.Games.Models;
|
|
|
|
|
using NadekoBot.Services.Games;
|
2016-10-10 17:18:50 +00:00
|
|
|
|
using Newtonsoft.Json;
|
2016-09-01 01:12:08 +00:00
|
|
|
|
using System.Collections.Concurrent;
|
2016-10-10 17:18:50 +00:00
|
|
|
|
using System.IO;
|
2016-09-01 01:12:08 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace NadekoBot.Modules.Games
|
|
|
|
|
{
|
2016-09-08 20:46:38 +00:00
|
|
|
|
public partial class Games
|
2016-09-01 01:12:08 +00:00
|
|
|
|
{
|
2016-09-08 20:46:38 +00:00
|
|
|
|
[Group]
|
2017-02-19 14:18:40 +00:00
|
|
|
|
public class SpeedTypingCommands : NadekoSubmodule
|
2016-09-01 01:12:08 +00:00
|
|
|
|
{
|
2017-05-27 08:19:27 +00:00
|
|
|
|
public static ConcurrentDictionary<ulong, TypingGame> RunningContests = new ConcurrentDictionary<ulong, TypingGame>();
|
|
|
|
|
private readonly GamesService _games;
|
|
|
|
|
private readonly DiscordShardedClient _client;
|
2016-10-10 17:18:50 +00:00
|
|
|
|
|
2017-05-27 08:19:27 +00:00
|
|
|
|
public SpeedTypingCommands(DiscordShardedClient client, GamesService games)
|
2016-10-10 17:18:50 +00:00
|
|
|
|
{
|
2017-05-27 08:19:27 +00:00
|
|
|
|
_games = games;
|
|
|
|
|
_client = client;
|
2016-10-10 17:18:50 +00:00
|
|
|
|
}
|
2016-09-08 20:46:38 +00:00
|
|
|
|
|
2016-10-05 03:09:44 +00:00
|
|
|
|
[NadekoCommand, Usage, Description, Aliases]
|
2016-09-08 20:46:38 +00:00
|
|
|
|
[RequireContext(ContextType.Guild)]
|
2016-12-16 18:43:57 +00:00
|
|
|
|
public async Task TypeStart()
|
2016-09-01 01:12:08 +00:00
|
|
|
|
{
|
2016-12-16 18:43:57 +00:00
|
|
|
|
var channel = (ITextChannel)Context.Channel;
|
2016-09-08 20:46:38 +00:00
|
|
|
|
|
2017-05-30 04:54:59 +00:00
|
|
|
|
var game = RunningContests.GetOrAdd(channel.Guild.Id, id => new TypingGame(_games, _client, channel, Prefix));
|
2016-09-08 20:46:38 +00:00
|
|
|
|
|
|
|
|
|
if (game.IsActive)
|
|
|
|
|
{
|
2016-12-11 00:42:58 +00:00
|
|
|
|
await channel.SendErrorAsync(
|
2016-09-08 20:46:38 +00:00
|
|
|
|
$"Contest already running in " +
|
|
|
|
|
$"{game.Channel.Mention} channel.")
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
await game.Start().ConfigureAwait(false);
|
|
|
|
|
}
|
2016-09-01 01:12:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-05 03:09:44 +00:00
|
|
|
|
[NadekoCommand, Usage, Description, Aliases]
|
2016-09-08 20:46:38 +00:00
|
|
|
|
[RequireContext(ContextType.Guild)]
|
2016-12-16 18:43:57 +00:00
|
|
|
|
public async Task TypeStop()
|
2016-09-01 01:12:08 +00:00
|
|
|
|
{
|
2016-12-16 18:43:57 +00:00
|
|
|
|
var channel = (ITextChannel)Context.Channel;
|
2016-09-08 20:46:38 +00:00
|
|
|
|
TypingGame game;
|
|
|
|
|
if (RunningContests.TryRemove(channel.Guild.Id, out game))
|
|
|
|
|
{
|
|
|
|
|
await game.Stop().ConfigureAwait(false);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2016-12-11 00:42:58 +00:00
|
|
|
|
await channel.SendErrorAsync("No contest to stop on this channel.").ConfigureAwait(false);
|
2016-09-01 01:12:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-12-11 00:42:58 +00:00
|
|
|
|
|
2016-10-05 03:09:44 +00:00
|
|
|
|
[NadekoCommand, Usage, Description, Aliases]
|
2016-09-30 02:20:09 +00:00
|
|
|
|
[RequireContext(ContextType.Guild)]
|
|
|
|
|
[OwnerOnly]
|
2016-12-16 19:45:46 +00:00
|
|
|
|
public async Task Typeadd([Remainder] string text)
|
2016-09-30 02:20:09 +00:00
|
|
|
|
{
|
2016-12-16 18:43:57 +00:00
|
|
|
|
var channel = (ITextChannel)Context.Channel;
|
2017-05-29 04:13:22 +00:00
|
|
|
|
if (string.IsNullOrWhiteSpace(text))
|
|
|
|
|
return;
|
2016-12-11 00:42:58 +00:00
|
|
|
|
|
2017-05-29 04:13:22 +00:00
|
|
|
|
_games.AddTypingArticle(Context.User, text);
|
2016-09-30 02:20:09 +00:00
|
|
|
|
|
2016-12-11 00:42:58 +00:00
|
|
|
|
await channel.SendConfirmAsync("Added new article for typing game.").ConfigureAwait(false);
|
2016-09-30 02:20:09 +00:00
|
|
|
|
}
|
2016-10-28 11:14:16 +00:00
|
|
|
|
|
|
|
|
|
[NadekoCommand, Usage, Description, Aliases]
|
|
|
|
|
[RequireContext(ContextType.Guild)]
|
2016-12-16 19:45:46 +00:00
|
|
|
|
public async Task Typelist(int page = 1)
|
2016-10-28 11:14:16 +00:00
|
|
|
|
{
|
2016-12-16 18:43:57 +00:00
|
|
|
|
var channel = (ITextChannel)Context.Channel;
|
2016-10-28 11:14:16 +00:00
|
|
|
|
|
|
|
|
|
if (page < 1)
|
|
|
|
|
return;
|
|
|
|
|
|
2017-05-27 08:19:27 +00:00
|
|
|
|
var articles = _games.TypingArticles.Skip((page - 1) * 15).Take(15).ToArray();
|
2016-10-28 11:14:16 +00:00
|
|
|
|
|
|
|
|
|
if (!articles.Any())
|
|
|
|
|
{
|
2016-12-16 18:43:57 +00:00
|
|
|
|
await channel.SendErrorAsync($"{Context.User.Mention} `No articles found on that page.`").ConfigureAwait(false);
|
2016-10-28 11:14:16 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
var i = (page - 1) * 15;
|
2017-02-16 15:34:14 +00:00
|
|
|
|
await channel.SendConfirmAsync("List of articles for Type Race", string.Join("\n", articles.Select(a => $"`#{++i}` - {a.Text.TrimTo(50)}")))
|
2016-10-28 11:14:16 +00:00
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[NadekoCommand, Usage, Description, Aliases]
|
|
|
|
|
[RequireContext(ContextType.Guild)]
|
|
|
|
|
[OwnerOnly]
|
2016-12-16 19:45:46 +00:00
|
|
|
|
public async Task Typedel(int index)
|
2016-10-28 11:14:16 +00:00
|
|
|
|
{
|
2016-12-16 18:43:57 +00:00
|
|
|
|
var channel = (ITextChannel)Context.Channel;
|
2016-10-28 11:14:16 +00:00
|
|
|
|
|
|
|
|
|
index -= 1;
|
2017-05-27 08:19:27 +00:00
|
|
|
|
if (index < 0 || index >= _games.TypingArticles.Count)
|
2016-10-28 11:14:16 +00:00
|
|
|
|
return;
|
|
|
|
|
|
2017-05-27 08:19:27 +00:00
|
|
|
|
var removed = _games.TypingArticles[index];
|
|
|
|
|
_games.TypingArticles.RemoveAt(index);
|
2016-10-28 11:14:16 +00:00
|
|
|
|
|
2017-05-27 08:19:27 +00:00
|
|
|
|
File.WriteAllText(_games.TypingArticlesPath, JsonConvert.SerializeObject(_games.TypingArticles));
|
2016-10-28 11:14:16 +00:00
|
|
|
|
|
2016-12-11 00:42:58 +00:00
|
|
|
|
await channel.SendConfirmAsync($"`Removed typing article:` #{index + 1} - {removed.Text.TrimTo(50)}")
|
2016-10-28 11:14:16 +00:00
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
}
|
2016-09-08 20:46:38 +00:00
|
|
|
|
}
|
2016-09-01 01:12:08 +00:00
|
|
|
|
}
|
2016-09-08 20:46:38 +00:00
|
|
|
|
}
|