Updated Gambling module

I really need to rename this module.
This commit is contained in:
Toastie (DCS Team) 2024-07-15 15:44:07 +12:00
parent 32db627aef
commit 90dd47e013
Signed by: toastie_t0ast
GPG key ID: 27F3B6855AFD40A4
5 changed files with 239 additions and 133 deletions

View file

@ -74,6 +74,27 @@ public partial class Gambling
} }
} }
[Cmd]
[OwnerOnly]
public async Task BankBalance([Leftover] IUser user)
{
var bal = await _bank.GetBalanceAsync(user.Id);
var eb = _sender.CreateEmbed()
.WithOkColor()
.WithDescription(GetText(strs.bank_balance_other(user.ToString(), N(bal))));
try
{
await Response().User(ctx.User).Embed(eb).SendAsync();
await ctx.OkAsync();
}
catch
{
await Response().Error(strs.cant_dm).SendAsync();
}
}
private async Task BankTakeInternalAsync(long amount, ulong userId) private async Task BankTakeInternalAsync(long amount, ulong userId)
{ {
if (await _bank.TakeAsync(userId, amount)) if (await _bank.TakeAsync(userId, amount))

View file

@ -3,6 +3,7 @@ using EllieBot.Modules.Gambling.Common;
using EllieBot.Modules.Gambling.Common.Waifu; using EllieBot.Modules.Gambling.Common.Waifu;
using EllieBot.Modules.Gambling.Services; using EllieBot.Modules.Gambling.Services;
using EllieBot.Db.Models; using EllieBot.Db.Models;
using TwitchLib.Api.Helix.Models.Teams;
namespace EllieBot.Modules.Gambling; namespace EllieBot.Modules.Gambling;
@ -317,7 +318,9 @@ public partial class Gambling
.AddField(GetText(strs.price), N(wi.Price), true) .AddField(GetText(strs.price), N(wi.Price), true)
.AddField(GetText(strs.claimed_by), wi.ClaimerName ?? nobody, true) .AddField(GetText(strs.claimed_by), wi.ClaimerName ?? nobody, true)
.AddField(GetText(strs.likes), wi.AffinityName ?? nobody, true) .AddField(GetText(strs.likes), wi.AffinityName ?? nobody, true)
.AddField(GetText(strs.changes_of_heart), $"{wi.AffinityCount} - \"the {affInfo}\"", true) .AddField(GetText(strs.changes_of_heart),
$"{wi.AffinityCount} - \"the {affInfo}\"",
true)
.AddField(GetText(strs.divorces), wi.DivorceCount.ToString(), true) .AddField(GetText(strs.divorces), wi.DivorceCount.ToString(), true)
.AddField("\u200B", "\u200B", true) .AddField("\u200B", "\u200B", true)
.AddField(GetText(strs.fans(fansList.Count)), fansStr, true) .AddField(GetText(strs.fans(fansList.Count)), fansStr, true)
@ -364,30 +367,27 @@ public partial class Gambling
[Cmd] [Cmd]
[RequireContext(ContextType.Guild)] [RequireContext(ContextType.Guild)]
[Priority(0)] [Priority(0)]
public async Task WaifuGift(string itemName, [Leftover] IUser waifu) public async Task WaifuGift(MultipleWaifuItems items, [Leftover] IUser waifu)
{ {
if (waifu.Id == ctx.User.Id) if (waifu.Id == ctx.User.Id)
return; return;
var allItems = _service.GetWaifuItems(); var sucess = await _service.GiftWaifuAsync(ctx.User, waifu, items.Item, items.Count);
var item = allItems.FirstOrDefault(x => x.Name.ToLowerInvariant() == itemName.ToLowerInvariant());
if (item is null)
{
await Response().Error(strs.waifu_gift_not_exist).SendAsync();
return;
}
var sucess = await _service.GiftWaifuAsync(ctx.User, waifu, item);
if (sucess) if (sucess)
{ {
await Response() await Response()
.Confirm(strs.waifu_gift(Format.Bold(item + " " + item.ItemEmoji), .Confirm(strs.waifu_gift(Format.Bold($"{GetCountString(items)}{items.Item} {items.Item.ItemEmoji}"),
Format.Bold(waifu.ToString()))) Format.Bold(waifu.ToString())))
.SendAsync(); .SendAsync();
} }
else else
await Response().Error(strs.not_enough(CurrencySign)).SendAsync(); await Response().Error(strs.not_enough(CurrencySign)).SendAsync();
} }
private static string GetCountString(MultipleWaifuItems items)
=> items.Count > 1
? $"{items.Count}x "
: string.Empty;
} }
} }

