2016-12-22 10:39:19 +00:00
|
|
|
|
using Discord;
|
|
|
|
|
using Discord.Commands;
|
2017-01-01 12:26:17 +00:00
|
|
|
|
using Discord.WebSocket;
|
2016-12-22 10:39:19 +00:00
|
|
|
|
using NadekoBot.Extensions;
|
|
|
|
|
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;
|
2017-07-27 16:43:15 +00:00
|
|
|
|
using NadekoBot.Modules.Games.Common.Acrophobia;
|
2017-10-04 22:51:12 +00:00
|
|
|
|
using NadekoBot.Modules.Games.Services;
|
2016-12-22 10:39:19 +00:00
|
|
|
|
|
|
|
|
|
namespace NadekoBot.Modules.Games
|
|
|
|
|
{
|
|
|
|
|
public partial class Games
|
|
|
|
|
{
|
|
|
|
|
[Group]
|
2017-10-04 22:51:12 +00:00
|
|
|
|
public class AcropobiaCommands : NadekoSubmodule<GamesService>
|
2016-12-22 10:39:19 +00:00
|
|
|
|
{
|
2017-06-19 13:42:10 +00:00
|
|
|
|
private readonly DiscordSocketClient _client;
|
2017-05-27 08:19:27 +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)]
|
2017-07-27 16:43:15 +00:00
|
|
|
|
public async Task Acro(int submissionTime = 30)
|
2016-12-22 10:39:19 +00:00
|
|
|
|
{
|
2017-07-27 16:43:15 +00:00
|
|
|
|
if (submissionTime < 10 || submissionTime > 120)
|
2017-02-18 13:13:05 +00:00
|
|
|
|
return;
|
2017-01-01 12:28:35 +00:00
|
|
|
|
var channel = (ITextChannel)Context.Channel;
|
2016-12-22 10:39:19 +00:00
|
|
|
|
|
2017-07-27 16:43:15 +00:00
|
|
|
|
var game = new Acrophobia(submissionTime);
|
2017-10-04 22:51:12 +00:00
|
|
|
|
if (_service.AcrophobiaGames.TryAdd(channel.Id, game))
|
2016-12-22 10:39:19 +00:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2017-07-27 16:43:15 +00:00
|
|
|
|
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
|
|
|
|
|
{
|
2017-07-27 16:43:15 +00:00
|
|
|
|
_client.MessageReceived -= _client_MessageReceived;
|
2017-10-04 22:51:12 +00:00
|
|
|
|
_service.AcrophobiaGames.TryRemove(channel.Id, out game);
|
2017-07-27 16:43:15 +00:00
|
|
|
|
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
|
|
|
|
|
2017-07-27 16:43:15 +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
|
|
|
|
|
2017-07-27 16:43:15 +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
|
|
|
|
|
2017-07-27 16:43:15 +00:00
|
|
|
|
return Task.CompletedTask;
|
2016-12-22 10:39:19 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-27 16:43:15 +00:00
|
|
|
|
private Task Game_OnStarted(Acrophobia game)
|
2016-12-22 10:39:19 +00:00
|
|
|
|
{
|
2017-07-27 16:43:15 +00:00
|
|
|
|
var embed = new EmbedBuilder().WithOkColor()
|
2017-02-19 14:18:40 +00:00
|
|
|
|
.WithTitle(GetText("acrophobia"))
|
2017-07-27 16:43:15 +00:00
|
|
|
|
.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
|
|
|
|
|
2017-07-27 16:43:15 +00:00
|
|
|
|
return Context.Channel.EmbedAsync(embed);
|
2016-12-22 10:39:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-07-27 16:43:15 +00:00
|
|
|
|
private Task Game_OnUserVoted(string user)
|
2016-12-22 10:39:19 +00:00
|
|
|
|
{
|
2017-07-27 16:43:15 +00:00
|
|
|
|
return Context.Channel.SendConfirmAsync(
|
|
|
|
|
GetText("acrophobia"),
|
|
|
|
|
GetText("acro_vote_cast", Format.Bold(user)));
|
|
|
|
|
}
|
2016-12-22 10:39:19 +00:00
|
|
|
|
|
2017-07-27 16:43:15 +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
|
|
|
|
{
|
2017-07-27 16:43:15 +00:00
|
|
|
|
await Context.Channel.SendErrorAsync(GetText("acrophobia"), GetText("acro_ended_no_sub"));
|
2016-12-22 10:39:19 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2017-07-27 16:43:15 +00:00
|
|
|
|
if (submissions.Length == 1)
|
2016-12-22 10:39:19 +00:00
|
|
|
|
{
|
2017-07-27 16:43:15 +00:00
|
|
|
|
await Context.Channel.EmbedAsync(new EmbedBuilder().WithOkColor()
|
2017-02-19 14:18:40 +00:00
|
|
|
|
.WithDescription(
|
|
|
|
|
GetText("acro_winner_only",
|
2017-07-27 16:43:15 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2017-07-27 16:43:15 +00:00
|
|
|
|
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")));
|
2017-06-05 22:46:58 +00:00
|
|
|
|
|
2017-07-27 16:43:15 +00:00
|
|
|
|
await Context.Channel.EmbedAsync(embed).ConfigureAwait(false);
|
2016-12-22 10:39:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-07-27 16:43:15 +00:00
|
|
|
|
private async Task Game_OnEnded(Acrophobia game, ImmutableArray<KeyValuePair<AcrophobiaUser, int>> votes)
|
2016-12-22 10:39:19 +00:00
|
|
|
|
{
|
2017-07-27 16:43:15 +00:00
|
|
|
|
if (!votes.Any() || votes.All(x => x.Value == 0))
|
2016-12-22 10:39:19 +00:00
|
|
|
|
{
|
2017-07-27 16:43:15 +00:00
|
|
|
|
await Context.Channel.SendErrorAsync(GetText("acrophobia"), GetText("acro_no_votes_cast")).ConfigureAwait(false);
|
2016-12-22 10:39:19 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2017-07-27 16:43:15 +00:00
|
|
|
|
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"))
|
2017-07-27 16:43:15 +00:00
|
|
|
|
.WithDescription(GetText("acro_winner", Format.Bold(winner.Key.UserName),
|
2017-02-19 14:18:40 +00:00
|
|
|
|
Format.Bold(winner.Value.ToString())))
|
2017-07-27 16:43:15 +00:00
|
|
|
|
.WithFooter(efb => efb.WithText(winner.Key.Input));
|
2016-12-22 10:39:19 +00:00
|
|
|
|
|
2017-07-27 16:43:15 +00:00
|
|
|
|
await Context.Channel.EmbedAsync(embed).ConfigureAwait(false);
|
2016-12-22 10:39:19 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|