mirror of
https://github.com/Bubuni-Team/telegram-bot.git
synced 2026-07-31 00:29:24 +03:00
[entertainment] use thread safe random, update /roll number range
This commit is contained in:
@@ -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<Roll> _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<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;
|
||||
_userService = userService;
|
||||
_logger = logger;
|
||||
_rng = rng;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user