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
@@ -0,0 +1,413 @@
1
+ // Interest filtering (HUB.md "Town sessions"): the real-time session sends each
2
+ // client only the players the hub layer's `interest` hook says it sees, with
3
+ // `in` and `out`, and still broadcasts once when everybody sees everybody; a
4
+ // session whose hooks have no `interest` broadcasts exactly as before.
5
+ // `npm run check` runs it; it needs nothing installed.
6
+
7
+ import assert from 'node:assert/strict';
8
+ import { register } from 'node:module';
9
+
10
+ const sceneStub = 'export const readScene = () => [];';
11
+ register(
12
+ `data:text/javascript,${encodeURIComponent(`
13
+ export async function resolve(specifier, context, next) {
14
+ if (specifier === '@imports/castle.multiplayer-3d/code/server/sceneBodies.js') {
15
+ return { url: 'data:text/javascript,${encodeURIComponent(sceneStub)}', shortCircuit: true };
16
+ }
17
+ return next(specifier, context);
18
+ }
19
+ `)}`,
20
+ );
21
+ const { nearestPlayers } = await import('../code/server/hub.js');
22
+ const { makeSession } = await import('../../real-time/code/server/session.js');
23
+
24
+ // A one-dimensional world: each player is a point on x.
25
+ const world = {
26
+ async createSimulation() {
27
+ return { players: new Map(), objects: new Map(), timeMs: 0 };
28
+ },
29
+ step(sim) {
30
+ sim.timeMs += 33;
31
+ },
32
+ objectPoses: () => [],
33
+ restorePoses: () => 0,
34
+ };
35
+ const players = {
36
+ addPlayer(sim, id) {
37
+ sim.players.set(id, { at: { x: 0, y: 0, z: 0 } });
38
+ },
39
+ removePlayer(sim, id) {
40
+ sim.players.delete(id);
41
+ },
42
+ setPose() {},
43
+ playerPoses: (sim) => [...sim.players].map(([id, p]) => [id, p.at.x, p.at.y, p.at.z, 0, 0]),
44
+ };
45
+ const ownership = {
46
+ claim() {},
47
+ release() {},
48
+ setObjectPoses() {},
49
+ sweepOwners() {},
50
+ ownerships: () => [],
51
+ };
52
+ const poses = { PLAYER_GEOMETRY: 4, epsilonAt: () => 0.001 };
53
+
54
+ // `hub` is the host's hub ops, as a hub place's session carries them; none is
55
+ // an ordinary real-time deck (`session.hub` null).
56
+ async function room(game, ids, hub = null) {
57
+ const sent = [];
58
+ const session = {
59
+ sessionId: 'dev:test',
60
+ players: ids.map((playerId) => ({ playerId, userId: null })),
61
+ hub,
62
+ send: (to, msg) => sent.push({ to, msg }),
63
+ broadcast: (msg) => sent.push({ to: '*', msg }),
64
+ };
65
+ const server = makeSession({ label: 'test', poses, world, players, ownership, game });
66
+ server.onTick(session);
67
+ await new Promise((resolve) => setTimeout(resolve, 10));
68
+ return {
69
+ sent,
70
+ session,
71
+ tick() {
72
+ sent.length = 0;
73
+ server.onTick(session);
74
+ return sent.slice();
75
+ },
76
+ move(id, x) {
77
+ session.__sim.players.get(id).at = { x, y: 0, z: 0 };
78
+ },
79
+ rejoin(id) {
80
+ server.onPlayerJoin(session, { playerId: id, username: id, userId: null });
81
+ },
82
+ };
83
+ }
84
+
85
+ // The sim is only reachable through the hooks; `ready` hands it over.
86
+ function hooks(extra = {}) {
87
+ const game = {
88
+ persist: false,
89
+ ready(sim) {
90
+ game.sim = sim;
91
+ },
92
+ ...extra,
93
+ };
94
+ return game;
95
+ }
96
+
97
+ async function placeOf(game, ids, hub = null) {
98
+ const r = await room(game, ids, hub);
99
+ r.session.__sim = game.sim;
100
+ return r;
101
+ }
102
+
103
+ const rows = (msg) => (msg.ps ?? []).map(([id]) => id).sort();
104
+
105
+ // No hook: one broadcast a tick, no `in`/`out`, the deck as it always was.
106
+ {
107
+ const game = hooks();
108
+ const r = await placeOf(game, ['a', 'b', 'c']);
109
+ const first = r.tick();
110
+ assert.equal(first.filter((s) => s.msg.k === 'w').length, 3, 'each player gets the world');
111
+ r.move('a', 5);
112
+ const out = r.tick();
113
+ assert.deepEqual(out.map((s) => s.to), ['*']);
114
+ assert.deepEqual(rows(out[0].msg), ['a']);
115
+ assert.ok(!('in' in out[0].msg) && !('out' in out[0].msg));
116
+ }
117
+
118
+ // No hook: the STATE keys are in the order they always were.
119
+ {
120
+ const game = hooks();
121
+ const r = await placeOf(game, ['a', 'b']);
122
+ r.tick();
123
+ r.move('a', 3);
124
+ const [only] = r.tick();
125
+ assert.deepEqual(Object.keys(only.msg), ['k', 't', 'ps', 'os', 'own']);
126
+ }
127
+
128
+ // A hook that stops answering has everyone sent again with `in`, not left out.
129
+ {
130
+ let off = false;
131
+ const game = hooks({ interest: (sim, known) => (off ? null : nearestPlayers(sim, known, 1)) });
132
+ const r = await placeOf(game, ['a', 'b', 'c']);
133
+ r.tick();
134
+ r.move('a', 0);
135
+ r.move('b', 1);
136
+ r.move('c', 10);
137
+ r.tick();
138
+ off = true;
139
+ const back = r.tick();
140
+ const a = back.find((s) => s.to === 'a').msg;
141
+ assert.deepEqual(a.in, ['c'], 'a is sent c again, whole row');
142
+ assert.deepEqual(rows(a), ['c']);
143
+ off = false;
144
+ r.move('c', 10.2);
145
+ r.tick();
146
+ // Again with everyone moving, so every client's rows equal the broadcast's:
147
+ // the `in` still has to reach the client that had c out.
148
+ off = true;
149
+ r.move('a', 0.05);
150
+ r.move('b', 1.05);
151
+ r.move('c', 10.4);
152
+ const moving = r.tick();
153
+ assert.ok(moving.every((s) => s.to !== '*'), 'no broadcast while a client is owed an in');
154
+ assert.deepEqual(moving.find((s) => s.to === 'a').msg.in, ['c']);
155
+ off = false;
156
+ r.move('c', 10.6);
157
+ const again = r.tick().find((s) => s.to === 'a').msg;
158
+ assert.deepEqual(again.out, ['c'], 'and told c is out once filtering resumes');
159
+ }
160
+
161
+ // A roster member still waiting for its body is never sent a broadcast.
162
+ {
163
+ const game = hooks({ interest: (sim, known) => nearestPlayers(sim, known, 30) });
164
+ const r = await placeOf(game, ['a', 'b']);
165
+ r.tick();
166
+ r.session.players.push({ playerId: 'late', userId: null });
167
+ // Admission is immediate in this world; hold `late` back for one tick.
168
+ const add = game.sim.players.set.bind(game.sim.players);
169
+ game.sim.players.set = (id, v) => (id === 'late' ? game.sim.players : add(id, v));
170
+ r.move('a', 2);
171
+ const out = r.tick();
172
+ assert.ok(out.every((s) => s.to !== '*'), 'no broadcast while late waits');
173
+ assert.ok(!out.some((s) => s.to === 'late' && s.msg.k === 's'), 'late is sent no poses before its body');
174
+ game.sim.players.set = add;
175
+ }
176
+
177
+ // Everybody fits in the set: still one broadcast, identical to no hook.
178
+ {
179
+ const game = hooks({ interest: (sim, known) => nearestPlayers(sim, known, 30) });
180
+ const r = await placeOf(game, ['a', 'b', 'c']);
181
+ r.tick();
182
+ r.move('b', 2);
183
+ const out = r.tick();
184
+ assert.deepEqual(out.map((s) => s.to), ['*']);
185
+ assert.deepEqual(rows(out[0].msg), ['b']);
186
+ }
187
+
188
+ // Nearest 1: each client hears of only its nearest other player.
189
+ {
190
+ const game = hooks({ interest: (sim, known) => nearestPlayers(sim, known, 1) });
191
+ const r = await placeOf(game, ['a', 'b', 'c']);
192
+ r.tick();
193
+ r.move('a', 0);
194
+ r.move('b', 1);
195
+ r.move('c', 10);
196
+ const spread = r.tick();
197
+ assert.ok(spread.every((s) => s.to !== '*'), 'filtered sends go per client');
198
+ r.tick();
199
+ // a and b are each other's nearest; c, far off, is in nobody's set.
200
+ r.move('c', 10.5);
201
+ const moved = r.tick();
202
+ const toA = moved.find((s) => s.to === 'a');
203
+ assert.ok(!toA || !rows(toA.msg).includes('c'), 'a is not sent c, who is not near it');
204
+ const toC = moved.find((s) => s.to === 'c');
205
+ assert.deepEqual(rows(toC.msg), ['c'], 'c gets its own row back, nothing of b');
206
+
207
+ // c walks next to a, past b: a starts seeing c (`in`, whole row) and stops seeing b (`out`).
208
+ r.move('c', 0.1);
209
+ const swap = r.tick();
210
+ const a = swap.find((s) => s.to === 'a').msg;
211
+ assert.deepEqual(a.in, ['c']);
212
+ assert.deepEqual(a.out, ['b']);
213
+ assert.deepEqual(rows(a), ['c']);
214
+
215
+ // A player who leaves the session is not listed in anyone's `out`.
216
+ r.session.players = r.session.players.filter((p) => p.playerId !== 'c');
217
+ const left = r.tick();
218
+ for (const s of left) {
219
+ assert.ok(!(s.msg.out ?? []).includes('c'));
220
+ }
221
+ }
222
+
223
+ // Rows every third tick (`poseEvery`): a client's own row only as a correction
224
+ // (here never); other players' changed rows wait for its row tick and go at
225
+ // their newest, each with a short number its client is told once (`idm`),
226
+ // and `pi` says how far
227
+ // apart they are. Nothing a client is owed is lost when a broadcast resumes.
228
+ {
229
+ const game = hooks({ interest: (sim, known) => nearestPlayers(sim, known, 1), poseEvery: 3, poseIntervalMs: 100 });
230
+ const r = await placeOf(game, ['a', 'b', 'c']);
231
+ const world = r.tick().find((s) => s.to === 'a' && s.msg.k === 'w').msg;
232
+ assert.ok(world.ps.every(([id]) => typeof id === 'number'), 'WORLD rows carry numbers');
233
+ assert.equal(world.pi, 100);
234
+ const names = new Map(world.idm);
235
+ r.move('a', 0);
236
+ r.move('b', 1);
237
+ r.move('c', 10);
238
+ r.tick();
239
+ const toA = [];
240
+ for (let i = 0; i < 6; i += 1) {
241
+ r.move('b', 1 + (i + 1) * 0.1);
242
+ r.move('a', (i + 1) * 0.01);
243
+ const m = r.tick().find((s) => s.to === 'a')?.msg;
244
+ for (const [n, id] of m?.idm ?? []) names.set(n, id);
245
+ toA.push(m);
246
+ }
247
+ const bRows = toA.map((m) => (m?.ps ?? []).filter(([n]) => names.get(n) === 'b'));
248
+ const withB = bRows.filter((rows) => rows.length > 0);
249
+ assert.equal(withB.length, 2, `b's row every third tick, got ${bRows.map((x) => x.length)}`);
250
+ assert.ok(Math.abs(withB[1][0][1] - (1 + 0.1 * bRows.lastIndexOf(withB[1]) + 0.1)) < 1e-9, 'and at its newest');
251
+ assert.ok(toA.every((m) => !(m?.ps ?? []).some(([n]) => names.get(n) === 'a')), "a's own row only when it is a correction");
252
+ assert.ok(toA.filter((m) => m?.ps?.length).every((m) => m.pi === 100));
253
+ }
254
+
255
+ // A small session's WORLD is what it always was, numbers and all.
256
+ {
257
+ const game = hooks({ interest: (sim, known) => nearestPlayers(sim, known, 30), poseEvery: 3, poseIntervalMs: 100 });
258
+ const r = await placeOf(game, ['a', 'b']);
259
+ const world = r.tick().find((s) => s.to === 'a' && s.msg.k === 'w').msg;
260
+ assert.ok(world.ps.every(([id]) => typeof id === 'string'));
261
+ assert.ok(!('idm' in world) && !('pi' in world));
262
+ }
263
+
264
+ // A client back on its own body (a reload that kept its player id) is sent the
265
+ // world again, numbers named afresh.
266
+ {
267
+ const game = hooks({ interest: (sim, known) => nearestPlayers(sim, known, 1), poseEvery: 3, poseIntervalMs: 100 });
268
+ const r = await placeOf(game, ['a', 'b', 'c']);
269
+ r.tick();
270
+ r.move('a', 0);
271
+ r.move('b', 1);
272
+ r.move('c', 10);
273
+ r.tick();
274
+ r.tick();
275
+ r.rejoin('a');
276
+ const again = r.tick().find((s) => s.to === 'a' && s.msg.k === 'w')?.msg;
277
+ assert.ok(again, 'a fresh WORLD');
278
+ const named = new Map(again.idm);
279
+ assert.ok(again.ps.every(([n]) => named.has(n)), 'every row in it named');
280
+ }
281
+
282
+ // Hysteresis: a known player is kept until another is clearly nearer.
283
+ {
284
+ const sim = {
285
+ players: new Map([
286
+ ['me', { at: { x: 0, y: 0, z: 0 } }],
287
+ ['old', { at: { x: 10, y: 0, z: 0 } }],
288
+ ['new', { at: { x: 9, y: 0, z: 0 } }],
289
+ ]),
290
+ };
291
+ const known = new Map([['me', new Set(['old'])]]);
292
+ assert.deepEqual([...nearestPlayers(sim, known, 1).get('me')], ['old'], 'within 20% keeps the old one');
293
+ sim.players.get('new').at.x = 7;
294
+ assert.deepEqual([...nearestPlayers(sim, known, 1).get('me')], ['new'], 'clearly nearer replaces it');
295
+ }
296
+
297
+ // A hidden pair (a block) is left out before the nearest are picked: a hidden
298
+ // player nearer than the n-th visible one takes none of the n places.
299
+ {
300
+ const at = { me: 0, hid: 1, c: 2, d: 3, e: 10 };
301
+ const sim = { players: new Map(Object.entries(at).map(([id, x]) => [id, { at: { x, y: 0, z: 0 } }])) };
302
+ const pair = (x, y) => [x, y].sort().join() === 'hid,me';
303
+ const sees = nearestPlayers(sim, new Map(), 2, pair);
304
+ assert.deepEqual([...sees.get('me')].sort(), ['c', 'd'], 'me still gets two others, hid takes no place');
305
+ assert.deepEqual([...sees.get('hid')].sort(), ['c', 'd'], 'and the other way round');
306
+ // Already sent the hidden player last tick (hysteresis): still left out.
307
+ const kept = nearestPlayers(sim, new Map([['me', new Set(['hid'])]]), 1, pair);
308
+ assert.deepEqual([...kept.get('me')], ['c']);
309
+ }
310
+
311
+ // Everyone fits: the shortcut's sets still leave the hidden pair out.
312
+ {
313
+ const sim = { players: new Map(['a', 'b', 'c'].map((id, x) => [id, { at: { x, y: 0, z: 0 } }])) };
314
+ const sees = nearestPlayers(sim, new Map(), 30, (x, y) => [x, y].sort().join() === 'a,b');
315
+ assert.deepEqual([...sees.get('a')], ['c']);
316
+ assert.deepEqual([...sees.get('b')], ['c']);
317
+ assert.deepEqual([...sees.get('c')].sort(), ['a', 'b']);
318
+ // No predicate: everyone, as before.
319
+ const all = nearestPlayers(sim, new Map(), 30);
320
+ assert.deepEqual([...all.get('a')].sort(), ['b', 'c']);
321
+ }
322
+
323
+ // The player ids a client was sent rows or `in` for.
324
+ function heardOf(sent, id) {
325
+ const seen = new Set();
326
+ for (const { to, msg } of sent) {
327
+ if (to !== id && to !== '*') {
328
+ continue;
329
+ }
330
+ const names = new Map(msg.idm ?? []);
331
+ for (const [row] of msg.ps ?? []) {
332
+ seen.add(typeof row === 'string' ? row : names.get(row));
333
+ }
334
+ for (const other of msg.in ?? []) {
335
+ seen.add(other);
336
+ }
337
+ }
338
+ seen.delete(id);
339
+ return seen;
340
+ }
341
+
342
+ // A hook that throws or returns null falls back to everyone seeing everyone --
343
+ // but in a hub session (`session.hub.hiddenPair`) never a hidden pair, and a
344
+ // pair the host cannot answer for counts as hidden.
345
+ for (const failure of ['throw', 'null']) {
346
+ let off = false;
347
+ const fail = () => {
348
+ if (failure === 'throw') {
349
+ throw new Error('interest broke');
350
+ }
351
+ return null;
352
+ };
353
+ const errors = console.error;
354
+ console.error = () => {};
355
+ const hub = { hiddenPair: (x, y) => [x, y].sort().join() === 'a,b' };
356
+ const game = hooks({ interest: (sim, known) => (off ? fail() : nearestPlayers(sim, known, 30, hub.hiddenPair)) });
357
+ const r = await placeOf(game, ['a', 'b', 'c'], hub);
358
+ const all = [...r.sent];
359
+ all.push(...r.tick());
360
+ off = true;
361
+ for (let i = 0; i < 3; i += 1) {
362
+ r.move('a', i * 0.5);
363
+ r.move('b', 1 + i * 0.5);
364
+ r.move('c', 2 + i * 0.5);
365
+ all.push(...r.tick());
366
+ }
367
+ console.error = errors;
368
+ assert.ok(!heardOf(all, 'a').has('b'), `a never hears of b (${failure})`);
369
+ assert.ok(!heardOf(all, 'b').has('a'), `b never hears of a (${failure})`);
370
+ assert.ok(heardOf(all, 'a').has('c') && heardOf(all, 'c').has('a') && heardOf(all, 'c').has('b'));
371
+ }
372
+ {
373
+ const errors = console.error;
374
+ console.error = () => {};
375
+ const hub = {
376
+ hiddenPair() {
377
+ throw new Error('host down');
378
+ },
379
+ };
380
+ // The hub layer's own hook asks the host too, so it throws first.
381
+ const game = hooks({ interest: (sim, known) => nearestPlayers(sim, known, 30, hub.hiddenPair) });
382
+ const r = await placeOf(game, ['a', 'b'], hub);
383
+ const all = [...r.sent];
384
+ all.push(...r.tick());
385
+ r.move('a', 1);
386
+ r.move('b', 2);
387
+ all.push(...r.tick());
388
+ console.error = errors;
389
+ assert.equal(heardOf(all, 'a').size + heardOf(all, 'b').size, 0, 'a host that cannot say hides everyone');
390
+ }
391
+
392
+ // No hub: the fallback is everyone, exactly as before -- each set with the
393
+ // player itself in it.
394
+ {
395
+ let off = false;
396
+ let fallback;
397
+ const errors = console.error;
398
+ console.error = () => {};
399
+ const game = hooks({ interest: (sim, known) => (off ? null : nearestPlayers(sim, known, 1)) });
400
+ const r = await placeOf(game, ['a', 'b', 'c']);
401
+ r.tick();
402
+ r.move('b', 1);
403
+ r.move('c', 10);
404
+ r.tick();
405
+ off = true;
406
+ const back = r.tick();
407
+ console.error = errors;
408
+ fallback = back.find((s) => s.to === 'a').msg;
409
+ assert.deepEqual(fallback.in, ['c'], 'a is sent c again');
410
+ assert.deepEqual(back.find((s) => s.to === 'c').msg.in, ['b'], 'and c is sent b');
411
+ }
412
+
413
+ console.log('test-interest: ok');
@@ -0,0 +1,50 @@
1
+ // Tests over the hub's scroll-key guard (code/client/keys.js): movement keys
2
+ // never scroll the page around a hub place, text fields keep theirs, and
3
+ // browser shortcuts pass. `npm run check` runs this.
4
+
5
+ import assert from 'node:assert/strict';
6
+ import { guardScrollKeys, swallowsScroll } from '../code/client/keys.js';
7
+
8
+ const key = (code, extra = {}) => ({ code, target: { tagName: 'CANVAS' }, defaultPrevented: false, ...extra });
9
+
10
+ for (const code of ['ArrowUp', 'ArrowDown', 'ArrowLeft', 'ArrowRight', 'Space', 'PageDown', 'PageUp', 'Home', 'End']) {
11
+ assert.equal(swallowsScroll(key(code)), true, `${code} on the canvas`);
12
+ assert.equal(swallowsScroll(key(code, { target: { tagName: 'BODY' } })), true, `${code} on the body`);
13
+ assert.equal(swallowsScroll(key(code, { target: { tagName: 'BUTTON' } })), code !== 'Space', `${code} on a chrome button`);
14
+ assert.equal(swallowsScroll(key(code, { target: null })), true, `${code} with no target`);
15
+ }
16
+ for (const tagName of ['INPUT', 'TEXTAREA', 'SELECT']) {
17
+ assert.equal(swallowsScroll(key('ArrowLeft', { target: { tagName } })), false, `ArrowLeft in ${tagName}`);
18
+ assert.equal(swallowsScroll(key('Space', { target: { tagName } })), false, `Space in ${tagName}`);
19
+ }
20
+ assert.equal(swallowsScroll(key('Space', { target: { tagName: 'DIV', isContentEditable: true } })), false);
21
+ for (const code of ['KeyW', 'KeyS', 'KeyE', 'Tab', 'Enter', 'Escape']) {
22
+ assert.equal(swallowsScroll(key(code)), false, code);
23
+ }
24
+ assert.equal(swallowsScroll(key('ArrowLeft', { altKey: true })), false, 'Alt+Left is history back');
25
+ assert.equal(swallowsScroll(key('ArrowDown', { metaKey: true })), false);
26
+ assert.equal(swallowsScroll(key('Home', { ctrlKey: true })), false);
27
+ assert.equal(swallowsScroll(key('ArrowDown', { shiftKey: true })), true, 'Shift+Arrow still moves');
28
+ assert.equal(swallowsScroll(key('ArrowDown', { defaultPrevented: true })), false);
29
+
30
+ // The guard on a fake window: added once, prevents, and removes cleanly.
31
+ const listeners = new Map();
32
+ const win = {
33
+ addEventListener: (type, fn) => listeners.set(type, fn),
34
+ removeEventListener: (type, fn) => {
35
+ if (listeners.get(type) === fn) listeners.delete(type);
36
+ },
37
+ };
38
+ const remove = guardScrollKeys(win);
39
+ let prevented = 0;
40
+ const fire = (code, target = { tagName: 'CANVAS' }) =>
41
+ listeners.get('keydown')({ code, target, defaultPrevented: false, preventDefault: () => prevented++ });
42
+ fire('ArrowDown');
43
+ fire('Space');
44
+ fire('KeyW');
45
+ fire('ArrowDown', { tagName: 'INPUT' });
46
+ assert.equal(prevented, 2);
47
+ remove();
48
+ assert.equal(listeners.has('keydown'), false);
49
+
50
+ console.log('keys: all passed');