diff --git a/KfChatDotNetBot/Commands/Kasino/DiceCommand.cs b/KfChatDotNetBot/Commands/Kasino/DiceCommand.cs index 178b847..bba804b 100644 --- a/KfChatDotNetBot/Commands/Kasino/DiceCommand.cs +++ b/KfChatDotNetBot/Commands/Kasino/DiceCommand.cs @@ -50,19 +50,18 @@ public class DiceCommand : ICommand return; } var rolled = Money.GetRandomDouble(gambler); - decimal newBalance; var colors = await SettingsProvider.GetMultipleValuesAsync([ BuiltIn.Keys.KiwiFarmsGreenColor, BuiltIn.Keys.KiwiFarmsRedColor ]); // print dice game slider await botInstance.SendChatMessageAsync($"{ConstructDiceGameOutput(rolled)}",true, autoDeleteAfter: cleanupDelay); + decimal newBalance; if (rolled > 0.5 + _houseEdge) { // you win dice var effect = wager; - await Money.NewWagerAsync(gambler.Id, wager, effect, WagerGame.Dice, ct: ctx); - newBalance = gambler.Balance + effect; + newBalance = await Money.NewWagerAsync(gambler.Id, wager, effect, WagerGame.Dice, ct: ctx); await botInstance.SendChatMessageAsync( $"{user.FormatUsername()}, you rolled a {rolled * 100:N2} and [B][COLOR={colors[BuiltIn.Keys.KiwiFarmsGreenColor].Value}]WON![/COLOR][/B] " + $"You won {await effect.FormatKasinoCurrencyAsync()} and your balance is now {await newBalance.FormatKasinoCurrencyAsync()}", @@ -71,8 +70,7 @@ public class DiceCommand : ICommand else { // you lose dice - await Money.NewWagerAsync(gambler.Id, wager, -wager, WagerGame.Dice, ct: ctx); - newBalance = gambler.Balance - wager; + newBalance = await Money.NewWagerAsync(gambler.Id, wager, -wager, WagerGame.Dice, ct: ctx); await botInstance.SendChatMessageAsync( $"{user.FormatUsername()}, you rolled a {rolled * 100:N2} and [B][COLOR={colors[BuiltIn.Keys.KiwiFarmsRedColor].Value}]LOST![/COLOR][/B] " + $"Your balance is now {await newBalance.FormatKasinoCurrencyAsync()}", diff --git a/KfChatDotNetBot/Commands/Kasino/GuessWhatNumberCommand.cs b/KfChatDotNetBot/Commands/Kasino/GuessWhatNumberCommand.cs index 13a6e3d..9b52a4b 100644 --- a/KfChatDotNetBot/Commands/Kasino/GuessWhatNumberCommand.cs +++ b/KfChatDotNetBot/Commands/Kasino/GuessWhatNumberCommand.cs @@ -58,16 +58,14 @@ public class GuessWhatNumberCommand : ICommand if (guess == answer) { var effect = wager * 9; - await Money.NewWagerAsync(gambler.Id, wager, effect, WagerGame.GuessWhatNumber, ct: ctx); - newBalance = gambler.Balance + effect; + newBalance = await Money.NewWagerAsync(gambler.Id, wager, effect, WagerGame.GuessWhatNumber, ct: ctx); await botInstance.SendChatMessageAsync( $"{user.FormatUsername()}, [color={colors[BuiltIn.Keys.KiwiFarmsGreenColor].Value}]correct![/color] You won {await effect.FormatKasinoCurrencyAsync()} and your balance is now {await newBalance.FormatKasinoCurrencyAsync()}", true, autoDeleteAfter: cleanupDelay); return; } - await Money.NewWagerAsync(gambler.Id, wager, -wager, WagerGame.GuessWhatNumber, ct: ctx); - newBalance = gambler.Balance - wager; + newBalance = await Money.NewWagerAsync(gambler.Id, wager, -wager, WagerGame.GuessWhatNumber, ct: ctx); await botInstance.SendChatMessageAsync( $"{user.FormatUsername()}, [color={colors[BuiltIn.Keys.KiwiFarmsRedColor].Value}]wrong![/color] I was thinking of {answer}. Your balance is now {await newBalance.FormatKasinoCurrencyAsync()}", true, autoDeleteAfter: cleanupDelay); diff --git a/KfChatDotNetBot/Commands/Kasino/KenoCommand.cs b/KfChatDotNetBot/Commands/Kasino/KenoCommand.cs index 0547baa..2a79982 100644 --- a/KfChatDotNetBot/Commands/Kasino/KenoCommand.cs +++ b/KfChatDotNetBot/Commands/Kasino/KenoCommand.cs @@ -90,10 +90,10 @@ public class KenoCommand : ICommand await SettingsProvider.GetMultipleValuesAsync([ BuiltIn.Keys.KiwiFarmsGreenColor, BuiltIn.Keys.KiwiFarmsRedColor ]); - var newBalance = gambler.Balance - wager; + decimal newBalance; if (payoutMulti == 0) //you lose { - await Money.NewWagerAsync(gambler.Id, wager, -wager, WagerGame.Keno, ct: ctx); + newBalance = await Money.NewWagerAsync(gambler.Id, wager, -wager, WagerGame.Keno, ct: ctx); await botInstance.SendChatMessageAsync( $"{user.FormatUsername()}, you [color={colors[BuiltIn.Keys.KiwiFarmsRedColor].Value}]lost {await wager.FormatKasinoCurrencyAsync()}[/color]. Your balance is now: {await newBalance.FormatKasinoCurrencyAsync()}.", true, autoDeleteAfter: cleanupDelay); @@ -104,8 +104,7 @@ public class KenoCommand : ICommand //you win var win = wager * (decimal)payoutMulti; // Required to avoid compiler errors when trying to format it in the win message - newBalance = gambler.Balance + win; - await Money.NewWagerAsync(gambler.Id, wager, wager * (decimal)payoutMulti, WagerGame.Keno, ct: ctx); + newBalance = await Money.NewWagerAsync(gambler.Id, wager, wager * (decimal)payoutMulti, WagerGame.Keno, ct: ctx); await botInstance.SendChatMessageAsync( $"{user.FormatUsername()}, you [color={colors[BuiltIn.Keys.KiwiFarmsGreenColor].Value}]won {await win.FormatKasinoCurrencyAsync()} with a {payoutMulti}x multi![/color]. Your balance is now: {await newBalance.FormatKasinoCurrencyAsync()}.", true, autoDeleteAfter: cleanupDelay); diff --git a/KfChatDotNetBot/Commands/Kasino/LambchopCommand.cs b/KfChatDotNetBot/Commands/Kasino/LambchopCommand.cs index 3d19b69..bade8c2 100644 --- a/KfChatDotNetBot/Commands/Kasino/LambchopCommand.cs +++ b/KfChatDotNetBot/Commands/Kasino/LambchopCommand.cs @@ -247,15 +247,13 @@ public class LambchopCommand : ICommand { var multi = LambchopPayoutMultiplier(targetTile); var lambchopPayout = Math.Round(wager * multi - wager, 2); - await Money.NewWagerAsync(gambler.Id, wager, lambchopPayout, WagerGame.LambChop, ct: ctx); - newBalance = gambler.Balance + lambchopPayout; + newBalance = await Money.NewWagerAsync(gambler.Id, wager, lambchopPayout, WagerGame.LambChop, ct: ctx); lambchopResultMessage = $"{user.FormatUsername()}, you [B][COLOR={colors[BuiltIn.Keys.KiwiFarmsGreenColor].Value}]WON[/COLOR][/B]" + $" | Multi {multi} | Balance {await newBalance.FormatKasinoCurrencyAsync()}"; } else { - await Money.NewWagerAsync(gambler.Id, wager, -wager, WagerGame.LambChop, ct: ctx); - newBalance = gambler.Balance - wager; + newBalance = await Money.NewWagerAsync(gambler.Id, wager, -wager, WagerGame.LambChop, ct: ctx); lambchopResultMessage = $"{user.FormatUsername()}, you [B][COLOR={colors[BuiltIn.Keys.KiwiFarmsGreenColor].Value}]LOST[/COLOR][/B]" + $", better luck next time | Balance {await newBalance.FormatKasinoCurrencyAsync()}"; diff --git a/KfChatDotNetBot/Commands/Kasino/PlanesCommand.cs b/KfChatDotNetBot/Commands/Kasino/PlanesCommand.cs index 725490b..540d2ed 100644 --- a/KfChatDotNetBot/Commands/Kasino/PlanesCommand.cs +++ b/KfChatDotNetBot/Commands/Kasino/PlanesCommand.cs @@ -239,12 +239,11 @@ public class Planes : ICommand await SettingsProvider.GetMultipleValuesAsync([ BuiltIn.Keys.KiwiFarmsGreenColor, BuiltIn.Keys.KiwiFarmsRedColor ]); - var newBalance = gambler.Balance - wager; + decimal newBalance; if ((fullCounter - 3) % carrierCount == 0) //if you landed on the carrier { var win = plane.MultiTracker * wager; - newBalance = gambler.Balance + win; - await Money.NewWagerAsync(gambler.Id, wager, win, WagerGame.Planes, ct: ctx); + newBalance = await Money.NewWagerAsync(gambler.Id, wager, win, WagerGame.Planes, ct: ctx); planesDisplay = GetGameBoard(fullCounter, planesBoards, plane, carrierCount, noseUp); await botInstance.KfClient.EditMessageAsync(msgId.ChatMessageId!.Value, planesDisplay); await botInstance.SendChatMessageAsync( @@ -254,7 +253,7 @@ public class Planes : ICommand return; } plane.Crash(); - await Money.NewWagerAsync(gambler.Id, wager, -wager, WagerGame.Planes, ct: ctx); + newBalance = await Money.NewWagerAsync(gambler.Id, wager, -wager, WagerGame.Planes, ct: ctx); planesDisplay = GetGameBoard(fullCounter, planesBoards, plane, carrierCount, noseUp); await Task.Delay(TimeSpan.FromMilliseconds(frameLength), ctx); await botInstance.KfClient.EditMessageAsync(msgId.ChatMessageId!.Value, planesDisplay); diff --git a/KfChatDotNetBot/Services/Money.cs b/KfChatDotNetBot/Services/Money.cs index 5184d7f..a4cdf1b 100644 --- a/KfChatDotNetBot/Services/Money.cs +++ b/KfChatDotNetBot/Services/Money.cs @@ -282,7 +282,8 @@ public static class Money /// Optional comment to provide for the transaction /// If applicable, who sent the transaction (e.g. if a juicer) /// Cancellation token - public static async Task ModifyBalanceAsync(int gamblerId, decimal effect, + /// New balance after modification + public static async Task ModifyBalanceAsync(int gamblerId, decimal effect, TransactionSourceEventType eventSource, string? comment = null, int? fromId = null, CancellationToken ct = default) { @@ -308,6 +309,7 @@ public static class Money TimeUnixEpochSeconds = DateTimeOffset.UtcNow.ToUnixTimeSeconds() }, ct); await db.SaveChangesAsync(ct); + return gambler.Balance; } /// @@ -328,7 +330,8 @@ public static class Money /// Whether the game is 'complete'. Set to false for wagers with unknown outcomes. /// NOTE: wagerEffect will be ignored, instead value will be derived from the wagerAmount /// Cancellation token - public static async Task NewWagerAsync(int gamblerId, decimal wagerAmount, decimal wagerEffect, + /// Returns the gambler's balance + public static async Task NewWagerAsync(int gamblerId, decimal wagerAmount, decimal wagerEffect, WagerGame game, bool autoModifyBalance = true, dynamic? gameMeta = null, bool isComplete = true, CancellationToken ct = default) { @@ -378,8 +381,8 @@ public static class Money }, ct); await db.SaveChangesAsync(ct); _logger.Info($"Added wager row with ID {wager.Entity.Id}"); + if (!autoModifyBalance) return gambler.Balance; gambler.Balance += wagerEffect; - if (!autoModifyBalance) return; var txn = await db.Transactions.AddAsync(new TransactionDbModel { @@ -394,6 +397,7 @@ public static class Money }, ct); await db.SaveChangesAsync(ct); _logger.Info($"Added transaction with ID {txn.Entity.Id}"); + return gambler.Balance; } ///