castle-web-cli 0.4.238 → 0.4.240

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 (404) hide show
  1. package/dist/agent.js +5 -1
  2. package/dist/api.d.ts +20 -0
  3. package/dist/api.js +57 -0
  4. package/dist/bundle.d.ts +38 -1
  5. package/dist/bundle.js +309 -13
  6. package/dist/castle-host/host.js +218 -11
  7. package/dist/castleJson.d.ts +10 -0
  8. package/dist/castleJson.js +46 -2
  9. package/dist/check.js +5 -2
  10. package/dist/devSessionServer.js +4 -1
  11. package/dist/devSessionStorage.d.ts +1 -0
  12. package/dist/devSessionStorage.js +32 -7
  13. package/dist/importOrder.d.ts +2 -0
  14. package/dist/importOrder.js +29 -0
  15. package/dist/index.js +22 -3
  16. package/dist/init.js +24 -2
  17. package/dist/multiplayerDoc.d.ts +1 -1
  18. package/dist/multiplayerDoc.js +9 -0
  19. package/dist/platformDoc.js +4 -2
  20. package/dist/playPagePolicy.d.ts +1 -0
  21. package/dist/playPagePolicy.js +12 -3
  22. package/dist/resetLeaderboards.d.ts +8 -1
  23. package/dist/resetLeaderboards.js +43 -8
  24. package/dist/roomLogs.d.ts +39 -0
  25. package/dist/roomLogs.js +166 -0
  26. package/dist/save-deck.d.ts +32 -2
  27. package/dist/save-deck.js +240 -39
  28. package/kits/base/CLAUDE.md +2 -1
  29. package/kits/base/castle.json +1 -1
  30. package/kits/base/sdk/README.md +240 -8
  31. package/kits/base/sdk/analytics.d.ts +8 -0
  32. package/kits/base/sdk/analytics.js +92 -0
  33. package/kits/base/sdk/castle.d.ts +8 -2
  34. package/kits/base/sdk/castle.js +3 -0
  35. package/kits/base/sdk/commands.d.ts +79 -0
  36. package/kits/base/sdk/commands.js +1 -0
  37. package/kits/base/sdk/debugConsole.d.ts +17 -0
  38. package/kits/base/sdk/debugConsole.js +154 -0
  39. package/kits/base/sdk/experiments.js +24 -1
  40. package/kits/base/sdk/hostRetry.d.ts +7 -0
  41. package/kits/base/sdk/hostRetry.js +57 -0
  42. package/kits/base/sdk/hub.d.ts +20 -2
  43. package/kits/base/sdk/hub.js +99 -13
  44. package/kits/base/sdk/multiplayer.d.ts +2 -0
  45. package/kits/base/sdk/multiplayer.js +198 -26
  46. package/kits/base/sdk/reconnectBudget.d.ts +6 -0
  47. package/kits/base/sdk/reconnectBudget.js +41 -1
  48. package/kits/base/sdk/rooms.d.ts +11 -0
  49. package/kits/base/sdk/rooms.js +22 -0
  50. package/kits/base/sdk/runtime.js +36 -19
  51. package/kits/base/sdk/server/hub.d.ts +19 -0
  52. package/kits/base/sdk/server/hub.js +12 -0
  53. package/kits/base/sdk/server/platformHandle.d.ts +1 -0
  54. package/kits/base/sdk/server/wrapper.d.ts +1 -1
  55. package/kits/base/sdk/server/wrapper.js +26 -1
  56. package/kits/base/sdk/store.d.ts +8 -2
  57. package/kits/base/sdk/store.js +5 -1
  58. package/kits/base/sdk/transport.d.ts +2 -1
  59. package/kits/base/sdk/transport.js +35 -0
  60. package/kits/hub-game/CLAUDE.md +66 -19
  61. package/kits/hub-game/castle.json +4 -4
  62. package/kits/hub-game/code/client/act.js +9 -6
  63. package/kits/hub-game/code/client/actCore.js +26 -6
  64. package/kits/hub-game/code/client/actGateLog.js +36 -0
  65. package/kits/hub-game/code/client/actionOverflow.js +29 -8
  66. package/kits/hub-game/code/client/actionRailSurface.js +186 -0
  67. package/kits/hub-game/code/client/actionRailTree.js +173 -0
  68. package/kits/hub-game/code/client/askSurface.js +85 -0
  69. package/kits/hub-game/code/client/askTree.js +51 -0
  70. package/kits/hub-game/code/client/attention.js +31 -6
  71. package/kits/hub-game/code/client/ballot.js +42 -5
  72. package/kits/hub-game/code/client/ballotSurface.js +115 -0
  73. package/kits/hub-game/code/client/ballotTree.js +180 -0
  74. package/kits/hub-game/code/client/catchingUp.js +19 -0
  75. package/kits/hub-game/code/client/celebrate.js +66 -0
  76. package/kits/hub-game/code/client/chrome.js +474 -53
  77. package/kits/hub-game/code/client/companion.js +93 -23
  78. package/kits/hub-game/code/client/companionDress.js +1 -1
  79. package/kits/hub-game/code/client/cornerSurface.js +106 -0
  80. package/kits/hub-game/code/client/cornerTree.js +83 -0
  81. package/kits/hub-game/code/client/decor.js +20 -11
  82. package/kits/hub-game/code/client/deviceStorage.js +121 -0
  83. package/kits/hub-game/code/client/disguise.js +23 -0
  84. package/kits/hub-game/code/client/disguiseDraw.js +87 -0
  85. package/kits/hub-game/code/client/dockSurface.js +206 -0
  86. package/kits/hub-game/code/client/dockTree.js +232 -0
  87. package/kits/hub-game/code/client/earned.js +29 -0
  88. package/kits/hub-game/code/client/emoteSurface.js +188 -0
  89. package/kits/hub-game/code/client/emoteTree.js +264 -0
  90. package/kits/hub-game/code/client/farAvatars.js +4 -3
  91. package/kits/hub-game/code/client/fpsTelemetry.js +3 -1
  92. package/kits/hub-game/code/client/grab.js +121 -50
  93. package/kits/hub-game/code/client/holdShow.js +19 -0
  94. package/kits/hub-game/code/client/hookLayer.js +96 -12
  95. package/kits/hub-game/code/client/hubControls.js +7 -1
  96. package/kits/hub-game/code/client/hudSurface.js +11 -3
  97. package/kits/hub-game/code/client/hudTree.js +7 -4
  98. package/kits/hub-game/code/client/idle.js +21 -3
  99. package/kits/hub-game/code/client/invites.js +24 -3
  100. package/kits/hub-game/code/client/keys.js +15 -0
  101. package/kits/hub-game/code/client/labelsNear.js +8 -1
  102. package/kits/hub-game/code/client/layoutCache.js +57 -8
  103. package/kits/hub-game/code/client/loadingCover.js +53 -4
  104. package/kits/hub-game/code/client/menuIcons.js +15 -0
  105. package/kits/hub-game/code/client/menuRows.js +8 -2
  106. package/kits/hub-game/code/client/menuSurface.js +102 -0
  107. package/kits/hub-game/code/client/menuTree.js +135 -0
  108. package/kits/hub-game/code/client/modelLook.js +41 -0
  109. package/kits/hub-game/code/client/pickerSurface.js +152 -0
  110. package/kits/hub-game/code/client/pickerTree.js +163 -0
  111. package/kits/hub-game/code/client/pushVisit.js +4 -2
  112. package/kits/hub-game/code/client/quality.js +188 -29
  113. package/kits/hub-game/code/client/qualityEntry.js +10 -1
  114. package/kits/hub-game/code/client/quickChatSurface.js +222 -0
  115. package/kits/hub-game/code/client/quickChatTree.js +201 -0
  116. package/kits/hub-game/code/client/rehello.js +75 -0
  117. package/kits/hub-game/code/client/renderVisibility.js +17 -0
  118. package/kits/hub-game/code/client/replay.js +118 -0
  119. package/kits/hub-game/code/client/role.js +3 -0
  120. package/kits/hub-game/code/client/safeLayout.js +441 -56
  121. package/kits/hub-game/code/client/safeStyle.js +13 -10
  122. package/kits/hub-game/code/client/say.js +2 -0
  123. package/kits/hub-game/code/client/soundSurface.js +77 -0
  124. package/kits/hub-game/code/client/soundTree.js +74 -0
  125. package/kits/hub-game/code/client/soundscape.js +12 -9
  126. package/kits/hub-game/code/client/soundscapeCues.js +1 -1
  127. package/kits/hub-game/code/client/state.js +67 -1
  128. package/kits/hub-game/code/client/stuckSurface.js +76 -0
  129. package/kits/hub-game/code/client/stuckTree.js +67 -0
  130. package/kits/hub-game/code/client/talk.js +42 -12
  131. package/kits/hub-game/code/client/talkTree.js +4 -2
  132. package/kits/hub-game/code/client/textAskSurface.js +147 -0
  133. package/kits/hub-game/code/client/textAskTree.js +67 -0
  134. package/kits/hub-game/code/client/tiers.js +252 -27
  135. package/kits/hub-game/code/client/together.js +4 -1
  136. package/kits/hub-game/code/client/uiDom.js +86 -8
  137. package/kits/hub-game/code/client/uiTree.js +33 -4
  138. package/kits/hub-game/code/client/vehicle.js +75 -52
  139. package/kits/hub-game/code/client/weakestTier.js +75 -31
  140. package/kits/hub-game/code/client/wear.js +159 -5
  141. package/kits/hub-game/code/client/worldLabels.js +215 -26
  142. package/kits/hub-game/code/companionSpec.js +4 -2
  143. package/kits/hub-game/code/server/act.js +3 -0
  144. package/kits/hub-game/code/server/actGate.js +83 -0
  145. package/kits/hub-game/code/server/actions.js +71 -15
  146. package/kits/hub-game/code/server/actors.js +20 -5
  147. package/kits/hub-game/code/server/analytics.js +40 -15
  148. package/kits/hub-game/code/server/ask.js +39 -3
  149. package/kits/hub-game/code/server/ballot.js +167 -4
  150. package/kits/hub-game/code/server/celebrate.js +64 -0
  151. package/kits/hub-game/code/server/companion.js +17 -2
  152. package/kits/hub-game/code/server/decor.js +11 -2
  153. package/kits/hub-game/code/server/disguise.js +63 -0
  154. package/kits/hub-game/code/server/doorAnalytics.js +2 -5
  155. package/kits/hub-game/code/server/earned.js +6 -2
  156. package/kits/hub-game/code/server/friendBests.js +13 -4
  157. package/kits/hub-game/code/server/friendsAmong.js +67 -8
  158. package/kits/hub-game/code/server/grab.js +12 -2
  159. package/kits/hub-game/code/server/heldOffers.js +142 -0
  160. package/kits/hub-game/code/server/holdShow.js +98 -0
  161. package/kits/hub-game/code/server/hub.js +854 -45
  162. package/kits/hub-game/code/server/idle.js +110 -9
  163. package/kits/hub-game/code/server/index.js +9 -0
  164. package/kits/hub-game/code/server/labels.js +13 -3
  165. package/kits/hub-game/code/server/loadAbandon.js +2 -5
  166. package/kits/hub-game/code/server/manifest.js +94 -6
  167. package/kits/hub-game/code/server/menu.js +26 -3
  168. package/kits/hub-game/code/server/npcs.js +55 -9
  169. package/kits/hub-game/code/server/observe.js +67 -6
  170. package/kits/hub-game/code/server/players.js +8 -1
  171. package/kits/hub-game/code/server/replay.js +135 -0
  172. package/kits/hub-game/code/server/storageLimit.js +4 -1
  173. package/kits/hub-game/code/server/townDay.js +74 -0
  174. package/kits/hub-game/code/server/trackQueue.js +29 -0
  175. package/kits/hub-game/code/server/vehicle.js +3 -0
  176. package/kits/hub-game/code/server/vis.js +26 -4
  177. package/kits/hub-game/code/server/wallet.js +677 -0
  178. package/kits/hub-game/code/server/wear.js +13 -3
  179. package/kits/hub-game/code/server/writerLease.js +191 -0
  180. package/kits/hub-game/code/shared/act.js +1 -1
  181. package/kits/hub-game/code/shared/expression.js +6 -0
  182. package/kits/hub-game/code/shared/replay.js +37 -0
  183. package/kits/hub-game/code/systems/hub.js +437 -57
  184. package/kits/hub-game/docs/analytics.md +20 -3
  185. package/kits/hub-game/docs/contract.md +942 -99
  186. package/kits/hub-game/docs/home-pieces.md +5 -6
  187. package/kits/hub-game/docs/observed-progress.md +49 -1
  188. package/kits/hub-game/docs/vehicle-preset.md +23 -0
  189. package/kits/hub-game/index.html +1 -0
  190. package/kits/hub-game/models/hats/baker-cap.pxmodel +3 -3
  191. package/kits/hub-game/models/hats/bat-clip.pxmodel +3 -3
  192. package/kits/hub-game/models/hats/beekeeper.pxmodel +3 -3
  193. package/kits/hub-game/models/hats/cap.pxmodel +3 -3
  194. package/kits/hub-game/models/hats/captain.pxmodel +3 -3
  195. package/kits/hub-game/models/hats/chef-toque.pxmodel +3 -3
  196. package/kits/hub-game/models/hats/crooked-top.pxmodel +3 -3
  197. package/kits/hub-game/models/hats/delivery-cap.pxmodel +3 -3
  198. package/kits/hub-game/models/hats/fire-helmet.pxmodel +3 -3
  199. package/kits/hub-game/models/hats/flower-crown.pxmodel +3 -3
  200. package/kits/hub-game/models/hats/foam-crown.pxmodel +3 -3
  201. package/kits/hub-game/models/hats/hard-hat.pxmodel +3 -3
  202. package/kits/hub-game/models/hats/lantern.pxmodel +3 -3
  203. package/kits/hub-game/models/hats/laurel.pxmodel +3 -3
  204. package/kits/hub-game/models/hats/lighthouse.pxmodel +3 -3
  205. package/kits/hub-game/models/hats/nurse-cap.pxmodel +3 -3
  206. package/kits/hub-game/models/hats/police-cap.pxmodel +3 -3
  207. package/kits/hub-game/models/hats/sailor.pxmodel +3 -3
  208. package/kits/hub-game/models/hats/souwester.pxmodel +3 -3
  209. package/kits/hub-game/models/hats/straw.pxmodel +3 -3
  210. package/kits/hub-game/models/hats/sunflower.pxmodel +3 -3
  211. package/kits/hub-game/models/hats/sweatband.pxmodel +3 -3
  212. package/kits/hub-game/models/hats/teacher-beret.pxmodel +3 -3
  213. package/kits/hub-game/models/hats/witch.pxmodel +3 -3
  214. package/kits/hub-game/models/hold-show/daisy.pxmodel +231 -0
  215. package/kits/hub-game/models/hold-show/moonflower.pxmodel +171 -0
  216. package/kits/hub-game/models/hold-show/rose.pxmodel +191 -0
  217. package/kits/hub-game/models/hold-show/square/daisy.pxmodel +233 -0
  218. package/kits/hub-game/models/hold-show/square/moonflower.pxmodel +173 -0
  219. package/kits/hub-game/models/hold-show/square/rose.pxmodel +193 -0
  220. package/kits/hub-game/models/hold-show/square/sunflower.pxmodel +313 -0
  221. package/kits/hub-game/models/hold-show/square/tulip.pxmodel +133 -0
  222. package/kits/hub-game/models/hold-show/sunflower.pxmodel +311 -0
  223. package/kits/hub-game/models/hold-show/tulip.pxmodel +131 -0
  224. package/kits/hub-game/package-lock.json +3 -3
  225. package/kits/hub-game/package.json +4 -3
  226. package/kits/hub-game/scripts/fixtures/engine-models-stub.mjs +1 -0
  227. package/kits/hub-game/scripts/fixtures/hud-surface.json +12 -12
  228. package/kits/hub-game/scripts/fixtures/talk-surface.json +8 -8
  229. package/kits/hub-game/scripts/gen-hats.mjs +1 -1
  230. package/kits/hub-game/scripts/gen-hold-show.mjs +27 -0
  231. package/kits/hub-game/scripts/pack.mjs +89 -23
  232. package/kits/hub-game/scripts/test-act-client.mjs +5 -0
  233. package/kits/hub-game/scripts/test-act-core.mjs +32 -0
  234. package/kits/hub-game/scripts/test-act-gate.mjs +186 -0
  235. package/kits/hub-game/scripts/test-action-rail-crowded-dom.mjs +7 -1
  236. package/kits/hub-game/scripts/test-action-rail-default-dom.mjs +102 -0
  237. package/kits/hub-game/scripts/test-action-rail-never-silent-dom.mjs +50 -0
  238. package/kits/hub-game/scripts/test-action-rail-surface.mjs +368 -0
  239. package/kits/hub-game/scripts/test-action-rail-town-jobs-dom.mjs +118 -0
  240. package/kits/hub-game/scripts/test-actions-client.mjs +5 -3
  241. package/kits/hub-game/scripts/test-actions.mjs +24 -0
  242. package/kits/hub-game/scripts/test-actors.mjs +19 -0
  243. package/kits/hub-game/scripts/test-again-corner-dom.mjs +59 -0
  244. package/kits/hub-game/scripts/test-analytics.mjs +80 -1
  245. package/kits/hub-game/scripts/test-arrival-pose-server.mjs +25 -8
  246. package/kits/hub-game/scripts/test-ask-client.mjs +9 -0
  247. package/kits/hub-game/scripts/test-ask-surface.mjs +188 -0
  248. package/kits/hub-game/scripts/test-attention.mjs +43 -1
  249. package/kits/hub-game/scripts/test-ballot-sheet-dom.mjs +71 -0
  250. package/kits/hub-game/scripts/test-ballot-surface.mjs +276 -0
  251. package/kits/hub-game/scripts/test-ballot.mjs +420 -3
  252. package/kits/hub-game/scripts/test-ban-join.mjs +1 -1
  253. package/kits/hub-game/scripts/test-celebrate-client.mjs +62 -0
  254. package/kits/hub-game/scripts/test-celebrate.mjs +116 -0
  255. package/kits/hub-game/scripts/test-chat.mjs +73 -0
  256. package/kits/hub-game/scripts/test-companion-client.mjs +203 -21
  257. package/kits/hub-game/scripts/test-companion.mjs +110 -3
  258. package/kits/hub-game/scripts/test-control-layout-dom.mjs +57 -10
  259. package/kits/hub-game/scripts/test-corner-surface.mjs +268 -0
  260. package/kits/hub-game/scripts/test-decor-client.mjs +59 -3
  261. package/kits/hub-game/scripts/test-decor.mjs +27 -0
  262. package/kits/hub-game/scripts/test-device-class.mjs +4 -1
  263. package/kits/hub-game/scripts/test-disguise-client.mjs +281 -0
  264. package/kits/hub-game/scripts/test-disguise.mjs +176 -0
  265. package/kits/hub-game/scripts/test-dock-surface.mjs +404 -0
  266. package/kits/hub-game/scripts/test-door-analytics.mjs +1 -1
  267. package/kits/hub-game/scripts/test-door-stay-client.mjs +602 -0
  268. package/kits/hub-game/scripts/test-door-stay.mjs +613 -0
  269. package/kits/hub-game/scripts/test-earned-client.mjs +119 -0
  270. package/kits/hub-game/scripts/test-emote-surface.mjs +616 -0
  271. package/kits/hub-game/scripts/test-fps-telemetry.mjs +39 -4
  272. package/kits/hub-game/scripts/test-friend-bests.mjs +38 -12
  273. package/kits/hub-game/scripts/test-ghost-click-dom.mjs +75 -0
  274. package/kits/hub-game/scripts/test-grab-client.mjs +29 -2
  275. package/kits/hub-game/scripts/test-grab-orbit.mjs +260 -11
  276. package/kits/hub-game/scripts/test-grab.mjs +10 -1
  277. package/kits/hub-game/scripts/test-hats.mjs +28 -0
  278. package/kits/hub-game/scripts/test-held-offers.mjs +394 -0
  279. package/kits/hub-game/scripts/test-high-only.mjs +3 -3
  280. package/kits/hub-game/scripts/test-hold-show-client.mjs +175 -0
  281. package/kits/hub-game/scripts/test-hold-show.mjs +355 -0
  282. package/kits/hub-game/scripts/test-hub-controls.mjs +3 -1
  283. package/kits/hub-game/scripts/test-hub-idle.mjs +348 -0
  284. package/kits/hub-game/scripts/test-hub-leaving.mjs +21 -0
  285. package/kits/hub-game/scripts/test-hub-ops.mjs +147 -0
  286. package/kits/hub-game/scripts/test-hub-trip.mjs +260 -5
  287. package/kits/hub-game/scripts/test-hud-clear.mjs +83 -0
  288. package/kits/hub-game/scripts/test-hud-rate.mjs +2 -1
  289. package/kits/hub-game/scripts/test-idle.mjs +160 -0
  290. package/kits/hub-game/scripts/test-invites-client.mjs +53 -0
  291. package/kits/hub-game/scripts/test-labels.mjs +11 -0
  292. package/kits/hub-game/scripts/test-landscape-layout.mjs +20 -0
  293. package/kits/hub-game/scripts/test-load-abandon.mjs +2 -2
  294. package/kits/hub-game/scripts/test-loading-cover.mjs +67 -1
  295. package/kits/hub-game/scripts/test-manifest.mjs +27 -0
  296. package/kits/hub-game/scripts/test-menu-client.mjs +55 -6
  297. package/kits/hub-game/scripts/test-menu-surface.mjs +236 -0
  298. package/kits/hub-game/scripts/test-npcs.mjs +128 -0
  299. package/kits/hub-game/scripts/test-observed.mjs +171 -3
  300. package/kits/hub-game/scripts/test-pack.mjs +166 -0
  301. package/kits/hub-game/scripts/test-picker-surface.mjs +321 -0
  302. package/kits/hub-game/scripts/test-placeat-server.mjs +395 -0
  303. package/kits/hub-game/scripts/test-placement-cache.mjs +1 -1
  304. package/kits/hub-game/scripts/test-pointer-clear-dom.mjs +82 -0
  305. package/kits/hub-game/scripts/test-progress-reset.mjs +184 -6
  306. package/kits/hub-game/scripts/test-push-visit.mjs +9 -0
  307. package/kits/hub-game/scripts/test-quality.mjs +312 -9
  308. package/kits/hub-game/scripts/test-quick-chat-surface.mjs +511 -0
  309. package/kits/hub-game/scripts/test-rehello.mjs +237 -0
  310. package/kits/hub-game/scripts/test-rejoin-yaw.mjs +138 -0
  311. package/kits/hub-game/scripts/test-relations-among.mjs +13 -7
  312. package/kits/hub-game/scripts/test-relations-refresh.mjs +90 -2
  313. package/kits/hub-game/scripts/test-replay-client.mjs +110 -0
  314. package/kits/hub-game/scripts/test-replay.mjs +178 -0
  315. package/kits/hub-game/scripts/test-resume-held.mjs +13 -0
  316. package/kits/hub-game/scripts/test-resume-pose-server.mjs +43 -5
  317. package/kits/hub-game/scripts/test-resync.mjs +13 -0
  318. package/kits/hub-game/scripts/test-role-client.mjs +10 -3
  319. package/kits/hub-game/scripts/test-safe-act.mjs +233 -0
  320. package/kits/hub-game/scripts/test-safe-caption.mjs +364 -0
  321. package/kits/hub-game/scripts/test-safe-layout.mjs +79 -0
  322. package/kits/hub-game/scripts/test-safe-prompt.mjs +163 -0
  323. package/kits/hub-game/scripts/test-sound-surface.mjs +256 -0
  324. package/kits/hub-game/scripts/test-soundscape.mjs +85 -2
  325. package/kits/hub-game/scripts/test-stuck-surface.mjs +174 -0
  326. package/kits/hub-game/scripts/test-stuck.mjs +31 -0
  327. package/kits/hub-game/scripts/test-talk-surface.mjs +23 -6
  328. package/kits/hub-game/scripts/test-talk-walk-dom.mjs +123 -0
  329. package/kits/hub-game/scripts/test-text-ask-surface.mjs +273 -0
  330. package/kits/hub-game/scripts/test-tiers.mjs +453 -3
  331. package/kits/hub-game/scripts/test-toast-no-row-dom.mjs +71 -0
  332. package/kits/hub-game/scripts/test-together.mjs +11 -0
  333. package/kits/hub-game/scripts/test-town-day.mjs +90 -0
  334. package/kits/hub-game/scripts/test-track-queue.mjs +64 -0
  335. package/kits/hub-game/scripts/test-try-on.mjs +39 -0
  336. package/kits/hub-game/scripts/test-ui-dom-dom.mjs +49 -2
  337. package/kits/hub-game/scripts/test-ui-tree.mjs +28 -0
  338. package/kits/hub-game/scripts/test-vehicle-client.mjs +77 -1
  339. package/kits/hub-game/scripts/test-vis.mjs +145 -0
  340. package/kits/hub-game/scripts/test-wallet.mjs +650 -0
  341. package/kits/hub-game/scripts/test-weakest-tier.mjs +135 -1
  342. package/kits/hub-game/scripts/test-world-labels.mjs +380 -0
  343. package/kits/hub-game/scripts/test-writer-lease-server.mjs +234 -0
  344. package/kits/hub-game/scripts/test-writer-lease.mjs +281 -0
  345. package/kits/hub-game/scripts/writer-storage-fixture.mjs +37 -0
  346. package/kits/hub-game/tools/hubScene/export.mjs +6 -5
  347. package/kits/hub-game/tools/hubScene/hubdata.mjs +10 -0
  348. package/kits/hub-game/tools/hubScene/test/export-fixture.mjs +48 -2
  349. package/kits/hub-game/tools/hubScene/validate-manifest.mjs +21 -3
  350. package/kits/multiplayer-3d/castle.json +5 -5
  351. package/kits/multiplayer-3d/code/client/resumeGate.js +49 -2
  352. package/kits/multiplayer-3d/code/server/ownership.js +25 -6
  353. package/kits/multiplayer-3d/code/server/world.js +19 -4
  354. package/kits/multiplayer-3d/code/systems/multiplayer.js +67 -11
  355. package/kits/multiplayer-3d/package-lock.json +1 -1
  356. package/kits/multiplayer-3d/package.json +2 -1
  357. package/kits/multiplayer-3d/scripts/test-ground-half.mjs +56 -0
  358. package/kits/multiplayer-3d/scripts/test-join-watchdog.mjs +54 -3
  359. package/kits/multiplayer-3d/scripts/test-owned-moves.mjs +190 -0
  360. package/kits/multiplayer-3d/scripts/test-resume-gate.mjs +191 -5
  361. package/kits/multiplayer-3d/scripts/test-resync-reconnect.mjs +68 -0
  362. package/kits/physics-2d/castle.json +2 -2
  363. package/kits/physics-2d/engine/scene.js +10 -2
  364. package/kits/physics-3d/CLAUDE.md +14 -0
  365. package/kits/physics-3d/behaviors/Player.jsx +39 -3
  366. package/kits/physics-3d/castle.json +3 -3
  367. package/kits/physics-3d/docs/engine-model-cache.md +109 -0
  368. package/kits/physics-3d/docs/engine-player.md +76 -0
  369. package/kits/physics-3d/docs/engine-skip-work.md +16 -0
  370. package/kits/physics-3d/docs/pxmodel-format.md +5 -1
  371. package/kits/physics-3d/engine3d/gait.js +16 -8
  372. package/kits/physics-3d/engine3d/groundsize.js +23 -0
  373. package/kits/physics-3d/engine3d/kithooks.js +50 -0
  374. package/kits/physics-3d/engine3d/look.js +110 -17
  375. package/kits/physics-3d/engine3d/lookbake.js +19 -1
  376. package/kits/physics-3d/engine3d/materialpresets.js +3 -2
  377. package/kits/physics-3d/engine3d/modelcache.js +280 -0
  378. package/kits/physics-3d/engine3d/modelcachedigest.js +37 -0
  379. package/kits/physics-3d/engine3d/modelcachegate.js +69 -0
  380. package/kits/physics-3d/engine3d/modelrefs.js +13 -0
  381. package/kits/physics-3d/engine3d/optimize.js +20 -9
  382. package/kits/physics-3d/engine3d/playerRun.js +26 -1
  383. package/kits/physics-3d/engine3d/pxmodel.js +7 -3
  384. package/kits/physics-3d/engine3d/templatecache.js +722 -0
  385. package/kits/physics-3d/engine3d/world3d.js +211 -17
  386. package/kits/physics-3d/package-lock.json +1 -1
  387. package/kits/physics-3d/package.json +2 -1
  388. package/kits/physics-3d/play/behaviors/Player.js +43 -3
  389. package/kits/physics-3d/scripts/render.mjs +56 -8
  390. package/kits/physics-3d/scripts/template-cache-fixtures.mjs +37 -0
  391. package/kits/physics-3d/scripts/template-cache-v1-bytes.json +49 -0
  392. package/kits/physics-3d/scripts/test-gait.mjs +53 -9
  393. package/kits/physics-3d/scripts/test-ground-half.mjs +120 -0
  394. package/kits/physics-3d/scripts/test-model-cache.mjs +600 -0
  395. package/kits/physics-3d/scripts/test-orbit-shadow-fade.mjs +1 -0
  396. package/kits/physics-3d/scripts/test-sleeping-models.mjs +384 -0
  397. package/kits/physics-3d/scripts/test-template-cache-minified.mjs +106 -0
  398. package/kits/physics-3d/scripts/test-template-cache.mjs +519 -0
  399. package/kits/physics-3d/scripts/test-wall-slide.mjs +272 -0
  400. package/kits/physics-3d/systems/physics3d.js +51 -2
  401. package/kits/real-time/castle.json +2 -2
  402. package/kits/real-time/code/client/connection.js +3 -0
  403. package/package.json +2 -2
  404. package/vendor/three/examples/jsm/libs/fflate.module.js +2672 -0
