import { readFile } from "node:fs/promises"; import { createEconomancyBuyCommitment, createEconomancyInvestmentCommitment, type EconomancyBuyCommitment, type EconomancyBuyReveal, type EconomancyInvestmentCommitment, type EconomancyInvestmentReveal, } from "instlang"; import { createEconomancyMatch, type MatchCommandEnvelope, type MatchCommandResult, type PublicMatchView, } from "@instlang/economancy-host"; export interface ScriptedMatchInput { readonly matchId: string; readonly players: readonly string[]; readonly commands: readonly MatchCommandEnvelope[]; } export interface ScriptedMatchOutput { readonly results: readonly MatchCommandResult[]; readonly finalView: PublicMatchView; } export async function runScriptedMatch(input: ScriptedMatchInput): Promise { const created = createEconomancyMatch(input.matchId, input.players); if (created.kind !== "accepted") throw new Error(created.reason); const results: MatchCommandResult[] = []; for (const command of input.commands) { const result = await created.session.dispatch(command); results.push(result); if (result.kind !== "accepted") throw new Error(`${result.reason} (${command.commandId})`); } return Object.freeze({ results: Object.freeze(results), finalView: created.session.getPublicView() }); } export async function runDemoMatch(): Promise { const investmentId = "demo-investment-1"; const investmentReveals = [ { investmentId, player: "alice", coins: 0, nonce: "alice-demo-investment" }, { investmentId, player: "bob", coins: 0, nonce: "bob-demo-investment" }, ] as const; const investmentCommitments = await Promise.all(investmentReveals.map(createInvestmentCommitment)); const buyId = "demo-buy-1"; const buyReveals = [ { buyId, player: "alice", card: "board_of_monopoly", nonce: "alice-demo-buy-nonce" }, { buyId, player: "bob", card: "pass", nonce: "bob-demo-buy-nonce-1" }, ] as const; const buyCommitments = await Promise.all(buyReveals.map(createBuyCommitment)); return runScriptedMatch({ matchId: "demo-match", players: ["alice", "bob"], commands: [ { commandId: "demo-income", actor: "system", command: { kind: "advance_income" } }, ...investmentReveals.map((reveal, index): MatchCommandEnvelope => ({ commandId: `demo-investment-commit-${index}`, actor: reveal.player, command: { kind: "submit_investment_commitment", commitment: investmentCommitments[index]! } })), ...investmentReveals.map((reveal, index): MatchCommandEnvelope => ({ commandId: `demo-investment-reveal-${index}`, actor: reveal.player, command: { kind: "submit_investment_reveal", reveal } })), ...buyReveals.map((reveal, index): MatchCommandEnvelope => ({ commandId: `demo-buy-commit-${index}`, actor: reveal.player, command: { kind: "submit_buy_commitment", commitment: buyCommitments[index]! } })), ...buyReveals.map((reveal, index): MatchCommandEnvelope => ({ commandId: `demo-buy-reveal-${index}`, actor: reveal.player, command: { kind: "submit_buy_reveal", reveal } })), ], }); } async function createInvestmentCommitment(reveal: EconomancyInvestmentReveal): Promise { const created = await createEconomancyInvestmentCommitment(reveal); if (created.kind !== "accepted") throw new Error(created.reason); return created.commitment; } async function createBuyCommitment(reveal: EconomancyBuyReveal): Promise { const created = await createEconomancyBuyCommitment(reveal); if (created.kind !== "accepted") throw new Error(created.reason); return created.commitment; } async function main(): Promise { const args = process.argv.slice(2); if (args[0] === "demo") { const output = await runDemoMatch(); for (const result of output.results) console.log(JSON.stringify(result)); return; } if (args[0] === "--script" && args[1]) { const input = JSON.parse(await readFile(args[1], "utf8")) as ScriptedMatchInput; const output = await runScriptedMatch(input); for (const result of output.results) console.log(JSON.stringify(result)); console.log(JSON.stringify(output.finalView)); return; } throw new Error("usage: economancy demo | economancy --script path.json"); } if (import.meta.url === `file://${process.argv[1]}`) await main();