castle-web-cli 0.4.229 → 0.4.231

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (259) hide show
  1. package/dist/agent-prompts.d.ts +2 -0
  2. package/dist/agent-prompts.js +16 -8
  3. package/dist/agent.d.ts +2 -0
  4. package/dist/agent.js +21 -21
  5. package/dist/api.d.ts +28 -2
  6. package/dist/api.js +74 -16
  7. package/dist/castle-host/host.d.ts +91 -0
  8. package/dist/castle-host/host.js +860 -3
  9. package/dist/castleJson.d.ts +44 -0
  10. package/dist/castleJson.js +131 -0
  11. package/dist/check.js +27 -1
  12. package/dist/contentPack.d.ts +49 -0
  13. package/dist/contentPack.js +273 -0
  14. package/dist/devExperiments.d.ts +21 -0
  15. package/dist/devExperiments.js +139 -0
  16. package/dist/devHub.d.ts +55 -0
  17. package/dist/devHub.js +187 -0
  18. package/dist/devSaveStorage.d.ts +36 -0
  19. package/dist/devSaveStorage.js +469 -0
  20. package/dist/devSessionChild.js +25 -13
  21. package/dist/devSessionServer.d.ts +14 -1
  22. package/dist/devSessionServer.js +237 -46
  23. package/dist/devSessionStorage.d.ts +9 -0
  24. package/dist/devSessionStorage.js +66 -1
  25. package/dist/experimentAssignment.d.ts +15 -0
  26. package/dist/experimentAssignment.js +41 -0
  27. package/dist/importCheckHook.d.ts +40 -0
  28. package/dist/importCheckHook.js +171 -0
  29. package/dist/importRegression.d.ts +15 -0
  30. package/dist/importRegression.js +100 -0
  31. package/dist/importTrust.d.ts +2 -0
  32. package/dist/importTrust.js +27 -0
  33. package/dist/importUpdateHook.d.ts +18 -0
  34. package/dist/importUpdateHook.js +31 -8
  35. package/dist/importUpdateHookRunner.js +19 -7
  36. package/dist/imports.d.ts +16 -0
  37. package/dist/imports.js +141 -31
  38. package/dist/index.js +3 -1
  39. package/dist/init.d.ts +3 -0
  40. package/dist/init.js +126 -9
  41. package/dist/install.js +4 -1
  42. package/dist/localPaths.js +4 -0
  43. package/dist/login.js +2 -2
  44. package/dist/platformDoc.js +3 -8
  45. package/dist/playPagePolicy.js +3 -1
  46. package/dist/remix.js +5 -0
  47. package/dist/routerTaskReport.d.ts +28 -0
  48. package/dist/routerTaskReport.js +56 -0
  49. package/dist/save-deck.d.ts +18 -0
  50. package/dist/save-deck.js +142 -6
  51. package/dist/serve.d.ts +8 -0
  52. package/dist/serve.js +99 -14
  53. package/dist/serverBundle.d.ts +1 -1
  54. package/dist/serverBundle.js +5 -1
  55. package/dist/webSourceSync.d.ts +2 -0
  56. package/dist/webSourceSync.js +2 -0
  57. package/kits/base/CLAUDE.md +2 -3
  58. package/kits/base/castle.json +1 -1
  59. package/kits/base/sdk/README.md +406 -53
  60. package/kits/base/sdk/agent.js +4 -4
  61. package/kits/base/sdk/avatar.d.ts +12 -0
  62. package/kits/base/sdk/avatar.js +23 -0
  63. package/kits/base/sdk/castle.d.ts +9 -0
  64. package/kits/base/sdk/castle.js +5 -0
  65. package/kits/base/sdk/commands.d.ts +223 -0
  66. package/kits/base/sdk/commands.js +16 -0
  67. package/kits/base/sdk/context.d.ts +6 -0
  68. package/kits/base/sdk/experiments.d.ts +15 -0
  69. package/kits/base/sdk/experiments.js +83 -0
  70. package/kits/base/sdk/hub.d.ts +85 -0
  71. package/kits/base/sdk/hub.js +107 -0
  72. package/kits/base/sdk/multiplayer.d.ts +45 -1
  73. package/kits/base/sdk/multiplayer.js +158 -8
  74. package/kits/base/sdk/save.d.ts +197 -0
  75. package/kits/base/sdk/save.js +525 -0
  76. package/kits/base/sdk/saveCoordinator.d.ts +39 -0
  77. package/kits/base/sdk/saveCoordinator.js +252 -0
  78. package/kits/base/sdk/server/hub.d.ts +169 -0
  79. package/kits/base/sdk/server/hub.js +125 -0
  80. package/kits/base/sdk/server/platformHandle.d.ts +42 -1
  81. package/kits/base/sdk/server/storage.d.ts +58 -4
  82. package/kits/base/sdk/server/storage.js +25 -5
  83. package/kits/base/sdk/server/wrapper.d.ts +36 -2
  84. package/kits/base/sdk/server/wrapper.js +92 -11
  85. package/kits/base/sdk/store.d.ts +3 -2
  86. package/kits/base/sdk/store.js +5 -0
  87. package/kits/base/sdk/transport.d.ts +2 -1
  88. package/kits/base/sdk/transport.js +13 -1
  89. package/kits/hub-game/.prettierrc +5 -0
  90. package/kits/hub-game/CLAUDE.md +149 -0
  91. package/kits/hub-game/blueprints/avatar-0.scene +28 -0
  92. package/kits/hub-game/blueprints/avatar-1.scene +28 -0
  93. package/kits/hub-game/blueprints/avatar-2.scene +28 -0
  94. package/kits/hub-game/blueprints/avatar-3.scene +28 -0
  95. package/kits/hub-game/blueprints/bean/avatar-0.scene +28 -0
  96. package/kits/hub-game/blueprints/bean/avatar-1.scene +28 -0
  97. package/kits/hub-game/blueprints/bean/avatar-2.scene +28 -0
  98. package/kits/hub-game/blueprints/bean/avatar-3.scene +28 -0
  99. package/kits/hub-game/blueprints/blocky/avatar-0.scene +28 -0
  100. package/kits/hub-game/blueprints/blocky/avatar-1.scene +28 -0
  101. package/kits/hub-game/blueprints/blocky/avatar-2.scene +28 -0
  102. package/kits/hub-game/blueprints/blocky/avatar-3.scene +28 -0
  103. package/kits/hub-game/blueprints/pixel/avatar-0.scene +28 -0
  104. package/kits/hub-game/blueprints/pixel/avatar-1.scene +28 -0
  105. package/kits/hub-game/blueprints/pixel/avatar-2.scene +28 -0
  106. package/kits/hub-game/blueprints/pixel/avatar-3.scene +28 -0
  107. package/kits/hub-game/castle.json +96 -0
  108. package/kits/hub-game/code/client/attention.js +133 -0
  109. package/kits/hub-game/code/client/avatar.js +288 -0
  110. package/kits/hub-game/code/client/cabinets.js +90 -0
  111. package/kits/hub-game/code/client/chat.js +135 -0
  112. package/kits/hub-game/code/client/chrome.js +1035 -0
  113. package/kits/hub-game/code/client/doorFrame.js +15 -0
  114. package/kits/hub-game/code/client/firstJoin.js +14 -0
  115. package/kits/hub-game/code/client/grab.js +376 -0
  116. package/kits/hub-game/code/client/hookLayer.js +236 -0
  117. package/kits/hub-game/code/client/idle.js +326 -0
  118. package/kits/hub-game/code/client/keys.js +48 -0
  119. package/kits/hub-game/code/client/pack.js +232 -0
  120. package/kits/hub-game/code/client/packFormat.js +397 -0
  121. package/kits/hub-game/code/client/say.js +127 -0
  122. package/kits/hub-game/code/client/sha256.js +80 -0
  123. package/kits/hub-game/code/client/sound.js +147 -0
  124. package/kits/hub-game/code/client/state.js +143 -0
  125. package/kits/hub-game/code/client/talk.js +233 -0
  126. package/kits/hub-game/code/client/tiers.js +480 -0
  127. package/kits/hub-game/code/client/together.js +440 -0
  128. package/kits/hub-game/code/client/wear.js +149 -0
  129. package/kits/hub-game/code/server/actors.js +627 -0
  130. package/kits/hub-game/code/server/blocks.js +103 -0
  131. package/kits/hub-game/code/server/cabinets.js +309 -0
  132. package/kits/hub-game/code/server/game.js +137 -0
  133. package/kits/hub-game/code/server/grab.js +248 -0
  134. package/kits/hub-game/code/server/history.js +96 -0
  135. package/kits/hub-game/code/server/hub.js +1998 -0
  136. package/kits/hub-game/code/server/idle.js +412 -0
  137. package/kits/hub-game/code/server/index.js +64 -0
  138. package/kits/hub-game/code/server/labels.js +59 -0
  139. package/kits/hub-game/code/server/manifest.js +554 -0
  140. package/kits/hub-game/code/server/npcs.js +733 -0
  141. package/kits/hub-game/code/server/party.js +439 -0
  142. package/kits/hub-game/code/server/place.js +43 -0
  143. package/kits/hub-game/code/server/say.js +83 -0
  144. package/kits/hub-game/code/server/wear.js +142 -0
  145. package/kits/hub-game/code/systems/hub.js +2347 -0
  146. package/kits/hub-game/docs/content-pack.md +151 -0
  147. package/kits/hub-game/docs/contract.md +1209 -0
  148. package/kits/hub-game/docs/style.md +137 -0
  149. package/kits/hub-game/drawings/blocky/castle-eye.sprite +58 -0
  150. package/kits/hub-game/drawings/blocky/dash.sprite +59 -0
  151. package/kits/hub-game/drawings/blocky/pom.sprite +59 -0
  152. package/kits/hub-game/drawings/blocky/tabby.sprite +59 -0
  153. package/kits/hub-game/drawings/pixel/castle-arm.sprite +13 -0
  154. package/kits/hub-game/drawings/pixel/castle-body.sprite +23 -0
  155. package/kits/hub-game/drawings/pixel/castle-leg.sprite +12 -0
  156. package/kits/hub-game/drawings/pixel/kai-arm.sprite +14 -0
  157. package/kits/hub-game/drawings/pixel/kai-head.sprite +24 -0
  158. package/kits/hub-game/drawings/pixel/kai-leg.sprite +15 -0
  159. package/kits/hub-game/drawings/pixel/kai-torso.sprite +18 -0
  160. package/kits/hub-game/drawings/pixel/mimi-arm.sprite +13 -0
  161. package/kits/hub-game/drawings/pixel/mimi-head.sprite +23 -0
  162. package/kits/hub-game/drawings/pixel/mimi-leg.sprite +15 -0
  163. package/kits/hub-game/drawings/pixel/mimi-torso.sprite +18 -0
  164. package/kits/hub-game/drawings/pixel/nox-arm.sprite +13 -0
  165. package/kits/hub-game/drawings/pixel/nox-head.sprite +21 -0
  166. package/kits/hub-game/drawings/pixel/nox-leg.sprite +14 -0
  167. package/kits/hub-game/drawings/pixel/nox-torso.sprite +18 -0
  168. package/kits/hub-game/eslint.config.js +20 -0
  169. package/kits/hub-game/fonts.generated.js +9 -0
  170. package/kits/hub-game/index.html +22 -0
  171. package/kits/hub-game/main.jsx +6 -0
  172. package/kits/hub-game/models/avatar-0.pxmodel +21 -0
  173. package/kits/hub-game/models/avatar-1.pxmodel +24 -0
  174. package/kits/hub-game/models/avatar-2.pxmodel +27 -0
  175. package/kits/hub-game/models/avatar-3.pxmodel +22 -0
  176. package/kits/hub-game/models/bean/avatar-0.pxmodel +19 -0
  177. package/kits/hub-game/models/bean/avatar-1.pxmodel +24 -0
  178. package/kits/hub-game/models/bean/avatar-2.pxmodel +22 -0
  179. package/kits/hub-game/models/bean/avatar-3.pxmodel +24 -0
  180. package/kits/hub-game/models/blocky/avatar-0.pxmodel +21 -0
  181. package/kits/hub-game/models/blocky/avatar-1.pxmodel +30 -0
  182. package/kits/hub-game/models/blocky/avatar-2.pxmodel +28 -0
  183. package/kits/hub-game/models/blocky/avatar-3.pxmodel +27 -0
  184. package/kits/hub-game/models/pixel/avatar-0.pxmodel +24 -0
  185. package/kits/hub-game/models/pixel/avatar-1.pxmodel +16 -0
  186. package/kits/hub-game/models/pixel/avatar-2.pxmodel +19 -0
  187. package/kits/hub-game/models/pixel/avatar-3.pxmodel +23 -0
  188. package/kits/hub-game/package-lock.json +365 -0
  189. package/kits/hub-game/package.json +32 -0
  190. package/kits/hub-game/scenes/main.scene +318 -0
  191. package/kits/hub-game/scripts/check.mjs +339 -0
  192. package/kits/hub-game/scripts/fixtures/sdk-stub.mjs +3 -0
  193. package/kits/hub-game/scripts/gen-avatars-bean.mjs +748 -0
  194. package/kits/hub-game/scripts/gen-avatars-blocky.mjs +607 -0
  195. package/kits/hub-game/scripts/gen-avatars-pixel.mjs +771 -0
  196. package/kits/hub-game/scripts/gen-avatars.mjs +621 -0
  197. package/kits/hub-game/scripts/pack.mjs +192 -0
  198. package/kits/hub-game/scripts/test-actors.mjs +666 -0
  199. package/kits/hub-game/scripts/test-attention.mjs +103 -0
  200. package/kits/hub-game/scripts/test-avatar.mjs +472 -0
  201. package/kits/hub-game/scripts/test-ban-join.mjs +194 -0
  202. package/kits/hub-game/scripts/test-blocks.mjs +351 -0
  203. package/kits/hub-game/scripts/test-chat.mjs +98 -0
  204. package/kits/hub-game/scripts/test-classic.mjs +590 -0
  205. package/kits/hub-game/scripts/test-door-jump.mjs +131 -0
  206. package/kits/hub-game/scripts/test-door.mjs +56 -0
  207. package/kits/hub-game/scripts/test-grab-client.mjs +328 -0
  208. package/kits/hub-game/scripts/test-grab.mjs +331 -0
  209. package/kits/hub-game/scripts/test-hub-ops.mjs +835 -0
  210. package/kits/hub-game/scripts/test-hub-trip.mjs +1518 -0
  211. package/kits/hub-game/scripts/test-idle.mjs +518 -0
  212. package/kits/hub-game/scripts/test-interest.mjs +413 -0
  213. package/kits/hub-game/scripts/test-keys.mjs +50 -0
  214. package/kits/hub-game/scripts/test-manifest.mjs +270 -0
  215. package/kits/hub-game/scripts/test-no-forms.mjs +16 -0
  216. package/kits/hub-game/scripts/test-npcs.mjs +724 -0
  217. package/kits/hub-game/scripts/test-pack.mjs +424 -0
  218. package/kits/hub-game/scripts/test-party.mjs +369 -0
  219. package/kits/hub-game/scripts/test-say.mjs +149 -0
  220. package/kits/hub-game/scripts/test-tiers.mjs +666 -0
  221. package/kits/hub-game/scripts/test-together.mjs +215 -0
  222. package/kits/hub-game/theme.style +3 -0
  223. package/kits/multiplayer-3d/castle.json +5 -5
  224. package/kits/multiplayer-3d/code/client/avatars.js +101 -1
  225. package/kits/multiplayer-3d/code/server/players.js +4 -1
  226. package/kits/multiplayer-3d/code/server/sceneBodies.js +27 -6
  227. package/kits/multiplayer-3d/code/server/world.js +20 -7
  228. package/kits/multiplayer-3d/code/systems/multiplayer.js +222 -11
  229. package/kits/physics-2d/CLAUDE.md +2 -0
  230. package/kits/physics-2d/castle.json +2 -2
  231. package/kits/physics-2d/engine/art.js +13 -0
  232. package/kits/physics-2d/engine/assets.js +4 -2
  233. package/kits/physics-2d/engine/scene.js +117 -5
  234. package/kits/physics-2d/package.json +1 -1
  235. package/kits/physics-2d/scripts/test-skip-unchanged.mjs +254 -0
  236. package/kits/physics-3d/CLAUDE.md +285 -1
  237. package/kits/physics-3d/behaviors/Player.jsx +74 -1
  238. package/kits/physics-3d/castle.json +3 -3
  239. package/kits/physics-3d/engine3d/Scene3DPlayer.jsx +33 -5
  240. package/kits/physics-3d/engine3d/actornode.js +253 -0
  241. package/kits/physics-3d/engine3d/artobjects.js +2 -2
  242. package/kits/physics-3d/engine3d/materials.js +29 -1
  243. package/kits/physics-3d/engine3d/nearshadows.js +600 -0
  244. package/kits/physics-3d/engine3d/unchanged.js +153 -0
  245. package/kits/physics-3d/engine3d/world3d.js +527 -5
  246. package/kits/physics-3d/package.json +1 -1
  247. package/kits/physics-3d/scripts/test-camera-follow.mjs +533 -0
  248. package/kits/physics-3d/scripts/test-near-shadows.mjs +1073 -0
  249. package/kits/physics-3d/scripts/test-static-actors.mjs +662 -0
  250. package/kits/physics-3d/scripts/test-static-passes.mjs +459 -0
  251. package/kits/physics-3d/scripts/test-static-physics.mjs +128 -0
  252. package/kits/physics-3d/systems/physics3d.js +187 -5
  253. package/kits/real-time/castle.json +2 -2
  254. package/kits/real-time/code/client/connection.js +77 -7
  255. package/kits/real-time/code/client/gameHooks.js +63 -3
  256. package/kits/real-time/code/client/messages.js +7 -0
  257. package/kits/real-time/code/server/gameHooks.js +24 -0
  258. package/kits/real-time/code/server/session.js +390 -22
  259. package/package.json +2 -2