@@ -205,6 +205,8 @@ export interface MultiplayerConnection {
205
205
  readonly sharedCharacters: boolean;
206
206
  /** Sends byte payloads as binary frames and all other payloads as JSON. */
207
207
  send(data: unknown): void;
208
+ /** Starts recovery for a connected socket; false when there is none. */
209
+ reconnect(): boolean;
208
210
  /** Requests one billed character turn through this authenticated world socket. */
209
211
  requestCharacterTurn(request: MultiplayerCharacterTurnRequest): Promise<MultiplayerCharacterTurnAdmission>;
210
212
  /** Recovers the durable status of a prior operation after reconnecting. */
@@ -1,6 +1,7 @@
1
1
  import { CastleError } from "./errors";
2
2
  import { BINARY_MESSAGE_TAG, isBinaryPayload, MAX_CLIENT_FRAME_BYTES, toUint8Array, } from "./multiplayerProtocol";
3
3
  import { hostRequest } from "./transport";
4
+ import { retryHostTransport } from "./hostRetry";
4
5
  import { MultiplayerVoiceImpl } from "./multiplayerVoice";
5
6
  import { ReconnectBudget } from "./reconnectBudget";
6
7
  import { noteSessionRejoined } from "./hubRejoin";
@@ -8,6 +9,20 @@ const KEEPALIVE_INTERVAL_MS = 25_000;
8
9
  const HELLO_TIMEOUT_MS = 10_000;
9
10
  const RECONNECT_BUDGET_MS = 30_000;
10
11
  const RECONNECT_DELAYS_MS = [500, 1_000, 2_000, 4_000, 8_000];
12
+ // A connected socket hears something at least once a keepalive (the pong, or
13
+ // the tick that sends the ping). Silence well past that means the page was not
14
+ // running: hidden, frozen or suspended. A connection lost across such a gap is
15
+ // found on the page's return, and its first try goes at once instead of after
16
+ // the first backoff step: the backoff is for a failing network, and an outage
17
+ // found on return has not failed a single try yet. A hidden page whose timers
18
+ // and frames kept running is away too: each sign of life notes whether the
19
+ // page was hidden, and the first one seen visible after that is a return. A
20
+ // resumed page can run an overdue keepalive tick or deliver buffered frames
21
+ // before the socket's close (or a pong deadline's fresh probe can outlast
22
+ // them), so a return stands until a socket frame arrives RETURN_GRACE_MS or
23
+ // more after it, which shows the socket carried on past the return.
24
+ const AWAY_SILENCE_MS = KEEPALIVE_INTERVAL_MS + 5_000;
25
+ const RETURN_GRACE_MS = 5_000;
11
26
  const INVALID_DEV_RECONNECT_CLOSE_CODE = 4003;
12
27
  // The play proxy closes a handshake it refuses with a reason (cauldron-game-server
13
28
  // shared/src/constants.ts). The host that owns the session rejecting the credential is
@@ -27,6 +42,18 @@ let nextCharacterStatusRequestId = 1;
27
42
  let activeConnection = null;
28
43
  let latestJoinAttempt = 0;
29
44
  let pendingDestinationJoin = null;
45
+ // Connections waiting for a newer join to settle (`whenJoinOwner`), woken when
46
+ // a destination join settles or a connection closes, to look again.
47
+ let joinSettledWaiters = [];
48
+ function nextJoinSettled() {
49
+ return new Promise((resolve) => joinSettledWaiters.push(resolve));
50
+ }
51
+ function wakeJoinWaiters() {
52
+ const waiters = joinSettledWaiters;
53
+ joinSettledWaiters = [];
54
+ for (const wake of waiters)
55
+ wake();
56
+ }
30
57
  export const Multiplayer = {
31
58
  placeCharacters: true,
32
59
  join,
@@ -52,10 +79,14 @@ async function join(options) {
52
79
  if (!Number.isFinite(pongTimeoutMs) || pongTimeoutMs < 0 || pongTimeoutMs > 2_147_483_647) {
53
80
  throw new RangeError("pongTimeoutMs must be between 0 and 2147483647 milliseconds");
54
81
  }
55
- latestJoinAttempt += 1;
82
+ const attempt = ++latestJoinAttempt;
83
+ // Leaving runs the deck's state listeners, which may begin a newer join:
84
+ // then this one is superseded before it asks for anything.
56
85
  cancelPendingDestinationJoin();
57
86
  activeConnection?.leave();
58
- const connection = new MultiplayerConnectionImpl(request, () => {
87
+ if (attempt !== latestJoinAttempt)
88
+ throw supersededJoinError();
89
+ const connection = new MultiplayerConnectionImpl(request, attempt, () => {
59
90
  if (activeConnection === connection)
60
91
  activeConnection = null;
61
92
  }, null, pongTimeoutMs);
@@ -75,6 +106,9 @@ async function join(options) {
75
106
  // The host answers `unavailable` while ghost is busy with this player; the
76
107
  // trip stays pending there, so asking again is safe.
77
108
  const HUB_COMMIT_RETRY_MS = [1_000, 2_000, 4_000, 8_000];
109
+ // A hub join's tries of the same nonce while the proxy cannot route the new
110
+ // session yet (`openHubSocket`), well inside the nonce's unconsumed life.
111
+ const HUB_UNROUTED_RETRY_MS = [500, 1_000, 2_000];
78
112
  async function commitHubTrip(nonce, sessionId) {
79
113
  for (let attempt = 0;; attempt++) {
80
114
  let status;
@@ -116,8 +150,15 @@ function hubJoin(options) {
116
150
  async function joinDestination(options) {
117
151
  const params = destinationParams(options);
118
152
  const attempt = ++latestJoinAttempt;
153
+ // Cancelling a pending destination join leaves its connection, whose state
154
+ // listeners may begin a newer join: then this one is superseded already.
119
155
  cancelPendingDestinationJoin();
156
+ if (attempt !== latestJoinAttempt)
157
+ return { status: "cancelled" };
120
158
  if (activeConnection?.matchesDestination(params)) {
159
+ // This join is the connection's own join now, for its fence.
160
+ activeConnection.adoptJoinAttempt(attempt);
161
+ wakeJoinWaiters();
121
162
  return {
122
163
  status: "joined",
123
164
  connection: activeConnection,
@@ -125,14 +166,26 @@ async function joinDestination(options) {
125
166
  };
126
167
  }
127
168
  const previousConnection = activeConnection;
169
+ try {
170
+ return await joinNewDestination(params, attempt, previousConnection);
171
+ }
172
+ finally {
173
+ // The newest join ended without replacing the connection it found: that
174
+ // connection is the player's again, and may ask for its session again.
175
+ if (attempt === latestJoinAttempt && previousConnection && activeConnection === previousConnection) {
176
+ previousConnection.adoptJoinAttempt(attempt);
177
+ }
178
+ wakeJoinWaiters();
179
+ }
180
+ }
181
+ async function joinNewDestination(params, attempt, previousConnection) {
128
182
  const controller = new AbortController();
129
183
  const pending = { controller, connection: null };
130
184
  pendingDestinationJoin = pending;
131
185
  let acquired;
132
186
  try {
133
- acquired = await hostRequest("multiplayer.joinDestination", params, {
134
- signal: controller.signal,
135
- });
187
+ // A request lost in transit is asked again until a newer join or a cancel.
188
+ acquired = await retryHostTransport("multiplayer.joinDestination", () => hostRequest("multiplayer.joinDestination", params, { signal: controller.signal }), { signal: controller.signal, stopped: () => attempt !== latestJoinAttempt });
136
189
  }
137
190
  catch (error) {
138
191
  if (pendingDestinationJoin === pending)
@@ -157,7 +210,7 @@ async function joinDestination(options) {
157
210
  pendingDestinationJoin = null;
158
211
  return { status: "unavailable" };
159
212
  }
160
- const connection = new MultiplayerConnectionImpl({ command: "multiplayer.joinDestination", params }, () => {
213
+ const connection = new MultiplayerConnectionImpl({ command: "multiplayer.joinDestination", params }, attempt, () => {
161
214
  if (activeConnection === connection)
162
215
  activeConnection = null;
163
216
  }, acquired.audience);
@@ -180,6 +233,11 @@ async function joinDestination(options) {
180
233
  return { status: "cancelled" };
181
234
  }
182
235
  previousConnection?.leave();
236
+ // The previous connection's state listeners may have begun a newer join.
237
+ if (attempt !== latestJoinAttempt) {
238
+ connection.leave();
239
+ return { status: "cancelled" };
240
+ }
183
241
  activeConnection = connection;
184
242
  return { status: "joined", connection, audience: acquired.audience };
185
243
  }
@@ -187,6 +245,7 @@ async function joinDestination(options) {
187
245
  const EARLY_MESSAGES_MAX = 512;
188
246
  class MultiplayerConnectionImpl {
189
247
  request;
248
+ joinAttempt;
190
249
  onClosed;
191
250
  destinationAudience;
192
251
  pongTimeoutMs;
@@ -209,6 +268,11 @@ class MultiplayerConnectionImpl {
209
268
  greeted = false;
210
269
  recovering = false;
211
270
  reconnectBudget = null;
271
+ // Wall clock, not monotonic: the gap it measures is a suspension, which a
272
+ // paused page's monotonic clock may not count.
273
+ lastHeardAt = 0;
274
+ returnedAt = null;
275
+ heardHidden = false;
212
276
  hubSettled = false;
213
277
  hubCommit = Promise.resolve(true);
214
278
  roster = null;
@@ -232,8 +296,13 @@ class MultiplayerConnectionImpl {
232
296
  players: () => this.roster?.players ?? [],
233
297
  isSolo: () => this.solo,
234
298
  });
235
- constructor(request, onClosed, destinationAudience = null, pongTimeoutMs = 0) {
299
+ constructor(request,
300
+ // The join (Multiplayer.join or joinDestination) this connection belongs
301
+ // to, the attempt it took. While a newer join is pending, this connection
302
+ // asks for no session (`whenJoinOwner`).
303
+ joinAttempt, onClosed, destinationAudience = null, pongTimeoutMs = 0) {
236
304
  this.request = request;
305
+ this.joinAttempt = joinAttempt;
237
306
  this.onClosed = onClosed;
238
307
  this.destinationAudience = destinationAudience;
239
308
  this.pongTimeoutMs = pongTimeoutMs;
@@ -296,6 +365,12 @@ class MultiplayerConnectionImpl {
296
365
  return;
297
366
  this.socket.send(payload);
298
367
  }
368
+ reconnect() {
369
+ if (this.currentState !== "connected" || this.recovering || this.solo || !this.socket)
370
+ return false;
371
+ this.recoverSocket(this.socket, "reconnect");
372
+ return true;
373
+ }
299
374
  requestCharacterTurn(request) {
300
375
  const normalized = normalizeCharacterTurnRequest(request, this.currentPlayerId);
301
376
  return this.sendCharacterRequest(normalized.requestId, normalized.operationId, "admission", JSON.stringify({ t: "worldCharacterTurn", ...normalized }));
@@ -424,6 +499,9 @@ class MultiplayerConnectionImpl {
424
499
  hubCommitted() {
425
500
  return this.hubCommit;
426
501
  }
502
+ adoptJoinAttempt(attempt) {
503
+ this.joinAttempt = attempt;
504
+ }
427
505
  matchesDestination(params) {
428
506
  return (this.currentState !== "closed" &&
429
507
  this.request.command === "multiplayer.joinDestination" &&
@@ -431,9 +509,28 @@ class MultiplayerConnectionImpl {
431
509
  this.request.params.slot === params.slot &&
432
510
  this.destinationAudience !== null);
433
511
  }
512
+ // Sends an acquisition once this connection owns the newest join attempt,
513
+ // or rejects once it is closed. A destination join keeps this connection
514
+ // open until its own socket connects; an acquisition asked meanwhile would
515
+ // supersede the newer one at the host, so it waits for that join to settle:
516
+ // one that replaced this connection closed it, one that did not handed it
517
+ // the attempt. The check and the send run in the same synchronous callback,
518
+ // so no join can begin between them; after any wake the gate is entered again.
519
+ whenJoinOwner(send) {
520
+ if (this.currentState === "closed")
521
+ return Promise.reject(this.closedError());
522
+ if (this.joinAttempt === latestJoinAttempt)
523
+ return send();
524
+ return nextJoinSettled().then(() => this.whenJoinOwner(send));
525
+ }
526
+ closedError() {
527
+ return multiplayerError("MULTIPLAYER_CONNECTION_CLOSED", "The multiplayer connection is closed.");
528
+ }
434
529
  async acquire(initial) {
435
530
  this.transition("acquiring");
436
- const result = initial ?? await requestAcquisition(this.request);
531
+ // Every ask, the first and each retry, waits until this connection owns
532
+ // the newest join (`whenJoinOwner`); a retry wait ends once it closes.
533
+ const result = initial ?? await requestAcquisition(this.request, (send) => this.whenJoinOwner(send), () => this.currentState === "closed");
437
534
  this.assertOpen();
438
535
  if (result.status === "solo") {
439
536
  this.connectSolo(result);
@@ -458,8 +555,35 @@ class MultiplayerConnectionImpl {
458
555
  if (savedToken && await this.restoreDevReconnect(savedToken))
459
556
  return;
460
557
  this.assertOpen();
558
+ if (this.request.command === "hub") {
559
+ await this.openHubSocket(result.nonce);
560
+ return;
561
+ }
461
562
  await this.openSocket("nonce", result.nonce);
462
563
  }
564
+ // A hub session ghost has just started can be refused by the play proxy
565
+ // before its route is readable there (4404, before any hello). The proxy
566
+ // refuses before the session sees the nonce, so the nonce is still good:
567
+ // try it again for a few seconds before the trip is given back.
568
+ async openHubSocket(nonce) {
569
+ for (let attempt = 0;; attempt++) {
570
+ try {
571
+ await this.openSocket("nonce", nonce);
572
+ return;
573
+ }
574
+ catch (error) {
575
+ const delay = HUB_UNROUTED_RETRY_MS[attempt];
576
+ if (!(error instanceof SocketClosedError) ||
577
+ error.code !== PROXY_SESSION_ENDED_CLOSE_CODE ||
578
+ delay === undefined) {
579
+ throw error;
580
+ }
581
+ this.assertOpen();
582
+ await pause(delay);
583
+ this.assertOpen();
584
+ }
585
+ }
586
+ }
463
587
  async restoreDevReconnect(token) {
464
588
  this.assertOpen();
465
589
  const budget = this.reconnectBudget = new ReconnectBudget();
@@ -528,6 +652,8 @@ class MultiplayerConnectionImpl {
528
652
  closeSocket(socket, 1000, "hello timeout");
529
653
  }, HELLO_TIMEOUT_MS);
530
654
  socket.onmessage = (event) => {
655
+ if (this.socketAttempt === attempt)
656
+ this.hear(true);
531
657
  const data = event.data;
532
658
  inbound = inbound
533
659
  .then(async () => {
@@ -674,6 +800,11 @@ class MultiplayerConnectionImpl {
674
800
  const budget = this.reconnectBudget = new ReconnectBudget();
675
801
  let attempt = 0;
676
802
  let sessionEndedCloses = 0;
803
+ // Lost while the page was away (AWAY_SILENCE_MS), or found while it still
804
+ // is: the first try goes as soon as the page is back. It is still a try
805
+ // with the token, which is the only way to learn whether the host kept the
806
+ // player; the backoff resumes from its first step after it.
807
+ let immediate = !budget.active || this.heardHidden || this.cameBack();
677
808
  try {
678
809
  while (true) {
679
810
  await budget.waitUntilActive();
@@ -682,11 +813,16 @@ class MultiplayerConnectionImpl {
682
813
  // which keeps the player's identity while the host holds its lineage.
683
814
  if (budget.exhausted(RECONNECT_BUDGET_MS))
684
815
  break;
685
- const delay = RECONNECT_DELAYS_MS[Math.min(attempt, RECONNECT_DELAYS_MS.length - 1)];
686
- attempt += 1;
687
- await pause(delay);
688
- await budget.waitUntilActive();
689
- this.assertOpen();
816
+ if (immediate) {
817
+ immediate = false;
818
+ }
819
+ else {
820
+ const delay = RECONNECT_DELAYS_MS[Math.min(attempt, RECONNECT_DELAYS_MS.length - 1)];
821
+ attempt += 1;
822
+ await pause(delay);
823
+ await budget.waitUntilActive();
824
+ this.assertOpen();
825
+ }
690
826
  try {
691
827
  budget.noteAttempt();
692
828
  await this.openSocket("reconnect", this.reconnectToken);
@@ -714,6 +850,23 @@ class MultiplayerConnectionImpl {
714
850
  }
715
851
  await this.reacquireOnce();
716
852
  }
853
+ // A sign the page is running: a frame from the socket or a keepalive tick.
854
+ hear(fromSocket = false) {
855
+ const now = Date.now();
856
+ const hidden = typeof document !== "undefined" && document.visibilityState === "hidden";
857
+ if (now - this.lastHeardAt > AWAY_SILENCE_MS || (this.heardHidden && !hidden)) {
858
+ this.returnedAt = now;
859
+ }
860
+ else if (fromSocket && this.returnedAt !== null && now - this.returnedAt >= RETURN_GRACE_MS) {
861
+ this.returnedAt = null;
862
+ }
863
+ this.heardHidden = hidden;
864
+ this.lastHeardAt = now;
865
+ }
866
+ cameBack() {
867
+ const now = Date.now();
868
+ return now - this.lastHeardAt > AWAY_SILENCE_MS || this.returnedAt !== null;
869
+ }
717
870
  async reacquireOnce() {
718
871
  if (this.reAcquisitionUsed) {
719
872
  throw this.closeAfterRecovery();
@@ -736,7 +889,11 @@ class MultiplayerConnectionImpl {
736
889
  startKeepalive() {
737
890
  this.stopKeepalive();
738
891
  this.pongExtensionUsed = false;
892
+ this.lastHeardAt = Date.now();
893
+ this.returnedAt = null;
894
+ this.heardHidden = false;
739
895
  this.keepalive = setInterval(() => {
896
+ this.hear();
740
897
  if (this.socket?.readyState === WebSocket.OPEN) {
741
898
  if (this.pongDeadline !== null)
742
899
  return;
@@ -782,15 +939,18 @@ class MultiplayerConnectionImpl {
782
939
  }
783
940
  catch { /* Fall through to recovery if the fresh probe cannot send. */ }
784
941
  }
785
- // A half-open transport may never emit close. Fence its queued callbacks
786
- // and enter recovery directly rather than waiting for that close event.
787
- this.socketAttempt += 1;
788
- this.socket = null;
789
- this.stopKeepalive();
790
- closeSocket(socket, 1000, "pong timeout");
791
- this.beginRecovery();
942
+ this.recoverSocket(socket, "pong timeout");
792
943
  }, this.pongTimeoutMs);
793
944
  }
945
+ recoverSocket(socket, reason) {
946
+ // A half-open transport may never emit close. Fence its queued callbacks
947
+ // and enter recovery directly rather than waiting for that close event.
948
+ this.socketAttempt += 1;
949
+ this.socket = null;
950
+ this.stopKeepalive();
951
+ closeSocket(socket, 1000, reason);
952
+ this.beginRecovery();
953
+ }
794
954
  transition(state, metadata) {
795
955
  if (this.currentState === "closed" || this.currentState === state)
796
956
  return;
@@ -820,11 +980,11 @@ class MultiplayerConnectionImpl {
820
980
  for (const callback of this.states)
821
981
  callStateListener(callback, "closed", metadata);
822
982
  this.onClosed();
983
+ wakeJoinWaiters();
823
984
  }
824
985
  assertOpen() {
825
- if (this.currentState === "closed") {
826
- throw multiplayerError("MULTIPLAYER_CONNECTION_CLOSED", "The multiplayer connection is closed.");
827
- }
986
+ if (this.currentState === "closed")
987
+ throw this.closedError();
828
988
  }
829
989
  acceptWorldCharacterResult(frame) {
830
990
  const pending = this.pendingCharacterRequests.get(frame.requestId);
@@ -925,22 +1085,34 @@ function destinationParams(options) {
925
1085
  }
926
1086
  return { routeId: options.routeId, slot: options.slot };
927
1087
  }
1088
+ // What a join superseded by a newer one before it had a connection rejects
1089
+ // with: the same code as one whose connection the newer join closed.
1090
+ function supersededJoinError() {
1091
+ return multiplayerError("MULTIPLAYER_CONNECTION_CLOSED", "A newer join began; this one was superseded.", "Multiplayer.join");
1092
+ }
928
1093
  function cancelPendingDestinationJoin() {
929
1094
  const pending = pendingDestinationJoin;
930
1095
  pendingDestinationJoin = null;
931
1096
  pending?.controller.abort();
932
1097
  pending?.connection?.leave();
933
1098
  }
934
- async function requestAcquisition(request) {
1099
+ async function requestAcquisition(request,
1100
+ // Sends one ask, when the connection may (`whenJoinOwner`).
1101
+ gate, closed) {
935
1102
  if (request.command === "hub") {
936
1103
  // The nonce was single-use and the trip is the host's; a hub session that
937
1104
  // cannot be reconnected to is left by going back through the door.
938
1105
  throw multiplayerError("MULTIPLAYER_CONNECTION_LOST", "A hub session cannot be acquired again. Go back through the door.");
939
1106
  }
1107
+ // A request lost in transit is asked again until the connection closes; the
1108
+ // wait under way runs out first. Each ask goes through `gate`.
1109
+ const ask = (send) => () => gate(send);
940
1110
  if (request.command === "multiplayer.getSession") {
941
- return hostRequest(request.command, request.params);
1111
+ const params = request.params;
1112
+ return retryHostTransport(request.command, ask(() => hostRequest("multiplayer.getSession", params)), { stopped: closed });
942
1113
  }
943
- return hostRequest(request.command, request.params);
1114
+ const params = request.params;
1115
+ return retryHostTransport(request.command, ask(() => hostRequest("multiplayer.joinDestination", params)), { stopped: closed });
944
1116
  }
945
1117
  function destinationStatusFromError(error) {
946
1118
  if (!(error instanceof CastleError))
@@ -5,10 +5,14 @@ export declare class ReconnectBudget {
5
5
  private wokeSinceAttempt;
6
6
  private gapGraceUsed;
7
7
  private frozen;
8
+ private offline;
8
9
  private disposed;
9
10
  private readonly waiters;
10
11
  private readonly page;
12
+ private readonly host;
11
13
  constructor();
14
+ /** Whether the page is running now: visible, not frozen, not disposed. */
15
+ get active(): boolean;
12
16
  get elapsedMs(): number;
13
17
  noteAttempt(): void;
14
18
  exhausted(limitMs: number): boolean;
@@ -16,6 +20,8 @@ export declare class ReconnectBudget {
16
20
  dispose(): void;
17
21
  private readonly freeze;
18
22
  private readonly resume;
23
+ private readonly goOffline;
24
+ private readonly goOnline;
19
25
  private readonly update;
20
26
  private readonly observe;
21
27
  private stop;
@@ -3,6 +3,15 @@
3
3
  // Not every suspension is announced (a debugger pause, a native app pausing
4
4
  // its web view while the document stays visible), so active time accrues in
5
5
  // heartbeat steps and a gap longer than one step counts only that step.
6
+ //
7
+ // Time the browser reports offline does not accrue either: a network drop
8
+ // with the page visible would otherwise spend the whole budget on attempts
9
+ // that fail instantly, and the player rejoins fresh while the host still
10
+ // holds their identity. Offline only pauses the clock; attempts continue at
11
+ // the normal backoff, because navigator.onLine can be wrong (some WebViews
12
+ // and captive networks report false while sockets work) and a wrong flag must
13
+ // never stop a reconnect. If the player stays away past the host's hold, the
14
+ // host refuses the token and recovery ends on that, not on this budget.
6
15
  const HEARTBEAT_MS = 1_000;
7
16
  const MAX_STEP_MS = 2_000;
8
17
  export class ReconnectBudget {
@@ -15,15 +24,30 @@ export class ReconnectBudget {
15
24
  // frames for minutes would otherwise renew it forever.
16
25
  gapGraceUsed = false;
17
26
  frozen = false;
27
+ offline = false;
18
28
  disposed = false;
19
29
  waiters = new Set();
20
30
  page = typeof document === "undefined" ? null : document;
31
+ // Where online/offline arrive: the window, or a worker's own global. With
32
+ // neither, a change of navigator.onLine cannot be seen, so it is not trusted.
33
+ host = typeof window !== "undefined"
34
+ ? window
35
+ : typeof globalThis.addEventListener === "function"
36
+ ? globalThis
37
+ : null;
21
38
  constructor() {
22
39
  this.page?.addEventListener("visibilitychange", this.update);
23
40
  this.page?.addEventListener("freeze", this.freeze);
24
41
  this.page?.addEventListener("resume", this.resume);
42
+ this.host?.addEventListener?.("offline", this.goOffline);
43
+ this.host?.addEventListener?.("online", this.goOnline);
44
+ this.offline = this.host !== null && typeof navigator !== "undefined" && navigator?.onLine === false;
25
45
  this.update();
26
46
  }
47
+ /** Whether the page is running now: visible, not frozen, not disposed. */
48
+ get active() {
49
+ return this.lastSeen !== null;
50
+ }
27
51
  get elapsedMs() {
28
52
  this.observe();
29
53
  return this.elapsed;
@@ -47,6 +71,8 @@ export class ReconnectBudget {
47
71
  this.page?.removeEventListener("visibilitychange", this.update);
48
72
  this.page?.removeEventListener("freeze", this.freeze);
49
73
  this.page?.removeEventListener("resume", this.resume);
74
+ this.host?.removeEventListener?.("offline", this.goOffline);
75
+ this.host?.removeEventListener?.("online", this.goOnline);
50
76
  this.wakeWaiters();
51
77
  }
52
78
  freeze = () => {
@@ -57,6 +83,19 @@ export class ReconnectBudget {
57
83
  this.frozen = false;
58
84
  this.update();
59
85
  };
86
+ // Each edge settles the time before it at the old state.
87
+ goOffline = () => {
88
+ this.observe();
89
+ this.offline = true;
90
+ };
91
+ // Coming back online is a return like the foreground: one try with the token.
92
+ goOnline = () => {
93
+ if (!this.offline)
94
+ return;
95
+ this.observe();
96
+ this.offline = false;
97
+ this.wokeSinceAttempt = true;
98
+ };
60
99
  update = () => {
61
100
  const active = !this.disposed && !this.frozen && this.page?.visibilityState !== "hidden";
62
101
  if (active && this.lastSeen === null) {
@@ -78,7 +117,8 @@ export class ReconnectBudget {
78
117
  this.gapGraceUsed = true;
79
118
  this.wokeSinceAttempt = true;
80
119
  }
81
- this.elapsed += Math.min(Math.max(gap, 0), MAX_STEP_MS);
120
+ if (!this.offline)
121
+ this.elapsed += Math.min(Math.max(gap, 0), MAX_STEP_MS);
82
122
  this.lastSeen = now;
83
123
  };
84
124
  stop() {
@@ -0,0 +1,11 @@
1
+ import type { RoomsListResult } from "./commands";
2
+ export type { RoomSummary, RoomsListResult } from "./commands";
3
+ export interface CastleRoomsApi {
4
+ /**
5
+ * This deck's public rooms with their player counts, or null when Castle
6
+ * will not list them for this player or the app running the deck predates
7
+ * the call. Null is "not available", never "no rooms".
8
+ */
9
+ list(): Promise<RoomsListResult | null>;
10
+ }
11
+ export declare const Rooms: CastleRoomsApi;
@@ -0,0 +1,22 @@
1
+ // Rooms — the current deck's public multiplayer rooms and how many players are
2
+ // in each, for a lobby or a "players online" line. Castle always lists them
3
+ // to the deck's owner and Castle admins, to other players only as far as its
4
+ // presence rollout reaches, and never to a player under 13 or kept out of
5
+ // multiplayer; anyone it will not list them for gets null.
6
+ //
7
+ // Counts come from a rollup Castle refreshes about every 10 seconds, so a
8
+ // player who just joined or left may not show yet. Private, party and named
9
+ // rooms are not listed.
10
+ import { CastleError } from "./errors";
11
+ import { hostRequest } from "./transport";
12
+ async function list() {
13
+ try {
14
+ return await hostRequest("rooms.list", {});
15
+ }
16
+ catch (error) {
17
+ if (error instanceof CastleError && error.code === "UNKNOWN_COMMAND")
18
+ return null;
19
+ throw error;
20
+ }
21
+ }
22
+ export const Rooms = Object.freeze({ list });
@@ -1,5 +1,6 @@
1
1
  import { CASTLE_SDK_PROTOCOL, } from "./commands";
2
2
  import { getCastleEmbed, isEdit } from "./context";
3
+ import { captureErrors, formatConsoleArgs, noteConsoleHooked, recordConsoleLine } from "./debugConsole";
3
4
  import { cardUnit, fitStage, getDisplay } from "./display";
4
5
  import { appliedNotice, applyTunedParams, } from "./paramsRegistry";
5
6
  export const CARD_RATIO = 5 / 7;
@@ -27,6 +28,8 @@ const origError = console.error;
27
28
  const dynamicImport = new Function("u", "return import(u)");
28
29
  export function setup() {
29
30
  interceptConsole();
31
+ captureErrors();
32
+ initHistoryShim();
30
33
  connectLocal();
31
34
  initLocalWake();
32
35
  initHostCapture();
@@ -429,35 +432,49 @@ function resolveLocalRequest(msg) {
429
432
  return true;
430
433
  }
431
434
  function interceptConsole() {
435
+ noteConsoleHooked();
432
436
  console.log = (...args) => {
433
437
  origLog(...args);
434
- sendMsg({ type: "log", level: "log", msg: formatConsoleArgs(args) });
438
+ forwardConsoleLine("log", args);
435
439
  };
436
440
  console.warn = (...args) => {
437
441
  origWarn(...args);
438
- sendMsg({ type: "log", level: "warn", msg: formatConsoleArgs(args) });
442
+ forwardConsoleLine("warn", args);
439
443
  };
440
444
  console.error = (...args) => {
441
445
  origError(...args);
442
- sendMsg({ type: "log", level: "error", msg: formatConsoleArgs(args) });
446
+ forwardConsoleLine("error", args);
443
447
  };
444
448
  }
445
- function formatConsoleArgs(args) {
446
- return args
447
- .map((arg) => {
448
- if (typeof arg === "string")
449
- return arg;
450
- if (arg instanceof Error)
451
- return arg.stack || arg.message;
452
- try {
453
- const json = JSON.stringify(arg);
454
- return json ?? String(arg);
455
- }
456
- catch {
457
- return String(arg);
458
- }
459
- })
460
- .join(" ");
449
+ function forwardConsoleLine(level, args) {
450
+ const msg = formatConsoleArgs(args);
451
+ recordConsoleLine(level, msg);
452
+ sendMsg({ type: "log", level, msg });
453
+ }
454
+ // A published deck's frame has an opaque origin (about:srcdoc on the web), so
455
+ // pushState/replaceState throw a SecurityError the moment they are given a URL.
456
+ // Keep the state and drop the URL: the history entry still lands, and routers
457
+ // that read state rather than the address keep working. Calls without a URL
458
+ // never threw and pass straight through. Only an opaque origin gets this: with
459
+ // an origin of its own a URL that throws is a real error (a cross-origin URL a
460
+ // router may catch to navigate instead), and is left to throw.
461
+ function initHistoryShim() {
462
+ if (typeof history === "undefined" || location.origin !== "null")
463
+ return;
464
+ for (const name of ["pushState", "replaceState"]) {
465
+ const original = history[name];
466
+ history[name] = function (data, unused, url) {
467
+ try {
468
+ original.call(this, data, unused, url);
469
+ }
470
+ catch (error) {
471
+ const security = error instanceof DOMException && error.name === "SecurityError";
472
+ if (!security || url === undefined || url === null)
473
+ throw error;
474
+ original.call(this, data, unused);
475
+ }
476
+ };
477
+ }
461
478
  }
462
479
  // Compositing a card needs html2canvas, and where it comes from decides whether
463
480
  // a capture can fail for reasons that have nothing to do with the deck.