NadekoBot/NadekoBot.Modules.Games/Common/Trivia/TriviaGame.cs

266 lines
11 KiB
C#
Raw Normal View History

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;
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;
using NadekoBot.Services;
using NadekoBot.Services.Impl;
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
{
public class TriviaGame
2016-03-26 02:25:03 +00:00
{
private readonly SemaphoreSlim _guessLock = new SemaphoreSlim(1, 1);
private readonly Logger _log;
2017-05-27 08:19:27 +00:00
private readonly NadekoStrings _strings;
private readonly DiscordSocketClient _client;
private readonly IBotConfigProvider _bc;
private readonly CurrencyService _cs;
2016-02-02 11:23:58 +00:00
public IGuild Guild { get; }
public ITextChannel Channel { get; }
2016-02-02 11:23:58 +00:00
private readonly int _questionDurationMiliseconds = 30000;
private readonly int _hintTimeoutMiliseconds = 6000;
public bool ShowHints { get; }
2017-03-23 11:09:15 +00:00
public bool IsPokemon { get; }
private CancellationTokenSource _triviaCancelSource;
2016-02-02 11:23:58 +00:00
public TriviaQuestion CurrentQuestion { get; private set; }
public HashSet<TriviaQuestion> OldQuestions { get; } = new HashSet<TriviaQuestion>();
2016-02-02 11:23:58 +00:00
public ConcurrentDictionary<IGuildUser, int> Users { get; } = new ConcurrentDictionary<IGuildUser, int>();
2016-02-02 11:23:58 +00:00
public bool GameActive { get; private set; }
2016-02-02 11:23:58 +00:00
public bool ShouldStopGame { get; private set; }
public int WinRequirement { get; }
2016-02-02 11:23:58 +00:00
public TriviaGame(NadekoStrings strings, DiscordSocketClient client, IBotConfigProvider bc,
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
{
_log = LogManager.GetCurrentClassLogger();
2017-05-27 08:19:27 +00:00
_strings = strings;
_client = client;
_bc = bc;
_cs = cs;
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
}
private string GetText(string key, params object[] replacements) =>
2017-05-27 08:19:27 +00:00
_strings.GetText(key,
Channel.GuildId,
typeof(Games).Name.ToLowerInvariant(),
replacements);
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
_triviaCancelSource = new CancellationTokenSource();
2016-02-02 11:23:58 +00:00
// load question
2017-03-23 11:09:15 +00:00
CurrentQuestion = TriviaQuestionPool.Instance.GetRandomQuestion(OldQuestions, IsPokemon);
if (string.IsNullOrWhiteSpace(CurrentQuestion?.Answer) || string.IsNullOrWhiteSpace(CurrentQuestion.Question))
2016-03-26 02:25:03 +00:00
{
await Channel.SendErrorAsync(GetText("trivia_game"), GetText("failed_loading_question")).ConfigureAwait(false);
2016-02-02 11:23:58 +00:00
return;
}
OldQuestions.Add(CurrentQuestion); //add it to exclusion list so it doesn't show up again
EmbedBuilder questionEmbed;
IUserMessage questionMessage;
try
2016-10-12 21:41:31 +00:00
{
questionEmbed = new EmbedBuilder().WithOkColor()
.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);
questionMessage = await Channel.EmbedAsync(questionEmbed).ConfigureAwait(false);
}
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)
{
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
//allow people to guess
GameActive = true;
try
{
//hint
await Task.Delay(_hintTimeoutMiliseconds, _triviaCancelSource.Token).ConfigureAwait(false);
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())
.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)
{
break;
}
catch (Exception ex) { _log.Warn(ex); }
//timeout
await Task.Delay(_questionDurationMiliseconds - _hintTimeoutMiliseconds, _triviaCancelSource.Token).ConfigureAwait(false);
2016-02-02 11:23:58 +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
}
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);
}
}
await Task.Delay(5000).ConfigureAwait(false);
2016-02-02 11:23:58 +00:00
}
}
public async Task EnsureStopped()
2016-03-26 02:25:03 +00:00
{
ShouldStopGame = true;
await Channel.EmbedAsync(new EmbedBuilder().WithOkColor()
.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()
{
var old = ShouldStopGame;
2016-02-02 11:23:58 +00:00
ShouldStopGame = true;
if (!old)
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
{
var _ = Task.Run(async () =>
2016-03-26 02:25:03 +00:00
{
try
{
if (imsg.Author.IsBot)
return;
2016-12-28 06:56:44 +00:00
var umsg = imsg as SocketUserMessage;
2016-12-28 06:56:44 +00:00
var textChannel = umsg?.Channel as ITextChannel;
if (textChannel == null || textChannel.Guild != Guild)
return;
2016-09-04 15:23:58 +00:00
var guildUser = (IGuildUser)umsg.Author;
2016-09-04 15:23:58 +00:00
var guess = false;
await _guessLock.WaitAsync().ConfigureAwait(false);
try
2016-03-26 02:25:03 +00:00
{
if (GameActive && CurrentQuestion.IsAnswerCorrect(umsg.Content) && !_triviaCancelSource.IsCancellationRequested)
{
Users.AddOrUpdate(guildUser, 1, (gu, old) => ++old);
guess = true;
}
}
finally { _guessLock.Release(); }
if (!guess) return;
_triviaCancelSource.Cancel();
if (Users[guildUser] == WinRequirement)
{
ShouldStopGame = true;
try
{
2017-07-07 21:22:34 +00:00
var embedS = new EmbedBuilder().WithOkColor()
.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);
}
catch
{
// ignored
}
var reward = _bc.BotConfig.TriviaCurrencyReward;
if (reward > 0)
await _cs.AddAsync(guildUser, "Won trivia", reward, true).ConfigureAwait(false);
return;
}
2017-07-07 21:22:34 +00:00
var embed = new EmbedBuilder().WithOkColor()
.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);
}
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()
{
if (Users.Count == 0)
return GetText("no_results");
2016-02-02 11:23:58 +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
{
sb.AppendLine(GetText("trivia_points", Format.Bold(kvp.Key.ToString()), kvp.Value).SnPl(kvp.Value));
2016-02-02 11:23:58 +00:00
}
return sb.ToString();
2016-02-02 11:23:58 +00:00
}
}
}