@@ -0,0 +1,248 @@
1
+ // Grabbing and throwing a place's loose objects (contract §6, v1.3), the
2
+ // server's half. The hub client holds and throws: it already simulates any
3
+ // loose object near its player as that object's owner (multiplayer-3d), so a
4
+ // held object is one it keeps putting in front of the player, and a throw is
5
+ // the velocity it lets go with. The owner's reports are bounded by
6
+ // multiplayer-3d exactly as before. Here the place hears about it, and may
7
+ // refuse a grab or end a hold:
8
+ //
9
+ // { k: 'hub-grab', actor, op: 'grab' | 'throw' } client -> server
10
+ // { k: 'hub-grab-deny', actor } server -> that client
11
+ // { k: 'hub-drop' } server -> that client
12
+ //
13
+ // `h` is the hub layer's state: `place`, `actors`, `sim`, `hidden`, `held`,
14
+ // `hooks`, `ctx`, `session`, and `grabs`, `grabBudget`, `flying` from
15
+ // `newGrabState`. Every message is a client's word, so each is checked
16
+ // against the simulation and bounded per player.
17
+
18
+ // How far from a player's body (metres) an object they say they grabbed may
19
+ // be: the client offers one within about 2.5 m (with a place's `grab.reach`
20
+ // or `grab.steady`, v1.22, never one whose centre is past 3.5 m), and the
21
+ // server's poses lag a moving player's.
22
+ export const GRAB_REACH = 4;
23
+ // A held object is in front of its player; a throw is told as it leaves the
24
+ // hand, from about multiplayer-3d's claim reach.
25
+ export const THROW_REACH = 6;
26
+ // hub-grab messages each player may send a second, with a burst.
27
+ export const GRAB_RATE = 4;
28
+ export const GRAB_BURST = 4;
29
+ // How long a thrown object may fly at the place's throw speed before the
30
+ // server's shove cap (multiplayer-3d `MAX_PUSHED_SPEED`, 4 m/s) applies to it
31
+ // again, and the speed below which it has landed.
32
+ export const FLIGHT_MS = 3000;
33
+ const LANDED_SPEED = 4;
34
+
35
+ export function newGrabState() {
36
+ return {
37
+ // playerId -> the actor id they hold, as far as the place knows
38
+ grabs: new Map(),
39
+ // playerId -> { tokens, at }
40
+ grabBudget: new Map(),
41
+ // actorId -> { until }: thrown objects in flight (their sim entry carries
42
+ // the flight's `speedCap` meanwhile)
43
+ flying: new Map(),
44
+ };
45
+ }
46
+
47
+ // A scene actor whose body multiplayer-3d hands to clients: authored with a
48
+ // dynamic Body, built as a claimable object, and not hidden by the place.
49
+ function looseObject(h, actorId) {
50
+ if (typeof actorId !== 'string' || actorId.length > 128 || h.hidden.has(actorId)) {
51
+ return null;
52
+ }
53
+ const actor = h.actors.find((a) => a.id === actorId);
54
+ if (actor?.components.Body?.type !== 'dynamic') {
55
+ return null;
56
+ }
57
+ const entry = h.sim?.objects?.get(actorId);
58
+ return entry?.claimable && entry.body ? entry : null;
59
+ }
60
+
61
+ function near(h, playerId, entry, reach) {
62
+ const at = h.sim.players.get(playerId)?.at;
63
+ const t = entry.body.translation();
64
+ return Boolean(at && t) && Math.hypot(at.x - t.x, at.y - t.y, at.z - t.z) <= reach;
65
+ }
66
+
67
+ // One token per message, refilled at GRAB_RATE a second of simulation time.
68
+ function spend(h, playerId) {
69
+ const now = h.sim.timeMs;
70
+ const budget = h.grabBudget.get(playerId) ?? { tokens: GRAB_BURST, at: now };
71
+ budget.tokens = Math.min(GRAB_BURST, budget.tokens + ((now - budget.at) / 1000) * GRAB_RATE);
72
+ budget.at = now;
73
+ h.grabBudget.set(playerId, budget);
74
+ if (budget.tokens < 1) {
75
+ return false;
76
+ }
77
+ budget.tokens -= 1;
78
+ return true;
79
+ }
80
+
81
+ function deny(h, playerId, actorId) {
82
+ h.session?.send(playerId, { k: 'hub-grab-deny', actor: actorId });
83
+ }
84
+
85
+ // The place's `grab` hook. It refuses a grab by returning false; a hook that
86
+ // throws refuses it too, so a broken rule errs toward nothing moving.
87
+ function ask(h, playerId, actorId, op) {
88
+ if (typeof h.hooks.grab !== 'function') {
89
+ return true;
90
+ }
91
+ try {
92
+ return h.hooks.grab(h.ctx, playerId, actorId, op) !== false;
93
+ } catch (err) {
94
+ console.warn(`[hub] grab hook failed: ${err?.message ?? err}`);
95
+ return false;
96
+ }
97
+ }
98
+
99
+ // `{ k: 'hub-grab', actor, op }` from a client. A malformed message is
100
+ // dropped. A grab that is refused -- past the rate, not a loose object of the
101
+ // place, too far, not this player's to simulate, a held or departed player,
102
+ // or the place's hook said no -- is answered `hub-grab-deny`, and the client
103
+ // lets go. A throw of something the place does not know this player holds is
104
+ // dropped.
105
+ export function message(h, playerId, data) {
106
+ const op = data?.op;
107
+ if (
108
+ playerId === undefined ||
109
+ playerId === null ||
110
+ !h.place.grab ||
111
+ !h.sim ||
112
+ (op !== 'grab' && op !== 'throw') ||
113
+ typeof data.actor !== 'string' ||
114
+ data.actor.length > 128
115
+ ) {
116
+ return;
117
+ }
118
+ const actorId = data.actor;
119
+ if (!spend(h, playerId)) {
120
+ if (op === 'grab') {
121
+ deny(h, playerId, actorId);
122
+ }
123
+ return;
124
+ }
125
+ const entry = looseObject(h, actorId);
126
+ const entered = h.players.get(playerId)?.arrived && !h.players.get(playerId)?.ended;
127
+ if (op === 'throw') {
128
+ if (h.grabs.get(playerId) !== actorId) {
129
+ return;
130
+ }
131
+ h.grabs.delete(playerId);
132
+ // Still theirs, now: the object may have been handed to someone else, or
133
+ // the player held, since the place last looked.
134
+ if (entry && entry.owner === playerId && entered && !h.held.has(playerId) && near(h, playerId, entry, THROW_REACH)) {
135
+ fly(h, actorId, entry);
136
+ ask(h, playerId, actorId, 'throw');
137
+ }
138
+ return;
139
+ }
140
+ // The client offers only what multiplayer-3d has handed it, so the object
141
+ // is this player's on the server too.
142
+ if (
143
+ !entered ||
144
+ h.held.has(playerId) ||
145
+ entry?.owner !== playerId ||
146
+ !near(h, playerId, entry, GRAB_REACH)
147
+ ) {
148
+ deny(h, playerId, actorId);
149
+ return;
150
+ }
151
+ if (!ask(h, playerId, actorId, 'grab')) {
152
+ deny(h, playerId, actorId);
153
+ return;
154
+ }
155
+ // One hold per player, and one holder per object.
156
+ for (const [other, held] of h.grabs) {
157
+ if (held === actorId && other !== playerId) {
158
+ h.grabs.delete(other);
159
+ }
160
+ }
161
+ h.grabs.set(playerId, actorId);
162
+ }
163
+
164
+ // A thrown object is not slowed to a shove's speed while it flies: until it
165
+ // lands, or FLIGHT_MS, it has its own cap instead. But never faster
166
+ // than the place's throw: the release a client reports is its own word, so
167
+ // the server holds the flight to `throwSpeed` (and a little slack) itself.
168
+ export const THROW_SLACK = 1.1;
169
+
170
+ function throwCap(h) {
171
+ return (h.place.grab?.throwSpeed ?? 10) * THROW_SLACK + 0.5;
172
+ }
173
+
174
+ // The flight's cap is multiplayer-3d's own per-object `speedCap`, which its
175
+ // world enforces after every physics step (contacts included); an older
176
+ // multiplayer-3d ignores it and holds the flight to a shove's speed.
177
+ function fly(h, actorId, entry) {
178
+ h.flying.set(actorId, { until: h.sim.timeMs + FLIGHT_MS });
179
+ entry.speedCap = throwCap(h);
180
+ capSpeed(entry.body, entry.speedCap);
181
+ }
182
+
183
+ function capSpeed(body, cap) {
184
+ const v = body?.linvel?.();
185
+ const speed = v ? Math.hypot(v.x, v.y, v.z) : 0;
186
+ if (speed > cap) {
187
+ const k = cap / speed;
188
+ body.setLinvel({ x: v.x * k, y: v.y * k, z: v.z * k }, true);
189
+ }
190
+ }
191
+
192
+ // `hub.drop(playerId?)`: whoever holds something lets go (default: everyone).
193
+ // Only players the place knows hold something are told. Returns how many.
194
+ export function drop(h, playerId) {
195
+ let n = 0;
196
+ for (const [holder, actorId] of [...h.grabs]) {
197
+ if (playerId !== undefined && holder !== playerId) {
198
+ continue;
199
+ }
200
+ h.grabs.delete(holder);
201
+ h.session?.send(holder, { k: 'hub-drop', actor: actorId });
202
+ n += 1;
203
+ }
204
+ return n;
205
+ }
206
+
207
+ // Each tick: forget holds that are over (the object went to someone else or
208
+ // back to the server, the place hid it, the player is held or gone), and end
209
+ // flights that have landed or run out.
210
+ export function tick(h) {
211
+ for (const [playerId, actorId] of [...h.grabs]) {
212
+ const entry = looseObject(h, actorId);
213
+ if (
214
+ !entry ||
215
+ entry.owner !== playerId ||
216
+ h.held.has(playerId) ||
217
+ !h.sim.players.has(playerId)
218
+ ) {
219
+ h.grabs.delete(playerId);
220
+ }
221
+ }
222
+ for (const [actorId, flight] of [...h.flying]) {
223
+ const entry = h.sim.objects?.get(actorId);
224
+ // In the server's hands, a flight is held to the place's throw however
225
+ // it was released or handed back.
226
+ if (entry && !entry.owner) {
227
+ capSpeed(entry.body, throwCap(h));
228
+ }
229
+ const v = entry && !entry.owner ? entry.body.linvel?.() : null;
230
+ const landed = v ? Math.hypot(v.x, v.y, v.z) <= LANDED_SPEED : false;
231
+ if (!entry || landed || h.sim.timeMs >= flight.until) {
232
+ if (entry) {
233
+ delete entry.speedCap;
234
+ }
235
+ h.flying.delete(actorId);
236
+ }
237
+ }
238
+ }
239
+
240
+ export function forget(h, playerId) {
241
+ h.grabs.delete(playerId);
242
+ h.grabBudget.delete(playerId);
243
+ }
244
+
245
+ // Who holds what, for the place: actor id or null.
246
+ export function holding(h, playerId) {
247
+ return h.grabs.get(playerId) ?? null;
248
+ }
@@ -0,0 +1,96 @@
1
+ // What ghost holds of each player's hub results (contract §7, v1.5): their
2
+ // newest results across places and their record per place, read with the
3
+ // arrival read the kit already makes and refreshed once a minute for everyone
4
+ // here, so a place's deck code asks `hub.results(playerId)` rather than
5
+ // calling the host's results read itself. Only where the host has hub ops;
6
+ // null on the dev serve and for an anonymous player.
7
+
8
+ export const HISTORY_EVERY_MS = 60000;
9
+ const MAX_ROWS = 20;
10
+ const MAX_USERS = 24;
11
+
12
+ const str = (v, n) => (typeof v === 'string' ? v.slice(0, n) : null);
13
+ const num = (v) => (typeof v === 'number' && Number.isFinite(v) ? v : null);
14
+
15
+ function cleanRow(r) {
16
+ return {
17
+ deckId: str(r?.deckId, 64),
18
+ place: str(r?.place, 32),
19
+ title: str(r?.title, 40),
20
+ outcome: ['win', 'lose', 'draw'].includes(r?.outcome) ? r.outcome : null,
21
+ score: num(r?.score),
22
+ unit: ['ms', 's', 'points'].includes(r?.unit) ? r.unit : null,
23
+ better: r?.better === 'higher' || r?.better === 'lower' ? r.better : null,
24
+ stats: r?.stats && typeof r.stats === 'object' && !Array.isArray(r.stats) ? { ...r.stats } : {},
25
+ at: str(r?.at, 40),
26
+ };
27
+ }
28
+
29
+ function cleanRecord(r) {
30
+ const o = r?.outcomes ?? {};
31
+ return {
32
+ deckId: str(r?.deckId, 64),
33
+ place: str(r?.place, 32),
34
+ title: str(r?.title, 40),
35
+ plays: num(r?.plays) ?? 0,
36
+ outcomes: { win: num(o.win) ?? 0, lose: num(o.lose) ?? 0, draw: num(o.draw) ?? 0 },
37
+ bestScore: num(r?.bestScore),
38
+ unit: ['ms', 's', 'points'].includes(r?.unit) ? r.unit : null,
39
+ better: r?.better === 'higher' || r?.better === 'lower' ? r.better : null,
40
+ winStreak: num(r?.winStreak) ?? 0,
41
+ lastOutcome: ['win', 'lose', 'draw'].includes(r?.lastOutcome) ? r.lastOutcome : null,
42
+ lastScore: num(r?.lastScore),
43
+ lastAt: str(r?.lastAt, 40),
44
+ };
45
+ }
46
+
47
+ // One player's read, as ghost answered it, kept on their entry -- unless a
48
+ // read that started later has already landed (the arrival read and a
49
+ // refresh can be in flight together).
50
+ export function take(h, playerId, read, startedAt = Date.now()) {
51
+ const entry = h.players.get(playerId);
52
+ if (!entry || !read || (entry.historyFrom ?? -Infinity) > startedAt) {
53
+ return;
54
+ }
55
+ entry.historyFrom = startedAt;
56
+ entry.history = {
57
+ recent: (Array.isArray(read.recent) ? read.recent : []).slice(0, MAX_ROWS).map(cleanRow),
58
+ records: (Array.isArray(read.records) ? read.records : []).slice(0, MAX_ROWS).map(cleanRecord),
59
+ readAt: Date.now(),
60
+ };
61
+ }
62
+
63
+ // Each tick where the host has hub ops: once a minute, one read for every
64
+ // arrived signed-in player here (up to MAX_USERS a read).
65
+ export function refresh(h, hub, membersOf, refused) {
66
+ const now = Date.now();
67
+ if (h.historyReading || now - (h.historyAt ?? 0) < HISTORY_EVERY_MS) {
68
+ return;
69
+ }
70
+ h.historyAt = now;
71
+ const arrived = [...h.players].filter(([, e]) => e.arrived && !e.ended).map(([id]) => id);
72
+ const users = membersOf(h, arrived);
73
+ if (!users.size) {
74
+ return;
75
+ }
76
+ const ids = [...users.keys()].slice(0, MAX_USERS);
77
+ h.historyReading = true;
78
+ const startedAt = now;
79
+ hub
80
+ .readResults({ userIds: ids })
81
+ .then((read) => {
82
+ for (const userId of ids) {
83
+ take(h, users.get(userId), read?.players?.[userId], startedAt);
84
+ }
85
+ })
86
+ .catch((err) => refused(h, 'history', err))
87
+ .finally(() => {
88
+ h.historyReading = false;
89
+ });
90
+ }
91
+
92
+ // `hub.results(playerId)`: { recent, records, readAt } (copies), or null.
93
+ export function results(h, playerId) {
94
+ const history = h.players.get(playerId)?.history;
95
+ return history ? structuredClone(history) : null;
96
+ }