2017-07-17 19:42:36 +00:00
|
|
|
|
using System;
|
2016-02-02 11:23:58 +00:00
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2016-03-02 05:00:44 +00:00
|
|
|
|
using System.Text;
|
2016-02-02 11:23:58 +00:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2017-07-17 19:42:36 +00:00
|
|
|
|
using Discord;
|
|
|
|
|
using Discord.Net;
|
|
|
|
|
using Discord.WebSocket;
|
|
|
|
|
using NadekoBot.Extensions;
|
2017-10-13 04:14:54 +00:00
|
|
|
|
using NadekoBot.Core.Services;
|
|
|
|
|
using NadekoBot.Core.Services.Impl;
|
2017-07-17 19:42:36 +00:00
|
|
|
|
using NLog;
|
2016-02-02 11:23:58 +00:00
|
|
|
|
|
2017-07-17 19:42:36 +00:00
|
|
|
|
namespace NadekoBot.Modules.Games.Common.Trivia
|
2016-03-26 02:25:03 +00:00
|
|
|
|
{
|
2016-08-16 13:29:48 +00:00
|
|
|
|
public class TriviaGame
|
2016-03-26 02:25:03 +00:00
|
|
|
|
{
|
2016-08-16 13:29:48 +00:00
|
|
|
|
private readonly SemaphoreSlim _guessLock = new SemaphoreSlim(1, 1);
|
2017-02-23 22:30:38 +00:00
|
|
|
|
private readonly Logger _log;
|
2017-05-27 08:19:27 +00:00
|
|
|
|
private readonly NadekoStrings _strings;
|
2017-06-19 13:42:10 +00:00
|
|
|
|
private readonly DiscordSocketClient _client;
|
2017-07-20 03:10:39 +00:00
|
|
|
|
private readonly IBotConfigProvider _bc;
|
2017-05-29 04:13:22 +00:00
|
|
|
|
private readonly CurrencyService _cs;
|
2016-02-02 11:23:58 +00:00
|
|
|
|
|
2017-02-23 22:30:38 +00:00
|
|
|
|
public IGuild Guild { get; }
|
|
|
|
|
public ITextChannel Channel { get; }
|
2016-02-02 11:23:58 +00:00
|
|
|
|
|
2017-09-10 22:21:14 +00:00
|
|
|
|
private readonly int _questionDurationMiliseconds = 30000;
|
|
|
|
|
private readonly int _hintTimeoutMiliseconds = 6000;
|
2017-02-23 22:30:38 +00:00
|
|
|
|
public bool ShowHints { get; }
|
2017-03-23 11:09:15 +00:00
|
|
|
|
public bool IsPokemon { get; }
|
2017-09-10 22:21:14 +00:00
|
|
|
|
private CancellationTokenSource _triviaCancelSource;
|
2016-02-02 11:23:58 +00:00
|
|
|
|
|
|
|
|
|
public TriviaQuestion CurrentQuestion { get; private set; }
|
2017-02-23 22:30:38 +00:00
|
|
|
|
public HashSet<TriviaQuestion> OldQuestions { get; } = new HashSet<TriviaQuestion>();
|
2016-02-02 11:23:58 +00:00
|
|
|
|
|
2016-08-16 13:29:48 +00:00
|
|
|
|
public ConcurrentDictionary<IGuildUser, int> Users { get; } = new ConcurrentDictionary<IGuildUser, int>();
|
2016-02-02 11:23:58 +00:00
|
|
|
|
|
2017-02-23 22:30:38 +00:00
|
|
|
|
public bool GameActive { get; private set; }
|
2016-02-02 11:23:58 +00:00
|
|
|
|
public bool ShouldStopGame { get; private set; }
|
|
|
|
|
|
2017-02-23 22:30:38 +00:00
|
|
|
|
public int WinRequirement { get; }
|
2016-02-02 11:23:58 +00:00
|
|
|
|
|
2017-07-20 03:10:39 +00:00
|
|
|
|
public TriviaGame(NadekoStrings strings, DiscordSocketClient client, IBotConfigProvider bc,
|
2017-05-29 04:13:22 +00:00
|
|
|
|
CurrencyService cs, IGuild guild, ITextChannel channel,
|
2017-05-27 08:19:27 +00:00
|
|
|
|
bool showHints, int winReq, bool isPokemon)
|
2016-03-26 02:25:03 +00:00
|
|
|
|
{
|
2017-02-23 22:30:38 +00:00
|
|
|
|
_log = LogManager.GetCurrentClassLogger();
|
2017-05-27 08:19:27 +00:00
|
|
|
|
_strings = strings;
|
|
|
|
|
_client = client;
|
|
|
|
|
_bc = bc;
|
2017-05-29 04:13:22 +00:00
|
|
|
|
_cs = cs;
|
2016-12-25 12:09:22 +00:00
|
|
|
|
|
2017-02-23 22:30:38 +00:00
|
|
|
|
ShowHints = showHints;
|
|
|
|
|
Guild = guild;
|
|
|
|
|
Channel = channel;
|
|
|
|
|
WinRequirement = winReq;
|
2017-03-23 11:09:15 +00:00
|
|
|
|
IsPokemon = isPokemon;
|
2016-02-02 11:23:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-02-23 22:30:38 +00:00
|
|
|
|
private string GetText(string key, params object[] replacements) =>
|
2017-05-27 08:19:27 +00:00
|
|
|
|
_strings.GetText(key,
|
|
|
|
|
Channel.GuildId,
|
2017-02-23 22:30:38 +00:00
|
|
|
|
typeof(Games).Name.ToLowerInvariant(),
|
|
|
|
|
replacements);
|
|
|
|
|
|
2016-12-25 12:09:22 +00:00
|
|
|
|
public async Task StartGame()
|
2016-03-26 02:25:03 +00:00
|
|
|
|
{
|
|
|
|
|
while (!ShouldStopGame)
|
|
|
|
|
{
|
2016-02-02 11:23:58 +00:00
|
|
|
|
// reset the cancellation source
|
2017-09-10 22:21:14 +00:00
|
|
|
|
_triviaCancelSource = new CancellationTokenSource();
|
2016-12-25 12:09:22 +00:00
|
|
|
|
|
2016-02-02 11:23:58 +00:00
|
|
|
|
// load question
|
2017-03-23 11:09:15 +00:00
|
|
|
|
CurrentQuestion = TriviaQuestionPool.Instance.GetRandomQuestion(OldQuestions, IsPokemon);
|
2017-02-23 22:30:38 +00:00
|
|
|
|
if (string.IsNullOrWhiteSpace(CurrentQuestion?.Answer) || string.IsNullOrWhiteSpace(CurrentQuestion.Question))
|
2016-03-26 02:25:03 +00:00
|
|
|
|
{
|
2017-02-23 22:30:38 +00:00
|
|
|
|
await Channel.SendErrorAsync(GetText("trivia_game"), GetText("failed_loading_question")).ConfigureAwait(false);
|
2016-02-02 11:23:58 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2017-02-23 22:30:38 +00:00
|
|
|
|
OldQuestions.Add(CurrentQuestion); //add it to exclusion list so it doesn't show up again
|
2016-12-25 12:09:22 +00:00
|
|
|
|
|
|
|
|
|
EmbedBuilder questionEmbed;
|
|
|
|
|
IUserMessage questionMessage;
|
|
|
|
|
try
|
2016-10-12 21:41:31 +00:00
|
|
|
|
{
|
2016-12-25 12:09:22 +00:00
|
|
|
|
questionEmbed = new EmbedBuilder().WithOkColor()
|
2017-02-23 22:30:38 +00:00
|
|
|
|
.WithTitle(GetText("trivia_game"))
|
|
|
|
|
.AddField(eab => eab.WithName(GetText("category")).WithValue(CurrentQuestion.Category))
|
2017-07-07 21:22:34 +00:00
|
|
|
|
.AddField(eab => eab.WithName(GetText("question")).WithValue(CurrentQuestion.Question));
|
|
|
|
|
if (Uri.IsWellFormedUriString(CurrentQuestion.ImageUrl, UriKind.Absolute))
|
|
|
|
|
questionEmbed.WithImageUrl(CurrentQuestion.ImageUrl);
|
2016-12-25 12:09:22 +00:00
|
|
|
|
|
2017-02-23 22:30:38 +00:00
|
|
|
|
questionMessage = await Channel.EmbedAsync(questionEmbed).ConfigureAwait(false);
|
2016-12-25 12:09:22 +00:00
|
|
|
|
}
|
2017-07-07 21:22:34 +00:00
|
|
|
|
catch (HttpException ex) when (ex.HttpCode == System.Net.HttpStatusCode.NotFound ||
|
2017-01-28 23:38:09 +00:00
|
|
|
|
ex.HttpCode == System.Net.HttpStatusCode.Forbidden ||
|
|
|
|
|
ex.HttpCode == System.Net.HttpStatusCode.BadRequest)
|
2016-12-25 12:09:22 +00:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_log.Warn(ex);
|
|
|
|
|
await Task.Delay(2000).ConfigureAwait(false);
|
|
|
|
|
continue;
|
2016-10-12 21:41:31 +00:00
|
|
|
|
}
|
2016-02-02 11:23:58 +00:00
|
|
|
|
|
|
|
|
|
//receive messages
|
2016-03-26 02:25:03 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2017-05-27 08:19:27 +00:00
|
|
|
|
_client.MessageReceived += PotentialGuess;
|
2016-02-02 11:23:58 +00:00
|
|
|
|
|
2016-12-25 12:09:22 +00:00
|
|
|
|
//allow people to guess
|
|
|
|
|
GameActive = true;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
//hint
|
2017-09-10 22:21:14 +00:00
|
|
|
|
await Task.Delay(_hintTimeoutMiliseconds, _triviaCancelSource.Token).ConfigureAwait(false);
|
2016-12-25 12:09:22 +00:00
|
|
|
|
if (ShowHints)
|
|
|
|
|
try
|
|
|
|
|
{
|
2017-01-03 15:02:02 +00:00
|
|
|
|
await questionMessage.ModifyAsync(m => m.Embed = questionEmbed.WithFooter(efb => efb.WithText(CurrentQuestion.GetHint())).Build())
|
2016-12-25 12:09:22 +00:00
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
}
|
2017-01-28 23:38:09 +00:00
|
|
|
|
catch (HttpException ex) when (ex.HttpCode == System.Net.HttpStatusCode.NotFound || ex.HttpCode == System.Net.HttpStatusCode.Forbidden)
|
2016-12-25 12:09:22 +00:00
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex) { _log.Warn(ex); }
|
|
|
|
|
|
|
|
|
|
//timeout
|
2017-09-10 22:21:14 +00:00
|
|
|
|
await Task.Delay(_questionDurationMiliseconds - _hintTimeoutMiliseconds, _triviaCancelSource.Token).ConfigureAwait(false);
|
2016-02-02 11:23:58 +00:00
|
|
|
|
|
2016-12-25 12:09:22 +00:00
|
|
|
|
}
|
|
|
|
|
catch (TaskCanceledException) { } //means someone guessed the answer
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
GameActive = false;
|
2017-05-27 08:19:27 +00:00
|
|
|
|
_client.MessageReceived -= PotentialGuess;
|
2016-03-26 02:25:03 +00:00
|
|
|
|
}
|
2017-09-10 22:21:14 +00:00
|
|
|
|
if (!_triviaCancelSource.IsCancellationRequested)
|
2017-03-23 11:09:15 +00:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2017-07-07 21:22:34 +00:00
|
|
|
|
var embed = new EmbedBuilder().WithErrorColor()
|
2017-03-23 11:09:15 +00:00
|
|
|
|
.WithTitle(GetText("trivia_game"))
|
2017-07-07 21:22:34 +00:00
|
|
|
|
.WithDescription(GetText("trivia_times_up", Format.Bold(CurrentQuestion.Answer)));
|
|
|
|
|
if (Uri.IsWellFormedUriString(CurrentQuestion.AnswerImageUrl, UriKind.Absolute))
|
|
|
|
|
embed.WithImageUrl(CurrentQuestion.AnswerImageUrl);
|
|
|
|
|
|
|
|
|
|
await Channel.EmbedAsync(embed).ConfigureAwait(false);
|
2017-03-23 11:09:15 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_log.Warn(ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-03-25 08:32:04 +00:00
|
|
|
|
await Task.Delay(5000).ConfigureAwait(false);
|
2016-02-02 11:23:58 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-25 12:09:22 +00:00
|
|
|
|
public async Task EnsureStopped()
|
2016-03-26 02:25:03 +00:00
|
|
|
|
{
|
2016-02-23 07:33:16 +00:00
|
|
|
|
ShouldStopGame = true;
|
2016-12-25 12:09:22 +00:00
|
|
|
|
|
2017-02-23 22:30:38 +00:00
|
|
|
|
await Channel.EmbedAsync(new EmbedBuilder().WithOkColor()
|
2016-12-25 12:09:22 +00:00
|
|
|
|
.WithAuthor(eab => eab.WithName("Trivia Game Ended"))
|
|
|
|
|
.WithTitle("Final Results")
|
2016-12-31 16:34:21 +00:00
|
|
|
|
.WithDescription(GetLeaderboard())).ConfigureAwait(false);
|
2016-02-02 11:23:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-03-26 02:25:03 +00:00
|
|
|
|
public async Task StopGame()
|
|
|
|
|
{
|
2016-12-25 12:09:22 +00:00
|
|
|
|
var old = ShouldStopGame;
|
2016-02-02 11:23:58 +00:00
|
|
|
|
ShouldStopGame = true;
|
2016-12-25 12:09:22 +00:00
|
|
|
|
if (!old)
|
2017-02-23 22:30:38 +00:00
|
|
|
|
try { await Channel.SendConfirmAsync(GetText("trivia_game"), GetText("trivia_stopping")).ConfigureAwait(false); } catch (Exception ex) { _log.Warn(ex); }
|
2016-02-02 11:23:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-06-15 23:55:14 +00:00
|
|
|
|
private Task PotentialGuess(SocketMessage imsg)
|
2016-03-26 02:25:03 +00:00
|
|
|
|
{
|
2017-06-05 22:46:58 +00:00
|
|
|
|
var _ = Task.Run(async () =>
|
2016-03-26 02:25:03 +00:00
|
|
|
|
{
|
2017-06-05 22:46:58 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (imsg.Author.IsBot)
|
|
|
|
|
return;
|
2016-12-28 06:56:44 +00:00
|
|
|
|
|
2017-06-05 22:46:58 +00:00
|
|
|
|
var umsg = imsg as SocketUserMessage;
|
2016-12-28 06:56:44 +00:00
|
|
|
|
|
2017-06-05 22:46:58 +00:00
|
|
|
|
var textChannel = umsg?.Channel as ITextChannel;
|
|
|
|
|
if (textChannel == null || textChannel.Guild != Guild)
|
|
|
|
|
return;
|
2016-09-04 15:23:58 +00:00
|
|
|
|
|
2017-06-05 22:46:58 +00:00
|
|
|
|
var guildUser = (IGuildUser)umsg.Author;
|
2016-09-04 15:23:58 +00:00
|
|
|
|
|
2017-06-05 22:46:58 +00:00
|
|
|
|
var guess = false;
|
|
|
|
|
await _guessLock.WaitAsync().ConfigureAwait(false);
|
|
|
|
|
try
|
2016-03-26 02:25:03 +00:00
|
|
|
|
{
|
2017-09-10 22:21:14 +00:00
|
|
|
|
if (GameActive && CurrentQuestion.IsAnswerCorrect(umsg.Content) && !_triviaCancelSource.IsCancellationRequested)
|
2017-06-05 22:46:58 +00:00
|
|
|
|
{
|
|
|
|
|
Users.AddOrUpdate(guildUser, 1, (gu, old) => ++old);
|
|
|
|
|
guess = true;
|
|
|
|
|
}
|
2016-02-26 08:46:50 +00:00
|
|
|
|
}
|
2017-06-05 22:46:58 +00:00
|
|
|
|
finally { _guessLock.Release(); }
|
|
|
|
|
if (!guess) return;
|
2017-09-10 22:21:14 +00:00
|
|
|
|
_triviaCancelSource.Cancel();
|
2016-12-25 12:09:22 +00:00
|
|
|
|
|
2016-12-28 05:31:39 +00:00
|
|
|
|
|
2017-06-05 22:46:58 +00:00
|
|
|
|
if (Users[guildUser] == WinRequirement)
|
2017-02-23 22:30:38 +00:00
|
|
|
|
{
|
2017-06-05 22:46:58 +00:00
|
|
|
|
ShouldStopGame = true;
|
|
|
|
|
try
|
|
|
|
|
{
|
2017-07-07 21:22:34 +00:00
|
|
|
|
var embedS = new EmbedBuilder().WithOkColor()
|
2017-06-05 22:46:58 +00:00
|
|
|
|
.WithTitle(GetText("trivia_game"))
|
|
|
|
|
.WithDescription(GetText("trivia_win",
|
|
|
|
|
guildUser.Mention,
|
2017-07-07 21:22:34 +00:00
|
|
|
|
Format.Bold(CurrentQuestion.Answer)));
|
|
|
|
|
if (Uri.IsWellFormedUriString(CurrentQuestion.AnswerImageUrl, UriKind.Absolute))
|
|
|
|
|
embedS.WithImageUrl(CurrentQuestion.AnswerImageUrl);
|
|
|
|
|
await Channel.EmbedAsync(embedS).ConfigureAwait(false);
|
2017-06-05 22:46:58 +00:00
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
// ignored
|
|
|
|
|
}
|
2017-07-20 03:10:39 +00:00
|
|
|
|
var reward = _bc.BotConfig.TriviaCurrencyReward;
|
2017-06-05 22:46:58 +00:00
|
|
|
|
if (reward > 0)
|
|
|
|
|
await _cs.AddAsync(guildUser, "Won trivia", reward, true).ConfigureAwait(false);
|
|
|
|
|
return;
|
2017-02-23 22:30:38 +00:00
|
|
|
|
}
|
2017-07-07 21:22:34 +00:00
|
|
|
|
var embed = new EmbedBuilder().WithOkColor()
|
2017-06-05 22:46:58 +00:00
|
|
|
|
.WithTitle(GetText("trivia_game"))
|
2017-07-07 21:22:34 +00:00
|
|
|
|
.WithDescription(GetText("trivia_guess", guildUser.Mention, Format.Bold(CurrentQuestion.Answer)));
|
|
|
|
|
if (Uri.IsWellFormedUriString(CurrentQuestion.AnswerImageUrl, UriKind.Absolute))
|
|
|
|
|
embed.WithImageUrl(CurrentQuestion.AnswerImageUrl);
|
|
|
|
|
await Channel.EmbedAsync(embed).ConfigureAwait(false);
|
2017-06-05 22:46:58 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex) { _log.Warn(ex); }
|
|
|
|
|
});
|
2017-06-15 23:55:14 +00:00
|
|
|
|
return Task.CompletedTask;
|
2016-02-02 11:23:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-03-26 02:25:03 +00:00
|
|
|
|
public string GetLeaderboard()
|
|
|
|
|
{
|
2016-03-02 05:00:44 +00:00
|
|
|
|
if (Users.Count == 0)
|
2017-02-23 22:30:38 +00:00
|
|
|
|
return GetText("no_results");
|
2016-02-02 11:23:58 +00:00
|
|
|
|
|
2016-03-02 05:00:44 +00:00
|
|
|
|
var sb = new StringBuilder();
|
2016-02-02 11:23:58 +00:00
|
|
|
|
|
2016-10-12 21:41:31 +00:00
|
|
|
|
foreach (var kvp in Users.OrderByDescending(kvp => kvp.Value))
|
2016-03-26 02:25:03 +00:00
|
|
|
|
{
|
2017-02-23 22:30:38 +00:00
|
|
|
|
sb.AppendLine(GetText("trivia_points", Format.Bold(kvp.Key.ToString()), kvp.Value).SnPl(kvp.Value));
|
2016-02-02 11:23:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-03-02 05:00:44 +00:00
|
|
|
|
return sb.ToString();
|
2016-02-02 11:23:58 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-12-25 12:09:22 +00:00
|
|
|
|
}
|