1
Fork 0
mirror of https://gitlab.com/Kwoth/nadekobot.git synced 2024-10-02 20:13:13 +00:00

- Added currency.transactionsLifetime which controls auto-deletion of currency transactions from the database

- Updated changelog
This commit is contained in:
Kwoth 2022-02-02 13:52:54 +01:00
parent 15ee3dd638
commit eecccc8100
6 changed files with 70 additions and 21 deletions

View file

@ -16,6 +16,7 @@ Experimental changelog. Mostly based on [keepachangelog](https://keepachangelog.
- waifu.decay.percent - How much % to subtract from unclaimed waifu
- waifu.decay.hourInterval - How often to decay the price
- waifu.decay.minPrice - Unclaimed waifus with price lower than the one specified here will not be affected by the decay
- Added `currency.transactionsLifetime` to `data/gambling.yml` Any transaction older than the number of days specified will be automatically deleted
### Fixed
- Fixed an extra whitespace in usage part of command help if the command has no arguments

View file

@ -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)

View file

@ -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]

View file

@ -1,4 +1,5 @@
#nullable disable
using LinqToDB;
using Microsoft.EntityFrameworkCore;
using NadekoBot.Common.ModuleBehaviors;
using NadekoBot.Db;
@ -37,13 +38,48 @@ 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;
using var timer = new PeriodicTimer(TimeSpan.FromMinutes(5));
while (await timer.WaitForNextTickAsync())
{
try
{
var config = _gss.Data;
var maxDecay = config.Decay.MaxDecay;
@ -81,6 +117,13 @@ WHERE CurrencyAmount > {config.Decay.MinThreshold} AND UserId!={_client.CurrentU
_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)

View file

@ -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;

View file

@ -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