[reputation] KarmaMode now actually changes smth

This commit is contained in:
Andriy
2023-07-21 18:39:38 +03:00
parent 864f6d0f57
commit 613ac5ae22
2 changed files with 20 additions and 6 deletions
+2 -2
View File
@@ -1,5 +1,5 @@
using Kruzya.TelegramBot.Core;
using Kruzya.TelegramBot.Core.Options;
using Kruzya.TelegramBot.Core.Extensions;
using Kruzya.TelegramBot.Core.Service;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
@@ -21,7 +21,7 @@ public class Reputation : Module
var dsn = Configuration.GetConnectionString("Reputation");
services.AddDbContext<ReputationContext>(options => options.UseMySql(dsn, ServerVersion.AutoDetect(dsn)));
services.AddOption<KarmaMode>();
services.AddScoped<IReputation, ReputationService>();
services.AddScoped<IOption, KarmaMode>();
}
}
@@ -4,16 +4,19 @@ using Kruzya.TelegramBot.Core.Service;
using Microsoft.EntityFrameworkCore;
using Telegram.Bot.Types;
using West.TelegramBot.Reputation.Data;
using West.TelegramBot.Reputation.Options;
namespace West.TelegramBot.Reputation.Service;
public class ReputationService : IReputation
{
private readonly ReputationContext _reputationContext;
private readonly KarmaMode _karmaMode;
public ReputationService(ReputationContext reputationContext)
public ReputationService(ReputationContext reputationContext, KarmaMode karmaMode)
{
_reputationContext = reputationContext;
_karmaMode = karmaMode;
}
public async Task<double> GetUserReputationAsync(Chat chat, User user)
@@ -45,14 +48,25 @@ public class ReputationService : IReputation
public async Task<double> GetReputationDiffAsync(Chat chat, User sender, User receiver)
{
var senderRep = await FindUserReputation(chat, sender);
if (senderRep == null)
{
return 1;
}
var karmaMode = await _karmaMode.GetValueAsync(chat.Id);
var senderValue = senderRep.Value;
return Math.Round(senderValue == 0 ? 1 : Math.Sqrt(senderValue), 2);
double diff;
if (senderValue == 0 || karmaMode == "linear")
{
diff = 1;
}
else
{
diff = Math.Round(Math.Sqrt(senderValue), 2);
}
return diff;
}
public async Task<IEnumerable<IReputationEntity>> GetChatRatingAsync(Chat chat, int limit = 10, int offset = 0)