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.
Files changed (243) hide show
  1. package/dist/agent-failures.d.ts +1 -0
  2. package/dist/agent-failures.js +6 -4
  3. package/dist/agent-prompts.js +4 -0
  4. package/dist/agent.d.ts +11 -0
  5. package/dist/agent.js +175 -15
  6. package/dist/bundle.js +8 -2
  7. package/dist/castle-host/host.js +114 -8
  8. package/dist/castleJson.js +6 -1
  9. package/dist/check.d.ts +1 -0
  10. package/dist/check.js +25 -0
  11. package/dist/devSessionServer.js +31 -7
  12. package/dist/imports.js +1 -19
  13. package/dist/index.js +15 -1
  14. package/dist/init.d.ts +2 -2
  15. package/dist/init.js +78 -42
  16. package/dist/mcpPlaytest.d.ts +1 -0
  17. package/dist/mcpPlaytest.js +182 -0
  18. package/dist/metering.d.ts +24 -0
  19. package/dist/metering.js +87 -0
  20. package/dist/sdkResolve.d.ts +3 -0
  21. package/dist/sdkResolve.js +55 -0
  22. package/dist/serve.js +50 -4
  23. package/dist/serverBundle.js +8 -3
  24. package/dist/shell/assets/{index-DsDkYFvP.js → index-BuSYLy-C.js} +64 -64
  25. package/dist/shell/assets/{index-DwezawBs.css → index-DfZmu3XC.css} +1 -1
  26. package/dist/shell/index.html +2 -2
  27. package/dist/vitePlugins.d.ts +1 -0
  28. package/dist/vitePlugins.js +20 -0
  29. package/kits/base/CLAUDE.md +196 -0
  30. package/kits/{blank → base}/castle.json +5 -2
  31. package/kits/base/editorHost.js +172 -0
  32. package/kits/{blank → base}/node_modules/.package-lock.json +3 -2
  33. package/kits/{blank → base}/package-lock.json +7 -4
  34. package/kits/{blank → base}/package.json +4 -2
  35. package/kits/base/sdk/README.md +569 -0
  36. package/kits/base/sdk/castle.d.ts +27 -0
  37. package/kits/base/sdk/castle.js +16 -0
  38. package/kits/base/sdk/commands.d.ts +181 -0
  39. package/kits/base/sdk/commands.js +16 -0
  40. package/kits/base/sdk/context.d.ts +17 -0
  41. package/kits/base/sdk/context.js +15 -0
  42. package/kits/base/sdk/errors.d.ts +22 -0
  43. package/kits/base/sdk/errors.js +16 -0
  44. package/kits/base/sdk/files.d.ts +15 -0
  45. package/kits/base/sdk/files.js +52 -0
  46. package/kits/base/sdk/haptics.d.ts +6 -0
  47. package/kits/base/sdk/haptics.js +36 -0
  48. package/kits/base/sdk/leaderboard.d.ts +26 -0
  49. package/kits/base/sdk/leaderboard.js +296 -0
  50. package/kits/base/sdk/lifecycle.d.ts +4 -0
  51. package/kits/base/sdk/lifecycle.js +13 -0
  52. package/kits/base/sdk/multiplayer.d.ts +43 -0
  53. package/kits/base/sdk/multiplayer.js +464 -0
  54. package/kits/base/sdk/multiplayerProtocol.d.ts +51 -0
  55. package/kits/base/sdk/multiplayerProtocol.js +5 -0
  56. package/kits/base/sdk/passes.d.ts +7 -0
  57. package/kits/base/sdk/passes.js +84 -0
  58. package/kits/base/sdk/portal.d.ts +7 -0
  59. package/kits/base/sdk/portal.js +35 -0
  60. package/kits/base/sdk/runtime.d.ts +62 -0
  61. package/kits/base/sdk/runtime.js +970 -0
  62. package/kits/base/sdk/saveQueue.d.ts +27 -0
  63. package/kits/base/sdk/saveQueue.js +205 -0
  64. package/kits/base/sdk/server/platformHandle.d.ts +25 -0
  65. package/kits/base/sdk/server/platformHandle.js +1 -0
  66. package/kits/base/sdk/server/wrapper.d.ts +28 -0
  67. package/kits/base/sdk/server/wrapper.js +109 -0
  68. package/kits/base/sdk/storage.d.ts +16 -0
  69. package/kits/base/sdk/storage.js +323 -0
  70. package/kits/base/sdk/time.d.ts +17 -0
  71. package/kits/base/sdk/time.js +120 -0
  72. package/kits/base/sdk/transport.d.ts +17 -0
  73. package/kits/base/sdk/transport.js +141 -0
  74. package/kits/base/sdk/types.d.ts +3 -0
  75. package/kits/base/sdk/types.js +1 -0
  76. package/kits/base/sdk/user.d.ts +10 -0
  77. package/kits/base/sdk/user.js +45 -0
  78. package/kits/multiplayer-2d/.prettierrc +5 -0
  79. package/kits/multiplayer-2d/CLAUDE.md +129 -0
  80. package/kits/multiplayer-2d/behaviors/Box.jsx +92 -0
  81. package/kits/multiplayer-2d/behaviors/Player.jsx +86 -0
  82. package/kits/multiplayer-2d/blueprints/crate.scene +13 -0
  83. package/kits/multiplayer-2d/blueprints/other-player.scene +13 -0
  84. package/kits/multiplayer-2d/blueprints/player.scene +15 -0
  85. package/kits/multiplayer-2d/blueprints/wall.scene +12 -0
  86. package/kits/multiplayer-2d/castle.json +39 -0
  87. package/kits/multiplayer-2d/code/client/avatars.js +79 -0
  88. package/kits/multiplayer-2d/code/client/poses.js +168 -0
  89. package/kits/multiplayer-2d/code/server/index.js +26 -0
  90. package/kits/multiplayer-2d/code/server/limits.js +99 -0
  91. package/kits/multiplayer-2d/code/server/ownership.js +177 -0
  92. package/kits/multiplayer-2d/code/server/players.js +127 -0
  93. package/kits/multiplayer-2d/code/server/sceneBodies.js +98 -0
  94. package/kits/multiplayer-2d/code/server/world.js +337 -0
  95. package/kits/multiplayer-2d/code/systems/multiplayer.js +424 -0
  96. package/kits/multiplayer-2d/eslint.config.js +21 -0
  97. package/kits/multiplayer-2d/fonts.generated.js +9 -0
  98. package/kits/multiplayer-2d/index.html +25 -0
  99. package/kits/multiplayer-2d/main.jsx +10 -0
  100. package/kits/multiplayer-2d/package-lock.json +351 -0
  101. package/kits/multiplayer-2d/package.json +30 -0
  102. package/kits/multiplayer-2d/scenes/main.scene +20 -0
  103. package/kits/multiplayer-2d/test/two-client-test.mjs +341 -0
  104. package/kits/multiplayer-2d/theme.style +4 -0
  105. package/kits/multiplayer-3d/.prettierrc +5 -0
  106. package/kits/multiplayer-3d/CLAUDE.md +215 -0
  107. package/kits/multiplayer-3d/blueprints/ball.scene +21 -0
  108. package/kits/multiplayer-3d/blueprints/other-player.scene +21 -0
  109. package/kits/multiplayer-3d/castle.json +43 -0
  110. package/kits/multiplayer-3d/code/client/avatars.js +136 -0
  111. package/kits/multiplayer-3d/code/client/nameTags.js +101 -0
  112. package/kits/multiplayer-3d/code/client/poses.js +220 -0
  113. package/kits/multiplayer-3d/code/server/index.js +23 -0
  114. package/kits/multiplayer-3d/code/server/limits.js +107 -0
  115. package/kits/multiplayer-3d/code/server/ownership.js +157 -0
  116. package/kits/multiplayer-3d/code/server/players.js +114 -0
  117. package/kits/multiplayer-3d/code/server/sceneBodies.js +88 -0
  118. package/kits/multiplayer-3d/code/server/world.js +306 -0
  119. package/kits/multiplayer-3d/code/systems/multiplayer.js +427 -0
  120. package/kits/multiplayer-3d/eslint.config.js +20 -0
  121. package/kits/multiplayer-3d/fonts.generated.js +9 -0
  122. package/kits/multiplayer-3d/index.html +22 -0
  123. package/kits/multiplayer-3d/main.jsx +9 -0
  124. package/kits/multiplayer-3d/package-lock.json +368 -0
  125. package/kits/multiplayer-3d/package.json +32 -0
  126. package/kits/multiplayer-3d/scenes/main.scene +409 -0
  127. package/kits/multiplayer-3d/theme.style +3 -0
  128. package/kits/physics-2d/CLAUDE.md +6 -0
  129. package/kits/physics-2d/castle.json +7 -1
  130. package/kits/physics-2d/editors/behaviorRegistry.js +5 -0
  131. package/kits/physics-2d/editors/editorRegistry.jsx +3 -1
  132. package/kits/physics-2d/engine/files.js +9 -5
  133. package/kits/physics-2d/engine/systemRegistry.js +2 -0
  134. package/kits/physics-2d/engine/ui.module.css +11 -0
  135. package/kits/physics-2d/package-lock.json +8 -2
  136. package/kits/physics-2d/package.json +2 -2
  137. package/kits/physics-3d/CLAUDE.md +4 -4
  138. package/kits/physics-3d/behaviors/Shape.jsx +1 -1
  139. package/kits/physics-3d/blueprints/crate.scene +1 -1
  140. package/kits/physics-3d/blueprints/door.scene +4 -4
  141. package/kits/physics-3d/blueprints/pillar.scene +1 -1
  142. package/kits/physics-3d/blueprints/platform.scene +1 -1
  143. package/kits/physics-3d/blueprints/statue.scene +1 -1
  144. package/kits/physics-3d/blueprints/wall.scene +1 -1
  145. package/kits/physics-3d/castle.json +8 -11
  146. package/kits/physics-3d/docs/pxmodel-format.md +1 -1
  147. package/kits/physics-3d/engine3d/materials.js +3 -3
  148. package/kits/physics-3d/engine3d/modelFiles.js +4 -2
  149. package/kits/physics-3d/engine3d/world3d.js +1 -1
  150. package/kits/physics-3d/fonts.generated.js +9 -0
  151. package/kits/physics-3d/main.jsx +1 -1
  152. package/kits/physics-3d/package-lock.json +85 -2
  153. package/kits/physics-3d/package.json +6 -2
  154. package/kits/physics-3d/scenes/main.scene +1 -1
  155. package/kits/physics-3d/scenes/model-lab.scene +1 -1
  156. package/kits/real-time/.prettierrc +5 -0
  157. package/kits/real-time/CLAUDE.md +60 -0
  158. package/kits/real-time/castle.json +19 -0
  159. package/kits/real-time/code/client/clock.js +68 -0
  160. package/kits/real-time/code/client/connection.js +134 -0
  161. package/kits/real-time/code/client/gameHooks.js +50 -0
  162. package/kits/real-time/code/client/interpolate.js +82 -0
  163. package/kits/real-time/code/client/messages.js +55 -0
  164. package/kits/real-time/code/client/ownership.js +94 -0
  165. package/kits/real-time/code/client/smooth.js +54 -0
  166. package/kits/real-time/code/server/blockDelta.js +48 -0
  167. package/kits/real-time/code/server/gameHooks.js +47 -0
  168. package/kits/real-time/code/server/limits.js +35 -0
  169. package/kits/real-time/code/server/session.js +251 -0
  170. package/kits/real-time/eslint.config.js +22 -0
  171. package/kits/real-time/index.html +45 -0
  172. package/kits/real-time/package.json +14 -0
  173. package/kits/{tabletop → turn-based}/CLAUDE.md +42 -49
  174. package/kits/turn-based/blueprints/board.scene +15 -0
  175. package/kits/turn-based/blueprints/square.scene +25 -0
  176. package/kits/turn-based/castle.json +37 -0
  177. package/kits/turn-based/code/behaviors/Board.jsx +169 -0
  178. package/kits/turn-based/code/behaviors/Square.jsx +26 -0
  179. package/kits/turn-based/code/server/index.js +152 -0
  180. package/kits/turn-based/drawings/square.sprite +25 -0
  181. package/kits/turn-based/eslint.config.js +12 -0
  182. package/kits/turn-based/fonts.generated.js +9 -0
  183. package/kits/{tabletop → turn-based}/index.html +5 -3
  184. package/kits/{tabletop → turn-based}/main.jsx +2 -2
  185. package/kits/{tabletop → turn-based}/package-lock.json +17 -4
  186. package/kits/{tabletop → turn-based}/package.json +5 -2
  187. package/kits/turn-based/room.js +289 -0
  188. package/kits/turn-based/scenes/main.scene +227 -0
  189. package/kits/{tabletop → turn-based}/testing.js +131 -99
  190. package/kits/turn-based/theme.style +3 -0
  191. package/package.json +1 -1
  192. package/kits/blank/CLAUDE.md +0 -198
  193. package/kits/tabletop/castle.json +0 -34
  194. package/kits/tabletop/room.js +0 -269
  195. /package/kits/{blank → base}/index.html +0 -0
  196. /package/kits/{blank → base}/node_modules/castle-web-fonts/README.md +0 -0
  197. /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/catalog.d.ts +0 -0
  198. /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/catalog.js +0 -0
  199. /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/face.d.ts +0 -0
  200. /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/face.js +0 -0
  201. /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/Bore.d.ts +0 -0
  202. /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/Bore.js +0 -0
  203. /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/CourierPrime.d.ts +0 -0
  204. /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/CourierPrime.js +0 -0
  205. /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/DMSans.d.ts +0 -0
  206. /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/DMSans.js +0 -0
  207. /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/Glacier.d.ts +0 -0
  208. /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/Glacier.js +0 -0
  209. /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/HelicoCentrica.d.ts +0 -0
  210. /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/HelicoCentrica.js +0 -0
  211. /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/Piazzolla.d.ts +0 -0
  212. /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/Piazzolla.js +0 -0
  213. /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/Synco.d.ts +0 -0
  214. /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/Synco.js +0 -0
  215. /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/Tektur.d.ts +0 -0
  216. /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/Tektur.js +0 -0
  217. /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/YatraOne.d.ts +0 -0
  218. /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/YatraOne.js +0 -0
  219. /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/index.d.ts +0 -0
  220. /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/index.js +0 -0
  221. /package/kits/{blank → base}/node_modules/castle-web-fonts/licenses/CC-BY-ND-4.0.txt +0 -0
  222. /package/kits/{blank → base}/node_modules/castle-web-fonts/licenses/FONTS.md +0 -0
  223. /package/kits/{blank → base}/node_modules/castle-web-fonts/licenses/OFL-1.1.txt +0 -0
  224. /package/kits/{blank → base}/node_modules/castle-web-fonts/package.json +0 -0
  225. /package/kits/{blank → base}/node_modules/castle-web-fonts/woff2/Bore.woff2 +0 -0
  226. /package/kits/{blank → base}/node_modules/castle-web-fonts/woff2/CourierPrime.woff2 +0 -0
  227. /package/kits/{blank → base}/node_modules/castle-web-fonts/woff2/DMSans.woff2 +0 -0
  228. /package/kits/{blank → base}/node_modules/castle-web-fonts/woff2/Glacier.woff2 +0 -0
  229. /package/kits/{blank → base}/node_modules/castle-web-fonts/woff2/HelicoCentrica.woff2 +0 -0
  230. /package/kits/{blank → base}/node_modules/castle-web-fonts/woff2/Piazzolla.woff2 +0 -0
  231. /package/kits/{blank → base}/node_modules/castle-web-fonts/woff2/SOURCE.txt +0 -0
  232. /package/kits/{blank → base}/node_modules/castle-web-fonts/woff2/Synco.woff2 +0 -0
  233. /package/kits/{blank → base}/node_modules/castle-web-fonts/woff2/Tektur.woff2 +0 -0
  234. /package/kits/{blank → base}/node_modules/castle-web-fonts/woff2/YatraOne.woff2 +0 -0
  235. /package/kits/physics-3d/drawings/{crate.pxart → crate.sprite} +0 -0
  236. /package/kits/physics-3d/drawings/{door-edge.pxart → door-edge.sprite} +0 -0
  237. /package/kits/physics-3d/drawings/{door.pxart → door.sprite} +0 -0
  238. /package/kits/physics-3d/drawings/{face.pxart → face.sprite} +0 -0
  239. /package/kits/physics-3d/drawings/{floor.pxart → floor.sprite} +0 -0
  240. /package/kits/physics-3d/drawings/{platform.pxart → platform.sprite} +0 -0
  241. /package/kits/physics-3d/drawings/{statue.pxart → statue.sprite} +0 -0
  242. /package/kits/physics-3d/drawings/{wall.pxart → wall.sprite} +0 -0
  243. /package/kits/{tabletop → turn-based}/.prettierrc +0 -0
