castle-web-cli 0.4.229 → 0.4.230

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 (232) hide show
  1. package/dist/agent-prompts.d.ts +2 -0
  2. package/dist/agent-prompts.js +16 -8
  3. package/dist/agent.d.ts +2 -0
  4. package/dist/agent.js +21 -21
  5. package/dist/api.d.ts +28 -2
  6. package/dist/api.js +74 -16
  7. package/dist/castle-host/host.d.ts +91 -0
  8. package/dist/castle-host/host.js +779 -3
  9. package/dist/castleJson.d.ts +44 -0
  10. package/dist/castleJson.js +131 -0
  11. package/dist/check.js +27 -1
  12. package/dist/contentPack.d.ts +49 -0
  13. package/dist/contentPack.js +273 -0
  14. package/dist/devExperiments.d.ts +21 -0
  15. package/dist/devExperiments.js +139 -0
  16. package/dist/devHub.d.ts +55 -0
  17. package/dist/devHub.js +187 -0
  18. package/dist/devSessionChild.js +3 -1
  19. package/dist/devSessionServer.d.ts +14 -1
  20. package/dist/devSessionServer.js +229 -44
  21. package/dist/experimentAssignment.d.ts +15 -0
  22. package/dist/experimentAssignment.js +41 -0
  23. package/dist/importCheckHook.d.ts +40 -0
  24. package/dist/importCheckHook.js +171 -0
  25. package/dist/importRegression.d.ts +15 -0
  26. package/dist/importRegression.js +100 -0
  27. package/dist/importTrust.d.ts +2 -0
  28. package/dist/importTrust.js +27 -0
  29. package/dist/importUpdateHook.d.ts +18 -0
  30. package/dist/importUpdateHook.js +31 -8
  31. package/dist/importUpdateHookRunner.js +19 -7
  32. package/dist/imports.d.ts +16 -0
  33. package/dist/imports.js +141 -31
  34. package/dist/index.js +3 -1
  35. package/dist/init.d.ts +3 -0
  36. package/dist/init.js +125 -8
  37. package/dist/install.js +4 -1
  38. package/dist/localPaths.js +4 -0
  39. package/dist/login.js +2 -2
  40. package/dist/platformDoc.js +3 -8
  41. package/dist/playPagePolicy.js +3 -1
  42. package/dist/remix.js +5 -0
  43. package/dist/routerTaskReport.d.ts +28 -0
  44. package/dist/routerTaskReport.js +56 -0
  45. package/dist/save-deck.d.ts +18 -0
  46. package/dist/save-deck.js +142 -6
  47. package/dist/serve.d.ts +8 -0
  48. package/dist/serve.js +94 -13
  49. package/dist/serverBundle.d.ts +1 -1
  50. package/dist/serverBundle.js +5 -1
  51. package/dist/webSourceSync.d.ts +2 -0
  52. package/dist/webSourceSync.js +2 -0
  53. package/kits/base/CLAUDE.md +2 -3
  54. package/kits/base/castle.json +1 -1
  55. package/kits/base/sdk/README.md +258 -52
  56. package/kits/base/sdk/agent.js +4 -4
  57. package/kits/base/sdk/avatar.d.ts +12 -0
  58. package/kits/base/sdk/avatar.js +23 -0
  59. package/kits/base/sdk/castle.d.ts +5 -0
  60. package/kits/base/sdk/castle.js +3 -0
  61. package/kits/base/sdk/commands.d.ts +208 -0
  62. package/kits/base/sdk/commands.js +16 -0
  63. package/kits/base/sdk/context.d.ts +6 -0
  64. package/kits/base/sdk/experiments.d.ts +15 -0
  65. package/kits/base/sdk/experiments.js +83 -0
  66. package/kits/base/sdk/hub.d.ts +85 -0
  67. package/kits/base/sdk/hub.js +107 -0
  68. package/kits/base/sdk/multiplayer.d.ts +45 -1
  69. package/kits/base/sdk/multiplayer.js +106 -4
  70. package/kits/base/sdk/server/hub.d.ts +169 -0
  71. package/kits/base/sdk/server/hub.js +125 -0
  72. package/kits/base/sdk/server/platformHandle.d.ts +42 -1
  73. package/kits/base/sdk/server/storage.d.ts +55 -2
  74. package/kits/base/sdk/server/wrapper.d.ts +32 -2
  75. package/kits/base/sdk/server/wrapper.js +90 -11
  76. package/kits/base/sdk/transport.d.ts +2 -1
  77. package/kits/base/sdk/transport.js +13 -1
  78. package/kits/hub-game/.prettierrc +5 -0
  79. package/kits/hub-game/CLAUDE.md +146 -0
  80. package/kits/hub-game/blueprints/avatar-0.scene +28 -0
  81. package/kits/hub-game/blueprints/avatar-1.scene +28 -0
  82. package/kits/hub-game/blueprints/avatar-2.scene +28 -0
  83. package/kits/hub-game/blueprints/avatar-3.scene +28 -0
  84. package/kits/hub-game/blueprints/bean/avatar-0.scene +28 -0
  85. package/kits/hub-game/blueprints/bean/avatar-1.scene +28 -0
  86. package/kits/hub-game/blueprints/bean/avatar-2.scene +28 -0
  87. package/kits/hub-game/blueprints/bean/avatar-3.scene +28 -0
  88. package/kits/hub-game/blueprints/blocky/avatar-0.scene +28 -0
  89. package/kits/hub-game/blueprints/blocky/avatar-1.scene +28 -0
  90. package/kits/hub-game/blueprints/blocky/avatar-2.scene +28 -0
  91. package/kits/hub-game/blueprints/blocky/avatar-3.scene +28 -0
  92. package/kits/hub-game/blueprints/pixel/avatar-0.scene +28 -0
  93. package/kits/hub-game/blueprints/pixel/avatar-1.scene +28 -0
  94. package/kits/hub-game/blueprints/pixel/avatar-2.scene +28 -0
  95. package/kits/hub-game/blueprints/pixel/avatar-3.scene +28 -0
  96. package/kits/hub-game/castle.json +96 -0
  97. package/kits/hub-game/code/client/attention.js +133 -0
  98. package/kits/hub-game/code/client/avatar.js +288 -0
  99. package/kits/hub-game/code/client/cabinets.js +90 -0
  100. package/kits/hub-game/code/client/chat.js +135 -0
  101. package/kits/hub-game/code/client/chrome.js +1001 -0
  102. package/kits/hub-game/code/client/doorFrame.js +15 -0
  103. package/kits/hub-game/code/client/firstJoin.js +14 -0
  104. package/kits/hub-game/code/client/grab.js +241 -0
  105. package/kits/hub-game/code/client/hookLayer.js +230 -0
  106. package/kits/hub-game/code/client/idle.js +326 -0
  107. package/kits/hub-game/code/client/keys.js +48 -0
  108. package/kits/hub-game/code/client/pack.js +232 -0
  109. package/kits/hub-game/code/client/packFormat.js +397 -0
  110. package/kits/hub-game/code/client/say.js +127 -0
  111. package/kits/hub-game/code/client/sha256.js +80 -0
  112. package/kits/hub-game/code/client/sound.js +147 -0
  113. package/kits/hub-game/code/client/state.js +131 -0
  114. package/kits/hub-game/code/client/talk.js +259 -0
  115. package/kits/hub-game/code/client/together.js +365 -0
  116. package/kits/hub-game/code/client/wear.js +149 -0
  117. package/kits/hub-game/code/server/actors.js +627 -0
  118. package/kits/hub-game/code/server/blocks.js +103 -0
  119. package/kits/hub-game/code/server/cabinets.js +309 -0
  120. package/kits/hub-game/code/server/game.js +137 -0
  121. package/kits/hub-game/code/server/grab.js +247 -0
  122. package/kits/hub-game/code/server/history.js +96 -0
  123. package/kits/hub-game/code/server/hub.js +1904 -0
  124. package/kits/hub-game/code/server/idle.js +412 -0
  125. package/kits/hub-game/code/server/index.js +64 -0
  126. package/kits/hub-game/code/server/labels.js +59 -0
  127. package/kits/hub-game/code/server/manifest.js +527 -0
  128. package/kits/hub-game/code/server/npcs.js +745 -0
  129. package/kits/hub-game/code/server/party.js +381 -0
  130. package/kits/hub-game/code/server/place.js +43 -0
  131. package/kits/hub-game/code/server/say.js +83 -0
  132. package/kits/hub-game/code/server/wear.js +142 -0
  133. package/kits/hub-game/code/systems/hub.js +2230 -0
  134. package/kits/hub-game/docs/content-pack.md +151 -0
  135. package/kits/hub-game/docs/contract.md +1139 -0
  136. package/kits/hub-game/docs/style.md +137 -0
  137. package/kits/hub-game/drawings/blocky/castle-eye.sprite +58 -0
  138. package/kits/hub-game/drawings/blocky/dash.sprite +59 -0
  139. package/kits/hub-game/drawings/blocky/pom.sprite +59 -0
  140. package/kits/hub-game/drawings/blocky/tabby.sprite +59 -0
  141. package/kits/hub-game/drawings/pixel/castle-arm.sprite +13 -0
  142. package/kits/hub-game/drawings/pixel/castle-body.sprite +23 -0
  143. package/kits/hub-game/drawings/pixel/castle-leg.sprite +12 -0
  144. package/kits/hub-game/drawings/pixel/kai-arm.sprite +14 -0
  145. package/kits/hub-game/drawings/pixel/kai-head.sprite +24 -0
  146. package/kits/hub-game/drawings/pixel/kai-leg.sprite +15 -0
  147. package/kits/hub-game/drawings/pixel/kai-torso.sprite +18 -0
  148. package/kits/hub-game/drawings/pixel/mimi-arm.sprite +13 -0
  149. package/kits/hub-game/drawings/pixel/mimi-head.sprite +23 -0
  150. package/kits/hub-game/drawings/pixel/mimi-leg.sprite +15 -0
  151. package/kits/hub-game/drawings/pixel/mimi-torso.sprite +18 -0
  152. package/kits/hub-game/drawings/pixel/nox-arm.sprite +13 -0
  153. package/kits/hub-game/drawings/pixel/nox-head.sprite +21 -0
  154. package/kits/hub-game/drawings/pixel/nox-leg.sprite +14 -0
  155. package/kits/hub-game/drawings/pixel/nox-torso.sprite +18 -0
  156. package/kits/hub-game/eslint.config.js +20 -0
  157. package/kits/hub-game/fonts.generated.js +9 -0
  158. package/kits/hub-game/index.html +22 -0
  159. package/kits/hub-game/main.jsx +6 -0
  160. package/kits/hub-game/models/avatar-0.pxmodel +21 -0
  161. package/kits/hub-game/models/avatar-1.pxmodel +24 -0
  162. package/kits/hub-game/models/avatar-2.pxmodel +27 -0
  163. package/kits/hub-game/models/avatar-3.pxmodel +22 -0
  164. package/kits/hub-game/models/bean/avatar-0.pxmodel +19 -0
  165. package/kits/hub-game/models/bean/avatar-1.pxmodel +24 -0
  166. package/kits/hub-game/models/bean/avatar-2.pxmodel +22 -0
  167. package/kits/hub-game/models/bean/avatar-3.pxmodel +24 -0
  168. package/kits/hub-game/models/blocky/avatar-0.pxmodel +21 -0
  169. package/kits/hub-game/models/blocky/avatar-1.pxmodel +30 -0
  170. package/kits/hub-game/models/blocky/avatar-2.pxmodel +28 -0
  171. package/kits/hub-game/models/blocky/avatar-3.pxmodel +27 -0
  172. package/kits/hub-game/models/pixel/avatar-0.pxmodel +24 -0
  173. package/kits/hub-game/models/pixel/avatar-1.pxmodel +16 -0
  174. package/kits/hub-game/models/pixel/avatar-2.pxmodel +19 -0
  175. package/kits/hub-game/models/pixel/avatar-3.pxmodel +23 -0
  176. package/kits/hub-game/package-lock.json +365 -0
  177. package/kits/hub-game/package.json +32 -0
  178. package/kits/hub-game/scenes/main.scene +318 -0
  179. package/kits/hub-game/scripts/check.mjs +339 -0
  180. package/kits/hub-game/scripts/fixtures/sdk-stub.mjs +3 -0
  181. package/kits/hub-game/scripts/gen-avatars-bean.mjs +748 -0
  182. package/kits/hub-game/scripts/gen-avatars-blocky.mjs +607 -0
  183. package/kits/hub-game/scripts/gen-avatars-pixel.mjs +771 -0
  184. package/kits/hub-game/scripts/gen-avatars.mjs +621 -0
  185. package/kits/hub-game/scripts/pack.mjs +192 -0
  186. package/kits/hub-game/scripts/test-actors.mjs +666 -0
  187. package/kits/hub-game/scripts/test-attention.mjs +103 -0
  188. package/kits/hub-game/scripts/test-avatar.mjs +472 -0
  189. package/kits/hub-game/scripts/test-ban-join.mjs +194 -0
  190. package/kits/hub-game/scripts/test-blocks.mjs +351 -0
  191. package/kits/hub-game/scripts/test-chat.mjs +98 -0
  192. package/kits/hub-game/scripts/test-classic.mjs +590 -0
  193. package/kits/hub-game/scripts/test-door-jump.mjs +131 -0
  194. package/kits/hub-game/scripts/test-door.mjs +56 -0
  195. package/kits/hub-game/scripts/test-grab.mjs +331 -0
  196. package/kits/hub-game/scripts/test-hub-ops.mjs +835 -0
  197. package/kits/hub-game/scripts/test-hub-trip.mjs +1310 -0
  198. package/kits/hub-game/scripts/test-idle.mjs +518 -0
  199. package/kits/hub-game/scripts/test-interest.mjs +413 -0
  200. package/kits/hub-game/scripts/test-keys.mjs +50 -0
  201. package/kits/hub-game/scripts/test-manifest.mjs +224 -0
  202. package/kits/hub-game/scripts/test-no-forms.mjs +16 -0
  203. package/kits/hub-game/scripts/test-npcs.mjs +714 -0
  204. package/kits/hub-game/scripts/test-pack.mjs +424 -0
  205. package/kits/hub-game/scripts/test-party.mjs +286 -0
  206. package/kits/hub-game/scripts/test-say.mjs +149 -0
  207. package/kits/hub-game/scripts/test-together.mjs +183 -0
  208. package/kits/hub-game/theme.style +3 -0
  209. package/kits/multiplayer-3d/castle.json +5 -5
  210. package/kits/multiplayer-3d/code/client/avatars.js +101 -1
  211. package/kits/multiplayer-3d/code/server/players.js +4 -1
  212. package/kits/multiplayer-3d/code/server/sceneBodies.js +27 -6
  213. package/kits/multiplayer-3d/code/server/world.js +20 -7
  214. package/kits/multiplayer-3d/code/systems/multiplayer.js +222 -11
  215. package/kits/physics-2d/castle.json +2 -2
  216. package/kits/physics-2d/engine/art.js +13 -0
  217. package/kits/physics-2d/engine/assets.js +4 -2
  218. package/kits/physics-2d/engine/scene.js +25 -5
  219. package/kits/physics-3d/CLAUDE.md +6 -1
  220. package/kits/physics-3d/castle.json +3 -3
  221. package/kits/physics-3d/engine3d/Scene3DPlayer.jsx +32 -4
  222. package/kits/physics-3d/engine3d/artobjects.js +2 -2
  223. package/kits/physics-3d/engine3d/materials.js +29 -1
  224. package/kits/physics-3d/engine3d/world3d.js +65 -1
  225. package/kits/physics-3d/systems/physics3d.js +5 -2
  226. package/kits/real-time/castle.json +2 -2
  227. package/kits/real-time/code/client/connection.js +77 -7
  228. package/kits/real-time/code/client/gameHooks.js +63 -3
  229. package/kits/real-time/code/client/messages.js +7 -0
  230. package/kits/real-time/code/server/gameHooks.js +24 -0
  231. package/kits/real-time/code/server/session.js +390 -22
  232. package/package.json +2 -2
