331 lines
12 KiB
JavaScript
331 lines
12 KiB
JavaScript
import test from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import { createTutorial } from "@boardgamers/protocol/tutorial";
|
|
import { chapters } from "../viewer/tutorials.js";
|
|
import { availableMoves, majority, move } from "../engine/index.js";
|
|
|
|
test("the full-round lesson forces a changed order and lets the player choose a lost stack", async () => {
|
|
for (const firstDiscard of ["build", "draw"]) {
|
|
const chapter = chapters["full-round"];
|
|
const lesson = await createTutorial(chapter);
|
|
const initialMoney = lesson.snapshot.state.players[0].money;
|
|
const actions = [];
|
|
while (!lesson.snapshot.completed) {
|
|
const { state, step, canContinue } = lesson.snapshot;
|
|
const current = chapter.steps[step];
|
|
const legal = availableMoves(state, 0);
|
|
if (current.id === "blocked") {
|
|
assert.equal(state.round, 1);
|
|
assert.equal(majority(state, 0, "fish"), false);
|
|
assert.equal(state.players[1].markers.fish, 2);
|
|
assert.deepEqual(
|
|
legal.filter((m) => m.type === "act").map((m) => m.action),
|
|
["rest"],
|
|
);
|
|
assert.equal(
|
|
state.players.every(
|
|
(p) => Object.values(p.markers).reduce((a, b) => a + b) === 5,
|
|
),
|
|
true,
|
|
);
|
|
}
|
|
if (current.id === "fish") {
|
|
assert.equal(state.players[1].markers.fish, 0);
|
|
assert.equal(majority(state, 0, "fish"), true);
|
|
assert.equal(state.board["0,0"].huts.length, 3);
|
|
}
|
|
if (current.id === "sell") {
|
|
assert.equal(state.market, 2);
|
|
assert.equal(state.board["0,0"].drawings, 1);
|
|
}
|
|
if (current.id === "retrieve") {
|
|
assert.equal(state.players[0].money, initialMoney + 2);
|
|
assert.equal(state.players[0].score, 0, "Rest has not paid yet");
|
|
assert.deepEqual(
|
|
legal.map((m) => m.type),
|
|
["discard", "discard"],
|
|
);
|
|
assert.deepEqual(
|
|
new Set(legal.map((m) => m.action)),
|
|
new Set(["build", "draw"]),
|
|
);
|
|
}
|
|
if (current.id === "last-stack") {
|
|
assert.equal(state.round, 1);
|
|
assert.equal(
|
|
state.players[0].score,
|
|
0,
|
|
"Rest waits for manual retrieval",
|
|
);
|
|
assert.equal(legal.length, 1);
|
|
assert.equal(legal[0].type, "discard");
|
|
assert.notEqual(legal[0].action, firstDiscard);
|
|
}
|
|
if (canContinue) await lesson.continue();
|
|
else {
|
|
const chosen = legal.find(
|
|
(m) =>
|
|
!current.validateMove?.(state, m) &&
|
|
(current.id !== "retrieve" || m.action === firstDiscard),
|
|
);
|
|
assert.ok(chosen, current.id);
|
|
if (chosen.type === "act") actions.push(chosen.action);
|
|
assert.equal(await lesson.play(chosen), true, current.id);
|
|
}
|
|
}
|
|
const state = lesson.snapshot.state;
|
|
assert.deepEqual(actions, ["rest", "fish", "sell"]);
|
|
assert.equal(state.round, 2);
|
|
assert.equal(
|
|
state.phase,
|
|
"expand",
|
|
"Stop before opponents start the next round",
|
|
);
|
|
assert.equal(state.first, 1);
|
|
assert.equal(state.players[0].money, initialMoney + 3);
|
|
assert.equal(state.players[0].score, 1);
|
|
assert.equal(
|
|
state.players.every((p) =>
|
|
Object.values(p.markers).every((n) => n === 0),
|
|
),
|
|
true,
|
|
);
|
|
lesson.destroy();
|
|
}
|
|
});
|
|
|
|
test("planning opponents spread their markers and release a blocked fishing action", () => {
|
|
const chapter = chapters.planning;
|
|
for (const choice of availableMoves(chapter.initialState(), 0)) {
|
|
let state = chapter.move(chapter.initialState(), choice);
|
|
for (const actions of [["sail", "fish"], ["draw", "rest"], ["tourist"]])
|
|
state = chapter.move(state, { type: "plan", actions });
|
|
assert.equal(state.phase, "actions");
|
|
for (const opponent of state.players.slice(1)) {
|
|
const stacks = Object.values(opponent.markers).filter(Boolean);
|
|
assert.equal(
|
|
stacks.reduce((a, b) => a + b, 0),
|
|
5,
|
|
);
|
|
assert.ok(
|
|
stacks.length >= 3,
|
|
`${choice.character}: ${opponent.name} spreads markers`,
|
|
);
|
|
assert.equal(Math.max(...stacks), 2);
|
|
}
|
|
assert.equal(state.players[1].markers.fish, 2);
|
|
assert.equal(majority(state, 0, "fish"), false);
|
|
state = chapter.move(
|
|
state,
|
|
availableMoves(state, 0).find(
|
|
(m) =>
|
|
m.action === "sail" && m.path.length === 1 && m.path[0] === "1,0",
|
|
),
|
|
);
|
|
assert.equal(state.players[1].markers.fish, 0);
|
|
assert.equal(majority(state, 0, "fish"), true);
|
|
const fish = availableMoves(state, 0).find((m) => m.action === "fish");
|
|
assert.ok(fish, `${choice.character}: the player can fish after Maya`);
|
|
state = chapter.move(state, fish);
|
|
assert.deepEqual(state.players[0].fish, [1]);
|
|
let turns = 0;
|
|
while (state.round === 1 && turns++ < 15) {
|
|
const legal = availableMoves(state, 0);
|
|
const next =
|
|
legal.find((m) => m.type === "act") ??
|
|
legal.find((m) => m.type === "rest" || m.type === "discard");
|
|
assert.ok(next, `${choice.character}: the round can continue`);
|
|
state = chapter.move(state, next);
|
|
}
|
|
assert.equal(state.round, 2, `${choice.character}: the round finishes`);
|
|
}
|
|
});
|
|
|
|
for (const [id, chapter] of Object.entries(chapters))
|
|
test(`tutorial ${id} is completable with real engine moves`, async () => {
|
|
const lesson = await createTutorial(chapter);
|
|
let count = 0;
|
|
while (!lesson.snapshot.completed && count++ < 20) {
|
|
const snap = lesson.snapshot;
|
|
if (snap.canContinue) {
|
|
await lesson.continue();
|
|
continue;
|
|
}
|
|
const step = chapter.steps[snap.step];
|
|
const moves = availableMoves(snap.state, 0).filter(
|
|
(m) => !step.validateMove?.(snap.state, m),
|
|
);
|
|
assert.ok(moves.length, `${id}/${snap.step}`);
|
|
assert.equal(await lesson.play(moves[0]), true);
|
|
}
|
|
assert.equal(lesson.snapshot.completed, true);
|
|
lesson.destroy();
|
|
});
|
|
|
|
test("the fishing lesson shows the sale's lower price before the next round resets it", async () => {
|
|
const saved = new Map();
|
|
const options = {
|
|
...chapters.fishing,
|
|
storage: {
|
|
getItem: (key) => saved.get(key) ?? null,
|
|
setItem: (key, value) => saved.set(key, value),
|
|
removeItem: (key) => saved.delete(key),
|
|
},
|
|
};
|
|
const lesson = await createTutorial(options);
|
|
for (const action of ["sail", "fish", "sell"]) {
|
|
while (lesson.snapshot.canContinue) await lesson.continue();
|
|
const state = lesson.snapshot.state;
|
|
const selected = availableMoves(state, 0).find(
|
|
(m) =>
|
|
m.action === action &&
|
|
(action !== "sail" || (m.path.length === 1 && m.path[0] === "1,0")),
|
|
);
|
|
assert.equal(await lesson.play(selected), true);
|
|
assert.equal(lesson.snapshot.state.market, action === "sell" ? 2 : 3);
|
|
assert.equal(lesson.snapshot.state.round, 1);
|
|
}
|
|
assert.equal(lesson.snapshot.completed, true);
|
|
assert.equal(lesson.snapshot.state.players[0].money, 0);
|
|
assert.equal(lesson.snapshot.state.players[0].score, 5);
|
|
assert.equal(lesson.snapshot.state.players[0].used, true);
|
|
assert.equal(lesson.snapshot.state.players[0].markers.sail, 0);
|
|
let next = lesson.snapshot.state;
|
|
next = move(
|
|
next,
|
|
availableMoves(next, 0).find((m) => m.action === "rest"),
|
|
0,
|
|
);
|
|
next = move(
|
|
next,
|
|
availableMoves(next, 0).find((m) => m.type === "rest"),
|
|
0,
|
|
);
|
|
assert.equal(next.round, 2);
|
|
assert.equal(next.market, 3);
|
|
lesson.destroy();
|
|
const resumed = await createTutorial(options);
|
|
assert.equal(resumed.snapshot.completed, true);
|
|
assert.equal(resumed.snapshot.state.market, 2);
|
|
assert.equal(resumed.snapshot.state.round, 1);
|
|
resumed.destroy();
|
|
});
|
|
|
|
test("water lesson demonstrates combined construction, the countdown and an island sinking", async () => {
|
|
const lesson = await createTutorial(chapters["rising-waters"]);
|
|
const seen = new Set();
|
|
while (!lesson.snapshot.completed) {
|
|
const { state, step, canContinue } = lesson.snapshot;
|
|
const current = chapters["rising-waters"].steps[step];
|
|
seen.add(current.id);
|
|
if (current.id === "coasts") {
|
|
assert.equal(state.players[0].money, 0);
|
|
assert.deepEqual(state.board["0,0"].dikes, ["0,1", "1,0"]);
|
|
assert.deepEqual(state.board["0,0"].huts, [0]);
|
|
assert.equal(state.waterCountdown, 2);
|
|
}
|
|
if (current.id === "tourist") assert.equal(state.waterCountdown, 1);
|
|
if (current.id === "flood") {
|
|
assert.equal(state.waterTriggered, true);
|
|
assert.equal(state.board["0,0"].water, 4);
|
|
assert.equal(state.board["0,0"].submerged, false);
|
|
assert.equal(state.board["2,1"].submerged, true);
|
|
assert.deepEqual(state.board["2,1"].huts, []);
|
|
assert.deepEqual(state.board["2,1"].goods, {});
|
|
assert.equal(state.board["2,1"].tourists, 0);
|
|
assert.equal(state.board["2,1"].drawings, 0);
|
|
assert.equal(state.round, 4);
|
|
}
|
|
if (canContinue) await lesson.continue();
|
|
else
|
|
await lesson.play(
|
|
availableMoves(state, 0).find((m) => !current.validateMove?.(state, m)),
|
|
);
|
|
}
|
|
assert.equal(seen.size, chapters["rising-waters"].steps.length);
|
|
lesson.destroy();
|
|
});
|
|
|
|
test("the export lesson explains both the ordinary reward and ship completion bonus", async () => {
|
|
for (const good of ["copra", "beef"]) {
|
|
const lesson = await createTutorial(chapters.trade);
|
|
let count = 0;
|
|
while (!lesson.snapshot.completed && count++ < 12) {
|
|
const { state, step, canContinue } = lesson.snapshot;
|
|
if (canContinue) {
|
|
await lesson.continue();
|
|
continue;
|
|
}
|
|
const current = chapters.trade.steps[step];
|
|
const selected = availableMoves(state, 0).find(
|
|
(m) =>
|
|
!current.validateMove?.(state, m) &&
|
|
(m.action !== "buy" || m.good === good),
|
|
);
|
|
assert.ok(selected, `${good}/${current.id}`);
|
|
assert.equal(await lesson.play(selected), true);
|
|
}
|
|
assert.equal(lesson.snapshot.completed, true);
|
|
const { state, text } = lesson.snapshot;
|
|
assert.equal(state.players[0].score, good === "copra" ? 3 : 7);
|
|
assert.equal(state.players[0].money, good === "copra" ? 1 : 0);
|
|
assert.equal(state.demands[0].filled.every(Boolean), good === "beef");
|
|
assert.equal(state.demands[1].filled.every(Boolean), false);
|
|
assert.match(
|
|
text,
|
|
good === "copra" ? /no completion bonus/ : /7 prosperity/,
|
|
);
|
|
lesson.destroy();
|
|
}
|
|
});
|
|
|
|
test("the Beggar lesson funds Build without negative prosperity or consuming another action", async () => {
|
|
const chapter = chapters.beggar;
|
|
const lesson = await createTutorial(chapter);
|
|
let state = lesson.snapshot.state;
|
|
assert.equal(
|
|
availableMoves(state, 0).some((m) => m.type === "beg"),
|
|
false,
|
|
);
|
|
assert.equal(
|
|
availableMoves(state, 0).some(
|
|
(m) => m.type === "act" && m.action === "build",
|
|
),
|
|
false,
|
|
);
|
|
await lesson.continue();
|
|
assert.equal(
|
|
await lesson.play(
|
|
availableMoves(state, 0).find((m) => m.action === "draw"),
|
|
),
|
|
true,
|
|
);
|
|
state = lesson.snapshot.state;
|
|
assert.equal(state.players[0].score, 3);
|
|
assert.equal(state.players[0].money, 0);
|
|
assert.equal(
|
|
await lesson.play({ type: "beg", amount: 2 }),
|
|
false,
|
|
"The guided route needs the full hut price",
|
|
);
|
|
const markers = { ...state.players[0].markers };
|
|
assert.equal(await lesson.play({ type: "beg", amount: 3 }), true);
|
|
state = lesson.snapshot.state;
|
|
assert.equal(state.actor, 0);
|
|
assert.deepEqual(state.players[0].markers, markers);
|
|
assert.equal(state.players[0].score, 0);
|
|
assert.equal(state.players[0].money, 3);
|
|
assert.equal(
|
|
availableMoves(state, 0).some((m) => m.type === "beg"),
|
|
false,
|
|
);
|
|
const build = availableMoves(state, 0).find(
|
|
(m) => m.type === "act" && m.action === "build",
|
|
);
|
|
assert.ok(build);
|
|
assert.equal(await lesson.play(build), true);
|
|
assert.equal(lesson.snapshot.completed, true);
|
|
assert.equal(lesson.snapshot.state.players[0].money, 0);
|
|
assert.deepEqual(lesson.snapshot.state.board["0,0"].huts, [0]);
|
|
lesson.destroy();
|
|
});
|