@@ -0,0 +1,289 @@
1
+ // Seats, spectators, reconnects and session freshness for a turn-based game.
2
+ // No game rules here: a deck supplies the seat ids and three durations, and
3
+ // keeps its own game at `table.game`. The kit never builds or reads it -- a
4
+ // clear sets `table.game` back to null, and the deck builds a fresh one in its
5
+ // join handler. There is no game factory in the kit; a rematch is the deck
6
+ // replacing `table.game` itself.
7
+ //
8
+ // A leave never clears game state. `leave` releases the seat, records the
9
+ // departure and promotes a watcher. Clearing happens on join, where there is
10
+ // someone to hand a clean table to.
11
+ //
12
+ // Laid out as: the clock, then the room and its tables, then seats, then the
13
+ // join and leave rules.
14
+
15
+ // --- the clock ------------------------------------------------------------
16
+ //
17
+ // Behind one indirection so a test can move it. The freshness rules are all
18
+ // "has it been N minutes". `testing.js` calls `setClock`; production does not.
19
+
20
+ let readClock = () => Date.now();
21
+
22
+ export function setClock(fn) {
23
+ readClock = fn ?? (() => Date.now());
24
+ }
25
+
26
+ // --- the room -------------------------------------------------------------
27
+
28
+ // `config`:
29
+ // seats seat ids, in the order they are handed out (required)
30
+ // idleMs how long an untended table may sit before an arriving player is
31
+ // given a clean one instead (required)
32
+ // graceMs how long a dropped player's seat is held for them (required)
33
+ // minPlayers present seat-holders at or above this protect the table from any
34
+ // join-side clear. Default: every seat.
35
+ export function createRoom(config) {
36
+ const settings = {
37
+ seats: [...config.seats],
38
+ idleMs: config.idleMs,
39
+ graceMs: config.graceMs,
40
+ minPlayers: config.minPlayers ?? config.seats.length,
41
+ };
42
+ const tables = new Map();
43
+ const table = (session) => tableFor(tables, settings, session);
44
+ return {
45
+ table,
46
+ join: (session, player) => joinTable(table(session), settings, session, player),
47
+ leave: (session, player) => leaveTable(table(session), settings, session, player),
48
+ };
49
+ }
50
+
51
+ // One table per session, made on first sight of it. Every entry point below
52
+ // makes the table it works on, so calling this from `onStart` is optional.
53
+ function tableFor(tables, settings, session) {
54
+ const existing = tables.get(session.sessionId);
55
+ if (existing) {
56
+ return existing;
57
+ }
58
+ const table = createTable(settings, session.sessionId);
59
+ tables.set(session.sessionId, table);
60
+ return table;
61
+ }
62
+
63
+ function createTable(settings, sessionId) {
64
+ const table = {
65
+ sessionId,
66
+
67
+ // The deck's own game, null until the deck makes one and again after a
68
+ // clear. The kit never reads it.
69
+ game: null,
70
+
71
+ // Seat id -> holder, or null. A holder is
72
+ // `{ playerId, userId, name }` -- see `seatEntry`.
73
+ seats: Object.fromEntries(settings.seats.map((seat) => [seat, null])),
74
+ lastActivityAt: readClock(),
75
+
76
+ // playerId -> { seat, holder, at }, for the grace window.
77
+ departed: new Map(),
78
+ };
79
+ return Object.assign(table, {
80
+ seatOf: (playerId) => seatOf(table, playerId),
81
+ playerAt: (seat) => table.seats[seat] ?? null,
82
+ seatNames: () =>
83
+ Object.fromEntries(settings.seats.map((seat) => [seat, table.seats[seat]?.name ?? null])),
84
+ spectatorCount: (session) => spectatorCount(table, session),
85
+
86
+ // Call this past every rejection, never before one. A refused message is not
87
+ // a sign of life, and a client able to refresh the clock with junk could keep
88
+ // a dead table alive indefinitely.
89
+ keepAlive: () => {
90
+ table.lastActivityAt = readClock();
91
+ },
92
+ });
93
+ }
94
+
95
+ // --- seats ----------------------------------------------------------------
96
+
97
+ function seatOf(table, playerId) {
98
+ for (const [seat, holder] of Object.entries(table.seats)) {
99
+ if (holder?.playerId === playerId) {
100
+ return seat;
101
+ }
102
+ }
103
+ return null;
104
+ }
105
+
106
+ // The name shown on a seat. Identity comes from the platform's `player`.
107
+ function nameOf(player) {
108
+ if (player.username) {
109
+ return player.username;
110
+ }
111
+ return player.isAnonymous ? 'guest' : 'player';
112
+ }
113
+
114
+ function seatEntry(settings, player) {
115
+ return {
116
+ playerId: player.playerId,
117
+ userId: player.userId,
118
+ name: nameOf(player),
119
+ };
120
+ }
121
+
122
+ // A seat is held by a connection, not an account. One account joining twice
123
+ // from two tabs is two players at the table.
124
+ function takeSeat(settings, table, player) {
125
+ if (seatOf(table, player.playerId)) {
126
+ return null;
127
+ }
128
+ for (const seat of settings.seats) {
129
+ if (table.seats[seat]) {
130
+ continue;
131
+ }
132
+ table.seats[seat] = seatEntry(settings, player);
133
+ return seat;
134
+ }
135
+ return null;
136
+ }
137
+
138
+ // Seats whose holder is in the session right now.
139
+ function coveredSeats(table, players) {
140
+ const ids = new Set(players.map((player) => player.playerId));
141
+ return Object.values(table.seats).filter((holder) => holder && ids.has(holder.playerId)).length;
142
+ }
143
+
144
+ // Clamped at zero. A seat can be held by a player the platform has already
145
+ // dropped from `session.players`, which would otherwise count as negative.
146
+ function spectatorCount(table, session) {
147
+ return Math.max(0, session.players.length - coveredSeats(table, session.players));
148
+ }
149
+
150
+ // --- join and leave -------------------------------------------------------
151
+
152
+ // Everyone in the session except this player. `players.length` already includes
153
+ // a joiner and excludes a leaver, so this filters instead of counting.
154
+ function othersIn(session, player) {
155
+ return session.players.filter((other) => other.playerId !== player.playerId);
156
+ }
157
+
158
+ // Gives a dropped player their seat back if it is still free and they are
159
+ // inside the grace window. Returns true for a return, false for an arrival;
160
+ // the clear rules below test it.
161
+ //
162
+ // `playerId` survives a transport reconnect (see CLAUDE.md, "What a reconnect
163
+ // is"), so a blipped connection comes back as the same player.
164
+ function reclaimSeat(settings, table, player) {
165
+ // Still seated, so nothing was lost.
166
+ if (seatOf(table, player.playerId)) {
167
+ return true;
168
+ }
169
+
170
+ const record = table.departed.get(player.playerId);
171
+ if (!record) {
172
+ return false;
173
+ }
174
+
175
+ // The record is spent either way. A player who missed the window is an
176
+ // arrival, and arriving twice should not consume a second grace period.
177
+ table.departed.delete(player.playerId);
178
+ if (readClock() - record.at > settings.graceMs) {
179
+ return false;
180
+ }
181
+
182
+ // A watcher was promoted into the seat while this player was away.
183
+ if (table.seats[record.seat]) {
184
+ return false;
185
+ }
186
+
187
+ table.seats[record.seat] = record.holder;
188
+ return true;
189
+ }
190
+
191
+ // Called on both join and leave, so the map cannot grow across a long session.
192
+ function pruneDeparted(settings, table) {
193
+ const cutoff = readClock() - settings.graceMs;
194
+ for (const [playerId, record] of table.departed) {
195
+ if (record.at < cutoff) {
196
+ table.departed.delete(playerId);
197
+ }
198
+ }
199
+ }
200
+
201
+ // The clear policy, in one place.
202
+ //
203
+ // A party gets a fresh session whenever it needs one, so there is nothing to
204
+ // clean up. The test is on `party`, not `public`. `=== 'public'` would
205
+ // also be dead code: the host converts a public request to `party` when the play
206
+ // came from one (`sdk/src/host.ts:332`), and `castle-web serve` reports `named`
207
+ // for every session (`cli/src/devSessionServer.ts`).
208
+ function shouldClear(settings, table, others, returning, mode) {
209
+ // A party's session and its state are the party's own.
210
+ if (mode === 'party') {
211
+ return false;
212
+ }
213
+
214
+ // A returning player is rejoining their own game.
215
+ if (returning) {
216
+ return false;
217
+ }
218
+
219
+ // Nobody else is here, so nothing on the table belongs to anyone.
220
+ if (others.length === 0) {
221
+ return true;
222
+ }
223
+
224
+ // Enough present players to be a live game.
225
+ if (coveredSeats(table, others) >= settings.minPlayers) {
226
+ return false;
227
+ }
228
+
229
+ return readClock() - table.lastActivityAt > settings.idleMs;
230
+ }
231
+
232
+ // A clean table for whoever just walked in. Seats held by present players
233
+ // survive it; the rest are freed.
234
+ //
235
+ // `departed` is emptied too. Those records point at a game that no longer
236
+ // exists, and a reconnect after this is an arrival.
237
+ function clearTable(settings, table, present) {
238
+ table.game = null;
239
+ table.lastActivityAt = readClock();
240
+ table.departed.clear();
241
+ const ids = new Set(present.map((player) => player.playerId));
242
+ for (const seat of settings.seats) {
243
+ if (table.seats[seat] && !ids.has(table.seats[seat].playerId)) {
244
+ table.seats[seat] = null;
245
+ }
246
+ }
247
+ }
248
+
249
+ // Returns `{ table }`. A deck knows to build a fresh game from `table.game`
250
+ // being null -- it starts null and a clear puts it back, so the deck's join
251
+ // handler is `if (!table.game) { table.game = ... }` and nothing else.
252
+ //
253
+ // The order below matters at three points.
254
+ function joinTable(table, settings, session, player) {
255
+ pruneDeparted(settings, table);
256
+ const others = othersIn(session, player);
257
+
258
+ // Before the clear decision, which asks whether this is a return.
259
+ const returning = reclaimSeat(settings, table, player);
260
+
261
+ if (shouldClear(settings, table, others, returning, session.mode)) {
262
+ clearTable(settings, table, others);
263
+ }
264
+
265
+ // After the clear, so a seat taken here survives it. A returner already has
266
+ // one and `takeSeat` leaves them alone.
267
+ takeSeat(settings, table, player);
268
+ return { table };
269
+ }
270
+
271
+ // Returns `{ table }`. It does not touch `game`; see the file header.
272
+ function leaveTable(table, settings, session, player) {
273
+ const seat = seatOf(table, player.playerId);
274
+ if (seat) {
275
+ // Remembered, not just dropped, so a reconnect inside the grace window gets
276
+ // this seat back instead of arriving as a stranger.
277
+ table.departed.set(player.playerId, { seat, holder: table.seats[seat], at: readClock() });
278
+ table.seats[seat] = null;
279
+ }
280
+
281
+ // Somebody already in the room outranks a seat being held for a connection
282
+ // that may never come back. Once a promoted player fills the seat,
283
+ // `reclaimSeat` refuses the reclaim and the returner arrives as a watcher.
284
+ for (const other of othersIn(session, player)) {
285
+ takeSeat(settings, table, other);
286
+ }
287
+ pruneDeparted(settings, table);
288
+ return { table };
289
+ }
@@ -0,0 +1,227 @@
1
+ {
2
+ "name": "Main",
3
+ "background": "#141327",
4
+ "physics": {
5
+ "gravity": 0
6
+ },
7
+ "actors": [
8
+ {
9
+ "id": "board",
10
+ "blueprint": "blueprints/board.scene",
11
+ "components": {
12
+ "Layout": {
13
+ "x": 0,
14
+ "y": 0
15
+ }
16
+ }
17
+ },
18
+ {
19
+ "id": "square0",
20
+ "blueprint": "blueprints/square.scene",
21
+ "components": {
22
+ "Layout": {
23
+ "x": 46,
24
+ "y": 170
25
+ },
26
+ "Square": {
27
+ "index": 0
28
+ }
29
+ }
30
+ },
31
+ {
32
+ "id": "square1",
33
+ "blueprint": "blueprints/square.scene",
34
+ "components": {
35
+ "Layout": {
36
+ "x": 150,
37
+ "y": 170
38
+ },
39
+ "Square": {
40
+ "index": 1
41
+ }
42
+ }
43
+ },
44
+ {
45
+ "id": "square2",
46
+ "blueprint": "blueprints/square.scene",
47
+ "components": {
48
+ "Layout": {
49
+ "x": 254,
50
+ "y": 170
51
+ },
52
+ "Square": {
53
+ "index": 2
54
+ }
55
+ }
56
+ },
57
+ {
58
+ "id": "square3",
59
+ "blueprint": "blueprints/square.scene",
60
+ "components": {
61
+ "Layout": {
62
+ "x": 358,
63
+ "y": 170
64
+ },
65
+ "Square": {
66
+ "index": 3
67
+ }
68
+ }
69
+ },
70
+ {
71
+ "id": "square4",
72
+ "blueprint": "blueprints/square.scene",
73
+ "components": {
74
+ "Layout": {
75
+ "x": 46,
76
+ "y": 274
77
+ },
78
+ "Square": {
79
+ "index": 4
80
+ }
81
+ }
82
+ },
83
+ {
84
+ "id": "square5",
85
+ "blueprint": "blueprints/square.scene",
86
+ "components": {
87
+ "Layout": {
88
+ "x": 150,
89
+ "y": 274
90
+ },
91
+ "Square": {
92
+ "index": 5
93
+ }
94
+ }
95
+ },
96
+ {
97
+ "id": "square6",
98
+ "blueprint": "blueprints/square.scene",
99
+ "components": {
100
+ "Layout": {
101
+ "x": 254,
102
+ "y": 274
103
+ },
104
+ "Square": {
105
+ "index": 6
106
+ }
107
+ }
108
+ },
109
+ {
110
+ "id": "square7",
111
+ "blueprint": "blueprints/square.scene",
112
+ "components": {
113
+ "Layout": {
114
+ "x": 358,
115
+ "y": 274
116
+ },
117
+ "Square": {
118
+ "index": 7
119
+ }
120
+ }
121
+ },
122
+ {
123
+ "id": "square8",
124
+ "blueprint": "blueprints/square.scene",
125
+ "components": {
126
+ "Layout": {
127
+ "x": 46,
128
+ "y": 378
129
+ },
130
+ "Square": {
131
+ "index": 8
132
+ }
133
+ }
134
+ },
135
+ {
136
+ "id": "square9",
137
+ "blueprint": "blueprints/square.scene",
138
+ "components": {
139
+ "Layout": {
140
+ "x": 150,
141
+ "y": 378
142
+ },
143
+ "Square": {
144
+ "index": 9
145
+ }
146
+ }
147
+ },
148
+ {
149
+ "id": "square10",
150
+ "blueprint": "blueprints/square.scene",
151
+ "components": {
152
+ "Layout": {
153
+ "x": 254,
154
+ "y": 378
155
+ },
156
+ "Square": {
157
+ "index": 10
158
+ }
159
+ }
160
+ },
161
+ {
162
+ "id": "square11",
163
+ "blueprint": "blueprints/square.scene",
164
+ "components": {
165
+ "Layout": {
166
+ "x": 358,
167
+ "y": 378
168
+ },
169
+ "Square": {
170
+ "index": 11
171
+ }
172
+ }
173
+ },
174
+ {
175
+ "id": "square12",
176
+ "blueprint": "blueprints/square.scene",
177
+ "components": {
178
+ "Layout": {
179
+ "x": 46,
180
+ "y": 482
181
+ },
182
+ "Square": {
183
+ "index": 12
184
+ }
185
+ }
186
+ },
187
+ {
188
+ "id": "square13",
189
+ "blueprint": "blueprints/square.scene",
190
+ "components": {
191
+ "Layout": {
192
+ "x": 150,
193
+ "y": 482
194
+ },
195
+ "Square": {
196
+ "index": 13
197
+ }
198
+ }
199
+ },
200
+ {
201
+ "id": "square14",
202
+ "blueprint": "blueprints/square.scene",
203
+ "components": {
204
+ "Layout": {
205
+ "x": 254,
206
+ "y": 482
207
+ },
208
+ "Square": {
209
+ "index": 14
210
+ }
211
+ }
212
+ },
213
+ {
214
+ "id": "square15",
215
+ "blueprint": "blueprints/square.scene",
216
+ "components": {
217
+ "Layout": {
218
+ "x": 358,
219
+ "y": 482
220
+ },
221
+ "Square": {
222
+ "index": 15
223
+ }
224
+ }
225
+ }
226
+ ]
227
+ }