castle-web-cli 0.4.229 → 0.4.231

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 (259) 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 +860 -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/devSaveStorage.d.ts +36 -0
  19. package/dist/devSaveStorage.js +469 -0
  20. package/dist/devSessionChild.js +25 -13
  21. package/dist/devSessionServer.d.ts +14 -1
  22. package/dist/devSessionServer.js +237 -46
  23. package/dist/devSessionStorage.d.ts +9 -0
  24. package/dist/devSessionStorage.js +66 -1
  25. package/dist/experimentAssignment.d.ts +15 -0
  26. package/dist/experimentAssignment.js +41 -0
  27. package/dist/importCheckHook.d.ts +40 -0
  28. package/dist/importCheckHook.js +171 -0
  29. package/dist/importRegression.d.ts +15 -0
  30. package/dist/importRegression.js +100 -0
  31. package/dist/importTrust.d.ts +2 -0
  32. package/dist/importTrust.js +27 -0
  33. package/dist/importUpdateHook.d.ts +18 -0
  34. package/dist/importUpdateHook.js +31 -8
  35. package/dist/importUpdateHookRunner.js +19 -7
  36. package/dist/imports.d.ts +16 -0
  37. package/dist/imports.js +141 -31
  38. package/dist/index.js +3 -1
  39. package/dist/init.d.ts +3 -0
  40. package/dist/init.js +126 -9
  41. package/dist/install.js +4 -1
  42. package/dist/localPaths.js +4 -0
  43. package/dist/login.js +2 -2
  44. package/dist/platformDoc.js +3 -8
  45. package/dist/playPagePolicy.js +3 -1
  46. package/dist/remix.js +5 -0
  47. package/dist/routerTaskReport.d.ts +28 -0
  48. package/dist/routerTaskReport.js +56 -0
  49. package/dist/save-deck.d.ts +18 -0
  50. package/dist/save-deck.js +142 -6
  51. package/dist/serve.d.ts +8 -0
  52. package/dist/serve.js +99 -14
  53. package/dist/serverBundle.d.ts +1 -1
  54. package/dist/serverBundle.js +5 -1
  55. package/dist/webSourceSync.d.ts +2 -0
  56. package/dist/webSourceSync.js +2 -0
  57. package/kits/base/CLAUDE.md +2 -3
  58. package/kits/base/castle.json +1 -1
  59. package/kits/base/sdk/README.md +406 -53
  60. package/kits/base/sdk/agent.js +4 -4
  61. package/kits/base/sdk/avatar.d.ts +12 -0
  62. package/kits/base/sdk/avatar.js +23 -0
  63. package/kits/base/sdk/castle.d.ts +9 -0
  64. package/kits/base/sdk/castle.js +5 -0
  65. package/kits/base/sdk/commands.d.ts +223 -0
  66. package/kits/base/sdk/commands.js +16 -0
  67. package/kits/base/sdk/context.d.ts +6 -0
  68. package/kits/base/sdk/experiments.d.ts +15 -0
  69. package/kits/base/sdk/experiments.js +83 -0
  70. package/kits/base/sdk/hub.d.ts +85 -0
  71. package/kits/base/sdk/hub.js +107 -0
  72. package/kits/base/sdk/multiplayer.d.ts +45 -1
  73. package/kits/base/sdk/multiplayer.js +158 -8
  74. package/kits/base/sdk/save.d.ts +197 -0
  75. package/kits/base/sdk/save.js +525 -0
  76. package/kits/base/sdk/saveCoordinator.d.ts +39 -0
  77. package/kits/base/sdk/saveCoordinator.js +252 -0
  78. package/kits/base/sdk/server/hub.d.ts +169 -0
  79. package/kits/base/sdk/server/hub.js +125 -0
  80. package/kits/base/sdk/server/platformHandle.d.ts +42 -1
  81. package/kits/base/sdk/server/storage.d.ts +58 -4
  82. package/kits/base/sdk/server/storage.js +25 -5
  83. package/kits/base/sdk/server/wrapper.d.ts +36 -2
  84. package/kits/base/sdk/server/wrapper.js +92 -11
  85. package/kits/base/sdk/store.d.ts +3 -2
  86. package/kits/base/sdk/store.js +5 -0
  87. package/kits/base/sdk/transport.d.ts +2 -1
  88. package/kits/base/sdk/transport.js +13 -1
  89. package/kits/hub-game/.prettierrc +5 -0
  90. package/kits/hub-game/CLAUDE.md +149 -0
  91. package/kits/hub-game/blueprints/avatar-0.scene +28 -0
  92. package/kits/hub-game/blueprints/avatar-1.scene +28 -0
  93. package/kits/hub-game/blueprints/avatar-2.scene +28 -0
  94. package/kits/hub-game/blueprints/avatar-3.scene +28 -0
  95. package/kits/hub-game/blueprints/bean/avatar-0.scene +28 -0
  96. package/kits/hub-game/blueprints/bean/avatar-1.scene +28 -0
  97. package/kits/hub-game/blueprints/bean/avatar-2.scene +28 -0
  98. package/kits/hub-game/blueprints/bean/avatar-3.scene +28 -0
  99. package/kits/hub-game/blueprints/blocky/avatar-0.scene +28 -0
  100. package/kits/hub-game/blueprints/blocky/avatar-1.scene +28 -0
  101. package/kits/hub-game/blueprints/blocky/avatar-2.scene +28 -0
  102. package/kits/hub-game/blueprints/blocky/avatar-3.scene +28 -0
  103. package/kits/hub-game/blueprints/pixel/avatar-0.scene +28 -0
  104. package/kits/hub-game/blueprints/pixel/avatar-1.scene +28 -0
  105. package/kits/hub-game/blueprints/pixel/avatar-2.scene +28 -0
  106. package/kits/hub-game/blueprints/pixel/avatar-3.scene +28 -0
  107. package/kits/hub-game/castle.json +96 -0
  108. package/kits/hub-game/code/client/attention.js +133 -0
  109. package/kits/hub-game/code/client/avatar.js +288 -0
  110. package/kits/hub-game/code/client/cabinets.js +90 -0
  111. package/kits/hub-game/code/client/chat.js +135 -0
  112. package/kits/hub-game/code/client/chrome.js +1035 -0
  113. package/kits/hub-game/code/client/doorFrame.js +15 -0
  114. package/kits/hub-game/code/client/firstJoin.js +14 -0
  115. package/kits/hub-game/code/client/grab.js +376 -0
  116. package/kits/hub-game/code/client/hookLayer.js +236 -0
  117. package/kits/hub-game/code/client/idle.js +326 -0
  118. package/kits/hub-game/code/client/keys.js +48 -0
  119. package/kits/hub-game/code/client/pack.js +232 -0
  120. package/kits/hub-game/code/client/packFormat.js +397 -0
  121. package/kits/hub-game/code/client/say.js +127 -0
  122. package/kits/hub-game/code/client/sha256.js +80 -0
  123. package/kits/hub-game/code/client/sound.js +147 -0
  124. package/kits/hub-game/code/client/state.js +143 -0
  125. package/kits/hub-game/code/client/talk.js +233 -0
  126. package/kits/hub-game/code/client/tiers.js +480 -0
  127. package/kits/hub-game/code/client/together.js +440 -0
  128. package/kits/hub-game/code/client/wear.js +149 -0
  129. package/kits/hub-game/code/server/actors.js +627 -0
  130. package/kits/hub-game/code/server/blocks.js +103 -0
  131. package/kits/hub-game/code/server/cabinets.js +309 -0
  132. package/kits/hub-game/code/server/game.js +137 -0
  133. package/kits/hub-game/code/server/grab.js +248 -0
  134. package/kits/hub-game/code/server/history.js +96 -0
  135. package/kits/hub-game/code/server/hub.js +1998 -0
  136. package/kits/hub-game/code/server/idle.js +412 -0
  137. package/kits/hub-game/code/server/index.js +64 -0
  138. package/kits/hub-game/code/server/labels.js +59 -0
  139. package/kits/hub-game/code/server/manifest.js +554 -0
  140. package/kits/hub-game/code/server/npcs.js +733 -0
  141. package/kits/hub-game/code/server/party.js +439 -0
  142. package/kits/hub-game/code/server/place.js +43 -0
  143. package/kits/hub-game/code/server/say.js +83 -0
  144. package/kits/hub-game/code/server/wear.js +142 -0
  145. package/kits/hub-game/code/systems/hub.js +2347 -0
  146. package/kits/hub-game/docs/content-pack.md +151 -0
  147. package/kits/hub-game/docs/contract.md +1209 -0
  148. package/kits/hub-game/docs/style.md +137 -0
  149. package/kits/hub-game/drawings/blocky/castle-eye.sprite +58 -0
  150. package/kits/hub-game/drawings/blocky/dash.sprite +59 -0
  151. package/kits/hub-game/drawings/blocky/pom.sprite +59 -0
  152. package/kits/hub-game/drawings/blocky/tabby.sprite +59 -0
  153. package/kits/hub-game/drawings/pixel/castle-arm.sprite +13 -0
  154. package/kits/hub-game/drawings/pixel/castle-body.sprite +23 -0
  155. package/kits/hub-game/drawings/pixel/castle-leg.sprite +12 -0
  156. package/kits/hub-game/drawings/pixel/kai-arm.sprite +14 -0
  157. package/kits/hub-game/drawings/pixel/kai-head.sprite +24 -0
  158. package/kits/hub-game/drawings/pixel/kai-leg.sprite +15 -0
  159. package/kits/hub-game/drawings/pixel/kai-torso.sprite +18 -0
  160. package/kits/hub-game/drawings/pixel/mimi-arm.sprite +13 -0
  161. package/kits/hub-game/drawings/pixel/mimi-head.sprite +23 -0
  162. package/kits/hub-game/drawings/pixel/mimi-leg.sprite +15 -0
  163. package/kits/hub-game/drawings/pixel/mimi-torso.sprite +18 -0
  164. package/kits/hub-game/drawings/pixel/nox-arm.sprite +13 -0
  165. package/kits/hub-game/drawings/pixel/nox-head.sprite +21 -0
  166. package/kits/hub-game/drawings/pixel/nox-leg.sprite +14 -0
  167. package/kits/hub-game/drawings/pixel/nox-torso.sprite +18 -0
  168. package/kits/hub-game/eslint.config.js +20 -0
  169. package/kits/hub-game/fonts.generated.js +9 -0
  170. package/kits/hub-game/index.html +22 -0
  171. package/kits/hub-game/main.jsx +6 -0
  172. package/kits/hub-game/models/avatar-0.pxmodel +21 -0
  173. package/kits/hub-game/models/avatar-1.pxmodel +24 -0
  174. package/kits/hub-game/models/avatar-2.pxmodel +27 -0
  175. package/kits/hub-game/models/avatar-3.pxmodel +22 -0
  176. package/kits/hub-game/models/bean/avatar-0.pxmodel +19 -0
  177. package/kits/hub-game/models/bean/avatar-1.pxmodel +24 -0
  178. package/kits/hub-game/models/bean/avatar-2.pxmodel +22 -0
  179. package/kits/hub-game/models/bean/avatar-3.pxmodel +24 -0
  180. package/kits/hub-game/models/blocky/avatar-0.pxmodel +21 -0
  181. package/kits/hub-game/models/blocky/avatar-1.pxmodel +30 -0
  182. package/kits/hub-game/models/blocky/avatar-2.pxmodel +28 -0
  183. package/kits/hub-game/models/blocky/avatar-3.pxmodel +27 -0
  184. package/kits/hub-game/models/pixel/avatar-0.pxmodel +24 -0
  185. package/kits/hub-game/models/pixel/avatar-1.pxmodel +16 -0
  186. package/kits/hub-game/models/pixel/avatar-2.pxmodel +19 -0
  187. package/kits/hub-game/models/pixel/avatar-3.pxmodel +23 -0
  188. package/kits/hub-game/package-lock.json +365 -0
  189. package/kits/hub-game/package.json +32 -0
  190. package/kits/hub-game/scenes/main.scene +318 -0
  191. package/kits/hub-game/scripts/check.mjs +339 -0
  192. package/kits/hub-game/scripts/fixtures/sdk-stub.mjs +3 -0
  193. package/kits/hub-game/scripts/gen-avatars-bean.mjs +748 -0
  194. package/kits/hub-game/scripts/gen-avatars-blocky.mjs +607 -0
  195. package/kits/hub-game/scripts/gen-avatars-pixel.mjs +771 -0
  196. package/kits/hub-game/scripts/gen-avatars.mjs +621 -0
  197. package/kits/hub-game/scripts/pack.mjs +192 -0
  198. package/kits/hub-game/scripts/test-actors.mjs +666 -0
  199. package/kits/hub-game/scripts/test-attention.mjs +103 -0
  200. package/kits/hub-game/scripts/test-avatar.mjs +472 -0
  201. package/kits/hub-game/scripts/test-ban-join.mjs +194 -0
  202. package/kits/hub-game/scripts/test-blocks.mjs +351 -0
  203. package/kits/hub-game/scripts/test-chat.mjs +98 -0
  204. package/kits/hub-game/scripts/test-classic.mjs +590 -0
  205. package/kits/hub-game/scripts/test-door-jump.mjs +131 -0
  206. package/kits/hub-game/scripts/test-door.mjs +56 -0
  207. package/kits/hub-game/scripts/test-grab-client.mjs +328 -0
  208. package/kits/hub-game/scripts/test-grab.mjs +331 -0
  209. package/kits/hub-game/scripts/test-hub-ops.mjs +835 -0
  210. package/kits/hub-game/scripts/test-hub-trip.mjs +1518 -0
  211. package/kits/hub-game/scripts/test-idle.mjs +518 -0
  212. package/kits/hub-game/scripts/test-interest.mjs +413 -0
  213. package/kits/hub-game/scripts/test-keys.mjs +50 -0
  214. package/kits/hub-game/scripts/test-manifest.mjs +270 -0
  215. package/kits/hub-game/scripts/test-no-forms.mjs +16 -0
  216. package/kits/hub-game/scripts/test-npcs.mjs +724 -0
  217. package/kits/hub-game/scripts/test-pack.mjs +424 -0
  218. package/kits/hub-game/scripts/test-party.mjs +369 -0
  219. package/kits/hub-game/scripts/test-say.mjs +149 -0
  220. package/kits/hub-game/scripts/test-tiers.mjs +666 -0
  221. package/kits/hub-game/scripts/test-together.mjs +215 -0
  222. package/kits/hub-game/theme.style +3 -0
  223. package/kits/multiplayer-3d/castle.json +5 -5
  224. package/kits/multiplayer-3d/code/client/avatars.js +101 -1
  225. package/kits/multiplayer-3d/code/server/players.js +4 -1
  226. package/kits/multiplayer-3d/code/server/sceneBodies.js +27 -6
  227. package/kits/multiplayer-3d/code/server/world.js +20 -7
  228. package/kits/multiplayer-3d/code/systems/multiplayer.js +222 -11
  229. package/kits/physics-2d/CLAUDE.md +2 -0
  230. package/kits/physics-2d/castle.json +2 -2
  231. package/kits/physics-2d/engine/art.js +13 -0
  232. package/kits/physics-2d/engine/assets.js +4 -2
  233. package/kits/physics-2d/engine/scene.js +117 -5
  234. package/kits/physics-2d/package.json +1 -1
  235. package/kits/physics-2d/scripts/test-skip-unchanged.mjs +254 -0
  236. package/kits/physics-3d/CLAUDE.md +285 -1
  237. package/kits/physics-3d/behaviors/Player.jsx +74 -1
  238. package/kits/physics-3d/castle.json +3 -3
  239. package/kits/physics-3d/engine3d/Scene3DPlayer.jsx +33 -5
  240. package/kits/physics-3d/engine3d/actornode.js +253 -0
  241. package/kits/physics-3d/engine3d/artobjects.js +2 -2
  242. package/kits/physics-3d/engine3d/materials.js +29 -1
  243. package/kits/physics-3d/engine3d/nearshadows.js +600 -0
  244. package/kits/physics-3d/engine3d/unchanged.js +153 -0
  245. package/kits/physics-3d/engine3d/world3d.js +527 -5
  246. package/kits/physics-3d/package.json +1 -1
  247. package/kits/physics-3d/scripts/test-camera-follow.mjs +533 -0
  248. package/kits/physics-3d/scripts/test-near-shadows.mjs +1073 -0
  249. package/kits/physics-3d/scripts/test-static-actors.mjs +662 -0
  250. package/kits/physics-3d/scripts/test-static-passes.mjs +459 -0
  251. package/kits/physics-3d/scripts/test-static-physics.mjs +128 -0
  252. package/kits/physics-3d/systems/physics3d.js +187 -5
  253. package/kits/real-time/castle.json +2 -2
  254. package/kits/real-time/code/client/connection.js +77 -7
  255. package/kits/real-time/code/client/gameHooks.js +63 -3
  256. package/kits/real-time/code/client/messages.js +7 -0
  257. package/kits/real-time/code/server/gameHooks.js +24 -0
  258. package/kits/real-time/code/server/session.js +390 -22
  259. package/package.json +2 -2
