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,381 @@
1
+ // A party in a town (contract §5, v1.10): a few players who go through doors
2
+ // together. One player invites another from the People list or by walking up
3
+ // to them; the other accepts. A member who picks a door from the Doors list
4
+ // proposes it: they are put on its pad, and each member who says yes is put
5
+ // on it too, so the pad counts them and fires for all of them at once. A
6
+ // party is the place's own and lasts while its layer does: a member away
7
+ // through a door is kept for half an hour and is back in it on return.
8
+ // A game (a place with `result`) has no parties and answers none of this.
9
+ //
10
+ // Members are kept by user id where a player has one (so a return is the same
11
+ // member) and by player id for a guest, who is a member only while here.
12
+
13
+ // The most players one party holds.
14
+ export const PARTY_MAX = 8;
15
+ // An invitation and a proposed door wait this long for an answer.
16
+ const INVITE_MS = 60000;
17
+ const PROPOSAL_MS = 45000;
18
+ // How long of that a proposal may hold the pad's countdown (`hub.coming`).
19
+ const PROPOSAL_HOLD_MS = 20000;
20
+ // A party's proposal holds a pad's countdown (`hub.coming`) at most once in
21
+ // this long, so proposing again and again cannot keep a door shut.
22
+ const HOLD_EVERY_MS = 60000;
23
+ // One player's party messages: a burst of this many, then one a second.
24
+ const RATE_BURST = 6;
25
+ const RATE_EVERY_MS = 1000;
26
+ // A member who left the layer (through a door) is kept this long.
27
+ const AWAY_MS = 30 * 60 * 1000;
28
+ // How far from a pad's middle party members are put, in a ring.
29
+ const PAD_RING_M = 0.9;
30
+
31
+ const DEG = Math.PI / 180;
32
+
33
+ export function newPartyState() {
34
+ return {
35
+ party: {
36
+ seq: 0,
37
+ // member key -> party id, and party id -> { keys: Set, away: Map(key -> since) }
38
+ of: new Map(),
39
+ parties: new Map(),
40
+ // invitee playerId -> { from: playerId, until }
41
+ invites: new Map(),
42
+ // party id -> { id, door, by: playerId, until, holdUntil, asked: Set(playerId), n }
43
+ proposals: new Map(),
44
+ // playerId -> { tokens, at }, and the JSON last sent to each player
45
+ rate: new Map(),
46
+ sent: new Map(),
47
+ },
48
+ };
49
+ }
50
+
51
+ const keyOf = (h, playerId) => {
52
+ const userId = h.players.get(playerId)?.userId;
53
+ return userId ? `u:${userId}` : `p:${playerId}`;
54
+ };
55
+
56
+ // The players of a party who are here, arrived.
57
+ function present(h, partyId) {
58
+ const party = h.party.parties.get(partyId);
59
+ const out = [];
60
+ if (party) {
61
+ for (const [playerId, entry] of h.players) {
62
+ if (entry.arrived && h.sim?.players.has(playerId) && party.keys.has(keyOf(h, playerId))) {
63
+ out.push(playerId);
64
+ }
65
+ }
66
+ }
67
+ return out;
68
+ }
69
+
70
+ function spend(h, playerId) {
71
+ const now = Date.now();
72
+ const r = h.party.rate.get(playerId) ?? { tokens: RATE_BURST, at: now };
73
+ r.tokens = Math.min(RATE_BURST, r.tokens + (now - r.at) / RATE_EVERY_MS);
74
+ r.at = now;
75
+ h.party.rate.set(playerId, r);
76
+ if (r.tokens < 1) {
77
+ return false;
78
+ }
79
+ r.tokens -= 1;
80
+ return true;
81
+ }
82
+
83
+ function leaveParty(h, key) {
84
+ const p = h.party;
85
+ const partyId = p.of.get(key);
86
+ const party = partyId ? p.parties.get(partyId) : null;
87
+ p.of.delete(key);
88
+ if (!party) {
89
+ return;
90
+ }
91
+ party.keys.delete(key);
92
+ party.away.delete(key);
93
+ if (party.keys.size < 2) {
94
+ for (const other of party.keys) {
95
+ p.of.delete(other);
96
+ }
97
+ p.parties.delete(partyId);
98
+ p.proposals.delete(partyId);
99
+ }
100
+ }
101
+
102
+ function hidden(d, a, b) {
103
+ return Boolean(d.blockedPairs(d.h)?.get(a)?.has(b));
104
+ }
105
+
106
+ // `d` is the hub layer's own: `{ h, blockedPairs, usernameOf, placeAt, stand,
107
+ // jumpDoors, front }`.
108
+ export function message(d, player, data) {
109
+ const h = d.h;
110
+ const playerId = player?.playerId;
111
+ const me = playerId ? h.players.get(playerId) : null;
112
+ if (!h.sim || h.place.result || !me?.arrived || !spend(h, playerId)) {
113
+ return;
114
+ }
115
+ const p = h.party;
116
+ const now = Date.now();
117
+ const myKey = keyOf(h, playerId);
118
+ switch (data?.op) {
119
+ case 'invite': {
120
+ const otherId = data.player;
121
+ const other = typeof otherId === 'string' && otherId !== playerId ? h.players.get(otherId) : null;
122
+ if (!other?.arrived || !h.sim.players.has(otherId) || hidden(d, playerId, otherId)) {
123
+ return;
124
+ }
125
+ const partyId = p.of.get(myKey);
126
+ if (partyId && (p.of.get(keyOf(h, otherId)) === partyId || p.parties.get(partyId).keys.size >= PARTY_MAX)) {
127
+ return;
128
+ }
129
+ // One invitation waits on a player at a time; a newer one waits its turn.
130
+ const pending = p.invites.get(otherId);
131
+ if (pending && pending.until > now && pending.from !== playerId) {
132
+ return;
133
+ }
134
+ p.invites.set(otherId, { from: playerId, until: now + INVITE_MS });
135
+ return;
136
+ }
137
+ case 'accept': {
138
+ // An answer is to the invitation the player was shown, by its inviter.
139
+ const invite = p.invites.get(playerId);
140
+ if (!invite || invite.from !== data.from) {
141
+ return;
142
+ }
143
+ p.invites.delete(playerId);
144
+ const from = invite.until > now ? invite.from : null;
145
+ if (!from || !h.players.get(from)?.arrived || hidden(d, playerId, from)) {
146
+ return;
147
+ }
148
+ const fromKey = keyOf(h, from);
149
+ let partyId = p.of.get(fromKey);
150
+ if (partyId && p.parties.get(partyId).keys.size >= PARTY_MAX) {
151
+ return;
152
+ }
153
+ leaveParty(h, myKey);
154
+ // Leaving a party of two ends it, which may have been the inviter's too.
155
+ partyId = p.of.get(fromKey);
156
+ if (!partyId) {
157
+ p.seq += 1;
158
+ partyId = `party-${p.seq}`;
159
+ p.parties.set(partyId, { keys: new Set([fromKey]), away: new Map(), heldAt: -Infinity });
160
+ p.of.set(fromKey, partyId);
161
+ }
162
+ p.parties.get(partyId).keys.add(myKey);
163
+ p.of.set(myKey, partyId);
164
+ return;
165
+ }
166
+ case 'decline':
167
+ if (p.invites.get(playerId)?.from === data.from) {
168
+ p.invites.delete(playerId);
169
+ }
170
+ return;
171
+ case 'leave':
172
+ leaveParty(h, myKey);
173
+ return;
174
+ case 'go': {
175
+ // A member picked a door: they stand on its pad, and the rest are asked.
176
+ const partyId = p.of.get(myKey);
177
+ const door = d.jumpDoors(h).find((x) => x.id === data.door);
178
+ if (!partyId || !door) {
179
+ return;
180
+ }
181
+ // The door the party is already going to: a member asked about it has
182
+ // said yes, and the proposer is put back on its pad; no second proposal.
183
+ const going = p.proposals.get(partyId);
184
+ if (going && going.door === door.id && going.until > now) {
185
+ if (going.asked.delete(playerId)) {
186
+ if (toPad(d, playerId, door, going.n)) {
187
+ going.n += 1;
188
+ }
189
+ if (!going.asked.size) {
190
+ p.proposals.delete(partyId);
191
+ }
192
+ } else {
193
+ toPad(d, playerId, door, 0);
194
+ }
195
+ return;
196
+ }
197
+ // A member a block hides from the proposer is not asked, or told of it.
198
+ const others = present(h, partyId).filter((id) => id !== playerId && !hidden(d, playerId, id));
199
+ if (!toPad(d, playerId, door, 0)) {
200
+ return;
201
+ }
202
+ const party = p.parties.get(partyId);
203
+ const holds = now - party.heldAt >= HOLD_EVERY_MS;
204
+ if (holds) {
205
+ party.heldAt = now;
206
+ }
207
+ p.seq += 1;
208
+ p.proposals.set(partyId, {
209
+ id: p.seq,
210
+ door: door.id,
211
+ by: playerId,
212
+ until: now + PROPOSAL_MS,
213
+ holdUntil: holds ? now + PROPOSAL_HOLD_MS : now,
214
+ asked: new Set(others),
215
+ n: 1,
216
+ });
217
+ return;
218
+ }
219
+ case 'go-yes':
220
+ case 'go-no': {
221
+ const partyId = p.of.get(myKey);
222
+ const proposal = partyId ? p.proposals.get(partyId) : null;
223
+ // An answer is to the proposal the player was shown, by its id.
224
+ if (!proposal || proposal.id !== data.id || proposal.until <= now || !proposal.asked.delete(playerId)) {
225
+ return;
226
+ }
227
+ const door = d.jumpDoors(h).find((x) => x.id === proposal.door);
228
+ if (data.op === 'go-yes' && door && toPad(d, playerId, door, proposal.n)) {
229
+ proposal.n += 1;
230
+ }
231
+ if (!proposal.asked.size) {
232
+ p.proposals.delete(partyId);
233
+ }
234
+ return;
235
+ }
236
+ default:
237
+ }
238
+ }
239
+
240
+ // Put a player on a door's pad (its `HubPad` actor), the `n`th of a ring
241
+ // about its middle, facing the door; in front of a door with no pad, as a
242
+ // jump to it does. A sitter stands first; a player the place holds stays.
243
+ export function toPad(d, playerId, door, n) {
244
+ const h = d.h;
245
+ d.stand(h, playerId);
246
+ if (h.held.has(playerId)) {
247
+ return false;
248
+ }
249
+ const facing = ((door.t.rotationY ?? 0) + 180) * DEG;
250
+ const at = padPoint(h, door, n) ?? d.front(door);
251
+ d.placeAt(h, playerId, { x: at.x, y: 1.2, z: at.z }, facing);
252
+ h.session?.send(playerId, { k: 'hub-jumped', facing });
253
+ return true;
254
+ }
255
+
256
+ // Where the `n`th player sent to a door stands: on its pad, a ring about the
257
+ // middle, or null for a door with no pad.
258
+ export function padPoint(h, door, n) {
259
+ const pad = h.actors.find((a) => a.components.HubPad?.door === door.id && a.components.Transform && !h.hidden.has(a.id));
260
+ if (!pad) {
261
+ return null;
262
+ }
263
+ const t = pad.components.Transform;
264
+ const radius = Math.min(PAD_RING_M, (pad.components.HubPad.radius ?? pad.components.Shape?.radius ?? 2.3) * 0.5);
265
+ const angle = (n * 2 * Math.PI) / 6 + (door.t.rotationY ?? 0) * DEG;
266
+ return { x: t.x + Math.sin(angle) * radius, z: t.z + Math.cos(angle) * radius };
267
+ }
268
+
269
+ // Each tick: what has run out goes, and each player in a town is told their
270
+ // party, the invitation waiting on them and the door proposed to them, when
271
+ // any of it changed.
272
+ export function tick(d) {
273
+ const h = d.h;
274
+ const p = h.party;
275
+ if (!h.sim || h.place.result) {
276
+ return;
277
+ }
278
+ const now = Date.now();
279
+ for (const [playerId, invite] of p.invites) {
280
+ if (invite.until <= now || !h.players.has(playerId) || !h.players.has(invite.from)) {
281
+ p.invites.delete(playerId);
282
+ }
283
+ }
284
+ for (const [partyId, proposal] of p.proposals) {
285
+ if (proposal.until <= now || !p.parties.has(partyId)) {
286
+ p.proposals.delete(partyId);
287
+ }
288
+ }
289
+ const here = new Set();
290
+ for (const [playerId, entry] of h.players) {
291
+ if (entry.arrived) {
292
+ here.add(keyOf(h, playerId));
293
+ }
294
+ }
295
+ for (const party of p.parties.values()) {
296
+ for (const key of [...party.keys]) {
297
+ if (here.has(key)) {
298
+ party.away.delete(key);
299
+ } else if (!key.startsWith('u:')) {
300
+ // A guest is a member only while here.
301
+ leaveParty(h, key);
302
+ } else if (!party.away.has(key)) {
303
+ party.away.set(key, now);
304
+ } else if (now - party.away.get(key) > AWAY_MS) {
305
+ leaveParty(h, key);
306
+ }
307
+ }
308
+ }
309
+ for (const [playerId, entry] of h.players) {
310
+ if (!entry.arrived) {
311
+ continue;
312
+ }
313
+ const partyId = p.of.get(keyOf(h, playerId));
314
+ const invite = p.invites.get(playerId);
315
+ const proposal = partyId ? p.proposals.get(partyId) : null;
316
+ const state = {
317
+ // The members here, this player left out; a block hides a member's name.
318
+ members: partyId ? present(h, partyId).filter((id) => id !== playerId && !hidden(d, playerId, id)) : null,
319
+ away: partyId ? p.parties.get(partyId).away.size : 0,
320
+ invite: invite ? { from: invite.from, username: d.usernameOf(h, invite.from) } : null,
321
+ // Only the proposer and the members asked hear of a proposal.
322
+ proposal: proposal && (proposal.by === playerId || proposal.asked.has(playerId))
323
+ ? {
324
+ id: proposal.id,
325
+ door: proposal.door,
326
+ by: proposal.by,
327
+ username: d.usernameOf(h, proposal.by),
328
+ asked: proposal.asked.has(playerId),
329
+ }
330
+ : null,
331
+ };
332
+ const json = JSON.stringify(state);
333
+ if (json !== (p.sent.get(playerId) ?? 'null') && !(json === EMPTY && !p.sent.has(playerId))) {
334
+ p.sent.set(playerId, json);
335
+ h.session?.send(playerId, { k: 'hub-party', ...state });
336
+ }
337
+ }
338
+ }
339
+
340
+ const EMPTY = JSON.stringify({ members: null, away: 0, invite: null, proposal: null });
341
+
342
+ // `hub.coming(doorId)`: how many party members were asked to a door and have
343
+ // not answered yet, so a place may hold its pad's countdown for them. A
344
+ // party holds a pad this way for at most 20 s in any minute.
345
+ export function coming(h, doorId) {
346
+ let n = 0;
347
+ const now = Date.now();
348
+ for (const proposal of h.party.proposals.values()) {
349
+ if (proposal.door === doorId && proposal.holdUntil > now) {
350
+ n += proposal.asked.size;
351
+ }
352
+ }
353
+ return n;
354
+ }
355
+
356
+ // Whether two players here are in one party (Join goes to a party member).
357
+ export function together(h, a, b) {
358
+ const partyId = h.party.of.get(keyOf(h, a));
359
+ return Boolean(partyId) && partyId === h.party.of.get(keyOf(h, b));
360
+ }
361
+
362
+ // A player left the session. A guest leaves their party with them; a
363
+ // signed-in member is kept as away (`tick`). Called while the player is known.
364
+ export function forget(h, playerId) {
365
+ const p = h.party;
366
+ const key = keyOf(h, playerId);
367
+ if (!key.startsWith('u:')) {
368
+ leaveParty(h, key);
369
+ }
370
+ p.invites.delete(playerId);
371
+ for (const [id, invite] of p.invites) {
372
+ if (invite.from === playerId) {
373
+ p.invites.delete(id);
374
+ }
375
+ }
376
+ for (const proposal of p.proposals.values()) {
377
+ proposal.asked.delete(playerId);
378
+ }
379
+ p.rate.delete(playerId);
380
+ p.sent.delete(playerId);
381
+ }
@@ -0,0 +1,43 @@
1
+ // Which place a session runs, from the session alone (contract §2). A client
2
+ // never names it: the M0 town let the first client pick the scene, which let
3
+ // any client start a session of any of the deck's scenes.
4
+ //
5
+ // 1. `session.place`, stamped by the host from the door that placed the
6
+ // session (ghost's door table). No production host stamps it yet.
7
+ // 2. On the dev serve only, the named key in the session id:
8
+ // `dev:hub.<place>.<instance>`, the key `hub.go` mints (behind a
9
+ // `deck:<name>/` prefix when the door leads to another local deck). A `dev:` id exists
10
+ // only on `castle-web serve`, where every client is the author's own; a
11
+ // production named key is a client's choice and is never read.
12
+ // 3. Otherwise the deck's main place.
13
+
14
+ import { MAIN_PLACE } from './manifest.js';
15
+
16
+ // A door to another local deck runs that deck's server under
17
+ // `deck:<name>/hub.<place>.<instance>` (the serve's devDecks, content-pack.md).
18
+ const DEV_KEY = /^dev:(?:deck:[A-Za-z0-9_-]{1,40}\/)?hub\.([a-z][a-z0-9-]{0,23})\.[A-Za-z0-9-]{1,40}$/;
19
+
20
+ // `{ id, source }`, `id` always a place the manifest has.
21
+ export function resolvePlace(session, manifest) {
22
+ const stamped = typeof session?.place === 'string' ? session.place : null;
23
+ if (stamped) {
24
+ if (manifest.places[stamped]) {
25
+ return { id: stamped, source: 'host' };
26
+ }
27
+ console.warn(`[hub] the host placed this session at "${stamped}", which this deck does not declare`);
28
+ return { id: MAIN_PLACE, source: 'fallback' };
29
+ }
30
+ const dev = DEV_KEY.exec(String(session?.sessionId ?? ''));
31
+ if (dev) {
32
+ if (manifest.places[dev[1]]) {
33
+ return { id: dev[1], source: 'dev-key' };
34
+ }
35
+ console.warn(`[hub] dev session key names "${dev[1]}", which this deck does not declare`);
36
+ }
37
+ return { id: MAIN_PLACE, source: 'default' };
38
+ }
39
+
40
+ // The named key `hub.go` joins in dev: the place, then a fresh instance.
41
+ export function devKey(placeId) {
42
+ return `hub.${placeId}.${Math.random().toString(36).slice(2, 10)}`;
43
+ }
@@ -0,0 +1,83 @@
1
+ // Quick reactions and phrases (contract §6, v1.11): what every player can say
2
+ // without typing a word. A reaction is one of the kit's emoji; a phrase is one
3
+ // of the kit's canned lines, named by id. The words are the kit's, written on
4
+ // each client from the id, so nothing a player types is ever carried. Shown
5
+ // as a bubble over the speaker for everyone here who may see them, never
6
+ // across a block.
7
+
8
+ // The emoji a reaction may be, by index.
9
+ export const REACTIONS = ['👍', '😂', '❤️', '😮', '👋', '🎉'];
10
+ // The phrases, by id: where each may be said (`town`, a place with no
11
+ // `result`; `game`, one with) and whether it names a door.
12
+ export const PHRASES = {
13
+ here: { town: true, game: true },
14
+ play: { town: true, game: false, door: true },
15
+ wait: { town: true, game: true },
16
+ yes: { town: true, game: true },
17
+ no: { town: true, game: true },
18
+ gg: { town: false, game: true },
19
+ again: { town: false, game: true },
20
+ back: { town: false, game: true },
21
+ };
22
+ // One player's: a burst of three, then one every 1.5 s.
23
+ const BURST = 3;
24
+ const EVERY_MS = 1500;
25
+ // "Let's play <door>" names the door the speaker stands within this of.
26
+ const PLAY_NEAR_M = 12;
27
+
28
+ export function newSayState() {
29
+ return { sayRate: new Map() };
30
+ }
31
+
32
+ // `d` is the hub layer's own: `{ h, blockedPairs, jumpDoors }`.
33
+ export function message(d, player, data) {
34
+ const h = d.h;
35
+ const playerId = player?.playerId;
36
+ const body = playerId ? h.sim?.players.get(playerId) : null;
37
+ if (!body || !h.players.get(playerId)?.arrived) {
38
+ return;
39
+ }
40
+ const game = Boolean(h.place.result);
41
+ const out = { k: 'hub-said', player: playerId };
42
+ if (Number.isInteger(data?.r) && data.r >= 0 && data.r < REACTIONS.length) {
43
+ out.r = data.r;
44
+ } else if (typeof data?.p === 'string' && Object.hasOwn(PHRASES, data.p) && PHRASES[data.p][game ? 'game' : 'town']) {
45
+ out.p = data.p;
46
+ if (PHRASES[data.p].door) {
47
+ // The door they stand by, if any: the client words it from its own list.
48
+ let best = null;
49
+ for (const door of d.jumpDoors(h)) {
50
+ const dist = Math.hypot(body.at.x - door.t.x, body.at.z - door.t.z);
51
+ if (dist <= PLAY_NEAR_M && (!best || dist < best.dist)) {
52
+ best = { dist, id: door.id };
53
+ }
54
+ }
55
+ out.door = best?.id ?? null;
56
+ }
57
+ } else {
58
+ return;
59
+ }
60
+ const now = Date.now();
61
+ const r = h.sayRate.get(playerId) ?? { tokens: BURST, at: now };
62
+ r.tokens = Math.min(BURST, r.tokens + (now - r.at) / EVERY_MS);
63
+ r.at = now;
64
+ h.sayRate.set(playerId, r);
65
+ if (r.tokens < 1) {
66
+ return;
67
+ }
68
+ r.tokens -= 1;
69
+ const hidden = d.blockedPairs(h)?.get(playerId);
70
+ if (!hidden?.size) {
71
+ h.session?.broadcast(out);
72
+ return;
73
+ }
74
+ for (const id of h.sim.players.keys()) {
75
+ if (!hidden.has(id)) {
76
+ h.session?.send(id, out);
77
+ }
78
+ }
79
+ }
80
+
81
+ export function forget(h, playerId) {
82
+ h.sayRate.delete(playerId);
83
+ }
@@ -0,0 +1,142 @@
1
+ // Wearables (contract §11, v1.4): one small cosmetic on a player's platform
2
+ // avatar, `{ kind, color, rarity }`, drawn by the hub system over the avatar in
3
+ // every hub place. A game puts one on with `hub.wear(playerId, spec | null)`;
4
+ // where the host has hub ops it goes to ghost too, which keeps it on the
5
+ // account (only from a curated place) and hands it back to every place on the
6
+ // arrival read. On the dev serve it lasts the session.
7
+ //
8
+ // The snapshot's `hubWear` is playerId -> wearable for helloed players who
9
+ // wear one, without anyone a block hides from someone here: theirs go to each
10
+ // player who may see them alone (`hub-wear`), as their avatars do (`hub-av`).
11
+
12
+ import { warnOnce } from './actors.js';
13
+
14
+ export const KIND = /^[a-z][a-z0-9-]{0,23}$/;
15
+ const COLOR = /^#[0-9a-f]{6}$/i;
16
+ export const MAX_RARITY = 9;
17
+
18
+ export function newWearState() {
19
+ return {
20
+ // playerId -> { kind, color, rarity }
21
+ wearing: new Map(),
22
+ // playerId -> how many times the game changed it, so a read that began
23
+ // before the change does not undo it (`applyRead`)
24
+ wearRev: new Map(),
25
+ // playerId -> JSON of what ghost last heard or said, so the same
26
+ // wearable is not sent again
27
+ wearSent: new Map(),
28
+ // playerId -> JSON of the wearables last sent to that player alone
29
+ sentWearMe: new Map(),
30
+ };
31
+ }
32
+
33
+ // A wearable as ghost takes it, `null` to clear, or `undefined` when it is
34
+ // not one. Unknown keys are refused, as ghost refuses them.
35
+ export function cleanWearable(spec) {
36
+ if (spec === null) {
37
+ return null;
38
+ }
39
+ if (!spec || typeof spec !== 'object' || Array.isArray(spec)) {
40
+ return undefined;
41
+ }
42
+ if (Object.keys(spec).some((key) => key !== 'kind' && key !== 'color' && key !== 'rarity')) {
43
+ return undefined;
44
+ }
45
+ const { kind, color, rarity } = spec;
46
+ if (typeof kind !== 'string' || !KIND.test(kind) || typeof color !== 'string' || !COLOR.test(color)) {
47
+ return undefined;
48
+ }
49
+ if (!Number.isInteger(rarity) || rarity < 0 || rarity > MAX_RARITY) {
50
+ return undefined;
51
+ }
52
+ return { kind, color: color.toLowerCase(), rarity };
53
+ }
54
+
55
+ // `hub.wear(playerId, spec | null)`: true when it took. `platform` is the
56
+ // host's hub ops or null; `userId` the player's, or null for a guest, whose
57
+ // wearable is this session's alone; `refused` logs a refusal once.
58
+ export function wear(h, playerId, spec, { platform, userId, refused }) {
59
+ if (typeof playerId !== 'string' || !(h.players.has(playerId) || h.sim?.players.has(playerId))) {
60
+ warnOnce(h, `wear:${playerId}`, `hub.wear: "${playerId}" is not a player here`);
61
+ return false;
62
+ }
63
+ const clean = cleanWearable(spec === undefined ? null : spec);
64
+ if (clean === undefined) {
65
+ warnOnce(h, 'wear:spec', 'hub.wear: a wearable is { kind, color: "#rrggbb", rarity: 0-9 } or null');
66
+ return false;
67
+ }
68
+ if (clean) {
69
+ h.wearing.set(playerId, clean);
70
+ } else {
71
+ h.wearing.delete(playerId);
72
+ }
73
+ h.wearRev.set(playerId, (h.wearRev.get(playerId) ?? 0) + 1);
74
+ const json = JSON.stringify(clean);
75
+ if (platform && userId && typeof platform.wear === 'function' && h.wearSent.get(playerId) !== json) {
76
+ h.wearSent.set(playerId, json);
77
+ platform.wear(playerId, clean).catch((err) => {
78
+ if (h.wearSent.get(playerId) === json) {
79
+ h.wearSent.delete(playerId);
80
+ }
81
+ refused(err);
82
+ });
83
+ }
84
+ return true;
85
+ }
86
+
87
+ // Ghost's word on a player's wearable, from the arrival read: kept unless the
88
+ // game set one since the read began. A ghost older than wearables says
89
+ // nothing, which changes nothing.
90
+ export function applyRead(h, playerId, wearRev, read) {
91
+ if (!read || !('wearable' in read) || (h.wearRev.get(playerId) ?? 0) !== wearRev) {
92
+ return;
93
+ }
94
+ const clean = cleanWearable(read.wearable ?? null);
95
+ if (clean === undefined) {
96
+ return;
97
+ }
98
+ if (clean) {
99
+ h.wearing.set(playerId, clean);
100
+ } else {
101
+ h.wearing.delete(playerId);
102
+ }
103
+ h.wearSent.set(playerId, JSON.stringify(clean));
104
+ }
105
+
106
+ // Everyone's wearables, for everyone: helloed players only, without the ones
107
+ // a block hides from someone here.
108
+ export function wearingOf(h, hidden) {
109
+ const out = {};
110
+ for (const [playerId, wearable] of h.wearing) {
111
+ if (h.players.get(playerId)?.hello && !hidden?.has(playerId)) {
112
+ out[playerId] = wearable;
113
+ }
114
+ }
115
+ return out;
116
+ }
117
+
118
+ // The wearables of players in a hidden pair, each to the players who may see
119
+ // them, when they change. A session with no blocks sends nothing here.
120
+ export function sendOverrides(h, hidden) {
121
+ for (const playerId of h.sim.players.keys()) {
122
+ const out = {};
123
+ for (const shrouded of hidden?.keys() ?? []) {
124
+ const wearable = h.wearing.get(shrouded);
125
+ if (wearable && h.players.get(shrouded)?.hello && !hidden.get(playerId)?.has(shrouded)) {
126
+ out[shrouded] = wearable;
127
+ }
128
+ }
129
+ const json = JSON.stringify(out);
130
+ if ((h.sentWearMe.get(playerId) ?? '{}') !== json) {
131
+ h.sentWearMe.set(playerId, json);
132
+ h.session?.send(playerId, { k: 'hub-wear', hubWear: out });
133
+ }
134
+ }
135
+ }
136
+
137
+ export function forget(h, playerId) {
138
+ h.wearing.delete(playerId);
139
+ h.wearRev.delete(playerId);
140
+ h.wearSent.delete(playerId);
141
+ h.sentWearMe.delete(playerId);
142
+ }