castle-web-cli 0.4.156 → 0.4.158
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/agent-failures.d.ts +1 -0
- package/dist/agent-failures.js +6 -4
- package/dist/agent-prompts.js +4 -0
- package/dist/agent.d.ts +11 -0
- package/dist/agent.js +175 -15
- package/dist/bundle.js +8 -2
- package/dist/castle-host/host.js +114 -8
- package/dist/castleJson.js +6 -1
- package/dist/check.d.ts +1 -0
- package/dist/check.js +25 -0
- package/dist/devSessionServer.js +31 -7
- package/dist/imports.js +1 -19
- package/dist/index.js +15 -1
- package/dist/init.d.ts +2 -2
- package/dist/init.js +78 -42
- package/dist/mcpPlaytest.d.ts +1 -0
- package/dist/mcpPlaytest.js +182 -0
- package/dist/metering.d.ts +24 -0
- package/dist/metering.js +87 -0
- package/dist/sdkResolve.d.ts +3 -0
- package/dist/sdkResolve.js +55 -0
- package/dist/serve.js +50 -4
- package/dist/serverBundle.js +8 -3
- package/dist/shell/assets/{index-DsDkYFvP.js → index-BuSYLy-C.js} +64 -64
- package/dist/shell/assets/{index-DwezawBs.css → index-DfZmu3XC.css} +1 -1
- package/dist/shell/index.html +2 -2
- package/dist/vitePlugins.d.ts +1 -0
- package/dist/vitePlugins.js +20 -0
- package/kits/base/CLAUDE.md +196 -0
- package/kits/{blank → base}/castle.json +5 -2
- package/kits/base/editorHost.js +172 -0
- package/kits/{blank → base}/node_modules/.package-lock.json +3 -2
- package/kits/{blank → base}/package-lock.json +7 -4
- package/kits/{blank → base}/package.json +4 -2
- package/kits/base/sdk/README.md +569 -0
- package/kits/base/sdk/castle.d.ts +27 -0
- package/kits/base/sdk/castle.js +16 -0
- package/kits/base/sdk/commands.d.ts +181 -0
- package/kits/base/sdk/commands.js +16 -0
- package/kits/base/sdk/context.d.ts +17 -0
- package/kits/base/sdk/context.js +15 -0
- package/kits/base/sdk/errors.d.ts +22 -0
- package/kits/base/sdk/errors.js +16 -0
- package/kits/base/sdk/files.d.ts +15 -0
- package/kits/base/sdk/files.js +52 -0
- package/kits/base/sdk/haptics.d.ts +6 -0
- package/kits/base/sdk/haptics.js +36 -0
- package/kits/base/sdk/leaderboard.d.ts +26 -0
- package/kits/base/sdk/leaderboard.js +296 -0
- package/kits/base/sdk/lifecycle.d.ts +4 -0
- package/kits/base/sdk/lifecycle.js +13 -0
- package/kits/base/sdk/multiplayer.d.ts +43 -0
- package/kits/base/sdk/multiplayer.js +464 -0
- package/kits/base/sdk/multiplayerProtocol.d.ts +51 -0
- package/kits/base/sdk/multiplayerProtocol.js +5 -0
- package/kits/base/sdk/passes.d.ts +7 -0
- package/kits/base/sdk/passes.js +84 -0
- package/kits/base/sdk/portal.d.ts +7 -0
- package/kits/base/sdk/portal.js +35 -0
- package/kits/base/sdk/runtime.d.ts +62 -0
- package/kits/base/sdk/runtime.js +970 -0
- package/kits/base/sdk/saveQueue.d.ts +27 -0
- package/kits/base/sdk/saveQueue.js +205 -0
- package/kits/base/sdk/server/platformHandle.d.ts +25 -0
- package/kits/base/sdk/server/platformHandle.js +1 -0
- package/kits/base/sdk/server/wrapper.d.ts +28 -0
- package/kits/base/sdk/server/wrapper.js +109 -0
- package/kits/base/sdk/storage.d.ts +16 -0
- package/kits/base/sdk/storage.js +323 -0
- package/kits/base/sdk/time.d.ts +17 -0
- package/kits/base/sdk/time.js +120 -0
- package/kits/base/sdk/transport.d.ts +17 -0
- package/kits/base/sdk/transport.js +141 -0
- package/kits/base/sdk/types.d.ts +3 -0
- package/kits/base/sdk/types.js +1 -0
- package/kits/base/sdk/user.d.ts +10 -0
- package/kits/base/sdk/user.js +45 -0
- package/kits/multiplayer-2d/.prettierrc +5 -0
- package/kits/multiplayer-2d/CLAUDE.md +129 -0
- package/kits/multiplayer-2d/behaviors/Box.jsx +92 -0
- package/kits/multiplayer-2d/behaviors/Player.jsx +86 -0
- package/kits/multiplayer-2d/blueprints/crate.scene +13 -0
- package/kits/multiplayer-2d/blueprints/other-player.scene +13 -0
- package/kits/multiplayer-2d/blueprints/player.scene +15 -0
- package/kits/multiplayer-2d/blueprints/wall.scene +12 -0
- package/kits/multiplayer-2d/castle.json +39 -0
- package/kits/multiplayer-2d/code/client/avatars.js +79 -0
- package/kits/multiplayer-2d/code/client/poses.js +168 -0
- package/kits/multiplayer-2d/code/server/index.js +26 -0
- package/kits/multiplayer-2d/code/server/limits.js +99 -0
- package/kits/multiplayer-2d/code/server/ownership.js +177 -0
- package/kits/multiplayer-2d/code/server/players.js +127 -0
- package/kits/multiplayer-2d/code/server/sceneBodies.js +98 -0
- package/kits/multiplayer-2d/code/server/world.js +337 -0
- package/kits/multiplayer-2d/code/systems/multiplayer.js +424 -0
- package/kits/multiplayer-2d/eslint.config.js +21 -0
- package/kits/multiplayer-2d/fonts.generated.js +9 -0
- package/kits/multiplayer-2d/index.html +25 -0
- package/kits/multiplayer-2d/main.jsx +10 -0
- package/kits/multiplayer-2d/package-lock.json +351 -0
- package/kits/multiplayer-2d/package.json +30 -0
- package/kits/multiplayer-2d/scenes/main.scene +20 -0
- package/kits/multiplayer-2d/test/two-client-test.mjs +341 -0
- package/kits/multiplayer-2d/theme.style +4 -0
- package/kits/multiplayer-3d/.prettierrc +5 -0
- package/kits/multiplayer-3d/CLAUDE.md +215 -0
- package/kits/multiplayer-3d/blueprints/ball.scene +21 -0
- package/kits/multiplayer-3d/blueprints/other-player.scene +21 -0
- package/kits/multiplayer-3d/castle.json +43 -0
- package/kits/multiplayer-3d/code/client/avatars.js +136 -0
- package/kits/multiplayer-3d/code/client/nameTags.js +101 -0
- package/kits/multiplayer-3d/code/client/poses.js +220 -0
- package/kits/multiplayer-3d/code/server/index.js +23 -0
- package/kits/multiplayer-3d/code/server/limits.js +107 -0
- package/kits/multiplayer-3d/code/server/ownership.js +157 -0
- package/kits/multiplayer-3d/code/server/players.js +114 -0
- package/kits/multiplayer-3d/code/server/sceneBodies.js +88 -0
- package/kits/multiplayer-3d/code/server/world.js +306 -0
- package/kits/multiplayer-3d/code/systems/multiplayer.js +427 -0
- package/kits/multiplayer-3d/eslint.config.js +20 -0
- package/kits/multiplayer-3d/fonts.generated.js +9 -0
- package/kits/multiplayer-3d/index.html +22 -0
- package/kits/multiplayer-3d/main.jsx +9 -0
- package/kits/multiplayer-3d/package-lock.json +368 -0
- package/kits/multiplayer-3d/package.json +32 -0
- package/kits/multiplayer-3d/scenes/main.scene +409 -0
- package/kits/multiplayer-3d/theme.style +3 -0
- package/kits/physics-2d/CLAUDE.md +6 -0
- package/kits/physics-2d/castle.json +7 -1
- package/kits/physics-2d/editors/behaviorRegistry.js +5 -0
- package/kits/physics-2d/editors/editorRegistry.jsx +3 -1
- package/kits/physics-2d/engine/files.js +9 -5
- package/kits/physics-2d/engine/systemRegistry.js +2 -0
- package/kits/physics-2d/engine/ui.module.css +11 -0
- package/kits/physics-2d/package-lock.json +8 -2
- package/kits/physics-2d/package.json +2 -2
- package/kits/physics-3d/CLAUDE.md +4 -4
- package/kits/physics-3d/behaviors/Shape.jsx +1 -1
- package/kits/physics-3d/blueprints/crate.scene +1 -1
- package/kits/physics-3d/blueprints/door.scene +4 -4
- package/kits/physics-3d/blueprints/pillar.scene +1 -1
- package/kits/physics-3d/blueprints/platform.scene +1 -1
- package/kits/physics-3d/blueprints/statue.scene +1 -1
- package/kits/physics-3d/blueprints/wall.scene +1 -1
- package/kits/physics-3d/castle.json +8 -11
- package/kits/physics-3d/docs/pxmodel-format.md +1 -1
- package/kits/physics-3d/engine3d/materials.js +3 -3
- package/kits/physics-3d/engine3d/modelFiles.js +4 -2
- package/kits/physics-3d/engine3d/world3d.js +1 -1
- package/kits/physics-3d/fonts.generated.js +9 -0
- package/kits/physics-3d/main.jsx +1 -1
- package/kits/physics-3d/package-lock.json +85 -2
- package/kits/physics-3d/package.json +6 -2
- package/kits/physics-3d/scenes/main.scene +1 -1
- package/kits/physics-3d/scenes/model-lab.scene +1 -1
- package/kits/real-time/.prettierrc +5 -0
- package/kits/real-time/CLAUDE.md +60 -0
- package/kits/real-time/castle.json +19 -0
- package/kits/real-time/code/client/clock.js +68 -0
- package/kits/real-time/code/client/connection.js +134 -0
- package/kits/real-time/code/client/gameHooks.js +50 -0
- package/kits/real-time/code/client/interpolate.js +82 -0
- package/kits/real-time/code/client/messages.js +55 -0
- package/kits/real-time/code/client/ownership.js +94 -0
- package/kits/real-time/code/client/smooth.js +54 -0
- package/kits/real-time/code/server/blockDelta.js +48 -0
- package/kits/real-time/code/server/gameHooks.js +47 -0
- package/kits/real-time/code/server/limits.js +35 -0
- package/kits/real-time/code/server/session.js +251 -0
- package/kits/real-time/eslint.config.js +22 -0
- package/kits/real-time/index.html +45 -0
- package/kits/real-time/package.json +14 -0
- package/kits/{tabletop → turn-based}/CLAUDE.md +42 -49
- package/kits/turn-based/blueprints/board.scene +15 -0
- package/kits/turn-based/blueprints/square.scene +25 -0
- package/kits/turn-based/castle.json +37 -0
- package/kits/turn-based/code/behaviors/Board.jsx +169 -0
- package/kits/turn-based/code/behaviors/Square.jsx +26 -0
- package/kits/turn-based/code/server/index.js +152 -0
- package/kits/turn-based/drawings/square.sprite +25 -0
- package/kits/turn-based/eslint.config.js +12 -0
- package/kits/turn-based/fonts.generated.js +9 -0
- package/kits/{tabletop → turn-based}/index.html +5 -3
- package/kits/{tabletop → turn-based}/main.jsx +2 -2
- package/kits/{tabletop → turn-based}/package-lock.json +17 -4
- package/kits/{tabletop → turn-based}/package.json +5 -2
- package/kits/turn-based/room.js +289 -0
- package/kits/turn-based/scenes/main.scene +227 -0
- package/kits/{tabletop → turn-based}/testing.js +131 -99
- package/kits/turn-based/theme.style +3 -0
- package/package.json +1 -1
- package/kits/blank/CLAUDE.md +0 -198
- package/kits/tabletop/castle.json +0 -34
- package/kits/tabletop/room.js +0 -269
- /package/kits/{blank → base}/index.html +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/README.md +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/catalog.d.ts +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/catalog.js +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/face.d.ts +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/face.js +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/Bore.d.ts +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/Bore.js +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/CourierPrime.d.ts +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/CourierPrime.js +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/DMSans.d.ts +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/DMSans.js +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/Glacier.d.ts +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/Glacier.js +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/HelicoCentrica.d.ts +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/HelicoCentrica.js +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/Piazzolla.d.ts +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/Piazzolla.js +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/Synco.d.ts +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/Synco.js +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/Tektur.d.ts +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/Tektur.js +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/YatraOne.d.ts +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/YatraOne.js +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/index.d.ts +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/index.js +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/licenses/CC-BY-ND-4.0.txt +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/licenses/FONTS.md +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/licenses/OFL-1.1.txt +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/package.json +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/woff2/Bore.woff2 +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/woff2/CourierPrime.woff2 +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/woff2/DMSans.woff2 +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/woff2/Glacier.woff2 +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/woff2/HelicoCentrica.woff2 +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/woff2/Piazzolla.woff2 +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/woff2/SOURCE.txt +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/woff2/Synco.woff2 +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/woff2/Tektur.woff2 +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/woff2/YatraOne.woff2 +0 -0
- /package/kits/physics-3d/drawings/{crate.pxart → crate.sprite} +0 -0
- /package/kits/physics-3d/drawings/{door-edge.pxart → door-edge.sprite} +0 -0
- /package/kits/physics-3d/drawings/{door.pxart → door.sprite} +0 -0
- /package/kits/physics-3d/drawings/{face.pxart → face.sprite} +0 -0
- /package/kits/physics-3d/drawings/{floor.pxart → floor.sprite} +0 -0
- /package/kits/physics-3d/drawings/{platform.pxart → platform.sprite} +0 -0
- /package/kits/physics-3d/drawings/{statue.pxart → statue.sprite} +0 -0
- /package/kits/physics-3d/drawings/{wall.pxart → wall.sprite} +0 -0
- /package/kits/{tabletop → turn-based}/.prettierrc +0 -0
package/kits/tabletop/room.js
DELETED
|
@@ -1,269 +0,0 @@
|
|
|
1
|
-
// The room a board game is played in -- who is sitting where, who is only
|
|
2
|
-
// watching, who just dropped and might be back, and whether the state left in
|
|
3
|
-
// this session still belongs to anybody who is here.
|
|
4
|
-
//
|
|
5
|
-
// Nothing in this file knows a rule of any game. The deck supplies the numbers
|
|
6
|
-
// and a factory for a fresh game; the bookkeeping below is what three decks,
|
|
7
|
-
// written independently from one spec, each ended up writing for themselves.
|
|
8
|
-
//
|
|
9
|
-
// One rule the kit ENFORCES rather than offers: **a leave never clears game
|
|
10
|
-
// state.** `leave` releases the seat, remembers the departure, and promotes a
|
|
11
|
-
// watcher -- the board is left exactly as it was. Clearing is a decision made on
|
|
12
|
-
// the way IN, where there is somebody to hand a clean table to. There is no code
|
|
13
|
-
// path from `leave` to `createState`, so a deck built on this cannot destroy a
|
|
14
|
-
// present player's game because their opponent's train went into a tunnel.
|
|
15
|
-
|
|
16
|
-
// --- the clock ------------------------------------------------------------
|
|
17
|
-
//
|
|
18
|
-
// Behind one indirection so a test can move it: every freshness rule here is
|
|
19
|
-
// "has it been N minutes", and N is minutes, so a test that waits for real time
|
|
20
|
-
// is a test nobody runs. `testing.js` uses `setNow`; production never touches it.
|
|
21
|
-
|
|
22
|
-
let clock = () => Date.now();
|
|
23
|
-
|
|
24
|
-
export function now() {
|
|
25
|
-
return clock();
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export function setNow(fn) {
|
|
29
|
-
clock = fn;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export function restoreNow() {
|
|
33
|
-
clock = () => Date.now();
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
// --- the room -------------------------------------------------------------
|
|
37
|
-
|
|
38
|
-
// A seat needs something to show. Decks that want other wording pass
|
|
39
|
-
// `displayName`; identity itself always comes from the platform's `player`.
|
|
40
|
-
export function displayName(player) {
|
|
41
|
-
if (player.username) return player.username;
|
|
42
|
-
return player.isAnonymous ? 'guest' : 'player';
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
// `config`:
|
|
46
|
-
// seats seat ids, in the order they are handed out (required)
|
|
47
|
-
// createState () => the deck's fresh game, stored at `table.game` (required)
|
|
48
|
-
// idleMs how long an untended table may sit before an arriving
|
|
49
|
-
// player is given a clean one instead (required)
|
|
50
|
-
// graceMs how long a dropped player's seat is held for them (required)
|
|
51
|
-
// minPlayers present seat-holders at or above this protect the table
|
|
52
|
-
// from any join-side clear. Default: every seat.
|
|
53
|
-
// displayName (player) => string
|
|
54
|
-
// now () => ms, if the deck wants its own clock rather than the
|
|
55
|
-
// kit's (which `setNow` above makes movable for tests)
|
|
56
|
-
export function createRoom(config) {
|
|
57
|
-
const settings = {
|
|
58
|
-
seats: [...config.seats],
|
|
59
|
-
createState: config.createState,
|
|
60
|
-
idleMs: config.idleMs,
|
|
61
|
-
graceMs: config.graceMs,
|
|
62
|
-
minPlayers: config.minPlayers ?? config.seats.length,
|
|
63
|
-
displayName: config.displayName ?? displayName,
|
|
64
|
-
now: config.now ?? now,
|
|
65
|
-
};
|
|
66
|
-
const tables = new Map();
|
|
67
|
-
const table = (session) => tableFor(tables, settings, session);
|
|
68
|
-
return {
|
|
69
|
-
table,
|
|
70
|
-
join: (session, player) => joinTable(table(session), settings, session, player),
|
|
71
|
-
leave: (session, player) => leaveTable(table(session), settings, session, player),
|
|
72
|
-
// One process per session today, but the map is keyed by sessionId so that
|
|
73
|
-
// staying true is not load-bearing. `forget` is for a host that reuses one.
|
|
74
|
-
forget: (session) => tables.delete(session.sessionId),
|
|
75
|
-
};
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
// One table per session, made on first sight of it. A deck's `onStart` can call
|
|
79
|
-
// `room.table(session)` to have it exist before anyone joins, but nothing needs
|
|
80
|
-
// it to: every entry point below makes the table it works on.
|
|
81
|
-
function tableFor(tables, settings, session) {
|
|
82
|
-
const existing = tables.get(session.sessionId);
|
|
83
|
-
if (existing) return existing;
|
|
84
|
-
const table = createTable(settings, session.sessionId);
|
|
85
|
-
tables.set(session.sessionId, table);
|
|
86
|
-
return table;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
function createTable(settings, sessionId) {
|
|
90
|
-
const table = {
|
|
91
|
-
sessionId,
|
|
92
|
-
// The deck's own game object. The kit replaces it wholesale on a clear and
|
|
93
|
-
// otherwise never reads or writes it.
|
|
94
|
-
game: settings.createState(),
|
|
95
|
-
// Seat id -> holder, or null. A holder is
|
|
96
|
-
// `{ playerId, userId, name }` -- see `seatEntry`.
|
|
97
|
-
seats: Object.fromEntries(settings.seats.map((seat) => [seat, null])),
|
|
98
|
-
lastActivityAt: settings.now(),
|
|
99
|
-
// playerId -> { seat, holder, at }, for the grace window.
|
|
100
|
-
departed: new Map(),
|
|
101
|
-
};
|
|
102
|
-
return Object.assign(table, {
|
|
103
|
-
seatOf: (playerId) => seatOf(table, playerId),
|
|
104
|
-
holder: (seat) => table.seats[seat] ?? null,
|
|
105
|
-
names: () => Object.fromEntries(settings.seats.map((s) => [s, table.seats[s]?.name ?? null])),
|
|
106
|
-
occupied: () => settings.seats.filter((seat) => table.seats[seat]).length,
|
|
107
|
-
watching: (session) => watchingCount(table, session),
|
|
108
|
-
// Call this ONLY past every rejection. A refused message is not a sign of
|
|
109
|
-
// life, and a client that could refresh the clock with junk could keep a
|
|
110
|
-
// dead table alive forever.
|
|
111
|
-
touch: () => {
|
|
112
|
-
table.lastActivityAt = settings.now();
|
|
113
|
-
},
|
|
114
|
-
// A rematch the players asked for: fresh game, same seats, clock restarted.
|
|
115
|
-
newRound: () => {
|
|
116
|
-
table.game = settings.createState();
|
|
117
|
-
table.lastActivityAt = settings.now();
|
|
118
|
-
},
|
|
119
|
-
// Shift every holder one seat BACKWARDS along the declared order, so a
|
|
120
|
-
// series alternates who plays what.
|
|
121
|
-
rotate: () => rotateSeats(settings, table),
|
|
122
|
-
});
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
function seatOf(table, playerId) {
|
|
126
|
-
for (const [seat, holder] of Object.entries(table.seats)) {
|
|
127
|
-
if (holder?.playerId === playerId) return seat;
|
|
128
|
-
}
|
|
129
|
-
return null;
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
function seatEntry(settings, player) {
|
|
133
|
-
return {
|
|
134
|
-
playerId: player.playerId,
|
|
135
|
-
userId: player.userId,
|
|
136
|
-
name: settings.displayName(player),
|
|
137
|
-
};
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
// A seat is held by a CONNECTION, not an account: the platform lets one account
|
|
141
|
-
// join twice from two tabs, and each of those is a separate player at the table
|
|
142
|
-
// (which is also what makes local two-player testing work at all).
|
|
143
|
-
function takeSeat(settings, table, player) {
|
|
144
|
-
if (seatOf(table, player.playerId)) return null;
|
|
145
|
-
for (const seat of settings.seats) {
|
|
146
|
-
if (table.seats[seat]) continue;
|
|
147
|
-
table.seats[seat] = seatEntry(settings, player);
|
|
148
|
-
return seat;
|
|
149
|
-
}
|
|
150
|
-
return null;
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
// Empty seats rotate with the held ones -- this moves the whole ring, it does
|
|
154
|
-
// not compact it.
|
|
155
|
-
function rotateSeats(settings, table) {
|
|
156
|
-
const held = settings.seats.map((seat) => table.seats[seat]);
|
|
157
|
-
settings.seats.forEach((seat, index) => {
|
|
158
|
-
table.seats[seat] = held[(index + held.length - 1) % held.length];
|
|
159
|
-
});
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
function presentIds(players) {
|
|
163
|
-
return new Set(players.map((player) => player.playerId));
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
// Seats held by somebody who is actually in the session right now. A seat whose
|
|
167
|
-
// holder has dropped is not cover for anything.
|
|
168
|
-
function coveredSeats(table, players) {
|
|
169
|
-
const ids = presentIds(players);
|
|
170
|
-
return Object.values(table.seats).filter((holder) => holder && ids.has(holder.playerId)).length;
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
function watchingCount(table, session) {
|
|
174
|
-
return Math.max(0, session.players.length - coveredSeats(table, session.players));
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
// Everyone in the session except this player. Filtered rather than counted off
|
|
178
|
-
// `players.length`, so it reads the same whether or not the platform has already
|
|
179
|
-
// added a joiner to the roster (it has) or removed a leaver (it has).
|
|
180
|
-
function othersIn(session, player) {
|
|
181
|
-
return session.players.filter((other) => other.playerId !== player.playerId);
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
// Give a dropped player their seat back, if it is still free and they are inside
|
|
185
|
-
// the grace window. Returns whether this join is a RETURN rather than an
|
|
186
|
-
// arrival, which is the thing the clear rules turn on.
|
|
187
|
-
//
|
|
188
|
-
// `playerId` survives a transport reconnect (see CLAUDE.md, "What a reconnect
|
|
189
|
-
// is"), so someone whose connection blipped comes back as themselves rather than
|
|
190
|
-
// as a stranger walking in on their own game.
|
|
191
|
-
function reclaimSeat(settings, table, player) {
|
|
192
|
-
if (seatOf(table, player.playerId)) return true; // never actually lost it
|
|
193
|
-
const record = table.departed.get(player.playerId);
|
|
194
|
-
if (!record) return false;
|
|
195
|
-
table.departed.delete(player.playerId);
|
|
196
|
-
if (settings.now() - record.at > settings.graceMs) return false;
|
|
197
|
-
if (table.seats[record.seat]) return false; // a present watcher already has it
|
|
198
|
-
table.seats[record.seat] = record.holder;
|
|
199
|
-
return true;
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
function pruneDeparted(settings, table) {
|
|
203
|
-
const cutoff = settings.now() - settings.graceMs;
|
|
204
|
-
for (const [playerId, record] of table.departed) {
|
|
205
|
-
if (record.at < cutoff) table.departed.delete(playerId);
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
// The whole clear policy, in one place.
|
|
210
|
-
//
|
|
211
|
-
// Tested against `party` rather than `public` on purpose. A party already spins
|
|
212
|
-
// up a fresh session whenever it needs one, so it is the one mode with nothing
|
|
213
|
-
// to clean up -- and the host converts a `public` request into `party` when the
|
|
214
|
-
// play came from one (`sdk/src/host.ts:332`) while `castle-web serve` reports
|
|
215
|
-
// `named` for every session (`cli/src/devSessionServer.ts`), so a `=== 'public'`
|
|
216
|
-
// test would be dead code both in production and on a serve.
|
|
217
|
-
function shouldClear(settings, table, others, returning, mode) {
|
|
218
|
-
if (mode === 'party') return false; // the party's own session, and its own state
|
|
219
|
-
if (returning) return false; // their game, not a stranger's
|
|
220
|
-
if (others.length === 0) return true; // arrived alone: nothing here is anyone's
|
|
221
|
-
if (coveredSeats(table, others) >= settings.minPlayers) return false; // a live game
|
|
222
|
-
return settings.now() - table.lastActivityAt > settings.idleMs;
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
// A clean table for whoever just walked in. Seats held by people who are still
|
|
226
|
-
// here survive it -- they did not ask for a new game, they are just in the room.
|
|
227
|
-
function clearTable(settings, table, present) {
|
|
228
|
-
table.game = settings.createState();
|
|
229
|
-
table.lastActivityAt = settings.now();
|
|
230
|
-
table.departed.clear();
|
|
231
|
-
const ids = presentIds(present);
|
|
232
|
-
for (const seat of settings.seats) {
|
|
233
|
-
if (table.seats[seat] && !ids.has(table.seats[seat].playerId)) table.seats[seat] = null;
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
// Returns `{ table, seat, returning, cleared }`. `seat` is null for a spectator:
|
|
238
|
-
// everyone past the last seat watches.
|
|
239
|
-
function joinTable(table, settings, session, player) {
|
|
240
|
-
pruneDeparted(settings, table);
|
|
241
|
-
const others = othersIn(session, player);
|
|
242
|
-
const returning = reclaimSeat(settings, table, player);
|
|
243
|
-
const cleared = shouldClear(settings, table, others, returning, session.mode);
|
|
244
|
-
if (cleared) clearTable(settings, table, others);
|
|
245
|
-
takeSeat(settings, table, player);
|
|
246
|
-
return { table, seat: table.seatOf(player.playerId), returning, cleared };
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
// Returns `{ table, seat, promoted }` -- the seat that was freed (null if the
|
|
250
|
-
// leaver was watching) and the players moved up into seats.
|
|
251
|
-
//
|
|
252
|
-
// Note what is NOT here: any mention of `game`. See the file header.
|
|
253
|
-
function leaveTable(table, settings, session, player) {
|
|
254
|
-
const seat = seatOf(table, player.playerId);
|
|
255
|
-
if (seat) {
|
|
256
|
-
// Remembered, not just dropped, so a reconnect inside the grace window gets
|
|
257
|
-
// this seat back instead of arriving as a stranger.
|
|
258
|
-
table.departed.set(player.playerId, { seat, holder: table.seats[seat], at: settings.now() });
|
|
259
|
-
table.seats[seat] = null;
|
|
260
|
-
}
|
|
261
|
-
// Somebody already in the room outranks a seat being held for a connection
|
|
262
|
-
// that may never come back; `reclaimSeat` gives way once the seat is taken.
|
|
263
|
-
const promoted = [];
|
|
264
|
-
for (const other of othersIn(session, player)) {
|
|
265
|
-
if (takeSeat(settings, table, other)) promoted.push(other.playerId);
|
|
266
|
-
}
|
|
267
|
-
pruneDeparted(settings, table);
|
|
268
|
-
return { table, seat, promoted };
|
|
269
|
-
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|