mirror of
https://gitlab.com/Kwoth/nadekobot.git
synced 2025-09-10 17:28:27 -04:00
- Added currency.transactionsLifetime which controls auto-deletion of currency transactions from the database
- Updated changelog
This commit is contained in:
@@ -16,6 +16,7 @@ Experimental changelog. Mostly based on [keepachangelog](https://keepachangelog.
|
|||||||
- waifu.decay.percent - How much % to subtract from unclaimed waifu
|
- waifu.decay.percent - How much % to subtract from unclaimed waifu
|
||||||
- waifu.decay.hourInterval - How often to decay the price
|
- 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
|
- 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
|
||||||
- Fixed an extra whitespace in usage part of command help if the command has no arguments
|
- Fixed an extra whitespace in usage part of command help if the command has no arguments
|
||||||
|
@@ -206,7 +206,6 @@ public partial class Gambling : GamblingModule<GamblingService>
|
|||||||
public partial Task CurrencyTransactions(IUser usr, int page)
|
public partial Task CurrencyTransactions(IUser usr, int page)
|
||||||
=> InternalCurrencyTransactions(usr.Id, page);
|
=> InternalCurrencyTransactions(usr.Id, page);
|
||||||
|
|
||||||
// todo curtrs max lifetime
|
|
||||||
private async Task InternalCurrencyTransactions(ulong userId, int page)
|
private async Task InternalCurrencyTransactions(ulong userId, int page)
|
||||||
{
|
{
|
||||||
if (--page < 0)
|
if (--page < 0)
|
||||||
|
@@ -77,6 +77,10 @@ public class CurrencyConfig
|
|||||||
|
|
||||||
[Comment(@"What is the name of the currency")]
|
[Comment(@"What is the name of the currency")]
|
||||||
public string Name { get; set; } = "Nadeko Flower";
|
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]
|
[Cloneable]
|
||||||
|
@@ -1,4 +1,5 @@
|
|||||||
#nullable disable
|
#nullable disable
|
||||||
|
using LinqToDB;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using NadekoBot.Common.ModuleBehaviors;
|
using NadekoBot.Common.ModuleBehaviors;
|
||||||
using NadekoBot.Db;
|
using NadekoBot.Db;
|
||||||
@@ -37,7 +38,40 @@ public class GamblingService : INService, IReadyExecutor
|
|||||||
_gss = gss;
|
_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)
|
if (_bot.Client.ShardId != 0)
|
||||||
return;
|
return;
|
||||||
@@ -45,28 +79,30 @@ public class GamblingService : INService, IReadyExecutor
|
|||||||
using var timer = new PeriodicTimer(TimeSpan.FromMinutes(5));
|
using var timer = new PeriodicTimer(TimeSpan.FromMinutes(5));
|
||||||
while (await timer.WaitForNextTickAsync())
|
while (await timer.WaitForNextTickAsync())
|
||||||
{
|
{
|
||||||
var config = _gss.Data;
|
try
|
||||||
var maxDecay = config.Decay.MaxDecay;
|
{
|
||||||
if (config.Decay.Percent is <= 0 or > 1 || maxDecay < 0)
|
var config = _gss.Data;
|
||||||
continue;
|
var maxDecay = config.Decay.MaxDecay;
|
||||||
|
if (config.Decay.Percent is <= 0 or > 1 || maxDecay < 0)
|
||||||
|
continue;
|
||||||
|
|
||||||
await using var uow = _db.GetDbContext();
|
await using var uow = _db.GetDbContext();
|
||||||
var lastCurrencyDecay = _cache.GetLastCurrencyDecay();
|
var lastCurrencyDecay = _cache.GetLastCurrencyDecay();
|
||||||
|
|
||||||
if (DateTime.UtcNow - lastCurrencyDecay < TimeSpan.FromHours(config.Decay.HourInterval))
|
if (DateTime.UtcNow - lastCurrencyDecay < TimeSpan.FromHours(config.Decay.HourInterval))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
Log.Information(@"Decaying users' currency - decay: {ConfigDecayPercent}%
|
Log.Information(@"Decaying users' currency - decay: {ConfigDecayPercent}%
|
||||||
| max: {MaxDecay}
|
| max: {MaxDecay}
|
||||||
| threshold: {DecayMinTreshold}",
|
| threshold: {DecayMinTreshold}",
|
||||||
config.Decay.Percent * 100,
|
config.Decay.Percent * 100,
|
||||||
maxDecay,
|
maxDecay,
|
||||||
config.Decay.MinThreshold);
|
config.Decay.MinThreshold);
|
||||||
|
|
||||||
if (maxDecay == 0)
|
if (maxDecay == 0)
|
||||||
maxDecay = int.MaxValue;
|
maxDecay = int.MaxValue;
|
||||||
|
|
||||||
await uow.Database.ExecuteSqlInterpolatedAsync($@"
|
await uow.Database.ExecuteSqlInterpolatedAsync($@"
|
||||||
UPDATE DiscordUser
|
UPDATE DiscordUser
|
||||||
SET CurrencyAmount=
|
SET CurrencyAmount=
|
||||||
CASE WHEN
|
CASE WHEN
|
||||||
@@ -78,11 +114,18 @@ SET CurrencyAmount=
|
|||||||
END
|
END
|
||||||
WHERE CurrencyAmount > {config.Decay.MinThreshold} AND UserId!={_client.CurrentUser.Id};");
|
WHERE CurrencyAmount > {config.Decay.MinThreshold} AND UserId!={_client.CurrentUser.Id};");
|
||||||
|
|
||||||
_cache.SetLastCurrencyDecay();
|
_cache.SetLastCurrencyDecay();
|
||||||
await uow.SaveChangesAsync();
|
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)
|
public async Task<SlotResponse> SlotAsync(ulong userId, long amount)
|
||||||
{
|
{
|
||||||
var takeRes = await _cs.RemoveAsync(userId, amount, new("slot", "bet"));
|
var takeRes = await _cs.RemoveAsync(userId, amount, new("slot", "bet"));
|
||||||
|
@@ -10,7 +10,6 @@ using NadekoBot.Services.Database.Models;
|
|||||||
|
|
||||||
namespace NadekoBot.Modules.Gambling.Services;
|
namespace NadekoBot.Modules.Gambling.Services;
|
||||||
|
|
||||||
// todo waifu price int
|
|
||||||
public class WaifuService : INService, IReadyExecutor
|
public class WaifuService : INService, IReadyExecutor
|
||||||
{
|
{
|
||||||
private readonly DbService _db;
|
private readonly DbService _db;
|
||||||
|
@@ -6,6 +6,9 @@ currency:
|
|||||||
sign: "🌸"
|
sign: "🌸"
|
||||||
# What is the name of the currency
|
# What is the name of the currency
|
||||||
name: Nadeko Flower
|
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)
|
# Minimum amount users can bet (>=0)
|
||||||
minBet: 0
|
minBet: 0
|
||||||
# Maximum amount users can bet
|
# Maximum amount users can bet
|
||||||
|
Reference in New Issue
Block a user