From 2f57c74d564754c1dc6a4e15a2d21a482a474a78 Mon Sep 17 00:00:00 2001 From: coyotte508 Date: Sun, 4 Oct 2026 14:04:05 +0200 Subject: [PATCH] Reveal all completed sealed bids in auction history --- packages/engine/index.ts | 1 + packages/engine/package.json | 2 +- packages/engine/src/auction-history.spec.ts | 120 ++++++++++++++++++++ packages/engine/src/auction-history.ts | 54 +++++++++ packages/engine/src/describe.ts | 20 ++-- packages/engine/wrapper.ts | 45 +------- packages/viewer/package.json | 2 +- packages/viewer/src/lib/LogFeed.svelte | 55 +++++++++ packages/viewer/src/lib/store.svelte.ts | 5 + packages/viewer/src/localization/da.json | 3 +- packages/viewer/src/localization/de.json | 3 +- packages/viewer/src/localization/el.json | 3 +- packages/viewer/src/localization/en.json | 3 +- packages/viewer/src/localization/fa.json | 3 +- packages/viewer/src/localization/fr.json | 3 +- packages/viewer/src/localization/hi.json | 3 +- packages/viewer/src/localization/it.json | 3 +- packages/viewer/src/localization/ko.json | 3 +- packages/viewer/src/localization/nl.json | 3 +- packages/viewer/src/localization/pl.json | 3 +- packages/viewer/src/localization/pt-BR.json | 3 +- packages/viewer/src/localization/ro.json | 3 +- packages/viewer/src/localization/ru.json | 3 +- packages/viewer/src/localization/vi.json | 3 +- packages/viewer/src/localization/zh-TW.json | 3 +- 25 files changed, 287 insertions(+), 65 deletions(-) create mode 100644 packages/engine/src/auction-history.spec.ts create mode 100644 packages/engine/src/auction-history.ts diff --git a/packages/engine/index.ts b/packages/engine/index.ts index 2ebad08..063761d 100644 --- a/packages/engine/index.ts +++ b/packages/engine/index.ts @@ -8,5 +8,6 @@ export * from "./src/production.js"; export * from "./src/moves.js"; export * from "./src/rankings.js"; export * from "./src/describe.js"; +export * from "./src/auction-history.js"; export * from "./src/replay.js"; export * from "./src/ai.js"; diff --git a/packages/engine/package.json b/packages/engine/package.json index 262273b..9f5c234 100644 --- a/packages/engine/package.json +++ b/packages/engine/package.json @@ -1,6 +1,6 @@ { "name": "outpost-engine", - "version": "0.10.20", + "version": "0.10.21", "description": "Unofficial Outpost rules engine for boardgamers.space", "license": "AGPL-3.0", "files": [ diff --git a/packages/engine/src/auction-history.spec.ts b/packages/engine/src/auction-history.spec.ts new file mode 100644 index 0000000..f779f56 --- /dev/null +++ b/packages/engine/src/auction-history.spec.ts @@ -0,0 +1,120 @@ +import assert from "node:assert/strict"; +import { test } from "node:test"; +import { logSlice, stripSecret } from "../wrapper.js"; +import { sealedAuctionHistory } from "./auction-history.js"; +import { describeLog, describeLogEntry } from "./describe.js"; +import { applyMove, initGame } from "./moves.js"; +import { replay } from "./replay.js"; +import type { GameState, ProductionCard } from "./types.js"; + +function game() { + const state = initGame(4, { fastBid: true }, "auction-history"); + for (const player of state.players) { + player.hand = Array.from({ length: 6 }, (): ProductionCard => ({ t: "research", v: 10 })); + } + const round = state.log.find((entry) => entry.type === "round"); + assert.ok(round?.type === "round"); + for (const produced of round.produced) { + produced.cards = structuredClone(state.players[produced.player]!.hand); + } + return state; +} + +function open(state: GameState) { + const seats = [state.activeSeat, ...state.purchaseOrder.filter((seat) => seat !== state.activeSeat)]; + applyMove(state, { action: "auction", marketIndex: 0, bid: 25 }, seats[0]!); + return seats as [number, number, number, number]; +} + +test("sealed history stays private until completion, including passes and the opening bid", () => { + const state = game(); + const [opener, winner, passer, last] = open(state); + applyMove(state, { action: "bid", amount: 40 }, winner); + applyMove(state, { action: "bidPass" }, passer); + for (const viewer of [undefined, opener, winner, passer, last]) { + const during = stripSecret(state, viewer); + assert.equal(sealedAuctionHistory(during.log).results.size, 0); + assert.equal(sealedAuctionHistory(during.log).revealed.size, 0); + assert.ok(describeLog(during).at(-1)?.includes("takes part")); + assert.ok(describeLog(during).at(-2)?.includes("takes part")); + } + applyMove(state, { action: "bid", amount: 30 }, last); + for (const viewer of [undefined, opener, winner, passer, last]) { + const after = stripSecret(state, viewer); + const history = sealedAuctionHistory(after.log); + assert.equal(history.revealed.size, 4); + assert.deepEqual(history.results.get(after.log.length - 1), { + winner, + bids: [ + { player: winner, amount: 40 }, + { player: last, amount: 30 }, + { player: opener, amount: 25 }, + { player: passer, amount: 0 }, + ], + }); + } +}); + +test("completed bids remain public during the next sealed auction, including sliced descriptions", () => { + const state = game(); + const [opener, winner, passer, last] = open(state); + const openingIndex = state.log.length - 1; + applyMove(state, { action: "bid", amount: 40 }, winner); + applyMove(state, { action: "bidPass" }, passer); + applyMove(state, { action: "bid", amount: 30 }, last); + const resultIndex = state.log.length - 1; + applyMove(state, { action: "pay", cards: [0, 1, 2, 3] }, winner); + open(state); + applyMove(state, { action: "bidPass" }, winner); + for (const viewer of [undefined, opener, winner, passer, last]) { + const visible = stripSecret(state, viewer); + const lines = describeLog(visible); + assert.match(lines[openingIndex]!, /up for auction at 25/); + assert.equal(lines[openingIndex + 1], `${state.players[winner]!.name} bids 40`); + assert.equal(lines[openingIndex + 2], `${state.players[passer]!.name} passes on the auction`); + assert.equal(describeLogEntry(visible, visible.log[openingIndex + 1]!), lines[openingIndex + 1]); + assert.match(lines.at(-2)!, /up for sealed auction$/); + assert.match(lines.at(-1)!, /takes part in the sealed auction$/); + assert.deepEqual([...sealedAuctionHistory(visible.log).results.keys()], [resultIndex]); + const slice = logSlice(state, { player: viewer, start: openingIndex + 1, end: openingIndex + 3 }); + assert.deepEqual( + slice.log.map((entry) => entry.simple), + lines.slice(openingIndex + 1, openingIndex + 3) + ); + } + const replayed = replay(stripSecret(state)); + assert.deepEqual(sealedAuctionHistory(replayed.log), sealedAuctionHistory(stripSecret(state).log)); +}); + +test("an immediately resolved auction reveals the opener and every automatic pass", () => { + const state = game(); + const opener = state.activeSeat; + for (const [seat, player] of state.players.entries()) { + if (seat !== opener) { + player.hand = []; + } + } + open(state); + assert.equal(state.phase, "auctionPayment"); + const visible = stripSecret(state); + const last = visible.log.at(-1); + assert.ok(last?.type === "move" && last.move.action === "auction"); + assert.equal(last.move.bid, 25); + const result = sealedAuctionHistory(visible.log).results.get(visible.log.length - 1); + assert.equal(result?.bids.length, 4); + assert.equal(result?.bids.filter((bid) => bid.amount === 0).length, 3); + assert.deepEqual(result?.bids[0], { player: opener, amount: 25 }); +}); + +test("a pass can resolve the result; tied bids put the winner first", () => { + const state = game(); + const [opener, bidder, passer, last] = open(state); + applyMove(state, { action: "bid", amount: 40 }, bidder); + applyMove(state, { action: "bid", amount: 40 }, passer); + applyMove(state, { action: "bidPass" }, last); + const result = sealedAuctionHistory(stripSecret(state).log).results.get(state.log.length - 1)!; + assert.equal(result.bids[0]?.player, state.auction?.highBidder); + assert.equal(result.bids[0]?.amount, 40); + assert.equal(result.bids.find((bid) => bid.player === opener)?.amount, 25); + assert.equal(result.bids.find((bid) => bid.player === last)?.amount, 0); +}); diff --git a/packages/engine/src/auction-history.ts b/packages/engine/src/auction-history.ts new file mode 100644 index 0000000..8c5525e --- /dev/null +++ b/packages/engine/src/auction-history.ts @@ -0,0 +1,54 @@ +import type { LogEntry } from "./types.js"; + +export interface SealedAuctionResult { + winner: number; + bids: { player: number; amount: number }[]; +} + +export function sealedAuctionHistory(log: LogEntry[]): { + revealed: Set; + results: Map; +} { + const revealed = new Set(); + const results = new Map(); + let pending: number[] = []; + let bids = new Map(); + for (const [index, entry] of log.entries()) { + if (entry.type !== "move") { + continue; + } + if (entry.move.action === "auction") { + pending = []; + bids = new Map([[entry.player, entry.move.bid]]); + } else if (entry.move.action === "bid") { + bids.set(entry.player, entry.move.amount); + } else if (entry.move.action === "bidPass") { + bids.set(entry.player, 0); + } else { + continue; + } + pending.push(index); + for (const seat of entry.info?.autoPassed ?? []) { + bids.set(seat, 0); + } + if (entry.info?.winningBid !== undefined) { + for (const bidIndex of pending) { + revealed.add(bidIndex); + } + const winner = entry.info.winner ?? entry.player; + results.set(index, { + winner, + bids: [...bids] + .filter(([, amount]) => amount >= 0) + .map(([player, amount]) => ({ player, amount })) + .sort( + (a, b) => + b.amount - a.amount || Number(b.player === winner) - Number(a.player === winner) || a.player - b.player + ), + }); + pending = []; + bids.clear(); + } + } + return { revealed, results }; +} diff --git a/packages/engine/src/describe.ts b/packages/engine/src/describe.ts index d895220..3d3a1d0 100644 --- a/packages/engine/src/describe.ts +++ b/packages/engine/src/describe.ts @@ -1,4 +1,5 @@ import { FACTORIES, KICKER_SPECS, UPGRADE_SPECS } from "./data.js"; +import { sealedAuctionHistory } from "./auction-history.js"; import type { GameState, LogEntry, MoveInfo } from "./types.js"; /** Display name of the card a move concerns (colony upgrade or Kicker card). */ @@ -43,11 +44,15 @@ function autoPassSuffix(state: GameState, info: MoveInfo | undefined): string { * no auto-pass names. Once the resolving move is logged the real moves are * revealed (amounts, passes) by the wrapper's unmasking and described fully. */ -function sealedQuiet(state: GameState): boolean { - return state.phase === "auction" && state.auction?.bids !== undefined; +function sealedQuiet(state: GameState, entry: LogEntry, revealed?: boolean): boolean { + return ( + state.phase === "auction" && + state.auction?.bids !== undefined && + !(revealed ?? sealedAuctionHistory(state.log).revealed.has(state.log.indexOf(entry))) + ); } -export function describeLogEntry(state: GameState, entry: LogEntry): string { +export function describeLogEntry(state: GameState, entry: LogEntry, sealedBidVisible?: boolean): string { switch (entry.type) { case "init": return `Game started with ${entry.players} players`; @@ -82,7 +87,7 @@ export function describeLogEntry(state: GameState, entry: LogEntry): string { // fastBid: while the sealed bids are collected, the opening is // neutral for everyone (even the auctioneer — the amount is // sealed). After resolution the wrapper reveals the bid. - if (sealedQuiet(state) || move.bid < 0) { + if (sealedQuiet(state, entry, sealedBidVisible) || move.bid < 0) { return `${name} puts ${cardName(info, "an upgrade")} up for sealed auction`; } return ( @@ -94,7 +99,7 @@ export function describeLogEntry(state: GameState, entry: LogEntry): string { const won = playerName(state, info.winner ?? entry.player); return `${name} bids ${move.amount} (sealed) — ${won} wins at ${sealedPrice(info)}`; } - if (sealedQuiet(state) || move.amount < 0) { + if (sealedQuiet(state, entry, sealedBidVisible) || move.amount < 0) { return `${name} takes part in the sealed auction`; } return `${name} bids ${move.amount}` + autoPassSuffix(state, info); @@ -105,7 +110,7 @@ export function describeLogEntry(state: GameState, entry: LogEntry): string { const won = playerName(state, info.winner ?? entry.player); return `${name} passes — ${won} wins the sealed auction at ${sealedPrice(info)}`; } - if (sealedQuiet(state)) { + if (sealedQuiet(state, entry, sealedBidVisible)) { return `${name} takes part in the sealed auction`; } return `${name} passes on the auction` + autoPassSuffix(state, info); @@ -158,5 +163,6 @@ export function describeLogEntry(state: GameState, entry: LogEntry): string { } export function describeLog(state: GameState): string[] { - return state.log.map((entry) => describeLogEntry(state, entry)); + const { revealed } = sealedAuctionHistory(state.log); + return state.log.map((entry, index) => describeLogEntry(state, entry, revealed.has(index))); } diff --git a/packages/engine/wrapper.ts b/packages/engine/wrapper.ts index 322fc94..96d6089 100644 --- a/packages/engine/wrapper.ts +++ b/packages/engine/wrapper.ts @@ -4,6 +4,7 @@ import { RESOURCES } from "./src/types.js"; import { maxBid, needsMegaChoice } from "./src/state.js"; import { moveAI as moveAICore } from "./src/ai.js"; import { describeLogEntry } from "./src/describe.js"; +import { sealedAuctionHistory } from "./src/auction-history.js"; import { applyMove, dropPlayer as dropPlayerCore, initGame } from "./src/moves.js"; import { rankings as computeRankings } from "./src/rankings.js"; import { replay as replayCore } from "./src/replay.js"; @@ -183,43 +184,6 @@ function hideProduced( }; } -/** - * fastBid reveal: the log indexes whose sealed bids are public because their - * auction has already resolved by that point. The resolving move itself (the - * one carrying info.winningBid) is where the bids turn visible, so its own - * index is included. A stripped log is the source of truth for replays, so - * the reveal must be derivable from the stripped entries alone — it is: the - * resolution outcome (winningBid/secondBid/winner) is deliberately never - * masked, and auction boundaries are marked by "auction" moves. - */ -function revealedBidIndexes(log: LogEntry[]): Set { - const revealed = new Set(); - // Indexes of the bid moves of the auction currently collecting sealed bids. - let pending: number[] = []; - for (let i = 0; i < log.length; i++) { - const entry = log[i] as LogEntry; - if (entry.type !== "move") { - continue; - } - if (entry.move.action === "auction") { - pending = [i]; - continue; - } - if (entry.move.action === "bid") { - pending.push(i); - } - if (entry.info?.winningBid !== undefined) { - // The auction resolves here: every sealed bid of it is revealed. - for (const j of pending) { - revealed.add(j); - } - revealed.add(i); - pending = []; - } - } - return revealed; -} - export function stripSecret(data: GameState, player?: number): GameState { const viewer = player !== undefined && player >= 0 ? player : undefined; // fastBid: other players' sealed bids are hidden while the auction runs. @@ -303,7 +267,7 @@ function maskLog( start = 0, end = data.log.length ): (LogEntry & { simple?: string })[] { - const revealed = revealedBidIndexes(data.log); + const { revealed } = sealedAuctionHistory(data.log); const hideOwn = hideOwnProduction(data, viewer); // An exchange's received card is parked on the upgrade until the exchange // step (end of the discard phase) closes, i.e. while data.exchange is set. @@ -357,9 +321,10 @@ export function logSlice(data: GameState, options?: LogSliceOptions): LogSliceRe // in the game list, and our structured entries otherwise stringify to noise. // describeLogEntry never reveals hidden values (unresolved sealed bids, // exchange takes). - const log = maskLog(data, viewer, start, end).map((masked) => ({ + const { revealed } = sealedAuctionHistory(data.log); + const log = maskLog(data, viewer, start, end).map((masked, index) => ({ ...masked, - simple: describeLogEntry(data, masked), + simple: describeLogEntry(data, masked, revealed.has(start + index)), })); const result: LogSliceResult = { log }; if (options?.end === undefined) { diff --git a/packages/viewer/package.json b/packages/viewer/package.json index 674501d..88ef6f0 100644 --- a/packages/viewer/package.json +++ b/packages/viewer/package.json @@ -1,6 +1,6 @@ { "name": "outpost-viewer", - "version": "0.1.16", + "version": "0.1.17", "description": "Browser viewer for the Outpost game on boardgamers.space", "license": "AGPL-3.0", "files": [ diff --git a/packages/viewer/src/lib/LogFeed.svelte b/packages/viewer/src/lib/LogFeed.svelte index 50c72c5..5389dd7 100644 --- a/packages/viewer/src/lib/LogFeed.svelte +++ b/packages/viewer/src/lib/LogFeed.svelte @@ -1,5 +1,6 @@