Skip to content

About

Local-first Magic: The Gathering Commander board in React and TypeScript, with a Colyseus multiplayer room server

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

MTG Board

A local-first Magic: The Gathering Commander board built with React 19, TypeScript 6, Vite 8, and Zustand 5. Solo play remains local-first; multiplayer uses the included Colyseus room server.

Development

npm install
npm run dev

The app opens at the local Vite URL. It imports the bundled Blight Curse deck on a fresh install when Scryfall is reachable.

Multiplayer

Run the room server in a second terminal:

npm --prefix server install
npm run server:build
npm run server:start

The client defaults to ws://localhost:2567. Set VITE_COLYSEUS_URL in a local .env file for a build-time default, or enter a hostname/IP/URL in the multiplayer screen for a runtime server. The server address is included in room invite links. See docs/API.md for the room lifecycle, state visibility, actions, and events.

SQLite backup restore

For local testing, restore a SQLite backup from a magnet link or .torrent file with the guarded CLI workflow:

npm run db:restore -- \
  --source 'magnet:?xt=urn:btih:YOUR_INFO_HASH' \
  --target "$PWD/.local/mtg.sqlite" \
  --yes

It downloads into a temporary directory, validates the incoming database with PRAGMA integrity_check, and keeps a timestamped safety copy before replacing an existing target. Use --dry-run to validate without changing the target, or pass a local .sqlite file as --source to test the restore path without a torrent. See docs/SQLITE-RESTORE.md for prerequisites and multi-file torrent usage.

Production deployment

  • PORT — server port (default 2567)
  • CORS_ORIGIN — comma-separated allowed browser origins (default * for local play). Set it to your app origin before public deployment.
  • Rate limiting — the server rejects >30 connection attempts per IP per minute (anti code-guessing). Add reverse-proxy rate limits on room create/join for a very public deployment.
  • TLS — run the WebSocket behind a reverse proxy (nginx/caddy) terminating TLS; the app is ws:// internally.
  • Reconnect is protected by a server-issued per-player auth token; room codes are 5 letters (~11.9M combos).

Checks

npm run build             # tsc -b && vite build
npm run lint              # oxlint
npm test                  # vitest — client, protocol, and script tests (rules, SBA, combat, triggers, undo, AI turns, deck import/persistence, SQLite restore, per-deck smoke)
npm run test:server       # vitest — room server tests (room lifecycle, private zones, combat, turn cleanup); needs `npm --prefix server install` first
npm run check             # all of the above in one go
npm audit --omit=dev

The six SQLite-restore tests shell out to the sqlite3 CLI and are skipped (not failed) when it is not installed. GitHub Actions (.github/workflows/ci.yml) runs typecheck, lint, both test suites, and the production build on every push and pull request.

