mirror of
				https://gitlab.com/Kwoth/nadekobot.git
				synced 2025-11-04 00:34:26 -05:00 
			
		
		
		
	- Added currency.transactionsLifetime which controls auto-deletion of currency transactions from the database
- Updated changelog
This commit is contained in:
		@@ -206,7 +206,6 @@ public partial class Gambling : GamblingModule<GamblingService>
 | 
			
		||||
    public partial Task CurrencyTransactions(IUser usr, int page)
 | 
			
		||||
        => InternalCurrencyTransactions(usr.Id, page);
 | 
			
		||||
 | 
			
		||||
    // todo curtrs max lifetime
 | 
			
		||||
    private async Task InternalCurrencyTransactions(ulong userId, int page)
 | 
			
		||||
    {
 | 
			
		||||
        if (--page < 0)
 | 
			
		||||
 
 | 
			
		||||
@@ -77,6 +77,10 @@ public class CurrencyConfig
 | 
			
		||||
 | 
			
		||||
    [Comment(@"What is the name of the currency")]
 | 
			
		||||
    public string Name { get; set; } = "Nadeko Flower";
 | 
			
		||||
 | 
			
		||||
    [Comment(@"For how long will the transactions be kept in the database (curtrs)
 | 
			
		||||
Set 0 to disable cleanup (keep transactions forever)")]
 | 
			
		||||
    public int TransactionsLifetime { get; set; } = 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
[Cloneable]
 | 
			
		||||
 
 | 
			
		||||
@@ -1,4 +1,5 @@
 | 
			
		||||
#nullable disable
 | 
			
		||||
using LinqToDB;
 | 
			
		||||
using Microsoft.EntityFrameworkCore;
 | 
			
		||||
using NadekoBot.Common.ModuleBehaviors;
 | 
			
		||||
using NadekoBot.Db;
 | 
			
		||||
@@ -37,7 +38,40 @@ public class GamblingService : INService, IReadyExecutor
 | 
			
		||||
        _gss = gss;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public async Task OnReadyAsync()
 | 
			
		||||
    public Task OnReadyAsync()
 | 
			
		||||
        => Task.WhenAll(CurrencyDecayLoopAsync(), TransactionClearLoopAsync());
 | 
			
		||||
 | 
			
		||||
    private async Task TransactionClearLoopAsync()
 | 
			
		||||
    {
 | 
			
		||||
        if (_bot.Client.ShardId != 0)
 | 
			
		||||
            return;
 | 
			
		||||
 | 
			
		||||
        using var timer = new PeriodicTimer(TimeSpan.FromHours(1));
 | 
			
		||||
        while (await timer.WaitForNextTickAsync())
 | 
			
		||||
        {
 | 
			
		||||
            try
 | 
			
		||||
            {
 | 
			
		||||
                var lifetime = _gss.Data.Currency.TransactionsLifetime;
 | 
			
		||||
                if (lifetime <= 0)
 | 
			
		||||
                    continue;
 | 
			
		||||
 | 
			
		||||
                var now = DateTime.UtcNow;
 | 
			
		||||
                var days = TimeSpan.FromDays(lifetime);
 | 
			
		||||
                await using var uow = _db.GetDbContext();
 | 
			
		||||
                await uow.CurrencyTransactions
 | 
			
		||||
                         .DeleteAsync(ct => ct.DateAdded == null || now - ct.DateAdded < days);
 | 
			
		||||
                await uow.SaveChangesAsync();
 | 
			
		||||
            }
 | 
			
		||||
            catch (Exception ex)
 | 
			
		||||
            {
 | 
			
		||||
                Log.Warning(ex,
 | 
			
		||||
                    "An unexpected error occurred in transactions cleanup loop: {ErrorMessage}",
 | 
			
		||||
                    ex.Message);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    private async Task CurrencyDecayLoopAsync()
 | 
			
		||||
    {
 | 
			
		||||
        if (_bot.Client.ShardId != 0)
 | 
			
		||||
            return;
 | 
			
		||||
@@ -45,28 +79,30 @@ public class GamblingService : INService, IReadyExecutor
 | 
			
		||||
        using var timer = new PeriodicTimer(TimeSpan.FromMinutes(5));
 | 
			
		||||
        while (await timer.WaitForNextTickAsync())
 | 
			
		||||
        {
 | 
			
		||||
            var config = _gss.Data;
 | 
			
		||||
            var maxDecay = config.Decay.MaxDecay;
 | 
			
		||||
            if (config.Decay.Percent is <= 0 or > 1 || maxDecay < 0)
 | 
			
		||||
                continue;
 | 
			
		||||
            try
 | 
			
		||||
            {
 | 
			
		||||
                var config = _gss.Data;
 | 
			
		||||
                var maxDecay = config.Decay.MaxDecay;
 | 
			
		||||
                if (config.Decay.Percent is <= 0 or > 1 || maxDecay < 0)
 | 
			
		||||
                    continue;
 | 
			
		||||
 | 
			
		||||
            await using var uow = _db.GetDbContext();
 | 
			
		||||
            var lastCurrencyDecay = _cache.GetLastCurrencyDecay();
 | 
			
		||||
                await using var uow = _db.GetDbContext();
 | 
			
		||||
                var lastCurrencyDecay = _cache.GetLastCurrencyDecay();
 | 
			
		||||
 | 
			
		||||
            if (DateTime.UtcNow - lastCurrencyDecay < TimeSpan.FromHours(config.Decay.HourInterval))
 | 
			
		||||
                continue;
 | 
			
		||||
                if (DateTime.UtcNow - lastCurrencyDecay < TimeSpan.FromHours(config.Decay.HourInterval))
 | 
			
		||||
                    continue;
 | 
			
		||||
 | 
			
		||||
            Log.Information(@"Decaying users' currency - decay: {ConfigDecayPercent}% 
 | 
			
		||||
                Log.Information(@"Decaying users' currency - decay: {ConfigDecayPercent}% 
 | 
			
		||||
                                    | max: {MaxDecay} 
 | 
			
		||||
                                    | threshold: {DecayMinTreshold}",
 | 
			
		||||
                config.Decay.Percent * 100,
 | 
			
		||||
                maxDecay,
 | 
			
		||||
                config.Decay.MinThreshold);
 | 
			
		||||
                    config.Decay.Percent * 100,
 | 
			
		||||
                    maxDecay,
 | 
			
		||||
                    config.Decay.MinThreshold);
 | 
			
		||||
 | 
			
		||||
            if (maxDecay == 0)
 | 
			
		||||
                maxDecay = int.MaxValue;
 | 
			
		||||
                if (maxDecay == 0)
 | 
			
		||||
                    maxDecay = int.MaxValue;
 | 
			
		||||
 | 
			
		||||
            await uow.Database.ExecuteSqlInterpolatedAsync($@"
 | 
			
		||||
                await uow.Database.ExecuteSqlInterpolatedAsync($@"
 | 
			
		||||
UPDATE DiscordUser
 | 
			
		||||
SET CurrencyAmount=
 | 
			
		||||
    CASE WHEN
 | 
			
		||||
@@ -78,11 +114,18 @@ SET CurrencyAmount=
 | 
			
		||||
    END
 | 
			
		||||
WHERE CurrencyAmount > {config.Decay.MinThreshold} AND UserId!={_client.CurrentUser.Id};");
 | 
			
		||||
 | 
			
		||||
            _cache.SetLastCurrencyDecay();
 | 
			
		||||
            await uow.SaveChangesAsync();
 | 
			
		||||
                _cache.SetLastCurrencyDecay();
 | 
			
		||||
                await uow.SaveChangesAsync();
 | 
			
		||||
            }
 | 
			
		||||
            catch (Exception ex)
 | 
			
		||||
            {
 | 
			
		||||
                Log.Warning(ex,
 | 
			
		||||
                    "An unexpected error occurred in currency decay loop: {ErrorMessage}",
 | 
			
		||||
                    ex.Message);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    
 | 
			
		||||
    public async Task<SlotResponse> SlotAsync(ulong userId, long amount)
 | 
			
		||||
    {
 | 
			
		||||
        var takeRes = await _cs.RemoveAsync(userId, amount, new("slot", "bet"));
 | 
			
		||||
 
 | 
			
		||||
@@ -10,7 +10,6 @@ using NadekoBot.Services.Database.Models;
 | 
			
		||||
 | 
			
		||||
namespace NadekoBot.Modules.Gambling.Services;
 | 
			
		||||
 | 
			
		||||
// todo waifu price int
 | 
			
		||||
public class WaifuService : INService, IReadyExecutor
 | 
			
		||||
{
 | 
			
		||||
    private readonly DbService _db;
 | 
			
		||||
 
 | 
			
		||||
@@ -6,6 +6,9 @@ currency:
 | 
			
		||||
  sign: "🌸"
 | 
			
		||||
  # What is the name of the currency
 | 
			
		||||
  name: Nadeko Flower
 | 
			
		||||
  # For how long will the transactions be kept in the database (curtrs)
 | 
			
		||||
  # Set 0 to disable cleanup (keep transactions forever)
 | 
			
		||||
  transactionsLifetime: 0
 | 
			
		||||
# Minimum amount users can bet (>=0)
 | 
			
		||||
minBet: 0
 | 
			
		||||
# Maximum amount users can bet
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user