dev: Added argumentoutofrange static methods, no functional change

This commit is contained in:
Kwoth
2024-05-13 14:50:55 +00:00
parent 7b2ce072ee
commit 52438f45e1
24 changed files with 69 additions and 116 deletions

View File

@@ -61,8 +61,7 @@ public sealed class AnimalRace : IDisposable
public async Task<AnimalRacingUser> JoinRace(ulong userId, string userName, long bet = 0)
{
if (bet < 0)
throw new ArgumentOutOfRangeException(nameof(bet));
ArgumentOutOfRangeException.ThrowIfNegative(bet);
var user = new AnimalRacingUser(userName, userId, bet);

View File

@@ -17,8 +17,7 @@ public sealed class BankService : IBankService, INService
public async Task<bool> AwardAsync(ulong userId, long amount)
{
if (amount <= 0)
throw new ArgumentOutOfRangeException(nameof(amount));
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(amount);
await using var ctx = _db.GetDbContext();
await ctx.GetTable<BankUser>()
@@ -41,9 +40,8 @@ public sealed class BankService : IBankService, INService
public async Task<bool> TakeAsync(ulong userId, long amount)
{
if (amount <= 0)
throw new ArgumentOutOfRangeException(nameof(amount));
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(amount);
await using var ctx = _db.GetDbContext();
var rows = await ctx.Set<BankUser>()
.ToLinqToDBTable()
@@ -58,9 +56,8 @@ public sealed class BankService : IBankService, INService
public async Task<bool> DepositAsync(ulong userId, long amount)
{
if (amount <= 0)
throw new ArgumentOutOfRangeException(nameof(amount));
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(amount);
if (!await _cur.RemoveAsync(userId, amount, new("bank", "deposit")))
return false;
@@ -86,9 +83,8 @@ public sealed class BankService : IBankService, INService
public async Task<bool> WithdrawAsync(ulong userId, long amount)
{
if (amount <= 0)
throw new ArgumentOutOfRangeException(nameof(amount));
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(amount);
await using var ctx = _db.GetDbContext();
var rows = await ctx.Set<BankUser>()
.ToLinqToDBTable()

View File

@@ -49,8 +49,7 @@ public class User : Player
public User(IUser user, long bet)
{
if (bet <= 0)
throw new ArgumentOutOfRangeException(nameof(bet));
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(bet);
Bet = bet;
DiscordUser = user;

View File

@@ -20,10 +20,8 @@ public class ShopService : IShopService, INService
public async Task<bool> ChangeEntryPriceAsync(ulong guildId, int index, int newPrice)
{
if (index < 0)
throw new ArgumentOutOfRangeException(nameof(index));
if (newPrice <= 0)
throw new ArgumentOutOfRangeException(nameof(newPrice));
ArgumentOutOfRangeException.ThrowIfNegative(index);
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(newPrice);
await using var uow = _db.GetDbContext();
var entries = GetEntriesInternal(uow, guildId);
@@ -38,8 +36,8 @@ public class ShopService : IShopService, INService
public async Task<bool> ChangeEntryNameAsync(ulong guildId, int index, string newName)
{
if (index < 0)
throw new ArgumentOutOfRangeException(nameof(index));
ArgumentOutOfRangeException.ThrowIfNegative(index);
if (string.IsNullOrWhiteSpace(newName))
throw new ArgumentNullException(nameof(newName));
@@ -56,10 +54,8 @@ public class ShopService : IShopService, INService
public async Task<bool> SwapEntriesAsync(ulong guildId, int index1, int index2)
{
if (index1 < 0)
throw new ArgumentOutOfRangeException(nameof(index1));
if (index2 < 0)
throw new ArgumentOutOfRangeException(nameof(index2));
ArgumentOutOfRangeException.ThrowIfNegative(index1);
ArgumentOutOfRangeException.ThrowIfNegative(index2);
await using var uow = _db.GetDbContext();
var entries = GetEntriesInternal(uow, guildId);
@@ -76,10 +72,8 @@ public class ShopService : IShopService, INService
public async Task<bool> MoveEntryAsync(ulong guildId, int fromIndex, int toIndex)
{
if (fromIndex < 0)
throw new ArgumentOutOfRangeException(nameof(fromIndex));
if (toIndex < 0)
throw new ArgumentOutOfRangeException(nameof(toIndex));
ArgumentOutOfRangeException.ThrowIfNegative(fromIndex);
ArgumentOutOfRangeException.ThrowIfNegative(toIndex);
await using var uow = _db.GetDbContext();
var entries = GetEntriesInternal(uow, guildId);

View File

@@ -27,8 +27,8 @@ public static class WaifuExtensions
public static IEnumerable<WaifuLbResult> GetTop(this DbSet<WaifuInfo> waifus, int count, int skip = 0)
{
if (count < 0)
throw new ArgumentOutOfRangeException(nameof(count));
ArgumentOutOfRangeException.ThrowIfNegative(count);
if (count == 0)
return new List<WaifuLbResult>();

View File

@@ -14,5 +14,5 @@ public interface IGamblingService
Task<OneOf<SlotResult, GamblingError>> SlotAsync(ulong userId, long amount);
Task<FlipResult[]> FlipAsync(int count);
Task<OneOf<RpsResult, GamblingError>> RpsAsync(ulong userId, long amount, byte pick);
Task<OneOf<BetdrawResult, GamblingError>> BetDrawAsync(ulong userId, long amount, byte? guessValue, byte? guessColor);
Task<OneOf<BetdrawResult, GamblingError>> BetDrawAsync(ulong userId, long amount, byte? maybeGuessValue, byte? maybeGuessColor);
}

View File

@@ -20,9 +20,8 @@ public sealed class NewGamblingService : IGamblingService, INService
public async Task<OneOf<LuLaResult, GamblingError>> LulaAsync(ulong userId, long amount)
{
if (amount < 0)
throw new ArgumentOutOfRangeException(nameof(amount));
ArgumentOutOfRangeException.ThrowIfNegative(amount);
if (amount > 0)
{
var isTakeSuccess = await _cs.RemoveAsync(userId, amount, new("lula", "bet"));
@@ -47,8 +46,7 @@ public sealed class NewGamblingService : IGamblingService, INService
public async Task<OneOf<BetrollResult, GamblingError>> BetRollAsync(ulong userId, long amount)
{
if (amount < 0)
throw new ArgumentOutOfRangeException(nameof(amount));
ArgumentOutOfRangeException.ThrowIfNegative(amount);
if (amount > 0)
{
@@ -77,11 +75,9 @@ public sealed class NewGamblingService : IGamblingService, INService
public async Task<OneOf<BetflipResult, GamblingError>> BetFlipAsync(ulong userId, long amount, byte guess)
{
if (amount < 0)
throw new ArgumentOutOfRangeException(nameof(amount));
ArgumentOutOfRangeException.ThrowIfNegative(amount);
if (guess > 1)
throw new ArgumentOutOfRangeException(nameof(guess));
ArgumentOutOfRangeException.ThrowIfGreaterThan(guess, 1);
if (amount > 0)
{
@@ -105,19 +101,18 @@ public sealed class NewGamblingService : IGamblingService, INService
return result;
}
public async Task<OneOf<BetdrawResult, GamblingError>> BetDrawAsync(ulong userId, long amount, byte? guessValue, byte? guessColor)
public async Task<OneOf<BetdrawResult, GamblingError>> BetDrawAsync(ulong userId, long amount, byte? maybeGuessValue, byte? maybeGuessColor)
{
if (amount < 0)
throw new ArgumentOutOfRangeException(nameof(amount));
ArgumentOutOfRangeException.ThrowIfNegative(amount);
if (guessColor is null && guessValue is null)
if (maybeGuessColor is null && maybeGuessValue is null)
throw new ArgumentNullException();
if (guessColor > 1)
throw new ArgumentOutOfRangeException(nameof(guessColor));
if (maybeGuessColor > 1)
throw new ArgumentOutOfRangeException(nameof(maybeGuessColor));
if (guessValue > 1)
throw new ArgumentOutOfRangeException(nameof(guessValue));
if (maybeGuessValue > 1)
throw new ArgumentOutOfRangeException(nameof(maybeGuessValue));
if (amount > 0)
{
@@ -130,7 +125,7 @@ public sealed class NewGamblingService : IGamblingService, INService
}
var game = new BetdrawGame();
var result = game.Draw((BetdrawValueGuess?)guessValue, (BetdrawColorGuess?)guessColor, amount);
var result = game.Draw((BetdrawValueGuess?)maybeGuessValue, (BetdrawColorGuess?)maybeGuessColor, amount);
var won = (long)result.Won;
if (won > 0)
@@ -143,9 +138,8 @@ public sealed class NewGamblingService : IGamblingService, INService
public async Task<OneOf<SlotResult, GamblingError>> SlotAsync(ulong userId, long amount)
{
if (amount < 0)
throw new ArgumentOutOfRangeException(nameof(amount));
ArgumentOutOfRangeException.ThrowIfNegative(amount);
if (amount > 0)
{
var isTakeSuccess = await _cs.RemoveAsync(userId, amount, new("slot", "bet"));
@@ -170,9 +164,8 @@ public sealed class NewGamblingService : IGamblingService, INService
public Task<FlipResult[]> FlipAsync(int count)
{
if (count < 1)
throw new ArgumentOutOfRangeException(nameof(count));
ArgumentOutOfRangeException.ThrowIfLessThan(count, 1);
var game = new BetflipGame(0);
var results = new FlipResult[count];
@@ -242,11 +235,8 @@ public sealed class NewGamblingService : IGamblingService, INService
public async Task<OneOf<RpsResult, GamblingError>> RpsAsync(ulong userId, long amount, byte pick)
{
if (amount < 0)
throw new ArgumentOutOfRangeException(nameof(amount));
if (pick > 2)
throw new ArgumentOutOfRangeException(nameof(pick));
ArgumentOutOfRangeException.ThrowIfNegative(amount);
ArgumentOutOfRangeException.ThrowIfGreaterThan(pick, 2);
if (amount > 0)
{