Features

  • Board zones: Battlefield (sectioned by card type — Creatures, Artifacts, Enchantments, etc.), Command, Hand, Library, Graveyard, Exile — with card movement between zones
  • Tap/untap: Double-click, right-click menu, or inline ↻ badge; all battlefield cards untap on turn advance
  • Counters: +1/+1 and −1/−1 via right-click context menu
  • Hand management: Private by default, hold ⌥ Alt to enlarge, double-tap Alt to lock expanded, ▲ toggle button
  • Mana pool: Manual mana add/spend, multi-color land tapping with potential mana tracking, usage log
  • Life tracker: ± buttons with color-coded danger thresholds (≤5 yellow, ≤0 red)
  • Turn management: Advance turn (untap + draw), discard-to-7 prompt, second-land override, conditional enters-tapped resolution
  • Dice & coin: d4–d100 roller, coin flip, roll history
  • Camera widget: Draggable, resizable, croppable video preview — persisted position/size in localStorage
  • Broadcast window: OBS-ready public board popup (hand omitted, 16:9)
  • Multiplayer rooms: Colyseus-backed 1v1, 2v2, and four-player free-for-all rooms with server-authoritative turns and private hands
  • Multiplayer combat: Server-authoritative creature-only declarations with per-defender blocker gates, damage cleanup at turn transition, SBA deaths, and keywords (flying/reach blocking, first strike/double strike order, trample overflow, lifelink, deathtouch, indestructible, vigilance); cast from the command zone with commander tax; hand-size discard and deck-out loss; rematch button on game over
  • Commander damage: 21 points of commander damage (trample counted) loses the game; tracked per commander in the card inspector
  • Turn timers: Per-player accumulated turn time in solo and multiplayer; timers freeze when the game ends
  • Custom backgrounds: Upload, select, delete — stored as data URLs in localStorage
  • Card rule database: Per-card enters-tapped override with search/filter
  • Local card catalog: Checked-in card metadata for special lands, images, Oracle snippets, and gameplay features; Scryfall refreshes generic card data
  • Persistent state: All game state and decks saved to localStorage, cross-tab sync
  • 16 complete prebuilt Commander decks: Official 100-card lists (Commander Masters, Tales of Middle-earth, MH3, Bloomburrow, Duskmourn, Fallout, and more) sourced from src/data/decklists/*.json, shared with the trigger-engine rules feed
  • Deck builder: Scryfall search, add/remove cards, commander selection, save/delete/rename imported decks
  • AI opponent: Plays a real deck (any prebuilt or imported) with automated turns — casts spells, resolves its ETB triggers, plays lands, attacks (free swings, avoids bad trades), blocks, uses its commander, tracks commander tax, discards to 7, and loses on deck-out
  • Combat keywords: Flying (blocking validity), trample (damage overflow), lifelink (healing), deathtouch (lethal mark), indestructible (survives SBA), haste (attacks the turn it enters), reach (blocks fliers), vigilance, first strike & double strike (two damage steps with SBA between), ward (pay-or-counter prompt), hexproof-aware targeting
  • Auras & pumps: Auras attach on entry, pump only the enchanted creature, and die with it; "until end of turn" pumps show a green badge; static anthems apply in combat/SBA/AI
  • Modal spells & kicker: Parser marks optional modes; Skip button on the stack for unplayed modes
  • State-based actions: Lethal damage destroys creatures, 0-loyalty planeswalkers die, counters and damage are cleaned up at the start of each turn
  • Card image caching: Scryfall-resolved images are downloaded once into an IndexedDB cache and served locally on every subsequent load; 143 pre-bundled card images ship in public/cards/
  • Game over flow: Life reaching zero ends the game with a winner banner and replay option

Deck import

Supports:

  • Prebuilt decks from the bundled library
  • Moxfield / Archidekt URLs — fetches raw deck list
  • Plain text with quantities (4 Lightning Bolt, 4x Sol Ring)
  • Commander: and Sideboard: sections (sideboard cards ignored)
  • Bracket [quantity] notation and decoration headers

Solo card state and camera streams stay in the browser. Multiplayer sends the minimal card metadata needed to render a room board; hand and library contents are sent only to their owner. Scryfall is contacted directly by the browser for card imports and images.

SpellTable workflow

SpellTable expects a video input device. Click Open MTG Stream; the app opens a fixed-size public window titled MTG Stream — Public Board with the hand omitted. Add that window to OBS Studio as a Window Capture source, start OBS Virtual Camera, and select OBS Virtual Camera as SpellTable's video input. OBS is only acting as the camera bridge; it no longer needs to crop the board.

The browser button controls the public-output window and shows when it is active; it does not create a system webcam by itself.

Known Issues

  • Backgrounds stored as data URLs in localStorage (~5MB limit)
  • Room codes (5 letters) are shareable invite codes; reconnect is protected by a server-issued auth token and the server limits connection attempts per IP, but a public deployment should still add reverse-proxy rate limits around room creation/join
  • Full priority system — partial: players get response windows when the AI casts spells (play an instant or pass); formal pass-priority rounds in every phase, response prompts for AI attacks, and multiplayer priority sync are not yet modeled.

About

Local-first Magic: The Gathering Commander board in React and TypeScript, with a Colyseus multiplayer room server

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages