From 927e98514aad2927655caebfbd5057781eed9100 Mon Sep 17 00:00:00 2001 From: Master Kwoth Date: Mon, 11 Sep 2017 00:21:14 +0200 Subject: [PATCH] Fixed exclusion list after restarts, closes #1571 --- .../Modules/Games/Common/Trivia/TriviaGame.cs | 18 ++++++------- .../Modules/Xp/Services/XpService.cs | 25 +++++++++++++++++++ .../Impl/GuildConfigRepository.cs | 2 ++ 3 files changed, 36 insertions(+), 9 deletions(-) diff --git a/src/NadekoBot/Modules/Games/Common/Trivia/TriviaGame.cs b/src/NadekoBot/Modules/Games/Common/Trivia/TriviaGame.cs index 77714d58..abafdb67 100644 --- a/src/NadekoBot/Modules/Games/Common/Trivia/TriviaGame.cs +++ b/src/NadekoBot/Modules/Games/Common/Trivia/TriviaGame.cs @@ -27,11 +27,11 @@ namespace NadekoBot.Modules.Games.Common.Trivia public IGuild Guild { get; } public ITextChannel Channel { get; } - private int questionDurationMiliseconds { get; } = 30000; - private int hintTimeoutMiliseconds { get; } = 6000; + private readonly int _questionDurationMiliseconds = 30000; + private readonly int _hintTimeoutMiliseconds = 6000; public bool ShowHints { get; } public bool IsPokemon { get; } - private CancellationTokenSource triviaCancelSource { get; set; } + private CancellationTokenSource _triviaCancelSource; public TriviaQuestion CurrentQuestion { get; private set; } public HashSet OldQuestions { get; } = new HashSet(); @@ -71,7 +71,7 @@ namespace NadekoBot.Modules.Games.Common.Trivia while (!ShouldStopGame) { // reset the cancellation source - triviaCancelSource = new CancellationTokenSource(); + _triviaCancelSource = new CancellationTokenSource(); // load question CurrentQuestion = TriviaQuestionPool.Instance.GetRandomQuestion(OldQuestions, IsPokemon); @@ -118,7 +118,7 @@ namespace NadekoBot.Modules.Games.Common.Trivia try { //hint - await Task.Delay(hintTimeoutMiliseconds, triviaCancelSource.Token).ConfigureAwait(false); + await Task.Delay(_hintTimeoutMiliseconds, _triviaCancelSource.Token).ConfigureAwait(false); if (ShowHints) try { @@ -132,7 +132,7 @@ namespace NadekoBot.Modules.Games.Common.Trivia catch (Exception ex) { _log.Warn(ex); } //timeout - await Task.Delay(questionDurationMiliseconds - hintTimeoutMiliseconds, triviaCancelSource.Token).ConfigureAwait(false); + await Task.Delay(_questionDurationMiliseconds - _hintTimeoutMiliseconds, _triviaCancelSource.Token).ConfigureAwait(false); } catch (TaskCanceledException) { } //means someone guessed the answer @@ -142,7 +142,7 @@ namespace NadekoBot.Modules.Games.Common.Trivia GameActive = false; _client.MessageReceived -= PotentialGuess; } - if (!triviaCancelSource.IsCancellationRequested) + if (!_triviaCancelSource.IsCancellationRequested) { try { @@ -202,7 +202,7 @@ namespace NadekoBot.Modules.Games.Common.Trivia await _guessLock.WaitAsync().ConfigureAwait(false); try { - if (GameActive && CurrentQuestion.IsAnswerCorrect(umsg.Content) && !triviaCancelSource.IsCancellationRequested) + if (GameActive && CurrentQuestion.IsAnswerCorrect(umsg.Content) && !_triviaCancelSource.IsCancellationRequested) { Users.AddOrUpdate(guildUser, 1, (gu, old) => ++old); guess = true; @@ -210,7 +210,7 @@ namespace NadekoBot.Modules.Games.Common.Trivia } finally { _guessLock.Release(); } if (!guess) return; - triviaCancelSource.Cancel(); + _triviaCancelSource.Cancel(); if (Users[guildUser] == WinRequirement) diff --git a/src/NadekoBot/Modules/Xp/Services/XpService.cs b/src/NadekoBot/Modules/Xp/Services/XpService.cs index 52944064..11a7aaa9 100644 --- a/src/NadekoBot/Modules/Xp/Services/XpService.cs +++ b/src/NadekoBot/Modules/Xp/Services/XpService.cs @@ -78,6 +78,31 @@ namespace NadekoBot.Modules.Xp.Services _log = LogManager.GetCurrentClassLogger(); _strings = strings; + //load settings + _excludedChannels = allGuildConfigs + .ToDictionary( + x => x.GuildId, + x => new ConcurrentHashSet(x.XpSettings + .ExclusionList + .Where(ex => ex.ItemType == ExcludedItemType.Channel) + .Select(ex => ex.ItemId) + .Distinct())) + .ToConcurrent(); + + _excludedRoles = allGuildConfigs + .ToDictionary( + x => x.GuildId, + x => new ConcurrentHashSet(x.XpSettings + .ExclusionList + .Where(ex => ex.ItemType == ExcludedItemType.Role) + .Select(ex => ex.ItemId) + .Distinct())) + .ToConcurrent(); + + _excludedServers = new ConcurrentHashSet( + allGuildConfigs.Where(x => x.XpSettings.ServerExcluded) + .Select(x => x.GuildId)); + //todo 60 move to font provider or somethign _fonts = new FontCollection(); if (Directory.Exists("data/fonts")) diff --git a/src/NadekoBot/Services/Database/Repositories/Impl/GuildConfigRepository.cs b/src/NadekoBot/Services/Database/Repositories/Impl/GuildConfigRepository.cs index 1c296db7..720fd794 100644 --- a/src/NadekoBot/Services/Database/Repositories/Impl/GuildConfigRepository.cs +++ b/src/NadekoBot/Services/Database/Repositories/Impl/GuildConfigRepository.cs @@ -47,6 +47,8 @@ namespace NadekoBot.Services.Database.Repositories.Impl .Include(gc => gc.FollowedStreams) .Include(gc => gc.StreamRole) .Include(gc => gc.NsfwBlacklistedTags) + .Include(gc => gc.XpSettings) + .ThenInclude(x => x.ExclusionList) .ToList(); ///