v0.7.0 — four game types, a lobby you can read and leave, and a multiplayer game that makes a sound
The multiplayer set-up, the lobby, the start of a game, and four signals a remote client had never been sent. Reasoning, the preset table and what was verified how: CHANGELOG.md. - Co-op, Competitive, Cutthroat, Solitaire and Custom, on both screens, from one shared block — they had drifted, and each was missing a question the other asked. - A player reads the whole rule set before taking a seat, may leave a lobby or a running game, and keeps a seat across a reload. The host may clear a chair. The browser remembers every game it is in, not just the last one. - The start of a game is drawn: a handoff beat, an announcement, the code and type in the header. - Sound, the timetable flash, announcements and the just-drawn badge now reach a remote client; justDrawn goes to the seat that drew it and nobody else. - Played on StartOS, which found the rest: an Extra belongs to the player who played it, the board never named the Superintendent, bot seats were reported as absent players, and rule section numbers are out of every string a player reads. Also carries the previous session's Heavy Grade documentation work — asked again, answer unchanged. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016JczK5i33ZNSf2PtzZqdhS
This commit is contained in:
co-authored by
Claude Opus 5
parent
42adfda390
commit
06db36e5b5
+129
-3
@@ -41,6 +41,7 @@ import { createSession } from './session.ts';
|
||||
import type { GameSession, Push } from './session.ts';
|
||||
import {
|
||||
createLobby,
|
||||
leaveLobby,
|
||||
freshGameCode,
|
||||
playerCountAllowed,
|
||||
joinLobby,
|
||||
@@ -129,6 +130,15 @@ async function serveStatic(distDir: string, urlPath: string, res: ServerResponse
|
||||
* simply ending, indistinguishable from a network hiccup that `EventSource` would otherwise retry.
|
||||
*/
|
||||
type LobbyPush = { lobby: Lobby; you: PlayerIndex; started: boolean };
|
||||
/** What `/api/lobby/preview` answers with — everything a player weighing a join needs, and nothing
|
||||
* that would spoil the game. THE SEED IS NOT IN IT: it decides every shuffle and every roll. */
|
||||
type LobbyPreview = {
|
||||
gameCode: string;
|
||||
hostName: string;
|
||||
config: GameConfig;
|
||||
players: number;
|
||||
seated: { seat: number; who: string | null; bot: boolean }[];
|
||||
};
|
||||
|
||||
export function startServer(opts: ServerOptions): void {
|
||||
const games = opts.initialGames;
|
||||
@@ -143,6 +153,8 @@ export function startServer(opts: ServerOptions): void {
|
||||
const gameConnections = new Map<string, Map<PlayerIndex, ServerResponse>>();
|
||||
const gameEventIds = new Map<string, Map<PlayerIndex, number>>();
|
||||
const lobbyConnections = new Map<string, Map<string, ServerResponse>>();
|
||||
/** Which seats of a game have ever held a connection in THIS process — see `presenceOfOthers`. */
|
||||
const everConnected = new Map<string, Set<PlayerIndex>>();
|
||||
|
||||
function writeSse(res: ServerResponse, id: number, data: unknown): void {
|
||||
res.write(`id: ${id}\ndata: ${JSON.stringify(data)}\n\n`);
|
||||
@@ -176,11 +188,37 @@ export function startServer(opts: ServerOptions): void {
|
||||
if (!conns) return;
|
||||
for (const [other, res] of conns) {
|
||||
if (other === seat) continue;
|
||||
const push: Push = { menu: null, lines: [], presence: { seat, connected } };
|
||||
const push: Push = { menu: null, lines: [], presence: [{ seat, connected, seen: true }] };
|
||||
writeSse(res, nextEventId(gameId, other), push);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* EVERY OTHER SEAT'S STATE, for a client that has just connected.
|
||||
*
|
||||
* `broadcastPresence` only ever reports a CHANGE, so a player arriving at a table where two people
|
||||
* had not opened the game yet was told nothing about them at all — and "is everyone here?" is the
|
||||
* question at the moment a game starts. `seen` separates a seat that was here and dropped from one
|
||||
* that has never connected; it is remembered only for as long as this process runs, so after a
|
||||
* restart every absent seat reads as "not here yet", which is the more cautious of the two.
|
||||
*/
|
||||
function presenceOfOthers(
|
||||
gameId: string,
|
||||
seat: PlayerIndex,
|
||||
session: GameSession,
|
||||
): NonNullable<Push['presence']> {
|
||||
const conns = gameConnections.get(gameId);
|
||||
const ever = everConnected.get(gameId) ?? new Set<PlayerIndex>();
|
||||
const out: NonNullable<Push['presence']> = [];
|
||||
for (let other = 0 as PlayerIndex; other < session.playerCount; other++) {
|
||||
// A bot holds no connection and never will, so reporting it would put "waiting on Bot 1 — not
|
||||
// here yet" on every screen for the whole game. Found by playing a real 3-seat game.
|
||||
if (other === seat || session.isBot(other)) continue;
|
||||
out.push({ seat: other, connected: conns?.has(other) === true, seen: ever.has(other) });
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
function broadcastLobby(gameId: string): void {
|
||||
const lobby = lobbies.get(gameId);
|
||||
const conns = lobbyConnections.get(gameId);
|
||||
@@ -389,7 +427,12 @@ export function startServer(opts: ServerOptions): void {
|
||||
await persistLobby(result.lobby);
|
||||
await persistSession(result.session);
|
||||
broadcastLobby(lobby.gameId);
|
||||
sendJson(res, 200, { gameId: lobby.gameId, token: result.session.token, player: result.session.player });
|
||||
sendJson(res, 200, {
|
||||
gameId: lobby.gameId,
|
||||
gameCode: lobby.gameCode,
|
||||
token: result.session.token,
|
||||
player: result.session.player,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -418,6 +461,83 @@ export function startServer(opts: ServerOptions): void {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* GIVING UP A SEAT — the player's own, or (host only) somebody else's.
|
||||
*
|
||||
* There was no door out of a lobby before this: a mis-join or a player who wandered off left a
|
||||
* chair that could not be freed, and a table that cannot start until every chair is taken.
|
||||
* The host's "remove" and a player's "Leave" are the same act from opposite ends, so they are
|
||||
* one route — `seat` names somebody else's chair and is refused to anyone but the host.
|
||||
*/
|
||||
if (url.pathname === '/api/lobby/leave' && req.method === 'POST') {
|
||||
const body = (await readJson(req)) as { token?: string; seat?: number };
|
||||
const ps = typeof body.token === 'string' ? sessions.get(body.token) : undefined;
|
||||
const lobby = ps ? lobbies.get(ps.gameId) : undefined;
|
||||
if (!ps || !lobby) {
|
||||
sendJson(res, 404, { error: 'no such lobby' });
|
||||
return;
|
||||
}
|
||||
const seat = typeof body.seat === 'number' ? (body.seat as PlayerIndex) : undefined;
|
||||
if (seat !== undefined && seat !== ps.player && lobby.hostToken !== ps.token) {
|
||||
sendJson(res, 403, { error: 'NOT_HOST' });
|
||||
return;
|
||||
}
|
||||
const result = leaveLobby(lobby, ps.token, seat);
|
||||
if (result.empty) {
|
||||
// Nobody human is left to start it. Everything about this lobby goes, including the code,
|
||||
// so it cannot be joined into a game that will never begin.
|
||||
lobbies.delete(lobby.gameId);
|
||||
gameCodes.delete(lobby.gameCode);
|
||||
for (const [, watcher] of lobbyConnections.get(lobby.gameId) ?? []) watcher.end();
|
||||
lobbyConnections.delete(lobby.gameId);
|
||||
await deleteLobby(opts.dataDir, lobby.gameId);
|
||||
// The row goes with the lobby rather than being marked: a game that never started is not a
|
||||
// game an administrator has any use for a record of.
|
||||
await removeIndexEntry(opts.dataDir, lobby.gameId);
|
||||
sendJson(res, 200, { ok: true, closed: true });
|
||||
return;
|
||||
}
|
||||
await persistLobby(result.lobby);
|
||||
broadcastLobby(lobby.gameId);
|
||||
sendJson(res, 200, { ok: true });
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* WHAT AM I ABOUT TO JOIN? Read-only, takes no seat, and gated by the same join secret the
|
||||
* door itself is.
|
||||
*
|
||||
* A player used to have to take a chair before they could see a single rule of the game they
|
||||
* were sitting down to — and until 2026-08-23 there was then no way back out of it.
|
||||
*/
|
||||
if (url.pathname === '/api/lobby/preview' && req.method === 'GET') {
|
||||
if (url.searchParams.get('secret') !== opts.joinSecret) {
|
||||
sendJson(res, 403, { error: 'bad or missing secret' });
|
||||
return;
|
||||
}
|
||||
const code = (url.searchParams.get('gameCode') ?? '').trim().toUpperCase();
|
||||
const gameId = gameCodes.get(code);
|
||||
const lobby = gameId ? lobbies.get(gameId) : undefined;
|
||||
if (!lobby) {
|
||||
sendJson(res, 404, { error: 'no open lobby with that code' });
|
||||
return;
|
||||
}
|
||||
const hostSeat = lobby.seats.find((seat) => seat?.kind === 'human' && seat.token === lobby.hostToken);
|
||||
const preview: LobbyPreview = {
|
||||
gameCode: lobby.gameCode,
|
||||
hostName: hostSeat?.kind === 'human' ? hostSeat.displayName : 'unknown',
|
||||
config: lobby.config,
|
||||
players: lobby.seats.length,
|
||||
seated: lobby.seats.map((seat, i) => ({
|
||||
seat: i,
|
||||
who: seat?.kind === 'human' ? seat.displayName : null,
|
||||
bot: seat?.kind === 'bot',
|
||||
})),
|
||||
};
|
||||
sendJson(res, 200, preview);
|
||||
return;
|
||||
}
|
||||
|
||||
if (url.pathname === '/api/lobby/start' && req.method === 'POST') {
|
||||
const body = (await readJson(req)) as { token?: string };
|
||||
const ps = typeof body.token === 'string' ? sessions.get(body.token) : undefined;
|
||||
@@ -523,7 +643,13 @@ export function startServer(opts: ServerOptions): void {
|
||||
const conns = gameConnections.get(gameId) ?? new Map<PlayerIndex, ServerResponse>();
|
||||
conns.set(seat, res);
|
||||
gameConnections.set(gameId, conns);
|
||||
writeSse(res, nextEventId(gameId, seat), session.connect(seat));
|
||||
const ever = everConnected.get(gameId) ?? new Set<PlayerIndex>();
|
||||
ever.add(seat);
|
||||
everConnected.set(gameId, ever);
|
||||
// The board, and who else is at the table — the second half used to be missing entirely.
|
||||
const first = session.connect(seat);
|
||||
first.presence = presenceOfOthers(gameId, seat, session);
|
||||
writeSse(res, nextEventId(gameId, seat), first);
|
||||
broadcastPresence(gameId, seat, true);
|
||||
// Idle for minutes at a time is the expected shape of this game (multiplayer.md §9) — a
|
||||
// silent SSE connection is exactly what a proxy in the path may reap. A comment line is not a
|
||||
|
||||
Reference in New Issue
Block a user