Skip to content

Conversation

Copilot
Copy link

@Copilot Copilot AI commented Jul 27, 2025

This PR addresses critical issues in the casino betting system where players could exploit the delayed token deduction mechanism.

Issues Fixed

1. Multiple Active Games Exploit

Previously, a player with 1000 tokens could join multiple games simultaneously and bet their full balance in each game:

Player has 1000 tokens
- Join Game A, bet 1000 tokens ✅ (allowed)
- Join Game B, bet 1000 tokens ✅ (allowed - only checked individual game)
- If both games are lost, player should lose 2000 tokens but only has 1000

2. In-Game Bet Increases Without Validation

Actions like Blackjack's DoubleDown could increase bets beyond available balance:

Player has 1000 tokens, already committed 800 to other games
- Join Blackjack game, bet 300 tokens (total committed: 1100)  
- During game, DoubleDown to 600 tokens ✅ (allowed - no validation)
- Total commitment now 1300 tokens with only 1000 available

Solution

Multi-Game Bet Validation

  • Added GetCommittedTokens() method to calculate tokens locked in other active games
  • Modified SetBet() to validate against available tokens: user.Tokens - committedTokens
  • Only counts games in NotStarted and InProgress states (finished games don't lock tokens)

In-Game Bet Increase Validation

  • Added ValidateBetIncrease() method for validating bet increases during gameplay
  • Integrated validation into the action workflow to check DoubleDown attempts
  • Prevents bet doubling when insufficient tokens are available

Enhanced User Experience

  • Updated "All In" functionality to only bet uncommitted tokens
  • Added descriptive error messages showing available vs committed token breakdown
  • Maintains existing gameplay flow with no breaking changes

Example After Fix

Player has 1000 tokens
- Join Game A, bet 600 tokens ✅
- Try to join Game B, bet 500 tokens ❌ 
  Error: "Available: 400 (You have 600 tokens committed to other active games)"
- Join Game B, bet 300 tokens ✅ (total: 900)
- In Blackjack, try DoubleDown (300→600) ❌
  Error: "Cannot double down: insufficient tokens. Available: 100, Required: 300"

The implementation follows the suggested approach of having GameService check active games and account for committed tokens when validating new bets.

Fixes #347.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

…t increases

Co-authored-by: Pierre-Demessence <1756398+Pierre-Demessence@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] Rework betting system Fix betting system to prevent overlapping bets and validate in-game bet increases Jul 27, 2025
@Copilot Copilot AI requested a review from Pierre-Demessence July 27, 2025 15:22
Copilot finished work on behalf of Pierre-Demessence July 27, 2025 15:22
@Pierre-Demessence
Copy link
Member

Ok this is not bad @copilot but I don't like this part:
// Check for actions that require bet validation
if (action.ToString() == "DoubleDown" && gameSession.GameName == "Blackjack")

This is not clean at all.
Imo it would be better to modify Game.cs with a new method to modify the Bet of a user, and this method does "magic" that allows verification.
That way, directly in the game actions of each Implementation (like in DoubleDown() in Blackjack.cs), we can just do TryIncreaseBet(player, amount) for example (and it throws an exception if it fails?). It's much cleaner.
But constraint: Game.cs is totally abstract from the concept of "Discord" and "GameSessions" so it shouldn't have access to the other games at all.
So think about a solution and implement that.

Co-authored-by: Pierre-Demessence <1756398+Pierre-Demessence@users.noreply.github.com>
Copy link
Author

Copilot AI commented Jul 27, 2025

Ok this is not bad @copilot but I don't like this part:
// Check for actions that require bet validation
if (action.ToString() == "DoubleDown" && gameSession.GameName == "Blackjack")
...

I've refactored the bet validation system as requested. The changes include:

New Architecture:

  • Added IBetValidator interface to Game.cs for clean abstraction
  • Added TryIncreaseBet(player, amount) method to ACasinoGame base class
  • Modified Blackjack's DoubleDown() to use await TryIncreaseBet(player, player.Bet)
  • Injected validation logic via GameServiceBetValidator in GameService

Key Improvements:

  • Removed hardcoded action checks like action.ToString() == "DoubleDown"
  • Game.cs remains abstract from Discord/GameSession concepts
  • Validation happens directly in game actions as you suggested
  • Clean error handling with descriptive messages

Example Usage:

// In Blackjack.cs DoubleDown method:
await TryIncreaseBetAsync(player, player.Bet); // Validates & doubles bet

The bet validator is dependency-injected when creating game sessions, keeping the architecture clean while enabling proper validation across all active games.

Commit: ab69b51

Copilot finished work on behalf of Pierre-Demessence July 27, 2025 16:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rework betting system
2 participants