mirror of
https://gitlab.com/Kwoth/nadekobot.git
synced 2025-09-12 02:08:27 -04:00
Restructured the project structure back to the way it was, there's no reasonable way to split the modules
This commit is contained in:
118
src/NadekoBot/Modules/Gambling/Bank/BankCommands.cs
Normal file
118
src/NadekoBot/Modules/Gambling/Bank/BankCommands.cs
Normal file
@@ -0,0 +1,118 @@
|
||||
using NadekoBot.Common.TypeReaders;
|
||||
using NadekoBot.Modules.Gambling.Bank;
|
||||
using NadekoBot.Modules.Gambling.Common;
|
||||
using NadekoBot.Modules.Gambling.Services;
|
||||
|
||||
namespace NadekoBot.Modules.Gambling;
|
||||
|
||||
public partial class Gambling
|
||||
{
|
||||
[Name("Bank")]
|
||||
[Group("bank")]
|
||||
public partial class BankCommands : GamblingModule<IBankService>
|
||||
{
|
||||
private readonly IBankService _bank;
|
||||
private readonly DiscordSocketClient _client;
|
||||
|
||||
public BankCommands(GamblingConfigService gcs,
|
||||
IBankService bank,
|
||||
DiscordSocketClient client) : base(gcs)
|
||||
{
|
||||
_bank = bank;
|
||||
_client = client;
|
||||
}
|
||||
|
||||
[Cmd]
|
||||
public async Task BankDeposit([OverrideTypeReader(typeof(BalanceTypeReader))] long amount)
|
||||
{
|
||||
if (amount <= 0)
|
||||
return;
|
||||
|
||||
if (await _bank.DepositAsync(ctx.User.Id, amount))
|
||||
{
|
||||
await ReplyConfirmLocalizedAsync(strs.bank_deposited(N(amount)));
|
||||
}
|
||||
else
|
||||
{
|
||||
await ReplyErrorLocalizedAsync(strs.not_enough(CurrencySign));
|
||||
}
|
||||
}
|
||||
|
||||
[Cmd]
|
||||
public async Task BankWithdraw([OverrideTypeReader(typeof(BankBalanceTypeReader))] long amount)
|
||||
{
|
||||
if (amount <= 0)
|
||||
return;
|
||||
|
||||
if (await _bank.WithdrawAsync(ctx.User.Id, amount))
|
||||
{
|
||||
await ReplyConfirmLocalizedAsync(strs.bank_withdrew(N(amount)));
|
||||
}
|
||||
else
|
||||
{
|
||||
await ReplyErrorLocalizedAsync(strs.bank_withdraw_insuff(CurrencySign));
|
||||
}
|
||||
}
|
||||
|
||||
[Cmd]
|
||||
public async Task BankBalance()
|
||||
{
|
||||
var bal = await _bank.GetBalanceAsync(ctx.User.Id);
|
||||
|
||||
var eb = _eb.Create(ctx)
|
||||
.WithOkColor()
|
||||
.WithDescription(GetText(strs.bank_balance(N(bal))));
|
||||
|
||||
try
|
||||
{
|
||||
await ctx.User.EmbedAsync(eb);
|
||||
await ctx.OkAsync();
|
||||
}
|
||||
catch
|
||||
{
|
||||
await ReplyErrorLocalizedAsync(strs.cant_dm);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task BankTakeInternalAsync(long amount, ulong userId)
|
||||
{
|
||||
if (await _bank.TakeAsync(userId, amount))
|
||||
{
|
||||
await ctx.OkAsync();
|
||||
return;
|
||||
}
|
||||
|
||||
await ReplyErrorLocalizedAsync(strs.take_fail(N(amount),
|
||||
_client.GetUser(userId)?.ToString()
|
||||
?? userId.ToString(),
|
||||
CurrencySign));
|
||||
}
|
||||
|
||||
private async Task BankAwardInternalAsync(long amount, ulong userId)
|
||||
{
|
||||
if (await _bank.AwardAsync(userId, amount))
|
||||
{
|
||||
await ctx.OkAsync();
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
[Cmd]
|
||||
[OwnerOnly]
|
||||
[Priority(1)]
|
||||
public async Task BankTake(long amount, [Leftover] IUser user)
|
||||
=> await BankTakeInternalAsync(amount, user.Id);
|
||||
|
||||
[Cmd]
|
||||
[OwnerOnly]
|
||||
[Priority(0)]
|
||||
public async Task BankTake(long amount, ulong userId)
|
||||
=> await BankTakeInternalAsync(amount, userId);
|
||||
|
||||
[Cmd]
|
||||
[OwnerOnly]
|
||||
public async Task BankAward(long amount, [Leftover] IUser user)
|
||||
=> await BankAwardInternalAsync(amount, user.Id);
|
||||
}
|
||||
}
|
119
src/NadekoBot/Modules/Gambling/Bank/BankService.cs
Normal file
119
src/NadekoBot/Modules/Gambling/Bank/BankService.cs
Normal file
@@ -0,0 +1,119 @@
|
||||
using LinqToDB;
|
||||
using LinqToDB.EntityFrameworkCore;
|
||||
using NadekoBot.Db.Models;
|
||||
|
||||
namespace NadekoBot.Modules.Gambling.Bank;
|
||||
|
||||
public sealed class BankService : IBankService, INService
|
||||
{
|
||||
private readonly ICurrencyService _cur;
|
||||
private readonly DbService _db;
|
||||
|
||||
public BankService(ICurrencyService cur, DbService db)
|
||||
{
|
||||
_cur = cur;
|
||||
_db = db;
|
||||
}
|
||||
|
||||
public async Task<bool> AwardAsync(ulong userId, long amount)
|
||||
{
|
||||
if (amount <= 0)
|
||||
throw new ArgumentOutOfRangeException(nameof(amount));
|
||||
|
||||
await using var ctx = _db.GetDbContext();
|
||||
await ctx.GetTable<BankUser>()
|
||||
.InsertOrUpdateAsync(() => new()
|
||||
{
|
||||
UserId = userId,
|
||||
Balance = amount
|
||||
},
|
||||
(old) => new()
|
||||
{
|
||||
Balance = old.Balance + amount
|
||||
},
|
||||
() => new()
|
||||
{
|
||||
UserId = userId
|
||||
});
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public async Task<bool> TakeAsync(ulong userId, long amount)
|
||||
{
|
||||
if (amount <= 0)
|
||||
throw new ArgumentOutOfRangeException(nameof(amount));
|
||||
|
||||
await using var ctx = _db.GetDbContext();
|
||||
var rows = await ctx.Set<BankUser>()
|
||||
.ToLinqToDBTable()
|
||||
.Where(x => x.UserId == userId && x.Balance >= amount)
|
||||
.UpdateAsync((old) => new()
|
||||
{
|
||||
Balance = old.Balance - amount
|
||||
});
|
||||
|
||||
return rows > 0;
|
||||
}
|
||||
|
||||
public async Task<bool> DepositAsync(ulong userId, long amount)
|
||||
{
|
||||
if (amount <= 0)
|
||||
throw new ArgumentOutOfRangeException(nameof(amount));
|
||||
|
||||
if (!await _cur.RemoveAsync(userId, amount, new("bank", "deposit")))
|
||||
return false;
|
||||
|
||||
await using var ctx = _db.GetDbContext();
|
||||
await ctx.Set<BankUser>()
|
||||
.ToLinqToDBTable()
|
||||
.InsertOrUpdateAsync(() => new()
|
||||
{
|
||||
UserId = userId,
|
||||
Balance = amount
|
||||
},
|
||||
(old) => new()
|
||||
{
|
||||
Balance = old.Balance + amount
|
||||
},
|
||||
() => new()
|
||||
{
|
||||
UserId = userId
|
||||
});
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public async Task<bool> WithdrawAsync(ulong userId, long amount)
|
||||
{
|
||||
if (amount <= 0)
|
||||
throw new ArgumentOutOfRangeException(nameof(amount));
|
||||
|
||||
await using var ctx = _db.GetDbContext();
|
||||
var rows = await ctx.Set<BankUser>()
|
||||
.ToLinqToDBTable()
|
||||
.Where(x => x.UserId == userId && x.Balance >= amount)
|
||||
.UpdateAsync((old) => new()
|
||||
{
|
||||
Balance = old.Balance - amount
|
||||
});
|
||||
|
||||
if (rows > 0)
|
||||
{
|
||||
await _cur.AddAsync(userId, amount, new("bank", "withdraw"));
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public async Task<long> GetBalanceAsync(ulong userId)
|
||||
{
|
||||
await using var ctx = _db.GetDbContext();
|
||||
return (await ctx.Set<BankUser>()
|
||||
.ToLinqToDBTable()
|
||||
.FirstOrDefaultAsync(x => x.UserId == userId))
|
||||
?.Balance
|
||||
?? 0;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user