castle-web-cli 0.4.229 → 0.4.230

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 (232) 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 +779 -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/devSessionChild.js +3 -1
  19. package/dist/devSessionServer.d.ts +14 -1
  20. package/dist/devSessionServer.js +229 -44
  21. package/dist/experimentAssignment.d.ts +15 -0
  22. package/dist/experimentAssignment.js +41 -0
  23. package/dist/importCheckHook.d.ts +40 -0
  24. package/dist/importCheckHook.js +171 -0
  25. package/dist/importRegression.d.ts +15 -0
  26. package/dist/importRegression.js +100 -0
  27. package/dist/importTrust.d.ts +2 -0
  28. package/dist/importTrust.js +27 -0
  29. package/dist/importUpdateHook.d.ts +18 -0
  30. package/dist/importUpdateHook.js +31 -8
  31. package/dist/importUpdateHookRunner.js +19 -7
  32. package/dist/imports.d.ts +16 -0
  33. package/dist/imports.js +141 -31
  34. package/dist/index.js +3 -1
  35. package/dist/init.d.ts +3 -0
  36. package/dist/init.js +125 -8
  37. package/dist/install.js +4 -1
  38. package/dist/localPaths.js +4 -0
  39. package/dist/login.js +2 -2
  40. package/dist/platformDoc.js +3 -8
  41. package/dist/playPagePolicy.js +3 -1
  42. package/dist/remix.js +5 -0
  43. package/dist/routerTaskReport.d.ts +28 -0
  44. package/dist/routerTaskReport.js +56 -0
  45. package/dist/save-deck.d.ts +18 -0
  46. package/dist/save-deck.js +142 -6
  47. package/dist/serve.d.ts +8 -0
  48. package/dist/serve.js +94 -13
  49. package/dist/serverBundle.d.ts +1 -1
  50. package/dist/serverBundle.js +5 -1
  51. package/dist/webSourceSync.d.ts +2 -0
  52. package/dist/webSourceSync.js +2 -0
  53. package/kits/base/CLAUDE.md +2 -3
  54. package/kits/base/castle.json +1 -1
  55. package/kits/base/sdk/README.md +258 -52
  56. package/kits/base/sdk/agent.js +4 -4
  57. package/kits/base/sdk/avatar.d.ts +12 -0
  58. package/kits/base/sdk/avatar.js +23 -0
  59. package/kits/base/sdk/castle.d.ts +5 -0
  60. package/kits/base/sdk/castle.js +3 -0
  61. package/kits/base/sdk/commands.d.ts +208 -0
  62. package/kits/base/sdk/commands.js +16 -0
  63. package/kits/base/sdk/context.d.ts +6 -0
  64. package/kits/base/sdk/experiments.d.ts +15 -0
  65. package/kits/base/sdk/experiments.js +83 -0
  66. package/kits/base/sdk/hub.d.ts +85 -0
  67. package/kits/base/sdk/hub.js +107 -0
  68. package/kits/base/sdk/multiplayer.d.ts +45 -1
  69. package/kits/base/sdk/multiplayer.js +106 -4
  70. package/kits/base/sdk/server/hub.d.ts +169 -0
  71. package/kits/base/sdk/server/hub.js +125 -0
  72. package/kits/base/sdk/server/platformHandle.d.ts +42 -1
  73. package/kits/base/sdk/server/storage.d.ts +55 -2
  74. package/kits/base/sdk/server/wrapper.d.ts +32 -2
  75. package/kits/base/sdk/server/wrapper.js +90 -11
  76. package/kits/base/sdk/transport.d.ts +2 -1
  77. package/kits/base/sdk/transport.js +13 -1
  78. package/kits/hub-game/.prettierrc +5 -0
  79. package/kits/hub-game/CLAUDE.md +146 -0
  80. package/kits/hub-game/blueprints/avatar-0.scene +28 -0
  81. package/kits/hub-game/blueprints/avatar-1.scene +28 -0
  82. package/kits/hub-game/blueprints/avatar-2.scene +28 -0
  83. package/kits/hub-game/blueprints/avatar-3.scene +28 -0
  84. package/kits/hub-game/blueprints/bean/avatar-0.scene +28 -0
  85. package/kits/hub-game/blueprints/bean/avatar-1.scene +28 -0
  86. package/kits/hub-game/blueprints/bean/avatar-2.scene +28 -0
  87. package/kits/hub-game/blueprints/bean/avatar-3.scene +28 -0
  88. package/kits/hub-game/blueprints/blocky/avatar-0.scene +28 -0
  89. package/kits/hub-game/blueprints/blocky/avatar-1.scene +28 -0
  90. package/kits/hub-game/blueprints/blocky/avatar-2.scene +28 -0
  91. package/kits/hub-game/blueprints/blocky/avatar-3.scene +28 -0
  92. package/kits/hub-game/blueprints/pixel/avatar-0.scene +28 -0
  93. package/kits/hub-game/blueprints/pixel/avatar-1.scene +28 -0
  94. package/kits/hub-game/blueprints/pixel/avatar-2.scene +28 -0
  95. package/kits/hub-game/blueprints/pixel/avatar-3.scene +28 -0
  96. package/kits/hub-game/castle.json +96 -0
  97. package/kits/hub-game/code/client/attention.js +133 -0
  98. package/kits/hub-game/code/client/avatar.js +288 -0
  99. package/kits/hub-game/code/client/cabinets.js +90 -0
  100. package/kits/hub-game/code/client/chat.js +135 -0
  101. package/kits/hub-game/code/client/chrome.js +1001 -0
  102. package/kits/hub-game/code/client/doorFrame.js +15 -0
  103. package/kits/hub-game/code/client/firstJoin.js +14 -0
  104. package/kits/hub-game/code/client/grab.js +241 -0
  105. package/kits/hub-game/code/client/hookLayer.js +230 -0
  106. package/kits/hub-game/code/client/idle.js +326 -0
  107. package/kits/hub-game/code/client/keys.js +48 -0
  108. package/kits/hub-game/code/client/pack.js +232 -0
  109. package/kits/hub-game/code/client/packFormat.js +397 -0
  110. package/kits/hub-game/code/client/say.js +127 -0
  111. package/kits/hub-game/code/client/sha256.js +80 -0
  112. package/kits/hub-game/code/client/sound.js +147 -0
  113. package/kits/hub-game/code/client/state.js +131 -0
  114. package/kits/hub-game/code/client/talk.js +259 -0
  115. package/kits/hub-game/code/client/together.js +365 -0
  116. package/kits/hub-game/code/client/wear.js +149 -0
  117. package/kits/hub-game/code/server/actors.js +627 -0
  118. package/kits/hub-game/code/server/blocks.js +103 -0
  119. package/kits/hub-game/code/server/cabinets.js +309 -0
  120. package/kits/hub-game/code/server/game.js +137 -0
  121. package/kits/hub-game/code/server/grab.js +247 -0
  122. package/kits/hub-game/code/server/history.js +96 -0
  123. package/kits/hub-game/code/server/hub.js +1904 -0
  124. package/kits/hub-game/code/server/idle.js +412 -0
  125. package/kits/hub-game/code/server/index.js +64 -0
  126. package/kits/hub-game/code/server/labels.js +59 -0
  127. package/kits/hub-game/code/server/manifest.js +527 -0
  128. package/kits/hub-game/code/server/npcs.js +745 -0
  129. package/kits/hub-game/code/server/party.js +381 -0
  130. package/kits/hub-game/code/server/place.js +43 -0
  131. package/kits/hub-game/code/server/say.js +83 -0
  132. package/kits/hub-game/code/server/wear.js +142 -0
  133. package/kits/hub-game/code/systems/hub.js +2230 -0
  134. package/kits/hub-game/docs/content-pack.md +151 -0
  135. package/kits/hub-game/docs/contract.md +1139 -0
  136. package/kits/hub-game/docs/style.md +137 -0
  137. package/kits/hub-game/drawings/blocky/castle-eye.sprite +58 -0
  138. package/kits/hub-game/drawings/blocky/dash.sprite +59 -0
  139. package/kits/hub-game/drawings/blocky/pom.sprite +59 -0
  140. package/kits/hub-game/drawings/blocky/tabby.sprite +59 -0
  141. package/kits/hub-game/drawings/pixel/castle-arm.sprite +13 -0
  142. package/kits/hub-game/drawings/pixel/castle-body.sprite +23 -0
  143. package/kits/hub-game/drawings/pixel/castle-leg.sprite +12 -0
  144. package/kits/hub-game/drawings/pixel/kai-arm.sprite +14 -0
  145. package/kits/hub-game/drawings/pixel/kai-head.sprite +24 -0
  146. package/kits/hub-game/drawings/pixel/kai-leg.sprite +15 -0
  147. package/kits/hub-game/drawings/pixel/kai-torso.sprite +18 -0
  148. package/kits/hub-game/drawings/pixel/mimi-arm.sprite +13 -0
  149. package/kits/hub-game/drawings/pixel/mimi-head.sprite +23 -0
  150. package/kits/hub-game/drawings/pixel/mimi-leg.sprite +15 -0
  151. package/kits/hub-game/drawings/pixel/mimi-torso.sprite +18 -0
  152. package/kits/hub-game/drawings/pixel/nox-arm.sprite +13 -0
  153. package/kits/hub-game/drawings/pixel/nox-head.sprite +21 -0
  154. package/kits/hub-game/drawings/pixel/nox-leg.sprite +14 -0
  155. package/kits/hub-game/drawings/pixel/nox-torso.sprite +18 -0
  156. package/kits/hub-game/eslint.config.js +20 -0
  157. package/kits/hub-game/fonts.generated.js +9 -0
  158. package/kits/hub-game/index.html +22 -0
  159. package/kits/hub-game/main.jsx +6 -0
  160. package/kits/hub-game/models/avatar-0.pxmodel +21 -0
  161. package/kits/hub-game/models/avatar-1.pxmodel +24 -0
  162. package/kits/hub-game/models/avatar-2.pxmodel +27 -0
  163. package/kits/hub-game/models/avatar-3.pxmodel +22 -0
  164. package/kits/hub-game/models/bean/avatar-0.pxmodel +19 -0
  165. package/kits/hub-game/models/bean/avatar-1.pxmodel +24 -0
  166. package/kits/hub-game/models/bean/avatar-2.pxmodel +22 -0
  167. package/kits/hub-game/models/bean/avatar-3.pxmodel +24 -0
  168. package/kits/hub-game/models/blocky/avatar-0.pxmodel +21 -0
  169. package/kits/hub-game/models/blocky/avatar-1.pxmodel +30 -0
  170. package/kits/hub-game/models/blocky/avatar-2.pxmodel +28 -0
  171. package/kits/hub-game/models/blocky/avatar-3.pxmodel +27 -0
  172. package/kits/hub-game/models/pixel/avatar-0.pxmodel +24 -0
  173. package/kits/hub-game/models/pixel/avatar-1.pxmodel +16 -0
  174. package/kits/hub-game/models/pixel/avatar-2.pxmodel +19 -0
  175. package/kits/hub-game/models/pixel/avatar-3.pxmodel +23 -0
  176. package/kits/hub-game/package-lock.json +365 -0
  177. package/kits/hub-game/package.json +32 -0
  178. package/kits/hub-game/scenes/main.scene +318 -0
  179. package/kits/hub-game/scripts/check.mjs +339 -0
  180. package/kits/hub-game/scripts/fixtures/sdk-stub.mjs +3 -0
  181. package/kits/hub-game/scripts/gen-avatars-bean.mjs +748 -0
  182. package/kits/hub-game/scripts/gen-avatars-blocky.mjs +607 -0
  183. package/kits/hub-game/scripts/gen-avatars-pixel.mjs +771 -0
  184. package/kits/hub-game/scripts/gen-avatars.mjs +621 -0
  185. package/kits/hub-game/scripts/pack.mjs +192 -0
  186. package/kits/hub-game/scripts/test-actors.mjs +666 -0
  187. package/kits/hub-game/scripts/test-attention.mjs +103 -0
  188. package/kits/hub-game/scripts/test-avatar.mjs +472 -0
  189. package/kits/hub-game/scripts/test-ban-join.mjs +194 -0
  190. package/kits/hub-game/scripts/test-blocks.mjs +351 -0
  191. package/kits/hub-game/scripts/test-chat.mjs +98 -0
  192. package/kits/hub-game/scripts/test-classic.mjs +590 -0
  193. package/kits/hub-game/scripts/test-door-jump.mjs +131 -0
  194. package/kits/hub-game/scripts/test-door.mjs +56 -0
  195. package/kits/hub-game/scripts/test-grab.mjs +331 -0
  196. package/kits/hub-game/scripts/test-hub-ops.mjs +835 -0
  197. package/kits/hub-game/scripts/test-hub-trip.mjs +1310 -0
  198. package/kits/hub-game/scripts/test-idle.mjs +518 -0
  199. package/kits/hub-game/scripts/test-interest.mjs +413 -0
  200. package/kits/hub-game/scripts/test-keys.mjs +50 -0
  201. package/kits/hub-game/scripts/test-manifest.mjs +224 -0
  202. package/kits/hub-game/scripts/test-no-forms.mjs +16 -0
  203. package/kits/hub-game/scripts/test-npcs.mjs +714 -0
  204. package/kits/hub-game/scripts/test-pack.mjs +424 -0
  205. package/kits/hub-game/scripts/test-party.mjs +286 -0
  206. package/kits/hub-game/scripts/test-say.mjs +149 -0
  207. package/kits/hub-game/scripts/test-together.mjs +183 -0
  208. package/kits/hub-game/theme.style +3 -0
  209. package/kits/multiplayer-3d/castle.json +5 -5
  210. package/kits/multiplayer-3d/code/client/avatars.js +101 -1
  211. package/kits/multiplayer-3d/code/server/players.js +4 -1
  212. package/kits/multiplayer-3d/code/server/sceneBodies.js +27 -6
  213. package/kits/multiplayer-3d/code/server/world.js +20 -7
  214. package/kits/multiplayer-3d/code/systems/multiplayer.js +222 -11
  215. package/kits/physics-2d/castle.json +2 -2
  216. package/kits/physics-2d/engine/art.js +13 -0
  217. package/kits/physics-2d/engine/assets.js +4 -2
  218. package/kits/physics-2d/engine/scene.js +25 -5
  219. package/kits/physics-3d/CLAUDE.md +6 -1
  220. package/kits/physics-3d/castle.json +3 -3
  221. package/kits/physics-3d/engine3d/Scene3DPlayer.jsx +32 -4
  222. package/kits/physics-3d/engine3d/artobjects.js +2 -2
  223. package/kits/physics-3d/engine3d/materials.js +29 -1
  224. package/kits/physics-3d/engine3d/world3d.js +65 -1
  225. package/kits/physics-3d/systems/physics3d.js +5 -2
  226. package/kits/real-time/castle.json +2 -2
  227. package/kits/real-time/code/client/connection.js +77 -7
  228. package/kits/real-time/code/client/gameHooks.js +63 -3
  229. package/kits/real-time/code/client/messages.js +7 -0
  230. package/kits/real-time/code/server/gameHooks.js +24 -0
  231. package/kits/real-time/code/server/session.js +390 -22
  232. package/package.json +2 -2
