Change reputation counting algo.

This commit is contained in:
West14
2022-01-07 22:25:26 +02:00
parent ad13577541
commit 31a371b034
2 changed files with 26 additions and 16 deletions
+1 -2
View File
@@ -8,8 +8,7 @@ namespace Kruzya.TelegramBot.Core
{ {
public static void Main(string[] args) public static void Main(string[] args)
{ {
CultureInfo.CurrentCulture = new CultureInfo("ru-RU"); CultureInfo.CurrentCulture = new CultureInfo("en-US");
CreateHostBuilder(args).Build().Run(); CreateHostBuilder(args).Build().Run();
} }
+24 -13
View File
@@ -1,5 +1,6 @@
#nullable enable #nullable enable
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using BotFramework.Attributes; using BotFramework.Attributes;
@@ -8,7 +9,6 @@ using BotFramework.Utils;
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.Configuration;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Telegram.Bot; using Telegram.Bot;
using Telegram.Bot.Types; using Telegram.Bot.Types;
@@ -84,32 +84,43 @@ namespace West.TelegramBot.ChatManagement.Handler
if (canChangeRep && (RepUpChars.Contains(text) || RepDownChars.Contains(text))) if (canChangeRep && (RepUpChars.Contains(text) || RepDownChars.Contains(text)))
{ {
var repOption = await GetReputationOption(receiver!); var receiverRepOption = await GetReputationOption(receiver!);
var repCount = repOption.GetValue<int>(); var receiverRepCount = receiverRepOption.GetValue<double>();
var senderRepCount = (await GetReputationOption(From)).GetValue<double>();
if (senderRepCount < 0)
{
await Reply(new HtmlString()
.Code("Ты не можешь голосовать с отрицательной репутацией.")
.ToString()
);
return;
}
var diff = Math.Round(senderRepCount == 0 ? 1 : Math.Sqrt(senderRepCount), 2);
string action; string action;
if (RepUpChars.Contains(text)) if (RepUpChars.Contains(text))
{ {
repCount++; receiverRepCount += diff;
action = "увеличил"; action = "увеличил";
} }
else else
{ {
repCount--; receiverRepCount -= diff;
action = "уменьшил"; action = "уменьшил";
} }
repOption.SetValue(repCount); receiverRepOption.SetValue(receiverRepCount);
Db.MarkAsModified(repOption); Db.MarkAsModified(receiverRepOption);
await Reply($"{From.ToHtml()}<code>({await GetUserReputation(From)}) " + await Reply($"{From.ToHtml()}<code>({await GetUserReputation(From)}) " +
$"{action} репутацию </code>{receiver.ToHtml()}<code>({repCount})</code>"); $"{action} репутацию </code>{receiver.ToHtml()}<code>({Math.Round(receiverRepCount, 2)})</code>");
} }
} }
[ParametrizedCommand(InChat.Public, "setrep", CommandParseMode.Both)] [ParametrizedCommand(InChat.Public, "setrep", CommandParseMode.Both)]
public async Task HandleSetRep(int reputation) public async Task HandleSetRep(double reputation)
{ {
if (!_userService.IsUserSuper(From)) if (!_userService.IsUserSuper(From))
{ {
@@ -122,6 +133,7 @@ namespace West.TelegramBot.ChatManagement.Handler
return; return;
} }
reputation = Math.Round(reputation, 2);
var opt = await GetReputationOption(RepliedUser); var opt = await GetReputationOption(RepliedUser);
opt.SetValue(reputation); opt.SetValue(reputation);
Db.MarkAsModified(opt); Db.MarkAsModified(opt);
@@ -135,10 +147,9 @@ namespace West.TelegramBot.ChatManagement.Handler
); );
} }
private async Task<double> GetUserReputation(User user)
private async Task<int> GetUserReputation(User user)
{ {
return (await GetReputationOption(user)).GetValue<int>(); return Math.Round((await GetReputationOption(user)).GetValue<double>(), 2);
} }
private async Task<BotUserValue> GetReputationOption(User user) private async Task<BotUserValue> GetReputationOption(User user)