From 83296efbb7b17564adc61d991f1b641e1a08f986 Mon Sep 17 00:00:00 2001 From: Andriy <30056636+West14@users.noreply.github.com> Date: Tue, 17 Jan 2023 23:13:06 +0200 Subject: [PATCH] [entertainment] use thread safe random, update /roll number range --- .../Handler/Fun/AbstractAnimationReply.cs | 7 +++- modules/Entertainment/Handler/Fun/Bayan.cs | 3 +- .../Entertainment/Handler/Fun/JavaScript.cs | 3 +- modules/Entertainment/Handler/Fun/Python.cs | 3 +- modules/Entertainment/Handler/Roll.cs | 40 ++++++++++++++++++- 5 files changed, 49 insertions(+), 7 deletions(-) diff --git a/modules/Entertainment/Handler/Fun/AbstractAnimationReply.cs b/modules/Entertainment/Handler/Fun/AbstractAnimationReply.cs index fe48b90..a5de2cf 100644 --- a/modules/Entertainment/Handler/Fun/AbstractAnimationReply.cs +++ b/modules/Entertainment/Handler/Fun/AbstractAnimationReply.cs @@ -5,6 +5,7 @@ using System.Text.RegularExpressions; using System.Threading.Tasks; using BotFramework; using Castle.Core.Internal; +using Kruzya.TelegramBot.Core; using Kruzya.TelegramBot.Core.Data; using Kruzya.TelegramBot.Core.Extensions; using Microsoft.Extensions.Logging; @@ -18,8 +19,9 @@ namespace West.Entertainment.Handler.Fun private readonly CoreContext _db; private readonly ILogger _logger; + private readonly ThreadSafeRandom _rng; - protected virtual TimeSpan Cooldown => TimeSpan.FromMinutes(new Random().Next(10, 1337)); + protected virtual TimeSpan Cooldown => TimeSpan.FromMinutes(_rng.Next(10, 1337)); protected Message? Message => RawUpdate.Message; @@ -30,10 +32,11 @@ namespace West.Entertainment.Handler.Fun get; } - protected AbstractAnimationReply(CoreContext db, ILogger logger) + protected AbstractAnimationReply(CoreContext db, ILogger logger, ThreadSafeRandom rng) { _db = db; _logger = logger; + _rng = rng; } protected abstract string GetAnimationName(); diff --git a/modules/Entertainment/Handler/Fun/Bayan.cs b/modules/Entertainment/Handler/Fun/Bayan.cs index 777252a..47e12d7 100644 --- a/modules/Entertainment/Handler/Fun/Bayan.cs +++ b/modules/Entertainment/Handler/Fun/Bayan.cs @@ -5,6 +5,7 @@ using System.Text.RegularExpressions; using System.Threading.Tasks; using BotFramework.Attributes; using BotFramework.Enums; +using Kruzya.TelegramBot.Core; using Kruzya.TelegramBot.Core.Data; using Microsoft.Extensions.Logging; using Telegram.Bot; @@ -15,7 +16,7 @@ namespace West.Entertainment.Handler.Fun { protected override TimeSpan Cooldown => TimeSpan.Zero; - public Bayan(CoreContext db, ILogger logger) : base(db, logger) { } + public Bayan(CoreContext db, ILogger logger, ThreadSafeRandom rng) : base(db, logger, rng) { } protected override Regex AnimationMatch => new("^баян$", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase); diff --git a/modules/Entertainment/Handler/Fun/JavaScript.cs b/modules/Entertainment/Handler/Fun/JavaScript.cs index 2c344d8..d360ab3 100644 --- a/modules/Entertainment/Handler/Fun/JavaScript.cs +++ b/modules/Entertainment/Handler/Fun/JavaScript.cs @@ -2,6 +2,7 @@ using System.Threading.Tasks; using BotFramework.Attributes; using BotFramework.Enums; +using Kruzya.TelegramBot.Core; using Kruzya.TelegramBot.Core.Data; using Microsoft.Extensions.Logging; @@ -9,7 +10,7 @@ namespace West.Entertainment.Handler.Fun { public class JavaScript : AbstractAnimationReply { - public JavaScript(CoreContext db, ILogger logger) : base(db, logger) { } + public JavaScript(CoreContext db, ILogger logger, ThreadSafeRandom rng) : base(db, logger, rng) { } protected override Regex AnimationMatch => new( @"((? logger) : base(db, logger) { } + public Python(CoreContext db, ILogger logger, ThreadSafeRandom rng) : base(db, logger, rng) { } } } \ No newline at end of file diff --git a/modules/Entertainment/Handler/Roll.cs b/modules/Entertainment/Handler/Roll.cs index 633b68a..eadf62a 100644 --- a/modules/Entertainment/Handler/Roll.cs +++ b/modules/Entertainment/Handler/Roll.cs @@ -2,18 +2,21 @@ using BotFramework.Enums; using System.Threading.Tasks; using System; +using System.Collections.Generic; using System.Linq; using Kruzya.TelegramBot.Core; using Kruzya.TelegramBot.Core.Data; using Kruzya.TelegramBot.Core.Extensions; using Kruzya.TelegramBot.Core.Service; using Microsoft.Extensions.Logging; +using Telegram.Bot; namespace West.Entertainment.Handler; public class Roll : CommonHandler { private readonly ILogger _logger; + private readonly ThreadSafeRandom _rng; private readonly IReputation _reputation; private readonly UserService _userService; @@ -36,7 +39,7 @@ public class Roll : CommonHandler return; } - var newRep = new Random().Next(-50, 50); + var newRep = _rng.Next(-20, 80); await Reply($"Вы получили {newRep} кармы."); await _reputation.IncrementReputationAsync(Chat, From, newRep); @@ -81,10 +84,43 @@ public class Roll : CommonHandler await Reply($"Кулдаун /roll сброшен для чата \"{Chat.Title}\"."); } - public Roll(CoreContext db, IReputation reputation, UserService userService, ILogger logger) : base(db) + [ParametrizedCommand(InChat.Public, "rng_test", CommandParseMode.Both)] + public async Task HandleRngTest(int minValue, int maxValue) + { + if (!_userService.IsUserSuper(From)) + { + _logger.LogWarning("{User} attempted to use super-user command /rng_test in {Chat}", + From.ToLog(), + Chat.ToLog() + ); + return; + } + + var positive = 0; + var negative = 0; + + for (var i = 0; i < 100; i++) + { + var number = _rng.Next(minValue, maxValue); + + if (number > 0) + { + positive++; + } + else + { + negative++; + } + } + + await Bot.SendTextMessageAsync(Chat, $"Positive: {positive}.\nNegative: {negative}"); + } + + public Roll(CoreContext db, IReputation reputation, UserService userService, ILogger logger, ThreadSafeRandom rng) : base(db) { _reputation = reputation; _userService = userService; _logger = logger; + _rng = rng; } } \ No newline at end of file