Poker Game Implementation
The Poker game in MCG is implemented as a real-time multiplayer Texas Hold'em engine. It is split cleanly between the game rule logic in the mcg-poker crate, server orchestration and WebSocket broadcasting in native_mcg, and the interactive graphical client in frontend.
This page details the game architecture, state synchronization, and provides a step-by-step walkthrough on how to locally test the Poker implementation.
Architecture
The poker subsystem consists of three major layers:
mcg-pokercrate:- Game Flow & Betting (
game/): Manages stages (PreFlop,Flop,Turn,River,Showdown), blind posting (Small Blind / Big Blind), pot calculation, and side pots. - Hand Evaluator (
eval/): High-performance 7-card evaluator determining hand ranks. - Bot Logic (
bot/&driver/): Implements automated player agents with configurable decision delays (bot_delayin milliseconds).
- Game Flow & Betting (
native_mcgcontroller:- Maintains the authoritative game state.
- Validates incoming
Frontend2BackendMsg::Actionrequests against the currentto_actplayer. - Broadcasts the updated public game state (
Backend2FrontendMsg::UpdatePokerState) to all connected WebSocket clients.
frontendscreen (PokerOnlineScreen):- Renders player avatars, chips, active bets, community cards, and chip counts using
egui. - Offers controls to join as a specific player ("Play as"), rename players, toggle bots, and submit player actions.
- Renders player avatars, chips, active bets, community cards, and chip counts using
Testing Poker Locally with Two Instances
Testing poker locally with two concurrent instances allows you to verify real-time WebSocket state broadcasting, turn alternation, betting constraints, and UI updates between distinct player seats.
Step 1: Build the Frontend and Start the Backend
In the root repository directory, run:
just start devThis recipe:
- Compiles the frontend crate to WebAssembly.
- Starts the
native_mcgbackend server. - Binds the HTTP and WebSocket listeners to the first available local port (default:
http://127.0.0.1:3000).
Once the server outputs starting server, the application is ready. You'll see an address printed to STDOUT which you can either copy & paste into the browser or directly click to open it.
Step 2: Open Two Independent Browser Instances
Open two browser windows with your backend address and navigate to Poker Online
TIP
Opening Window 2 in a private/incognito window or a different browser profile prevents cached local state or identical browser sessions from interfering with client identity and makes visual side-by-side comparison easy.
Step 3: Connect Both Clients to the Backend
In both windows:
- Verify the Server address input displays your active backend port.
- Click the Connect button at the top of the screen.
- Both windows will establish a WebSocket connection and register for state updates.
Step 4: Configure Player Seats ("Play as")
The lobby table in the setup view shows the list of configured player seats:
+----+------------+-------+-------------------------+
| ID | Name | Bot | Actions |
+----+------------+-------+-------------------------+
| 0 | Alice | [ ] | ( ) Play as [Edit] |
| 1 | Bob | [ ] | ( ) Play as [Edit] |
+----+------------+-------+-------------------------+To configure two human players:
- Ensure neither player is marked as a bot:
- Uncheck the Bot checkbox for both Player 0 and Player 1.
- Assign Seat 1 in Window 1:
- In Window 1, click the "Play as" button next to Player 0.
- Assign Seat 2 in Window 2:
- In Window 2, click the "Play as" button next to Player 1.
Step 5: Start the New Game
- In Window 1, click the Start New Game button.
- The backend generates a fresh game:
- Evaluates player count and starting stacks (e.g., 1000 chips each).
- Posts small blind and big blind.
- Deals private hole cards to both players.
- Sets the initial stage to
PreFlop. - Sets
to_actto the first active player. - Pushed the current state to the second window.
- Both browser windows will immediately receive the new state broadcast and switch to the active poker table view.