✨ Social credit, leveling service
This commit is contained in:
17
DysonNetwork.Pass/Credit/SocialCreditRecord.cs
Normal file
17
DysonNetwork.Pass/Credit/SocialCreditRecord.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using DysonNetwork.Shared.Data;
|
||||
using NodaTime;
|
||||
|
||||
namespace DysonNetwork.Pass.Credit;
|
||||
|
||||
public class SocialCreditRecord : ModelBase
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
[MaxLength(1024)] public string ReasonType { get; set; } = string.Empty;
|
||||
[MaxLength(1024)] public string Reason { get; set; } = string.Empty;
|
||||
public double Delta { get; set; }
|
||||
public Instant? ExpiredAt { get; set; }
|
||||
|
||||
public Guid AccountId { get; set; }
|
||||
public Account.Account Account { get; set; } = null!;
|
||||
}
|
38
DysonNetwork.Pass/Credit/SocialCreditService.cs
Normal file
38
DysonNetwork.Pass/Credit/SocialCreditService.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
using DysonNetwork.Shared.Cache;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace DysonNetwork.Pass.Credit;
|
||||
|
||||
public class SocialCreditService(AppDatabase db, ICacheService cache)
|
||||
{
|
||||
private const string CacheKeyPrefix = "account:credits:";
|
||||
|
||||
public async Task<SocialCreditRecord> AddRecord(string reasonType, string reason, double delta, Guid accountId)
|
||||
{
|
||||
var record = new SocialCreditRecord
|
||||
{
|
||||
ReasonType = reasonType,
|
||||
Reason = reason,
|
||||
Delta = delta,
|
||||
AccountId = accountId,
|
||||
};
|
||||
db.SocialCreditRecords.Add(record);
|
||||
await db.SaveChangesAsync();
|
||||
return record;
|
||||
}
|
||||
|
||||
private const double BaseSocialCredit = 100;
|
||||
|
||||
public async Task<double> GetSocialCredit(Guid accountId)
|
||||
{
|
||||
var cached = await cache.GetAsync<double?>($"{CacheKeyPrefix}{accountId}");
|
||||
if (cached.HasValue) return cached.Value;
|
||||
|
||||
var records = await db.SocialCreditRecords
|
||||
.Where(x => x.AccountId == accountId)
|
||||
.SumAsync(x => x.Delta);
|
||||
records += BaseSocialCredit;
|
||||
await cache.SetAsync($"{CacheKeyPrefix}{accountId}", records);
|
||||
return records;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user