diff --git a/docs/achievements.md b/docs/achievements.md new file mode 100644 index 0000000..33e8ac8 --- /dev/null +++ b/docs/achievements.md @@ -0,0 +1,59 @@ +# Fuji achievements — revision 1 + +Private-playtest feature only. This does not change visibility, resolve the BGS +publication gates, or authorize a release. The engine and viewer use the published +`@boardgamers/protocol` 0.11.0 package. The wrapper exports the optional +`achievementDefinitions()` and `achievements(state)` hooks from its engine contract. +The latter accepts the persisted JSON-compatible engine state (not a JSON string). + +## Manifest + +All feats require a completed cooperative victory and are awarded to every seat: + +| Stable ID | Requirement | +| ----------------------------------------------- | --------------------------------------------------------------------------- | +| `escape-together` | Win an expedition. | +| `scenario-1-escape` through `scenario-7-escape` | Win that numbered scenario, at any difficulty. | +| `difficulty-2-escape` | Win at difficulty 2 or higher. | +| `difficulty-3-escape` | Win at difficulty 3 or higher. | +| `difficulty-4-escape` | Win at difficulty 4. | +| `strength-to-spare` | Everyone has at least 11 stamina remaining. | +| `unscathed-team` | Everyone has no injuries, including pending injury selections. | +| `narrow-escape` | Someone has 5 or less stamina remaining. | +| `well-equipped` | Everyone still carries an equipment card (including a partially used card). | +| `four-to-safety` | Win with four explorers. | + +Sixteen definitions in total. IDs must not be repurposed; definition changes require +a manifest revision bump. Icons are omitted for the platform's default game-icon +fallback; no transient asset URLs or unregistered asset keys are introduced. + +## Semantics and eligibility + +Fuji's ranks are all 1, even on defeat. Explicit `won` / `lost` outcomes map to +`win` / `loss` for **every** seat. Losses receive no feats. BGS remains responsible +for account mapping and bot, dropped-player and cancellation eligibility; the +engine does not guess human identity from names. Drop-induced engine defeats +still report loss rather than falsely reporting a win from tied rankings. + +Only `phase: ended` with an actual outcome is evaluated. Explicit boolean +`initOptions.tutorial`, `practice`, or `analysis` excludes the session entirely. +Tutorial construction and both analysis constructors now set their respective +flags. Ordinary games, including historical saves without these flags, remain +eligible; old saves without a resolved scenario cannot earn a scenario feat. +A random scenario uses its resolved `state.scenario`, not the setup option. +No seed-prefix heuristics or undocumented history claims are used. + +Importantly, the internal `player.stamina` is **exhaustion accumulated**, from 0 +to 25, not stamina remaining. These checks use `EXHAUSTION - player.stamina`. +All feats inspect final state only: no claims about never using equipment, +rerolling, communications, or past dice. No private data is returned or logged. + +## Verification + +Tests validate the manifest and results with the official protocol schemas and +optional-method inspector. Deterministic legal moveAI expeditions (used as a +fixture driver, not evidence of account eligibility) reach every one of the 16 +feats, with exact engine replays and JSON round trips. Additional tests cover a +natural defeat, drop defeat, all-seat equality, difficulty stacking, scenario +exclusivity, excluded/incomplete sessions, purity, independent result arrays, +and explicitly synthetic threshold probes. Browser behavior is not asserted. diff --git a/packages/engine/package.json b/packages/engine/package.json index 1f48544..4a20386 100644 --- a/packages/engine/package.json +++ b/packages/engine/package.json @@ -19,6 +19,7 @@ "prepack": "pnpm build" }, "dependencies": { + "@boardgamers/protocol": "0.11.0", "seedrandom": "3.0.5" }, "devDependencies": { diff --git a/packages/engine/src/achievements.spec.ts b/packages/engine/src/achievements.spec.ts new file mode 100644 index 0000000..a62b0d8 --- /dev/null +++ b/packages/engine/src/achievements.spec.ts @@ -0,0 +1,143 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import { achievementManifestSchema, achievementResultsSchema } from "@boardgamers/protocol/achievements"; +import { inspectEngine } from "@boardgamers/protocol/engine"; +import * as wrapper from "../wrapper.js"; +import { initGame, replay, type State } from "../index.js"; + +const json = (value: T): T => JSON.parse(JSON.stringify(value)); +function finish(players: number, scenario: number | "random", difficulty: number, seed: string): State { + let state = initGame(players, { scenario, difficulty }, seed); + for (let moves = 0; !state.outcome && moves < 1000; moves++) { + state = wrapper.moveAI(state, [wrapper.currentPlayer(state)].flat()[0]!); + } + assert.equal(state.phase, "ended"); + assert.deepEqual(replay(state), state); + return json(state); +} +const win = finish(2, 1, 1, "achievement-0"); +const ids = (state: State) => wrapper.achievements(state)[0]!.earned; + +test("revision 1 manifest and optional hooks use the official published protocol", () => { + const manifest = wrapper.achievementDefinitions(); + achievementManifestSchema.parse(manifest); + assert.equal(manifest.revision, 1); + assert.equal(manifest.achievements.length, 16); + assert.equal(new Set(manifest.achievements.map((a) => a.id)).size, 16); + for (const { id } of manifest.achievements) assert.match(id, /^[a-z0-9]+(?:-[a-z0-9]+)*$/); + assert(manifest.achievements.every((a) => a.id.length <= 64)); + const inspection = inspectEngine(wrapper); + assert(inspection.compatible); + assert(!inspection.optional.missing.includes("achievements")); + assert(!inspection.optional.missing.includes("achievementDefinitions")); + manifest.achievements[0]!.name = "mutated"; + assert.equal(wrapper.achievementDefinitions().achievements[0]!.name, "Escape Together"); +}); + +test("every feat is attainable in legal completed, replayable expeditions; all seats share the win", () => { + const earned = new Set(); + for (const [players, scenario, difficulty, seed] of [ + [2, 1, 1, 0], + [2, 3, 1, 0], + [2, 5, 1, 0], + [2, 6, 1, 0], + [3, 1, 2, 1], + [3, 2, 2, 1], + [3, 4, 2, 1], + [3, 6, 2, 1], + [4, 1, 3, 2], + [2, 2, 4, 3], + [2, 7, 3, 18], + ] as const) { + const state = finish(players, scenario, difficulty, `achievement-${seed}`); + assert.equal(state.outcome, "won"); + const before = json(state); + const result = wrapper.achievements(state); + achievementResultsSchema(wrapper.achievementDefinitions(), players).parse(result); + assert.deepEqual(result, wrapper.achievements(json(state))); + assert.deepEqual(state, before, "evaluation leaves input and RNG unchanged"); + assert.equal(result.length, players); + for (const [seat, row] of result.entries()) { + assert.equal(row.seat, seat); + assert.equal(row.outcome, "win"); + assert.deepEqual(row.earned, result[0]!.earned); + row.earned.forEach((id) => earned.add(id)); + assert(row.earned.includes(`scenario-${scenario}-escape`)); + assert.equal(row.earned.filter((id) => id.startsWith("scenario-")).length, 1); + for (let level = 2; level <= 4; level++) + assert.equal(row.earned.includes(`difficulty-${level}-escape`), difficulty >= level); + } + result[0]!.earned.push("mutated"); + assert(!result[1]!.earned.includes("mutated")); + } + assert.deepEqual( + [...earned].sort(), + wrapper + .achievementDefinitions() + .achievements.map((a) => a.id) + .sort() + ); +}); + +test("loss is cooperative even with tied ranks; incomplete and excluded sessions earn nothing", () => { + const loss = finish(2, 2, 1, "achievement-0"); + assert.equal(loss.outcome, "lost"); + assert.deepEqual(wrapper.rankings(loss), [1, 1]); + assert.deepEqual( + wrapper.achievements(loss), + [0, 1].map((seat) => ({ seat, outcome: "loss", earned: [] })) + ); + assert.deepEqual(wrapper.achievements(initGame()), []); + for (const flag of ["tutorial", "practice", "analysis"]) { + const state = json(win); + state.initOptions[flag] = true; + assert.deepEqual(wrapper.achievements(state), []); + } + assert.deepEqual(wrapper.achievements(wrapper.createAnalysis(win, { to: win.log.length, sourceEnded: true })), []); + assert.deepEqual(wrapper.achievements(wrapper.createAnalysisScenario(win, { seed: "analysis" })), []); + const dropped = wrapper.dropPlayer(initGame(), 0); + assert.deepEqual( + wrapper.achievements(dropped), + [0, 1, 2].map((seat) => ({ seat, outcome: "loss", earned: [] })) + ); + assert.deepEqual(wrapper.achievements({ ...win, outcome: null }), []); +}); + +test("random scenarios award only the resolved scenario", () => { + const state = finish(2, "random", 1, "achievement-0"); + assert.equal(state.outcome, "won"); + assert.equal(state.initOptions.scenario, "random"); + assert.deepEqual( + ids(state).filter((id) => id.startsWith("scenario-")), + [`scenario-${state.scenario}-escape`] + ); +}); + +test("isolated predicate boundaries use remaining stamina, pending injuries and held equipment", () => { + // These are predicate probes, not presented as legal completed-game fixtures. + const state = json(win); + state.players.forEach((p) => { + p.stamina = 14; + p.injuries = []; + p.pendingInjuries = 0; + }); + assert(ids(state).includes("strength-to-spare")); + state.players[0]!.stamina = 15; + assert(!ids(state).includes("strength-to-spare")); + state.players[0]!.stamina = 19; + assert(!ids(state).includes("narrow-escape")); + state.players[0]!.stamina = 20; + assert(ids(state).includes("narrow-escape")); + assert(ids(state).includes("unscathed-team")); + state.players[0]!.pendingInjuries = 1; + assert(!ids(state).includes("unscathed-team")); + state.players[0]!.pendingInjuries = 0; + state.players[0]!.injuries = ["amnesia"]; + assert(!ids(state).includes("unscathed-team")); + state.players[0]!.cards = []; + assert(!ids(state).includes("well-equipped")); + state.scenario = undefined; + assert(!ids(state).some((id) => id.startsWith("scenario-")), "legacy unknown scenario is not invented"); + state.phase = "planning"; + assert.deepEqual(wrapper.achievements(state), []); +}); diff --git a/packages/engine/src/achievements.ts b/packages/engine/src/achievements.ts new file mode 100644 index 0000000..ded87fe --- /dev/null +++ b/packages/engine/src/achievements.ts @@ -0,0 +1,71 @@ +import type { AchievementDefinition, AchievementManifest, SeatAchievements } from "@boardgamers/protocol/engine"; +import { EXHAUSTION } from "./data.js"; +import type { State } from "./types.js"; + +type Feat = AchievementDefinition & { qualifies: (state: State) => boolean }; +const feats: Feat[] = [ + { id: "escape-together", name: "Escape Together", description: "Win an expedition together.", qualifies: () => true }, + ...Array.from( + { length: 7 }, + (_, i): Feat => ({ + id: `scenario-${i + 1}-escape`, + name: `Scenario ${i + 1} Escape`, + description: `Win scenario ${i + 1} at any difficulty.`, + qualifies: (s) => s.scenario === i + 1, + }) + ), + ...Array.from( + { length: 3 }, + (_, i): Feat => ({ + id: `difficulty-${i + 2}-escape`, + name: `Difficulty ${i + 2} Escape`, + description: `Win at difficulty ${i + 2}${i < 2 ? " or higher" : ""}.`, + qualifies: (s) => s.difficulty >= i + 2, + }) + ), + { + id: "strength-to-spare", + name: "Strength to Spare", + description: "Win with every explorer having at least 11 stamina remaining.", + qualifies: (s) => s.players.every((p) => EXHAUSTION - p.stamina >= 11), + }, + { + id: "unscathed-team", + name: "Unscathed Team", + description: "Win with no explorer having an injury or an injury awaiting selection.", + qualifies: (s) => s.players.every((p) => p.injuries.length === 0 && p.pendingInjuries === 0), + }, + { + id: "narrow-escape", + name: "Narrow Escape", + description: "Win with at least one explorer having 5 or less stamina remaining.", + qualifies: (s) => s.players.some((p) => EXHAUSTION - p.stamina <= 5), + }, + { + id: "well-equipped", + name: "Well Equipped", + description: "Win with every explorer still carrying at least one equipment card.", + qualifies: (s) => s.players.every((p) => p.cards.length > 0), + }, + { + id: "four-to-safety", + name: "Four to Safety", + description: "Win as a team of four explorers.", + qualifies: (s) => s.players.length === 4, + }, +]; + +export function achievementDefinitions(): AchievementManifest { + return { revision: 1, achievements: feats.map(({ qualifies: _, ...definition }) => ({ ...definition })) }; +} + +/** Only authoritative final state is inspected; never infer feats from private rolls or log prose. + * BGS owns account eligibility (humans, bots, cancellations and dropped seats). + */ +export function achievements(s: State): SeatAchievements[] { + if (s.phase !== "ended" || (s.outcome !== "won" && s.outcome !== "lost")) return []; + if (["tutorial", "practice", "analysis"].some((flag) => s.initOptions[flag] === true)) return []; + const outcome = s.outcome === "won" ? "win" : "loss"; + const earned = outcome === "win" ? feats.filter((feat) => feat.qualifies(s)).map((feat) => feat.id) : []; + return s.players.map((_, seat) => ({ seat, earned: [...earned], outcome })); +} diff --git a/packages/engine/src/game.ts b/packages/engine/src/game.ts index 7f77ffd..51c9310 100644 --- a/packages/engine/src/game.ts +++ b/packages/engine/src/game.ts @@ -1245,7 +1245,7 @@ export function rewind(s: State, logTo: number): State { /** Start a manual sandbox from only the requesting seat's visible information. */ export function createAnalysisScenario(data: State, { player, seed }: { player?: number; seed: string }): State { const { deckCount, ...visible } = stripSecret(data, player); - const s: State = { ...visible, seed, counter: 0, deck: [], history: [], initOptions: {} }; + const s: State = { ...visible, seed, counter: 0, deck: [], history: [], initOptions: { analysis: true } }; const used = new Set([...s.discard, ...s.players.flatMap((p) => p.cards.map((c) => c.id))]); s.deck = shuffle( s, diff --git a/packages/engine/wrapper.ts b/packages/engine/wrapper.ts index 1dbcf93..2579125 100644 --- a/packages/engine/wrapper.ts +++ b/packages/engine/wrapper.ts @@ -11,6 +11,7 @@ import { } from "./src/game.js"; import type { State } from "./src/types.js"; export { stripSecret }; +export { achievementDefinitions, achievements } from "./src/achievements.js"; export { moveAI } from "./src/ai.js"; export const hashSeed = true; // State sharing and time awards are independent. @@ -62,6 +63,7 @@ export function createAnalysis(s: State, { to }: { to: number; sourceEnded: bool copy.players.forEach((player, seat) => { player.name = s.players[seat]!.name; }); + copy.initOptions.analysis = true; delete copy.chatMessages; return copy; } diff --git a/packages/viewer/package.json b/packages/viewer/package.json index 3e6d5c1..0c0131b 100644 --- a/packages/viewer/package.json +++ b/packages/viewer/package.json @@ -11,7 +11,7 @@ "test:tutorial": "node --test src/tutorial/*.spec.ts" }, "dependencies": { - "@boardgamers/protocol": "0.10.0", + "@boardgamers/protocol": "0.11.0", "fuji-engine": "workspace:*" }, "devDependencies": { diff --git a/packages/viewer/src/tutorial/first-steps.ts b/packages/viewer/src/tutorial/first-steps.ts index b02215c..ed33dfa 100644 --- a/packages/viewer/src/tutorial/first-steps.ts +++ b/packages/viewer/src/tutorial/first-steps.ts @@ -19,6 +19,7 @@ export function firstStepsState(): State { let state = initGame( 3, { + tutorial: true, skillAssignment: "fixed", skills: ["buddy", "scout", "manager"], difficulty: 1, diff --git a/packages/viewer/src/tutorial/lessons.spec.ts b/packages/viewer/src/tutorial/lessons.spec.ts index 532281d..a33072a 100644 --- a/packages/viewer/src/tutorial/lessons.spec.ts +++ b/packages/viewer/src/tutorial/lessons.spec.ts @@ -1,6 +1,7 @@ import { test } from "node:test"; import assert from "node:assert/strict"; import { createTutorial } from "@boardgamers/protocol/tutorial"; +import { achievements } from "fuji-engine/wrapper"; import { applyMove, cell, paths, stripSecret, terrain, threatened } from "fuji-engine"; import { equipmentLesson, @@ -126,6 +127,8 @@ test("all three explorers reach the village and win before another eruption", as assert.equal(c.snapshot.state.phase, "equipment"); assert.equal(await c.play({ action: "ready" }), true, c.snapshot.error); assert.equal(c.snapshot.state.outcome, "won"); + assert.equal(c.snapshot.state.initOptions.tutorial, true); + assert.deepEqual(achievements(c.snapshot.state), []); assert.ok( c.snapshot.state.players.every((p) => terrain(cell(c.snapshot.state, p.position).terrain).kind === "village") ); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 0249b05..34064fc 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -17,6 +17,9 @@ importers: packages/engine: dependencies: + '@boardgamers/protocol': + specifier: 0.11.0 + version: 0.11.0 seedrandom: specifier: 3.0.5 version: 3.0.5 @@ -34,8 +37,8 @@ importers: packages/viewer: dependencies: '@boardgamers/protocol': - specifier: 0.10.0 - version: 0.10.0 + specifier: 0.11.0 + version: 0.11.0 fuji-engine: specifier: workspace:* version: link:../engine @@ -61,8 +64,8 @@ importers: 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==} @@ -631,7 +634,7 @@ packages: snapshots: - '@boardgamers/protocol@0.10.0': + '@boardgamers/protocol@0.11.0': dependencies: zod: 4.6.2 diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml index 45b07f9..6fadaaf 100644 --- a/pnpm-workspace.yaml +++ b/pnpm-workspace.yaml @@ -1,4 +1,4 @@ packages: - packages/* minimumReleaseAgeExclude: - - "@boardgamers/protocol@0.2.2 || 0.3.0 || 0.3.1 || 0.3.3 || 0.4.0 || 0.4.2 || 0.5.0 || 0.6.0 || 0.6.1 || 0.10.0" + - "@boardgamers/protocol@0.2.2 || 0.3.0 || 0.3.1 || 0.3.3 || 0.4.0 || 0.4.2 || 0.5.0 || 0.6.0 || 0.6.1 || 0.10.0 || 0.11.0"