View file

@ -7,6 +7,7 @@ using EllieBot.Db;
using EllieBot.Db.Models; using EllieBot.Db.Models;
using EllieBot.Modules.Gambling.Common; using EllieBot.Modules.Gambling.Common;
using EllieBot.Modules.Gambling.Common.Waifu; using EllieBot.Modules.Gambling.Common.Waifu;
using SixLabors.ImageSharp;
namespace EllieBot.Modules.Gambling.Services; namespace EllieBot.Modules.Gambling.Services;
@ -89,9 +90,14 @@ public class WaifuService : IEService, IReadyExecutor
if (waifu is null) if (waifu is null)
return settings.Waifu.MinPrice; return settings.Waifu.MinPrice;
var divorces = uow.Set<WaifuUpdate>().Count(x var divorces = uow.Set<WaifuUpdate>()
=> x.Old != null && x.Old.UserId == user.Id && x.UpdateType == WaifuUpdateType.Claimed && x.New == null); .Count(x
var affs = uow.Set<WaifuUpdate>().AsQueryable() => x.Old != null
&& x.Old.UserId == user.Id
&& x.UpdateType == WaifuUpdateType.Claimed
&& x.New == null);
var affs = uow.Set<WaifuUpdate>()
.AsQueryable()
.Where(w => w.User.UserId == user.Id .Where(w => w.User.UserId == user.Id
&& w.UpdateType == WaifuUpdateType.AffinityChanged && w.UpdateType == WaifuUpdateType.AffinityChanged
&& w.New != null) && w.New != null)
@ -110,12 +116,14 @@ public class WaifuService : IEService, IReadyExecutor
if (!await _cs.RemoveAsync(user.Id, price, new("waifu", "reset"))) if (!await _cs.RemoveAsync(user.Id, price, new("waifu", "reset")))
return false; return false;
var affs = uow.Set<WaifuUpdate>().AsQueryable() var affs = uow.Set<WaifuUpdate>()
.AsQueryable()
.Where(w => w.User.UserId == user.Id .Where(w => w.User.UserId == user.Id
&& w.UpdateType == WaifuUpdateType.AffinityChanged && w.UpdateType == WaifuUpdateType.AffinityChanged
&& w.New != null); && w.New != null);
var divorces = uow.Set<WaifuUpdate>().AsQueryable() var divorces = uow.Set<WaifuUpdate>()
.AsQueryable()
.Where(x => x.Old != null .Where(x => x.Old != null
&& x.Old.UserId == user.Id && x.Old.UserId == user.Id
&& x.UpdateType == WaifuUpdateType.Claimed && x.UpdateType == WaifuUpdateType.Claimed
@ -158,14 +166,16 @@ public class WaifuService : IEService, IReadyExecutor
result = WaifuClaimResult.NotEnoughFunds; result = WaifuClaimResult.NotEnoughFunds;
else else
{ {
uow.Set<WaifuInfo>().Add(w = new() uow.Set<WaifuInfo>()
.Add(w = new()
{ {
Waifu = waifu, Waifu = waifu,
Claimer = claimer, Claimer = claimer,
Affinity = null, Affinity = null,
Price = amount Price = amount
}); });
uow.Set<WaifuUpdate>().Add(new() uow.Set<WaifuUpdate>()
.Add(new()
{ {
User = waifu, User = waifu,
Old = null, Old = null,
@ -186,7 +196,8 @@ public class WaifuService : IEService, IReadyExecutor
w.Price = amount + (amount / 4); w.Price = amount + (amount / 4);
result = WaifuClaimResult.Success; result = WaifuClaimResult.Success;
uow.Set<WaifuUpdate>().Add(new() uow.Set<WaifuUpdate>()
.Add(new()
{ {
User = w.Waifu, User = w.Waifu,
Old = oldClaimer, Old = oldClaimer,
@ -206,7 +217,8 @@ public class WaifuService : IEService, IReadyExecutor
w.Price = amount; w.Price = amount;
result = WaifuClaimResult.Success; result = WaifuClaimResult.Success;
uow.Set<WaifuUpdate>().Add(new() uow.Set<WaifuUpdate>()
.Add(new()
{ {
User = w.Waifu, User = w.Waifu,
Old = oldClaimer, Old = oldClaimer,
@ -248,7 +260,8 @@ public class WaifuService : IEService, IReadyExecutor
else if (w is null) else if (w is null)
{ {
var thisUser = uow.GetOrCreateUser(user); var thisUser = uow.GetOrCreateUser(user);
uow.Set<WaifuInfo>().Add(new() uow.Set<WaifuInfo>()
.Add(new()
{ {
Affinity = newAff, Affinity = newAff,
Waifu = thisUser, Waifu = thisUser,
@ -257,7 +270,8 @@ public class WaifuService : IEService, IReadyExecutor
}); });
success = true; success = true;
uow.Set<WaifuUpdate>().Add(new() uow.Set<WaifuUpdate>()
.Add(new()
{ {
User = thisUser, User = thisUser,
Old = null, Old = null,
@ -272,7 +286,8 @@ public class WaifuService : IEService, IReadyExecutor
w.Affinity = newAff; w.Affinity = newAff;
success = true; success = true;
uow.Set<WaifuUpdate>().Add(new() uow.Set<WaifuUpdate>()
.Add(new()
{ {
User = w.Waifu, User = w.Waifu,
Old = oldAff, Old = oldAff,
@ -343,7 +358,8 @@ public class WaifuService : IEService, IReadyExecutor
var oldClaimer = w.Claimer; var oldClaimer = w.Claimer;
w.Claimer = null; w.Claimer = null;
uow.Set<WaifuUpdate>().Add(new() uow.Set<WaifuUpdate>()
.Add(new()
{ {
User = w.Waifu, User = w.Waifu,
Old = oldClaimer, Old = oldClaimer,
@ -358,16 +374,29 @@ public class WaifuService : IEService, IReadyExecutor
return (w, result, amount, remaining); return (w, result, amount, remaining);
} }
public async Task<bool> GiftWaifuAsync(IUser from, IUser giftedWaifu, WaifuItemModel itemObj) public async Task<bool> GiftWaifuAsync(
IUser from,
IUser giftedWaifu,
WaifuItemModel itemObj,
int count)
{ {
if (!await _cs.RemoveAsync(from, itemObj.Price, new("waifu", "item"))) ArgumentOutOfRangeException.ThrowIfLessThan(count, 1, nameof(count));
if (!await _cs.RemoveAsync(from, itemObj.Price * count, new("waifu", "item")))
return false; return false;
var totalValue = itemObj.Price * count;
await using var uow = _db.GetDbContext(); await using var uow = _db.GetDbContext();
var w = uow.Set<WaifuInfo>().ByWaifuUserId(giftedWaifu.Id, set => set.Include(x => x.Items).Include(x => x.Claimer)); var w = uow.Set<WaifuInfo>()
.ByWaifuUserId(giftedWaifu.Id,
set => set
.Include(x => x.Items)
.Include(x => x.Claimer));
if (w is null) if (w is null)
{ {
uow.Set<WaifuInfo>().Add(w = new() uow.Set<WaifuInfo>()
.Add(w = new()
{ {
Affinity = null, Affinity = null,
Claimer = null, Claimer = null,
@ -378,20 +407,21 @@ public class WaifuService : IEService, IReadyExecutor
if (!itemObj.Negative) if (!itemObj.Negative)
{ {
w.Items.Add(new() w.Items.AddRange(Enumerable.Range(0, count)
.Select((_) => new WaifuItem()
{ {
Name = itemObj.Name.ToLowerInvariant(), Name = itemObj.Name.ToLowerInvariant(),
ItemEmoji = itemObj.ItemEmoji ItemEmoji = itemObj.ItemEmoji
}); }));
if (w.Claimer?.UserId == from.Id) if (w.Claimer?.UserId == from.Id)
w.Price += (long)(itemObj.Price * _gss.Data.Waifu.Multipliers.GiftEffect); w.Price += (long)(totalValue * _gss.Data.Waifu.Multipliers.GiftEffect);
else else
w.Price += itemObj.Price / 2; w.Price += totalValue / 2;
} }
else else
{ {
w.Price -= (long)(itemObj.Price * _gss.Data.Waifu.Multipliers.NegativeGiftEffect); w.Price -= (long)(totalValue * _gss.Data.Waifu.Multipliers.NegativeGiftEffect);
if (w.Price < 1) if (w.Price < 1)
w.Price = 1; w.Price = 1;
} }
@ -492,6 +522,7 @@ public class WaifuService : IEService, IReadyExecutor
} }
private static readonly TypedKey<long> _waifuDecayKey = $"waifu:last_decay"; private static readonly TypedKey<long> _waifuDecayKey = $"waifu:last_decay";
public async Task OnReadyAsync() public async Task OnReadyAsync()
{ {
// only decay waifu values from shard 0 // only decay waifu values from shard 0
@ -533,7 +564,6 @@ public class WaifuService : IEService, IReadyExecutor
{ {
Price = (long)(old.Price * multi) Price = (long)(old.Price * multi)
}); });
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -557,6 +587,7 @@ public class WaifuService : IEService, IReadyExecutor
.Select(x => $"{x.Username}#{x.Discriminator}") .Select(x => $"{x.Username}#{x.Discriminator}")
.ToListAsyncEF(); .ToListAsyncEF();
} }
public async Task<IReadOnlyCollection<string>> GetFansNames(int waifuId) public async Task<IReadOnlyCollection<string>> GetFansNames(int waifuId)
{ {
await using var ctx = _db.GetDbContext(); await using var ctx = _db.GetDbContext();
@ -573,7 +604,8 @@ public class WaifuService : IEService, IReadyExecutor
{ {
await using var ctx = _db.GetDbContext(); await using var ctx = _db.GetDbContext();
return await ctx.GetTable<WaifuItem>() return await ctx.GetTable<WaifuItem>()
.Where(x => x.WaifuInfoId == ctx.GetTable<WaifuInfo>() .Where(x => x.WaifuInfoId
== ctx.GetTable<WaifuInfo>()
.Where(x => x.WaifuId == waifuId) .Where(x => x.WaifuId == waifuId)
.Select(x => x.Id) .Select(x => x.Id)
.FirstOrDefault()) .FirstOrDefault())

View file

@ -0,0 +1,6 @@
#nullable disable
using EllieBot.Modules.Gambling.Common;
namespace EllieBot.Modules.Gambling;
public record class MultipleWaifuItems(int Count, WaifuItemModel Item);

View file

@ -0,0 +1,47 @@
#nullable disable
using EllieBot.Common.TypeReaders;
using EllieBot.Modules.Gambling.Services;
using System.Text.RegularExpressions;
namespace EllieBot.Modules.Gambling;
public partial class MultipleWaifuItemsTypeReader : EllieTypeReader<MultipleWaifuItems>
{
private readonly WaifuService _service;
[GeneratedRegex(@"(?:(?<count>\d+)[x*])?(?<item>.+)")]
private static partial Regex ItemRegex();
public MultipleWaifuItemsTypeReader(WaifuService service)
{
_service = service;
}
public override ValueTask<TypeReaderResult<MultipleWaifuItems>> ReadAsync(ICommandContext ctx, string input)
{
input = input.ToLowerInvariant();
var match = ItemRegex().Match(input);
if (!match.Success)
{
return new(Discord.Commands.TypeReaderResult.FromError(CommandError.ParseFailed, "Invalid input."));
}
var count = 1;
if (match.Groups["count"].Success)
{
if (!int.TryParse(match.Groups["count"].Value, out count) || count < 1)
{
return new(Discord.Commands.TypeReaderResult.FromError(CommandError.ParseFailed, "Invalid count."));
}
}
var itemName = match.Groups["item"].Value?.ToLowerInvariant();
var allItems = _service.GetWaifuItems();
var item = allItems.FirstOrDefault(x => x.Name.ToLowerInvariant() == itemName);
if (item is null)
{
return new(Discord.Commands.TypeReaderResult.FromError(CommandError.ParseFailed, "Waifu gift does not exist."));
}
return new(Discord.Commands.TypeReaderResult.FromSuccess(new MultipleWaifuItems(count, item)));
}
}