Added unclaimed waifu decay to gambling.yml

Fixed regular decay. It was doing the opposite of what the comment says. All waifu decays will be reset
This commit is contained in:
Toastie (DCS Team) 2024-08-25 20:12:09 +12:00
parent e324d49cbc
commit d478a6ca72
Signed by: toastie_t0ast
GPG key ID: 27F3B6855AFD40A4
5 changed files with 142 additions and 100 deletions

View file

@ -11,7 +11,7 @@ namespace EllieBot.Modules.Gambling.Common;
public sealed partial class GamblingConfig : ICloneable<GamblingConfig> public sealed partial class GamblingConfig : ICloneable<GamblingConfig>
{ {
[Comment("""DO NOT CHANGE""")] [Comment("""DO NOT CHANGE""")]
public int Version { get; set; } = 2; public int Version { get; set; } = 8;
[Comment("""Currency settings""")] [Comment("""Currency settings""")]
public CurrencyConfig Currency { get; set; } public CurrencyConfig Currency { get; set; }
@ -278,7 +278,13 @@ public sealed partial class WaifuConfig
Set 0 to disable Set 0 to disable
For example if a waifu has a price of 500$, setting this value to 10 would reduce the waifu value by 10% (50$) For example if a waifu has a price of 500$, setting this value to 10 would reduce the waifu value by 10% (50$)
""")] """)]
public int Percent { get; set; } = 0; public int UnclaimedDecayPercent { get; set; } = 0;
[Comment("""
Claimed waifus will decay by this percentage (0 - 100).
Default is 0 (disabled)
""")]
public int ClaimedDecayPercent { get; set; } = 0;
[Comment("""How often to decay waifu values, in hours""")] [Comment("""How often to decay waifu values, in hours""")]
public int HourInterval { get; set; } = 24; public int HourInterval { get; set; } = 24;

View file

@ -190,5 +190,14 @@ public sealed class GamblingConfigService : ConfigServiceBase<GamblingConfig>
c.Version = 7; c.Version = 7;
}); });
} }
if (data.Version < 8)
{
ModifyConfig(c =>
{
c.Version = 8;
c.Waifu.Decay.UnclaimedDecayPercent = 0;
});
}
} }
} }

View file

@ -531,11 +531,18 @@ public class WaifuService : IEService, IReadyExecutor
{ {
try try
{ {
var multi = _gss.Data.Waifu.Decay.Percent / 100f; var decay = _gss.Data.Waifu.Decay;
var minPrice = _gss.Data.Waifu.Decay.MinPrice;
var decayInterval = _gss.Data.Waifu.Decay.HourInterval;
if (multi is < 0f or > 1f || decayInterval < 0) var unclaimedMulti = 1 - (decay.UnclaimedDecayPercent / 100f);
var claimedMulti = 1 - (decay.ClaimedDecayPercent / 100f);
var minPrice = decay.MinPrice;
var decayInterval = decay.HourInterval;
if (decayInterval <= 0)
continue;
if ((unclaimedMulti < 0 || unclaimedMulti > 1) && (claimedMulti < 0 || claimedMulti > 1))
continue; continue;
var now = DateTime.UtcNow; var now = DateTime.UtcNow;
@ -554,15 +561,29 @@ public class WaifuService : IEService, IReadyExecutor
await _cache.AddAsync(_waifuDecayKey, nowB); await _cache.AddAsync(_waifuDecayKey, nowB);
if (unclaimedMulti is > 0 and <= 1)
{
await using var uow = _db.GetDbContext(); await using var uow = _db.GetDbContext();
await uow.GetTable<WaifuInfo>() await uow.GetTable<WaifuInfo>()
.Where(x => x.Price > minPrice && x.ClaimerId == null) .Where(x => x.Price > minPrice && x.ClaimerId == null)
.UpdateAsync(old => new() .UpdateAsync(old => new()
{ {
Price = (long)(old.Price * multi) Price = (long)(old.Price * unclaimedMulti)
}); });
} }
if (claimedMulti is > 0 and <= 1)
{
await using var uow = _db.GetDbContext();
await uow.GetTable<WaifuInfo>()
.Where(x => x.Price > minPrice && x.ClaimerId == null)
.UpdateAsync(old => new()
{
Price = (long)(old.Price * claimedMulti)
});
}
}
catch (Exception ex) catch (Exception ex)
{ {
Log.Error(ex, "Unexpected error occured in waifu decay loop: {ErrorMessage}", ex.Message); Log.Error(ex, "Unexpected error occured in waifu decay loop: {ErrorMessage}", ex.Message);

View file

@ -63,6 +63,8 @@ public static class WaifuExtensions
public static async Task<WaifuInfoStats> GetWaifuInfoAsync(this DbContext ctx, ulong userId) public static async Task<WaifuInfoStats> GetWaifuInfoAsync(this DbContext ctx, ulong userId)
{ {
await ctx.EnsureUserCreatedAsync(userId);
await ctx.Set<WaifuInfo>() await ctx.Set<WaifuInfo>()
.ToLinqToDBTable() .ToLinqToDBTable()
.InsertOrUpdateAsync(() => new() .InsertOrUpdateAsync(() => new()
@ -78,7 +80,8 @@ public static class WaifuExtensions
WaifuId = ctx.Set<DiscordUser>().Where(x => x.UserId == userId).Select(x => x.Id).First() WaifuId = ctx.Set<DiscordUser>().Where(x => x.UserId == userId).Select(x => x.Id).First()
}); });
var toReturn = ctx.Set<WaifuInfo>().AsQueryable() var toReturn = ctx.Set<WaifuInfo>()
.AsQueryable()
.Where(w => w.WaifuId .Where(w => w.WaifuId
== ctx.Set<DiscordUser>() == ctx.Set<DiscordUser>()
.AsQueryable() .AsQueryable()

View file

@ -1,5 +1,5 @@
# DO NOT CHANGE # DO NOT CHANGE
version: 7 version: 8
# Currency settings # Currency settings
currency: currency:
# What is the emoji/character which represents the currency # What is the emoji/character which represents the currency
@ -128,7 +128,10 @@ waifu:
# Percentage (0 - 100) of the waifu value to reduce. # Percentage (0 - 100) of the waifu value to reduce.
# Set 0 to disable # Set 0 to disable
# For example if a waifu has a price of 500$, setting this value to 10 would reduce the waifu value by 10% (50$) # For example if a waifu has a price of 500$, setting this value to 10 would reduce the waifu value by 10% (50$)
percent: 0 unclaimedDecayPercent: 0
# Claimed waifus will decay by this percentage (0 - 100).
# Default is 0 (disabled)
claimedDecayPercent: 0
# How often to decay waifu values, in hours # How often to decay waifu values, in hours
hourInterval: 24 hourInterval: 24
# Minimum waifu price required for the decay to be applied. # Minimum waifu price required for the decay to be applied.