NadekoBot/NadekoBot.Modules.Games/AcropobiaCommands.cs

151 lines
6.0 KiB
C#
Raw Normal View History

2016-12-22 10:39:19 +00:00
using Discord;
using Discord.Commands;
using Discord.WebSocket;
2016-12-22 10:39:19 +00:00
using NadekoBot.Extensions;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Threading.Tasks;
2017-07-17 19:42:36 +00:00
using NadekoBot.Common.Attributes;
using NadekoBot.Modules.Games.Common.Acrophobia;
2016-12-22 10:39:19 +00:00
namespace NadekoBot.Modules.Games
{
public partial class Games
{
[Group]
2017-07-17 19:42:36 +00:00
public class AcropobiaCommands : NadekoSubmodule
2016-12-22 10:39:19 +00:00
{
private readonly DiscordSocketClient _client;
2017-05-27 08:19:27 +00:00
2016-12-22 10:39:19 +00:00
//channelId, game
public static ConcurrentDictionary<ulong, Acrophobia> AcrophobiaGames { get; } = new ConcurrentDictionary<ulong, Acrophobia>();
2016-12-22 10:39:19 +00:00
2017-07-17 19:42:36 +00:00
public AcropobiaCommands(DiscordSocketClient client)
2017-05-27 08:19:27 +00:00
{
_client = client;
}
2016-12-22 10:39:19 +00:00
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
public async Task Acro(int submissionTime = 30)
2016-12-22 10:39:19 +00:00
{
if (submissionTime < 10 || submissionTime > 120)
return;
2017-01-01 12:28:35 +00:00
var channel = (ITextChannel)Context.Channel;
2016-12-22 10:39:19 +00:00
var game = new Acrophobia(submissionTime);
2016-12-22 10:39:19 +00:00
if (AcrophobiaGames.TryAdd(channel.Id, game))
{
try
{
game.OnStarted += Game_OnStarted;
game.OnEnded += Game_OnEnded;
game.OnVotingStarted += Game_OnVotingStarted;
game.OnUserVoted += Game_OnUserVoted;
_client.MessageReceived += _client_MessageReceived;
await game.Run().ConfigureAwait(false);
2016-12-22 10:39:19 +00:00
}
finally
{
_client.MessageReceived -= _client_MessageReceived;
2016-12-22 10:39:19 +00:00
AcrophobiaGames.TryRemove(channel.Id, out game);
game.Dispose();
2016-12-22 10:39:19 +00:00
}
}
else
{
2017-02-19 14:18:40 +00:00
await ReplyErrorLocalized("acro_running").ConfigureAwait(false);
2016-12-22 10:39:19 +00:00
}
2016-12-22 11:14:18 +00:00
Task _client_MessageReceived(SocketMessage msg)
{
if (msg.Channel.Id != Context.Channel.Id)
return Task.CompletedTask;
2016-12-22 10:39:19 +00:00
var _ = Task.Run(async () =>
{
try
{
var success = await game.UserInput(msg.Author.Id, msg.Author.ToString(), msg.Content)
.ConfigureAwait(false);
if (success)
await msg.DeleteAsync().ConfigureAwait(false);
}
catch { }
});
2016-12-22 10:39:19 +00:00
return Task.CompletedTask;
2016-12-22 10:39:19 +00:00
}
}
private Task Game_OnStarted(Acrophobia game)
2016-12-22 10:39:19 +00:00
{
var embed = new EmbedBuilder().WithOkColor()
2017-02-19 14:18:40 +00:00
.WithTitle(GetText("acrophobia"))
.WithDescription(GetText("acro_started", Format.Bold(string.Join(".", game.StartingLetters))))
.WithFooter(efb => efb.WithText(GetText("acro_started_footer", game.SubmissionPhaseLength)));
2017-02-19 14:18:40 +00:00
return Context.Channel.EmbedAsync(embed);
2016-12-22 10:39:19 +00:00
}
private Task Game_OnUserVoted(string user)
2016-12-22 10:39:19 +00:00
{
return Context.Channel.SendConfirmAsync(
GetText("acrophobia"),
GetText("acro_vote_cast", Format.Bold(user)));
}
2016-12-22 10:39:19 +00:00
private async Task Game_OnVotingStarted(Acrophobia game, ImmutableArray<KeyValuePair<AcrophobiaUser, int>> submissions)
{
if (submissions.Length == 0)
2016-12-22 10:39:19 +00:00
{
await Context.Channel.SendErrorAsync(GetText("acrophobia"), GetText("acro_ended_no_sub"));
2016-12-22 10:39:19 +00:00
return;
}
if (submissions.Length == 1)
2016-12-22 10:39:19 +00:00
{
await Context.Channel.EmbedAsync(new EmbedBuilder().WithOkColor()
2017-02-19 14:18:40 +00:00
.WithDescription(
GetText("acro_winner_only",
Format.Bold(submissions.First().Key.UserName)))
.WithFooter(efb => efb.WithText(submissions.First().Key.Input)))
2017-02-19 14:18:40 +00:00
.ConfigureAwait(false);
2016-12-22 10:39:19 +00:00
return;
}
var i = 0;
var embed = new EmbedBuilder()
.WithOkColor()
.WithTitle(GetText("acrophobia") + " - " + GetText("submissions_closed"))
.WithDescription(GetText("acro_nym_was", Format.Bold(string.Join(".", game.StartingLetters)) + "\n" +
$@"--
{submissions.Aggregate("", (agg, cur) => agg + $"`{++i}.` **{cur.Key.Input}**\n")}
--"))
.WithFooter(efb => efb.WithText(GetText("acro_vote")));
await Context.Channel.EmbedAsync(embed).ConfigureAwait(false);
2016-12-22 10:39:19 +00:00
}
private async Task Game_OnEnded(Acrophobia game, ImmutableArray<KeyValuePair<AcrophobiaUser, int>> votes)
2016-12-22 10:39:19 +00:00
{
if (!votes.Any() || votes.All(x => x.Value == 0))
2016-12-22 10:39:19 +00:00
{
await Context.Channel.SendErrorAsync(GetText("acrophobia"), GetText("acro_no_votes_cast")).ConfigureAwait(false);
2016-12-22 10:39:19 +00:00
return;
}
var table = votes.OrderByDescending(v => v.Value);
2016-12-22 10:39:19 +00:00
var winner = table.First();
var embed = new EmbedBuilder().WithOkColor()
2017-02-19 14:18:40 +00:00
.WithTitle(GetText("acrophobia"))
.WithDescription(GetText("acro_winner", Format.Bold(winner.Key.UserName),
2017-02-19 14:18:40 +00:00
Format.Bold(winner.Value.ToString())))
.WithFooter(efb => efb.WithText(winner.Key.Input));
2016-12-22 10:39:19 +00:00
await Context.Channel.EmbedAsync(embed).ConfigureAwait(false);
2016-12-22 10:39:19 +00:00
}
}
}
}