diff --git a/shared/duel/story/log.ts b/shared/duel/story/log.ts index 45d5c52..07e65f8 100644 --- a/shared/duel/story/log.ts +++ b/shared/duel/story/log.ts @@ -47,11 +47,12 @@ export class Narrator { player(seat: Seat): string { return this.record.players[seat].name; } - /** Assigns a reference number without mentioning the card (e.g. for the References list). */ + /** Assigns a reference number without mentioning the card (e.g. for the References list). Tokens get none (0). */ cite(id: string): number { let n = this.refByCard.get(id); if (n === undefined) { const c = this.card(id); + if (c.token || c.revisionId === undefined) return 0; n = this.references.length + 1; this.refByCard.set(id, n); this.references.push({ @@ -66,6 +67,7 @@ export class Narrator { /** A card's name for a sentence: italic, with its description on first mention. */ mention(id: string): string { const c = this.card(id); + if (c.token || c.revisionId === undefined) return c.title; const first = !this.refByCard.has(id); const n = this.cite(id); if (!first) return `*${c.title}*`; @@ -157,6 +159,7 @@ export function renderEvent(nar: Narrator, ev: StoryEvent, index: number, viewer case 'rivalry': return nar.sentence('rivalry', index, { a: M(ev.a), b: M(ev.b), reason: () => ev.reason }); case 'gameOver': { + if (ev.winner === null) return nar.sentence('gameOverDraw', index, { turn: N(ev.turn) }); const key = ({ life: 'gameOverLife', library: 'gameOverLibrary', concede: 'gameOverConcede', timeout: 'gameOverTimeout', turnCap: 'gameOverTurnCap' } as const)[ev.reason]; return nar.sentence(key, index, { winner: P(ev.winner), loser: P(other(ev.winner)), turn: N(ev.turn) }); } diff --git a/shared/duel/story/recap.ts b/shared/duel/story/recap.ts index 2a0b6f0..21ec228 100644 --- a/shared/duel/story/recap.ts +++ b/shared/duel/story/recap.ts @@ -50,7 +50,9 @@ export function longDate(iso: string): string { interface Played { id: string; seat: Seat; order: number } interface Facts { + /** For a draw: seat 0, with `draw` set. */ winner: Seat; + draw: boolean; loser: Seat; reason: GameOverReason; turns: number; @@ -72,7 +74,8 @@ function facts(record: MatchRecord): Facts { if (e.kind === 'adopt') played.push({ id: e.doctrine, seat: e.seat, order: i }); if (e.kind === 'destroyed') destroyed.push(e.card); }); - return { winner: over.winner, loser: over.winner === 0 ? 1 : 0, reason: over.reason, turns: over.turn, life, played, destroyed }; + const winner: Seat = over.winner ?? 0; + return { winner, draw: over.winner === null, loser: winner === 0 ? 1 : 0, reason: over.reason, turns: over.turn, life, played, destroyed }; } /** Highest cost wins; ties go to the card played first. */ @@ -104,6 +107,7 @@ export function battleTitle(record: MatchRecord, f: Facts = facts(record)): stri function resultText(record: MatchRecord, f: Facts): string { const w = record.players[f.winner].name; + if (f.draw) return 'Inconclusive'; switch (f.reason) { case 'concede': case 'timeout': return `${w} victory by withdrawal`; case 'library': return `${w} victory by attrition`; @@ -138,6 +142,7 @@ function sub(t: string, v: Record): string { /** The result as a phrase for the lead: "a Pyrrhic victory for bob". */ function resultPhrase(record: MatchRecord, f: Facts): string { const w = record.players[f.winner].name; + if (f.draw) return 'a draw'; switch (f.reason) { case 'concede': case 'timeout': return `a victory for ${w} by withdrawal`; case 'library': return `a victory for ${w} by attrition`; @@ -216,8 +221,9 @@ export function recap(record: MatchRecord): RecapDoc { if (end) course.push(end.text); // Aftermath. - const aftermath = [ - `${record.players[f.winner].name} ended the engagement with ${f.life[f.winner]} Notability; ${record.players[f.loser].name} ended with ${f.life[f.loser]}.`, + const aftermath = [f.draw + ? `Both ${a} and ${b} ended the engagement with ${f.life[0]} Notability.` + : `${record.players[f.winner].name} ended the engagement with ${f.life[f.winner]} Notability; ${record.players[f.loser].name} ended with ${f.life[f.loser]}.`, ]; if (record.ratingChange) { const fmt = (n: number) => (n >= 0 ? `+${n}` : `${n}`); diff --git a/shared/duel/story/templates.ts b/shared/duel/story/templates.ts index 312ecca..657d29a 100644 --- a/shared/duel/story/templates.ts +++ b/shared/duel/story/templates.ts @@ -91,6 +91,11 @@ export const LOG_TEMPLATES = { '{loser} was reduced to 0 Notability. {winner} won.', '{winner} won on turn {turn}.', ], + gameOverDraw: [ + 'Both sides were reduced to 0 Notability. The engagement ended in a draw.', + 'The engagement ended in a draw on turn {turn}.', + 'Neither side retained any Notability. No winner was recorded.', + ], gameOverLibrary: [ '{loser} could not draw a card and lost.', '{loser} ran out of cards. {winner} won.', diff --git a/shared/duel/story/types.ts b/shared/duel/story/types.ts index 7e5625c..df8bab4 100644 --- a/shared/duel/story/types.ts +++ b/shared/duel/story/types.ts @@ -19,8 +19,10 @@ export interface StoryCard { cost: number; /** Portal (faction): Arts, Earth, History, Science, Society or Neutral. */ portal?: string; - /** The article revision the card was minted from (on-chain). */ - revisionId: number; + /** The article revision the card was minted from (on-chain). Absent for tokens: they are never cited. */ + revisionId?: number; + /** Tokens (Elected Member, Staff Member): generic, not articles, never cited or glossed. */ + token?: boolean; /** On-chain token id of the mint, if known. */ tokenId?: string; /** Seat that owns the card in this match. */ @@ -47,7 +49,8 @@ export type StoryEvent = | { kind: 'discard'; seat: Seat; card: string } | { kind: 'connected'; seat: Seat; a: string; b: string; reason: string } | { kind: 'rivalry'; a: string; b: string; reason: string } - | { kind: 'gameOver'; winner: Seat; reason: GameOverReason; turn: number }; + /** winner null = draw (both players reached 0 at once). */ + | { kind: 'gameOver'; winner: Seat | null; reason: GameOverReason; turn: number }; export interface StoryPlayer { /** Display name of the seat (account display name or "Bot (Normal)"). */ diff --git a/tests/duel-design-story.test.ts b/tests/duel-design-story.test.ts index 21dea81..779683a 100644 --- a/tests/duel-design-story.test.ts +++ b/tests/duel-design-story.test.ts @@ -216,3 +216,26 @@ test('golden: full log and recap prose match tests/duel-design-story.golden.json const r = recap(record); assert.deepEqual({ log: logParagraphs(renderLog(record).lines), lead: r.lead, sections: r.sections }, golden); }); + +test('tokens are named plainly and never cited; draws are recorded as inconclusive', () => { + const withToken: MatchRecord = { + ...record, + cards: { ...cards, 't:1': { id: 't:1', title: 'Elected Member', type: 'Figure', cost: 0, portal: 'Society', owner: 1, token: true } }, + events: [ + T(1), { kind: 'cast', seat: 1, card: 'b1' }, + { kind: 'tokenCreated', seat: 1, source: 'b1', token: 'Elected Member', count: 1 }, + T(2), { kind: 'attack', seat: 1, card: 't:1', power: 1, toughness: 1 }, + { kind: 'playerDamage', seat: 0, source: 't:1', amount: 20, life: 0 }, + { kind: 'playerDamage', seat: 1, amount: 20, life: 0 }, + { kind: 'gameOver', winner: null, reason: 'life', turn: 2 }, + ], + }; + const { lines, references } = renderLog(withToken); + assert.ok(lines.some(l => /Elected Member \(1\/1\)/.test(l.text) && !/\*Elected Member\*/.test(l.text))); + assert.deepEqual(references.map(r => r.title), ['1954 Gold Coast general election']); + assert.match(lines[lines.length - 1].text, /draw|No winner/); + const r = recap(withToken); + assert.equal(r.infobox.result, 'Inconclusive'); + assert.match(r.lead, /ended in a draw\./); + assert.ok(r.references.every(x => x.revisionId !== undefined)); +});