castle-web-cli 0.4.231 → 0.4.233

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 (368) hide show
  1. package/dist/agent-failures.d.ts +8 -0
  2. package/dist/agent-failures.js +19 -2
  3. package/dist/agent-prompts.d.ts +1 -1
  4. package/dist/agent-prompts.js +10 -3
  5. package/dist/agent.d.ts +64 -3
  6. package/dist/agent.js +170 -81
  7. package/dist/castle-host/host.d.ts +1 -0
  8. package/dist/castle-host/host.js +51 -3
  9. package/dist/castleJson.d.ts +4 -2
  10. package/dist/castleJson.js +2 -0
  11. package/dist/check.d.ts +3 -1
  12. package/dist/check.js +11 -1
  13. package/dist/devSessionStorage.js +47 -1
  14. package/dist/importPinMismatch.d.ts +22 -0
  15. package/dist/importPinMismatch.js +77 -0
  16. package/dist/importRegression.js +3 -15
  17. package/dist/imports.d.ts +10 -3
  18. package/dist/imports.js +88 -20
  19. package/dist/index.js +5 -4
  20. package/dist/init.js +1 -1
  21. package/dist/platformDoc.js +22 -0
  22. package/dist/routerFences.d.ts +5 -0
  23. package/dist/routerFences.js +12 -2
  24. package/dist/save-deck.d.ts +5 -0
  25. package/dist/save-deck.js +37 -4
  26. package/dist/serve.js +3 -0
  27. package/dist/serverBundle.js +5 -3
  28. package/dist/shell/assets/{index-D2tWM896.js → index-BjOxR-lW.js} +4 -4
  29. package/dist/shell/assets/index-MVLfNYd9.css +1 -0
  30. package/dist/shell/index.html +2 -2
  31. package/dist/taskSteer.d.ts +13 -0
  32. package/dist/taskSteer.js +22 -2
  33. package/kits/base/castle.json +1 -1
  34. package/kits/base/package-lock.json +1 -1
  35. package/kits/base/sdk/README.md +79 -2
  36. package/kits/base/sdk/avatar.d.ts +6 -2
  37. package/kits/base/sdk/avatar.js +12 -4
  38. package/kits/base/sdk/castle.d.ts +1 -1
  39. package/kits/base/sdk/commands.d.ts +14 -0
  40. package/kits/base/sdk/hub.d.ts +6 -2
  41. package/kits/base/sdk/hub.js +13 -0
  42. package/kits/base/sdk/multiplayer.js +17 -1
  43. package/kits/base/sdk/save.js +2 -1
  44. package/kits/base/sdk/server/hub.d.ts +87 -0
  45. package/kits/base/sdk/server/hub.js +70 -0
  46. package/kits/base/sdk/server/platformHandle.d.ts +31 -1
  47. package/kits/base/sdk/server/storage.d.ts +13 -0
  48. package/kits/base/sdk/server/storage.js +46 -0
  49. package/kits/base/sdk/server/wrapper.d.ts +36 -5
  50. package/kits/base/sdk/server/wrapper.js +83 -8
  51. package/kits/hub-game/CLAUDE.md +352 -12
  52. package/kits/hub-game/blueprints/avatar-10.scene +28 -0
  53. package/kits/hub-game/blueprints/avatar-11.scene +28 -0
  54. package/kits/hub-game/blueprints/avatar-12.scene +28 -0
  55. package/kits/hub-game/blueprints/avatar-13.scene +28 -0
  56. package/kits/hub-game/blueprints/avatar-14.scene +28 -0
  57. package/kits/hub-game/blueprints/avatar-15.scene +28 -0
  58. package/kits/hub-game/blueprints/avatar-4.scene +28 -0
  59. package/kits/hub-game/blueprints/avatar-5.scene +28 -0
  60. package/kits/hub-game/blueprints/avatar-6.scene +28 -0
  61. package/kits/hub-game/blueprints/avatar-7.scene +28 -0
  62. package/kits/hub-game/blueprints/avatar-8.scene +28 -0
  63. package/kits/hub-game/blueprints/avatar-9.scene +28 -0
  64. package/kits/hub-game/code/client/actorBudget.js +47 -0
  65. package/kits/hub-game/code/client/attention.js +11 -2
  66. package/kits/hub-game/code/client/avatar.js +345 -32
  67. package/kits/hub-game/code/client/avatarCap.js +228 -0
  68. package/kits/hub-game/code/client/avatarColors.js +70 -0
  69. package/kits/hub-game/code/client/avatarFaces.js +21 -0
  70. package/kits/hub-game/code/client/avatarShare.js +251 -0
  71. package/kits/hub-game/code/client/ballot.js +467 -0
  72. package/kits/hub-game/code/client/build.js +22 -0
  73. package/kits/hub-game/code/client/chat.js +12 -0
  74. package/kits/hub-game/code/client/chrome.js +1334 -50
  75. package/kits/hub-game/code/client/companion.js +716 -0
  76. package/kits/hub-game/code/client/companionDress.js +99 -0
  77. package/kits/hub-game/code/client/decor.js +509 -0
  78. package/kits/hub-game/code/client/earned.js +131 -0
  79. package/kits/hub-game/code/client/emoteBubble.js +75 -0
  80. package/kits/hub-game/code/client/grab.js +363 -20
  81. package/kits/hub-game/code/client/hats.js +26 -0
  82. package/kits/hub-game/code/client/hookLayer.js +328 -15
  83. package/kits/hub-game/code/client/hudClear.js +126 -0
  84. package/kits/hub-game/code/client/idle.js +163 -22
  85. package/kits/hub-game/code/client/interestCap.js +81 -0
  86. package/kits/hub-game/code/client/keys.js +36 -0
  87. package/kits/hub-game/code/client/labelsNear.js +205 -0
  88. package/kits/hub-game/code/client/menuIcons.js +34 -0
  89. package/kits/hub-game/code/client/pack.js +6 -2
  90. package/kits/hub-game/code/client/packFormat.js +3 -3
  91. package/kits/hub-game/code/client/rehello.js +167 -0
  92. package/kits/hub-game/code/client/shot.js +92 -0
  93. package/kits/hub-game/code/client/sound.js +63 -8
  94. package/kits/hub-game/code/client/soundscape.js +1151 -0
  95. package/kits/hub-game/code/client/soundscapeActions.js +99 -0
  96. package/kits/hub-game/code/client/soundscapeAmbience.js +453 -0
  97. package/kits/hub-game/code/client/soundscapeBaked.js +143 -0
  98. package/kits/hub-game/code/client/soundscapeBusker.js +217 -0
  99. package/kits/hub-game/code/client/soundscapeCourt.js +200 -0
  100. package/kits/hub-game/code/client/soundscapeCrowd.js +331 -0
  101. package/kits/hub-game/code/client/soundscapeCues.js +362 -0
  102. package/kits/hub-game/code/client/soundscapeMap.js +181 -0
  103. package/kits/hub-game/code/client/soundscapeMarket.js +176 -0
  104. package/kits/hub-game/code/client/soundscapeMusic.js +126 -0
  105. package/kits/hub-game/code/client/soundscapeMusicEngine.js +460 -0
  106. package/kits/hub-game/code/client/soundscapeSimlish.js +737 -0
  107. package/kits/hub-game/code/client/soundscapeSongs.js +127 -0
  108. package/kits/hub-game/code/client/soundscapeSongsRound3.js +63 -0
  109. package/kits/hub-game/code/client/soundscapeSynthesis.js +168 -0
  110. package/kits/hub-game/code/client/soundscapeUi.js +211 -0
  111. package/kits/hub-game/code/client/soundscapeVoice.js +1209 -0
  112. package/kits/hub-game/code/client/soundscapeWaves.js +206 -0
  113. package/kits/hub-game/code/client/spin.js +101 -0
  114. package/kits/hub-game/code/client/state.js +131 -1
  115. package/kits/hub-game/code/client/stuck.js +259 -0
  116. package/kits/hub-game/code/client/talk.js +538 -47
  117. package/kits/hub-game/code/client/tiers.js +541 -27
  118. package/kits/hub-game/code/client/toastBand.js +37 -0
  119. package/kits/hub-game/code/client/together.js +98 -1
  120. package/kits/hub-game/code/client/visibility.js +43 -0
  121. package/kits/hub-game/code/client/wear.js +470 -97
  122. package/kits/hub-game/code/client/yours.js +199 -0
  123. package/kits/hub-game/code/companionSpec.js +45 -0
  124. package/kits/hub-game/code/privateText.js +7 -0
  125. package/kits/hub-game/code/server/actions.js +133 -0
  126. package/kits/hub-game/code/server/actors.js +14 -1
  127. package/kits/hub-game/code/server/ask.js +155 -0
  128. package/kits/hub-game/code/server/ballot.js +539 -0
  129. package/kits/hub-game/code/server/cells.js +100 -0
  130. package/kits/hub-game/code/server/companion.js +82 -0
  131. package/kits/hub-game/code/server/cues.js +70 -0
  132. package/kits/hub-game/code/server/decor.js +269 -0
  133. package/kits/hub-game/code/server/earned.js +461 -0
  134. package/kits/hub-game/code/server/friendBests.js +166 -0
  135. package/kits/hub-game/code/server/friendsAmong.js +232 -0
  136. package/kits/hub-game/code/server/grab.js +296 -11
  137. package/kits/hub-game/code/server/history.js +5 -0
  138. package/kits/hub-game/code/server/hub.js +1317 -52
  139. package/kits/hub-game/code/server/idle.js +30 -11
  140. package/kits/hub-game/code/server/index.js +2 -2
  141. package/kits/hub-game/code/server/labels.js +28 -2
  142. package/kits/hub-game/code/server/manifest.js +424 -8
  143. package/kits/hub-game/code/server/menu.js +133 -0
  144. package/kits/hub-game/code/server/npcs.js +830 -80
  145. package/kits/hub-game/code/server/party.js +13 -2
  146. package/kits/hub-game/code/server/players.js +22 -0
  147. package/kits/hub-game/code/server/say.js +8 -2
  148. package/kits/hub-game/code/server/shot.js +110 -0
  149. package/kits/hub-game/code/server/standing.js +124 -0
  150. package/kits/hub-game/code/server/vis.js +113 -0
  151. package/kits/hub-game/code/server/wear.js +128 -11
  152. package/kits/hub-game/code/server/yours.js +59 -0
  153. package/kits/hub-game/code/shared/cues.js +9 -0
  154. package/kits/hub-game/code/shared/decor.js +41 -0
  155. package/kits/hub-game/code/shared/shot.js +19 -0
  156. package/kits/hub-game/code/shared/spin.js +57 -0
  157. package/kits/hub-game/code/systems/hub.js +1083 -85
  158. package/kits/hub-game/docs/content-pack.md +1 -1
  159. package/kits/hub-game/docs/contract.md +2440 -61
  160. package/kits/hub-game/docs/home-pieces.md +64 -0
  161. package/kits/hub-game/models/avatar-10.pxmodel +28 -0
  162. package/kits/hub-game/models/avatar-11.pxmodel +28 -0
  163. package/kits/hub-game/models/avatar-12.pxmodel +27 -0
  164. package/kits/hub-game/models/avatar-13.pxmodel +24 -0
  165. package/kits/hub-game/models/avatar-14.pxmodel +29 -0
  166. package/kits/hub-game/models/avatar-15.pxmodel +24 -0
  167. package/kits/hub-game/models/avatar-4.pxmodel +29 -0
  168. package/kits/hub-game/models/avatar-5.pxmodel +27 -0
  169. package/kits/hub-game/models/avatar-6.pxmodel +26 -0
  170. package/kits/hub-game/models/avatar-7.pxmodel +25 -0
  171. package/kits/hub-game/models/avatar-8.pxmodel +24 -0
  172. package/kits/hub-game/models/avatar-9.pxmodel +30 -0
  173. package/kits/hub-game/models/earned/badge-round.pxmodel +100 -0
  174. package/kits/hub-game/models/earned/badge-shield.pxmodel +117 -0
  175. package/kits/hub-game/models/earned/badge-star.pxmodel +87 -0
  176. package/kits/hub-game/models/earned/belt-plain.pxmodel +72 -0
  177. package/kits/hub-game/models/earned/held-ball.pxmodel +43 -0
  178. package/kits/hub-game/models/earned/held-lantern.pxmodel +85 -0
  179. package/kits/hub-game/models/earned/held-net.pxmodel +57 -0
  180. package/kits/hub-game/models/earned/held-trophy.pxmodel +109 -0
  181. package/kits/hub-game/models/earned/pin-round.pxmodel +62 -0
  182. package/kits/hub-game/models/earned/sash-plain.pxmodel +66 -0
  183. package/kits/hub-game/models/earned/square/belt-plain.pxmodel +104 -0
  184. package/kits/hub-game/models/earned/square/sash-plain.pxmodel +98 -0
  185. package/kits/hub-game/models/hats/bat-clip.pxmodel +206 -0
  186. package/kits/hub-game/models/hats/beekeeper.pxmodel +149 -0
  187. package/kits/hub-game/models/hats/cap.pxmodel +162 -0
  188. package/kits/hub-game/models/hats/captain.pxmodel +178 -0
  189. package/kits/hub-game/models/hats/crooked-top.pxmodel +172 -0
  190. package/kits/hub-game/models/hats/flower-crown.pxmodel +451 -0
  191. package/kits/hub-game/models/hats/foam-crown.pxmodel +179 -0
  192. package/kits/hub-game/models/hats/lantern.pxmodel +230 -0
  193. package/kits/hub-game/models/hats/laurel.pxmodel +533 -0
  194. package/kits/hub-game/models/hats/lighthouse.pxmodel +96 -0
  195. package/kits/hub-game/models/hats/sailor.pxmodel +141 -0
  196. package/kits/hub-game/models/hats/souwester.pxmodel +131 -0
  197. package/kits/hub-game/models/hats/straw.pxmodel +139 -0
  198. package/kits/hub-game/models/hats/sunflower.pxmodel +406 -0
  199. package/kits/hub-game/models/hats/sweatband.pxmodel +112 -0
  200. package/kits/hub-game/models/hats/witch.pxmodel +147 -0
  201. package/kits/hub-game/package-lock.json +2 -0
  202. package/kits/hub-game/package.json +2 -1
  203. package/kits/hub-game/scripts/check.mjs +78 -11
  204. package/kits/hub-game/scripts/companion-follower-wire.json +1 -0
  205. package/kits/hub-game/scripts/fixtures/decor-keyless-draw.json +121 -0
  206. package/kits/hub-game/scripts/fixtures/decor-keyless.json +293 -0
  207. package/kits/hub-game/scripts/fixtures/engine-models-stub.mjs +23 -0
  208. package/kits/hub-game/scripts/fixtures/fake-dom.mjs +107 -0
  209. package/kits/hub-game/scripts/fixtures/interest-legacy.json +385 -0
  210. package/kits/hub-game/scripts/fixtures/npc-notes-v1.77.jsonl +4 -0
  211. package/kits/hub-game/scripts/fixtures/vis-shared-wire.json +1 -0
  212. package/kits/hub-game/scripts/gen-avatars.mjs +933 -16
  213. package/kits/hub-game/scripts/gen-earned.mjs +285 -0
  214. package/kits/hub-game/scripts/gen-hats.mjs +827 -0
  215. package/kits/hub-game/scripts/grab.before-orbit.mjs +400 -0
  216. package/kits/hub-game/scripts/soundscape-wave-analysis.mjs +125 -0
  217. package/kits/hub-game/scripts/test-actions-client.mjs +488 -0
  218. package/kits/hub-game/scripts/test-actions.mjs +298 -0
  219. package/kits/hub-game/scripts/test-actor-budget.mjs +44 -0
  220. package/kits/hub-game/scripts/test-actors.mjs +19 -17
  221. package/kits/hub-game/scripts/test-ask-client.mjs +358 -0
  222. package/kits/hub-game/scripts/test-ask.mjs +269 -0
  223. package/kits/hub-game/scripts/test-attention.mjs +10 -1
  224. package/kits/hub-game/scripts/test-avatar-cap.mjs +379 -0
  225. package/kits/hub-game/scripts/test-avatar-colors.mjs +72 -0
  226. package/kits/hub-game/scripts/test-avatar-models.mjs +181 -0
  227. package/kits/hub-game/scripts/test-avatar-share.mjs +318 -0
  228. package/kits/hub-game/scripts/test-avatar-wire.mjs +753 -0
  229. package/kits/hub-game/scripts/test-avatar.mjs +540 -7
  230. package/kits/hub-game/scripts/test-ballot.mjs +1001 -0
  231. package/kits/hub-game/scripts/test-build.mjs +31 -0
  232. package/kits/hub-game/scripts/test-cells.mjs +71 -0
  233. package/kits/hub-game/scripts/test-chat.mjs +694 -0
  234. package/kits/hub-game/scripts/test-classic.mjs +2 -0
  235. package/kits/hub-game/scripts/test-companion-chat.mjs +198 -0
  236. package/kits/hub-game/scripts/test-companion-client.mjs +1167 -0
  237. package/kits/hub-game/scripts/test-companion.mjs +259 -0
  238. package/kits/hub-game/scripts/test-decor-client.mjs +303 -0
  239. package/kits/hub-game/scripts/test-decor.mjs +457 -0
  240. package/kits/hub-game/scripts/test-earned-client.mjs +459 -0
  241. package/kits/hub-game/scripts/test-earned.mjs +1194 -0
  242. package/kits/hub-game/scripts/test-emote-bubble.mjs +110 -0
  243. package/kits/hub-game/scripts/test-friend-bests.mjs +435 -0
  244. package/kits/hub-game/scripts/test-grab-client.mjs +186 -3
  245. package/kits/hub-game/scripts/test-grab-orbit.mjs +918 -0
  246. package/kits/hub-game/scripts/test-grab.mjs +864 -3
  247. package/kits/hub-game/scripts/test-hats.mjs +483 -0
  248. package/kits/hub-game/scripts/test-hub-leaving.mjs +203 -0
  249. package/kits/hub-game/scripts/test-hub-ops.mjs +439 -2
  250. package/kits/hub-game/scripts/test-hub-trip.mjs +1228 -1
  251. package/kits/hub-game/scripts/test-hud-clear.mjs +1000 -0
  252. package/kits/hub-game/scripts/test-idle.mjs +361 -1
  253. package/kits/hub-game/scripts/test-interest-cap.mjs +36 -0
  254. package/kits/hub-game/scripts/test-interest-cells.mjs +493 -0
  255. package/kits/hub-game/scripts/test-interest-client.mjs +118 -0
  256. package/kits/hub-game/scripts/test-keys.mjs +46 -1
  257. package/kits/hub-game/scripts/test-labels.mjs +596 -0
  258. package/kits/hub-game/scripts/test-manifest.mjs +282 -0
  259. package/kits/hub-game/scripts/test-menu-client.mjs +480 -0
  260. package/kits/hub-game/scripts/test-menu.mjs +296 -0
  261. package/kits/hub-game/scripts/test-npcs.mjs +1718 -12
  262. package/kits/hub-game/scripts/test-object-entry.mjs +76 -0
  263. package/kits/hub-game/scripts/test-pack.mjs +7 -0
  264. package/kits/hub-game/scripts/test-party.mjs +22 -1
  265. package/kits/hub-game/scripts/test-picker.mjs +314 -0
  266. package/kits/hub-game/scripts/test-rehello.mjs +1039 -0
  267. package/kits/hub-game/scripts/test-relations-among.mjs +222 -0
  268. package/kits/hub-game/scripts/test-relations-refresh.mjs +188 -0
  269. package/kits/hub-game/scripts/test-say.mjs +59 -2
  270. package/kits/hub-game/scripts/test-shot.mjs +756 -0
  271. package/kits/hub-game/scripts/test-soundscape.mjs +5432 -0
  272. package/kits/hub-game/scripts/test-spin.mjs +314 -0
  273. package/kits/hub-game/scripts/test-standing.mjs +416 -0
  274. package/kits/hub-game/scripts/test-stuck.mjs +1903 -0
  275. package/kits/hub-game/scripts/test-tier-cells.mjs +706 -0
  276. package/kits/hub-game/scripts/test-tiers-render.mjs +339 -0
  277. package/kits/hub-game/scripts/test-tiers.mjs +666 -3
  278. package/kits/hub-game/scripts/test-toast-band.mjs +61 -0
  279. package/kits/hub-game/scripts/test-try-on.mjs +940 -0
  280. package/kits/hub-game/scripts/test-vis.mjs +343 -0
  281. package/kits/hub-game/scripts/test-visibility.mjs +134 -0
  282. package/kits/hub-game/scripts/test-yours-chrome.mjs +166 -0
  283. package/kits/hub-game/scripts/test-yours.mjs +372 -0
  284. package/kits/multiplayer-3d/CLAUDE.md +50 -0
  285. package/kits/multiplayer-3d/castle.json +5 -5
  286. package/kits/multiplayer-3d/code/client/avatars.js +7 -0
  287. package/kits/multiplayer-3d/code/client/gait.js +65 -0
  288. package/kits/multiplayer-3d/code/client/nameTags.js +59 -4
  289. package/kits/multiplayer-3d/code/options.js +5 -0
  290. package/kits/multiplayer-3d/code/server/ownership.js +10 -2
  291. package/kits/multiplayer-3d/code/server/sceneBodies.js +12 -0
  292. package/kits/multiplayer-3d/code/server/world.js +7 -0
  293. package/kits/multiplayer-3d/code/systems/multiplayer.js +74 -4
  294. package/kits/multiplayer-3d/package.json +2 -1
  295. package/kits/multiplayer-3d/scripts/fixtures/README.md +11 -0
  296. package/kits/multiplayer-3d/scripts/fixtures/ownership-legacy.json +2 -0
  297. package/kits/multiplayer-3d/scripts/ownership-harness.mjs +102 -0
  298. package/kits/multiplayer-3d/scripts/ownership-loader.mjs +46 -0
  299. package/kits/multiplayer-3d/scripts/ownership-worker.mjs +15 -0
  300. package/kits/multiplayer-3d/scripts/record-ownership-session.mjs +98 -0
  301. package/kits/multiplayer-3d/scripts/test-eligible-claims.mjs +167 -0
  302. package/kits/multiplayer-3d/scripts/test-own-name-tag.mjs +247 -0
  303. package/kits/physics-2d/CLAUDE.md +9 -0
  304. package/kits/physics-2d/castle.json +2 -2
  305. package/kits/physics-2d/engine/scene.js +288 -1
  306. package/kits/physics-2d/engine/skyKey.js +129 -0
  307. package/kits/physics-2d/engine/stillClock.js +106 -0
  308. package/kits/physics-2d/engine/updateBudget.js +21 -0
  309. package/kits/physics-2d/scripts/test-skip-unchanged.mjs +370 -1
  310. package/kits/physics-3d/CLAUDE.md +1391 -0
  311. package/kits/physics-3d/behaviors/Player.jsx +921 -3
  312. package/kits/physics-3d/castle.json +3 -3
  313. package/kits/physics-3d/docs/pxmodel-format.md +12 -0
  314. package/kits/physics-3d/engine3d/Scene3DPlayer.jsx +98 -10
  315. package/kits/physics-3d/engine3d/actornode.js +86 -0
  316. package/kits/physics-3d/engine3d/animationBudget.js +23 -0
  317. package/kits/physics-3d/engine3d/editorWorld.js +6 -0
  318. package/kits/physics-3d/engine3d/emptynodes.js +232 -0
  319. package/kits/physics-3d/engine3d/framecap.js +96 -0
  320. package/kits/physics-3d/engine3d/gait.js +118 -0
  321. package/kits/physics-3d/engine3d/kithooks.js +47 -0
  322. package/kits/physics-3d/engine3d/look.js +507 -48
  323. package/kits/physics-3d/engine3d/lookbake.js +208 -0
  324. package/kits/physics-3d/engine3d/materialpresets.js +2 -0
  325. package/kits/physics-3d/engine3d/motion.js +39 -10
  326. package/kits/physics-3d/engine3d/nearshadows.js +37 -19
  327. package/kits/physics-3d/engine3d/optimize.js +58 -5
  328. package/kits/physics-3d/engine3d/orbitinput.js +755 -0
  329. package/kits/physics-3d/engine3d/orbitlens.js +48 -0
  330. package/kits/physics-3d/engine3d/ownmodel.js +213 -0
  331. package/kits/physics-3d/engine3d/pxmodel.js +42 -11
  332. package/kits/physics-3d/engine3d/shadowfade.js +882 -0
  333. package/kits/physics-3d/engine3d/sky.js +547 -0
  334. package/kits/physics-3d/engine3d/skylife.js +306 -0
  335. package/kits/physics-3d/engine3d/unchanged.js +11 -0
  336. package/kits/physics-3d/engine3d/viewrange.js +52 -0
  337. package/kits/physics-3d/engine3d/world3d.js +870 -87
  338. package/kits/physics-3d/main.jsx +5 -0
  339. package/kits/physics-3d/package.json +1 -1
  340. package/kits/physics-3d/scripts/Player.before-orbit.mjs +310 -0
  341. package/kits/physics-3d/scripts/Scene3DPlayer.before-orbit.jsx +337 -0
  342. package/kits/physics-3d/scripts/look-baseline.mjs +155 -0
  343. package/kits/physics-3d/scripts/test-camera-follow.mjs +12 -2
  344. package/kits/physics-3d/scripts/test-camera-orbit.mjs +4588 -0
  345. package/kits/physics-3d/scripts/test-drop-empty-parts.mjs +787 -0
  346. package/kits/physics-3d/scripts/test-frame-cap-bob.mjs +435 -0
  347. package/kits/physics-3d/scripts/test-frame-cap.mjs +452 -0
  348. package/kits/physics-3d/scripts/test-gait.mjs +387 -0
  349. package/kits/physics-3d/scripts/test-look-bake.mjs +1056 -0
  350. package/kits/physics-3d/scripts/test-model-parse-cache.mjs +92 -0
  351. package/kits/physics-3d/scripts/test-near-shadows.mjs +3 -0
  352. package/kits/physics-3d/scripts/test-orbit-shadow-fade.mjs +1920 -0
  353. package/kits/physics-3d/scripts/test-orbit-shift-lock.mjs +545 -0
  354. package/kits/physics-3d/scripts/test-orbit-shot.mjs +583 -0
  355. package/kits/physics-3d/scripts/test-orbit-view.mjs +230 -0
  356. package/kits/physics-3d/scripts/test-skip-empty-nodes.mjs +845 -0
  357. package/kits/physics-3d/scripts/test-sky.mjs +2470 -0
  358. package/kits/physics-3d/scripts/test-static-actors.mjs +13 -1
  359. package/kits/physics-3d/scripts/test-static-passes.mjs +1 -1
  360. package/kits/physics-3d/scripts/test-static-scenery.mjs +2171 -0
  361. package/kits/physics-3d/scripts/test-steady-hover.mjs +635 -0
  362. package/kits/physics-3d/scripts/test-view-range.mjs +260 -0
  363. package/kits/physics-3d/systems/physics3d.js +474 -12
  364. package/kits/real-time/castle.json +2 -2
  365. package/kits/real-time/code/server/gameHooks.js +7 -0
  366. package/kits/real-time/code/server/session.js +97 -19
  367. package/package.json +2 -2
  368. package/dist/shell/assets/index-Cf61cNfT.css +0 -1
