Acrophobia completely rewritten. Works the same as before, only much more maintainable. It won't repost itself after 10 messages anymore though.

This commit is contained in:
Master Kwoth 2017-07-27 18:43:15 +02:00
parent bf7585cd83
commit fece28b66b
4 changed files with 400 additions and 235 deletions

View File

@ -14,6 +14,7 @@ using NadekoBot.Common;
using NadekoBot.Common.Attributes;
using NadekoBot.Common.Collections;
using NadekoBot.Services.Impl;
using NadekoBot.Modules.Games.Common.Acrophobia;
namespace NadekoBot.Modules.Games
{
@ -25,7 +26,7 @@ namespace NadekoBot.Modules.Games
private readonly DiscordSocketClient _client;
//channelId, game
public static ConcurrentDictionary<ulong, AcrophobiaGame> AcrophobiaGames { get; } = new ConcurrentDictionary<ulong, AcrophobiaGame>();
public static ConcurrentDictionary<ulong, Acrophobia> AcrophobiaGames { get; } = new ConcurrentDictionary<ulong, Acrophobia>();
public AcropobiaCommands(DiscordSocketClient client)
{
@ -34,291 +35,251 @@ namespace NadekoBot.Modules.Games
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
public async Task Acro(int time = 60)
public async Task Acro(int submissionTime = 30)
{
if (time < 10 || time > 120)
if (submissionTime < 10 || submissionTime > 120)
return;
var channel = (ITextChannel)Context.Channel;
var game = new AcrophobiaGame(_client, _strings, channel, time);
var game = new Acrophobia(submissionTime);
if (AcrophobiaGames.TryAdd(channel.Id, game))
{
try
{
await game.Run();
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);
}
finally
{
game.EnsureStopped();
_client.MessageReceived -= _client_MessageReceived;
AcrophobiaGames.TryRemove(channel.Id, out game);
game.Dispose();
}
}
else
{
await ReplyErrorLocalized("acro_running").ConfigureAwait(false);
}
}
}
public enum AcroPhase
{
Submitting,
Idle, // used to wait for some other actions while transitioning through phases
Voting
}
//todo 85 Isolate, this shouldn't print or anything like that.
public class AcrophobiaGame
{
private readonly ITextChannel _channel;
private readonly int _time;
private readonly NadekoRandom _rng;
private readonly ImmutableArray<char> _startingLetters;
private readonly CancellationTokenSource _source;
private AcroPhase phase { get; set; } = AcroPhase.Submitting;
private readonly ConcurrentDictionary<string, IGuildUser> _submissions = new ConcurrentDictionary<string, IGuildUser>();
public IReadOnlyDictionary<string, IGuildUser> Submissions => _submissions;
private readonly ConcurrentHashSet<ulong> _usersWhoSubmitted = new ConcurrentHashSet<ulong>();
private readonly ConcurrentHashSet<ulong> _usersWhoVoted = new ConcurrentHashSet<ulong>();
private int _spamCount;
//text, votes
private readonly ConcurrentDictionary<string, int> _votes = new ConcurrentDictionary<string, int>();
private readonly Logger _log;
private readonly DiscordSocketClient _client;
private readonly NadekoStrings _strings;
public AcrophobiaGame(DiscordSocketClient client, NadekoStrings strings, ITextChannel channel, int time)
{
_log = LogManager.GetCurrentClassLogger();
_client = client;
_strings = strings;
_channel = channel;
_time = time;
_source = new CancellationTokenSource();
_rng = new NadekoRandom();
var wordCount = _rng.Next(3, 6);
var lettersArr = new char[wordCount];
for (int i = 0; i < wordCount; i++)
Task _client_MessageReceived(SocketMessage msg)
{
var randChar = (char)_rng.Next(65, 91);
lettersArr[i] = randChar == 'X' ? (char)_rng.Next(65, 88) : randChar;
if (msg.Channel.Id != Context.Channel.Id)
return Task.CompletedTask;
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 { }
});
return Task.CompletedTask;
}
_startingLetters = lettersArr.ToImmutableArray();
}
private EmbedBuilder GetEmbed()
private Task Game_OnStarted(Acrophobia game)
{
var i = 0;
return phase == AcroPhase.Submitting
? new EmbedBuilder().WithOkColor()
var embed = new EmbedBuilder().WithOkColor()
.WithTitle(GetText("acrophobia"))
.WithDescription(GetText("acro_started", Format.Bold(string.Join(".", _startingLetters))))
.WithFooter(efb => efb.WithText(GetText("acro_started_footer", _time)))
.WithDescription(GetText("acro_started", Format.Bold(string.Join(".", game.StartingLetters))))
.WithFooter(efb => efb.WithText(GetText("acro_started_footer", game.SubmissionPhaseLength)));
: new EmbedBuilder()
.WithOkColor()
.WithTitle(GetText("acrophobia") + " - " + GetText("submissions_closed"))
.WithDescription(GetText("acro_nym_was", Format.Bold(string.Join(".", _startingLetters)) + "\n" +
$@"--
{_submissions.Aggregate("",(agg, cur) => agg + $"`{++i}.` **{cur.Key.ToLowerInvariant().ToTitleCase()}**\n")}
--"))
.WithFooter(efb => efb.WithText(GetText("acro_vote")));
return Context.Channel.EmbedAsync(embed);
}
public async Task Run()
private Task Game_OnUserVoted(string user)
{
_client.MessageReceived += PotentialAcro;
var embed = GetEmbed();
return Context.Channel.SendConfirmAsync(
GetText("acrophobia"),
GetText("acro_vote_cast", Format.Bold(user)));
}
//SUBMISSIONS PHASE
await _channel.EmbedAsync(embed).ConfigureAwait(false);
try
{
await Task.Delay(_time * 1000, _source.Token).ConfigureAwait(false);
phase = AcroPhase.Idle;
}
catch (OperationCanceledException)
private async Task Game_OnVotingStarted(Acrophobia game, ImmutableArray<KeyValuePair<AcrophobiaUser, int>> submissions)
{
if (submissions.Length == 0)
{
await Context.Channel.SendErrorAsync(GetText("acrophobia"), GetText("acro_ended_no_sub"));
return;
}
//var i = 0;
if (_submissions.Count == 0)
if (submissions.Length == 1)
{
await _channel.SendErrorAsync(GetText("acrophobia"), GetText("acro_ended_no_sub"));
return;
}
if (_submissions.Count == 1)
{
await _channel.EmbedAsync(new EmbedBuilder().WithOkColor()
await Context.Channel.EmbedAsync(new EmbedBuilder().WithOkColor()
.WithDescription(
GetText("acro_winner_only",
Format.Bold(_submissions.First().Value.ToString())))
.WithFooter(efb => efb.WithText(_submissions.First().Key.ToLowerInvariant().ToTitleCase())))
Format.Bold(submissions.First().Key.UserName)))
.WithFooter(efb => efb.WithText(submissions.First().Key.Input)))
.ConfigureAwait(false);
return;
}
var submissionClosedEmbed = GetEmbed();
await _channel.EmbedAsync(submissionClosedEmbed).ConfigureAwait(false);
//VOTING PHASE
phase = AcroPhase.Voting;
try
{
//30 secondds for voting
await Task.Delay(30000, _source.Token).ConfigureAwait(false);
phase = AcroPhase.Idle;
}
catch (OperationCanceledException)
{
return;
}
await End().ConfigureAwait(false);
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);
}
private Task PotentialAcro(SocketMessage arg)
private async Task Game_OnEnded(Acrophobia game, ImmutableArray<KeyValuePair<AcrophobiaUser, int>> votes)
{
var _ = Task.Run(async () =>
if (!votes.Any() || votes.All(x => x.Value == 0))
{
try
{
var msg = arg as SocketUserMessage;
if (msg == null || msg.Author.IsBot || msg.Channel.Id != _channel.Id)
return;
++_spamCount;
var guildUser = (IGuildUser)msg.Author;
var input = msg.Content.ToUpperInvariant().Trim();
if (phase == AcroPhase.Submitting)
{
if (_spamCount > 10)
{
_spamCount = 0;
try { await _channel.EmbedAsync(GetEmbed()).ConfigureAwait(false); }
catch { }
}
var inputWords = input.Split(' '); //get all words
if (inputWords.Length != _startingLetters.Length) // number of words must be the same as the number of the starting letters
return;
for (int i = 0; i < _startingLetters.Length; i++)
{
var letter = _startingLetters[i];
if (!inputWords[i].StartsWith(letter.ToString())) // all first letters must match
return;
}
if (!_usersWhoSubmitted.Add(guildUser.Id))
return;
//try adding it to the list of answers
if (!_submissions.TryAdd(input, guildUser))
{
_usersWhoSubmitted.TryRemove(guildUser.Id);
return;
}
// all good. valid input. answer recorded
await _channel.SendConfirmAsync(GetText("acrophobia"),
GetText("acro_submit", guildUser.Mention,
_submissions.Count));
try
{
await msg.DeleteAsync();
}
catch
{
await msg.DeleteAsync(); //try twice
}
}
else if (phase == AcroPhase.Voting)
{
if (_spamCount > 10)
{
_spamCount = 0;
try { await _channel.EmbedAsync(GetEmbed()).ConfigureAwait(false); }
catch { }
}
//if (submissions.TryGetValue(input, out usr) && usr.Id != guildUser.Id)
//{
// if (!usersWhoVoted.Add(guildUser.Id))
// return;
// votes.AddOrUpdate(input, 1, (key, old) => ++old);
// await channel.SendConfirmAsync("Acrophobia", $"{guildUser.Mention} cast their vote!").ConfigureAwait(false);
// await msg.DeleteAsync().ConfigureAwait(false);
// return;
//}
int num;
if (int.TryParse(input, out num) && num > 0 && num <= _submissions.Count)
{
var kvp = _submissions.Skip(num - 1).First();
var usr = kvp.Value;
//can't vote for yourself, can't vote multiple times
if (usr.Id == guildUser.Id || !_usersWhoVoted.Add(guildUser.Id))
return;
_votes.AddOrUpdate(kvp.Key, 1, (key, old) => ++old);
await _channel.SendConfirmAsync(GetText("acrophobia"),
GetText("acro_vote_cast", Format.Bold(guildUser.ToString()))).ConfigureAwait(false);
await msg.DeleteAsync().ConfigureAwait(false);
}
}
}
catch (Exception ex)
{
_log.Warn(ex);
}
});
return Task.CompletedTask;
}
public async Task End()
{
if (!_votes.Any())
{
await _channel.SendErrorAsync(GetText("acrophobia"), GetText("acro_no_votes_cast")).ConfigureAwait(false);
await Context.Channel.SendErrorAsync(GetText("acrophobia"), GetText("acro_no_votes_cast")).ConfigureAwait(false);
return;
}
var table = _votes.OrderByDescending(v => v.Value);
var table = votes.OrderByDescending(v => v.Value);
var winner = table.First();
var embed = new EmbedBuilder().WithOkColor()
.WithTitle(GetText("acrophobia"))
.WithDescription(GetText("acro_winner", Format.Bold(_submissions[winner.Key].ToString()),
.WithDescription(GetText("acro_winner", Format.Bold(winner.Key.UserName),
Format.Bold(winner.Value.ToString())))
.WithFooter(efb => efb.WithText(winner.Key.ToLowerInvariant().ToTitleCase()));
.WithFooter(efb => efb.WithText(winner.Key.Input));
await _channel.EmbedAsync(embed).ConfigureAwait(false);
await Context.Channel.EmbedAsync(embed).ConfigureAwait(false);
}
public void EnsureStopped()
{
_client.MessageReceived -= PotentialAcro;
if (!_source.IsCancellationRequested)
_source.Cancel();
}
private string GetText(string key, params object[] replacements)
=> _strings.GetText(key,
_channel.Guild.Id,
typeof(Games).Name.ToLowerInvariant(),
replacements);
}
//public enum AcroPhase
//{
// Submitting,
// Idle, // used to wait for some other actions while transitioning through phases
// Voting
//}
////todo 85 Isolate, this shouldn't print or anything like that.
//public class OldAcrophobiaGame
//{
// private readonly ITextChannel _channel;
// private readonly int _time;
// private readonly NadekoRandom _rng;
// private readonly ImmutableArray<char> _startingLetters;
// private readonly CancellationTokenSource _source;
// private AcroPhase phase { get; set; } = AcroPhase.Submitting;
// private readonly ConcurrentDictionary<string, IGuildUser> _submissions = new ConcurrentDictionary<string, IGuildUser>();
// public IReadOnlyDictionary<string, IGuildUser> Submissions => _submissions;
// private readonly ConcurrentHashSet<ulong> _usersWhoSubmitted = new ConcurrentHashSet<ulong>();
// private readonly ConcurrentHashSet<ulong> _usersWhoVoted = new ConcurrentHashSet<ulong>();
// private int _spamCount;
// //text, votes
// private readonly ConcurrentDictionary<string, int> _votes = new ConcurrentDictionary<string, int>();
// private readonly Logger _log;
// private readonly DiscordSocketClient _client;
// private readonly NadekoStrings _strings;
// public OldAcrophobiaGame(DiscordSocketClient client, NadekoStrings strings, ITextChannel channel, int time)
// {
// _log = LogManager.GetCurrentClassLogger();
// _client = client;
// _strings = strings;
// _channel = channel;
// _time = time;
// _source = new CancellationTokenSource();
// _rng = new NadekoRandom();
// var wordCount = _rng.Next(3, 6);
// var lettersArr = new char[wordCount];
// for (int i = 0; i < wordCount; i++)
// {
// var randChar = (char)_rng.Next(65, 91);
// lettersArr[i] = randChar == 'X' ? (char)_rng.Next(65, 88) : randChar;
// }
// _startingLetters = lettersArr.ToImmutableArray();
// }
// private Task PotentialAcro(SocketMessage arg)
// {
// var _ = Task.Run(async () =>
// {
// try
// {
// var msg = arg as SocketUserMessage;
// if (msg == null || msg.Author.IsBot || msg.Channel.Id != _channel.Id)
// return;
// ++_spamCount;
// var guildUser = (IGuildUser)msg.Author;
// var input = msg.Content.ToUpperInvariant().Trim();
// if (phase == AcroPhase.Submitting)
// {
// // all good. valid input. answer recorded
// await _channel.SendConfirmAsync(GetText("acrophobia"),
// GetText("acro_submit", guildUser.Mention,
// _submissions.Count));
// try
// {
// await msg.DeleteAsync();
// }
// catch
// {
// await msg.DeleteAsync(); //try twice
// }
// }
// else if (phase == AcroPhase.Voting)
// {
// if (_spamCount > 10)
// {
// _spamCount = 0;
// try { await _channel.EmbedAsync(GetEmbed()).ConfigureAwait(false); }
// catch { }
// }
// //if (submissions.TryGetValue(input, out usr) && usr.Id != guildUser.Id)
// //{
// // if (!usersWhoVoted.Add(guildUser.Id))
// // return;
// // votes.AddOrUpdate(input, 1, (key, old) => ++old);
// // await channel.SendConfirmAsync("Acrophobia", $"{guildUser.Mention} cast their vote!").ConfigureAwait(false);
// // await msg.DeleteAsync().ConfigureAwait(false);
// // return;
// //}
// int num;
// if (int.TryParse(input, out num) && num > 0 && num <= _submissions.Count)
// {
// var kvp = _submissions.Skip(num - 1).First();
// var usr = kvp.Value;
// //can't vote for yourself, can't vote multiple times
// if (usr.Id == guildUser.Id || !_usersWhoVoted.Add(guildUser.Id))
// return;
// _votes.AddOrUpdate(kvp.Key, 1, (key, old) => ++old);
// }
// }
// }
// catch (Exception ex)
// {
// _log.Warn(ex);
// }
// });
// return Task.CompletedTask;
// }
//}
}
}

