castle-web-cli 0.4.232 → 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 (355) 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/shell/assets/{index-D2tWM896.js → index-BjOxR-lW.js} +4 -4
  28. package/dist/shell/assets/index-MVLfNYd9.css +1 -0
  29. package/dist/shell/index.html +2 -2
  30. package/dist/taskSteer.d.ts +13 -0
  31. package/dist/taskSteer.js +22 -2
  32. package/kits/base/castle.json +1 -1
  33. package/kits/base/package-lock.json +1 -1
  34. package/kits/base/sdk/README.md +79 -2
  35. package/kits/base/sdk/avatar.d.ts +6 -2
  36. package/kits/base/sdk/avatar.js +12 -4
  37. package/kits/base/sdk/castle.d.ts +1 -1
  38. package/kits/base/sdk/commands.d.ts +14 -0
  39. package/kits/base/sdk/hub.d.ts +6 -2
  40. package/kits/base/sdk/hub.js +13 -0
  41. package/kits/base/sdk/multiplayer.js +17 -1
  42. package/kits/base/sdk/save.js +2 -1
  43. package/kits/base/sdk/server/hub.d.ts +87 -0
  44. package/kits/base/sdk/server/hub.js +70 -0
  45. package/kits/base/sdk/server/platformHandle.d.ts +31 -1
  46. package/kits/base/sdk/server/storage.d.ts +13 -0
  47. package/kits/base/sdk/server/storage.js +46 -0
  48. package/kits/base/sdk/server/wrapper.d.ts +36 -5
  49. package/kits/base/sdk/server/wrapper.js +83 -8
  50. package/kits/hub-game/CLAUDE.md +329 -11
  51. package/kits/hub-game/blueprints/avatar-10.scene +28 -0
  52. package/kits/hub-game/blueprints/avatar-11.scene +28 -0
  53. package/kits/hub-game/blueprints/avatar-12.scene +28 -0
  54. package/kits/hub-game/blueprints/avatar-13.scene +28 -0
  55. package/kits/hub-game/blueprints/avatar-14.scene +28 -0
  56. package/kits/hub-game/blueprints/avatar-15.scene +28 -0
  57. package/kits/hub-game/blueprints/avatar-4.scene +28 -0
  58. package/kits/hub-game/blueprints/avatar-5.scene +28 -0
  59. package/kits/hub-game/blueprints/avatar-6.scene +28 -0
  60. package/kits/hub-game/blueprints/avatar-7.scene +28 -0
  61. package/kits/hub-game/blueprints/avatar-8.scene +28 -0
  62. package/kits/hub-game/blueprints/avatar-9.scene +28 -0
  63. package/kits/hub-game/code/client/actorBudget.js +47 -0
  64. package/kits/hub-game/code/client/attention.js +11 -2
  65. package/kits/hub-game/code/client/avatar.js +309 -35
  66. package/kits/hub-game/code/client/avatarCap.js +228 -0
  67. package/kits/hub-game/code/client/avatarColors.js +70 -0
  68. package/kits/hub-game/code/client/avatarFaces.js +21 -0
  69. package/kits/hub-game/code/client/avatarShare.js +251 -0
  70. package/kits/hub-game/code/client/ballot.js +467 -0
  71. package/kits/hub-game/code/client/build.js +22 -0
  72. package/kits/hub-game/code/client/chrome.js +1144 -50
  73. package/kits/hub-game/code/client/companion.js +716 -0
  74. package/kits/hub-game/code/client/companionDress.js +99 -0
  75. package/kits/hub-game/code/client/decor.js +509 -0
  76. package/kits/hub-game/code/client/earned.js +131 -0
  77. package/kits/hub-game/code/client/emoteBubble.js +75 -0
  78. package/kits/hub-game/code/client/grab.js +339 -20
  79. package/kits/hub-game/code/client/hookLayer.js +299 -13
  80. package/kits/hub-game/code/client/idle.js +163 -22
  81. package/kits/hub-game/code/client/interestCap.js +81 -0
  82. package/kits/hub-game/code/client/keys.js +36 -0
  83. package/kits/hub-game/code/client/labelsNear.js +205 -0
  84. package/kits/hub-game/code/client/menuIcons.js +34 -0
  85. package/kits/hub-game/code/client/pack.js +6 -2
  86. package/kits/hub-game/code/client/packFormat.js +3 -3
  87. package/kits/hub-game/code/client/rehello.js +167 -0
  88. package/kits/hub-game/code/client/shot.js +92 -0
  89. package/kits/hub-game/code/client/sound.js +63 -8
  90. package/kits/hub-game/code/client/soundscape.js +1151 -0
  91. package/kits/hub-game/code/client/soundscapeActions.js +99 -0
  92. package/kits/hub-game/code/client/soundscapeAmbience.js +453 -0
  93. package/kits/hub-game/code/client/soundscapeBaked.js +143 -0
  94. package/kits/hub-game/code/client/soundscapeBusker.js +217 -0
  95. package/kits/hub-game/code/client/soundscapeCourt.js +200 -0
  96. package/kits/hub-game/code/client/soundscapeCrowd.js +331 -0
  97. package/kits/hub-game/code/client/soundscapeCues.js +362 -0
  98. package/kits/hub-game/code/client/soundscapeMap.js +181 -0
  99. package/kits/hub-game/code/client/soundscapeMarket.js +176 -0
  100. package/kits/hub-game/code/client/soundscapeMusic.js +126 -0
  101. package/kits/hub-game/code/client/soundscapeMusicEngine.js +460 -0
  102. package/kits/hub-game/code/client/soundscapeSimlish.js +737 -0
  103. package/kits/hub-game/code/client/soundscapeSongs.js +127 -0
  104. package/kits/hub-game/code/client/soundscapeSongsRound3.js +63 -0
  105. package/kits/hub-game/code/client/soundscapeSynthesis.js +168 -0
  106. package/kits/hub-game/code/client/soundscapeUi.js +211 -0
  107. package/kits/hub-game/code/client/soundscapeVoice.js +1209 -0
  108. package/kits/hub-game/code/client/soundscapeWaves.js +206 -0
  109. package/kits/hub-game/code/client/spin.js +101 -0
  110. package/kits/hub-game/code/client/state.js +109 -1
  111. package/kits/hub-game/code/client/stuck.js +259 -0
  112. package/kits/hub-game/code/client/talk.js +538 -47
  113. package/kits/hub-game/code/client/tiers.js +516 -27
  114. package/kits/hub-game/code/client/toastBand.js +37 -0
  115. package/kits/hub-game/code/client/together.js +32 -0
  116. package/kits/hub-game/code/client/visibility.js +43 -0
  117. package/kits/hub-game/code/client/wear.js +461 -170
  118. package/kits/hub-game/code/client/yours.js +199 -0
  119. package/kits/hub-game/code/companionSpec.js +45 -0
  120. package/kits/hub-game/code/privateText.js +7 -0
  121. package/kits/hub-game/code/server/actions.js +133 -0
  122. package/kits/hub-game/code/server/actors.js +9 -1
  123. package/kits/hub-game/code/server/ask.js +155 -0
  124. package/kits/hub-game/code/server/ballot.js +539 -0
  125. package/kits/hub-game/code/server/cells.js +100 -0
  126. package/kits/hub-game/code/server/companion.js +82 -0
  127. package/kits/hub-game/code/server/cues.js +70 -0
  128. package/kits/hub-game/code/server/decor.js +269 -0
  129. package/kits/hub-game/code/server/earned.js +461 -0
  130. package/kits/hub-game/code/server/friendBests.js +166 -0
  131. package/kits/hub-game/code/server/friendsAmong.js +232 -0
  132. package/kits/hub-game/code/server/history.js +5 -0
  133. package/kits/hub-game/code/server/hub.js +1254 -50
  134. package/kits/hub-game/code/server/idle.js +30 -11
  135. package/kits/hub-game/code/server/index.js +2 -2
  136. package/kits/hub-game/code/server/labels.js +28 -2
  137. package/kits/hub-game/code/server/manifest.js +378 -7
  138. package/kits/hub-game/code/server/menu.js +133 -0
  139. package/kits/hub-game/code/server/npcs.js +830 -80
  140. package/kits/hub-game/code/server/party.js +2 -2
  141. package/kits/hub-game/code/server/players.js +22 -0
  142. package/kits/hub-game/code/server/shot.js +110 -0
  143. package/kits/hub-game/code/server/standing.js +124 -0
  144. package/kits/hub-game/code/server/vis.js +113 -0
  145. package/kits/hub-game/code/server/wear.js +12 -0
  146. package/kits/hub-game/code/server/yours.js +59 -0
  147. package/kits/hub-game/code/shared/cues.js +9 -0
  148. package/kits/hub-game/code/shared/decor.js +41 -0
  149. package/kits/hub-game/code/shared/shot.js +19 -0
  150. package/kits/hub-game/code/shared/spin.js +57 -0
  151. package/kits/hub-game/code/systems/hub.js +845 -95
  152. package/kits/hub-game/docs/content-pack.md +1 -1
  153. package/kits/hub-game/docs/contract.md +2244 -61
  154. package/kits/hub-game/docs/home-pieces.md +64 -0
  155. package/kits/hub-game/models/avatar-10.pxmodel +28 -0
  156. package/kits/hub-game/models/avatar-11.pxmodel +28 -0
  157. package/kits/hub-game/models/avatar-12.pxmodel +27 -0
  158. package/kits/hub-game/models/avatar-13.pxmodel +24 -0
  159. package/kits/hub-game/models/avatar-14.pxmodel +29 -0
  160. package/kits/hub-game/models/avatar-15.pxmodel +24 -0
  161. package/kits/hub-game/models/avatar-4.pxmodel +29 -0
  162. package/kits/hub-game/models/avatar-5.pxmodel +27 -0
  163. package/kits/hub-game/models/avatar-6.pxmodel +26 -0
  164. package/kits/hub-game/models/avatar-7.pxmodel +25 -0
  165. package/kits/hub-game/models/avatar-8.pxmodel +24 -0
  166. package/kits/hub-game/models/avatar-9.pxmodel +30 -0
  167. package/kits/hub-game/models/earned/badge-round.pxmodel +100 -0
  168. package/kits/hub-game/models/earned/badge-shield.pxmodel +117 -0
  169. package/kits/hub-game/models/earned/badge-star.pxmodel +87 -0
  170. package/kits/hub-game/models/earned/belt-plain.pxmodel +72 -0
  171. package/kits/hub-game/models/earned/held-ball.pxmodel +43 -0
  172. package/kits/hub-game/models/earned/held-lantern.pxmodel +85 -0
  173. package/kits/hub-game/models/earned/held-net.pxmodel +57 -0
  174. package/kits/hub-game/models/earned/held-trophy.pxmodel +109 -0
  175. package/kits/hub-game/models/earned/pin-round.pxmodel +62 -0
  176. package/kits/hub-game/models/earned/sash-plain.pxmodel +66 -0
  177. package/kits/hub-game/models/earned/square/belt-plain.pxmodel +104 -0
  178. package/kits/hub-game/models/earned/square/sash-plain.pxmodel +98 -0
  179. package/kits/hub-game/models/hats/bat-clip.pxmodel +206 -0
  180. package/kits/hub-game/models/hats/beekeeper.pxmodel +149 -0
  181. package/kits/hub-game/models/hats/cap.pxmodel +162 -0
  182. package/kits/hub-game/models/hats/captain.pxmodel +178 -0
  183. package/kits/hub-game/models/hats/crooked-top.pxmodel +172 -0
  184. package/kits/hub-game/models/hats/flower-crown.pxmodel +451 -0
  185. package/kits/hub-game/models/hats/foam-crown.pxmodel +179 -0
  186. package/kits/hub-game/models/hats/lantern.pxmodel +230 -0
  187. package/kits/hub-game/models/hats/laurel.pxmodel +533 -0
  188. package/kits/hub-game/models/hats/lighthouse.pxmodel +96 -0
  189. package/kits/hub-game/models/hats/sailor.pxmodel +141 -0
  190. package/kits/hub-game/models/hats/souwester.pxmodel +131 -0
  191. package/kits/hub-game/models/hats/straw.pxmodel +139 -0
  192. package/kits/hub-game/models/hats/sunflower.pxmodel +406 -0
  193. package/kits/hub-game/models/hats/sweatband.pxmodel +112 -0
  194. package/kits/hub-game/models/hats/witch.pxmodel +147 -0
  195. package/kits/hub-game/package-lock.json +2 -0
  196. package/kits/hub-game/package.json +2 -1
  197. package/kits/hub-game/scripts/check.mjs +78 -11
  198. package/kits/hub-game/scripts/companion-follower-wire.json +1 -0
  199. package/kits/hub-game/scripts/fixtures/decor-keyless-draw.json +121 -0
  200. package/kits/hub-game/scripts/fixtures/decor-keyless.json +293 -0
  201. package/kits/hub-game/scripts/fixtures/engine-models-stub.mjs +23 -0
  202. package/kits/hub-game/scripts/fixtures/fake-dom.mjs +107 -0
  203. package/kits/hub-game/scripts/fixtures/interest-legacy.json +385 -0
  204. package/kits/hub-game/scripts/fixtures/npc-notes-v1.77.jsonl +4 -0
  205. package/kits/hub-game/scripts/fixtures/vis-shared-wire.json +1 -0
  206. package/kits/hub-game/scripts/gen-avatars.mjs +933 -16
  207. package/kits/hub-game/scripts/gen-earned.mjs +285 -0
  208. package/kits/hub-game/scripts/gen-hats.mjs +827 -0
  209. package/kits/hub-game/scripts/grab.before-orbit.mjs +400 -0
  210. package/kits/hub-game/scripts/soundscape-wave-analysis.mjs +125 -0
  211. package/kits/hub-game/scripts/test-actions-client.mjs +488 -0
  212. package/kits/hub-game/scripts/test-actions.mjs +298 -0
  213. package/kits/hub-game/scripts/test-actor-budget.mjs +44 -0
  214. package/kits/hub-game/scripts/test-actors.mjs +19 -17
  215. package/kits/hub-game/scripts/test-ask-client.mjs +358 -0
  216. package/kits/hub-game/scripts/test-ask.mjs +269 -0
  217. package/kits/hub-game/scripts/test-attention.mjs +10 -1
  218. package/kits/hub-game/scripts/test-avatar-cap.mjs +379 -0
  219. package/kits/hub-game/scripts/test-avatar-colors.mjs +72 -0
  220. package/kits/hub-game/scripts/test-avatar-models.mjs +181 -0
  221. package/kits/hub-game/scripts/test-avatar-share.mjs +318 -0
  222. package/kits/hub-game/scripts/test-avatar-wire.mjs +753 -0
  223. package/kits/hub-game/scripts/test-avatar.mjs +540 -7
  224. package/kits/hub-game/scripts/test-ballot.mjs +1001 -0
  225. package/kits/hub-game/scripts/test-build.mjs +31 -0
  226. package/kits/hub-game/scripts/test-cells.mjs +71 -0
  227. package/kits/hub-game/scripts/test-chat.mjs +662 -0
  228. package/kits/hub-game/scripts/test-classic.mjs +2 -0
  229. package/kits/hub-game/scripts/test-companion-chat.mjs +198 -0
  230. package/kits/hub-game/scripts/test-companion-client.mjs +1167 -0
  231. package/kits/hub-game/scripts/test-companion.mjs +259 -0
  232. package/kits/hub-game/scripts/test-decor-client.mjs +303 -0
  233. package/kits/hub-game/scripts/test-decor.mjs +457 -0
  234. package/kits/hub-game/scripts/test-earned-client.mjs +459 -0
  235. package/kits/hub-game/scripts/test-earned.mjs +1194 -0
  236. package/kits/hub-game/scripts/test-emote-bubble.mjs +110 -0
  237. package/kits/hub-game/scripts/test-friend-bests.mjs +435 -0
  238. package/kits/hub-game/scripts/test-grab-orbit.mjs +918 -0
  239. package/kits/hub-game/scripts/test-grab.mjs +7 -0
  240. package/kits/hub-game/scripts/test-hats.mjs +422 -168
  241. package/kits/hub-game/scripts/test-hub-leaving.mjs +203 -0
  242. package/kits/hub-game/scripts/test-hub-ops.mjs +439 -2
  243. package/kits/hub-game/scripts/test-hub-trip.mjs +716 -2
  244. package/kits/hub-game/scripts/test-hud-clear.mjs +328 -4
  245. package/kits/hub-game/scripts/test-idle.mjs +361 -1
  246. package/kits/hub-game/scripts/test-interest-cap.mjs +36 -0
  247. package/kits/hub-game/scripts/test-interest-cells.mjs +493 -0
  248. package/kits/hub-game/scripts/test-interest-client.mjs +118 -0
  249. package/kits/hub-game/scripts/test-keys.mjs +46 -1
  250. package/kits/hub-game/scripts/test-labels.mjs +596 -0
  251. package/kits/hub-game/scripts/test-manifest.mjs +207 -0
  252. package/kits/hub-game/scripts/test-menu-client.mjs +480 -0
  253. package/kits/hub-game/scripts/test-menu.mjs +296 -0
  254. package/kits/hub-game/scripts/test-npcs.mjs +1718 -12
  255. package/kits/hub-game/scripts/test-object-entry.mjs +76 -0
  256. package/kits/hub-game/scripts/test-pack.mjs +7 -0
  257. package/kits/hub-game/scripts/test-picker.mjs +314 -0
  258. package/kits/hub-game/scripts/test-rehello.mjs +1039 -0
  259. package/kits/hub-game/scripts/test-relations-among.mjs +222 -0
  260. package/kits/hub-game/scripts/test-relations-refresh.mjs +188 -0
  261. package/kits/hub-game/scripts/test-shot.mjs +756 -0
  262. package/kits/hub-game/scripts/test-soundscape.mjs +5432 -0
  263. package/kits/hub-game/scripts/test-spin.mjs +314 -0
  264. package/kits/hub-game/scripts/test-standing.mjs +416 -0
  265. package/kits/hub-game/scripts/test-stuck.mjs +1903 -0
  266. package/kits/hub-game/scripts/test-tier-cells.mjs +706 -0
  267. package/kits/hub-game/scripts/test-tiers-render.mjs +339 -0
  268. package/kits/hub-game/scripts/test-tiers.mjs +477 -2
  269. package/kits/hub-game/scripts/test-toast-band.mjs +61 -0
  270. package/kits/hub-game/scripts/test-try-on.mjs +22 -22
  271. package/kits/hub-game/scripts/test-vis.mjs +343 -0
  272. package/kits/hub-game/scripts/test-visibility.mjs +134 -0
  273. package/kits/hub-game/scripts/test-yours-chrome.mjs +166 -0
  274. package/kits/hub-game/scripts/test-yours.mjs +372 -0
  275. package/kits/multiplayer-3d/CLAUDE.md +50 -0
  276. package/kits/multiplayer-3d/castle.json +5 -5
  277. package/kits/multiplayer-3d/code/client/avatars.js +7 -0
  278. package/kits/multiplayer-3d/code/client/gait.js +65 -0
  279. package/kits/multiplayer-3d/code/client/nameTags.js +59 -4
  280. package/kits/multiplayer-3d/code/options.js +5 -0
  281. package/kits/multiplayer-3d/code/server/ownership.js +10 -2
  282. package/kits/multiplayer-3d/code/server/sceneBodies.js +12 -0
  283. package/kits/multiplayer-3d/code/server/world.js +7 -0
  284. package/kits/multiplayer-3d/code/systems/multiplayer.js +74 -4
  285. package/kits/multiplayer-3d/package.json +2 -1
  286. package/kits/multiplayer-3d/scripts/fixtures/README.md +11 -0
  287. package/kits/multiplayer-3d/scripts/fixtures/ownership-legacy.json +2 -0
  288. package/kits/multiplayer-3d/scripts/ownership-harness.mjs +102 -0
  289. package/kits/multiplayer-3d/scripts/ownership-loader.mjs +46 -0
  290. package/kits/multiplayer-3d/scripts/ownership-worker.mjs +15 -0
  291. package/kits/multiplayer-3d/scripts/record-ownership-session.mjs +98 -0
  292. package/kits/multiplayer-3d/scripts/test-eligible-claims.mjs +167 -0
  293. package/kits/multiplayer-3d/scripts/test-own-name-tag.mjs +247 -0
  294. package/kits/physics-2d/CLAUDE.md +9 -0
  295. package/kits/physics-2d/castle.json +2 -2
  296. package/kits/physics-2d/engine/scene.js +253 -1
  297. package/kits/physics-2d/engine/skyKey.js +129 -0
  298. package/kits/physics-2d/engine/stillClock.js +106 -0
  299. package/kits/physics-2d/engine/updateBudget.js +21 -0
  300. package/kits/physics-2d/scripts/test-skip-unchanged.mjs +326 -1
  301. package/kits/physics-3d/CLAUDE.md +1169 -30
  302. package/kits/physics-3d/behaviors/Player.jsx +629 -72
  303. package/kits/physics-3d/castle.json +3 -3
  304. package/kits/physics-3d/docs/pxmodel-format.md +8 -0
  305. package/kits/physics-3d/engine3d/Scene3DPlayer.jsx +69 -13
  306. package/kits/physics-3d/engine3d/actornode.js +86 -0
  307. package/kits/physics-3d/engine3d/animationBudget.js +23 -0
  308. package/kits/physics-3d/engine3d/editorWorld.js +6 -0
  309. package/kits/physics-3d/engine3d/emptynodes.js +232 -0
  310. package/kits/physics-3d/engine3d/framecap.js +96 -0
  311. package/kits/physics-3d/engine3d/gait.js +118 -0
  312. package/kits/physics-3d/engine3d/kithooks.js +47 -0
  313. package/kits/physics-3d/engine3d/look.js +507 -48
  314. package/kits/physics-3d/engine3d/lookbake.js +208 -0
  315. package/kits/physics-3d/engine3d/materialpresets.js +2 -0
  316. package/kits/physics-3d/engine3d/motion.js +39 -10
  317. package/kits/physics-3d/engine3d/nearshadows.js +37 -19
  318. package/kits/physics-3d/engine3d/optimize.js +9 -5
  319. package/kits/physics-3d/engine3d/orbitinput.js +487 -9
  320. package/kits/physics-3d/engine3d/orbitlens.js +48 -0
  321. package/kits/physics-3d/engine3d/ownmodel.js +213 -0
  322. package/kits/physics-3d/engine3d/pxmodel.js +42 -11
  323. package/kits/physics-3d/engine3d/shadowfade.js +882 -0
  324. package/kits/physics-3d/engine3d/sky.js +547 -0
  325. package/kits/physics-3d/engine3d/skylife.js +306 -0
  326. package/kits/physics-3d/engine3d/unchanged.js +11 -0
  327. package/kits/physics-3d/engine3d/viewrange.js +52 -0
  328. package/kits/physics-3d/engine3d/world3d.js +796 -69
  329. package/kits/physics-3d/main.jsx +5 -0
  330. package/kits/physics-3d/package.json +1 -1
  331. package/kits/physics-3d/scripts/look-baseline.mjs +155 -0
  332. package/kits/physics-3d/scripts/test-camera-orbit.mjs +2563 -80
  333. package/kits/physics-3d/scripts/test-drop-empty-parts.mjs +1 -1
  334. package/kits/physics-3d/scripts/test-frame-cap-bob.mjs +435 -0
  335. package/kits/physics-3d/scripts/test-frame-cap.mjs +452 -0
  336. package/kits/physics-3d/scripts/test-gait.mjs +387 -0
  337. package/kits/physics-3d/scripts/test-look-bake.mjs +1056 -0
  338. package/kits/physics-3d/scripts/test-model-parse-cache.mjs +92 -0
  339. package/kits/physics-3d/scripts/test-near-shadows.mjs +3 -0
  340. package/kits/physics-3d/scripts/test-orbit-shadow-fade.mjs +1920 -0
  341. package/kits/physics-3d/scripts/test-orbit-shift-lock.mjs +545 -0
  342. package/kits/physics-3d/scripts/test-orbit-shot.mjs +583 -0
  343. package/kits/physics-3d/scripts/test-orbit-view.mjs +230 -0
  344. package/kits/physics-3d/scripts/test-skip-empty-nodes.mjs +845 -0
  345. package/kits/physics-3d/scripts/test-sky.mjs +2470 -0
  346. package/kits/physics-3d/scripts/test-static-actors.mjs +8 -1
  347. package/kits/physics-3d/scripts/test-static-scenery.mjs +2171 -0
  348. package/kits/physics-3d/scripts/test-steady-hover.mjs +2 -0
  349. package/kits/physics-3d/scripts/test-view-range.mjs +260 -0
  350. package/kits/physics-3d/systems/physics3d.js +383 -6
  351. package/kits/real-time/castle.json +2 -2
  352. package/kits/real-time/code/server/gameHooks.js +7 -0
  353. package/kits/real-time/code/server/session.js +97 -19
  354. package/package.json +2 -2
  355. package/dist/shell/assets/index-Cf61cNfT.css +0 -1
