mirror of
https://gitlab.com/Kwoth/nadekobot.git
synced 2025-09-11 09:48:26 -04:00
- Improved .curtrs (It will now have a lot more useful data in the database, show Tx ids, and be partially localized)
- Added .curtr command which lets you see full information about one of your own transactions with the specified id - Added .WithAuthor(IUser) extension for embedbuilder
This commit is contained in:
@@ -25,7 +25,7 @@ public class CurrencyService : ICurrencyService, INService
|
||||
public async Task AddBulkAsync(
|
||||
IReadOnlyCollection<ulong> userIds,
|
||||
long amount,
|
||||
Extra extra,
|
||||
TxData txData,
|
||||
CurrencyType type = CurrencyType.Default)
|
||||
{
|
||||
if (type == CurrencyType.Default)
|
||||
@@ -34,7 +34,7 @@ public class CurrencyService : ICurrencyService, INService
|
||||
foreach (var userId in userIds)
|
||||
{
|
||||
var wallet = new DefaultWallet(userId, ctx);
|
||||
await wallet.Add(amount, extra);
|
||||
await wallet.Add(amount, txData);
|
||||
}
|
||||
|
||||
await ctx.SaveChangesAsync();
|
||||
@@ -47,7 +47,7 @@ public class CurrencyService : ICurrencyService, INService
|
||||
public async Task RemoveBulkAsync(
|
||||
IReadOnlyCollection<ulong> userIds,
|
||||
long amount,
|
||||
Extra extra,
|
||||
TxData txData,
|
||||
CurrencyType type = CurrencyType.Default)
|
||||
{
|
||||
if (type == CurrencyType.Default)
|
||||
@@ -68,54 +68,55 @@ public class CurrencyService : ICurrencyService, INService
|
||||
}
|
||||
|
||||
private CurrencyTransaction GetCurrencyTransaction(ulong userId, string reason, long amount)
|
||||
=> new() { Amount = amount, UserId = userId, Reason = reason ?? "-" };
|
||||
=> new() { Amount = amount, UserId = userId, Note = reason ?? "-" };
|
||||
|
||||
public async Task AddAsync(
|
||||
ulong userId,
|
||||
long amount,
|
||||
Extra extra)
|
||||
TxData txData)
|
||||
{
|
||||
await using var wallet = await GetWalletAsync(userId);
|
||||
await wallet.Add(amount, extra);
|
||||
await wallet.Add(amount, txData);
|
||||
}
|
||||
|
||||
public async Task AddAsync(
|
||||
IUser user,
|
||||
long amount,
|
||||
Extra extra)
|
||||
TxData txData)
|
||||
{
|
||||
await using var wallet = await GetWalletAsync(user.Id);
|
||||
await wallet.Add(amount, extra);
|
||||
await wallet.Add(amount, txData);
|
||||
}
|
||||
|
||||
public async Task<bool> RemoveAsync(
|
||||
ulong userId,
|
||||
long amount,
|
||||
Extra extra)
|
||||
TxData txData)
|
||||
{
|
||||
await using var wallet = await GetWalletAsync(userId);
|
||||
return await wallet.Take(amount, extra);
|
||||
return await wallet.Take(amount, txData);
|
||||
}
|
||||
|
||||
public async Task<bool> RemoveAsync(
|
||||
IUser user,
|
||||
long amount,
|
||||
Extra extra)
|
||||
TxData txData)
|
||||
{
|
||||
await using var wallet = await GetWalletAsync(user.Id);
|
||||
return await wallet.Take(amount, extra);
|
||||
return await wallet.Take(amount, txData);
|
||||
}
|
||||
|
||||
public async Task<bool> TransferAsync(
|
||||
ulong from,
|
||||
ulong to,
|
||||
ulong fromId,
|
||||
ulong toId,
|
||||
long amount,
|
||||
string fromName,
|
||||
string note)
|
||||
{
|
||||
await using var fromWallet = await GetWalletAsync(@from);
|
||||
await using var toWallet = await GetWalletAsync(to);
|
||||
await using var fromWallet = await GetWalletAsync(fromId);
|
||||
await using var toWallet = await GetWalletAsync(toId);
|
||||
|
||||
var extra = new Extra("transfer", "gift", note);
|
||||
var extra = new TxData("gift", fromName, note, fromId);
|
||||
|
||||
return await fromWallet.Transfer(amount, toWallet, extra);
|
||||
}
|
||||
|
@@ -24,7 +24,7 @@ public class DefaultWallet : IWallet
|
||||
.Select(x => x.CurrencyAmount)
|
||||
.FirstOrDefaultAsync();
|
||||
|
||||
public async Task<bool> Take(long amount, Extra extra)
|
||||
public async Task<bool> Take(long amount, TxData txData)
|
||||
{
|
||||
if (amount < 0)
|
||||
throw new ArgumentOutOfRangeException(nameof(amount), "Amount to take must be non negative.");
|
||||
@@ -39,20 +39,21 @@ public class DefaultWallet : IWallet
|
||||
if (changed == 0)
|
||||
return false;
|
||||
|
||||
// todo type, subtype
|
||||
// todo from? by?
|
||||
await _ctx.CreateLinqToDbContext()
|
||||
.InsertAsync(new CurrencyTransaction()
|
||||
{
|
||||
Amount = -amount,
|
||||
Reason = extra.Note,
|
||||
Note = txData.Note,
|
||||
UserId = UserId,
|
||||
Type = txData.Type,
|
||||
Extra = txData.Extra,
|
||||
OtherId = txData.OtherId
|
||||
});
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public async Task Add(long amount, Extra extra)
|
||||
public async Task Add(long amount, TxData txData)
|
||||
{
|
||||
if (amount <= 0)
|
||||
throw new ArgumentOutOfRangeException(nameof(amount), "Amount must be greater than 0.");
|
||||
@@ -60,6 +61,7 @@ public class DefaultWallet : IWallet
|
||||
await using (var tran = await _ctx.Database.BeginTransactionAsync())
|
||||
{
|
||||
var changed = await _ctx.DiscordUser
|
||||
.Where(x => x.UserId == UserId)
|
||||
.UpdateAsync(x => new()
|
||||
{
|
||||
CurrencyAmount = x.CurrencyAmount + amount
|
||||
@@ -82,8 +84,11 @@ public class DefaultWallet : IWallet
|
||||
var ct = new CurrencyTransaction()
|
||||
{
|
||||
Amount = amount,
|
||||
Reason = extra.Note,
|
||||
UserId = UserId,
|
||||
Note = txData.Note,
|
||||
Type = txData.Type,
|
||||
Extra = txData.Extra,
|
||||
OtherId = txData.OtherId,
|
||||
};
|
||||
|
||||
await _ctx.CreateLinqToDbContext()
|
||||
|
@@ -1,3 +0,0 @@
|
||||
namespace NadekoBot.Services.Currency;
|
||||
|
||||
public record class Extra(string Type, string Subtype, string Note = "", ulong OtherId = 0);
|
@@ -10,38 +10,39 @@ public interface ICurrencyService
|
||||
Task AddBulkAsync(
|
||||
IReadOnlyCollection<ulong> userIds,
|
||||
long amount,
|
||||
Extra extra,
|
||||
TxData txData,
|
||||
CurrencyType type = CurrencyType.Default);
|
||||
|
||||
Task RemoveBulkAsync(
|
||||
IReadOnlyCollection<ulong> userIds,
|
||||
long amount,
|
||||
Extra extra,
|
||||
TxData txData,
|
||||
CurrencyType type = CurrencyType.Default);
|
||||
|
||||
Task AddAsync(
|
||||
ulong userId,
|
||||
long amount,
|
||||
Extra extra);
|
||||
TxData txData);
|
||||
|
||||
Task AddAsync(
|
||||
IUser user,
|
||||
long amount,
|
||||
Extra extra);
|
||||
TxData txData);
|
||||
|
||||
Task<bool> RemoveAsync(
|
||||
ulong userId,
|
||||
long amount,
|
||||
Extra extra);
|
||||
TxData txData);
|
||||
|
||||
Task<bool> RemoveAsync(
|
||||
IUser user,
|
||||
long amount,
|
||||
Extra extra);
|
||||
TxData txData);
|
||||
|
||||
Task<bool> TransferAsync(
|
||||
ulong from,
|
||||
ulong to,
|
||||
long amount,
|
||||
string fromName,
|
||||
string note);
|
||||
}
|
@@ -5,19 +5,19 @@ public interface IWallet : IDisposable, IAsyncDisposable
|
||||
public ulong UserId { get; }
|
||||
|
||||
public Task<long> GetBalance();
|
||||
public Task<bool> Take(long amount, Extra extra);
|
||||
public Task Add(long amount, Extra extra);
|
||||
public Task<bool> Take(long amount, TxData txData);
|
||||
public Task Add(long amount, TxData txData);
|
||||
|
||||
public async Task<bool> Transfer(
|
||||
long amount,
|
||||
IWallet to,
|
||||
Extra extra)
|
||||
TxData txData)
|
||||
{
|
||||
if (amount <= 0)
|
||||
throw new ArgumentOutOfRangeException(nameof(amount), "Amount must be greater than 0.");
|
||||
|
||||
var succ = await Take(amount,
|
||||
extra with
|
||||
txData with
|
||||
{
|
||||
OtherId = to.UserId
|
||||
});
|
||||
@@ -26,7 +26,7 @@ public interface IWallet : IDisposable, IAsyncDisposable
|
||||
return false;
|
||||
|
||||
await to.Add(amount,
|
||||
extra with
|
||||
txData with
|
||||
{
|
||||
OtherId = UserId
|
||||
});
|
||||
|
3
src/NadekoBot/Services/Currency/TxData.cs
Normal file
3
src/NadekoBot/Services/Currency/TxData.cs
Normal file
@@ -0,0 +1,3 @@
|
||||
namespace NadekoBot.Services.Currency;
|
||||
|
||||
public record class TxData(string Type, string Extra, string? Note = "", ulong? OtherId = null);
|
Reference in New Issue
Block a user