diff --git a/ACHIEVEMENTS.md b/ACHIEVEMENTS.md new file mode 100644 index 0000000..fe8577c --- /dev/null +++ b/ACHIEVEMENTS.md @@ -0,0 +1,76 @@ +# Outpost achievements + +Revision 1 exposes 16 game-local achievements through the optional `achievementDefinitions()` +and `achievements(data)` exports of `dist/wrapper.js`. Types come from the published, +exact-pinned `@boardgamers/protocol@0.11.0` `/engine` entry point (type-only imports). +No viewer changes or browser claims are involved. Icons are omitted so the platform can +use generic artwork; there are no third-party assets or viewer-bundle URLs. + +## Catalog + +| ID | Name | Final-state condition | +| ------------------------ | ----------------------- | ----------------------------------------------------- | +| `colony-victor` | Colony Victor | Sole first place | +| `head-to-head` | Head to Head | Sole first place with two original seats | +| `crowded-frontier` | Crowded Frontier | Sole first place with at least five original seats | +| `kicker-victor` | Kicker Victor | Sole first place with `options.kicker === true` | +| `sealed-bid-victor` | Sealed Bid Victor | Sole first place with `options.fastBid === true` | +| `seventy-five-points` | Victory Point Milestone | At least 75 VP, the normal victory target | +| `water-megaplex` | Water Megaplex | At least four manned water factories | +| `titanium-megaplex` | Titanium Megaplex | At least four manned titanium factories | +| `chemical-megaplex` | Chemical Megaplex | At least four manned new chemicals factories | +| `diversified-industry` | Diversified Industry | At least one manned factory of each of the five types | +| `research-campus` | Research Campus | Scientists, Laboratory, and a manned research factory | +| `automated-colony` | Automated Colony | Robots upgrade and at least one robot | +| `established-outpost` | Established Outpost | Outpost upgrade | +| `orbital-network` | Orbital Network | Space Station, Planetary Cruiser, and Moon Base | +| `lunar-colony` | Lunar Colony | Moon Base | +| `self-sustaining-colony` | Self-Sustaining Colony | Ecoplants and Nodule | + +Only the first five require victory. Factory thresholds of four correspond to the +actual Mega production rule; they do not claim a Mega card was taken. All ownership and +operation conditions describe the final position, not historical purchases or production. +The 75-VP check uses engine scoring, including Kicker VP and only manned factory VP. +IDs must never be repurposed; definition changes require a manifest revision bump. + +## Authority and eligibility + +The hook accepts the server's JSON-serializable engine state, not a JSON string or a +client report. It is deterministic and does not mutate that state. It returns nothing +unless `ended` is true, phase is `ended`, round exceeds one (the wrapper's cancellation +boundary), and at least two seats exist. Dropped seats are omitted, including their feats. +Original seat count includes dropped and bot seats; it is not a claim about human opponents. + +Outcomes use the existing final rankings: sole rank 1 is `win`, shared rank 1 is `draw`, +and other ranks are `loss`. Spending breaks purchase-order ties only, never final ties. +The remaining player can win after other seats drop, as in existing rankings. Engine +round-limit or stalled-economy completion uses the same rankings rather than inventing +new outcome rules. There is no supported solo game. + +**BGS is responsible for mapping seats to eligible human accounts**, rejecting bots, +practice/analysis games and cancelled games, and invoking the hook only on authoritative +completed game data. Engine state has no account or bot identity and cannot enforce those +platform policies. Calling `moveAI` in a test does not turn a seat into a platform bot. +Neither a seed nor a final log entry is treated as an authentication credential. + +## Verification and release gate + +`src/achievements.spec.ts` exercises games actually played to completion through the +engine (base two-player and five-player Kicker/sealed-bid), JSON round trips, outcomes, +non-mutation, fresh manifests, cancellation, dropped seats, ties and per-feat boundaries. +Constructed final positions are boundary tests, not claims that their logs reproduce them. +Run the complete `pnpm check` before committing or releasing. + +This implementation is held on an isolated review branch. Before release: + +1. Obtain independent coordinator review, including the platform's human/account and + authoritative-completion eligibility gates. +2. Integrate the then-current canonical release branch without disturbing local work, + choose an unused engine patch version and rerun `pnpm check`. +3. Follow `AGENTS.md` publishing: pack the engine, upload to existing game version doc 1, + and verify `engine.package.version`. This is engine-only; no viewer upload is needed. +4. Verify platform ingestion of manifest revision 1 and eligible completed-game awards. +5. Commit and push the released sources/version to the canonical release branch under + the repository's delivery workflow, and verify its remote head. + +No BGS upload, activation, or release-branch push is authorized before that review. diff --git a/README.md b/README.md index dcd7c50..1591c44 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,9 @@ Two packages: - **`packages/viewer`** (`outpost-viewer`) — Svelte 5 browser UI, built as a self-contained IIFE bundle for the BGS iframe (`window.outpost.launch(selector)`). +The engine's completed-game achievement hooks and release requirements are documented in +[ACHIEVEMENTS.md](ACHIEVEMENTS.md). + ## Play online at [boardgamers.space](https://boardgamers.space/boardgame/outpost) diff --git a/packages/engine/package.json b/packages/engine/package.json index b45f915..d62e5cf 100644 --- a/packages/engine/package.json +++ b/packages/engine/package.json @@ -35,6 +35,7 @@ "seedrandom": "3.0.5" }, "devDependencies": { + "@boardgamers/protocol": "0.11.0", "@types/node": "24.10.1", "@types/seedrandom": "3.0.8", "typescript": "5.9.3" diff --git a/packages/engine/src/achievements.spec.ts b/packages/engine/src/achievements.spec.ts new file mode 100644 index 0000000..d33cec0 --- /dev/null +++ b/packages/engine/src/achievements.spec.ts @@ -0,0 +1,289 @@ +import assert from "node:assert/strict"; +import { test } from "node:test"; +import type { Engine } from "@boardgamers/protocol/engine"; +import * as wrapper from "../wrapper.js"; +import { moveAI } from "./ai.js"; +import { initGame } from "./moves.js"; +import { rankings } from "./rankings.js"; +import { FACTORY_TYPES, UPGRADES, type GameState, type PlayerState } from "./types.js"; + +const hooks: Pick, "achievementDefinitions" | "achievements"> = wrapper; + +function complete(players: number, options: Record, seed: string): GameState { + const state = initGame(players, options, seed); + for (let moves = 0; !state.ended && moves < 100000; moves++) { + const current = wrapper.currentPlayer(state); + const seat = Array.isArray(current) ? current[0] : current; + assert.notEqual(seat, undefined); + moveAI(state, seat!); + } + assert.equal(state.ended, true); + assert.equal(wrapper.cancelled(state), false); + assert.equal(state.log.at(-1)?.type, "end"); + return JSON.parse(JSON.stringify(state)) as GameState; +} + +const completed = complete(2, {}, "ai-2p"); + +function boundaryState(): GameState { + const state = structuredClone(completed); + for (const player of state.players) { + player.factories = []; + player.robots = 0; + for (const upgrade of UPGRADES) { + player.upgrades[upgrade] = 0; + } + for (const kicker of Object.keys(player.kickers) as (keyof PlayerState["kickers"])[]) { + player.kickers[kicker] = 0; + } + } + state.players[1]!.upgrades.moonBase = 4; + return state; +} + +function earned(state: GameState, id: string): boolean { + return wrapper + .achievements(state) + .find((result) => result.seat === 0)! + .earned.includes(id); +} + +test("manifest uses stable unique IDs, fresh literals and published hook types", async () => { + const manifest = wrapper.achievementDefinitions(); + assert.equal(manifest.revision, 1); + assert.equal(manifest.achievements.length, 16); + assert.equal(new Set(manifest.achievements.map((entry) => entry.id)).size, 16); + for (const entry of manifest.achievements) { + assert.match(entry.id, /^[a-z0-9]+(?:-[a-z0-9]+)*$/); + assert.ok(entry.id.length <= 64); + assert.ok(entry.name && entry.description); + } + manifest.achievements[0]!.name = "changed"; + assert.notEqual(wrapper.achievementDefinitions().achievements[0]!.name, "changed"); + assert.deepEqual(await hooks.achievements!(completed), wrapper.achievements(completed)); +}); + +test("real completed games: deterministic, read-only, rank-aligned and variant-aware", () => { + for (const state of [completed, complete(5, { kicker: true, fastBid: true }, "achievement-kicker")]) { + const before = JSON.stringify(state); + const results = wrapper.achievements(state); + assert.deepEqual(results, wrapper.achievements(JSON.parse(before) as GameState)); + assert.equal(JSON.stringify(state), before); + const ranks = rankings(state); + for (const result of results) { + const win = ranks[result.seat] === 1 && ranks.filter((rank) => rank === 1).length === 1; + assert.equal(result.outcome, ranks[result.seat] !== 1 ? "loss" : win ? "win" : "draw"); + assert.equal(result.earned.includes("colony-victor"), win); + assert.equal(result.earned.includes("head-to-head"), win && state.players.length === 2); + assert.equal(result.earned.includes("crowded-frontier"), win && state.players.length >= 5); + assert.equal(result.earned.includes("kicker-victor"), win && state.options.kicker === true); + assert.equal(result.earned.includes("sealed-bid-victor"), win && state.options.fastBid === true); + assert.ok( + result.earned.every((id) => wrapper.achievementDefinitions().achievements.some((entry) => entry.id === id)) + ); + } + } +}); + +test("reject ongoing, cancelled and solo states; exclude dropped seats", () => { + assert.deepEqual(wrapper.achievements(initGame(2, {}, "ongoing")), []); + for (const change of [ + (state: GameState) => { + state.ended = false; + }, + (state: GameState) => { + state.phase = "actions"; + }, + (state: GameState) => { + state.round = 1; + }, + (state: GameState) => { + state.players.pop(); + }, + ]) { + const state = structuredClone(completed); + change(state); + assert.deepEqual(wrapper.achievements(state), []); + } + const state = boundaryState(); + state.players[1]!.dropped = true; + assert.deepEqual(wrapper.achievements(state), [ + { seat: 0, earned: ["colony-victor", "head-to-head"], outcome: "win" }, + ]); + state.players[0]!.dropped = true; + assert.deepEqual(wrapper.achievements(state), []); +}); + +test("top ties draw regardless of spending; lower ties lose and non-win feats remain", () => { + const state = boundaryState(); + state.players[0]!.upgrades.moonBase = 4; + state.players[0]!.spent = 99999; + for (const result of wrapper.achievements(state)) { + assert.equal(result.outcome, "draw"); + assert.ok(result.earned.includes("seventy-five-points")); + assert.ok(!result.earned.includes("colony-victor")); + } + state.players.push(structuredClone(state.players[0]!)); + state.players[2]!.upgrades.moonBase = 5; + assert.deepEqual( + wrapper.achievements(state).map((result) => result.outcome), + ["loss", "loss", "win"] + ); +}); + +test("win feats honor exact option flags and original seat count boundaries", () => { + const state = boundaryState(); + state.players.reverse(); + for (const value of [undefined, false, "true", 1]) { + state.options = { kicker: value, fastBid: value }; + assert.equal(earned(state, "kicker-victor"), false); + assert.equal(earned(state, "sealed-bid-victor"), false); + } + state.options = { kicker: true, fastBid: true }; + assert.equal(earned(state, "kicker-victor"), true); + assert.equal(earned(state, "sealed-bid-victor"), true); + state.players.push(structuredClone(state.players[1]!)); + assert.equal(earned(state, "head-to-head"), false); + state.players.push(structuredClone(state.players[1]!)); + assert.equal(earned(state, "crowded-frontier"), false); + state.players.push(structuredClone(state.players[1]!)); + assert.equal(earned(state, "crowded-frontier"), true); +}); + +test("compound upgrade feats require every component", () => { + const requirements = [ + ["research-campus", ["scientists", "laboratory"]], + ["automated-colony", ["robots"]], + ["orbital-network", ["spaceStation", "planetaryCruiser", "moonBase"]], + ["self-sustaining-colony", ["ecoplants", "nodule"]], + ] as const; + for (const [id, upgrades] of requirements) { + const state = boundaryState(); + const player = state.players[0]!; + player.factories = [{ type: "research", manned: true }]; + player.robots = 1; + for (const upgrade of upgrades) { + player.upgrades[upgrade] = 1; + } + assert.equal(earned(state, id), true); + for (const upgrade of upgrades) { + player.upgrades[upgrade] = 0; + assert.equal(earned(state, id), false); + player.upgrades[upgrade] = 1; + } + } +}); + +const cases: [string, (player: PlayerState) => void, (player: PlayerState) => void][] = [ + [ + "seventy-five-points", + (p) => { + p.upgrades.moonBase = 3; + p.upgrades.planetaryCruiser = 1; + }, + (p) => { + p.upgrades.planetaryCruiser = 0; + p.factories = Array.from({ length: 14 }, () => ({ type: "ore", manned: true })); + }, + ], + ...[ + ["water-megaplex", "water"], + ["titanium-megaplex", "titanium"], + ["chemical-megaplex", "newChemicals"], + ].map( + ([id, type]) => + [ + id!, + (p: PlayerState) => { + p.factories = Array.from({ length: 4 }, () => ({ + type: type as "water" | "titanium" | "newChemicals", + manned: true, + })); + }, + (p: PlayerState) => { + p.factories[0]!.manned = false; + }, + ] as [string, (player: PlayerState) => void, (player: PlayerState) => void] + ), + [ + "diversified-industry", + (p) => { + p.factories = FACTORY_TYPES.map((type) => ({ type, manned: true })); + }, + (p) => { + p.factories[0]!.manned = false; + }, + ], + [ + "research-campus", + (p) => { + p.upgrades.scientists = 1; + p.upgrades.laboratory = 1; + p.factories = [{ type: "research", manned: true }]; + }, + (p) => { + p.factories[0]!.manned = false; + }, + ], + [ + "automated-colony", + (p) => { + p.upgrades.robots = 1; + p.robots = 1; + }, + (p) => { + p.robots = 0; + }, + ], + [ + "established-outpost", + (p) => { + p.upgrades.outpost = 1; + }, + (p) => { + p.upgrades.outpost = 0; + }, + ], + [ + "orbital-network", + (p) => { + p.upgrades.spaceStation = 1; + p.upgrades.planetaryCruiser = 1; + p.upgrades.moonBase = 1; + }, + (p) => { + p.upgrades.spaceStation = 0; + }, + ], + [ + "lunar-colony", + (p) => { + p.upgrades.moonBase = 1; + }, + (p) => { + p.upgrades.moonBase = 0; + }, + ], + [ + "self-sustaining-colony", + (p) => { + p.upgrades.ecoplants = 1; + p.upgrades.nodule = 1; + }, + (p) => { + p.upgrades.nodule = 0; + }, + ], +]; + +for (const [id, qualify, disqualify] of cases) { + test(`${id}: positive and negative final-state boundaries without victory`, () => { + const state = boundaryState(); + assert.equal(earned(state, id), false); + qualify(state.players[0]!); + assert.equal(wrapper.achievements(state)[0]!.outcome, "loss"); + assert.equal(earned(state, id), true); + disqualify(state.players[0]!); + assert.equal(earned(state, id), false); + }); +} diff --git a/packages/engine/src/achievements.ts b/packages/engine/src/achievements.ts new file mode 100644 index 0000000..f719bb8 --- /dev/null +++ b/packages/engine/src/achievements.ts @@ -0,0 +1,129 @@ +import type { AchievementManifest, SeatAchievements } from "@boardgamers/protocol/engine"; +import { rankings } from "./rankings.js"; +import { victoryPoints } from "./state.js"; +import { FACTORY_TYPES, type FactoryType, type GameState, type PlayerState } from "./types.js"; + +type Context = { state: GameState; player: PlayerState; win: boolean }; +const operated = (player: PlayerState, type: FactoryType): number => + player.factories.filter((factory) => factory.manned && factory.type === type).length; + +const catalog: { id: string; name: string; description: string; qualifies: (context: Context) => boolean }[] = [ + { + id: "colony-victor", + name: "Colony Victor", + description: "Finish alone in first place.", + qualifies: ({ win }) => win, + }, + { + id: "head-to-head", + name: "Head to Head", + description: "Win outright in a two-seat game.", + qualifies: ({ win, state }) => win && state.players.length === 2, + }, + { + id: "crowded-frontier", + name: "Crowded Frontier", + description: "Win outright in a game with at least five seats.", + qualifies: ({ win, state }) => win && state.players.length >= 5, + }, + { + id: "kicker-victor", + name: "Kicker Victor", + description: "Win outright with the Kicker expansion enabled.", + qualifies: ({ win, state }) => win && state.options.kicker === true, + }, + { + id: "sealed-bid-victor", + name: "Sealed Bid Victor", + description: "Win outright with sealed bidding enabled.", + qualifies: ({ win, state }) => win && state.options.fastBid === true, + }, + { + id: "seventy-five-points", + name: "Victory Point Milestone", + description: "Finish with at least 75 victory points.", + qualifies: ({ player }) => victoryPoints(player) >= 75, + }, + { + id: "water-megaplex", + name: "Water Megaplex", + description: "Finish operating at least four water factories.", + qualifies: ({ player }) => operated(player, "water") >= 4, + }, + { + id: "titanium-megaplex", + name: "Titanium Megaplex", + description: "Finish operating at least four titanium factories.", + qualifies: ({ player }) => operated(player, "titanium") >= 4, + }, + { + id: "chemical-megaplex", + name: "Chemical Megaplex", + description: "Finish operating at least four new chemicals factories.", + qualifies: ({ player }) => operated(player, "newChemicals") >= 4, + }, + { + id: "diversified-industry", + name: "Diversified Industry", + description: "Finish operating at least one factory of each of the five types.", + qualifies: ({ player }) => FACTORY_TYPES.every((type) => operated(player, type) > 0), + }, + { + id: "research-campus", + name: "Research Campus", + description: "Finish owning Scientists and a Laboratory and operating a research factory.", + qualifies: ({ player }) => + player.upgrades.scientists > 0 && player.upgrades.laboratory > 0 && operated(player, "research") > 0, + }, + { + id: "automated-colony", + name: "Automated Colony", + description: "Finish owning the Robots upgrade and at least one robot.", + qualifies: ({ player }) => player.upgrades.robots > 0 && player.robots > 0, + }, + { + id: "established-outpost", + name: "Established Outpost", + description: "Finish owning an Outpost upgrade.", + qualifies: ({ player }) => player.upgrades.outpost > 0, + }, + { + id: "orbital-network", + name: "Orbital Network", + description: "Finish owning a Space Station, Planetary Cruiser and Moon Base.", + qualifies: ({ player }) => + player.upgrades.spaceStation > 0 && player.upgrades.planetaryCruiser > 0 && player.upgrades.moonBase > 0, + }, + { + id: "lunar-colony", + name: "Lunar Colony", + description: "Finish owning a Moon Base.", + qualifies: ({ player }) => player.upgrades.moonBase > 0, + }, + { + id: "self-sustaining-colony", + name: "Self-Sustaining Colony", + description: "Finish owning Ecoplants and a Nodule.", + qualifies: ({ player }) => player.upgrades.ecoplants > 0 && player.upgrades.nodule > 0, + }, +]; + +export function achievementDefinitions(): AchievementManifest { + return { revision: 1, achievements: catalog.map(({ id, name, description }) => ({ id, name, description })) }; +} + +export function achievements(state: GameState): SeatAchievements[] { + if (!state.ended || state.phase !== "ended" || state.round <= 1 || state.players.length < 2) { + return []; + } + const ranks = rankings(state); + const leaders = state.players.filter((player, seat) => !player.dropped && ranks[seat] === 1).length; + return state.players.flatMap((player, seat): SeatAchievements[] => { + if (player.dropped) { + return []; + } + const outcome = ranks[seat] === 1 ? (leaders === 1 ? "win" : "draw") : "loss"; + const context = { state, player, win: outcome === "win" }; + return [{ seat, earned: catalog.filter((entry) => entry.qualifies(context)).map((entry) => entry.id), outcome }]; + }); +} diff --git a/packages/engine/wrapper.ts b/packages/engine/wrapper.ts index d10c7a1..67ea67d 100644 --- a/packages/engine/wrapper.ts +++ b/packages/engine/wrapper.ts @@ -1,3 +1,5 @@ +export { achievementDefinitions, achievements } from "./src/achievements.js"; + import { canReviseChoice } from "./src/choice-revisions.js"; import { PRODUCTION_DECKS, KICKER_SPECS, UPGRADE_SPECS } from "./src/data.js"; import { nextInt, shuffle } from "./src/prng.js"; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 41a56d0..ed70d3e 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -27,6 +27,9 @@ importers: specifier: 3.0.5 version: 3.0.5 devDependencies: + '@boardgamers/protocol': + specifier: 0.11.0 + version: 0.11.0 '@types/node': specifier: 24.10.1 version: 24.10.1 @@ -70,6 +73,9 @@ packages: '@boardgamers/protocol@0.10.0': resolution: {integrity: sha512-U+nOK8xeQANvjzwacHirBW+ItDf9AOmHLtkoyTI/XYFCOpL6lPsEgCbmcxnmkXv2a1/lgtVN3WpSERHUqkQkXw==} + '@boardgamers/protocol@0.11.0': + resolution: {integrity: sha512-YQ54+aVOIZvxUJvFmUbZR7xSBEgQaemstPdmYKVGCUHCviRX2UXRLgqLSQl6PFhr++lUT+Nynbf+p7h19zN+/g==} + '@jridgewell/gen-mapping@0.3.13': resolution: {integrity: sha512-2kkt/7niJ6MgEPxF0bYdQ6etZaA+fQvDcLKckhy1yIQOzaoKjBBjSj63/aLVjYE3qhRt5dvM+uUyfCg6UKCBbA==} @@ -776,6 +782,10 @@ snapshots: dependencies: zod: 4.6.2 + '@boardgamers/protocol@0.11.0': + dependencies: + zod: 4.6.2 + '@jridgewell/gen-mapping@0.3.13': dependencies: '@jridgewell/sourcemap-codec': 1.5.5 diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml index 05abd2e..07ce0bd 100644 --- a/pnpm-workspace.yaml +++ b/pnpm-workspace.yaml @@ -1,4 +1,4 @@ packages: - "packages/*" minimumReleaseAgeExclude: - - "@boardgamers/protocol@0.3.1 || 0.3.3 || 0.4.0 || 0.4.2 || 0.5.0 || 0.6.0 || 0.6.1 || 0.6.2 || 0.8.1 || 0.10.0" + - "@boardgamers/protocol@0.3.1 || 0.3.3 || 0.4.0 || 0.4.2 || 0.5.0 || 0.6.0 || 0.6.1 || 0.6.2 || 0.8.1 || 0.10.0 || 0.11.0"