@@ -0,0 +1,199 @@
1
+ // One town visit's account hearts and arrival offer. No browser storage.
2
+ const OFFER_MS = 15000;
3
+ const ARRIVAL_WAIT_MS = 60000;
4
+ // hub.results refreshes about once a minute; allow several reads to catch up.
5
+ const HEARTS_CATCH_UP_MS = 3 * 60000;
6
+
7
+ export function relativeDay(at, now = Date.now()) {
8
+ if (!Number.isFinite(at)) {
9
+ return '';
10
+ }
11
+ const day = (time) => {
12
+ const date = new Date(time);
13
+ return Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()) / 86400000;
14
+ };
15
+ const days = Math.max(0, day(now) - day(at));
16
+ return days === 0 ? 'today' : days === 1 ? 'yesterday' : `${days} days ago`;
17
+ }
18
+
19
+ export function toggleHeart(hearts, door) {
20
+ return hearts.includes(door) ? hearts.filter((id) => id !== door) : [...hearts, door].slice(-8);
21
+ }
22
+
23
+ export function makeYours(api, user, toast, clock = () => performance.now()) {
24
+ let visit = 0;
25
+ let started = false;
26
+ let signed = false;
27
+ let available = false;
28
+ let confirmed = [];
29
+ let pending = [];
30
+ let saving = false;
31
+ let latestHearts = null;
32
+ let authoritativeUntil = 0;
33
+ let until = 0;
34
+ let waitUntil = 0;
35
+ let arrived = false;
36
+ let offered = false;
37
+ let returning = null;
38
+ let returnLine = '';
39
+ let dismissed = false;
40
+ const hearts = () => pending.reduce((ids, door) => toggleHeart(ids, door), confirmed);
41
+
42
+ // Each toggle reads the account's hearts from ghost and writes the list with the
43
+ // toggle applied, one write at a time in this tab. The accepted limit: two tabs or
44
+ // devices of one account that toggle within the same read-to-write moment can each
45
+ // write over the other's read, so one of those two hearts is lost (tapped again, it
46
+ // stays). Ghost replaces the whole list; there is no atomic toggle. Hearts are a
47
+ // convenience list, as tokens.js accepts its own two-tab race for play money.
48
+ async function save() {
49
+ if (saving || !pending.length) {
50
+ return;
51
+ }
52
+ saving = true;
53
+ const token = visit;
54
+ const door = pending[0];
55
+ try {
56
+ const fresh = await api.hearts();
57
+ if (token !== visit) {
58
+ return;
59
+ }
60
+ if (fresh?.status !== 'ok') {
61
+ throw new Error('unavailable');
62
+ }
63
+ const answer = await api.setHearts(toggleHeart(fresh.hearts, door));
64
+ if (token !== visit) {
65
+ return;
66
+ }
67
+ if (answer?.status !== 'ok') {
68
+ throw new Error('unavailable');
69
+ }
70
+ confirmed = [...answer.hearts];
71
+ authoritativeUntil = clock() + HEARTS_CATCH_UP_MS;
72
+ } catch (error) {
73
+ if (token !== visit) {
74
+ return;
75
+ }
76
+ if (error?.code === 'UNKNOWN_COMMAND') {
77
+ available = false;
78
+ }
79
+ toast("Couldn't save that heart.");
80
+ } finally {
81
+ if (token === visit) {
82
+ pending.shift();
83
+ saving = false;
84
+ void save();
85
+ }
86
+ }
87
+ }
88
+
89
+ return {
90
+ clear() {
91
+ visit += 1;
92
+ started = signed = available = saving = offered = dismissed = arrived = false;
93
+ confirmed = [];
94
+ pending = [];
95
+ latestHearts = returning = null;
96
+ authoritativeUntil = 0;
97
+ returnLine = '';
98
+ until = waitUntil = 0;
99
+ },
100
+ returned(door, result = null) {
101
+ returning = door;
102
+ const last = Number.isFinite(result?.score) ? `Last ${result.score}${['ms', 's', 'points'].includes(result.unit) ? ` ${result.unit}` : ''}` : result?.outcome ?? '';
103
+ returnLine = [last, 'today'].filter(Boolean).join(' · ');
104
+ offered = false;
105
+ dismissed = false;
106
+ arrived = false;
107
+ until = waitUntil = 0;
108
+ },
109
+ frame(town, state, now) {
110
+ if (!town) {
111
+ return;
112
+ }
113
+ if (!started) {
114
+ started = true;
115
+ const token = visit;
116
+ Promise.resolve(user?.getCurrent?.()).then(async (me) => {
117
+ if (token !== visit || !me?.userId || me.isAnonymous) {
118
+ return;
119
+ }
120
+ signed = true;
121
+ if (typeof api?.hearts !== 'function') {
122
+ return;
123
+ }
124
+ const answer = await api.hearts();
125
+ if (token === visit && answer?.status === 'ok') {
126
+ available = true;
127
+ if (!pending.length) {
128
+ // The private snapshot may already have arrived and been latched.
129
+ confirmed = [...(latestHearts ?? answer.hearts)];
130
+ }
131
+ }
132
+ }).catch(() => {});
133
+ }
134
+ if (!waitUntil) {
135
+ waitUntil = now + ARRIVAL_WAIT_MS;
136
+ }
137
+ if (state) {
138
+ latestHearts = [...state.hearts];
139
+ }
140
+ // Reconsider even a buffered snapshot each frame once the write's
141
+ // freshness window expires; a matching snapshot never shortens it.
142
+ if (!pending.length && latestHearts && clock() >= authoritativeUntil && JSON.stringify(latestHearts) !== JSON.stringify(confirmed)) {
143
+ confirmed = [...latestHearts];
144
+ }
145
+ if (now < waitUntil && signed && (state?.rows?.length || confirmed.length || returning)) {
146
+ arrived = true;
147
+ }
148
+ },
149
+ rows(state, doors) {
150
+ if (!signed) {
151
+ return [];
152
+ }
153
+ const ids = hearts();
154
+ const recent = (state?.rows ?? []).map((r) => ({ ...r, line: [r.line, relativeDay(r.lastAt)].filter(Boolean).join(' · ') })).filter((r) => doors.some((d) => d.id === r.door) && (ids.includes(r.door) || r.line));
155
+ const hearted = ids.flatMap((id) => {
156
+ const door = doors.find((d) => d.id === id);
157
+ return door ? [{ ...(recent.find((r) => r.door === id) ?? { door: id, title: door.title, line: '' }), hearted: true }] : [];
158
+ });
159
+ return [...hearted, ...recent.filter((r) => !ids.includes(r.door)).map((r) => ({ ...r, hearted: false }))].slice(0, 5);
160
+ },
161
+ controls() {
162
+ return signed && available ? { hearts: hearts() } : null;
163
+ },
164
+ toggle(door) {
165
+ if (!signed || !available) {
166
+ return;
167
+ }
168
+ pending.push(door);
169
+ void save();
170
+ },
171
+ ask(state, doors, party, now) {
172
+ if (!signed || dismissed || (until && now >= until) || (!arrived && !offered && waitUntil && now >= waitUntil)) {
173
+ return null;
174
+ }
175
+ const rows = this.rows(state, doors);
176
+ const row = returning ? rows.find((r) => r.door === returning) ?? (() => {
177
+ const door = doors.find((d) => d.id === returning);
178
+ return door ? { door: door.id, title: door.title, line: '' } : null;
179
+ })() : rows[0];
180
+ if (!row) {
181
+ return null;
182
+ }
183
+ if (!offered) {
184
+ offered = true;
185
+ if (!until) {
186
+ until = now + OFFER_MS;
187
+ }
188
+ }
189
+ const door = doors.find((d) => d.id === row.door);
190
+ const controls = this.controls();
191
+ const line = row.line || (returning ? returnLine : '');
192
+ const together = party?.length && !door?.solo;
193
+ return { key: `yours:${visit}:${row.door}:${hearts().join(',')}:${line}:${Boolean(together)}:${Boolean(returning && controls)}`, kind: 'yours', door: row.door, text: `♥ ${row.title}`, line, yes: together ? 'Go together' : 'Go', no: 'Not now', ...(returning && controls ? { heart: hearts().includes(row.door) } : {}) };
194
+ },
195
+ dismiss() {
196
+ dismissed = true;
197
+ },
198
+ };
199
+ }
@@ -0,0 +1,45 @@
1
+ // v1.78: the same private spec rules at the API and at the client boundary.
2
+ const DIRECTIONS = /[\u0000-\u001f\u007f-\u009f\u061c\u200b-\u200f\u2028-\u202e\u2060-\u2069\ufeff]/;
3
+ const object = (value) => value !== null && typeof value === 'object' && [Object.prototype, null].includes(Object.getPrototypeOf(value));
4
+ function need(condition, message) {
5
+ if (!condition) {
6
+ throw new TypeError(`hub.companion: ${message}`);
7
+ }
8
+ }
9
+ export const DRESS = Object.freeze({
10
+ species: Object.freeze(['songbird', 'owl', 'parrot', 'hummingbird']),
11
+ wear: Object.freeze(['tophat', 'sunhat', 'bow', 'scarf', 'stampcap', 'flowercrown']),
12
+ tint: Object.freeze(['rose', 'mint', 'midnight', 'gold']),
13
+ });
14
+ export function checkDress(value) {
15
+ need(object(value), 'dress must be a plain object');
16
+ need(Reflect.ownKeys(value).every((key) => Object.hasOwn(DRESS, key)), 'dress has an unknown key');
17
+ const out = {};
18
+ for (const key of Object.keys(DRESS)) {
19
+ if (!Object.hasOwn(value, key)) { continue; }
20
+ const choice = value[key];
21
+ need(DRESS[key].includes(choice), `dress.${key} is unknown`);
22
+ if (choice !== 'songbird') { out[key] = choice; }
23
+ }
24
+ return Object.keys(out).length ? out : undefined;
25
+ }
26
+ export function check(spec) {
27
+ if (spec === null) {
28
+ return null;
29
+ }
30
+ need(object(spec), 'spec must be a plain object or null');
31
+ need(Reflect.ownKeys(spec).every((key) => ['look', 'say', 'at', 'dress', 'pose'].includes(key)), 'spec has an unknown key');
32
+ need(Object.hasOwn(spec, 'look'), 'look must be an own property');
33
+ const look = spec.look;
34
+ const hasSay = Object.hasOwn(spec, 'say'), hasAt = Object.hasOwn(spec, 'at');
35
+ const say = hasSay ? spec.say : undefined, at = hasAt ? spec.at : undefined;
36
+ need(look === 'lantern' || look === 'bird', 'look must be lantern or bird');
37
+ need(!hasSay || (typeof say === 'string' && say.length <= 240 && say.trim().length > 0 && Array.from(say).length <= 80 && !DIRECTIONS.test(say)), 'say must be 1..80 code points, at most 240 UTF-16 units, one non-blank line without control, direction or zero-width characters');
38
+ need(!hasAt || (typeof at === 'string' && at.length >= 1 && at.length <= 64), 'at must be a scene actor id of 1..64 characters');
39
+ const hasDress = Object.hasOwn(spec, 'dress');
40
+ need(!hasDress || look === 'bird', 'dress requires bird');
41
+ const dress = hasDress ? checkDress(spec.dress) : undefined;
42
+ const hasPose = Object.hasOwn(spec, 'pose'), pose = hasPose ? spec.pose : undefined;
43
+ need(!hasPose || (look === 'bird' && pose === 'show'), 'pose must be show and requires bird');
44
+ return { look, ...(hasSay ? { say } : {}), ...(hasAt ? { at } : {}), ...(dress ? { dress } : {}), ...(hasPose ? { pose } : {}) };
45
+ }
@@ -0,0 +1,7 @@
1
+ // Shared by private chip labels, prompt titles and answers: controls, every
2
+ // Bidi_Control character and the zero-width ones are refused.
3
+ export const REFUSED_TEXT = /[\u0000-\u001f\u007f-\u009f\u061c\u200b-\u200f\u202a-\u202e\u2060-\u2069\ufeff]/;
4
+
5
+ // Pure validation shared by the client and server; no hooks or state imports.
6
+ export const validPrivateText = (text, max) => typeof text === 'string' && text.length <= 4 * max &&
7
+ text.trim().length > 0 && Array.from(text).length <= max && !REFUSED_TEXT.test(text);
@@ -0,0 +1,133 @@
1
+ // Action chips (contract v1.74): private offers; the place's hook alone
2
+ // decides what a press does. No state exists in a place without `actions`.
3
+ import { REFUSED_TEXT as DIRECTIONS } from '../privateText.js';
4
+
5
+ const ID = /^[a-z][a-z0-9_-]{0,23}$/;
6
+ const SEND_EVERY_MS = 250;
7
+ const object = (value) => value !== null && typeof value === 'object' && [Object.prototype, null].includes(Object.getPrototypeOf(value));
8
+ const id = (value) => typeof value === 'string' && ID.exec(value)?.[0] === value;
9
+
10
+ function need(condition, message) {
11
+ if (!condition) {
12
+ throw new TypeError(`hub.actions: ${message}`);
13
+ }
14
+ }
15
+ function check(items) {
16
+ if (items === null) {
17
+ return null;
18
+ }
19
+ need(Array.isArray(items) && items.length <= 3, 'items must be null or an array of 0..3 items');
20
+ const out = Array.from(items, (item) => {
21
+ need(object(item), 'item must be a plain object');
22
+ need(Reflect.ownKeys(item).every((key) => ['id', 'label'].includes(key)), 'item has an unknown key');
23
+ need(Object.hasOwn(item, 'id') && Object.hasOwn(item, 'label'), 'id and label must be own properties');
24
+ const itemId = item.id, label = item.label;
25
+ need(id(itemId), 'id must be a short id');
26
+ need(typeof label === 'string' && label.length <= 32 && label.trim().length > 0 && Array.from(label).length <= 16 && !DIRECTIONS.test(label), 'label must be 1..16 characters (code points), no control, direction or zero-width characters');
27
+ return { id: itemId, label };
28
+ });
29
+ need(new Set(out.map((item) => item.id)).size === out.length, 'ids must be unique');
30
+ return out.length ? out : null;
31
+ }
32
+ function present(h, playerId) {
33
+ return h.session?.players.some((p) => p.playerId === playerId) && h.sim?.players.has(playerId);
34
+ }
35
+ function arrived(h, playerId) {
36
+ const entry = h.players.get(playerId);
37
+ return present(h, playerId) && entry?.arrived && !entry.ended;
38
+ }
39
+ function current(h, playerId) {
40
+ return h.actionsMe?.get(playerId) ?? { items: null, n: 0, json: 'null', sentJson: 'null', sentN: 0, sentAt: -Infinity };
41
+ }
42
+ function send(h, playerId, state) {
43
+ h.session?.send(playerId, { k: 'hub-actions', items: state.items, n: state.n });
44
+ state.sentN = state.n;
45
+ state.sentJson = state.json;
46
+ state.sentAt = h.sim?.timeMs ?? 0;
47
+ }
48
+ // One counter for the place's session, so no offer ever shares a version with
49
+ // an earlier one, whoever it was for and whenever they left and came back.
50
+ function version(h) {
51
+ h.actionsSeq = (h.actionsSeq ?? 0) + 1;
52
+ return h.actionsSeq;
53
+ }
54
+
55
+ export function set(h, playerId, items) {
56
+ need(h.place.actions === true, 'this place does not declare actions');
57
+ need(typeof playerId === 'string', 'playerId must be a string');
58
+ const checked = check(items);
59
+ if (!arrived(h, playerId)) {
60
+ return false;
61
+ }
62
+ const state = current(h, playerId), json = JSON.stringify(checked);
63
+ if (state.json === json) {
64
+ return true;
65
+ }
66
+ // Record every changed list; the tick sends only the latest, at most 4/s.
67
+ // Back to the list the player was last sent, with nothing sent since: that
68
+ // offer and its version stand (the client never heard of the ones between).
69
+ const n = json === state.sentJson ? state.sentN : version(h);
70
+ (h.actionsMe ??= new Map()).set(playerId, { ...state, items: checked, json, n });
71
+ return true;
72
+ }
73
+
74
+ export function message(h, playerId, data) {
75
+ // Even a stale or malformed press spends a token. Six/s, burst eight.
76
+ if (h.place.actions !== true || !present(h, playerId)) {
77
+ return;
78
+ }
79
+ const now = h.sim.timeMs, budgets = (h.actionsBudget ??= new Map());
80
+ const b = budgets.get(playerId) ?? { tokens: 8, at: now };
81
+ b.tokens = Math.min(8, b.tokens + Math.max(0, now - b.at) * 0.006);
82
+ b.at = now;
83
+ budgets.set(playerId, b);
84
+ if (b.tokens < 1) {
85
+ return;
86
+ }
87
+ b.tokens -= 1;
88
+ if (!arrived(h, playerId) || !object(data) || data.k !== 'hub-action' ||
89
+ !Object.keys(data).every((key) => ['k', 'id', 'n'].includes(key))) {
90
+ return;
91
+ }
92
+ const actionId = data.id, n = data.n, state = current(h, playerId);
93
+ // The offer in force is the one this player was sent: a list recorded and
94
+ // not yet sent cannot be pressed, however its version was guessed.
95
+ if (!id(actionId) || !Number.isSafeInteger(n) || n !== state.n || n !== state.sentN || !state.items?.some((item) => item.id === actionId)) {
96
+ return;
97
+ }
98
+ const failed = (error) => {
99
+ if (!h.actionFailed) {
100
+ h.actionFailed = true;
101
+ console.warn(`[hub] action hook failed: ${error?.message ?? error}`);
102
+ }
103
+ };
104
+ try {
105
+ Promise.resolve(h.hooks.action?.(h.ctx, playerId, actionId)).catch(failed);
106
+ } catch (error) {
107
+ failed(error);
108
+ }
109
+ }
110
+
111
+ export function joinedAfresh(h, playerId) {
112
+ const state = current(h, playerId);
113
+ (h.actionsMe ??= new Map()).set(playerId, state);
114
+ send(h, playerId, state);
115
+ }
116
+ export function forget(h, playerId) {
117
+ h.actionsMe?.delete(playerId);
118
+ h.actionsBudget?.delete(playerId);
119
+ }
120
+ export function prune(h) {
121
+ for (const playerId of new Set([...(h.actionsMe?.keys() ?? []), ...(h.actionsBudget?.keys() ?? [])])) {
122
+ if (!present(h, playerId)) {
123
+ forget(h, playerId);
124
+ continue;
125
+ }
126
+ const state = h.actionsMe?.get(playerId);
127
+ // By version, not by content: a list changed and changed back has a new
128
+ // version the client must hear, or its presses would never match.
129
+ if (state && state.n !== state.sentN && h.sim.timeMs - state.sentAt >= SEND_EVERY_MS) {
130
+ send(h, playerId, state);
131
+ }
132
+ }
133
+ }
@@ -21,7 +21,7 @@ export const TINT_RATE = 20;
21
21
  export const TINT_BURST = 40;
