Divi Games

Courses / Build a game

Testing and the browser

Tests

Tic-tac-toe's tests use MockCtx (crates/dg-games/src/mock.rs), a fake Ctx with balances, height and events, so a test is a few lines:

ctx.submit(&TicTacToe, ALICE, json!({"kind": "create", "stake": "100"}))?;

Write a test for every op and every failure path. Tic-tac-toe covers: a win takes the pot, a draw refunds both players, a free game moves no GOLD, only the player to move may move, bad or occupied cells, invited opponents, cancel (refund, only while open), timeout after 150 blocks, joining without enough GOLD, bad ops, and the views.

Rust is built on the build host, never on a laptop: scripts/remote-test.sh test -p dg-games.

The browser game

web/play/tictactoe is a plain page: no framework, no build step. It uses the SDK, web/sdk/divigames.js:

  1. Create or load a key, new Chain() for the node.
  2. signTx(key, {nonce, action: {type:"game", game:"tictactoe", op:{...}}}), then chain.submit(tx).
  3. Read the receipt, and redraw from tictactoe.* events arriving on /v1/ws.

Amounts are decimal strings of base units; use formatAmount(units, 2) to show GOLD.

Checklist for your own game