@@ -0,0 +1,369 @@
1
+ // Parties in a town (contract §5, v1.10): invite and accept, a party's state
2
+ // told to each member, a door proposed to the party putting each member who
3
+ // says yes on its pad, `hub.coming`, a member kept while away through a door,
4
+ // and a jump to a door landing on its pad. A game has none of it, and an
5
+ // invitation never crosses a block. `npm run check` runs it; it needs nothing
6
+ // installed.
7
+
8
+ import assert from 'node:assert/strict';
9
+ import { padPoint } from '../code/server/party.js';
10
+ import { register } from 'node:module';
11
+
12
+ const sceneStub = 'export const readScene = () => globalThis.__hubTestActors ?? [];';
13
+ register(
14
+ `data:text/javascript,${encodeURIComponent(`
15
+ export async function resolve(specifier, context, next) {
16
+ if (specifier === '@imports/castle.multiplayer-3d/code/server/sceneBodies.js') {
17
+ return { url: 'data:text/javascript,${encodeURIComponent(sceneStub)}', shortCircuit: true };
18
+ }
19
+ return next(specifier, context);
20
+ }
21
+ `)}`,
22
+ );
23
+ const { layer } = await import('../code/server/hub.js');
24
+
25
+ globalThis.__hubTestActors = [
26
+ { id: 'spawn', components: { Player: {}, Transform: { x: 0, y: 1, z: 0 } } },
27
+ { id: 'door-race', components: { HubDoor: { id: 'race', to: 'race' }, Transform: { x: -28, y: 1.4, z: 0, rotationY: 90 } } },
28
+ { id: 'pad-race', components: { HubPad: { door: 'race', radius: 2 }, Transform: { x: -22, y: 0, z: 0 } } },
29
+ { id: 'door-gems', components: { HubDoor: { id: 'gems', to: 'race', title: 'Gem Rush' }, Transform: { x: 0, y: 1.4, z: -28, rotationY: 0 } } },
30
+ ];
31
+ const castleJson = {
32
+ server: { maxPlayers: 8 },
33
+ hub: {
34
+ contract: 1,
35
+ title: 'Town',
36
+ places: {
37
+ race: {
38
+ title: 'Foot Race',
39
+ scene: 'scenes/race.scene',
40
+ result: { outcomes: ['win', 'lose'], score: { unit: 'ms', better: 'lower', min: 0, max: 600000 } },
41
+ },
42
+ },
43
+ },
44
+ };
45
+ const sceneExists = (scene) => ['scenes/main.scene', 'scenes/race.scene'].includes(scene);
46
+ const ROSTER = [
47
+ { playerId: 'p1', userId: '101', username: 'ada' },
48
+ { playerId: 'p2', userId: '102', username: 'bo' },
49
+ { playerId: 'p3', userId: '103', username: 'cy' },
50
+ { playerId: 'g4', userId: null, username: 'guest' },
51
+ ];
52
+
53
+ function boot({ sessionId = 'dev:test', hiddenPair = null } = {}) {
54
+ let ctx = null;
55
+ const h = layer(castleJson, { ready: (hub) => (ctx = hub) }, {}, sceneExists);
56
+ const sent = [];
57
+ const session = {
58
+ sessionId,
59
+ players: [...ROSTER],
60
+ send: (playerId, msg) => sent.push([playerId, msg]),
61
+ broadcast() {},
62
+ ...(hiddenPair
63
+ ? {
64
+ hub: {
65
+ place: 'main',
66
+ arrival: () => null,
67
+ readResults: async () => ({ players: {}, boards: {} }),
68
+ updatePad: async () => ({ status: 'gathering', members: [] }),
69
+ reportResult: async () => ({ recorded: false }),
70
+ hiddenPair,
71
+ },
72
+ }
73
+ : {}),
74
+ };
75
+ h.sceneFor(session);
76
+ const players = new Map();
77
+ const body = () => ({ at: { x: 0, y: 1, z: 0 }, facing: 0, body: { setTranslation() {} } });
78
+ for (const { playerId } of ROSTER) {
79
+ players.set(playerId, body());
80
+ }
81
+ const world = { timeMs: 0, players, bodies: new Map(), scenePath: 'scenes/main.scene' };
82
+ h.ready(world);
83
+ const b = { h, session, world, sent, ctx: () => ctx, body };
84
+ for (const { playerId } of ROSTER) {
85
+ h.message(session, { playerId }, { k: 'hub-hello', avatar: 0 });
86
+ }
87
+ tick(b, 5000);
88
+ sent.length = 0;
89
+ return b;
90
+ }
91
+ // One server tick, `aheadMs` later than the clock says.
92
+ function tick(b, aheadMs = 0) {
93
+ const realNow = Date.now;
94
+ Date.now = () => realNow() + aheadMs;
95
+ try {
96
+ b.h.place(b.world, []);
97
+ b.h.step(b.world, []);
98
+ } finally {
99
+ Date.now = realNow;
100
+ }
101
+ }
102
+ const party = (b, playerId, data) => b.h.message(b.session, { playerId }, { k: 'hub-party', ...data });
103
+ const told = (b, playerId) => b.sent.filter(([to, msg]) => to === playerId && msg.k === 'hub-party').at(-1)?.[1] ?? null;
104
+ const at = (b, playerId) => b.world.players.get(playerId).at;
105
+
106
+ let n = 0;
107
+
108
+ // Invite, accept: a party of two, each told the other; a third joins it.
109
+ {
110
+ const b = boot();
111
+ party(b, 'p1', { op: 'invite', player: 'p2' });
112
+ tick(b);
113
+ assert.deepEqual(told(b, 'p2').invite, { from: 'p1', username: 'ada' });
114
+ assert.equal(told(b, 'p1'), null, 'the inviter is told nothing yet');
115
+ party(b, 'p2', { op: 'accept', from: 'p1' });
116
+ tick(b);
117
+ assert.deepEqual(told(b, 'p1').members, ['p2']);
118
+ assert.deepEqual(told(b, 'p2').members, ['p1']);
119
+ assert.equal(told(b, 'p2').invite, null);
120
+ party(b, 'p2', { op: 'invite', player: 'g4' });
121
+ party(b, 'g4', { op: 'accept', from: 'p2' });
122
+ tick(b);
123
+ assert.deepEqual(told(b, 'p1').members.sort(), ['g4', 'p2']);
124
+ n += 1;
125
+
126
+ // A door proposed to the party: the proposer is on its pad at once, the
127
+ // others are asked, and the place can see how many it waits on.
128
+ party(b, 'p1', { op: 'go', door: 'race' });
129
+ tick(b);
130
+ assert.ok(Math.hypot(at(b, 'p1').x + 22, at(b, 'p1').z) <= 1 + 1e-9, `on the pad: ${JSON.stringify(at(b, 'p1'))}`);
131
+ assert.ok(Math.abs(b.world.players.get('p1').facing - (270 * Math.PI) / 180) < 1e-9, 'facing the door');
132
+ const proposal = told(b, 'p2').proposal;
133
+ assert.deepEqual(proposal, { id: proposal.id, door: 'race', by: 'p1', username: 'ada', asked: true });
134
+ assert.equal(told(b, 'p1').proposal.asked, false);
135
+ assert.equal(b.ctx().coming('race'), 2);
136
+ assert.equal(b.ctx().coming('gems'), 0);
137
+ n += 1;
138
+
139
+ // Yes lands on the pad, on a spot of its own; no stays put; nobody is asked twice.
140
+ party(b, 'p2', { op: 'go-yes', id: proposal.id });
141
+ party(b, 'g4', { op: 'go-no', id: proposal.id });
142
+ party(b, 'g4', { op: 'go-yes', id: proposal.id });
143
+ tick(b);
144
+ assert.ok(Math.hypot(at(b, 'p2').x + 22, at(b, 'p2').z) <= 1 + 1e-9);
145
+ assert.ok(Math.hypot(at(b, 'p2').x - at(b, 'p1').x, at(b, 'p2').z - at(b, 'p1').z) >= 1.1 - 1e-9, 'clear of the proposer by more than two bodies');
146
+ assert.ok(Math.hypot(at(b, 'p2').x - at(b, 'p1').x, at(b, 'p2').z - at(b, 'p1').z) > 0.5, 'not on top of the proposer');
147
+ assert.ok(Math.hypot(at(b, 'g4').x, at(b, 'g4').z) < 3, 'the one who said no stayed');
148
+ assert.equal(b.ctx().coming('race'), 0);
149
+ assert.equal(told(b, 'p2').proposal, null);
150
+ n += 1;
151
+
152
+ // A signed-in member who leaves the layer (through the door) is kept as
153
+ // away, and is back in the party on return; a guest who leaves is gone.
154
+ b.world.players.delete('p2');
155
+ b.world.players.delete('g4');
156
+ b.session.players = b.session.players.filter((p) => !['p2', 'g4'].includes(p.playerId));
157
+ tick(b);
158
+ assert.deepEqual(told(b, 'p1').members, []);
159
+ assert.equal(told(b, 'p1').away, 1);
160
+ b.session.players.push({ playerId: 'p2b', userId: '102', username: 'bo' });
161
+ b.world.players.set('p2b', b.body());
162
+ b.h.message(b.session, { playerId: 'p2b' }, { k: 'hub-hello', avatar: 0 });
163
+ tick(b, 5000);
164
+ tick(b, 5000);
165
+ assert.deepEqual(told(b, 'p1').members, ['p2b']);
166
+ assert.deepEqual(told(b, 'p2b').members, ['p1']);
167
+ n += 1;
168
+
169
+ // Leave: a party of one is no party.
170
+ party(b, 'p2b', { op: 'leave' });
171
+ tick(b);
172
+ assert.equal(told(b, 'p1').members, null);
173
+ assert.equal(told(b, 'p2b').members, null);
174
+ n += 1;
175
+ }
176
+
177
+ // A proposal nobody answers, and an invitation nobody answers, run out.
178
+ {
179
+ const b = boot();
180
+ party(b, 'p1', { op: 'invite', player: 'p2' });
181
+ party(b, 'p2', { op: 'accept', from: 'p1' });
182
+ party(b, 'p1', { op: 'go', door: 'race' });
183
+ party(b, 'p3', { op: 'invite', player: 'g4' });
184
+ tick(b);
185
+ assert.equal(b.ctx().coming('race'), 1);
186
+ tick(b, 31000);
187
+ {
188
+ const realNow = Date.now;
189
+ Date.now = () => realNow() + 31000;
190
+ try {
191
+ assert.equal(b.ctx().coming('race'), 0, 'the pad is held 20 s');
192
+ } finally {
193
+ Date.now = realNow;
194
+ }
195
+ }
196
+ assert.equal(told(b, 'p2').proposal.door, 'race', 'the question stays up 45 s');
197
+ tick(b, 46000);
198
+ assert.equal(told(b, 'p2').proposal, null);
199
+ assert.deepEqual(told(b, 'g4').invite, { from: 'p3', username: 'cy' }, 'an invitation waits a minute');
200
+ tick(b, 61000);
201
+ assert.equal(told(b, 'g4').invite, null);
202
+ n += 1;
203
+ }
204
+
205
+ // Alone, Doors > Go lands on the pad too; a door with no pad, in front of it.
206
+ {
207
+ const b = boot();
208
+ b.h.message(b.session, { playerId: 'p1' }, { k: 'hub-door-jump', door: 'race' });
209
+ assert.ok(Math.hypot(at(b, 'p1').x + 22, at(b, 'p1').z) <= 1 + 1e-9, `on the pad: ${JSON.stringify(at(b, 'p1'))}`);
210
+ b.h.message(b.session, { playerId: 'p2' }, { k: 'hub-door-jump', door: 'gems' });
211
+ assert.ok(Math.abs(at(b, 'p2').z + 21) < 1e-9, '7 m in front of a door with no pad');
212
+ n += 1;
213
+ }
214
+
215
+ // A crowded pad: each newcomer is put where there is most room, never on
216
+ // someone, for as long as the pad has room.
217
+ {
218
+ const b = boot();
219
+ const realNow = Date.now;
220
+ const ids = ['p1', 'p2', 'g4'];
221
+ const placed = [];
222
+ for (let i = 0; i < 3; i += 1) {
223
+ Date.now = () => realNow() + 3000 * (i + 1);
224
+ b.h.message(b.session, { playerId: ids[i] }, { k: 'hub-door-jump', door: 'race' });
225
+ placed.push({ ...at(b, ids[i]) });
226
+ }
227
+ Date.now = realNow;
228
+ for (let i = 0; i < placed.length; i += 1) {
229
+ assert.ok(Math.hypot(placed[i].x + 22, placed[i].z) <= 2, `on the pad: ${JSON.stringify(placed[i])}`);
230
+ for (let j = 0; j < i; j += 1) {
231
+ assert.ok(Math.hypot(placed[i].x - placed[j].x, placed[i].z - placed[j].z) >= 0.9, `not touching: ${JSON.stringify(placed)}`);
232
+ }
233
+ }
234
+ n += 1;
235
+ }
236
+
237
+ // padPoint itself: the middle and the inner ring taken, the outer ring; every
238
+ // spot taken, the one with the most room.
239
+ {
240
+ const door = { id: 'race', t: { x: 0, z: 0, rotationY: 0 } };
241
+ const pad = { id: 'pad', components: { HubPad: { door: 'race', radius: 2.3 }, Transform: { x: 0, z: 0 } } };
242
+ const players = new Map();
243
+ const h = { actors: [pad], hidden: new Set(), sim: { players } };
244
+ players.set('c', { at: { x: 0, z: 0 } });
245
+ for (let i = 0; i < 6; i += 1) {
246
+ const a = (i * 2 * Math.PI) / 6;
247
+ players.set(`r${i}`, { at: { x: Math.sin(a) * 1.15, z: Math.cos(a) * 1.15 } });
248
+ }
249
+ const at = padPoint(h, door, 0, 'me');
250
+ const r = Math.hypot(at.x, at.z);
251
+ assert.ok(r > 1.5 && r <= 2.3, `the outer ring: ${JSON.stringify(at)}`);
252
+ // Less than PAD_CLEAR_M from its inner neighbours on a 2.3 m pad, but not touching anyone.
253
+ assert.ok([...players.values()].every((p) => Math.hypot(p.at.x - at.x, p.at.z - at.z) >= 0.9), 'touching nobody');
254
+ for (let i = 0; i < 6; i += 1) {
255
+ const a = ((i + 0.5) * 2 * Math.PI) / 6;
256
+ players.set(`o${i}`, { at: { x: Math.sin(a) * 1.75, z: Math.cos(a) * 1.75 } });
257
+ }
258
+ const last = padPoint(h, door, 0, 'me');
259
+ assert.ok(Math.hypot(last.x, last.z) <= 2.3, 'still on the pad');
260
+ n += 1;
261
+ }
262
+
263
+ // A spot on the pad that someone stands on is not used: a body put inside
264
+ // another is pushed off the pad. And a jump holds the body where it was put
265
+ // for a moment, so move reports still in flight cannot drag it back off.
266
+ {
267
+ const b = boot();
268
+ const realNow = Date.now;
269
+ for (let i = 0; i < 12; i += 1) {
270
+ // Each try a few seconds on: past the place's jump limit and the last hold.
271
+ Date.now = () => realNow() + 3000 * (i + 1);
272
+ b.h.place(b.world, []);
273
+ Object.assign(at(b, 'p2'), { x: -21.1, z: 0 });
274
+ Object.assign(at(b, 'p1'), { x: 0, z: 0 });
275
+ b.h.message(b.session, { playerId: 'p1' }, { k: 'hub-door-jump', door: 'race' });
276
+ const me = { ...at(b, 'p1') };
277
+ assert.ok(Math.hypot(me.x + 22, me.z) <= 1 + 1e-9, 'on the pad');
278
+ // More than two bodies' width: put down touching them, physics would push one off the pad.
279
+ assert.ok(Math.hypot(me.x - at(b, 'p2').x, me.z - at(b, 'p2').z) >= 0.9, `clear of the player on a ring spot: ${JSON.stringify(me)}`);
280
+ // A stale report moves the body toward where it came from: the hold puts it back.
281
+ Object.assign(at(b, 'p1'), { x: me.x + 1.5, z: me.z + 1 });
282
+ b.h.place(b.world, []);
283
+ assert.ok(Math.hypot(at(b, 'p1').x - me.x, at(b, 'p1').z - me.z) < 0.01, 'kept where the jump put it');
284
+ assert.ok(!b.h.join().held.includes('p1'), 'not a hold: the client keeps its input and what it carries');
285
+ }
286
+ // The hold ends by itself.
287
+ Date.now = () => realNow() + 3000 * 14;
288
+ b.h.place(b.world, []);
289
+ Object.assign(at(b, 'p1'), { x: 5, z: 5 });
290
+ b.h.place(b.world, []);
291
+ assert.deepEqual([at(b, 'p1').x, at(b, 'p1').z], [5, 5], 'free to walk again');
292
+ Date.now = realNow;
293
+ n += 1;
294
+ }
295
+
296
+ // No invitation across a block; no parties in a game; no go without a party.
297
+ {
298
+ const b = boot({ hiddenPair: (x, y) => [x, y].sort().join() === 'p1,p2' });
299
+ party(b, 'p1', { op: 'invite', player: 'p2' });
300
+ party(b, 'p3', { op: 'go', door: 'race' });
301
+ tick(b);
302
+ assert.equal(told(b, 'p2'), null);
303
+ assert.ok(Math.hypot(at(b, 'p3').x, at(b, 'p3').z) < 3, 'no party: go does nothing');
304
+ const game = boot({ sessionId: 'dev:hub.race.abc123' });
305
+ party(game, 'p1', { op: 'invite', player: 'p2' });
306
+ tick(game);
307
+ assert.equal(told(game, 'p2'), null);
308
+ n += 1;
309
+ }
310
+
311
+ // An answer is to what was shown: a yes to an older proposal moves nobody to
312
+ // the door that replaced it, and an accept names its inviter. A party holds
313
+ // a pad once a minute, however often it proposes.
314
+ {
315
+ const b = boot();
316
+ party(b, 'p1', { op: 'invite', player: 'p2' });
317
+ party(b, 'p2', { op: 'accept', from: 'p3' });
318
+ tick(b);
319
+ assert.equal(told(b, 'p1'), null, 'an accept naming someone else joins nothing');
320
+ party(b, 'p2', { op: 'accept', from: 'p1' });
321
+ party(b, 'p1', { op: 'go', door: 'race' });
322
+ tick(b);
323
+ const first = told(b, 'p2').proposal.id;
324
+ assert.equal(b.ctx().coming('race'), 1);
325
+ party(b, 'p1', { op: 'go', door: 'gems' });
326
+ party(b, 'p2', { op: 'go-yes', id: first });
327
+ tick(b);
328
+ assert.ok(Math.hypot(at(b, 'p2').x, at(b, 'p2').z) < 3, 'a yes to the old proposal goes nowhere');
329
+ assert.equal(told(b, 'p2').proposal.door, 'gems');
330
+ assert.equal(b.ctx().coming('gems'), 0, 'the second proposal in a minute holds no pad');
331
+ n += 1;
332
+ }
333
+
334
+ // A member who picks the door the party is already going to has said yes:
335
+ // they are put on the pad and no second proposal is made.
336
+ {
337
+ const b = boot();
338
+ party(b, 'p1', { op: 'invite', player: 'p2' });
339
+ party(b, 'p2', { op: 'accept', from: 'p1' });
340
+ party(b, 'p1', { op: 'go', door: 'race' });
341
+ tick(b);
342
+ const first = told(b, 'p2').proposal.id;
343
+ party(b, 'p2', { op: 'go', door: 'race' });
344
+ tick(b);
345
+ assert.ok(Math.hypot(at(b, 'p2').x + 22, at(b, 'p2').z) <= 1 + 1e-9, 'on the pad');
346
+ assert.equal(b.ctx().coming('race'), 0, 'nobody is left to answer');
347
+ assert.equal(told(b, 'p1').proposal, null);
348
+ party(b, 'p1', { op: 'go', door: 'race' });
349
+ tick(b);
350
+ assert.notEqual(told(b, 'p2').proposal?.id, first, 'a new proposal, once the first was answered');
351
+ n += 1;
352
+ }
353
+
354
+ // A member a block hides from the proposer hears nothing of the proposal.
355
+ {
356
+ const b = boot({ hiddenPair: (x, y) => [x, y].sort().join() === 'p1,p2' });
357
+ party(b, 'p3', { op: 'invite', player: 'p1' });
358
+ party(b, 'p1', { op: 'accept', from: 'p3' });
359
+ party(b, 'p3', { op: 'invite', player: 'p2' });
360
+ party(b, 'p2', { op: 'accept', from: 'p3' });
361
+ party(b, 'p1', { op: 'go', door: 'race' });
362
+ tick(b);
363
+ assert.equal(told(b, 'p2').proposal, null);
364
+ assert.deepEqual(told(b, 'p2').members, ['p3']);
365
+ assert.equal(told(b, 'p3').proposal.username, 'ada');
366
+ n += 1;
367
+ }
368
+
369
+ console.log(`test-party: ${n} passed`);
@@ -0,0 +1,149 @@
1
+ // Quick reactions and phrases (contract §6, v1.11): a reaction is an index
2
+ // into the kit's emoji and a phrase an id of the kit's; the server carries no
3
+ // text, names the door a "Let's play" speaker stands by, limits each player,
4
+ // and tells nobody a block hides the speaker from. The client words them.
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 = () => globalThis.__hubTestActors ?? [];';
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 { layer } = await import('../code/server/hub.js');
22
+ const HubState = await import('../code/client/state.js');
23
+ const { makeSay, REACTIONS } = await import('../code/client/say.js');
24
+
25
+ globalThis.__hubTestActors = [
26
+ { id: 'spawn', components: { Player: {}, Transform: { x: 0, y: 1, z: 0 } } },
27
+ { id: 'door-race', components: { HubDoor: { id: 'race', to: 'race' }, Transform: { x: -28, y: 1.4, z: 0, rotationY: 90 } } },
28
+ ];
29
+ const castleJson = {
30
+ server: { maxPlayers: 8 },
31
+ hub: {
32
+ contract: 1,
33
+ title: 'Town',
34
+ places: { race: { title: 'Foot Race', scene: 'scenes/race.scene', result: { outcomes: ['win', 'lose'] } } },
35
+ },
36
+ };
37
+ const sceneExists = (scene) => ['scenes/main.scene', 'scenes/race.scene'].includes(scene);
38
+ const ROSTER = ['p1', 'p2', 'p3'].map((playerId, i) => ({ playerId, userId: String(101 + i), username: playerId }));
39
+
40
+ function boot({ sessionId = 'dev:test', hiddenPair = null } = {}) {
41
+ const h = layer(castleJson, {}, {}, sceneExists);
42
+ const sent = [];
43
+ const session = {
44
+ sessionId,
45
+ players: ROSTER,
46
+ send: (playerId, msg) => sent.push([playerId, msg]),
47
+ broadcast: (msg) => sent.push(['*', msg]),
48
+ ...(hiddenPair
49
+ ? { hub: { place: 'main', arrival: () => null, readResults: async () => ({ players: {}, boards: {} }), updatePad: async () => ({}), reportResult: async () => ({}), hiddenPair } }
50
+ : {}),
51
+ };
52
+ h.sceneFor(session);
53
+ const players = new Map(ROSTER.map(({ playerId }) => [playerId, { at: { x: 0, y: 1, z: 0 }, facing: 0, body: { setTranslation() {} } }]));
54
+ const world = { timeMs: 0, players, bodies: new Map(), scenePath: 'scenes/main.scene' };
55
+ h.ready(world);
56
+ for (const { playerId } of ROSTER) {
57
+ h.message(session, { playerId }, { k: 'hub-hello', avatar: 0 });
58
+ }
59
+ const realNow = Date.now;
60
+ Date.now = () => realNow() + 5000;
61
+ h.place(world, []);
62
+ Date.now = realNow;
63
+ sent.length = 0;
64
+ return { h, session, world, sent };
65
+ }
66
+ const say = (b, playerId, data) => b.h.message(b.session, { playerId }, { k: 'hub-say', ...data });
67
+ const said = (b) => b.sent.filter(([, msg]) => msg.k === 'hub-said');
68
+
69
+ let n = 0;
70
+
71
+ // A reaction and a phrase go to everyone; text, an unknown id and a game's
72
+ // phrase in a town go nowhere.
73
+ {
74
+ const b = boot();
75
+ say(b, 'p1', { r: 2 });
76
+ say(b, 'p2', { p: 'here' });
77
+ say(b, 'p3', { p: 'gg' });
78
+ say(b, 'p3', { p: 'nonsense', text: 'hello' });
79
+ say(b, 'p3', { r: 99 });
80
+ say(b, 'p3', { text: 'free text' });
81
+ assert.deepEqual(said(b), [
82
+ ['*', { k: 'hub-said', player: 'p1', r: 2 }],
83
+ ['*', { k: 'hub-said', player: 'p2', p: 'here' }],
84
+ ]);
85
+ n += 1;
86
+
87
+ // "Let's play" names the door the speaker stands by, or none.
88
+ say(b, 'p3', { p: 'play' });
89
+ Object.assign(b.world.players.get('p3').at, { x: -22, z: 2 });
90
+ say(b, 'p3', { p: 'play' });
91
+ assert.deepEqual(said(b).slice(2).map(([, msg]) => msg.door), [null, 'race']);
92
+ n += 1;
93
+
94
+ // A burst of three, then no more until the limit refills.
95
+ say(b, 'p1', { r: 0 });
96
+ say(b, 'p1', { r: 0 });
97
+ say(b, 'p1', { r: 0 });
98
+ assert.equal(said(b).filter(([, msg]) => msg.player === 'p1').length, 3);
99
+ n += 1;
100
+ }
101
+
102
+ // Never across a block: the hidden player is not told, everyone else is.
103
+ {
104
+ const b = boot({ hiddenPair: (x, y) => [x, y].sort().join() === 'p1,p2' });
105
+ say(b, 'p1', { r: 1 });
106
+ assert.deepEqual(said(b).map(([to]) => to).sort(), ['p1', 'p3']);
107
+ n += 1;
108
+ }
109
+
110
+ // A game has its own phrases.
111
+ {
112
+ const b = boot({ sessionId: 'dev:hub.race.abc123' });
113
+ say(b, 'p1', { p: 'gg' });
114
+ say(b, 'p1', { p: 'play' });
115
+ assert.deepEqual(said(b).map(([, msg]) => msg.p), ['gg']);
116
+ n += 1;
117
+ }
118
+
119
+ // The client words what was said: bubbles, the feed, and "Over here!" heard.
120
+ {
121
+ HubState.hub.place = { title: 'Town', result: null };
122
+ HubState.hub.doors = [{ id: 'race', title: 'Foot Race' }];
123
+ const s = makeSay();
124
+ assert.deepEqual(s.phrases().map(([id]) => id), ['here', 'play', 'wait', 'yes', 'no']);
125
+ const heard = [];
126
+ HubState.hub.saidEvents.push({ player: 'p2', p: 'play', door: 'race' }, { player: 'p2', p: 'here' }, { player: 'me', r: 0 }, { player: 'p9', p: 'here' }, { player: 'p2', p: 'bogus' });
127
+ s.frame(1000, 'me', (id) => ({ p2: 'bo' })[id] ?? null, (id) => heard.push(id));
128
+ assert.deepEqual(s.feed().map((line) => line.text), ["bo: Let's play Foot Race", 'bo: Over here!']);
129
+ assert.deepEqual(heard, ['p2'], 'a player this client does not know is not heard');
130
+ const scene = { multiplayer: { avatars: { actors: new Map([['p2', 'a2']]) } }, getActor: (id) => ({ components: { Transform: { x: id === 'a2' ? 5 : 0, y: 1, z: 0 } } }) };
131
+ const items = s.labels(scene, { id: 'local' }, null, null, (cam, x) => ({ x, y: 0 }), 'me');
132
+ assert.deepEqual(items.map((i) => [i.id, i.text]), [['said:p2', 'Over here!'], ['react:me', REACTIONS[0]]]);
133
+ s.frame(20000, 'me', () => null);
134
+ assert.deepEqual([s.feed(), s.labels(scene, { id: 'local' }, null, null, () => null, 'me')], [[], []]);
135
+ // A chat line is a bubble and a feed line; another player's can be reported.
136
+ s.chat(21000, 'p2', 'bo', { id: 'm1', text: 'hello there', mine: false }, 6000);
137
+ s.chat(21000, 'me', 'You', { id: 'm2', text: 'hi', mine: true }, 6000);
138
+ assert.deepEqual(s.feed(), [{ text: 'bo: hello there', id: 'm1' }, { text: 'You: hi', id: null }]);
139
+ // Leaving the place leaves what was said there.
140
+ HubState.hub.saidEvents.push({ player: 'p2', p: 'here' });
141
+ s.frame(30000, 'me', () => 'bo');
142
+ s.clear();
143
+ assert.deepEqual(s.feed(), []);
144
+ HubState.hub.place = null;
145
+ HubState.hub.doors = [];
146
+ n += 1;
147
+ }
148
+
149
+ console.log(`test-say: ${n} passed`);