22
22
  export const MAX_TINTS = 128;
23
23
  // The box a moved actor stays in: multiplayer-3d's world bounds.
24
- const BOUNDS = { xz: 200, low: -30, high: 80 };
24
+ export const BOUNDS = { xz: 200, low: -30, high: 80 };
25
25
  // A turn a move may ask for at most (a hundred turns): enough for any spin,
26
26
  // small enough that interpolating it stays finite.
27
27
  const MAX_TURN_DEG = 36000;
@@ -400,6 +400,14 @@ export function despawn(h, id, engine) {
400
400
  for (const mine of h.labelsMe?.values() ?? []) {
401
401
  mine.delete(id);
402
402
  }
403
+ // v1.62: a gone copy's private visibility goes with it, so it never holds
404
+ // one of a viewer's override slots; the next send carries the shorter list.
405
+ for (const [viewer, mine] of h.visMe ?? []) {
406
+ mine.delete(id);
407
+ if (mine.size === 0) {
408
+ h.visMe.delete(viewer);
409
+ }
410
+ }
403
411
  if (h.sim && engine?.removeActor) {
404
412
  engine.removeActor(h.sim, id);
405
413
  }
@@ -0,0 +1,155 @@
1
+ // Private text prompts (contract v1.75). Only the place hook receives text;
2
+ // no answer is retained, echoed, included in state or used in an error line.
3
+ import { validPrivateText as textOk } from '../privateText.js';
4
+
5
+ const ID = /^[a-z][a-z0-9_-]{0,23}$/;
6
+ const SEND_EVERY_MS = 250;
7
+ const object = (value) => value !== null && typeof value === 'object' && [Object.prototype, null].includes(Object.getPrototypeOf(value));
8
+ const id = (value) => typeof value === 'string' && ID.exec(value)?.[0] === value;
9
+
10
+ function need(condition, message) {
11
+ if (!condition) {
12
+ throw new TypeError(`hub.ask: ${message}`);
13
+ }
14
+ }
15
+ function check(spec) {
16
+ if (spec === null) {
17
+ return null;
18
+ }
19
+ need(object(spec), 'spec must be a plain object or null');
20
+ need(Reflect.ownKeys(spec).length === 3 && ['id', 'title', 'max'].every((key) => Object.hasOwn(spec, key)), 'spec must have exactly own id, title and max');
21
+ const promptId = spec.id, title = spec.title, max = spec.max;
22
+ need(id(promptId), 'id must be a short id');
23
+ need(textOk(title, 32), 'title must be 1..32 code points, no control, direction or zero-width characters');
24
+ need(Number.isSafeInteger(max) && max >= 1 && max <= 32, 'max must be an integer in 1..32');
25
+ return { id: promptId, title, max };
26
+ }
27
+ function present(h, playerId) {
28
+ return h.session?.players.some((p) => p.playerId === playerId) && h.sim?.players.has(playerId);
29
+ }
30
+ function arrived(h, playerId) {
31
+ const entry = h.players.get(playerId);
32
+ return present(h, playerId) && entry?.arrived && !entry.ended;
33
+ }
34
+ function current(h, playerId) {
35
+ return h.askMe?.get(playerId) ?? { spec: null, n: 0, json: 'null', sentJson: 'null', sentN: 0, sentId: null, sentAt: -Infinity };
36
+ }
37
+ function version(h) {
38
+ const n = (h.askSeq ?? 0) + 1;
39
+ need(Number.isSafeInteger(n), 'version counter exhausted');
40
+ h.askSeq = n;
41
+ return n;
42
+ }
43
+ function send(h, playerId, state) {
44
+ h.session?.send(playerId, state.spec ? { k: 'hub-ask', ...state.spec, n: state.n } : { k: 'hub-ask', id: null, n: state.n });
45
+ state.sentN = state.n;
46
+ state.sentId = state.spec?.id ?? null;
47
+ state.sentJson = state.json;
48
+ state.sentAt = h.sim?.timeMs ?? 0;
49
+ }
50
+ export function set(h, playerId, spec) {
51
+ need(h.place.ask === true, 'this place does not declare ask');
52
+ need(typeof playerId === 'string', 'playerId must be a string');
53
+ const checked = check(spec);
54
+ if (!arrived(h, playerId)) {
55
+ return false;
56
+ }
57
+ const state = current(h, playerId), json = JSON.stringify(checked);
58
+ if (state.json !== json) {
59
+ // Every change invalidates the old version, even if it changes back
60
+ // before delivery. Delivery equality includes that new version.
61
+ const n = version(h);
62
+ (h.askMe ??= new Map()).set(playerId, { ...state, spec: checked, json, n });
63
+ }
64
+ return true;
65
+ }
66
+ // A separate scope: the rejection handler retains only the session, never
67
+ // the incoming message or answer while an async hook is pending.
68
+ function failureHandler(h) {
69
+ return () => {
70
+ if (!h.answerFailed) {
71
+ h.answerFailed = true;
72
+ // Even the hook's error message could contain the answer. Never log it.
73
+ console.warn('[hub] answer hook failed');
74
+ }
75
+ };
76
+ }
77
+ const ANSWER_KEYS = ['k', 'id', 'n', 'text'];
78
+ function answerKeys(data) {
79
+ // Parsed wire objects have enumerable string keys: stop at the fifth,
80
+ // without allocating an array of every key or reading any field values.
81
+ if (!object(data)) {
82
+ return false;
83
+ }
84
+ let count = 0;
85
+ for (const key in data) {
86
+ if (Object.hasOwn(data, key) && (++count > 4 || !ANSWER_KEYS.includes(key))) {
87
+ return false;
88
+ }
89
+ }
90
+ // Reflection is needed to catch hidden/symbol extras too. For ordinary
91
+ // JSON objects the bounded loop above has already limited this to four.
92
+ return Reflect.ownKeys(data).length === 4 && ANSWER_KEYS.every((key) => Object.hasOwn(data, key));
93
+ }
94
+ export function message(h, playerId, data) {
95
+ // A Proxy or getter is not ordinary wire data, but must still be contained.
96
+ // Validation failures are silent; only failures in the place hook log once.
97
+ try {
98
+ if (h.place.ask !== true || !present(h, playerId)) {
99
+ return;
100
+ }
101
+ // Every attempted answer spends a token, including malformed/oversized ones.
102
+ const now = h.sim.timeMs, budgets = (h.askBudget ??= new Map());
103
+ const b = budgets.get(playerId) ?? { tokens: 4, at: now };
104
+ b.tokens = Math.min(4, b.tokens + Math.max(0, now - b.at) * 0.002);
105
+ b.at = now;
106
+ budgets.set(playerId, b);
107
+ if (b.tokens < 1) {
108
+ return;
109
+ }
110
+ b.tokens -= 1;
111
+ if (!arrived(h, playerId) || !answerKeys(data) || data.k !== 'hub-answer') {
112
+ return;
113
+ }
114
+ const promptId = data.id, n = data.n, state = current(h, playerId);
115
+ if (!state.spec || !id(promptId) || promptId !== state.spec.id || promptId !== state.sentId ||
116
+ !Number.isSafeInteger(n) || n !== state.n || n !== state.sentN) {
117
+ return;
118
+ }
119
+ // Once the prompt's identity matches, refused text is a cancel. It never
120
+ // reaches content, and cannot strand a client that already closed its card.
121
+ const value = data.text, text = value === null || !textOk(value, state.spec.max) ? null : value;
122
+ set(h, playerId, null);
123
+ // The close shares the tick delivery budget. A hook may reopen the same
124
+ // spec before delivery; its advanced n still has to be sent.
125
+ const failed = failureHandler(h);
126
+ try {
127
+ Promise.resolve(h.hooks.answer?.(h.ctx, playerId, promptId, text)).catch(failed);
128
+ } catch {
129
+ failed();
130
+ }
131
+ } catch {
132
+ // Never log validation errors: a throwing getter may put the text in one.
133
+ }
134
+ }
135
+ export function joinedAfresh(h, playerId) {
136
+ const state = current(h, playerId);
137
+ (h.askMe ??= new Map()).set(playerId, state);
138
+ send(h, playerId, state);
139
+ }
140
+ export function forget(h, playerId) {
141
+ h.askMe?.delete(playerId);
142
+ h.askBudget?.delete(playerId);
143
+ }
144
+ export function prune(h) {
145
+ for (const playerId of new Set([...(h.askMe?.keys() ?? []), ...(h.askBudget?.keys() ?? [])])) {
146
+ if (!present(h, playerId)) {
147
+ forget(h, playerId);
148
+ continue;
149
+ }
150
+ const state = h.askMe?.get(playerId);
151
+ if (state && (state.json !== state.sentJson || state.n !== state.sentN) && h.sim.timeMs - state.sentAt >= SEND_EVERY_MS) {
152
+ send(h, playerId, state);
153
+ }
154
+ }
155
+ }