@@ -0,0 +1,1001 @@
1
+ // The choice card (contract v1.68, `hub.ballot`): the manifest's fixed lines,
2
+ // the server's card checks, per-viewer views, blocks, the tap checks and
3
+ // limits and the `ballot` hook; a place without the key is exactly what it
4
+ // was; and the client words the card from ids. `npm run check` runs it; it
5
+ // 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
+ const avatarsStub = 'export function dropMissing() {}';
12
+ register(
13
+ `data:text/javascript,${encodeURIComponent(`
14
+ import { readFile } from 'node:fs/promises';
15
+ import { fileURLToPath } from 'node:url';
16
+ const stubs = {
17
+ '@imports/castle.multiplayer-3d/code/server/sceneBodies.js': 'data:text/javascript,${encodeURIComponent(sceneStub)}',
18
+ '@imports/castle.multiplayer-3d/code/client/avatars.js': 'data:text/javascript,${encodeURIComponent(avatarsStub)}',
19
+ 'castle-web-sdk': ${JSON.stringify(new URL('./fixtures/sdk-stub.mjs', import.meta.url).href)},
20
+ };
21
+ export async function resolve(specifier, context, next) {
22
+ if (['pxmodel', 'optimize', 'look', 'lookbake'].some((m) => specifier === '@imports/castle.physics-3d/engine3d/' + m)) return { url: ${JSON.stringify(new URL('./fixtures/engine-models-stub.mjs', import.meta.url).href)}, shortCircuit: true };
23
+ if (specifier === '@imports/castle.physics-3d/engine3d/orbitlens.js') return { url: ${JSON.stringify(new URL('../../physics-3d/engine3d/orbitlens.js', import.meta.url).href)}, shortCircuit: true };
24
+ if (stubs[specifier]) {
25
+ return { url: stubs[specifier], shortCircuit: true };
26
+ }
27
+ return next(specifier, context);
28
+ }
29
+ // The hook layer reads the deck's castle.json through the bundler.
30
+ export async function load(url, context, next) {
31
+ if (url.endsWith('/code/client/hookLayer.js')) {
32
+ const source = await readFile(fileURLToPath(url), 'utf8');
33
+ return { format: 'module', source: source.replaceAll('import.meta.glob(', '(() => ({}))('), shortCircuit: true };
34
+ }
35
+ return next(url, context);
36
+ }
37
+ `)}`,
38
+ );
39
+ const { layer } = await import('../code/server/hub.js');
40
+ const { readManifest, publicPlace } = await import('../code/server/manifest.js');
41
+ const Ballot = await import('../code/server/ballot.js');
42
+ const Client = await import('../code/client/ballot.js');
43
+ const hookLayer = (await import('../code/client/hookLayer.js')).default;
44
+ const State = await import('../code/client/state.js');
45
+
46
+ globalThis.__hubTestActors = [{ id: 'spawn', components: { Player: {}, Transform: { x: 0, y: 1, z: 0 } } }];
47
+ const LINES = [
48
+ { id: 'boat', text: 'I saw them by the boat', about: 'face' },
49
+ { id: 'sus', text: 'Acting strange', about: 'face' },
50
+ { id: 'not-me', text: 'Not me!', about: 'self' },
51
+ ];
52
+ const deck = (ballot, extra = {}) => ({
53
+ server: { maxPlayers: 8 },
54
+ hub: { contract: 1, title: 'Sneaks', result: { outcomes: ['win', 'lose'] }, ...(ballot === undefined || ballot === 'absent' ? {} : { ballot }), ...extra },
55
+ });
56
+ const scenes = () => true;
57
+ const errorsOf = (ballot) => readManifest(deck(ballot), scenes).errors;
58
+ let n = 0;
59
+
60
+ // --- the manifest ---------------------------------------------------------
61
+ {
62
+ const { manifest } = readManifest(deck({ lines: LINES }), scenes);
63
+ assert.deepEqual(manifest.places.main.ballot, { lines: LINES });
64
+ assert.deepEqual(publicPlace('main', manifest.places.main).ballot, { lines: LINES });
65
+ assert.deepEqual(readManifest(deck(true), scenes).manifest.places.main.ballot, { lines: [] });
66
+ assert.deepEqual(readManifest(deck({}), scenes).manifest.places.main.ballot, { lines: [] });
67
+ for (const off of [undefined, false]) {
68
+ const place = readManifest(deck(off), scenes).manifest.places.main;
69
+ assert.ok(!('ballot' in place));
70
+ assert.ok(!('ballot' in publicPlace('main', place)));
71
+ }
72
+ // Text is trimmed; a place of its own may declare it too.
73
+ assert.equal(readManifest(deck({ lines: [{ id: 'a', text: ' Hi ', about: 'self' }] }), scenes).manifest.places.main.ballot.lines[0].text, 'Hi');
74
+ const places = readManifest(deck(undefined, { places: { meet: { title: 'Meeting', scene: 'scenes/meet.scene', ballot: { lines: [LINES[2]] } } } }), scenes).manifest.places;
75
+ assert.deepEqual(places.meet.ballot, { lines: [LINES[2]] });
76
+ assert.ok(!('ballot' in places.main));
77
+ n += 1;
78
+ }
79
+ {
80
+ const line = (over) => ({ lines: [{ id: 'a', text: 'Hi', about: 'self', ...over }] });
81
+ assert.match(errorsOf('yes').join(), /ballot must be true, false or an object/);
82
+ assert.match(errorsOf({ lines: LINES, extra: 1 }).join(), /ballot\.extra is not part/);
83
+ assert.match(errorsOf(line({ id: 'Boat' })).join(), /\.id must be/);
84
+ assert.match(errorsOf(line({ id: 'a'.repeat(25) })).join(), /\.id must be/);
85
+ assert.match(errorsOf(line({ text: '' })).join(), /\.text must be 1-40/);
86
+ assert.match(errorsOf(line({ text: 'x'.repeat(41) })).join(), /\.text must be 1-40/);
87
+ assert.match(errorsOf(line({ text: 'two\nlines' })).join(), /\.text must be 1-40/);
88
+ assert.match(errorsOf(line({ about: 'them' })).join(), /\.about must be/);
89
+ assert.match(errorsOf(line({ about: undefined })).join(), /\.about must be/);
90
+ assert.match(errorsOf(line({ said: 'x' })).join(), /lines\[0\]\.said is not part/);
91
+ assert.match(errorsOf({ lines: [LINES[0], { ...LINES[1], id: 'boat' }] }).join(), /listed twice/);
92
+ assert.match(errorsOf({ lines: Array.from({ length: 33 }, (_, i) => ({ id: `l${i}`, text: 'x', about: 'self' })) }).join(), /at most 32/);
93
+ assert.deepEqual(errorsOf({ lines: Array.from({ length: 32 }, (_, i) => ({ id: `l${i}`, text: 'x', about: 'self' })) }), []);
94
+ assert.match(errorsOf({ lines: 'boat' }).join(), /at most 32/);
95
+ n += 1;
96
+ }
97
+
98
+ // --- the server -------------------------------------------------------------
99
+ const ROSTER = ['p1', 'p2', 'p3', 'p4'].map((playerId, i) => ({ playerId, userId: String(101 + i), username: ['amy', 'bo', 'cy', 'di'][i] }));
100
+ function boot({ ballot = { lines: LINES }, hiddenPair = null, hooks = {}, extra = {} } = {}) {
101
+ const calls = [];
102
+ const heard = [];
103
+ const deckHooks = {
104
+ ballot: (hub, playerId, tap) => calls.push([playerId, tap]),
105
+ message: (hub, player, data) => heard.push([player.playerId, data]),
106
+ ...hooks,
107
+ };
108
+ const h = layer(deck(ballot, extra), deckHooks, {}, scenes);
109
+ const sent = [];
110
+ const session = {
111
+ sessionId: 'dev:test',
112
+ players: ROSTER,
113
+ send: (playerId, msg) => sent.push([playerId, JSON.parse(JSON.stringify(msg))]),
114
+ broadcast: (msg) => sent.push(['*', JSON.parse(JSON.stringify(msg))]),
115
+ ...(hiddenPair
116
+ ? { hub: { place: 'main', arrival: () => null, readResults: async () => ({ players: {}, boards: {} }), updatePad: async () => ({}), reportResult: async () => ({}), hiddenPair } }
117
+ : {}),
118
+ };
119
+ h.sceneFor(session);
120
+ const players = new Map(ROSTER.map(({ playerId }) => [playerId, { at: { x: 0, y: 1, z: 0 }, facing: 0, body: { setTranslation() {} } }]));
121
+ const world = { timeMs: 0, players, bodies: new Map(), scenePath: 'scenes/main.scene' };
122
+ h.ready(world);
123
+ ROSTER.forEach(({ playerId }, i) => h.message(session, { playerId }, { k: 'hub-hello', avatar: i + 3 }));
124
+ const realNow = Date.now;
125
+ Date.now = () => realNow() + 5000;
126
+ h.place(world, []);
127
+ Date.now = realNow;
128
+ const hub = captured.ctx;
129
+ sent.length = 0;
130
+ const tick = () => {
131
+ world.timeMs += 50;
132
+ h.step(world, []);
133
+ };
134
+ return { h, hub, session, world, sent, calls, heard, tick };
135
+ }
136
+ // The deck's `hub` is the context its hooks are handed: caught from `ready`.
137
+ const captured = {};
138
+ const b2calls = [];
139
+ // A tap as the client makes it: on the card last delivered to that player,
140
+ // carrying its version (rule A), unless the test names a `v` itself.
141
+ const deliveredTo = (b, playerId) => b.sent.filter(([to, msg]) => to === playerId && msg.k === 'hub-ballot-card').at(-1)?.[1].card ?? null;
142
+ const tapAs = (b, playerId, data) =>
143
+ b.h.message(b.session, { playerId }, { k: 'hub-ballot', ...(Object.hasOwn(data, 'v') ? {} : { v: deliveredTo(b, playerId)?.v }), ...data });
144
+ const cards = (b) => b.sent.filter(([, msg]) => msg.k === 'hub-ballot-card');
145
+ const warns = [];
146
+ const realWarn = console.warn;
147
+ console.warn = (...args) => warns.push(args.join(' '));
148
+ const realLog = console.log;
149
+ console.log = () => {};
150
+
151
+ function withCtx(options = {}) {
152
+ const hooks = { ready: (hub) => { captured.ctx = hub; }, ...(options.hooks ?? {}) };
153
+ return boot({ ...options, hooks });
154
+ }
155
+
156
+ const FACES = [{ playerId: 'p1' }, { playerId: 'p2', note: 'on the bench' }, { playerId: 'p3', off: true, note: 'out' }, { playerId: 'p4', mark: '#FF0000' }];
157
+
158
+ // A card for everyone: each player is sent their own view once, with the
159
+ // kit's names and avatars; nothing more until it changes.
160
+ {
161
+ const b = withCtx();
162
+ assert.equal(b.hub.ballot(null, { title: 'Meeting', text: 'Who was it?', until: 30_000, faces: FACES, options: [{ id: 'skip', text: 'Skip' }] }), true);
163
+ b.tick();
164
+ const got = cards(b);
165
+ assert.deepEqual(got.map(([to]) => to), ['p1', 'p2', 'p3', 'p4']);
166
+ const { v: firstV, ...card } = got[0][1].card;
167
+ // The version is an opaque string: this server's epoch and a counter.
168
+ assert.match(firstV, /^[0-9a-z]+-1$/);
169
+ assert.deepEqual(card, {
170
+ mode: 'talk',
171
+ title: 'Meeting',
172
+ text: 'Who was it?',
173
+ until: 30_000,
174
+ faces: [{ id: 'p1' }, { id: 'p2', note: 'on the bench' }, { id: 'p3', note: 'out', off: true }, { id: 'p4', mark: '#ff0000' }],
175
+ options: [{ id: 'skip', text: 'Skip' }],
176
+ lines: ['boat', 'sus', 'not-me'],
177
+ feed: [],
178
+ who: { p1: { name: 'amy', avatar: 3 }, p2: { name: 'bo', avatar: 4 }, p3: { name: 'cy', avatar: 5 }, p4: { name: 'di', avatar: 6 } },
179
+ });
180
+ b.sent.length = 0;
181
+ b.tick();
182
+ assert.equal(cards(b).length, 0);
183
+ // Per-player replaces shared for that player; clearing it shows shared again.
184
+ assert.equal(b.hub.ballot(['p2', 'nobody'], { mode: 'vote', faces: FACES, picked: { face: 'p1' } }), true);
185
+ b.tick();
186
+ assert.deepEqual(cards(b).map(([to, msg]) => [to, msg.card.mode, msg.card.picked]), [['p2', 'vote', { face: 'p1' }]]);
187
+ b.sent.length = 0;
188
+ b.hub.ballot(['p2'], null);
189
+ b.tick();
190
+ assert.deepEqual(cards(b).map(([to, msg]) => [to, msg.card.mode]), [['p2', 'talk']]);
191
+ // Clearing for everyone clears every card, a player's own included.
192
+ b.hub.ballot(['p3'], { mode: 'reveal', faces: FACES });
193
+ b.tick();
194
+ b.sent.length = 0;
195
+ b.hub.ballot(null, null);
196
+ b.tick();
197
+ assert.deepEqual(cards(b).map(([to, msg]) => [to, msg.card]), [['p1', null], ['p2', null], ['p3', null], ['p4', null]]);
198
+ // A client that joins afresh is sent its card again; a second hello (an avatar change) is not.
199
+ b.hub.ballot(null, { faces: FACES });
200
+ b.tick();
201
+ b.sent.length = 0;
202
+ b.h.message(b.session, { playerId: 'p1' }, { k: 'hub-hello', avatar: 3, again: true });
203
+ b.tick();
204
+ assert.equal(cards(b).length, 0);
205
+ b.h.message(b.session, { playerId: 'p1' }, { k: 'hub-hello', avatar: 3 });
206
+ b.tick();
207
+ assert.deepEqual(cards(b).map(([to]) => to), ['p1']);
208
+ n += 1;
209
+ }
210
+
211
+ // A bad card warns once and changes nothing; so does a place without the key.
212
+ {
213
+ const b = withCtx();
214
+ const bad = [
215
+ 'card',
216
+ { mode: 'shout' },
217
+ { title: 'x'.repeat(41) },
218
+ { text: 'x'.repeat(121) },
219
+ { until: Infinity },
220
+ { faces: [{ playerId: 'p1' }, { playerId: 'p1' }] },
221
+ { faces: [{ playerId: 'p1', note: 'x'.repeat(25) }] },
222
+ { faces: [{ playerId: 'p1', votes: -1 }] },
223
+ { faces: [{ playerId: 'p1', mark: 'red' }] },
224
+ { faces: [{ playerId: 'p1', name: 'evil' }] },
225
+ { faces: Array.from({ length: 25 }, (_, i) => ({ playerId: `x${i}` })) },
226
+ { options: [{ id: 'skip', text: 'x'.repeat(33) }] },
227
+ { options: [{ id: 'Skip', text: 'Skip' }] },
228
+ { lines: ['nope'] },
229
+ { feed: [{ from: 'p1', line: 'boat' }] },
230
+ { feed: [{ from: 'p1', to: 'p2', line: 'not-me' }] },
231
+ { feed: Array.from({ length: 7 }, () => ({ from: 'p1', line: 'not-me' })) },
232
+ { picked: { face: 'p9' }, faces: FACES },
233
+ { words: 'hello' },
234
+ ];
235
+ for (const spec of bad) {
236
+ assert.equal(b.hub.ballot(null, spec), false, JSON.stringify(spec));
237
+ }
238
+ assert.equal(b.hub.ballot('p1', { faces: FACES }), false);
239
+ b.tick();
240
+ assert.equal(cards(b).length, 0);
241
+ const before = warns.length;
242
+ b.hub.ballot(null, { mode: 'shout' });
243
+ assert.equal(warns.length, before);
244
+ n += 1;
245
+ }
246
+ {
247
+ const b = withCtx({ ballot: 'absent' });
248
+ // A deck that declares no card anywhere has no `hub.ballot` at all.
249
+ assert.equal('ballot' in b.hub, false);
250
+ // Keys absent: no card is ever sent, the snapshot's place has no ballot,
251
+ // and a `hub-ballot` message is the deck's own, as it always was.
252
+ b.tick();
253
+ assert.equal(b.sent.length, 0);
254
+ assert.ok(!('ballot' in b.h.join({ playerId: 'p1' }).hubPlace));
255
+ tapAs(b, 'p1', { v: 3, face: 'p2', line: 'boat' });
256
+ assert.deepEqual(b.heard, [['p1', { k: 'hub-ballot', v: 3, face: 'p2', line: 'boat' }]]);
257
+ assert.equal(b.calls.length, 0);
258
+ n += 1;
259
+ }
260
+
261
+ // A deck with a card in another place: `hub.ballot` here refuses, once.
262
+ {
263
+ warns.length = 0;
264
+ const b = withCtx({ ballot: 'absent', extra: { places: { meet: { title: 'Meeting', scene: 'scenes/meet.scene', ballot: true } } } });
265
+ assert.equal(b.hub.ballot(null, { faces: FACES }), false);
266
+ assert.equal(b.hub.ballot(null, { faces: FACES }), false);
267
+ assert.equal(warns.filter((w) => w.includes('hub.ballot')).length, 1);
268
+ b.tick();
269
+ assert.equal(cards(b).length, 0);
270
+ n += 1;
271
+ }
272
+
273
+ // Taps: ids only, checked against the card this player was sent.
274
+ {
275
+ const b = withCtx();
276
+ b.hub.ballot(null, { faces: FACES, options: [{ id: 'skip', text: 'Skip' }], lines: ['boat', 'not-me'] });
277
+ // Nothing before the card has been sent.
278
+ tapAs(b, 'p1', { line: 'not-me' });
279
+ assert.equal(b.calls.length, 0);
280
+ b.tick();
281
+ const refused = [
282
+ { face: 'p2', line: 'not-me' }, // a line about oneself takes no face
283
+ { line: 'boat' }, // a line about a face needs one
284
+ { face: 'p3', line: 'boat' }, // an off face
285
+ { face: 'p9', line: 'boat' }, // not on the card
286
+ { face: 'p2', line: 'sus' }, // not offered now
287
+ { face: 'p2', line: 'boat', text: 'free words' }, // nothing typed is carried
288
+ { line: 'not-me', option: 'skip' },
289
+ { option: 'nope' },
290
+ { face: 'p2' }, // talk: a face alone says nothing
291
+ { face: 7, line: 'boat' },
292
+ ];
293
+ for (const data of refused) {
294
+ tapAs(b, 'p1', data);
295
+ }
296
+ assert.deepEqual(b.calls, []);
297
+ assert.deepEqual(b.heard, []);
298
+ tapAs(b, 'p1', { face: 'p2', line: 'boat' });
299
+ tapAs(b, 'p2', { line: 'not-me' });
300
+ tapAs(b, 'p3', { option: 'skip' });
301
+ assert.deepEqual(b.calls, [
302
+ ['p1', { face: 'p2', line: 'boat', option: null, mode: 'talk' }],
303
+ ['p2', { face: null, line: 'not-me', option: null, mode: 'talk' }],
304
+ ['p3', { face: null, line: null, option: 'skip', mode: 'talk' }],
305
+ ]);
306
+ // A burst of three, then one every 1.5 s.
307
+ b.calls.length = 0;
308
+ for (let i = 0; i < 5; i += 1) {
309
+ tapAs(b, 'p4', { line: 'not-me' });
310
+ }
311
+ assert.equal(b.calls.length, 3);
312
+ const realNow = Date.now;
313
+ Date.now = () => realNow() + 1600;
314
+ tapAs(b, 'p4', { line: 'not-me' });
315
+ tapAs(b, 'p4', { line: 'not-me' });
316
+ Date.now = realNow;
317
+ assert.equal(b.calls.length, 4);
318
+ n += 1;
319
+ }
320
+ {
321
+ const b = withCtx();
322
+ b.hub.ballot(null, { mode: 'vote', faces: FACES, options: [{ id: 'skip', text: 'Skip' }] });
323
+ b.hub.ballot(['p4'], { mode: 'vote', faces: FACES, picked: { option: 'skip' }, options: [{ id: 'skip', text: 'Skip' }] });
324
+ b.tick();
325
+ for (const data of [{ face: 'p2', line: 'boat' }, { line: 'not-me' }, {}, { face: 'p3' }, { face: 'p2', option: 'skip' }]) {
326
+ tapAs(b, 'p1', data);
327
+ }
328
+ tapAs(b, 'p4', { face: 'p2' }); // locked in already
329
+ assert.deepEqual(b.calls, []);
330
+ tapAs(b, 'p1', { face: 'p2' });
331
+ tapAs(b, 'p2', { option: 'skip' });
332
+ assert.deepEqual(b.calls, [
333
+ ['p1', { face: 'p2', line: null, option: null, mode: 'vote' }],
334
+ ['p2', { face: null, line: null, option: 'skip', mode: 'vote' }],
335
+ ]);
336
+ b.hub.ballot(null, { mode: 'reveal', faces: FACES.map((f, i) => ({ ...f, votes: i })) });
337
+ b.hub.ballot(['p4'], null);
338
+ b.tick();
339
+ tapAs(b, 'p1', { face: 'p2' });
340
+ assert.equal(b.calls.length, 2);
341
+ n += 1;
342
+ }
343
+ // A hook that throws is the deck's problem, not the session's.
344
+ {
345
+ const b = withCtx({ hooks: { ballot: () => { throw new Error('boom'); } } });
346
+ b.hub.ballot(null, { faces: FACES });
347
+ b.tick();
348
+ assert.doesNotThrow(() => tapAs(b, 'p1', { line: 'not-me' }));
349
+ n += 1;
350
+ }
351
+
352
+ // Blocks: a player hidden from the viewer is not on their card, no feed row
353
+ // names them, and their name is out of the deck's words.
354
+ {
355
+ const b = withCtx({ hiddenPair: (x, y) => [x, y].sort().join() === 'p1,p2' });
356
+ b.hub.ballot(null, { title: 'bo is out', faces: FACES, feed: [{ from: 'p2', to: 'p4', line: 'boat' }, { from: 'p4', line: 'not-me' }, { from: 'p3', to: 'p2', line: 'boat' }] });
357
+ b.tick();
358
+ const view = (to) => cards(b).find(([id]) => id === to)[1].card;
359
+ assert.deepEqual(view('p1').faces.map((f) => f.id), ['p1', 'p3', 'p4']);
360
+ assert.deepEqual(view('p1').feed, [{ from: 'p4', line: 'not-me' }]);
361
+ assert.equal(view('p1').title, 'someone is out');
362
+ assert.ok(!('p2' in view('p1').who));
363
+ assert.deepEqual(view('p3').faces.map((f) => f.id), ['p1', 'p2', 'p3', 'p4']);
364
+ assert.equal(view('p3').feed.length, 3);
365
+ // And a tap about them is refused: they are not on that card.
366
+ tapAs(b, 'p1', { face: 'p2', line: 'boat' });
367
+ assert.equal(b.calls.length, 0);
368
+ n += 1;
369
+ }
370
+
371
+ // A player who leaves takes their own card with them, and is no face on
372
+ // anyone else's.
373
+ {
374
+ const b = withCtx();
375
+ b.hub.ballot(null, { title: 'Shared', faces: FACES });
376
+ b.hub.ballot(['p2'], { title: 'Mine', faces: FACES });
377
+ b.tick();
378
+ b.world.players.delete('p2');
379
+ b.session.players = ROSTER.filter((p) => p.playerId !== 'p2');
380
+ b.h.place(b.world, []);
381
+ b.sent.length = 0;
382
+ b.hub.ballot(null, { title: 'Shared', faces: FACES, options: [{ id: 'skip', text: 'Skip' }] });
383
+ b.tick();
384
+ assert.deepEqual(cards(b).at(-1)[1].card.faces.map((f) => f.id), ['p1', 'p3', 'p4']);
385
+ b.world.players.set('p2', { at: { x: 0, y: 1, z: 0 }, facing: 0, body: { setTranslation() {} } });
386
+ b.session.players = ROSTER;
387
+ b.h.message(b.session, { playerId: 'p2' }, { k: 'hub-hello', avatar: 4 });
388
+ b.sent.length = 0;
389
+ b.tick();
390
+ assert.deepEqual(cards(b).filter(([to]) => to === 'p2').map(([, msg]) => msg.card.title), ['Shared']);
391
+ n += 1;
392
+ }
393
+
394
+ // --- review fixes ----------------------------------------------------------
395
+
396
+ // A lock the hook sets holds at once: replayed votes in the same tick are
397
+ // refused before the next send.
398
+ {
399
+ const vote = { mode: 'vote', faces: FACES };
400
+ const b = withCtx({
401
+ hooks: {
402
+ ballot: (hub, playerId, tap) => {
403
+ b2calls.push([playerId, tap]);
404
+ hub.ballot([playerId], { ...vote, picked: { face: tap.face } });
405
+ },
406
+ },
407
+ });
408
+ b.hub.ballot(null, vote);
409
+ b.tick();
410
+ for (let i = 0; i < 3; i += 1) {
411
+ tapAs(b, 'p1', { face: 'p2' });
412
+ }
413
+ assert.equal(b2calls.length, 1);
414
+ // And a card the hook took down a moment ago takes no taps either.
415
+ b.hub.ballot(null, null);
416
+ tapAs(b, 'p3', { face: 'p2' });
417
+ assert.equal(b2calls.length, 1);
418
+ n += 1;
419
+ }
420
+
421
+ // Connections that never said hello leave nothing behind.
422
+ {
423
+ const players = new Map([['p1', {}]]);
424
+ const h = { place: { ballot: { lines: LINES } }, sim: { players, timeMs: 0 }, players: new Map(), session: { send() {} } };
425
+ const deps = { blockedPairs: () => null, namesOf: () => [], usernameOf: (id) => id, avatarOf: () => null };
426
+ for (let i = 0; i < 20; i += 1) {
427
+ players.set(`g${i}`, {});
428
+ }
429
+ assert.equal(Ballot.ballot(h, null, { faces: [{ playerId: 'p1' }] }), true);
430
+ assert.equal(Ballot.ballot(h, ['g0', 'g1'], { faces: [{ playerId: 'p1' }] }), true);
431
+ Ballot.send(h, deps);
432
+ assert.equal(h.ballotState.views.size, 21);
433
+ for (let i = 0; i < 20; i += 1) {
434
+ players.delete(`g${i}`);
435
+ }
436
+ Ballot.send(h, deps);
437
+ for (const map of ['me', 'sent', 'views', 'rate']) {
438
+ assert.deepEqual([...h.ballotState[map].keys()].filter((id) => id !== 'p1'), [], map);
439
+ }
440
+ n += 1;
441
+ }
442
+
443
+ // A lock on a face the viewer cannot see (blocked, or gone) stays a lock,
444
+ // and names nobody.
445
+ {
446
+ const vote = { mode: 'vote', faces: FACES, picked: { face: 'p2' } };
447
+ const b = withCtx({ hiddenPair: (x, y) => [x, y].sort().join() === 'p1,p2' });
448
+ b.hub.ballot(['p1', 'p3'], vote);
449
+ b.tick();
450
+ const view = (to) => cards(b).filter(([id]) => id === to).at(-1)[1].card;
451
+ assert.deepEqual(view('p1').picked, {});
452
+ assert.deepEqual(view('p3').picked, { face: 'p2' });
453
+ assert.ok(!JSON.stringify(view('p1')).includes('p2'));
454
+ tapAs(b, 'p1', { face: 'p4' });
455
+ assert.equal(b.calls.length, 0);
456
+ assert.deepEqual(Client.voteLabel(view('p1'), { face: null, option: null, pending: null }, 'p1', 0), { text: 'Locked in ✓', enabled: false });
457
+ // Gone: p2 leaves, p3's lock stays.
458
+ b.world.players.delete('p2');
459
+ b.session.players = ROSTER.filter((p) => p.playerId !== 'p2');
460
+ b.h.place(b.world, []);
461
+ b.tick();
462
+ assert.deepEqual(view('p3').picked, {});
463
+ tapAs(b, 'p3', { face: 'p4' });
464
+ assert.equal(b.calls.length, 0);
465
+ n += 1;
466
+ }
467
+
468
+ // (c) One gate on the client. A card that comes ahead of the WORLD snapshot
469
+ // (the server's first tick sends it before WORLD) is held, pending, with its
470
+ // connection; WORLD keeps it for a place with `ballot` or drops it for one
471
+ // without. In a place known to be keyless a card is dropped before anything
472
+ // is made (a legacy or custom `hub-ballot-card`). A card of another
473
+ // connection is let go.
474
+ {
475
+ const netA = { id: 'a' };
476
+ const netB = { id: 'b' };
477
+ const card = { v: 'e1-1', mode: 'talk', faces: [], options: [], lines: [], feed: [], who: {} };
478
+ const ballotPlace = { id: 'main', title: 'Meet', ballot: { lines: LINES } };
479
+ const keyless = { id: 'main', title: 'Town' };
480
+ const msg = (c) => ({ k: 'hub-ballot-card', card: c });
481
+ State.clear();
482
+ assert.equal(Client.held, null);
483
+ // Unknown place: one card held, pending, not drawn yet.
484
+ hookLayer.message({ multiplayer: { net: netA } }, msg({ ...card, v: 'e1-0' }));
485
+ hookLayer.message({ multiplayer: { net: netA } }, msg(card));
486
+ assert.equal(Client.held.pending, true);
487
+ assert.equal(Client.cardFor(netA), null);
488
+ hookLayer.state({}, { t: 1, hubPlace: ballotPlace }, null);
489
+ assert.equal(Client.cardFor(netA), card);
490
+ // Another connection's card is let go.
491
+ assert.equal(Client.cardFor(netB), null);
492
+ assert.equal(Client.held, null);
493
+ // Pending, then the place turns out keyless: dropped.
494
+ State.clear();
495
+ hookLayer.message({ multiplayer: { net: netA } }, msg(card));
496
+ hookLayer.state({}, { t: 1, hubPlace: keyless }, null);
497
+ assert.equal(Client.held, null);
498
+ // Known keyless place: dropped at the gate, nothing held (round 3 #3).
499
+ hookLayer.message({ multiplayer: { net: netA } }, msg(card));
500
+ assert.equal(Client.held, null);
501
+ assert.equal('ballot' in State.hub, false);
502
+ // Known ballot place: held at once; a malformed card or null is no card.
503
+ State.hub.place = ballotPlace;
504
+ hookLayer.message({ multiplayer: { net: netA } }, msg(card));
505
+ assert.equal(Client.cardFor(netA), card);
506
+ hookLayer.message({ multiplayer: { net: netA } }, msg({ mode: 'talk' }));
507
+ assert.equal(Client.cardFor(netA), null);
508
+ hookLayer.message({ multiplayer: { net: netA } }, msg(null));
509
+ assert.equal(Client.cardFor(netA), null);
510
+ // A tap carries the card's version.
511
+ assert.deepEqual(Client.tapMessage(card, { line: 'not-me' }), { v: 'e1-1', line: 'not-me' });
512
+ State.clear();
513
+ Client.cardFor(netB);
514
+ n += 1;
515
+ }
516
+
517
+ // Rule B, by source: every ballot touchpoint in the hosts. On the client
518
+ // everything is behind the one gate (`held`, set only by `gate`); on the
519
+ // server behind `h.place.ballot`, `h.ballotState` or `manifest.ballot`.
520
+ {
521
+ const fs = await import('node:fs');
522
+ const lines = (path) => fs.readFileSync(new URL(path, import.meta.url), 'utf8').split('\n').map((l) => l.trim()).filter((l) => !l.startsWith('//') && /ballot|carded|cardFor/i.test(l));
523
+ assert.deepEqual(lines('../code/systems/hub.js'), [
524
+ "import { cardFor, forget as forgetBallot, held as ballotHeld, makeBallot } from '../client/ballot.js';",
525
+ 'const card = ballotHeld ? cardFor(mp.net ?? null) : null;',
526
+ 'if (ballotHeld) {',
527
+ 'this.ballot ??= makeBallot({',
528
+ "send: (ids) => this.scene?.multiplayer?.net?.send({ k: 'hub-ballot', ...ids }),",
529
+ 'this.carded = card !== null && !this.trip;',
530
+ '} else if (this.ballot) {',
531
+ 'this.ballot.dispose();',
532
+ 'this.ballot = null;',
533
+ 'this.carded = false;',
534
+ 'this.idle.frame(scene, mp, local, !this.trip && this.pendingEnd === null && !this.talk.talking() && !decorating && !this.carded, this.sceneKey);',
535
+ "this.chrome.setActions(Hub.hub.actions?.items ?? null, (id, n) => mp.net?.send({ k: 'hub-action', id, n }), !this.trip && this.pendingEnd === null && !this.talk.talking() && !decorating && !this.carded && !this.askInput && !this.menuInput, Hub.hub.actions?.n);",
536
+ 'if (this.ballot) {',
537
+ 'const box = this.carded ? this.chrome.hudClearBox?.() : null;',
538
+ 'this.ballot.frame(this.carded ? cardFor(mp.net ?? null) : null, Hub.hub.place?.ballot?.lines ?? [], mp.net?.selfId?.() ?? null, Hub.simNow(), box ? box.bottom : 0);',
539
+ // A new player id on the same net drops a held card (only one held).
540
+ 'if (ballotHeld) {',
541
+ 'forgetBallot();',
542
+ "chrome.showPicker(idle && place !== null && !inGame && !this.talk.talking() && !this.carded, Avatar.current(scene), { available: Avatar.colorsAvailable(), style: Avatar.style(), color: Avatar.currentColor(scene), files: scene.files, model: Avatar.colorsAvailable() && Avatar.style() === 'toon' ? Avatar.model(scene.files, Avatar.current(scene), 'toon') : null });",
543
+ 'const free = idle && place !== null && this.pendingEnd === null && !this.talk.talking() && !this.carded;',
544
+ 'chrome.setFeed?.(idle && place !== null && !this.carded ? this.say.feed() : []);',
545
+ 'const canTalk = cam && idle && this.pendingEnd === null && !this.talk.talking() && this.decor?.opened() !== true && !this.carded;',
546
+ 'if (this.ballot) {',
547
+ 'this.ballot.dispose();',
548
+ 'this.ballot = null;',
549
+ ]);
550
+ assert.deepEqual(lines('../code/client/hookLayer.js'), [
551
+ "import * as Ballot from './ballot.js';",
552
+ "case 'hub-ballot-card':",
553
+ 'Ballot.gate(hub.place, scene?.multiplayer?.net ?? null, msg.card);',
554
+ 'if (msg.hubPlace && Ballot.held) {',
555
+ 'Ballot.settle(msg.hubPlace);',
556
+ ]);
557
+ assert.deepEqual(lines('../code/server/hub.js'), [
558
+ "import * as Ballot from './ballot.js';",
559
+ "if (h.place?.ballot && data?.k === 'hub-ballot') {",
560
+ 'Ballot.message(h, player, data, h.place.ballot.lines);',
561
+ 'if (h.ballotState) {',
562
+ 'Ballot.send(h, ballotDeps(h));',
563
+ 'if (manifest.ballot) {',
564
+ 'h.ctx.ballot = (playerIds, spec) => Ballot.ballot(h, playerIds, spec);',
565
+ 'const ballotStale = back || h.ballotState?.sent.get(player.playerId) === STALE;',
566
+ 'if (h.ballotState) {',
567
+ 'Ballot.joinedAfresh(h, player.playerId);',
568
+ 'if (ballotStale && h.ballotState) {',
569
+ 'h.ballotState.sent.set(player.playerId, STALE);',
570
+ 'if (h.ballotState) {',
571
+ 'Ballot.forget(h, playerId);',
572
+ 'function ballotDeps(h) {',
573
+ ]);
574
+ assert.deepEqual(lines('../code/client/state.js'), []);
575
+ // A keyless manifest carries no ballot anywhere.
576
+ const { manifest } = readManifest(deck('absent'), scenes);
577
+ assert.ok(!('ballot' in manifest) && !('ballot' in manifest.places.main));
578
+ assert.equal(readManifest(deck('absent', { places: { meet: { title: 'M', scene: 'scenes/meet.scene', ballot: true } } }), scenes).manifest.ballot, true);
579
+ n += 1;
580
+ }
581
+
582
+ // Rule A: an id the player was never delivered is refused, even when the
583
+ // current card offers it; one taken off the current card is refused too.
584
+ {
585
+ const b = withCtx();
586
+ b.hub.ballot(null, { faces: FACES, options: [{ id: 'old', text: 'Old' }] });
587
+ b.tick();
588
+ b.hub.ballot(null, { faces: FACES, options: [{ id: 'new', text: 'New' }] });
589
+ tapAs(b, 'p1', { option: 'new' });
590
+ tapAs(b, 'p1', { option: 'old' });
591
+ assert.equal(b.calls.length, 0);
592
+ b.tick();
593
+ tapAs(b, 'p1', { option: 'new' });
594
+ assert.deepEqual(b.calls.map(([, tap]) => tap.option), ['new']);
595
+ // A mode change waits for delivery too: a vote on a talk card is refused.
596
+ b.hub.ballot(null, { mode: 'vote', faces: FACES });
597
+ b.tick();
598
+ b.hub.ballot(null, { mode: 'talk', faces: FACES });
599
+ tapAs(b, 'p2', { face: 'p4' });
600
+ tapAs(b, 'p2', { line: 'not-me' });
601
+ assert.equal(b.calls.length, 1);
602
+ b.tick();
603
+ tapAs(b, 'p2', { line: 'not-me' });
604
+ assert.equal(b.calls.length, 2);
605
+ n += 1;
606
+ }
607
+
608
+ // (b) The card's version. Each `hub.ballot` call is a new, higher version;
609
+ // a tap is taken only when its `v` is both the version delivered and the
610
+ // current one. An option in both cards of an undelivered talk -> vote change
611
+ // is refused (round 3 #2), and so is a replay on an older version.
612
+ {
613
+ const b = withCtx();
614
+ b.hub.ballot(null, { faces: FACES, options: [{ id: 'skip', text: 'Skip' }] });
615
+ b.tick();
616
+ const v1 = deliveredTo(b, 'p1').v;
617
+ b.hub.ballot(null, { mode: 'vote', faces: FACES, options: [{ id: 'skip', text: 'Skip' }] });
618
+ tapAs(b, 'p1', { option: 'skip' });
619
+ assert.equal(b.calls.length, 0);
620
+ b.tick();
621
+ const v2 = deliveredTo(b, 'p1').v;
622
+ assert.notEqual(v2, v1);
623
+ tapAs(b, 'p1', { v: v1, option: 'skip' });
624
+ tapAs(b, 'p1', { v: `${v2}0`, option: 'skip' });
625
+ tapAs(b, 'p1', { v: undefined, option: 'skip' });
626
+ tapAs(b, 'p1', { v: Number(v2.split('-')[1]), option: 'skip' });
627
+ assert.equal(b.calls.length, 0);
628
+ tapAs(b, 'p1', { option: 'skip' });
629
+ assert.deepEqual(b.calls, [['p1', { face: null, line: null, option: 'skip', mode: 'vote' }]]);
630
+ // A player's own card with other taps is a new version of theirs alone;
631
+ // the same card again keeps it.
632
+ b.hub.ballot(['p2'], { mode: 'vote', faces: FACES });
633
+ b.hub.ballot(['p2'], { mode: 'vote', faces: FACES });
634
+ b.tick();
635
+ const v4 = deliveredTo(b, 'p2').v;
636
+ assert.ok(![v1, v2].includes(v4));
637
+ assert.equal(deliveredTo(b, 'p3').v, v2);
638
+ // A player's own card cleared: they are back on the shared card, and a
639
+ // tap on the cleared one is refused.
640
+ b.hub.ballot(['p2'], null);
641
+ tapAs(b, 'p2', { v: v4, face: 'p4' });
642
+ assert.equal(b.calls.length, 1);
643
+ n += 1;
644
+ }
645
+
646
+ // A card's version moves only when what can be tapped on it changes: a
647
+ // refresh of the feed, title, text, until, notes, votes, marks or option
648
+ // words keeps it (a tap made as another line lands is still taken), and a
649
+ // change of mode, faces offered or `off`, lines, option ids or `picked`
650
+ // moves it. A player's own card that differs from the shared one only in its
651
+ // feed keeps the shared card's version.
652
+ {
653
+ const b = withCtx();
654
+ const base = { title: 'Meeting', faces: FACES, lines: ['boat', 'not-me'], options: [{ id: 'skip', text: 'Skip' }] };
655
+ b.hub.ballot(null, base);
656
+ b.tick();
657
+ const v = deliveredTo(b, 'p1').v;
658
+ const vOf = (spec, to = null) => {
659
+ b.hub.ballot(to, spec);
660
+ b.tick();
661
+ return deliveredTo(b, to ? to[0] : 'p1').v;
662
+ };
663
+ // Feed-only refresh: the delivered card is still the current one, so a tap
664
+ // made on it before the new feed arrives is taken.
665
+ b.hub.ballot(null, { ...base, feed: [{ from: 'p2', line: 'not-me' }] });
666
+ tapAs(b, 'p1', { line: 'not-me' });
667
+ assert.equal(b.calls.length, 1);
668
+ b.tick();
669
+ assert.equal(deliveredTo(b, 'p1').v, v);
670
+ assert.deepEqual(deliveredTo(b, 'p1').feed, [{ from: 'p2', line: 'not-me' }]);
671
+ for (const words of [
672
+ { title: 'Meeting!' },
673
+ { text: 'Who?' },
674
+ { until: 9000 },
675
+ { faces: FACES.map((f) => ({ ...f, note: 'here', votes: 2, mark: '#123456' })) },
676
+ { options: [{ id: 'skip', text: 'Pass' }] },
677
+ ]) {
678
+ assert.equal(vOf({ ...base, ...words }), v, JSON.stringify(words));
679
+ }
680
+ // What can be tapped: each moves it, and back again moves it once more.
681
+ let last = v;
682
+ for (const taps of [
683
+ { mode: 'vote' },
684
+ { faces: FACES.slice(0, 3) },
685
+ { faces: FACES.map((f) => ({ ...f, off: false })) },
686
+ { lines: ['boat'] },
687
+ { options: [{ id: 'skip', text: 'Skip' }, { id: 'wait', text: 'Wait' }] },
688
+ { mode: 'vote', picked: { face: 'p2' } },
689
+ ]) {
690
+ const moved = vOf({ ...base, ...taps });
691
+ assert.notEqual(moved, last, JSON.stringify(taps));
692
+ last = vOf(base);
693
+ assert.ok(![moved, v].includes(last));
694
+ }
695
+ // A player's own card differing only in feed keeps the shared version, and
696
+ // a tap on it is taken before and after it is delivered.
697
+ const shared = deliveredTo(b, 'p3').v;
698
+ b.calls.length = 0;
699
+ b.hub.ballot(['p3'], { ...base, feed: [{ from: 'p1', line: 'not-me' }] });
700
+ tapAs(b, 'p3', { line: 'not-me' });
701
+ b.tick();
702
+ assert.equal(deliveredTo(b, 'p3').v, shared);
703
+ assert.equal(deliveredTo(b, 'p3').feed.length, 1);
704
+ tapAs(b, 'p3', { line: 'not-me' });
705
+ assert.equal(b.calls.length, 2);
706
+ // ...and one that changes its taps does not.
707
+ b.hub.ballot(['p3'], { ...base, lines: ['boat'] });
708
+ tapAs(b, 'p3', { line: 'not-me' });
709
+ assert.equal(b.calls.length, 2);
710
+ // The client keeps a selection through a refresh of the same version and
711
+ // starts afresh (nothing selected or pending) on a new one.
712
+ const card = { v: 'e1-5', mode: 'vote', faces: [{ id: 'p1' }, { id: 'p2' }], options: [], lines: [], feed: [], who: {} };
713
+ let sel = Client.keepSel(card, { face: null, option: null, pending: null, mode: null, v: null });
714
+ ({ sel } = Client.tap(card, LINES, sel, { face: 'p2' }));
715
+ sel = Client.tap(card, LINES, sel, { vote: true }, 100).sel;
716
+ assert.equal(Client.keepSel({ ...card, title: 'x' }, sel).pending, 100);
717
+ assert.deepEqual(Client.keepSel({ ...card, v: 'e1-6' }, sel), { face: null, option: null, pending: null, mode: 'vote', v: 'e1-6' });
718
+ n += 1;
719
+ }
720
+
721
+ // Round 4 #1: versions belong to one server. A replacement server for the
722
+ // same layer restarts its counter, but under its own epoch, so a tap made on
723
+ // the old server's card is never taken as one on the new server's.
724
+ {
725
+ const a = withCtx();
726
+ a.hub.ballot(null, { faces: FACES, options: [{ id: 'skip', text: 'Skip' }] });
727
+ a.tick();
728
+ const old = deliveredTo(a, 'p1').v;
729
+ const b = withCtx();
730
+ b.hub.ballot(null, { mode: 'vote', faces: FACES, options: [{ id: 'skip', text: 'Skip' }] });
731
+ b.tick();
732
+ const fresh = deliveredTo(b, 'p1').v;
733
+ assert.equal(old.split('-')[1], fresh.split('-')[1]);
734
+ assert.notEqual(old, fresh);
735
+ tapAs(b, 'p1', { v: old, option: 'skip' });
736
+ assert.equal(b.calls.length, 0);
737
+ tapAs(b, 'p1', { v: fresh, option: 'skip' });
738
+ assert.equal(b.calls.length, 1);
739
+ n += 1;
740
+ }
741
+
742
+ // Round 4 #2: every WORLD snapshot rechecks a held card, pending or not: a
743
+ // recovery that keeps the connection but lands in a keyless place lets go of
744
+ // an established card.
745
+ {
746
+ const net = { id: 'n' };
747
+ const card = { v: 'e1-1', mode: 'talk', faces: [], options: [], lines: [], feed: [], who: {} };
748
+ State.clear();
749
+ State.hub.place = { id: 'main', title: 'Meet', ballot: { lines: LINES } };
750
+ hookLayer.message({ multiplayer: { net } }, { k: 'hub-ballot-card', card });
751
+ assert.equal(Client.cardFor(net), card);
752
+ assert.equal(Client.held.pending, false);
753
+ // The same place again keeps it.
754
+ hookLayer.state({}, { t: 2, hubPlace: State.hub.place }, null);
755
+ assert.equal(Client.cardFor(net), card);
756
+ hookLayer.state({}, { t: 3, hubPlace: { id: 'main', title: 'Town' } }, null);
757
+ assert.equal(Client.held, null);
758
+ assert.equal(Client.cardFor(net), null);
759
+ State.clear();
760
+ n += 1;
761
+ }
762
+
763
+ // Round 4 #3: the hub system reads the gate again after `cardFor` (which
764
+ // lets go of another connection's card) and makes or draws nothing unless it
765
+ // still holds one: no make -> frame(null) -> dispose on a keyless visit.
766
+ {
767
+ const fs = await import('node:fs');
768
+ const src = fs.readFileSync(new URL('../code/systems/hub.js', import.meta.url), 'utf8');
769
+ const at = (text) => {
770
+ const i = src.indexOf(text);
771
+ assert.ok(i >= 0, text);
772
+ return i;
773
+ };
774
+ const lookup = at('const card = ballotHeld ? cardFor(mp.net ?? null) : null;');
775
+ const gate = src.indexOf('if (ballotHeld) {', lookup);
776
+ const make = at('this.ballot ??= makeBallot({');
777
+ assert.ok(lookup < gate && gate < make && src.lastIndexOf('if (ballotHeld)', make) === gate);
778
+ // And cardFor itself lets go, so `held` reads null right after.
779
+ Client.gate({ id: 'main', title: 'Meet', ballot: { lines: LINES } }, { id: 'old' }, { v: 'e1-1', mode: 'talk', faces: [], options: [], lines: [], feed: [], who: {} });
780
+ assert.equal(Client.cardFor({ id: 'new' }), null);
781
+ assert.equal(Client.held, null);
782
+ n += 1;
783
+ }
784
+
785
+ // (a) The card is a pure function of who is connected now. While a blocked
786
+ // player is here, the viewer has no face, row or name of theirs. Once they
787
+ // leave, every viewer loses their feed rows alike, nothing is remembered, and
788
+ // a departed name in the deck's own words is no longer redacted (the HUD's
789
+ // and labels' limit too). A viewer who reconnects with the same id (round 3
790
+ // #1) is shown exactly what everyone else is.
791
+ {
792
+ const lines = [...LINES, { id: 'bo-did', text: 'bo did it', about: 'self' }];
793
+ const b = withCtx({ ballot: { lines }, hiddenPair: (x, y) => [x, y].sort().join() === 'p1,p2' && b.session.players.some((p) => p.playerId === 'p2') });
794
+ const spec = { title: 'bo is out', faces: FACES, feed: [{ from: 'p3', line: 'bo-did' }, { from: 'p2', line: 'not-me' }, { from: 'p4', to: 'p2', line: 'boat' }] };
795
+ b.hub.ballot(null, spec);
796
+ b.tick();
797
+ const view = (to) => cards(b).filter(([id]) => id === to).at(-1)[1].card;
798
+ assert.equal(view('p1').title, 'someone is out');
799
+ assert.deepEqual(view('p1').feed, [{ from: 'p3', line: 'bo-did' }]);
800
+ assert.deepEqual(Client.feedWords(view('p1'), lines, 'p1'), ['cy: someone did it']);
801
+ assert.equal(view('p3').feed.length, 3);
802
+ // bo leaves: their rows go for everyone, from the next send on.
803
+ b.world.players.delete('p2');
804
+ b.session.players = ROSTER.filter((p) => p.playerId !== 'p2');
805
+ b.h.place(b.world, []);
806
+ b.tick();
807
+ for (const id of ['p1', 'p3', 'p4']) {
808
+ assert.deepEqual(view(id).feed, [{ from: 'p3', line: 'bo-did' }], id);
809
+ assert.ok(!('p2' in view(id).who), id);
810
+ }
811
+ assert.equal(view('p1').title, 'bo is out');
812
+ assert.ok(!('words' in view('p1')));
813
+ // p1 reconnects with the same id: the same card as everyone else.
814
+ b.world.players.delete('p1');
815
+ b.session.players = ROSTER.filter((p) => p.playerId !== 'p2' && p.playerId !== 'p1');
816
+ b.h.place(b.world, []);
817
+ b.tick();
818
+ b.world.players.set('p1', { at: { x: 0, y: 1, z: 0 }, facing: 0, body: { setTranslation() {} } });
819
+ b.session.players = ROSTER.filter((p) => p.playerId !== 'p2');
820
+ b.h.message(b.session, { playerId: 'p1' }, { k: 'hub-hello', avatar: 3 });
821
+ b.sent.length = 0;
822
+ b.tick();
823
+ const { v: v1, ...again } = view('p1');
824
+ const { v: v3, ...other } = view('p3');
825
+ assert.equal(v1, v3);
826
+ assert.deepEqual(again, other);
827
+ assert.deepEqual(again.feed, [{ from: 'p3', line: 'bo-did' }]);
828
+ n += 1;
829
+ }
830
+
831
+ // Ids are looked up as own properties only, client and server.
832
+ {
833
+ const lines = [{ id: 'constructor', text: 'Not me!', about: 'self' }];
834
+ const card = JSON.parse(JSON.stringify({ mode: 'talk', faces: [], options: [], lines: ['constructor'], feed: [{ from: 'toString', line: 'constructor' }], who: {}, words: {} }));
835
+ assert.equal(Client.textOf(card, lines[0]), 'Not me!');
836
+ assert.equal(Client.nameOf(card, 'toString', 'p1'), 'someone');
837
+ assert.deepEqual(Client.feedWords(card, lines, 'p1'), ['someone: Not me!']);
838
+ const view = Ballot.viewFor(
839
+ { mode: 'talk', faces: [{ playerId: '__proto__' }, { playerId: 'constructor' }], options: [], lines: [], feed: [] },
840
+ 'p1',
841
+ { usernameOf: (id) => `name-${id}`, avatarOf: () => 1, hidden: null, names: [], lines: [] },
842
+ );
843
+ const wire = JSON.parse(JSON.stringify(view));
844
+ assert.deepEqual(wire.faces.map((f) => f.id), ['__proto__', 'constructor']);
845
+ assert.equal(Client.nameOf(wire, '__proto__', 'p1'), 'name-__proto__');
846
+ assert.equal(Client.nameOf(wire, 'constructor', 'p1'), 'name-constructor');
847
+ assert.equal(Object.getPrototypeOf(wire), Object.prototype);
848
+ n += 1;
849
+ }
850
+
851
+ // A blocked player's name is out of every word the card shows: the
852
+ // manifest's lines too, in the chips and in the feed.
853
+ {
854
+ const lines = [...LINES, { id: 'bo-did', text: 'bo did it', about: 'self' }];
855
+ const b = withCtx({ ballot: { lines }, hiddenPair: (x, y) => [x, y].sort().join() === 'p1,p2' });
856
+ b.hub.ballot(null, { faces: FACES, feed: [{ from: 'p3', line: 'bo-did' }, { from: 'p2', line: 'not-me' }] });
857
+ b.tick();
858
+ const view = (to) => cards(b).find(([id]) => id === to)[1].card;
859
+ assert.deepEqual(view('p1').words, { 'bo-did': 'someone did it' });
860
+ assert.deepEqual(view('p1').feed, [{ from: 'p3', line: 'bo-did' }]);
861
+ assert.deepEqual(Client.feedWords(view('p1'), lines, 'p1'), ['cy: someone did it']);
862
+ assert.equal(Client.textOf(view('p1'), lines[3]), 'someone did it');
863
+ assert.ok(!('words' in view('p3')));
864
+ assert.deepEqual(Client.feedWords(view('p3'), lines, 'p3'), ['You: bo did it', 'bo: Not me!']);
865
+ n += 1;
866
+ }
867
+
868
+ // Clearing a player's own card is rule A too: back on the shared card they
869
+ // keep their version when it allows the same taps as the card they had, and
870
+ // get a new one otherwise (a follower copy of the shared card, its own `v`).
871
+ {
872
+ const talk = { faces: FACES };
873
+ const vote = { mode: 'vote', faces: FACES };
874
+ const b = withCtx();
875
+ const vOf = (playerId) => deliveredTo(b, playerId)?.v;
876
+ // (a) A different card in between: a tap still on the shared card's
877
+ // version from before it is refused; one on the new version is taken.
878
+ b.hub.ballot(null, talk);
879
+ b.tick();
880
+ const sharedV = vOf('p1');
881
+ b.hub.ballot(['p1'], vote);
882
+ b.tick();
883
+ const privateV = vOf('p1');
884
+ assert.notEqual(privateV, sharedV);
885
+ b.hub.ballot(['p1'], null);
886
+ b.tick();
887
+ const back = deliveredTo(b, 'p1');
888
+ assert.equal(back.mode, 'talk');
889
+ assert.ok(![sharedV, privateV].includes(back.v));
890
+ assert.ok(!('of' in back) && !('key' in back));
891
+ tapAs(b, 'p1', { v: sharedV, line: 'not-me' });
892
+ assert.equal(b.calls.length, 0);
893
+ tapAs(b, 'p1', { line: 'not-me' });
894
+ assert.deepEqual(b.calls.map(([who]) => who), ['p1']);
895
+ // (b) The same taps on both: a tap in flight on the player's own version
896
+ // is taken, before the next send and after it.
897
+ b.hub.ballot(['p2'], vote);
898
+ b.tick();
899
+ const ownV = vOf('p2');
900
+ b.hub.ballot(null, { ...vote, title: 'Vote now' });
901
+ b.tick();
902
+ assert.notEqual(vOf('p4'), ownV);
903
+ assert.equal(vOf('p2'), ownV);
904
+ b.hub.ballot(['p2'], null);
905
+ tapAs(b, 'p2', { v: ownV, face: 'p1' });
906
+ assert.deepEqual(b.calls.map(([who]) => who), ['p1', 'p2']);
907
+ b.tick();
908
+ assert.equal(deliveredTo(b, 'p2').title, 'Vote now');
909
+ assert.equal(vOf('p2'), ownV);
910
+ // (c) A follower keeps its version while the shared card it follows is
911
+ // replaced by one allowing the same taps, and takes the shared card's new
912
+ // version when it is not; clearing for everyone clears followers too.
913
+ b.hub.ballot(null, talk);
914
+ b.tick();
915
+ b.hub.ballot(['p3'], vote);
916
+ b.tick();
917
+ b.hub.ballot(['p3'], null);
918
+ b.tick();
919
+ const followV = vOf('p3');
920
+ assert.notEqual(followV, vOf('p4'));
921
+ b.hub.ballot(null, { ...talk, title: 'Still talking' });
922
+ b.tick();
923
+ assert.equal(deliveredTo(b, 'p3').title, 'Still talking');
924
+ assert.equal(vOf('p3'), followV);
925
+ b.hub.ballot(null, vote);
926
+ b.tick();
927
+ assert.equal(deliveredTo(b, 'p3').mode, 'vote');
928
+ assert.equal(vOf('p3'), vOf('p4'));
929
+ assert.notEqual(vOf('p3'), followV);
930
+ b.hub.ballot(['p3'], talk);
931
+ b.hub.ballot(['p3'], null);
932
+ b.tick();
933
+ const againV = vOf('p3');
934
+ assert.notEqual(againV, vOf('p4'));
935
+ b.hub.ballot(null, null);
936
+ b.tick();
937
+ assert.equal(deliveredTo(b, 'p3'), null);
938
+ b.hub.ballot(null, vote);
939
+ b.tick();
940
+ assert.equal(vOf('p3'), vOf('p4'));
941
+ assert.notEqual(vOf('p3'), againV);
942
+ // Every follower copy follows the shared card of now.
943
+ const h = { place: { ballot: { lines: LINES } }, sim: { players: new Map([['p1', {}], ['p2', {}]]) } };
944
+ Ballot.ballot(h, null, talk);
945
+ Ballot.ballot(h, ['p1', 'p2'], vote);
946
+ Ballot.ballot(h, ['p1', 'p2'], null);
947
+ Ballot.ballot(h, null, { ...talk, title: 'Again' });
948
+ assert.deepEqual([...h.ballotState.me.values()].map((card) => card.of === h.ballotState.shared), [true, true]);
949
+ n += 1;
950
+ }
951
+
952
+ // --- the client's words -------------------------------------------------------
953
+ {
954
+ const card = {
955
+ mode: 'talk',
956
+ faces: [{ id: 'p1' }, { id: 'p2' }, { id: 'p3', off: true }],
957
+ options: [{ id: 'skip', text: 'Skip' }],
958
+ lines: ['boat', 'not-me'],
959
+ feed: [{ from: 'p2', to: 'p1', line: 'boat' }, { from: 'p2', line: 'not-me' }, { from: 'p1', to: 'p9', line: 'boat' }, { from: 'p2', line: 'gone' }],
960
+ who: { p1: { name: 'amy', avatar: 0 }, p2: { name: 'bo', avatar: 1 } },
961
+ };
962
+ assert.deepEqual(Client.feedWords(card, LINES, 'p3'), ['bo → amy: I saw them by the boat', 'bo: Not me!', 'amy → someone: I saw them by the boat']);
963
+ assert.deepEqual(Client.feedWords(card, LINES, 'p1'), ['bo → You: I saw them by the boat', 'bo: Not me!', 'You → someone: I saw them by the boat']);
964
+ // Talk: a face, then a line about it; a line about oneself at once.
965
+ let sel = Client.keepSel(card, { face: null, option: null, pending: null, mode: null });
966
+ assert.equal(Client.hintOf(card, LINES, sel, 'p1'), 'Tap a face, then what to say');
967
+ assert.equal(Client.tap(card, LINES, sel, { line: 'boat' }).send, null);
968
+ assert.equal(Client.tap(card, LINES, sel, { face: 'p3' }).sel.face, null);
969
+ ({ sel } = Client.tap(card, LINES, sel, { face: 'p2' }));
970
+ assert.equal(Client.hintOf(card, LINES, sel, 'p1'), 'About bo:');
971
+ const said = Client.tap(card, LINES, sel, { line: 'boat' });
972
+ assert.deepEqual(said.send, { face: 'p2', line: 'boat' });
973
+ assert.equal(said.sel.face, null);
974
+ assert.deepEqual(Client.tap(card, LINES, sel, { line: 'not-me' }).send, { line: 'not-me' });
975
+ assert.deepEqual(Client.tap(card, LINES, sel, { option: 'skip' }).send, { option: 'skip' });
976
+ assert.equal(Client.tap(card, LINES, sel, { line: 'sus' }).send, null);
977
+ // Vote: pick, then Vote; locked once the server says so.
978
+ const vote = { ...card, mode: 'vote' };
979
+ sel = Client.keepSel(vote, sel);
980
+ assert.equal(sel.face, null);
981
+ assert.deepEqual(Client.voteLabel(vote, sel, 'p1', 0), { text: 'Vote', enabled: false });
982
+ ({ sel } = Client.tap(vote, LINES, sel, { face: 'p1' }));
983
+ assert.deepEqual(Client.voteLabel(vote, sel, 'p1', 0), { text: 'Vote for You', enabled: true });
984
+ ({ sel } = Client.tap(vote, LINES, sel, { option: 'skip' }));
985
+ assert.deepEqual(Client.voteLabel(vote, sel, 'p1', 0), { text: 'Vote: Skip', enabled: true });
986
+ const sent = Client.tap(vote, LINES, sel, { vote: true }, 1000);
987
+ assert.deepEqual(sent.send, { option: 'skip' });
988
+ assert.equal(Client.tap(vote, LINES, sent.sel, { vote: true }, 1500).send, null);
989
+ assert.deepEqual(Client.voteLabel(vote, sent.sel, 'p1', 1500), { text: 'Sending…', enabled: false });
990
+ const locked = { ...vote, picked: { face: 'p2' } };
991
+ assert.deepEqual(Client.voteLabel(locked, Client.keepSel(locked, sent.sel), 'p1', 1500), { text: 'Locked in: bo ✓', enabled: false });
992
+ assert.equal(Client.tap(locked, LINES, sel, { face: 'p1' }).send, null);
993
+ assert.equal(Client.tap({ ...card, mode: 'reveal' }, LINES, sel, { face: 'p1' }).send, null);
994
+ assert.equal(Client.clock(83.2), '1:24');
995
+ assert.equal(Client.clock(-3), '0:00');
996
+ n += 1;
997
+ }
998
+
999
+ console.warn = realWarn;
1000
+ console.log = realLog;
1001
+ console.log(`test-ballot: ${n} groups passed`);