Added an option to award currency based on received xp

This commit is contained in:
Kwoth
2022-08-26 13:41:30 +02:00
parent bed36f8784
commit 9eae27bc15
6 changed files with 63 additions and 39 deletions

View File

@@ -27,7 +27,7 @@ public class DefaultWallet : IWallet
.FirstOrDefaultAsync();
}
public async Task<bool> Take(long amount, TxData txData)
public async Task<bool> Take(long amount, TxData? txData)
{
if (amount < 0)
throw new ArgumentOutOfRangeException(nameof(amount), "Amount to take must be non negative.");
@@ -44,24 +44,27 @@ public class DefaultWallet : IWallet
if (changed == 0)
return false;
await ctx
.GetTable<CurrencyTransaction>()
.InsertAsync(() => new()
{
Amount = -amount,
Note = txData.Note,
UserId = userId,
Type = txData.Type,
Extra = txData.Extra,
OtherId = txData.OtherId,
DateAdded = DateTime.UtcNow
});
if (txData is not null)
{
await ctx
.GetTable<CurrencyTransaction>()
.InsertAsync(() => new()
{
Amount = -amount,
Note = txData.Note,
UserId = userId,
Type = txData.Type,
Extra = txData.Extra,
OtherId = txData.OtherId,
DateAdded = DateTime.UtcNow
});
}
return true;
}
public async Task Add(long amount, TxData txData)
public async Task Add(long amount, TxData? txData)
{
if (amount <= 0)
throw new ArgumentOutOfRangeException(nameof(amount), "Amount must be greater than 0.");
@@ -92,16 +95,19 @@ public class DefaultWallet : IWallet
await tran.CommitAsync();
}
await ctx.GetTable<CurrencyTransaction>()
.InsertAsync(() => new()
{
Amount = amount,
UserId = userId,
Note = txData.Note,
Type = txData.Type,
Extra = txData.Extra,
OtherId = txData.OtherId,
DateAdded = DateTime.UtcNow
});
if (txData is not null)
{
await ctx.GetTable<CurrencyTransaction>()
.InsertAsync(() => new()
{
Amount = amount,
UserId = userId,
Note = txData.Note,
Type = txData.Type,
Extra = txData.Extra,
OtherId = txData.OtherId,
DateAdded = DateTime.UtcNow
});
}
}
}

View File

@@ -5,31 +5,35 @@ public interface IWallet
public ulong UserId { get; }
public Task<long> GetBalance();
public Task<bool> Take(long amount, TxData txData);
public Task Add(long amount, TxData txData);
public Task<bool> Take(long amount, TxData? txData);
public Task Add(long amount, TxData? txData);
public async Task<bool> Transfer(
long amount,
IWallet to,
TxData txData)
TxData? txData)
{
if (amount <= 0)
throw new ArgumentOutOfRangeException(nameof(amount), "Amount must be greater than 0.");
var succ = await Take(amount,
txData with
if (txData is not null)
txData = txData with
{
OtherId = to.UserId
});
};
var succ = await Take(amount, txData);
if (!succ)
return false;
await to.Add(amount,
txData with
if (txData is not null)
txData = txData with
{
OtherId = UserId
});
};
await to.Add(amount, txData);
return true;
}