← Back to Projects
✦ Completed
Sep – Dec 2024

Network
Blackjack.

Full-stack multiplayer Blackjack — raw WebSocket server, real-time game state, betting economy, and a persistent SQLite leaderboard tied to cookie-based player identities.

Python WebSocket asyncio SQLite Multiplayer REST API Vanilla JS
5
Max Players
WS
Real-Time Protocol
SQL
Leaderboard DB
0
Ext. Dependencies

Architecture

How it works.

01 — WebSocket Server
RFC 6455 from Scratch
The server implements the full WebSocket handshake and frame parser in pure Python stdlib — no external packages. asyncio.start_server handles concurrent connections; _ws_decode_frames manually parses the length-prefixed binary frame format including masking, while _ws_encode handles server→client framing.
02 — Async Game Rooms
Concurrent State Machine
Each GameRoom instance manages a state machine across five phases: waiting → betting → playing → dealer_turn → finished. Turn sequencing uses await _advance_turn() with cooperative yielding — the dealer draw loop uses await asyncio.sleep() between cards to pace reveals without blocking other connections.
03 — SQLite Leaderboard
Persistent Player Profiles
Player identities are tied to a UUID generated in the browser and stored in a cookie, then sent with every create/join message. The server maps this UUID to a players table in leaderboard.db, tracking total_wins, total_losses, and best_balance using MAX(best_balance, ?) so high scores are preserved across sessions.
04 — Betting Economy
Per-Round Chip System
Each player starts with $1,000 in chips and sets a bet before cards are dealt. Payouts follow standard rules: win returns 1:1, blackjack pays 3:2 via math.floor(bet * 1.5), push returns the stake, bust/lose deducts the bet. For split hands, the bet is divided equally between both sub-hands. Balances persist to the DB at game end.
View Source on GitHub

All-Time Rankings

Leaderboard.

Players ranked by peak balance — the W/L record shown is from the session when that high score was first achieved. Your identity is tied to a cookie so your best score persists across sessions without an account.

#PlayerBest $WL
Loading leaderboard…
Page 1 of 1

Live Demo

Play the game.

Detecting mode…
Have a table code?
or browse tables
Searching for server…
Join Table
Enter your name to sit down.
Add your school or workplace so others know who they're playing against.
Waiting for players
Share this table code:
——
You are the host. Start whenever ready.
Place Your Bet
Min $50 · Round starts when all players bet
$1,000
Your Balance
💸 You went bankrupt! The house spotted you $100 to keep playing.
Waiting for players to bet…
Results in
Dealer
Balance $1,000
Bet $50
Your Hand
Waiting for game to start…
Round Over
PlayerTotalWLBalanceResult
Next Project
AI Playlist Generator →