The Casino module is a comprehensive gaming system for the Discord bot that enables users to play various casino-style games (Blackjack, Poker, Rock Paper Scissors) within Discord channels. The architecture follows a layered approach with clear separation of concerns between user interface, business logic, game logic, and data persistence.
-
Primary Interface: Handles all Discord slash commands and component interactions
-
Key Features:
- Token management commands (
/casino tokens balance,/casino tokens gift,/casino tokens daily) - Token leaderboards (
/casino tokens leaderboard)
- Token management commands (
-
Responsibilities:
- Input validation and permission checking
- Discord interaction handling (slash commands, buttons, modals)
- Response formatting and error handling
- Delegating business logic to services
-
Game Interface: Handles game-specific interactions and component callbacks
-
Key Features:
- Game creation and management (
/casino game,/casino rules) - Game Statistics and leaderboards (
/casino statistics,/casino leaderboard) - Game session management (join, leave, ready, play again)
- Player actions (hit, stand, bet, etc.)
- AI player management
- Real-time game state updates
- Game creation and management (
-
User Management: Core service for token and user data management
-
Key Features:
- User creation and token balance management
- Transaction recording and audit trails
- Daily reward system
- Game statistics and leaderboard generation
- Admin token management
-
Responsibilities:
- Database interactions for user data
- Token transfer and validation
- Statistical data aggregation
-
Session Management: Manages active game sessions and player interactions
-
Key Features:
- Game session lifecycle (create, join, end, remove)
- Game type factory pattern for different games
- Player action validation and processing
- Payout calculation and distribution
-
Responsibilities:
- Creating game instances and Discord wrappers
- Managing active session collection
- Coordinating between game logic and user service
-
Contract: Defines the interface all casino games must implement
-
Key Methods:
StartGame(),EndGame(),DoPlayerAction()HasNextDealerAction(),DoNextDealerAction()HasNextAIAction(),DoNextAIAction()GetPlayerGameResult(),CalculatePayout()
-
Base Class: Abstract implementation providing common game functionality
-
Generic Parameters:
TPlayerData: Game-specific player state (cards, actions, etc.)TPlayerAction: Enum defining possible player actions
-
Features:
- State management (NotStarted, InProgress, Finished, Abandoned)
- Player data management with strongly-typed game data
- Template method pattern for game flow
-
Session Contract: Defines game session management interface
-
Key Methods:
- Player management (add, remove, ready state)
- Action processing and state queries
- AI and dealer action handling
-
Session Implementation: Generic game session with strongly-typed game reference
-
Features:
- Player collection management
- Game state validation and auto-start logic
- Maximum seats and player count enforcement
-
Discord Integration: Extends game sessions with Discord-specific functionality
-
Key Methods:
GenerateEmbedAndButtons(): Creates Discord UI componentsGenerateRules(): Provides game rule explanationsShowHand(): Returns private hand information
-
Discord Wrapper: Abstract base for Discord-integrated game sessions
-
Features:
- Discord context management (guild, client, user)
- Embed and component generation
- Player name resolution and formatting
- Results display and payout information
- BlackjackPlayerAction: Hit, Stand, DoubleDown actions
- BlackjackPlayerData: Player cards and action history
- Blackjack: Core game logic with dealer AI and payout calculation
- BlackjackDiscordGameSession: Discord UI for blackjack games
- PokerPlayerAction: Call, Raise, Fold, Check actions
- PokerPlayerData: Hole cards, betting state
- Poker: Texas Hold'em implementation
- PokerDiscordGameSession: Discord UI with private hand support
- RPSPlayerAction: Rock, Paper, Scissors choices
- RockPaperScissors: Simple simultaneous choice game
- RockPaperScissorsDiscordGameSession: Discord UI for RPS
- CasinoUser: User profile with token balance and statistics
- GamePlayer: Base player representation
- DiscordGamePlayer: Discord-specific player with user ID and AI support
- Card, Deck: Card game utilities
- DatabaseService: Handles all database operations
- Transaction logging: Audit trail for all token movements
- Statistics tracking: Game results and player performance data
User Command → CasinoSlashModule → GameService → Game Factory → GameSession Creation → Discord Response
Discord Interaction → CasinoSlashModule → GameService → GameSession → Game Logic → State Update → Discord Update
Game End Condition → GameService.EndGame() → Payout Calculation → CasinoService.UpdateUserTokens() → Statistics Update
GameService.GetGameInstance()creates appropriate game typesGameService.CreateDiscordGameSession()creates Discord wrappers
ACasinoGame<T,U>provides common game structure- Specific games override abstract methods for custom logic
- Different games implement
ICasinoGameinterface - Allows runtime game type selection
- Game state changes trigger Discord UI updates
- Automatic AI and dealer action processing
-
Create Game Logic:
(Example:
Domain/Casino/Games/Blackjack.cs)- Define player action enum with
ButtonMetadataattributes - Create player data class implementing
ICasinoGamePlayerData - Implement game class extending
ACasinoGame<TPlayerData, TPlayerAction>
- Define player action enum with
-
Create Discord Integration:
(Example:
Domain/Casino/Discord/BlackjackDiscordGameSession.cs)- Implement Discord session class extending
DiscordGameSession<TGame> - Override embed and component generation methods
- Implement Discord session class extending
-
Register Game:
- Add to
CasinoGameenum inCasinoSlashModule.cs - Update factory methods in
GameService
- Add to
- Channel restrictions via
CasinoService.IsChannelAllowed() - Starting token amounts in
BotSettings.CasinoStartingTokens - Daily reward amounts and cooldowns
- Game-specific parameters (max players, betting limits)
- User Errors: Graceful handling with ephemeral Discord responses
- System Errors: Comprehensive logging with stack traces
- Transaction Errors: Rollback mechanisms and audit trails
- All significant actions logged via
ILoggingService - Separate logging for user actions vs system errors
- Transaction audit trail for compliance
- Server-side validation of all token operations
- Audit trail for all transactions
- Prevention of negative balances and invalid transfers
- Server-side game state management
- Action validation against current game state
- Prevention of duplicate actions and invalid moves