[entertainment] use thread safe random, update /roll number range

This commit is contained in:
Andriy
2023-01-17 23:13:06 +02:00
parent 98b1e2f0f7
commit 83296efbb7
5 changed files with 49 additions and 7 deletions
@@ -5,6 +5,7 @@ using System.Text.RegularExpressions;
using System.Threading.Tasks; using System.Threading.Tasks;
using BotFramework; using BotFramework;
using Castle.Core.Internal; using Castle.Core.Internal;
using Kruzya.TelegramBot.Core;
using Kruzya.TelegramBot.Core.Data; using Kruzya.TelegramBot.Core.Data;
using Kruzya.TelegramBot.Core.Extensions; using Kruzya.TelegramBot.Core.Extensions;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
@@ -18,8 +19,9 @@ namespace West.Entertainment.Handler.Fun
private readonly CoreContext _db; private readonly CoreContext _db;
private readonly ILogger _logger; 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; protected Message? Message => RawUpdate.Message;
@@ -30,10 +32,11 @@ namespace West.Entertainment.Handler.Fun
get; get;
} }
protected AbstractAnimationReply(CoreContext db, ILogger logger) protected AbstractAnimationReply(CoreContext db, ILogger logger, ThreadSafeRandom rng)
{ {
_db = db; _db = db;
_logger = logger; _logger = logger;
_rng = rng;
} }
protected abstract string GetAnimationName(); protected abstract string GetAnimationName();
+2 -1
View File
@@ -5,6 +5,7 @@ using System.Text.RegularExpressions;
using System.Threading.Tasks; using System.Threading.Tasks;
using BotFramework.Attributes; using BotFramework.Attributes;
using BotFramework.Enums; using BotFramework.Enums;
using Kruzya.TelegramBot.Core;
using Kruzya.TelegramBot.Core.Data; using Kruzya.TelegramBot.Core.Data;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Telegram.Bot; using Telegram.Bot;
@@ -15,7 +16,7 @@ namespace West.Entertainment.Handler.Fun
{ {
protected override TimeSpan Cooldown => TimeSpan.Zero; protected override TimeSpan Cooldown => TimeSpan.Zero;
public Bayan(CoreContext db, ILogger<Bayan> logger) : base(db, logger) { } public Bayan(CoreContext db, ILogger<Bayan> logger, ThreadSafeRandom rng) : base(db, logger, rng) { }
protected override Regex AnimationMatch protected override Regex AnimationMatch
=> new("^баян$", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase); => new("^баян$", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);
@@ -2,6 +2,7 @@
using System.Threading.Tasks; using System.Threading.Tasks;
using BotFramework.Attributes; using BotFramework.Attributes;
using BotFramework.Enums; using BotFramework.Enums;
using Kruzya.TelegramBot.Core;
using Kruzya.TelegramBot.Core.Data; using Kruzya.TelegramBot.Core.Data;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
@@ -9,7 +10,7 @@ namespace West.Entertainment.Handler.Fun
{ {
public class JavaScript : AbstractAnimationReply public class JavaScript : AbstractAnimationReply
{ {
public JavaScript(CoreContext db, ILogger<JavaScript> logger) : base(db, logger) { } public JavaScript(CoreContext db, ILogger<JavaScript> logger, ThreadSafeRandom rng) : base(db, logger, rng) { }
protected override Regex AnimationMatch => new( protected override Regex AnimationMatch => new(
@"((?<![\w])(жс|js|v8)(?![\w]))|жаваскрипт|javascript|жопаскрипт|жиес|жиэс|nodejs", @"((?<![\w])(жс|js|v8)(?![\w]))|жаваскрипт|javascript|жопаскрипт|жиес|жиэс|nodejs",
+2 -1
View File
@@ -4,6 +4,7 @@ using System.Text.RegularExpressions;
using System.Threading.Tasks; using System.Threading.Tasks;
using BotFramework.Attributes; using BotFramework.Attributes;
using BotFramework.Enums; using BotFramework.Enums;
using Kruzya.TelegramBot.Core;
using Kruzya.TelegramBot.Core.Data; using Kruzya.TelegramBot.Core.Data;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
@@ -28,6 +29,6 @@ namespace West.Entertainment.Handler.Fun
await HandleAnimation(); await HandleAnimation();
} }
public Python(CoreContext db, ILogger<Python> logger) : base(db, logger) { } public Python(CoreContext db, ILogger<Python> logger, ThreadSafeRandom rng) : base(db, logger, rng) { }
} }
} }
+38 -2
View File
@@ -2,18 +2,21 @@
using BotFramework.Enums; using BotFramework.Enums;
using System.Threading.Tasks; using System.Threading.Tasks;
using System; using System;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using Kruzya.TelegramBot.Core; using Kruzya.TelegramBot.Core;
using Kruzya.TelegramBot.Core.Data; using Kruzya.TelegramBot.Core.Data;
using Kruzya.TelegramBot.Core.Extensions; using Kruzya.TelegramBot.Core.Extensions;
using Kruzya.TelegramBot.Core.Service; using Kruzya.TelegramBot.Core.Service;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Telegram.Bot;
namespace West.Entertainment.Handler; namespace West.Entertainment.Handler;
public class Roll : CommonHandler public class Roll : CommonHandler
{ {
private readonly ILogger<Roll> _logger; private readonly ILogger<Roll> _logger;
private readonly ThreadSafeRandom _rng;
private readonly IReputation _reputation; private readonly IReputation _reputation;
private readonly UserService _userService; private readonly UserService _userService;
@@ -36,7 +39,7 @@ public class Roll : CommonHandler
return; return;
} }
var newRep = new Random().Next(-50, 50); var newRep = _rng.Next(-20, 80);
await Reply($"Вы получили {newRep} кармы."); await Reply($"Вы получили {newRep} кармы.");
await _reputation.IncrementReputationAsync(Chat, From, newRep); await _reputation.IncrementReputationAsync(Chat, From, newRep);
@@ -81,10 +84,43 @@ public class Roll : CommonHandler
await Reply($"Кулдаун /roll сброшен для чата \"{Chat.Title}\"."); await Reply($"Кулдаун /roll сброшен для чата \"{Chat.Title}\".");
} }
public Roll(CoreContext db, IReputation reputation, UserService userService, ILogger<Roll> 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<Roll> logger, ThreadSafeRandom rng) : base(db)
{ {
_reputation = reputation; _reputation = reputation;
_userService = userService; _userService = userService;
_logger = logger; _logger = logger;
_rng = rng;
} }
} }