@@ -19,6 +19,10 @@ const SAVE_INTERVAL_MS = 10000;
19
19
  // scene's spawn.
20
20
  const RESUME_WAIT_MS = 3000;
21
21
 
22
+ // How long a session whose deck takes its scene from the client waits for one
23
+ // to be named before building the default scene.
24
+ const SCENE_WAIT_MS = 3000;
25
+
22
26
  // A player whose last report is older than this has gone: a closed tab whose
23
27
  // connection the platform has not reaped yet.
24
28
  const GHOST_MS = 2000;
@@ -55,6 +59,16 @@ const GHOST_MS = 2000;
55
59
  // handovers ownership changes since the last broadcast, as
56
60
  // [objectId, playerId|0, ...], the rest of each row the
57
61
  // kit's own
62
+ // known playerId -> the other players that client has been sent
63
+ // and not told to drop, for a deck whose hooks filter by
64
+ // interest (`game.interest`); empty otherwise
65
+ // dirty playerId -> other players whose row changed since that
66
+ // client was last sent rows, while rows go out only every
67
+ // `game.poseEvery` ticks (filtered sends only)
68
+ // shortIds playerId -> the small number filtered rows carry instead
69
+ // of the id; `mapped` is, per client, the numbers it has
70
+ // been told (in `idm`), and `filterTick` counts filtered
71
+ // sends for the `poseEvery` phase
58
72
  function newServer(parts) {
59
73
  return {
60
74
  ...parts,
@@ -72,8 +86,15 @@ function newServer(parts) {
72
86
  saving: null,
73
87
  lastTickAt: 0,
74
88
  sent: { players: new Map(), objects: new Map() },
89
+ sceneTimer: null,
75
90
  waitingForWorld: new Set(),
76
91
  handovers: [],
92
+ known: new Map(),
93
+ dirty: new Map(),
94
+ shortIds: new Map(),
95
+ nextShortId: 1,
96
+ mapped: new Map(),
97
+ filterTick: 0,
77
98
  };
78
99
  }
79
100
 
@@ -87,11 +108,13 @@ export function makeSession(parts) {
87
108
 
88
109
  // Castle's handlers remain available while `createSimulation` is pending. A
89
110
  // build that fails is logged here; `buildWorld` sees the same rejection when
90
- // the first tick awaits it.
91
- server.building = server.world.createSimulation();
92
- server.building.catch((err) => {
93
- console.error(`[${server.label}] simulation failed to build: ${err?.stack ?? err}`);
94
- });
111
+ // the first tick awaits it. A deck whose hooks set `sceneFromClient` builds
112
+ // nothing yet: its first player names the scene (`chooseScene`). Nor does one
113
+ // whose hooks name the scene from the session (`sceneFor`), which is not
114
+ // known until the first callback carries it (`attach`).
115
+ if (!server.game.sceneFromClient && !server.game.sceneFor) {
116
+ startBuilding(server, undefined);
117
+ }
95
118
  return {
96
119
  // The world is built from the first tick or join, not here: `onStart` runs
97
120
  // while the process is still booting, and a storage read made then can go
@@ -102,6 +125,7 @@ export function makeSession(parts) {
102
125
  // full session roster on the next tick.
103
126
  onPlayerJoin(session, player) {
104
127
  attach(server, session);
128
+ awaitScene(server);
105
129
  console.log(`[${server.label}] join ${player.username} (${player.playerId})`);
106
130
  if (server.unloaded) {
107
131
  session.send(player.playerId, { k: Messages.UNLOADED });
@@ -111,6 +135,16 @@ export function makeSession(parts) {
111
135
  // The saved pose is read now, so the body this player is given on a later
112
136
  // tick can start there.
113
137
  beginResume(server, player);
138
+
139
+ // A filtering deck's client that comes back to a body it still has (a
140
+ // reload that resumed its player id) is a fresh page: it is sent the
141
+ // world again, and starts over on what it has been sent.
142
+ if (server.game.interest && server.sim?.players.has(player.playerId)) {
143
+ server.waitingForWorld.add(player.playerId);
144
+ server.known.delete(player.playerId);
145
+ server.dirty.delete(player.playerId);
146
+ server.mapped.delete(player.playerId);
147
+ }
114
148
  },
115
149
 
116
150
  onPlayerLeave(session, player) {
@@ -134,11 +168,38 @@ export function makeSession(parts) {
134
168
  },
135
169
 
136
170
  async onShutdown() {
171
+ // The game's own last word (the hub kit's `shutdown`: pending leaves
172
+ // and draining their saves), then the room.
173
+ await server.game.shutdown?.(server.sim);
137
174
  await saveRoom(server);
138
175
  },
139
176
  };
140
177
  }
141
178
 
179
+ // Start creating the simulation from one of the deck's scenes, `undefined`
180
+ // being its main scene.
181
+ function startBuilding(server, scenePath) {
182
+ server.building = server.world.createSimulation(scenePath);
183
+ server.building.catch((err) => {
184
+ console.error(`[${server.label}] simulation failed to build: ${err?.stack ?? err}`);
185
+ });
186
+ }
187
+
188
+ // The scene a client named, for a deck that takes it from the client. The first
189
+ // one decides; the world refuses a path the deck does not have, and that
190
+ // session builds its main scene instead.
191
+ function chooseScene(server, path) {
192
+ const named = typeof path === 'string' && path.length <= 256;
193
+ if (server.building || (path !== undefined && !named)) {
194
+ return;
195
+ }
196
+ console.log(`[${server.label}] scene ${path ?? 'main'}`);
197
+ startBuilding(server, path);
198
+ if (server.session) {
199
+ buildWorld(server);
200
+ }
201
+ }
202
+
142
203
  // Keep the first session a tick or join carries and build the world with it.
143
204
  // The room the session persists under is not known before this.
144
205
  function attach(server, session) {
@@ -147,7 +208,36 @@ function attach(server, session) {
147
208
  }
148
209
  server.session = session;
149
210
  server.room = Persist.roomOf(session);
150
- buildWorld(server);
211
+ if (!server.building && server.game.sceneFor) {
212
+ // The place a session runs is the session's own -- the host stamps it, or
213
+ // the dev serve's session id carries it -- and never any client's to name.
214
+ let scenePath;
215
+ try {
216
+ scenePath = server.game.sceneFor(session) ?? undefined;
217
+ } catch (err) {
218
+ console.error(`[${server.label}] sceneFor failed: ${err?.stack ?? err}`);
219
+ }
220
+ console.log(`[${server.label}] scene ${scenePath ?? 'main'} (from the session)`);
221
+ startBuilding(server, scenePath);
222
+ }
223
+ if (server.building) {
224
+ buildWorld(server);
225
+ }
226
+ }
227
+
228
+ // A session still waiting for its scene builds the main one if its first
229
+ // player names none in time. The wait starts at that join, not at the first
230
+ // tick: an empty session ticks before anyone has had the chance.
231
+ function awaitScene(server) {
232
+ if (server.building || server.sceneTimer || !server.game.sceneFromClient) {
233
+ return;
234
+ }
235
+ server.sceneTimer = setTimeout(() => {
236
+ if (!server.building) {
237
+ console.warn(`[${server.label}] no scene named in ${SCENE_WAIT_MS} ms; building the main one`);
238
+ chooseScene(server, undefined);
239
+ }
240
+ }, SCENE_WAIT_MS);
151
241
  }
152
242
 
153
243
  // Finish the simulation: read the saved room, run the deck's restore and ready
@@ -159,7 +249,9 @@ function attach(server, session) {
159
249
  async function buildWorld(server) {
160
250
  try {
161
251
  const created = await server.building;
162
- const saved = await Persist.loadRoom(server.session, server.room);
252
+ const saved = persists(server)
253
+ ? await Persist.loadRoom(server.session, server.room)
254
+ : { world: null, game: null };
163
255
  if (!saved) {
164
256
  server.unloaded = true;
165
257
  server.session.broadcast({ k: Messages.UNLOADED });
@@ -201,6 +293,12 @@ function receive(server, session, player, data) {
201
293
  markGone(server, data.id);
202
294
  return;
203
295
  }
296
+ // Only a deck that opted in has the kind intercepted; any other deck's own
297
+ // `k: 'c'` messages still reach its hook.
298
+ if (data?.k === Messages.SCENE && server.game.sceneFromClient) {
299
+ chooseScene(server, data.path);
300
+ return;
301
+ }
204
302
 
205
303
  // Deck control messages can run before simulation initialization completes.
206
304
  if (data?.k !== Messages.MOVE) {
@@ -264,6 +362,10 @@ function tick(server, session) {
264
362
  os: server.world.objectPoses(server.sim),
265
363
  };
266
364
 
365
+ // Which players each client is sent, from a deck that filters by interest;
366
+ // null sends everyone everything, as one broadcast.
367
+ const sees = interestOf(server);
368
+
267
369
  // A new player gets the whole world and the current ownerships. Sending the
268
370
  // ownerships with that first snapshot stops the client from simulating an
269
371
  // object somebody else already holds. The kit's `ownerships` rows may carry
@@ -276,6 +378,7 @@ function tick(server, session) {
276
378
  session.send(playerId, {
277
379
  k: Messages.WORLD,
278
380
  ...snapshot,
381
+ ...(sees ? worldRows(server, sees, playerId, snapshot.ps) : {}),
279
382
  own: server.ownership.ownerships(server.sim),
280
383
  gone: [...server.gone],
281
384
  ...(state === null ? {} : { me: state }),
@@ -283,10 +386,129 @@ function tick(server, session) {
283
386
  });
284
387
  }
285
388
  server.waitingForWorld.clear();
286
- broadcastDelta(server, session, snapshot);
389
+ broadcastDelta(server, session, snapshot, sees);
287
390
  maybeSave(server);
288
391
  }
289
392
 
393
+ // The deck's interest sets for this tick, or null for a deck without the hook.
394
+ // A hook that returns nothing or throws has every player see every other this
395
+ // tick -- through the filtered path, so clients told a player is `out` are
396
+ // sent it again with `in` -- rather than stopping the session.
397
+ function interestOf(server) {
398
+ if (!server.game.interest) {
399
+ return null;
400
+ }
401
+ try {
402
+ const sees = server.game.interest(server.sim, server.known);
403
+ if (sees) {
404
+ return sees;
405
+ }
406
+ } catch (err) {
407
+ console.error(`[${server.label}] interest failed: ${err?.stack ?? err}`);
408
+ }
409
+ const ids = [...server.sim.players.keys()];
410
+ const hiddenPair = server.session?.hub?.hiddenPair;
411
+ if (typeof hiddenPair !== 'function') {
412
+ return new Map(ids.map((id) => [id, new Set(ids)]));
413
+ }
414
+ return everyoneButHidden(server, ids, hiddenPair);
415
+ }
416
+
417
+ // That fallback in a hub session, whose host hides the two players of a block
418
+ // from each other (`session.hub.hiddenPair`): everyone but them. A pair it
419
+ // cannot answer for is hidden too, so no error path shows one to the other.
420
+ function everyoneButHidden(server, ids, hiddenPair) {
421
+ const sees = new Map(ids.map((id) => [id, new Set(ids)]));
422
+ let failed = false;
423
+ for (let i = 0; i < ids.length; i += 1) {
424
+ for (let j = i + 1; j < ids.length; j += 1) {
425
+ let hidden = true;
426
+ try {
427
+ hidden = Boolean(hiddenPair.call(server.session.hub, ids[i], ids[j]));
428
+ } catch (err) {
429
+ if (!failed) {
430
+ failed = true;
431
+ console.error(`[${server.label}] hiddenPair failed: ${err?.stack ?? err}`);
432
+ }
433
+ }
434
+ if (hidden) {
435
+ sees.get(ids[i]).delete(ids[j]);
436
+ sees.get(ids[j]).delete(ids[i]);
437
+ }
438
+ }
439
+ }
440
+ return sees;
441
+ }
442
+
443
+ // The player rows a joiner's WORLD snapshot carries when the deck filters:
444
+ // their own and the players they see, which become what they know.
445
+ function worldPoses(server, sees, playerId, ps) {
446
+ const visible = sees.get(playerId) ?? new Set();
447
+ const rows = ps.filter(([id]) => id === playerId || visible.has(id));
448
+ server.known.set(playerId, new Set(rows.map(([id]) => id).filter((id) => id !== playerId)));
449
+ server.dirty.delete(playerId);
450
+ server.mapped.delete(playerId);
451
+ return rows;
452
+ }
453
+
454
+ // A joiner's WORLD rows when the deck filters: theirs and the players they
455
+ // see, short-numbered where the deck sends rows every few ticks, with the
456
+ // row interval the client draws others behind by (`pi`).
457
+ function worldRows(server, sees, playerId, ps) {
458
+ const visible = worldPoses(server, sees, playerId, ps);
459
+ // A client that sees everyone gets the rows a session that does not filter
460
+ // would send; one that does not is in a filtering town.
461
+ if (visible.length >= ps.length) {
462
+ return { ps: visible };
463
+ }
464
+ const { rows, idm } = shortRows(server, playerId, visible);
465
+ return { ps: rows, ...(idm ? { idm, pi: server.game.poseIntervalMs ?? 0 } : {}) };
466
+ }
467
+
468
+ // Rows with each player's id replaced by its short number, and `idm`, the
469
+ // numbers this client has not been told yet as `[[n, playerId], ...]` -- for
470
+ // a deck that sends rows only every few ticks (`game.poseEvery`), whose
471
+ // clients are many. Any other deck's rows carry the ids as they always have.
472
+ function shortRows(server, clientId, rows) {
473
+ if (!(server.game.poseEvery > 1)) {
474
+ return { rows };
475
+ }
476
+ let mapped = server.mapped.get(clientId);
477
+ if (!mapped) {
478
+ server.mapped.set(clientId, (mapped = new Set()));
479
+ }
480
+ const idm = [];
481
+ const out = rows.map(([id, ...rest]) => {
482
+ let n = server.shortIds.get(id);
483
+ if (n === undefined) {
484
+ n = server.nextShortId;
485
+ server.nextShortId += 1;
486
+ server.shortIds.set(id, n);
487
+ }
488
+ if (!mapped.has(n)) {
489
+ mapped.add(n);
490
+ idm.push([n, id]);
491
+ }
492
+ return [n, ...rest];
493
+ });
494
+ return { rows: out, idm };
495
+ }
496
+
497
+ // Whether this filtered send carries a client's rows of other players: every
498
+ // tick, or every `game.poseEvery` ticks, the clients spread over the ticks
499
+ // between.
500
+ function rowTick(server, clientId) {
501
+ const every = Math.floor(server.game.poseEvery ?? 1);
502
+ if (!(every > 1)) {
503
+ return true;
504
+ }
505
+ let hash = 0;
506
+ for (let i = 0; i < clientId.length; i += 1) {
507
+ hash = (hash * 31 + clientId.charCodeAt(i)) >>> 0;
508
+ }
509
+ return (server.filterTick + hash) % every === 0;
510
+ }
511
+
290
512
  // Make the simulation's player map match Castle's roster. The roster covers the
291
513
  // connection changes that do not arrive as a join or a leave.
292
514
  function reconcilePlayers(server, session) {
@@ -310,6 +532,20 @@ function reconcilePlayers(server, session) {
310
532
 
311
533
  // Remove the delta baseline so a later return sends an initial player pose.
312
534
  server.sent.players.delete(playerId);
535
+ server.known.delete(playerId);
536
+ for (const known of server.known.values()) {
537
+ known.delete(playerId);
538
+ }
539
+ server.dirty.delete(playerId);
540
+ for (const dirty of server.dirty.values()) {
541
+ dirty.delete(playerId);
542
+ }
543
+ const short = server.shortIds.get(playerId);
544
+ server.shortIds.delete(playerId);
545
+ server.mapped.delete(playerId);
546
+ for (const mapped of server.mapped.values()) {
547
+ mapped.delete(short);
548
+ }
313
549
  }
314
550
  }
315
551
 
@@ -370,7 +606,7 @@ function resumeSaved(server, playerId, entry) {
370
606
 
371
607
  // Start reading a joining player's saved row.
372
608
  function beginResume(server, player) {
373
- if (!player.userId) {
609
+ if (!player.userId || !persists(server)) {
374
610
  return;
375
611
  }
376
612
  const entry = {
@@ -391,6 +627,9 @@ function beginResume(server, player) {
391
627
  // nothing: the newer body's pose is the one to keep.
392
628
  function savePlayer(server, player) {
393
629
  server.joining.delete(player.playerId);
630
+ if (!persists(server)) {
631
+ return;
632
+ }
394
633
  const holder = server.resumedUsers.get(player.userId);
395
634
  if (holder && holder !== player.playerId) {
396
635
  return;
@@ -404,6 +643,13 @@ function savePlayer(server, player) {
404
643
  void Persist.savePlayer(server.session, server.room, player.userId, { pose, state });
405
644
  }
406
645
 
646
+ // Whether this session reads and writes its room. A deck whose hooks set
647
+ // `persist: false` keeps nothing: a game instance minted per door trip would
648
+ // otherwise leave a room of rows in deck storage behind every visit.
649
+ function persists(server) {
650
+ return server.game.persist !== false;
651
+ }
652
+
407
653
  // Write the world every `SAVE_INTERVAL_MS`, and only when a pose moved since the
408
654
  // last write.
409
655
  function maybeSave(server) {
@@ -419,7 +665,7 @@ function maybeSave(server) {
419
665
  // flight is waited for, so a shutdown during the timer's write still lands its
420
666
  // own.
421
667
  async function saveRoom(server) {
422
- if (!server.sim || !server.session) {
668
+ if (!server.sim || !server.session || !persists(server)) {
423
669
  return;
424
670
  }
425
671
  if (server.saving) {
@@ -467,8 +713,12 @@ function poseOf(server, playerId) {
467
713
  }
468
714
 
469
715
  // Broadcast what changed since the previous tick. A tick where nothing changed
470
- // sends nothing at all.
471
- function broadcastDelta(server, session, snapshot) {
716
+ // sends nothing at all. With interest sets (`sees`), each client is sent only
717
+ // the players it sees; see `sendFiltered`.
718
+ function broadcastDelta(server, session, snapshot, sees) {
719
+ // Players with no baseline yet are new to the session, and so to everyone.
720
+ const fresh = sees ? new Set(snapshot.ps.map(([id]) => id).filter((id) => !server.sent.players.has(id))) : null;
721
+
472
722
  // `playerChanged` records each accepted player pose as its next baseline.
473
723
  const ps = snapshot.ps.filter((pose) => playerChanged(server, pose));
474
724
 
@@ -501,6 +751,22 @@ function broadcastDelta(server, session, snapshot) {
501
751
  // A changed deck block is its own reason to broadcast, including on a tick
502
752
  // where no pose and no ownership moved.
503
753
  const block = server.game.delta?.(server.sim) ?? null;
754
+ const message = (rows) => ({
755
+ k: Messages.STATE,
756
+ t: snapshot.t,
757
+ ps: rows,
758
+ os,
759
+ own,
760
+ ...(gone.length ? { gone } : {}),
761
+ ...(ack.length ? { ack } : {}),
762
+ ...block,
763
+ });
764
+
765
+ // Objects, handovers, removals, acks and the deck's block go to everyone.
766
+ const common = os.length > 0 || own.length > 0 || gone.length > 0 || ack.length > 0 || Boolean(block);
767
+ if (sees && sendFiltered(server, session, snapshot, sees, ps, fresh, message, common)) {
768
+ return;
769
+ }
504
770
  if (
505
771
  ps.length === 0 &&
506
772
  os.length === 0 &&
@@ -511,16 +777,118 @@ function broadcastDelta(server, session, snapshot) {
511
777
  ) {
512
778
  return;
513
779
  }
514
- session.broadcast({
515
- k: Messages.STATE,
516
- t: snapshot.t,
517
- ps,
518
- os,
519
- own,
520
- ...(gone.length ? { gone } : {}),
521
- ...(ack.length ? { ack } : {}),
522
- ...block,
523
- });
780
+ session.broadcast(message(ps));
781
+ }
782
+
783
+ // Send each client the players it sees: the changed rows of those it already
784
+ // knows, the whole current row of each it starts to see (listed in `in`), and
785
+ // `out`, the ones it stops seeing, which it stops drawing. Everything else in
786
+ // the delta is the same for all. Returns false, sending nothing, when every
787
+ // client would get exactly the broadcast -- a session no bigger than the
788
+ // interest set -- so the caller broadcasts it once instead. A roster member
789
+ // still waiting for its body would get that broadcast too, so while one is
790
+ // waiting each admitted client is sent its own copy.
791
+ function sendFiltered(server, session, snapshot, sees, ps, fresh, message, common) {
792
+ const current = new Map(snapshot.ps.map((row) => [row[0], row]));
793
+ const changedIds = new Set(ps.map(([id]) => id));
794
+ const plans = [];
795
+ let differs = false;
796
+ for (const playerId of server.sim.players.keys()) {
797
+ // A player admitted while the hook returned null was sent everyone.
798
+ const known = server.known.get(playerId) ?? new Set([...current.keys()].filter((id) => id !== playerId));
799
+ const visible = sees.get(playerId) ?? new Set();
800
+ const rows = [];
801
+ const entered = [];
802
+ for (const id of visible) {
803
+ if (id === playerId || !current.has(id)) {
804
+ continue;
805
+ }
806
+ if (!known.has(id)) {
807
+ rows.push(current.get(id));
808
+ entered.push(id);
809
+ } else if (changedIds.has(id)) {
810
+ rows.push(current.get(id));
811
+ }
812
+ }
813
+ if (changedIds.has(playerId)) {
814
+ rows.push(current.get(playerId));
815
+ }
816
+ const left = [...known].filter((id) => !visible.has(id) && current.has(id));
817
+ // The global baseline already counts a newcomer as changed, so a client
818
+ // that sees everyone gets exactly `ps`: as many rows, each a changed one.
819
+ // A player it starts seeing again after an `out` needs its `in`, which a
820
+ // broadcast does not carry; a newcomer is drawn as it always was.
821
+ differs ||=
822
+ left.length > 0 ||
823
+ rows.length !== ps.length ||
824
+ rows.some(([id]) => !changedIds.has(id)) ||
825
+ entered.some((id) => !fresh.has(id));
826
+ plans.push({ playerId, visible, rows, entered, left });
827
+ }
828
+ const waiting = session.players.some((player) => !server.sim.players.has(player.playerId));
829
+ // A client owed rows it was not sent yet must get them before a broadcast,
830
+ // which carries only this tick's changes.
831
+ const owed = [...server.dirty.values()].some((dirty) => dirty.size > 0);
832
+ if (!differs && !waiting && !owed) {
833
+ for (const plan of plans) {
834
+ server.known.set(plan.playerId, new Set([...plan.visible].filter((id) => id !== plan.playerId && current.has(id))));
835
+ }
836
+ return false;
837
+ }
838
+ server.filterTick += 1;
839
+ for (const { playerId, visible, rows, entered, left } of plans) {
840
+ server.known.set(playerId, new Set([...visible].filter((id) => id !== playerId && current.has(id))));
841
+ const sent = server.game.poseEvery > 1 ? rowsDue(server, playerId, visible, rows, entered, current) : rows;
842
+ if (sent.length === 0 && left.length === 0 && !common) {
843
+ continue;
844
+ }
845
+ const { rows: wire, idm } = shortRows(server, playerId, sent);
846
+ session.send(playerId, {
847
+ ...message(wire),
848
+ ...(idm?.length ? { idm } : {}),
849
+ ...(idm && wire.length ? { pi: server.game.poseIntervalMs ?? 0 } : {}),
850
+ ...(entered.length ? { in: entered } : {}),
851
+ ...(left.length ? { out: left } : {}),
852
+ });
853
+ }
854
+
855
+ // A client still waiting for its body is sent the world when it gets one,
856
+ // and nothing before.
857
+ return true;
858
+ }
859
+
860
+ // The rows a client is sent this tick when rows of others go out only every
861
+ // `game.poseEvery` ticks: its own when it is a correction (a clamp is never
862
+ // late), and any player it starts seeing (`in` carries the whole row), at once; every other changed row
863
+ // is remembered and sent, at its newest, on the client's next row tick.
864
+ function rowsDue(server, playerId, visible, rows, entered, current) {
865
+ let dirty = server.dirty.get(playerId);
866
+ if (!dirty) {
867
+ server.dirty.set(playerId, (dirty = new Set()));
868
+ }
869
+ const now = new Set(entered);
870
+ const out = [];
871
+ for (const row of rows) {
872
+ if (row[0] === playerId) {
873
+ // Its own row is read only when the server corrected it (`clamped`).
874
+ if (row[1 + server.poses.PLAYER_GEOMETRY]) {
875
+ out.push(row);
876
+ }
877
+ } else if (now.has(row[0])) {
878
+ out.push(row);
879
+ } else {
880
+ dirty.add(row[0]);
881
+ }
882
+ }
883
+ if (rowTick(server, playerId)) {
884
+ for (const id of dirty) {
885
+ if (visible.has(id) && current.has(id)) {
886
+ out.push(current.get(id));
887
+ }
888
+ }
889
+ dirty.clear();
890
+ }
891
+ return out;
524
892
  }
525
893
 
526
894
  // Whether one player's pose belongs in this delta, recording it as the new
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "castle-web-cli",
3
- "version": "0.4.229",
3
+ "version": "0.4.230",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/castle-xyz/castle-experimental-web.git"
@@ -12,7 +12,7 @@
12
12
  "scripts": {
13
13
  "build": "rm -rf dist && tsc && chmod +x dist/index.js && vite build && vite build -c vite.cover-bridge.config.ts && cp -R src/castle-host dist/castle-host && chmod -R u+w kits 2>/dev/null; rm -rf kits && cp -R ../kits kits && chmod -R u+w kits && rm -rf kits/*/imports",
14
14
  "dev": "tsc --watch",
15
- "check": "node ../scripts/sync-shared.mjs --check && node ../scripts/tests/icon-registry.mjs && node ../scripts/tests/sandbox-graphql-fields.mjs && node ../scripts/tests/kit-package-json.mjs && node ../scripts/tests/kit-catalog.mjs && node ../scripts/tests/import-deck-ref.mjs && node ../scripts/tests/save-failure-reason.mjs && node ../scripts/tests/flow-draft-status.mjs && node ../scripts/tests/router-fences.mjs && node ../scripts/tests/held-tasks.mjs && node ../scripts/tests/held-task-prompts.mjs && node ../scripts/tests/router-stops.mjs && node ../scripts/tests/task-steer.mjs && node ../scripts/tests/task-steer-store.mjs && node ../scripts/tests/router-task-endings.mjs && node ../scripts/tests/task-resume.mjs && node ../scripts/tests/task-model-request.mjs && node ../scripts/tests/plan-answered-questions.mjs && node ../scripts/tests/plan-no-standing-nudge.mjs && node ../scripts/tests/sdk-readme-limits.mjs && node ../scripts/tests/task-account.mjs && node ../scripts/tests/effort-support.mjs && eslint . && jscpd && tsc --noEmit && tsc --noEmit -p src/shell/tsconfig.json"
15
+ "check": "node ../scripts/sync-shared.mjs --check && node ../scripts/tests/icon-registry.mjs && node ../scripts/tests/sandbox-graphql-fields.mjs && node ../scripts/tests/kit-package-json.mjs && node ../scripts/tests/kit-catalog.mjs && node ../scripts/tests/import-deck-ref.mjs && node ../scripts/tests/save-failure-reason.mjs && node ../scripts/tests/flow-draft-status.mjs && node ../scripts/tests/router-fences.mjs && node ../scripts/tests/held-tasks.mjs && node ../scripts/tests/held-task-prompts.mjs && node ../scripts/tests/router-stops.mjs && node ../scripts/tests/task-steer.mjs && node ../scripts/tests/task-steer-store.mjs && node ../scripts/tests/router-task-endings.mjs && node ../scripts/tests/router-task-report.mjs && node ../scripts/tests/task-resume.mjs && node ../scripts/tests/task-model-request.mjs && node ../scripts/tests/plan-answered-questions.mjs && node ../scripts/tests/plan-no-standing-nudge.mjs && node ../scripts/tests/sdk-readme-limits.mjs && node ../scripts/tests/task-account.mjs && node ../scripts/tests/effort-support.mjs && node ../scripts/tests/cli-login-pkce.mjs && node ../scripts/tests/publish-timeout.mjs && eslint . && jscpd && tsc --noEmit && tsc --noEmit -p src/shell/tsconfig.json"
16
16
  },
17
17
  "jscpd": {
18
18
  "path": [