2022-02-14 14:39:55 +02:00
|
|
|
using Kruzya.TelegramBot.Core.Data;
|
|
|
|
|
using Kruzya.TelegramBot.Core.Extensions;
|
2022-02-14 00:18:53 +02:00
|
|
|
using Kruzya.TelegramBot.Core.Service;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using Telegram.Bot.Types;
|
|
|
|
|
using West.TelegramBot.Reputation.Data;
|
2023-07-21 18:39:38 +03:00
|
|
|
using West.TelegramBot.Reputation.Options;
|
2022-02-14 00:18:53 +02:00
|
|
|
|
|
|
|
|
namespace West.TelegramBot.Reputation.Service;
|
|
|
|
|
|
|
|
|
|
public class ReputationService : IReputation
|
|
|
|
|
{
|
|
|
|
|
private readonly ReputationContext _reputationContext;
|
2023-07-21 18:39:38 +03:00
|
|
|
private readonly KarmaMode _karmaMode;
|
2022-02-14 00:18:53 +02:00
|
|
|
|
2023-07-21 18:39:38 +03:00
|
|
|
public ReputationService(ReputationContext reputationContext, KarmaMode karmaMode)
|
2022-02-14 00:18:53 +02:00
|
|
|
{
|
|
|
|
|
_reputationContext = reputationContext;
|
2023-07-21 18:39:38 +03:00
|
|
|
_karmaMode = karmaMode;
|
2022-02-14 00:18:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<double> GetUserReputationAsync(Chat chat, User user)
|
|
|
|
|
{
|
|
|
|
|
var rep = await FindUserReputation(chat, user);
|
|
|
|
|
|
|
|
|
|
return rep?.Value ?? 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<double> SetUserReputationAsync(Chat chat, User user, double value)
|
|
|
|
|
{
|
|
|
|
|
var rep = await FindUserReputation(chat, user) ?? new UserReputation
|
|
|
|
|
{
|
|
|
|
|
ChatId = chat.Id,
|
|
|
|
|
UserId = user.Id
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
rep.Value = Math.Round(value, 2);
|
|
|
|
|
_reputationContext.AddOrUpdate(rep);
|
|
|
|
|
|
|
|
|
|
return rep.Value;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-23 21:42:17 +03:00
|
|
|
public async Task<double> IncrementReputationAsync(Chat chat, User user, double diff)
|
2022-02-14 00:18:53 +02:00
|
|
|
{
|
2022-04-23 21:42:17 +03:00
|
|
|
return await SetUserReputationAsync(chat, user, await GetUserReputationAsync(chat, user) + diff);
|
2022-02-14 00:18:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<double> GetReputationDiffAsync(Chat chat, User sender, User receiver)
|
|
|
|
|
{
|
|
|
|
|
var senderRep = await FindUserReputation(chat, sender);
|
|
|
|
|
if (senderRep == null)
|
|
|
|
|
{
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2023-07-21 18:39:38 +03:00
|
|
|
|
|
|
|
|
var karmaMode = await _karmaMode.GetValueAsync(chat.Id);
|
2022-02-14 00:18:53 +02:00
|
|
|
var senderValue = senderRep.Value;
|
2023-07-21 18:39:38 +03:00
|
|
|
|
|
|
|
|
double diff;
|
|
|
|
|
if (senderValue == 0 || karmaMode == "linear")
|
|
|
|
|
{
|
|
|
|
|
diff = 1;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
diff = Math.Round(Math.Sqrt(senderValue), 2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return diff;
|
2022-02-14 00:18:53 +02:00
|
|
|
}
|
|
|
|
|
|
2022-02-14 14:39:55 +02:00
|
|
|
public async Task<IEnumerable<IReputationEntity>> GetChatRatingAsync(Chat chat, int limit = 10, int offset = 0)
|
|
|
|
|
{
|
|
|
|
|
return await _reputationContext.UserReputation
|
2022-02-14 14:42:40 +02:00
|
|
|
.Where(r => r.ChatId == chat.Id && r.Value > 0D)
|
2022-02-14 14:39:55 +02:00
|
|
|
.OrderByDescending(r => r.Value)
|
|
|
|
|
.Skip(offset)
|
|
|
|
|
.Take(limit)
|
|
|
|
|
.ToListAsync();
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-14 00:18:53 +02:00
|
|
|
private async Task<UserReputation?> FindUserReputation(Chat chat, User user)
|
|
|
|
|
{
|
|
|
|
|
return await _reputationContext.UserReputation.SingleOrDefaultAsync(
|
|
|
|
|
e => e.ChatId == chat.Id && e.UserId == user.Id);
|
|
|
|
|
}
|
|
|
|
|
}
|