View File

@ -0,0 +1,177 @@
using NadekoBot.Common;
using NadekoBot.Extensions;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace NadekoBot.Modules.Games.Common.Acrophobia
{
/// <summary>
/// Platform-agnostic acrophobia game
/// </summary>
public class Acrophobia : IDisposable
{
private const int VotingPhaseLength = 30;
public enum Phase
{
Submission,
Voting,
Ended
}
public enum UserInputResult
{
Submitted,
SubmissionFailed,
Voted,
VotingFailed,
Failed
}
public int SubmissionPhaseLength { get; }
public Phase CurrentPhase { get; private set; } = Phase.Submission;
public ImmutableArray<char> StartingLetters { get; private set; }
private readonly Dictionary<AcrophobiaUser, int> submissions = new Dictionary<AcrophobiaUser, int>();
private readonly SemaphoreSlim locker = new SemaphoreSlim(1, 1);
private readonly NadekoRandom _rng;
public event Func<Acrophobia, Task> OnStarted = delegate { return Task.CompletedTask; };
public event Func<Acrophobia, ImmutableArray<KeyValuePair<AcrophobiaUser, int>>, Task> OnVotingStarted = delegate { return Task.CompletedTask; };
public event Func<string, Task> OnUserVoted = delegate { return Task.CompletedTask; };
public event Func<Acrophobia, ImmutableArray<KeyValuePair<AcrophobiaUser, int>>, Task> OnEnded = delegate { return Task.CompletedTask; };
private readonly HashSet<ulong> _usersWhoVoted = new HashSet<ulong>();
public Acrophobia(int submissionPhaseLength = 30)
{
_rng = new NadekoRandom();
SubmissionPhaseLength = submissionPhaseLength;
InitializeStartingLetters();
}
public async Task Run()
{
await OnStarted(this).ConfigureAwait(false);
await Task.Delay(SubmissionPhaseLength * 1000);
await locker.WaitAsync().ConfigureAwait(false);
try
{
if (submissions.Count == 0)
{
CurrentPhase = Phase.Ended;
await OnVotingStarted(this, ImmutableArray.Create<KeyValuePair<AcrophobiaUser, int>>()).ConfigureAwait(false);
return;
}
if (submissions.Count == 1)
{
CurrentPhase = Phase.Ended;
await OnVotingStarted(this, submissions.ToArray().ToImmutableArray()).ConfigureAwait(false);
return;
}
CurrentPhase = Phase.Voting;
await OnVotingStarted(this, submissions.ToArray().ToImmutableArray()).ConfigureAwait(false);
}
finally { locker.Release(); }
await Task.Delay(VotingPhaseLength * 1000);
await locker.WaitAsync().ConfigureAwait(false);
try
{
CurrentPhase = Phase.Ended;
await OnEnded(this, submissions.ToArray().ToImmutableArray()).ConfigureAwait(false) ;
}
finally { locker.Release(); }
}
private void InitializeStartingLetters()
{
var wordCount = _rng.Next(3, 6);
var lettersArr = new char[wordCount];
for (int i = 0; i < wordCount; i++)
{
var randChar = (char)_rng.Next(65, 91);
lettersArr[i] = randChar == 'X' ? (char)_rng.Next(65, 88) : randChar;
}
StartingLetters = lettersArr.ToImmutableArray();
}
public async Task<bool> UserInput(ulong userId, string userName, string input)
{
var user = new AcrophobiaUser(userId, userName, input.ToLowerInvariant().ToTitleCase());
await locker.WaitAsync();
try
{
switch (CurrentPhase)
{
case Phase.Submission:
if (submissions.ContainsKey(user) || !IsValidAnswer(input))
break;
submissions.Add(user, 0);
return true;
case Phase.Voting:
AcrophobiaUser toVoteFor;
if (!int.TryParse(input, out var index)
|| --index < 0
|| index >= submissions.Count
|| (toVoteFor = submissions.ToArray()[index].Key).UserId == user.UserId
|| !_usersWhoVoted.Add(userId))
break;
++submissions[toVoteFor];
var _ = Task.Run(() => OnUserVoted(userName));
return true;
default:
break;
}
return false;
}
finally
{
locker.Release();
}
}
private bool IsValidAnswer(string input)
{
input = input.ToUpperInvariant();
var inputWords = input.Split(' ');
if (inputWords.Length != StartingLetters.Length) // number of words must be the same as the number of the starting letters
return false;
for (int i = 0; i < StartingLetters.Length; i++)
{
var letter = StartingLetters[i];
if (!inputWords[i].StartsWith(letter.ToString())) // all first letters must match
return false;
}
return true;
}
public void Dispose()
{
this.CurrentPhase = Phase.Ended;
OnStarted = null;
OnEnded = null;
OnUserVoted = null;
OnVotingStarted = null;
_usersWhoVoted.Clear();
submissions.Clear();
locker.Dispose();
}
}
}

View File

@ -0,0 +1,28 @@
namespace NadekoBot.Modules.Games.Common.Acrophobia
{
public class AcrophobiaUser
{
public string UserName { get; }
public ulong UserId { get; }
public string Input { get; }
public AcrophobiaUser(ulong userId, string userName, string input)
{
this.UserName = userName;
this.UserId = userId;
this.Input = input;
}
public override int GetHashCode()
{
return UserId.GetHashCode();
}
public override bool Equals(object obj)
{
return obj is AcrophobiaUser x
? x.UserId == this.UserId
: false;
}
}
}

View File

@ -75,8 +75,7 @@ namespace NadekoBot.Modules.Games
}
var imgData = _games.GetRandomCurrencyImage();
//todo 81 upload all currency images to transfer.sh and use that one as cdn
var msgToSend = GetText("planted",
Format.Bold(Context.User.ToString()),
amount + _bc.BotConfig.CurrencySign,