Rebuild the board and reducer around the real content

shared/board.js: rebuilt from shared/tileInventory.js instead of the
sketch-inspired placeholder — one chain() per segment (same helper as
before), TEMP_SEGMENT_ORDER concatenating all 12 segments in the design
doc's own order since board-graph.json (the real segment connections/STOP
forks) doesn't exist yet — loudly commented as a placeholder to replace, not
a guess at real topology. A synthetic `finish` space is appended since no
tile in the source data has that type (win condition is itself an open
question). Movement is now one tile per turn (board.spaces[pos].next) —
no tile in the data implies a movement die, every `die` belongs to that
tile's own effect resolution, so walkForward() is gone.

shared/tileEffects.js (new): pure resolveTileEffect(space, rolls) covering
all 9 real tile types (payday, action_space, dice_space, roll_table_ref,
inline_table, cash_bonus, event, choice, stop) plus rollsNeededFor(space) so
the server knows what to pre-roll before constructing an action. Real
roll_table_ref/action_space/dice_space tiles resolve against the actual 48
placeholder tables; the two real content gaps (inline_table's 85 tiles,
payday/cash_bonus amounts) resolve against a synthesized generic banded
table per die size, every result clearly flagged `todo: true` so it's
visible in the game log, not just in docs.

shared/game.js: player state is now flat {cash, love, education, wealth,
age} fields (mirrors a roll-table effect object's shape directly). Landing
logic calls resolveTileEffect() instead of applying a single cash delta, and
only pauses for a decision when a space's `choices` array is actually
populated (true of nothing yet) — keeps the existing pending-choice
machinery intact for once board-graph.json exists, instead of needing
choice/stop tiles to block on options nobody has defined.

server/rooms.js: REQUEST_ROLL now looks up the player's next tile,
determines what it needs via rollsNeededFor(), and pre-rolls each die via
crypto.randomInt before constructing the ROLL action — same
randomness-happens-once-at-the-server-boundary pattern as before, just
covering N table rolls instead of one movement die. server/index.js:
default config now carries startingStats instead of startingCash, and
maxPlayers is 8.
This commit is contained in:
2026-07-28 10:06:59 -07:00
parent 220f70d342
commit 4d78937dca
5 changed files with 339 additions and 219 deletions
+43 -22
View File
@@ -2,17 +2,27 @@
* Life Journey — pure game reducer.
*
* reduce(state, action) -> newState is the ONLY way state changes, and it never
* generates randomness itself: the die value is generated by the server and
* travels inside the ROLL action payload, so server and client can both apply
* the exact same action through this exact same function and land on identical
* state. Illegal transitions throw rather than no-op — the server is expected
* to gate intents with getLegalIntents()/isPlayersTurn() before ever
* constructing an action, so a throw here means that gate was bypassed.
* generates randomness itself: every roll a tile needs is generated by the
* server and travels inside the action payload (`rolls`), so server and
* client can both apply the exact same action through this exact same
* function and land on identical state. Illegal transitions throw rather
* than no-op — the server is expected to gate intents with
* getLegalIntents()/isPlayersTurn() before ever constructing an action, so a
* throw here means that gate was bypassed.
*
* Movement is one tile per turn (board.js's `next` pointer) — there is no
* movement die; every `die` a tile carries is for resolving that tile's own
* effect via shared/tileEffects.js, not how far a player travels.
*/
import { board, walkForward } from './board.js';
import { board } from './board.js';
import { resolveTileEffect } from './tileEffects.js';
const DEFAULT_CONFIG = { minPlayers: 2, maxPlayers: 6, diceSides: 6, startingCash: 0 };
const DEFAULT_CONFIG = {
minPlayers: 2,
maxPlayers: 8,
startingStats: { cash: 0, love: 0, education: 0, wealth: 0, age: 18 },
};
const LOG_LIMIT = 50;
export function createInitialState(config = {}) {
@@ -90,7 +100,7 @@ function applyJoin(state, { playerId, name, seat, color }) {
seat,
color,
position: board.startSpaceId,
cash: state.config.startingCash,
...state.config.startingStats,
pendingChoice: null,
finished: false,
};
@@ -107,37 +117,38 @@ function applyStart(state) {
return { ...state, status: 'active', turnOrder, turnIndex: 0, currentTurn: turnOrder[0] };
}
function applyRoll(state, { playerId, value }) {
function applyRoll(state, { playerId, rolls }) {
assertActive(state);
assertPlayersTurn(state, playerId);
const player = state.players[playerId];
if (player.pendingChoice) throw new Error('Resolve pending choice before rolling');
const landed = walkForward(player.position, value);
return landOn(state, playerId, landed, { type: 'ROLL', value });
const nextSpaceId = board.spaces[player.position].next;
if (!nextSpaceId) throw new Error('No next space defined — already at the end of the board');
return landOn(state, playerId, nextSpaceId, rolls, { type: 'ROLL' });
}
function applyChoose(state, { playerId, spaceId }) {
function applyChoose(state, { playerId, spaceId, rolls }) {
assertActive(state);
assertPlayersTurn(state, playerId);
const player = state.players[playerId];
const pending = player.pendingChoice;
if (!pending) throw new Error('No pending choice');
if (!pending.options.includes(spaceId)) throw new Error('Illegal choice');
return landOn(state, playerId, spaceId, { type: 'CHOOSE' });
return landOn(state, playerId, spaceId, rolls, { type: 'CHOOSE' });
}
/** Shared landing logic for both a ROLL's terminal space and a CHOOSE's
* resolved branch: apply the space's effect, then either leave the turn
* open (choice pending), end the game (finish), or advance to the next player. */
function landOn(state, playerId, spaceId, logMeta) {
* resolved branch: resolve the space's tile effect, then either leave the
* turn open (a real choice is pending — only true once board-graph.json
* populates `choices`), end the game (finish), or advance to the next player. */
function landOn(state, playerId, spaceId, rolls, logMeta) {
const space = board.spaces[spaceId];
const cashDelta = space.cash ?? 0;
const { statDelta, description, todo } = resolveTileEffect(space, rolls ?? []);
const priorPlayer = state.players[playerId];
const nextPlayer = {
...priorPlayer,
...applyStatDelta(priorPlayer, statDelta),
position: spaceId,
cash: priorPlayer.cash + cashDelta,
pendingChoice: space.type === 'choice' ? { atSpace: spaceId, options: space.choices } : null,
pendingChoice: space.choices?.length ? { atSpace: spaceId, options: space.choices } : null,
finished: space.type === 'finish',
};
@@ -146,7 +157,9 @@ function landOn(state, playerId, spaceId, logMeta) {
playerId,
landedOn: spaceId,
label: space.label,
cashDelta,
statDelta,
description,
todo,
...logMeta,
});
@@ -162,6 +175,14 @@ function landOn(state, playerId, spaceId, logMeta) {
return advanceTurn(newState);
}
function applyStatDelta(player, statDelta) {
const next = { ...player };
for (const [stat, delta] of Object.entries(statDelta ?? {})) {
next[stat] = (next[stat] ?? 0) + delta;
}
return next;
}
function advanceTurn(state) {
const turnIndex = (state.turnIndex + 1) % state.turnOrder.length;
return { ...state, turnIndex, currentTurn: state.turnOrder[turnIndex] };