@@ -0,0 +1,247 @@
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, and the server's poses lag a
20
+ // moving player's.
21
+ export const GRAB_REACH = 4;
22
+ // A held object is in front of its player; a throw is told as it leaves the
23
+ // hand, from about multiplayer-3d's claim reach.
24
+ export const THROW_REACH = 6;
25
+ // hub-grab messages each player may send a second, with a burst.
26
+ export const GRAB_RATE = 4;
27
+ export const GRAB_BURST = 4;
28
+ // How long a thrown object may fly at the place's throw speed before the
29
+ // server's shove cap (multiplayer-3d `MAX_PUSHED_SPEED`, 4 m/s) applies to it
30
+ // again, and the speed below which it has landed.
31
+ export const FLIGHT_MS = 3000;
32
+ const LANDED_SPEED = 4;
33
+
34
+ export function newGrabState() {
35
+ return {
36
+ // playerId -> the actor id they hold, as far as the place knows
37
+ grabs: new Map(),
38
+ // playerId -> { tokens, at }
39
+ grabBudget: new Map(),
40
+ // actorId -> { until }: thrown objects in flight (their sim entry carries
41
+ // the flight's `speedCap` meanwhile)
42
+ flying: new Map(),
43
+ };
44
+ }
45
+
46
+ // A scene actor whose body multiplayer-3d hands to clients: authored with a
47
+ // dynamic Body, built as a claimable object, and not hidden by the place.
48
+ function looseObject(h, actorId) {
49
+ if (typeof actorId !== 'string' || actorId.length > 128 || h.hidden.has(actorId)) {
50
+ return null;
51
+ }
52
+ const actor = h.actors.find((a) => a.id === actorId);
53
+ if (actor?.components.Body?.type !== 'dynamic') {
54
+ return null;
55
+ }
56
+ const entry = h.sim?.objects?.get(actorId);
57
+ return entry?.claimable && entry.body ? entry : null;
58
+ }
59
+
60
+ function near(h, playerId, entry, reach) {
61
+ const at = h.sim.players.get(playerId)?.at;
62
+ const t = entry.body.translation();
63
+ return Boolean(at && t) && Math.hypot(at.x - t.x, at.y - t.y, at.z - t.z) <= reach;
64
+ }
65
+
66
+ // One token per message, refilled at GRAB_RATE a second of simulation time.
67
+ function spend(h, playerId) {
68
+ const now = h.sim.timeMs;
69
+ const budget = h.grabBudget.get(playerId) ?? { tokens: GRAB_BURST, at: now };
70
+ budget.tokens = Math.min(GRAB_BURST, budget.tokens + ((now - budget.at) / 1000) * GRAB_RATE);
71
+ budget.at = now;
72
+ h.grabBudget.set(playerId, budget);
73
+ if (budget.tokens < 1) {
74
+ return false;
75
+ }
76
+ budget.tokens -= 1;
77
+ return true;
78
+ }
79
+
80
+ function deny(h, playerId, actorId) {
81
+ h.session?.send(playerId, { k: 'hub-grab-deny', actor: actorId });
82
+ }
83
+
84
+ // The place's `grab` hook. It refuses a grab by returning false; a hook that
85
+ // throws refuses it too, so a broken rule errs toward nothing moving.
86
+ function ask(h, playerId, actorId, op) {
87
+ if (typeof h.hooks.grab !== 'function') {
88
+ return true;
89
+ }
90
+ try {
91
+ return h.hooks.grab(h.ctx, playerId, actorId, op) !== false;
92
+ } catch (err) {
93
+ console.warn(`[hub] grab hook failed: ${err?.message ?? err}`);
94
+ return false;
95
+ }
96
+ }
97
+
98
+ // `{ k: 'hub-grab', actor, op }` from a client. A malformed message is
99
+ // dropped. A grab that is refused -- past the rate, not a loose object of the
100
+ // place, too far, not this player's to simulate, a held or departed player,
101
+ // or the place's hook said no -- is answered `hub-grab-deny`, and the client
102
+ // lets go. A throw of something the place does not know this player holds is
103
+ // dropped.
104
+ export function message(h, playerId, data) {
105
+ const op = data?.op;
106
+ if (
107
+ playerId === undefined ||
108
+ playerId === null ||
109
+ !h.place.grab ||
110
+ !h.sim ||
111
+ (op !== 'grab' && op !== 'throw') ||
112
+ typeof data.actor !== 'string' ||
113
+ data.actor.length > 128
114
+ ) {
115
+ return;
116
+ }
117
+ const actorId = data.actor;
118
+ if (!spend(h, playerId)) {
119
+ if (op === 'grab') {
120
+ deny(h, playerId, actorId);
121
+ }
122
+ return;
123
+ }
124
+ const entry = looseObject(h, actorId);
125
+ const entered = h.players.get(playerId)?.arrived && !h.players.get(playerId)?.ended;
126
+ if (op === 'throw') {
127
+ if (h.grabs.get(playerId) !== actorId) {
128
+ return;
129
+ }
130
+ h.grabs.delete(playerId);
131
+ // Still theirs, now: the object may have been handed to someone else, or
132
+ // the player held, since the place last looked.
133
+ if (entry && entry.owner === playerId && entered && !h.held.has(playerId) && near(h, playerId, entry, THROW_REACH)) {
134
+ fly(h, actorId, entry);
135
+ ask(h, playerId, actorId, 'throw');
136
+ }
137
+ return;
138
+ }
139
+ // The client offers only what multiplayer-3d has handed it, so the object
140
+ // is this player's on the server too.
141
+ if (
142
+ !entered ||
143
+ h.held.has(playerId) ||
144
+ entry?.owner !== playerId ||
145
+ !near(h, playerId, entry, GRAB_REACH)
146
+ ) {
147
+ deny(h, playerId, actorId);
148
+ return;
149
+ }
150
+ if (!ask(h, playerId, actorId, 'grab')) {
151
+ deny(h, playerId, actorId);
152
+ return;
153
+ }
154
+ // One hold per player, and one holder per object.
155
+ for (const [other, held] of h.grabs) {
156
+ if (held === actorId && other !== playerId) {
157
+ h.grabs.delete(other);
158
+ }
159
+ }
160
+ h.grabs.set(playerId, actorId);
161
+ }
162
+
163
+ // A thrown object is not slowed to a shove's speed while it flies: until it
164
+ // lands, or FLIGHT_MS, it has its own cap instead. But never faster
165
+ // than the place's throw: the release a client reports is its own word, so
166
+ // the server holds the flight to `throwSpeed` (and a little slack) itself.
167
+ export const THROW_SLACK = 1.1;
168
+
169
+ function throwCap(h) {
170
+ return (h.place.grab?.throwSpeed ?? 10) * THROW_SLACK + 0.5;
171
+ }
172
+
173
+ // The flight's cap is multiplayer-3d's own per-object `speedCap`, which its
174
+ // world enforces after every physics step (contacts included); an older
175
+ // multiplayer-3d ignores it and holds the flight to a shove's speed.
176
+ function fly(h, actorId, entry) {
177
+ h.flying.set(actorId, { until: h.sim.timeMs + FLIGHT_MS });
178
+ entry.speedCap = throwCap(h);
179
+ capSpeed(entry.body, entry.speedCap);
180
+ }
181
+
182
+ function capSpeed(body, cap) {
183
+ const v = body?.linvel?.();
184
+ const speed = v ? Math.hypot(v.x, v.y, v.z) : 0;
185
+ if (speed > cap) {
186
+ const k = cap / speed;
187
+ body.setLinvel({ x: v.x * k, y: v.y * k, z: v.z * k }, true);
188
+ }
189
+ }
190
+
191
+ // `hub.drop(playerId?)`: whoever holds something lets go (default: everyone).
192
+ // Only players the place knows hold something are told. Returns how many.
193
+ export function drop(h, playerId) {
194
+ let n = 0;
195
+ for (const [holder, actorId] of [...h.grabs]) {
196
+ if (playerId !== undefined && holder !== playerId) {
197
+ continue;
198
+ }
199
+ h.grabs.delete(holder);
200
+ h.session?.send(holder, { k: 'hub-drop', actor: actorId });
201
+ n += 1;
202
+ }
203
+ return n;
204
+ }
205
+
206
+ // Each tick: forget holds that are over (the object went to someone else or
207
+ // back to the server, the place hid it, the player is held or gone), and end
208
+ // flights that have landed or run out.
209
+ export function tick(h) {
210
+ for (const [playerId, actorId] of [...h.grabs]) {
211
+ const entry = looseObject(h, actorId);
212
+ if (
213
+ !entry ||
214
+ entry.owner !== playerId ||
215
+ h.held.has(playerId) ||
216
+ !h.sim.players.has(playerId)
217
+ ) {
218
+ h.grabs.delete(playerId);
219
+ }
220
+ }
221
+ for (const [actorId, flight] of [...h.flying]) {
222
+ const entry = h.sim.objects?.get(actorId);
223
+ // In the server's hands, a flight is held to the place's throw however
224
+ // it was released or handed back.
225
+ if (entry && !entry.owner) {
226
+ capSpeed(entry.body, throwCap(h));
227
+ }
228
+ const v = entry && !entry.owner ? entry.body.linvel?.() : null;
229
+ const landed = v ? Math.hypot(v.x, v.y, v.z) <= LANDED_SPEED : false;
230
+ if (!entry || landed || h.sim.timeMs >= flight.until) {
231
+ if (entry) {
232
+ delete entry.speedCap;
233
+ }
234
+ h.flying.delete(actorId);
235
+ }
236
+ }
237
+ }
238
+
239
+ export function forget(h, playerId) {
240
+ h.grabs.delete(playerId);
241
+ h.grabBudget.delete(playerId);
242
+ }
243
+
244
+ // Who holds what, for the place: actor id or null.
245
+ export function holding(h, playerId) {
246
+ return h.grabs.get(playerId) ?? null;
247
+ }
@@ -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
+ }