Files
wikideck/scripts/sim/toy-engine.ts
T
claudeanderic 4f7a8a7c74 Night 1: Edit War card game, eom.dev accounts, peer-hosted modes, Android and iOS apps (#22)
Overnight build by 17 Claude agents (2026-11-16 22:45 → 01:30 EST), coordinated in [claude/wikideck milestone 63](https://gitea.eom.dev/claude/wikideck/milestone/63). **For Eric's review. Do not merge without it.** The full write-up is in `docs/night/REPORT.md`.

**What's in it** (38 reviewed PRs, squash-merged into `night/2026-11-16`):
- **Edit War:**
  - a 3-lane, 6-turn card game built on what WikiDeck cards actually hold
  - a pure, deterministic, off-chain rules engine (`shared/game/`, with the API in `docs/game/ENGINE_API.md`); rules are data and v1.1 is the default
  - a mobile-first Play UI with a lobby, deck builder, four loaner starter decks, a match against the AI, results and a tutorial
  - a landing page (fixes #3)
- **Design and balance:** `docs/game/DESIGN.md`, `CARD_DATA.md` (236 sampled articles) and `BALANCE.md` (bot simulations). Starter pairings went from 23–77% on v1 to roughly even on v1.1.
- **eom.dev accounts:** Dex OIDC with PKCE, plus off-chain wallet linking, where the client builds the message it signs (`server/account/`, `src/auth/`).
- **Peer-hosted game modes:** modes are JSON data, never code. Node API at `/api/game/*`, with host credit and leaderboards. Three example modes (`shared/game/modes.ts`, `server/game/`, `docs/game/HOSTING.md`).
- **Mobile:** Capacitor 7 Android app (a debug APK played a full match against the AI in the emulator), and an iOS project waiting on Xcode (`docs/night/ANDROID.md`, `docs/night/IOS.md`).
- **Testnet tooling:** `deploy/night/` (LAN-only, 3 nodes) and an optional `WIKIDECK_USER_AGENT` (the default is unchanged).

**Not touched:** consensus and protocol (`server/chain.ts`, `shared/protocol.ts`, `server/testnet/{consensus,ledger,…}.ts`, `native/`), the live deployment and mainnet. Matches run off-chain and never move or burn NFTs.

**Checks:** the final integration gate passed in `node:24-alpine`: `npm test` 241/242 with 0 fail (1 skipped), and `npm run build` passes. The repo needs Node 24, because Node 20 fails 12 tests falsely.

**Decisions for Eric** (see REPORT.md, Known issues and Next steps):
1. whether the live node should set `WIKIDECK_USER_AGENT`
2. the Xcode licence, which you should accept yourself
3. #49 in the fork, a bot change held after the freeze
4. on-chain host rewards (proposal only, in HOSTING.md)

🤖 Generated with [Claude Code](https://claude.com/claude-code)Reviewed-on: #22

Co-authored-by: Claude <claude@eom.dev>
2026-11-17 17:40:58 +00:00

117 lines
5.1 KiB
TypeScript

// Toy engine: a tiny lane-less creature game used to self-test the simulation harness
// before shared/game exists. Not the WikiDeck design; do not tune it.
import { mulberry32 } from '../../shared/bots/index.ts';
import type { DeckSpec, EngineAdapter, MatchOutcome, Seat } from '../../shared/bots/types.ts';
interface Card { id: string; cost: number; atk: number; hp: number; guard?: boolean; haste?: boolean }
interface Unit { card: Card; hp: number; ready: boolean }
interface Side { hp: number; deck: Card[]; hand: Card[]; board: Unit[]; mana: number; maxMana: number }
export interface ToyState { ruleset: string; turn: number; active: Seat; sides: [Side, Side]; over: boolean; winner: Seat | null; seed: number }
export type ToyAction = { kind: 'end' } | { kind: 'play'; hand: number } | { kind: 'attack'; unit: number; target: number /* -1 = face */ };
const POOL: Card[] = [];
for (let cost = 1; cost <= 6; cost++) {
POOL.push({ id: `c${cost}-beater`, cost, atk: cost + 1, hp: cost });
POOL.push({ id: `c${cost}-wall`, cost, atk: Math.max(1, cost - 1), hp: cost + 2, guard: true });
POOL.push({ id: `c${cost}-rush`, cost, atk: cost, hp: Math.max(1, cost - 1), haste: true });
}
const ARCHETYPES: Record<string, (c: Card) => number> = {
aggro: c => (c.cost <= 3 ? 3 : 0) + (c.haste ? 1 : 0),
control: c => (c.guard ? 2 : 0) + (c.cost >= 4 ? 2 : 0),
midrange: c => (c.cost >= 2 && c.cost <= 5 ? 2 : 0),
};
function buildDeck(arch: string): Card[] {
const w = ARCHETYPES[arch];
return POOL.filter(c => w(c) > 0).flatMap(c => Array(w(c)).fill(c)).slice(0, 20);
}
const clone = (s: ToyState): ToyState => ({
...s, sides: s.sides.map(p => ({ ...p, deck: [...p.deck], hand: [...p.hand], board: p.board.map(u => ({ ...u })) })) as [Side, Side],
});
function startTurn(s: ToyState): void {
const p = s.sides[s.active];
p.maxMana = Math.min(s.ruleset === 'toy-fast' ? 10 : 8, p.maxMana + 1);
p.mana = p.maxMana;
for (const u of p.board) u.ready = true;
const c = p.deck.shift();
if (c) p.hand.push(c); else p.hp -= 2; // fatigue
check(s);
}
function check(s: ToyState): void {
const [a, b] = s.sides;
if (a.hp <= 0 || b.hp <= 0) { s.over = true; s.winner = a.hp <= 0 && b.hp <= 0 ? null : a.hp <= 0 ? 1 : 0; }
}
export const toyEngine: EngineAdapter<ToyState, ToyAction> = {
name: 'toy',
rulesets: () => ['toy', 'toy-fast'],
decks: () => Object.keys(ARCHETYPES).map(a => ({ id: a, tags: [`arch:${a}`], payload: a })),
createMatch(ruleset, decks, seed) {
const rng = mulberry32(seed);
const side = (d: DeckSpec): Side => {
const deck = buildDeck(d.payload as string);
for (let i = deck.length - 1; i > 0; i--) { const j = Math.floor(rng() * (i + 1)); [deck[i], deck[j]] = [deck[j], deck[i]]; }
return { hp: 20, deck, hand: deck.splice(0, 3), board: [], mana: 0, maxMana: 0 };
};
const s: ToyState = { ruleset, turn: 1, active: (seed & 1) as Seat, sides: [side(decks[0]), side(decks[1])], over: false, winner: null, seed };
s.sides[1 - s.active].hand.push(s.sides[1 - s.active].deck.shift()!); // second player gets an extra card
startTurn(s);
return s;
},
toAct: s => s.active,
legalActions(s, seat) {
if (s.over || seat !== s.active) return [];
const me = s.sides[seat], them = s.sides[1 - seat];
const out: ToyAction[] = [{ kind: 'end' }];
me.hand.forEach((c, i) => { if (c.cost <= me.mana && me.board.length < 6) out.push({ kind: 'play', hand: i }); });
const guards = them.board.map((u, i) => (u.card.guard ? i : -2)).filter(i => i >= 0);
me.board.forEach((u, i) => {
if (!u.ready) return;
const targets = guards.length ? guards : [-1, ...them.board.map((_, j) => j)];
for (const t of targets) out.push({ kind: 'attack', unit: i, target: t });
});
return out;
},
applyAction(s0, a) {
const s = clone(s0);
const me = s.sides[s.active], them = s.sides[1 - s.active];
if (a.kind === 'end') {
s.active = (1 - s.active) as Seat;
s.turn++;
startTurn(s);
if (s.turn > 60) { s.over = true; s.winner = null; }
} else if (a.kind === 'play') {
const [c] = me.hand.splice(a.hand, 1);
me.mana -= c.cost;
me.board.push({ card: c, hp: c.hp, ready: !!c.haste });
} else {
const u = me.board[a.unit];
u.ready = false;
if (a.target < 0) them.hp -= u.card.atk;
else {
const v = them.board[a.target];
v.hp -= u.card.atk; u.hp -= v.card.atk;
them.board = them.board.filter(x => x.hp > 0);
me.board = me.board.filter(x => x.hp > 0);
}
check(s);
}
return s;
},
isOver: s => s.over,
result: (s): MatchOutcome => ({ winner: s.winner, reason: s.over ? (s.winner === null ? 'draw' : 'hp') : undefined }),
turn: s => s.turn,
evaluate(s, seat) {
const me = s.sides[seat], them = s.sides[1 - seat];
const board = (p: Side) => p.board.reduce((t, u) => t + u.card.atk + u.hp, 0);
const v = (me.hp - them.hp) / 20 + (board(me) - board(them)) / 20 + (me.hand.length - them.hand.length) / 20;
return Math.max(-1, Math.min(1, v));
},
cardsOf: (s, a) => (a.kind === 'play' ? [s.sides[s.active].hand[a.hand].id] : []),
actionKind: a => a.kind,
};