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.
- package/dist/agent-prompts.d.ts +2 -0
- package/dist/agent-prompts.js +16 -8
- package/dist/agent.d.ts +2 -0
- package/dist/agent.js +21 -21
- package/dist/api.d.ts +28 -2
- package/dist/api.js +74 -16
- package/dist/castle-host/host.d.ts +91 -0
- package/dist/castle-host/host.js +779 -3
- package/dist/castleJson.d.ts +44 -0
- package/dist/castleJson.js +131 -0
- package/dist/check.js +27 -1
- package/dist/contentPack.d.ts +49 -0
- package/dist/contentPack.js +273 -0
- package/dist/devExperiments.d.ts +21 -0
- package/dist/devExperiments.js +139 -0
- package/dist/devHub.d.ts +55 -0
- package/dist/devHub.js +187 -0
- package/dist/devSessionChild.js +3 -1
- package/dist/devSessionServer.d.ts +14 -1
- package/dist/devSessionServer.js +229 -44
- package/dist/experimentAssignment.d.ts +15 -0
- package/dist/experimentAssignment.js +41 -0
- package/dist/importCheckHook.d.ts +40 -0
- package/dist/importCheckHook.js +171 -0
- package/dist/importRegression.d.ts +15 -0
- package/dist/importRegression.js +100 -0
- package/dist/importTrust.d.ts +2 -0
- package/dist/importTrust.js +27 -0
- package/dist/importUpdateHook.d.ts +18 -0
- package/dist/importUpdateHook.js +31 -8
- package/dist/importUpdateHookRunner.js +19 -7
- package/dist/imports.d.ts +16 -0
- package/dist/imports.js +141 -31
- package/dist/index.js +3 -1
- package/dist/init.d.ts +3 -0
- package/dist/init.js +125 -8
- package/dist/install.js +4 -1
- package/dist/localPaths.js +4 -0
- package/dist/login.js +2 -2
- package/dist/platformDoc.js +3 -8
- package/dist/playPagePolicy.js +3 -1
- package/dist/remix.js +5 -0
- package/dist/routerTaskReport.d.ts +28 -0
- package/dist/routerTaskReport.js +56 -0
- package/dist/save-deck.d.ts +18 -0
- package/dist/save-deck.js +142 -6
- package/dist/serve.d.ts +8 -0
- package/dist/serve.js +94 -13
- package/dist/serverBundle.d.ts +1 -1
- package/dist/serverBundle.js +5 -1
- package/dist/webSourceSync.d.ts +2 -0
- package/dist/webSourceSync.js +2 -0
- package/kits/base/CLAUDE.md +2 -3
- package/kits/base/castle.json +1 -1
- package/kits/base/sdk/README.md +258 -52
- package/kits/base/sdk/agent.js +4 -4
- package/kits/base/sdk/avatar.d.ts +12 -0
- package/kits/base/sdk/avatar.js +23 -0
- package/kits/base/sdk/castle.d.ts +5 -0
- package/kits/base/sdk/castle.js +3 -0
- package/kits/base/sdk/commands.d.ts +208 -0
- package/kits/base/sdk/commands.js +16 -0
- package/kits/base/sdk/context.d.ts +6 -0
- package/kits/base/sdk/experiments.d.ts +15 -0
- package/kits/base/sdk/experiments.js +83 -0
- package/kits/base/sdk/hub.d.ts +85 -0
- package/kits/base/sdk/hub.js +107 -0
- package/kits/base/sdk/multiplayer.d.ts +45 -1
- package/kits/base/sdk/multiplayer.js +106 -4
- package/kits/base/sdk/server/hub.d.ts +169 -0
- package/kits/base/sdk/server/hub.js +125 -0
- package/kits/base/sdk/server/platformHandle.d.ts +42 -1
- package/kits/base/sdk/server/storage.d.ts +55 -2
- package/kits/base/sdk/server/wrapper.d.ts +32 -2
- package/kits/base/sdk/server/wrapper.js +90 -11
- package/kits/base/sdk/transport.d.ts +2 -1
- package/kits/base/sdk/transport.js +13 -1
- package/kits/hub-game/.prettierrc +5 -0
- package/kits/hub-game/CLAUDE.md +146 -0
- package/kits/hub-game/blueprints/avatar-0.scene +28 -0
- package/kits/hub-game/blueprints/avatar-1.scene +28 -0
- package/kits/hub-game/blueprints/avatar-2.scene +28 -0
- package/kits/hub-game/blueprints/avatar-3.scene +28 -0
- package/kits/hub-game/blueprints/bean/avatar-0.scene +28 -0
- package/kits/hub-game/blueprints/bean/avatar-1.scene +28 -0
- package/kits/hub-game/blueprints/bean/avatar-2.scene +28 -0
- package/kits/hub-game/blueprints/bean/avatar-3.scene +28 -0
- package/kits/hub-game/blueprints/blocky/avatar-0.scene +28 -0
- package/kits/hub-game/blueprints/blocky/avatar-1.scene +28 -0
- package/kits/hub-game/blueprints/blocky/avatar-2.scene +28 -0
- package/kits/hub-game/blueprints/blocky/avatar-3.scene +28 -0
- package/kits/hub-game/blueprints/pixel/avatar-0.scene +28 -0
- package/kits/hub-game/blueprints/pixel/avatar-1.scene +28 -0
- package/kits/hub-game/blueprints/pixel/avatar-2.scene +28 -0
- package/kits/hub-game/blueprints/pixel/avatar-3.scene +28 -0
- package/kits/hub-game/castle.json +96 -0
- package/kits/hub-game/code/client/attention.js +133 -0
- package/kits/hub-game/code/client/avatar.js +288 -0
- package/kits/hub-game/code/client/cabinets.js +90 -0
- package/kits/hub-game/code/client/chat.js +135 -0
- package/kits/hub-game/code/client/chrome.js +1001 -0
- package/kits/hub-game/code/client/doorFrame.js +15 -0
- package/kits/hub-game/code/client/firstJoin.js +14 -0
- package/kits/hub-game/code/client/grab.js +241 -0
- package/kits/hub-game/code/client/hookLayer.js +230 -0
- package/kits/hub-game/code/client/idle.js +326 -0
- package/kits/hub-game/code/client/keys.js +48 -0
- package/kits/hub-game/code/client/pack.js +232 -0
- package/kits/hub-game/code/client/packFormat.js +397 -0
- package/kits/hub-game/code/client/say.js +127 -0
- package/kits/hub-game/code/client/sha256.js +80 -0
- package/kits/hub-game/code/client/sound.js +147 -0
- package/kits/hub-game/code/client/state.js +131 -0
- package/kits/hub-game/code/client/talk.js +259 -0
- package/kits/hub-game/code/client/together.js +365 -0
- package/kits/hub-game/code/client/wear.js +149 -0
- package/kits/hub-game/code/server/actors.js +627 -0
- package/kits/hub-game/code/server/blocks.js +103 -0
- package/kits/hub-game/code/server/cabinets.js +309 -0
- package/kits/hub-game/code/server/game.js +137 -0
- package/kits/hub-game/code/server/grab.js +247 -0
- package/kits/hub-game/code/server/history.js +96 -0
- package/kits/hub-game/code/server/hub.js +1904 -0
- package/kits/hub-game/code/server/idle.js +412 -0
- package/kits/hub-game/code/server/index.js +64 -0
- package/kits/hub-game/code/server/labels.js +59 -0
- package/kits/hub-game/code/server/manifest.js +527 -0
- package/kits/hub-game/code/server/npcs.js +745 -0
- package/kits/hub-game/code/server/party.js +381 -0
- package/kits/hub-game/code/server/place.js +43 -0
- package/kits/hub-game/code/server/say.js +83 -0
- package/kits/hub-game/code/server/wear.js +142 -0
- package/kits/hub-game/code/systems/hub.js +2230 -0
- package/kits/hub-game/docs/content-pack.md +151 -0
- package/kits/hub-game/docs/contract.md +1139 -0
- package/kits/hub-game/docs/style.md +137 -0
- package/kits/hub-game/drawings/blocky/castle-eye.sprite +58 -0
- package/kits/hub-game/drawings/blocky/dash.sprite +59 -0
- package/kits/hub-game/drawings/blocky/pom.sprite +59 -0
- package/kits/hub-game/drawings/blocky/tabby.sprite +59 -0
- package/kits/hub-game/drawings/pixel/castle-arm.sprite +13 -0
- package/kits/hub-game/drawings/pixel/castle-body.sprite +23 -0
- package/kits/hub-game/drawings/pixel/castle-leg.sprite +12 -0
- package/kits/hub-game/drawings/pixel/kai-arm.sprite +14 -0
- package/kits/hub-game/drawings/pixel/kai-head.sprite +24 -0
- package/kits/hub-game/drawings/pixel/kai-leg.sprite +15 -0
- package/kits/hub-game/drawings/pixel/kai-torso.sprite +18 -0
- package/kits/hub-game/drawings/pixel/mimi-arm.sprite +13 -0
- package/kits/hub-game/drawings/pixel/mimi-head.sprite +23 -0
- package/kits/hub-game/drawings/pixel/mimi-leg.sprite +15 -0
- package/kits/hub-game/drawings/pixel/mimi-torso.sprite +18 -0
- package/kits/hub-game/drawings/pixel/nox-arm.sprite +13 -0
- package/kits/hub-game/drawings/pixel/nox-head.sprite +21 -0
- package/kits/hub-game/drawings/pixel/nox-leg.sprite +14 -0
- package/kits/hub-game/drawings/pixel/nox-torso.sprite +18 -0
- package/kits/hub-game/eslint.config.js +20 -0
- package/kits/hub-game/fonts.generated.js +9 -0
- package/kits/hub-game/index.html +22 -0
- package/kits/hub-game/main.jsx +6 -0
- package/kits/hub-game/models/avatar-0.pxmodel +21 -0
- package/kits/hub-game/models/avatar-1.pxmodel +24 -0
- package/kits/hub-game/models/avatar-2.pxmodel +27 -0
- package/kits/hub-game/models/avatar-3.pxmodel +22 -0
- package/kits/hub-game/models/bean/avatar-0.pxmodel +19 -0
- package/kits/hub-game/models/bean/avatar-1.pxmodel +24 -0
- package/kits/hub-game/models/bean/avatar-2.pxmodel +22 -0
- package/kits/hub-game/models/bean/avatar-3.pxmodel +24 -0
- package/kits/hub-game/models/blocky/avatar-0.pxmodel +21 -0
- package/kits/hub-game/models/blocky/avatar-1.pxmodel +30 -0
- package/kits/hub-game/models/blocky/avatar-2.pxmodel +28 -0
- package/kits/hub-game/models/blocky/avatar-3.pxmodel +27 -0
- package/kits/hub-game/models/pixel/avatar-0.pxmodel +24 -0
- package/kits/hub-game/models/pixel/avatar-1.pxmodel +16 -0
- package/kits/hub-game/models/pixel/avatar-2.pxmodel +19 -0
- package/kits/hub-game/models/pixel/avatar-3.pxmodel +23 -0
- package/kits/hub-game/package-lock.json +365 -0
- package/kits/hub-game/package.json +32 -0
- package/kits/hub-game/scenes/main.scene +318 -0
- package/kits/hub-game/scripts/check.mjs +339 -0
- package/kits/hub-game/scripts/fixtures/sdk-stub.mjs +3 -0
- package/kits/hub-game/scripts/gen-avatars-bean.mjs +748 -0
- package/kits/hub-game/scripts/gen-avatars-blocky.mjs +607 -0
- package/kits/hub-game/scripts/gen-avatars-pixel.mjs +771 -0
- package/kits/hub-game/scripts/gen-avatars.mjs +621 -0
- package/kits/hub-game/scripts/pack.mjs +192 -0
- package/kits/hub-game/scripts/test-actors.mjs +666 -0
- package/kits/hub-game/scripts/test-attention.mjs +103 -0
- package/kits/hub-game/scripts/test-avatar.mjs +472 -0
- package/kits/hub-game/scripts/test-ban-join.mjs +194 -0
- package/kits/hub-game/scripts/test-blocks.mjs +351 -0
- package/kits/hub-game/scripts/test-chat.mjs +98 -0
- package/kits/hub-game/scripts/test-classic.mjs +590 -0
- package/kits/hub-game/scripts/test-door-jump.mjs +131 -0
- package/kits/hub-game/scripts/test-door.mjs +56 -0
- package/kits/hub-game/scripts/test-grab.mjs +331 -0
- package/kits/hub-game/scripts/test-hub-ops.mjs +835 -0
- package/kits/hub-game/scripts/test-hub-trip.mjs +1310 -0
- package/kits/hub-game/scripts/test-idle.mjs +518 -0
- package/kits/hub-game/scripts/test-interest.mjs +413 -0
- package/kits/hub-game/scripts/test-keys.mjs +50 -0
- package/kits/hub-game/scripts/test-manifest.mjs +224 -0
- package/kits/hub-game/scripts/test-no-forms.mjs +16 -0
- package/kits/hub-game/scripts/test-npcs.mjs +714 -0
- package/kits/hub-game/scripts/test-pack.mjs +424 -0
- package/kits/hub-game/scripts/test-party.mjs +286 -0
- package/kits/hub-game/scripts/test-say.mjs +149 -0
- package/kits/hub-game/scripts/test-together.mjs +183 -0
- package/kits/hub-game/theme.style +3 -0
- package/kits/multiplayer-3d/castle.json +5 -5
- package/kits/multiplayer-3d/code/client/avatars.js +101 -1
- package/kits/multiplayer-3d/code/server/players.js +4 -1
- package/kits/multiplayer-3d/code/server/sceneBodies.js +27 -6
- package/kits/multiplayer-3d/code/server/world.js +20 -7
- package/kits/multiplayer-3d/code/systems/multiplayer.js +222 -11
- package/kits/physics-2d/castle.json +2 -2
- package/kits/physics-2d/engine/art.js +13 -0
- package/kits/physics-2d/engine/assets.js +4 -2
- package/kits/physics-2d/engine/scene.js +25 -5
- package/kits/physics-3d/CLAUDE.md +6 -1
- package/kits/physics-3d/castle.json +3 -3
- package/kits/physics-3d/engine3d/Scene3DPlayer.jsx +32 -4
- package/kits/physics-3d/engine3d/artobjects.js +2 -2
- package/kits/physics-3d/engine3d/materials.js +29 -1
- package/kits/physics-3d/engine3d/world3d.js +65 -1
- package/kits/physics-3d/systems/physics3d.js +5 -2
- package/kits/real-time/castle.json +2 -2
- package/kits/real-time/code/client/connection.js +77 -7
- package/kits/real-time/code/client/gameHooks.js +63 -3
- package/kits/real-time/code/client/messages.js +7 -0
- package/kits/real-time/code/server/gameHooks.js +24 -0
- package/kits/real-time/code/server/session.js +390 -22
- package/package.json +2 -2
package/dist/castle-host/host.js
CHANGED
|
@@ -31,13 +31,27 @@ const COMMAND_NAMES = [
|
|
|
31
31
|
"leaderboard.show",
|
|
32
32
|
"user.getCurrent",
|
|
33
33
|
"user.get",
|
|
34
|
+
"experiments.get",
|
|
34
35
|
"time.getServerTime",
|
|
35
36
|
"pass.has",
|
|
37
|
+
"avatar.get",
|
|
38
|
+
"avatar.set",
|
|
36
39
|
"pass.offer",
|
|
37
40
|
"snapshot.offer",
|
|
38
41
|
"portal.open",
|
|
39
42
|
"portal.prefetch",
|
|
40
43
|
"portal.info",
|
|
44
|
+
"hub.go",
|
|
45
|
+
"hub.back",
|
|
46
|
+
"hub.doors",
|
|
47
|
+
"hub.friends",
|
|
48
|
+
"hub.jump",
|
|
49
|
+
"hub.report",
|
|
50
|
+
"hub.chat",
|
|
51
|
+
"hub.say",
|
|
52
|
+
"hub.reportChat",
|
|
53
|
+
"hub.commit",
|
|
54
|
+
"hub.cancel",
|
|
41
55
|
"haptics.play",
|
|
42
56
|
"multiplayer.getSession",
|
|
43
57
|
"multiplayer.getJoinHint",
|
|
@@ -135,12 +149,24 @@ export async function executeCommand(ctx, command, params, capabilities, options
|
|
|
135
149
|
}
|
|
136
150
|
try {
|
|
137
151
|
const data = await runCommand(ctx, command, (params ?? {}), capabilities, options);
|
|
152
|
+
noteAcquiredSession(ctx, command, data, capabilities);
|
|
138
153
|
return { ok: true, data };
|
|
139
154
|
}
|
|
140
155
|
catch (error) {
|
|
141
156
|
return { ok: false, error: toSerializedError(error, command) };
|
|
142
157
|
}
|
|
143
158
|
}
|
|
159
|
+
// A door's source is the session the deck last acquired in its place.
|
|
160
|
+
function noteAcquiredSession(ctx, command, data, caps) {
|
|
161
|
+
if (!caps.hub?.sessionAcquired ||
|
|
162
|
+
(command !== "multiplayer.getSession" && command !== "multiplayer.joinDestination")) {
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
const result = data;
|
|
166
|
+
if (result?.status === "ok" && typeof result.sessionId === "string" && result.sessionId) {
|
|
167
|
+
caps.hub.sessionAcquired(result.sessionId, ctx);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
144
170
|
function runCommand(ctx, command, params, caps, options) {
|
|
145
171
|
if (command === "multiplayer.getSession" && caps.getMultiplayerSession) {
|
|
146
172
|
return caps.getMultiplayerSession(multiplayerSessionRequest(params), ctx);
|
|
@@ -181,12 +207,31 @@ function runCommand(ctx, command, params, caps, options) {
|
|
|
181
207
|
return Promise.resolve(userGetCurrent(ctx));
|
|
182
208
|
case "user.get":
|
|
183
209
|
return userGet(params, gql);
|
|
210
|
+
case "experiments.get":
|
|
211
|
+
return Promise.resolve(experimentsGet(ctx));
|
|
184
212
|
case "portal.info":
|
|
185
213
|
return portalInfo(params, gql);
|
|
214
|
+
// Dormant `unavailable` on a host without `caps.hub`.
|
|
215
|
+
case "hub.go":
|
|
216
|
+
case "hub.back":
|
|
217
|
+
case "hub.doors":
|
|
218
|
+
case "hub.friends":
|
|
219
|
+
case "hub.jump":
|
|
220
|
+
case "hub.report":
|
|
221
|
+
case "hub.chat":
|
|
222
|
+
case "hub.say":
|
|
223
|
+
case "hub.reportChat":
|
|
224
|
+
case "hub.commit":
|
|
225
|
+
case "hub.cancel":
|
|
226
|
+
return runHubCommand(ctx, command, params, caps, options.signal);
|
|
186
227
|
case "time.getServerTime":
|
|
187
228
|
return timeGetServerTime(gql);
|
|
188
229
|
case "pass.has":
|
|
189
230
|
return passHas(ctx, params, caps);
|
|
231
|
+
case "avatar.get":
|
|
232
|
+
return avatarGet(gql);
|
|
233
|
+
case "avatar.set":
|
|
234
|
+
return avatarSet(ctx, params, gql);
|
|
190
235
|
case "multiplayer.getSession":
|
|
191
236
|
return multiplayerGetSession(ctx, params, caps);
|
|
192
237
|
case "multiplayer.getJoinHint":
|
|
@@ -421,6 +466,61 @@ async function passHas(ctx, params, caps) {
|
|
|
421
466
|
const match = (data.passesForDeck ?? []).find((p) => p?.passId === passId);
|
|
422
467
|
return { hasPass: match?.isActive === true };
|
|
423
468
|
}
|
|
469
|
+
// The platform avatar: ghost keeps it per account (anonymous accounts too), so
|
|
470
|
+
// these are plain GraphQL with the player's own auth. A signed-out visitor has
|
|
471
|
+
// no account to keep one on, which is `unavailable`, not an error: the deck
|
|
472
|
+
// picks its own. Only a curated hub place may change it: the host names the
|
|
473
|
+
// place the player is in (ctx.deckId, never the deck's word) and ghost refuses
|
|
474
|
+
// any other deck, which is `unavailable` too.
|
|
475
|
+
const AVATAR_QUERY = `query CastleAvatar { cauldronHubAvatar { avatar chosen } }`;
|
|
476
|
+
const SET_AVATAR_MUTATION = `mutation CastleSetAvatar($deckId: ID!, $avatar: Int!) {
|
|
477
|
+
setCauldronHubAvatar(deckId: $deckId, avatar: $avatar) { avatar chosen }
|
|
478
|
+
}`;
|
|
479
|
+
const AVATAR_UNAVAILABLE_CODES = new Set([
|
|
480
|
+
"LOGIN_REQUIRED",
|
|
481
|
+
"CAULDRON_HUB_AVATAR_NOT_A_PLACE",
|
|
482
|
+
]);
|
|
483
|
+
// Bounds the argument only; ghost holds the real template count.
|
|
484
|
+
const AVATAR_MAX = 255;
|
|
485
|
+
function avatarResult(raw) {
|
|
486
|
+
const avatar = raw?.avatar;
|
|
487
|
+
return typeof avatar === "number" && Number.isInteger(avatar) && avatar >= 0
|
|
488
|
+
? { status: "ok", avatar, chosen: raw?.chosen === true }
|
|
489
|
+
: unavailableAvatar();
|
|
490
|
+
}
|
|
491
|
+
function unavailableAvatar() {
|
|
492
|
+
return { status: "unavailable", avatar: null, chosen: false };
|
|
493
|
+
}
|
|
494
|
+
async function avatarGet(gql) {
|
|
495
|
+
try {
|
|
496
|
+
const data = await graphql(gql, AVATAR_QUERY, {}, "avatar.get");
|
|
497
|
+
return avatarResult(data.cauldronHubAvatar);
|
|
498
|
+
}
|
|
499
|
+
catch (error) {
|
|
500
|
+
if (error instanceof HostCommandError && AVATAR_UNAVAILABLE_CODES.has(error.code)) {
|
|
501
|
+
return unavailableAvatar();
|
|
502
|
+
}
|
|
503
|
+
throw error;
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
async function avatarSet(ctx, params, gql) {
|
|
507
|
+
const avatar = params.avatar;
|
|
508
|
+
if (typeof avatar !== "number" || !Number.isInteger(avatar) || avatar < 0 || avatar > AVATAR_MAX) {
|
|
509
|
+
throw new HostCommandError("INVALID_ARGUMENT", "avatar must be a template index.", "avatar.set");
|
|
510
|
+
}
|
|
511
|
+
if (!ctx.deckId)
|
|
512
|
+
return unavailableAvatar();
|
|
513
|
+
try {
|
|
514
|
+
const data = await graphql(gql, SET_AVATAR_MUTATION, { deckId: ctx.deckId, avatar }, "avatar.set");
|
|
515
|
+
return avatarResult(data.setCauldronHubAvatar);
|
|
516
|
+
}
|
|
517
|
+
catch (error) {
|
|
518
|
+
if (error instanceof HostCommandError && AVATAR_UNAVAILABLE_CODES.has(error.code)) {
|
|
519
|
+
return unavailableAvatar();
|
|
520
|
+
}
|
|
521
|
+
throw error;
|
|
522
|
+
}
|
|
523
|
+
}
|
|
424
524
|
// A session id is ghost's: a deck id plus a short suffix, or a nanoid.
|
|
425
525
|
const VOICE_SESSION_ID_MAX_LENGTH = 256;
|
|
426
526
|
// A room holds at most 24 players; anything past this is a confused deck.
|
|
@@ -1076,6 +1176,16 @@ function userGetCurrent(ctx) {
|
|
|
1076
1176
|
user: { userId: ctx.userId, username: ctx.username, isAnonymous: ctx.isAnonymous === true },
|
|
1077
1177
|
};
|
|
1078
1178
|
}
|
|
1179
|
+
// Answered from what the host already resolved, like the current user. Only
|
|
1180
|
+
// string variants are passed on, so a malformed context cannot reach the deck.
|
|
1181
|
+
function experimentsGet(ctx) {
|
|
1182
|
+
const source = ctx.experimentVariants;
|
|
1183
|
+
if (!source || typeof source !== "object" || Array.isArray(source))
|
|
1184
|
+
return { variants: {} };
|
|
1185
|
+
return {
|
|
1186
|
+
variants: Object.fromEntries(Object.entries(source).filter((entry) => typeof entry[1] === "string")),
|
|
1187
|
+
};
|
|
1188
|
+
}
|
|
1079
1189
|
// One user's public profile, or null when there is no such user. The photo and
|
|
1080
1190
|
// frame are fetched here and handed over as data: urls, since a deck's CSP
|
|
1081
1191
|
// allows no image host and the host is outside it.
|
|
@@ -1403,7 +1513,7 @@ async function decksSaved(ctx, params, gql) {
|
|
|
1403
1513
|
const playlists = await Promise.all(playlistIds.map((id) => feedDecks(gql, `playlist:${id}`, command).catch(() => [])));
|
|
1404
1514
|
return decksResult([bookmarks, ...playlists].flat(), limit);
|
|
1405
1515
|
}
|
|
1406
|
-
// Decks is limited to Castle admins for now
|
|
1516
|
+
// Decks is limited to Castle admins for now.
|
|
1407
1517
|
async function requireDecksAdmin(gql, command) {
|
|
1408
1518
|
if (!(await isCastleAdmin(gql, command))) {
|
|
1409
1519
|
throw new HostCommandError("PERMISSION_DENIED", "Decks is limited to Castle admins.", command);
|
|
@@ -1489,8 +1599,9 @@ const DECKS_PLAYLISTS_QUERY = `
|
|
|
1489
1599
|
// Agent chat.
|
|
1490
1600
|
//
|
|
1491
1601
|
// The deck-agent service is Castle's proxy to OpenRouter for decks. It decides
|
|
1492
|
-
// who may call a model (Castle admins
|
|
1493
|
-
// charges each call to the player's daily play allowance,
|
|
1602
|
+
// who may call a model (Castle admins and the players given access), picks the
|
|
1603
|
+
// default model, and charges each call to the player's daily play allowance,
|
|
1604
|
+
// which Castle funds.
|
|
1494
1605
|
// It refuses before streaming with a status: 403 for a player it will not
|
|
1495
1606
|
// serve, 429 once the allowance is spent. Otherwise it answers with a
|
|
1496
1607
|
// server-sent event stream: OpenRouter's chunks, a heartbeat event every 15
|
|
@@ -2650,3 +2761,668 @@ const CAULDRON_STORAGE_BOARD_GET_QUERY = `
|
|
|
2650
2761
|
}
|
|
2651
2762
|
}
|
|
2652
2763
|
`;
|
|
2764
|
+
// What a host posts to the deck (web: postMessage to the iframe; mobile:
|
|
2765
|
+
// window.__castleSdkHost.receive) when the player presses its Back control.
|
|
2766
|
+
export function hubBackEnvelope() {
|
|
2767
|
+
return { castleSdk: 1, hub: { type: "back" } };
|
|
2768
|
+
}
|
|
2769
|
+
// Ghost's source check reads the session's heartbeat roster, which can lag an
|
|
2770
|
+
// arrival by up to 15 s: a door used in that window answers `forbidden`.
|
|
2771
|
+
const HUB_ROSTER_LAG_MS = 16_000;
|
|
2772
|
+
const HUB_FORBIDDEN_RETRY_MS = 1_000;
|
|
2773
|
+
const HUB_BUSY_ATTEMPTS = 8;
|
|
2774
|
+
const HUB_BUSY_RETRY_MS = 250;
|
|
2775
|
+
// Ghost keeps a stack of four return tickets; the trail mirrors it.
|
|
2776
|
+
const HUB_TRAIL_MAX = 4;
|
|
2777
|
+
// A deck stops waiting for `hub.go` after 30 s and never hears a later answer.
|
|
2778
|
+
// A cabinet's trip is committed by the host, so one acquired this long after
|
|
2779
|
+
// the deck asked is given back instead: committing it could leave the host in
|
|
2780
|
+
// a classic deck the place never learns it went to.
|
|
2781
|
+
const HUB_CLASSIC_COMMIT_DEADLINE_MS = 20_000;
|
|
2782
|
+
// And a commit that only lands this late is not followed: the deck may already
|
|
2783
|
+
// have stopped waiting, so the host stays in the place. Ghost then holds a
|
|
2784
|
+
// visit and a ticket for a trip nobody made, which the next trip replaces.
|
|
2785
|
+
const HUB_CLASSIC_ANSWER_DEADLINE_MS = 28_000;
|
|
2786
|
+
const HUB_DOOR_ID_PATTERN = /^[A-Za-z0-9_.:-]{1,64}$/;
|
|
2787
|
+
const HUB_USER_ID_PATTERN = /^[A-Za-z0-9_-]{1,64}$/;
|
|
2788
|
+
const HUB_HANDOFF_FIELDS = "status kind deckId url nonce sessionId bundleHash contentUrl transitionId arrivalDoorId";
|
|
2789
|
+
const CAULDRON_HUB_DOORS_QUERY = `query CastleHubDoors($deckId: ID!, $env: String) {
|
|
2790
|
+
cauldronHubDoors(deckId: $deckId, env: $env) {
|
|
2791
|
+
doorId
|
|
2792
|
+
toDeckId
|
|
2793
|
+
launcherKind
|
|
2794
|
+
placementKind
|
|
2795
|
+
placement
|
|
2796
|
+
padSize
|
|
2797
|
+
enabled
|
|
2798
|
+
featured
|
|
2799
|
+
status { playersInside friendsInside }
|
|
2800
|
+
}
|
|
2801
|
+
}`;
|
|
2802
|
+
const CAULDRON_HUB_GO_MUTATION = `mutation CastleHubGo($fromDeckId: ID!, $fromSessionId: ID!, $doorId: String!, $partyId: ID, $env: String) {
|
|
2803
|
+
acquireCauldronHubDoor(fromDeckId: $fromDeckId, fromSessionId: $fromSessionId, doorId: $doorId, partyId: $partyId, env: $env) { ${HUB_HANDOFF_FIELDS} }
|
|
2804
|
+
}`;
|
|
2805
|
+
const CAULDRON_HUB_BACK_MUTATION = `mutation CastleHubBack($fromDeckId: ID!, $partyId: ID, $env: String) {
|
|
2806
|
+
acquireCauldronHubReturn(fromDeckId: $fromDeckId, partyId: $partyId, env: $env) { ${HUB_HANDOFF_FIELDS} }
|
|
2807
|
+
}`;
|
|
2808
|
+
const CAULDRON_HUB_FRIENDS_QUERY = `query CastleHubFriends($deckId: ID!, $sessionId: ID, $env: String) {
|
|
2809
|
+
cauldronHubFriends(deckId: $deckId, sessionId: $sessionId, env: $env, behindDoors: true) { userId username here door }
|
|
2810
|
+
}`;
|
|
2811
|
+
const CAULDRON_HUB_JUMP_MUTATION = `mutation CastleHubJump($fromDeckId: ID!, $fromSessionId: ID!, $friendUserId: ID!, $partyId: ID, $env: String) {
|
|
2812
|
+
acquireCauldronHubFriend(fromDeckId: $fromDeckId, fromSessionId: $fromSessionId, friendUserId: $friendUserId, partyId: $partyId, env: $env) { ${HUB_HANDOFF_FIELDS} }
|
|
2813
|
+
}`;
|
|
2814
|
+
const CAULDRON_HUB_REPORT_MUTATION = `mutation CastleHubReport($deckId: ID!, $sessionId: ID!, $userId: ID!, $reason: String, $env: String) {
|
|
2815
|
+
reportCauldronHubPlayer(deckId: $deckId, sessionId: $sessionId, userId: $userId, reason: $reason, env: $env)
|
|
2816
|
+
}`;
|
|
2817
|
+
// A report's reason is a short note, never a transcript.
|
|
2818
|
+
const HUB_REPORT_REASON_MAX = 500;
|
|
2819
|
+
const CAULDRON_HUB_CHAT_QUERY = `query CastleHubChat($deckId: ID!, $sessionId: ID!, $after: String, $env: String) {
|
|
2820
|
+
cauldronHubChat(deckId: $deckId, sessionId: $sessionId, after: $after, env: $env) {
|
|
2821
|
+
canChat peers cursor messages { id fromUserId fromUsername text at }
|
|
2822
|
+
}
|
|
2823
|
+
}`;
|
|
2824
|
+
const CAULDRON_HUB_SAY_MUTATION = `mutation CastleHubSay($deckId: ID!, $sessionId: ID!, $text: String!, $env: String) {
|
|
2825
|
+
sendCauldronHubChat(deckId: $deckId, sessionId: $sessionId, text: $text, env: $env) { status id }
|
|
2826
|
+
}`;
|
|
2827
|
+
const CAULDRON_HUB_REPORT_CHAT_MUTATION = `mutation CastleHubReportChat($deckId: ID!, $sessionId: ID!, $messageId: ID!, $env: String) {
|
|
2828
|
+
reportCauldronHubChatMessage(deckId: $deckId, sessionId: $sessionId, messageId: $messageId, env: $env)
|
|
2829
|
+
}`;
|
|
2830
|
+
// Ghost's own limits, held here too so a wrong answer cannot flood a deck.
|
|
2831
|
+
const HUB_CHAT_TEXT_MAX = 200;
|
|
2832
|
+
const HUB_CHAT_MESSAGES_MAX = 50;
|
|
2833
|
+
const HUB_CHAT_PEERS_MAX = 200;
|
|
2834
|
+
// A cursor or a message id is ghost's own string handed back: bounded, opaque.
|
|
2835
|
+
const HUB_CHAT_TOKEN_MAX = 512;
|
|
2836
|
+
const HUB_SAY_STATUSES = new Set(["sent", "none", "refused", "limited"]);
|
|
2837
|
+
const CAULDRON_HUB_COMMIT_MUTATION = `mutation CastleHubCommit($transitionId: ID!, $env: String) {
|
|
2838
|
+
commitCauldronHubTransition(transitionId: $transitionId, env: $env) { status }
|
|
2839
|
+
}`;
|
|
2840
|
+
const CAULDRON_HUB_CANCEL_MUTATION = `mutation CastleHubCancel($transitionId: ID!, $env: String) {
|
|
2841
|
+
cancelCauldronHubTransition(transitionId: $transitionId, env: $env) { status }
|
|
2842
|
+
}`;
|
|
2843
|
+
const HUB_FAILURE_STATUSES = new Set([
|
|
2844
|
+
"cancelled",
|
|
2845
|
+
"full",
|
|
2846
|
+
"unavailable",
|
|
2847
|
+
"not_found",
|
|
2848
|
+
"forbidden",
|
|
2849
|
+
"party_required",
|
|
2850
|
+
]);
|
|
2851
|
+
export function unavailableHubHandoff() {
|
|
2852
|
+
return hubFailure("unavailable");
|
|
2853
|
+
}
|
|
2854
|
+
function hubFailure(status) {
|
|
2855
|
+
return { status, url: null, nonce: null, sessionId: null, bundleHash: null, contentUrl: null };
|
|
2856
|
+
}
|
|
2857
|
+
function defaultHubSleep(ms, signal) {
|
|
2858
|
+
return new Promise((resolve) => {
|
|
2859
|
+
if (signal?.aborted) {
|
|
2860
|
+
resolve();
|
|
2861
|
+
return;
|
|
2862
|
+
}
|
|
2863
|
+
const timer = setTimeout(done, ms);
|
|
2864
|
+
function done() {
|
|
2865
|
+
clearTimeout(timer);
|
|
2866
|
+
signal?.removeEventListener("abort", done);
|
|
2867
|
+
resolve();
|
|
2868
|
+
}
|
|
2869
|
+
signal?.addEventListener("abort", done, { once: true });
|
|
2870
|
+
});
|
|
2871
|
+
}
|
|
2872
|
+
export function createGhostHubVisit(options) {
|
|
2873
|
+
const visit = new GhostHubVisitImpl(options);
|
|
2874
|
+
return {
|
|
2875
|
+
go: (doorId, ctx, signal) => visit.go(doorId, ctx, signal),
|
|
2876
|
+
back: (ctx, signal) => visit.back(ctx, signal),
|
|
2877
|
+
doors: () => visit.doors(),
|
|
2878
|
+
friends: () => visit.friends(),
|
|
2879
|
+
jump: (userId, ctx, signal) => visit.jump(userId, ctx, signal),
|
|
2880
|
+
report: (userId, reason) => visit.report(userId, reason),
|
|
2881
|
+
chat: (after) => visit.chat(after),
|
|
2882
|
+
say: (text) => visit.say(text),
|
|
2883
|
+
reportChat: (messageId) => visit.reportChat(messageId),
|
|
2884
|
+
commit: (nonce) => visit.commit(nonce),
|
|
2885
|
+
cancel: (nonce) => visit.cancel(nonce),
|
|
2886
|
+
sessionAcquired: (sessionId) => visit.sessionAcquired(sessionId),
|
|
2887
|
+
placeDeckId: () => visit.place,
|
|
2888
|
+
state: () => visit.state(),
|
|
2889
|
+
};
|
|
2890
|
+
}
|
|
2891
|
+
// Every operation that reads or moves the visit runs one at a time, in the
|
|
2892
|
+
// order the deck asked, so a slow acquisition or commit can never land on top of
|
|
2893
|
+
// a newer one. Only a cancel reaches past the queue, to stop an acquisition
|
|
2894
|
+
// still in flight (one waiting out the roster lag can take 15 s).
|
|
2895
|
+
class GhostHubVisitImpl {
|
|
2896
|
+
options;
|
|
2897
|
+
place;
|
|
2898
|
+
gql;
|
|
2899
|
+
env;
|
|
2900
|
+
now;
|
|
2901
|
+
sleep;
|
|
2902
|
+
trail = [];
|
|
2903
|
+
arrivalDoorId = null;
|
|
2904
|
+
placeKind = "place";
|
|
2905
|
+
session = null;
|
|
2906
|
+
pending = null;
|
|
2907
|
+
queue = Promise.resolve();
|
|
2908
|
+
inFlight = new Set();
|
|
2909
|
+
// Door rows of the place they were read for: the fallback for a go's
|
|
2910
|
+
// destination when ghost does not name it.
|
|
2911
|
+
rows = null;
|
|
2912
|
+
constructor(options) {
|
|
2913
|
+
this.options = options;
|
|
2914
|
+
this.gql = options.graphqlFetch;
|
|
2915
|
+
this.env = options.env ?? null;
|
|
2916
|
+
this.now = options.now ?? (() => Date.now());
|
|
2917
|
+
this.sleep = options.sleep ?? defaultHubSleep;
|
|
2918
|
+
this.place = options.homeDeckId;
|
|
2919
|
+
}
|
|
2920
|
+
state() {
|
|
2921
|
+
const pending = this.pending;
|
|
2922
|
+
return {
|
|
2923
|
+
placeDeckId: this.place,
|
|
2924
|
+
placeKind: this.placeKind,
|
|
2925
|
+
trail: this.trail.slice(),
|
|
2926
|
+
arrivalDoorId: this.arrivalDoorId,
|
|
2927
|
+
pending: pending
|
|
2928
|
+
? { kind: pending.kind, sessionId: pending.sessionId, toDeckId: pending.toDeckId }
|
|
2929
|
+
: null,
|
|
2930
|
+
};
|
|
2931
|
+
}
|
|
2932
|
+
sessionAcquired(sessionId) {
|
|
2933
|
+
this.session = { sessionId, at: this.now() };
|
|
2934
|
+
}
|
|
2935
|
+
go(doorId, ctx, signal) {
|
|
2936
|
+
if (!HUB_DOOR_ID_PATTERN.test(doorId))
|
|
2937
|
+
return Promise.resolve(hubFailure("not_found"));
|
|
2938
|
+
const asked = this.now();
|
|
2939
|
+
return this.acquisition(signal, (abort) => this.goNow(doorId, ctx, abort, asked));
|
|
2940
|
+
}
|
|
2941
|
+
back(ctx, signal) {
|
|
2942
|
+
return this.acquisition(signal, (abort) => this.backNow(ctx, abort));
|
|
2943
|
+
}
|
|
2944
|
+
doors() {
|
|
2945
|
+
return this.serial(async () => {
|
|
2946
|
+
const raw = await this.readDoors("hub.doors");
|
|
2947
|
+
return { status: "ok", doors: raw.flatMap(publicHubDoor) };
|
|
2948
|
+
});
|
|
2949
|
+
}
|
|
2950
|
+
// A read of who is where, outside the queue: it moves nothing, and must not
|
|
2951
|
+
// wait behind an acquisition sitting out the roster lag.
|
|
2952
|
+
async friends() {
|
|
2953
|
+
const data = await graphql(this.gql, CAULDRON_HUB_FRIENDS_QUERY, { deckId: this.place, sessionId: this.session?.sessionId ?? null, env: this.env }, "hub.friends");
|
|
2954
|
+
const raw = Array.isArray(data.cauldronHubFriends) ? data.cauldronHubFriends : [];
|
|
2955
|
+
return { status: "ok", friends: raw.flatMap(publicHubFriend) };
|
|
2956
|
+
}
|
|
2957
|
+
jump(userId, ctx, signal) {
|
|
2958
|
+
if (!HUB_USER_ID_PATTERN.test(userId))
|
|
2959
|
+
return Promise.resolve(hubFailure("not_found"));
|
|
2960
|
+
return this.acquisition(signal, (abort) => this.jumpNow(userId, ctx, abort));
|
|
2961
|
+
}
|
|
2962
|
+
// Outside the queue, like friends: it moves nothing. The place and session
|
|
2963
|
+
// are the host's own, never the deck's.
|
|
2964
|
+
async report(userId, reason) {
|
|
2965
|
+
const session = this.session;
|
|
2966
|
+
if (!HUB_USER_ID_PATTERN.test(userId) || !session)
|
|
2967
|
+
return { status: "unavailable" };
|
|
2968
|
+
const note = reason?.trim().slice(0, HUB_REPORT_REASON_MAX) || null;
|
|
2969
|
+
const data = await graphql(this.gql, CAULDRON_HUB_REPORT_MUTATION, { deckId: this.place, sessionId: session.sessionId, userId, reason: note, env: this.env }, "hub.report");
|
|
2970
|
+
return { status: data.reportCauldronHubPlayer === true ? "ok" : "unavailable" };
|
|
2971
|
+
}
|
|
2972
|
+
// Chat is outside the queue too: reading and saying move nothing. Ghost
|
|
2973
|
+
// decides who may chat with whom; the deck names no place, session or reader.
|
|
2974
|
+
async chat(after) {
|
|
2975
|
+
const session = this.session;
|
|
2976
|
+
if (!session)
|
|
2977
|
+
return unavailableHubChat();
|
|
2978
|
+
const data = await graphql(this.gql, CAULDRON_HUB_CHAT_QUERY, {
|
|
2979
|
+
deckId: this.place,
|
|
2980
|
+
sessionId: session.sessionId,
|
|
2981
|
+
after: after && after.length <= HUB_CHAT_TOKEN_MAX ? after : null,
|
|
2982
|
+
env: this.env,
|
|
2983
|
+
}, "hub.chat");
|
|
2984
|
+
return publicHubChat(data.cauldronHubChat);
|
|
2985
|
+
}
|
|
2986
|
+
async say(text) {
|
|
2987
|
+
const session = this.session;
|
|
2988
|
+
if (!session)
|
|
2989
|
+
return { status: "unavailable", id: null };
|
|
2990
|
+
const said = text.trim().slice(0, HUB_CHAT_TEXT_MAX);
|
|
2991
|
+
if (!said)
|
|
2992
|
+
return { status: "refused", id: null };
|
|
2993
|
+
const data = await graphql(this.gql, CAULDRON_HUB_SAY_MUTATION, { deckId: this.place, sessionId: session.sessionId, text: said, env: this.env }, "hub.say");
|
|
2994
|
+
return publicHubSay(data.sendCauldronHubChat);
|
|
2995
|
+
}
|
|
2996
|
+
async reportChat(messageId) {
|
|
2997
|
+
const session = this.session;
|
|
2998
|
+
if (!session || messageId.length > HUB_CHAT_TOKEN_MAX)
|
|
2999
|
+
return { status: "unavailable" };
|
|
3000
|
+
const data = await graphql(this.gql, CAULDRON_HUB_REPORT_CHAT_MUTATION, { deckId: this.place, sessionId: session.sessionId, messageId, env: this.env }, "hub.reportChat");
|
|
3001
|
+
return { status: data.reportCauldronHubChatMessage === true ? "ok" : "unavailable" };
|
|
3002
|
+
}
|
|
3003
|
+
commit(nonce) {
|
|
3004
|
+
return this.serial(() => this.commitNow(nonce));
|
|
3005
|
+
}
|
|
3006
|
+
cancel(nonce) {
|
|
3007
|
+
if (nonce === null) {
|
|
3008
|
+
for (const controller of this.inFlight)
|
|
3009
|
+
controller.abort();
|
|
3010
|
+
}
|
|
3011
|
+
return this.serial(() => this.cancelNow(nonce));
|
|
3012
|
+
}
|
|
3013
|
+
serial(work) {
|
|
3014
|
+
const run = this.queue.then(work, work);
|
|
3015
|
+
this.queue = run.catch(() => { });
|
|
3016
|
+
return run;
|
|
3017
|
+
}
|
|
3018
|
+
acquisition(signal, work) {
|
|
3019
|
+
const controller = new AbortController();
|
|
3020
|
+
const forward = () => controller.abort();
|
|
3021
|
+
if (signal?.aborted)
|
|
3022
|
+
controller.abort();
|
|
3023
|
+
signal?.addEventListener("abort", forward, { once: true });
|
|
3024
|
+
this.inFlight.add(controller);
|
|
3025
|
+
return this.serial(() => work(controller.signal)).finally(() => {
|
|
3026
|
+
this.inFlight.delete(controller);
|
|
3027
|
+
signal?.removeEventListener("abort", forward);
|
|
3028
|
+
});
|
|
3029
|
+
}
|
|
3030
|
+
async goNow(doorId, ctx, signal, asked) {
|
|
3031
|
+
for (;;) {
|
|
3032
|
+
if (signal.aborted)
|
|
3033
|
+
return hubFailure("cancelled");
|
|
3034
|
+
const source = this.session;
|
|
3035
|
+
if (!source)
|
|
3036
|
+
return hubFailure("forbidden");
|
|
3037
|
+
const data = await graphql(this.gql, CAULDRON_HUB_GO_MUTATION, {
|
|
3038
|
+
fromDeckId: this.place,
|
|
3039
|
+
fromSessionId: source.sessionId,
|
|
3040
|
+
doorId,
|
|
3041
|
+
partyId: ctx.partyId ?? null,
|
|
3042
|
+
env: this.env,
|
|
3043
|
+
}, "hub.go");
|
|
3044
|
+
const raw = data.acquireCauldronHubDoor;
|
|
3045
|
+
// Just arrived: ghost has not seen this player in the session yet.
|
|
3046
|
+
if (raw?.status === "forbidden" &&
|
|
3047
|
+
this.now() - source.at < HUB_ROSTER_LAG_MS &&
|
|
3048
|
+
!signal.aborted) {
|
|
3049
|
+
await this.sleep(HUB_FORBIDDEN_RETRY_MS, signal);
|
|
3050
|
+
continue;
|
|
3051
|
+
}
|
|
3052
|
+
return this.accept("go", raw, () => this.destinationOf(doorId), signal, asked);
|
|
3053
|
+
}
|
|
3054
|
+
}
|
|
3055
|
+
// Into the layer a friend is in: the same place, another session. Ghost names
|
|
3056
|
+
// the place (`deckId` is the one left); the trail and return tickets stay.
|
|
3057
|
+
async jumpNow(userId, ctx, signal) {
|
|
3058
|
+
for (;;) {
|
|
3059
|
+
if (signal.aborted)
|
|
3060
|
+
return hubFailure("cancelled");
|
|
3061
|
+
const source = this.session;
|
|
3062
|
+
if (!source)
|
|
3063
|
+
return hubFailure("forbidden");
|
|
3064
|
+
const place = this.place;
|
|
3065
|
+
const data = await graphql(this.gql, CAULDRON_HUB_JUMP_MUTATION, {
|
|
3066
|
+
fromDeckId: place,
|
|
3067
|
+
fromSessionId: source.sessionId,
|
|
3068
|
+
friendUserId: userId,
|
|
3069
|
+
partyId: ctx.partyId ?? null,
|
|
3070
|
+
env: this.env,
|
|
3071
|
+
}, "hub.jump");
|
|
3072
|
+
const raw = data.acquireCauldronHubFriend;
|
|
3073
|
+
if (raw?.status === "forbidden" &&
|
|
3074
|
+
this.now() - source.at < HUB_ROSTER_LAG_MS &&
|
|
3075
|
+
!signal.aborted) {
|
|
3076
|
+
await this.sleep(HUB_FORBIDDEN_RETRY_MS, signal);
|
|
3077
|
+
continue;
|
|
3078
|
+
}
|
|
3079
|
+
return this.accept("jump", raw, () => Promise.resolve(place), signal);
|
|
3080
|
+
}
|
|
3081
|
+
}
|
|
3082
|
+
async backNow(ctx, signal) {
|
|
3083
|
+
if (signal.aborted)
|
|
3084
|
+
return hubFailure("cancelled");
|
|
3085
|
+
const data = await graphql(this.gql, CAULDRON_HUB_BACK_MUTATION, { fromDeckId: this.place, partyId: ctx.partyId ?? null, env: this.env }, "hub.back");
|
|
3086
|
+
// Without ghost naming it, the way back leads to the place this host saw
|
|
3087
|
+
// the player leave.
|
|
3088
|
+
const to = this.trail.at(-1) ?? null;
|
|
3089
|
+
return this.accept("back", data.acquireCauldronHubReturn, () => Promise.resolve(to), signal);
|
|
3090
|
+
}
|
|
3091
|
+
async commitNow(nonce) {
|
|
3092
|
+
const trip = this.pending;
|
|
3093
|
+
if (!trip || trip.nonce !== nonce)
|
|
3094
|
+
return { status: "none" };
|
|
3095
|
+
let status;
|
|
3096
|
+
try {
|
|
3097
|
+
status = await this.transition("commit", trip.transitionId);
|
|
3098
|
+
}
|
|
3099
|
+
catch {
|
|
3100
|
+
// The commit may have landed with its answer lost; ghost answers a retry
|
|
3101
|
+
// of a commit that landed with `committed`.
|
|
3102
|
+
return { status: "unavailable" };
|
|
3103
|
+
}
|
|
3104
|
+
// Still pending: the SDK asks again.
|
|
3105
|
+
if (status === "busy")
|
|
3106
|
+
return { status: "unavailable" };
|
|
3107
|
+
this.pending = null;
|
|
3108
|
+
if (status === "committed")
|
|
3109
|
+
this.arrive(trip);
|
|
3110
|
+
this.changed();
|
|
3111
|
+
if (status === "committed" || status === "superseded" || status === "cancelled") {
|
|
3112
|
+
return { status };
|
|
3113
|
+
}
|
|
3114
|
+
return { status: "expired" };
|
|
3115
|
+
}
|
|
3116
|
+
async cancelNow(nonce) {
|
|
3117
|
+
const trip = this.pending;
|
|
3118
|
+
if (!trip || (nonce !== null && trip.nonce !== nonce))
|
|
3119
|
+
return { status: "none" };
|
|
3120
|
+
let status;
|
|
3121
|
+
try {
|
|
3122
|
+
status = await this.transition("cancel", trip.transitionId);
|
|
3123
|
+
}
|
|
3124
|
+
catch {
|
|
3125
|
+
return { status: "unavailable" };
|
|
3126
|
+
}
|
|
3127
|
+
// Kept pending so a later cancel can still reach ghost.
|
|
3128
|
+
if (status === "busy")
|
|
3129
|
+
return { status: "unavailable" };
|
|
3130
|
+
if (this.pending === trip)
|
|
3131
|
+
this.pending = null;
|
|
3132
|
+
this.changed();
|
|
3133
|
+
return { status: status === "cancelled" ? "cancelled" : "expired" };
|
|
3134
|
+
}
|
|
3135
|
+
// A classic place (a cabinet's deck) has no session.
|
|
3136
|
+
arrive(trip) {
|
|
3137
|
+
// A jump changes the session, not the way back.
|
|
3138
|
+
if (trip.kind === "go") {
|
|
3139
|
+
this.trail.push(this.place);
|
|
3140
|
+
if (this.trail.length > HUB_TRAIL_MAX)
|
|
3141
|
+
this.trail.shift();
|
|
3142
|
+
}
|
|
3143
|
+
else if (trip.kind === "back") {
|
|
3144
|
+
this.trail.pop();
|
|
3145
|
+
}
|
|
3146
|
+
if (trip.toDeckId !== this.place)
|
|
3147
|
+
this.rows = null;
|
|
3148
|
+
this.place = trip.toDeckId;
|
|
3149
|
+
this.placeKind = trip.classic ? "classic" : "place";
|
|
3150
|
+
this.arrivalDoorId = trip.arrivalDoorId;
|
|
3151
|
+
this.session = trip.sessionId ? { sessionId: trip.sessionId, at: this.now() } : null;
|
|
3152
|
+
}
|
|
3153
|
+
changed() {
|
|
3154
|
+
try {
|
|
3155
|
+
this.options.onChange?.(this.state());
|
|
3156
|
+
}
|
|
3157
|
+
catch (error) {
|
|
3158
|
+
console.error("Castle hub onChange threw:", error);
|
|
3159
|
+
}
|
|
3160
|
+
}
|
|
3161
|
+
async readDoors(command) {
|
|
3162
|
+
const deckId = this.place;
|
|
3163
|
+
const data = await graphql(this.gql, CAULDRON_HUB_DOORS_QUERY, { deckId, env: this.env }, command);
|
|
3164
|
+
const doors = Array.isArray(data.cauldronHubDoors) ? data.cauldronHubDoors : [];
|
|
3165
|
+
if (this.place === deckId)
|
|
3166
|
+
this.rows = { deckId, doors };
|
|
3167
|
+
return doors;
|
|
3168
|
+
}
|
|
3169
|
+
async destinationOf(doorId) {
|
|
3170
|
+
const find = (doors) => asOptionalString(doors.find((door) => door.doorId === doorId)?.toDeckId);
|
|
3171
|
+
const cached = this.rows?.deckId === this.place ? find(this.rows.doors) : null;
|
|
3172
|
+
return cached ?? find(await this.readDoors("hub.go"));
|
|
3173
|
+
}
|
|
3174
|
+
async transition(kind, transitionId) {
|
|
3175
|
+
const field = kind === "commit" ? "commitCauldronHubTransition" : "cancelCauldronHubTransition";
|
|
3176
|
+
for (let attempt = 1;; attempt++) {
|
|
3177
|
+
const data = await graphql(this.gql, kind === "commit" ? CAULDRON_HUB_COMMIT_MUTATION : CAULDRON_HUB_CANCEL_MUTATION, { transitionId, env: this.env }, kind === "commit" ? "hub.commit" : "hub.cancel");
|
|
3178
|
+
const status = data[field]?.status ?? "expired";
|
|
3179
|
+
if (status !== "busy" || attempt >= HUB_BUSY_ATTEMPTS)
|
|
3180
|
+
return status;
|
|
3181
|
+
await this.sleep(HUB_BUSY_RETRY_MS);
|
|
3182
|
+
}
|
|
3183
|
+
}
|
|
3184
|
+
// Anything acquired that will not be joined is given back, so it neither
|
|
3185
|
+
// waits out its TTL as the pending one nor commits later by accident.
|
|
3186
|
+
async release(transitionId) {
|
|
3187
|
+
try {
|
|
3188
|
+
await this.transition("cancel", transitionId);
|
|
3189
|
+
}
|
|
3190
|
+
catch {
|
|
3191
|
+
// It expires on its own.
|
|
3192
|
+
}
|
|
3193
|
+
}
|
|
3194
|
+
// An ok handoff becomes the pending trip (ghost keeps one per player, so it
|
|
3195
|
+
// supersedes any earlier one); the deck gets only its public half.
|
|
3196
|
+
async accept(kind, raw, fallbackDestination, signal, asked) {
|
|
3197
|
+
const status = raw?.status ?? "unavailable";
|
|
3198
|
+
const transitionId = asOptionalString(raw?.transitionId);
|
|
3199
|
+
if (status !== "ok") {
|
|
3200
|
+
return hubFailure(HUB_FAILURE_STATUSES.has(status) ? status : "unavailable");
|
|
3201
|
+
}
|
|
3202
|
+
if (kind === "go" && raw?.kind === "classic") {
|
|
3203
|
+
return this.acceptClassic(raw, signal, asked ?? this.now());
|
|
3204
|
+
}
|
|
3205
|
+
const url = asOptionalString(raw?.url);
|
|
3206
|
+
const nonce = asOptionalString(raw?.nonce);
|
|
3207
|
+
const sessionId = asOptionalString(raw?.sessionId);
|
|
3208
|
+
if (!transitionId || !url || !nonce || !sessionId) {
|
|
3209
|
+
if (transitionId)
|
|
3210
|
+
await this.release(transitionId);
|
|
3211
|
+
return hubFailure("unavailable");
|
|
3212
|
+
}
|
|
3213
|
+
const destination = signal.aborted
|
|
3214
|
+
? null
|
|
3215
|
+
: asOptionalString(raw?.deckId) ?? await fallbackDestination();
|
|
3216
|
+
if (signal.aborted || !destination) {
|
|
3217
|
+
await this.release(transitionId);
|
|
3218
|
+
return hubFailure(signal.aborted ? "cancelled" : "not_found");
|
|
3219
|
+
}
|
|
3220
|
+
this.pending = {
|
|
3221
|
+
kind,
|
|
3222
|
+
transitionId,
|
|
3223
|
+
nonce,
|
|
3224
|
+
sessionId,
|
|
3225
|
+
toDeckId: destination,
|
|
3226
|
+
arrivalDoorId: asOptionalString(raw?.arrivalDoorId),
|
|
3227
|
+
};
|
|
3228
|
+
this.changed();
|
|
3229
|
+
return {
|
|
3230
|
+
status: "ok",
|
|
3231
|
+
url,
|
|
3232
|
+
nonce,
|
|
3233
|
+
sessionId,
|
|
3234
|
+
bundleHash: asOptionalString(raw?.bundleHash) ?? "",
|
|
3235
|
+
contentUrl: asOptionalString(raw?.contentUrl),
|
|
3236
|
+
};
|
|
3237
|
+
}
|
|
3238
|
+
// A cabinet: the door leads to a classic deck, which the host plays itself.
|
|
3239
|
+
// No socket will ever say hello, so the host commits the trip here and the
|
|
3240
|
+
// deck gets a handoff with nothing to join. Anything short of a commit gives
|
|
3241
|
+
// the transition back.
|
|
3242
|
+
async acceptClassic(raw, signal, asked) {
|
|
3243
|
+
const transitionId = asOptionalString(raw.transitionId);
|
|
3244
|
+
const deckId = asOptionalString(raw.deckId);
|
|
3245
|
+
if (!transitionId)
|
|
3246
|
+
return hubFailure("unavailable");
|
|
3247
|
+
if (!deckId || signal.aborted || this.now() - asked > HUB_CLASSIC_COMMIT_DEADLINE_MS) {
|
|
3248
|
+
await this.release(transitionId);
|
|
3249
|
+
return hubFailure(signal.aborted ? "cancelled" : "unavailable");
|
|
3250
|
+
}
|
|
3251
|
+
if ((await this.commitClassic(transitionId)) !== "committed") {
|
|
3252
|
+
await this.release(transitionId);
|
|
3253
|
+
return hubFailure("unavailable");
|
|
3254
|
+
}
|
|
3255
|
+
if (this.now() - asked > HUB_CLASSIC_ANSWER_DEADLINE_MS)
|
|
3256
|
+
return hubFailure("unavailable");
|
|
3257
|
+
// Ghost keeps one transition per player: this one took the place of any
|
|
3258
|
+
// acquisition still waiting for its join.
|
|
3259
|
+
this.pending = null;
|
|
3260
|
+
this.arrive({
|
|
3261
|
+
kind: "go",
|
|
3262
|
+
toDeckId: deckId,
|
|
3263
|
+
arrivalDoorId: asOptionalString(raw.arrivalDoorId),
|
|
3264
|
+
sessionId: null,
|
|
3265
|
+
classic: true,
|
|
3266
|
+
});
|
|
3267
|
+
this.changed();
|
|
3268
|
+
return {
|
|
3269
|
+
status: "ok",
|
|
3270
|
+
kind: "classic",
|
|
3271
|
+
url: null,
|
|
3272
|
+
nonce: null,
|
|
3273
|
+
sessionId: null,
|
|
3274
|
+
bundleHash: "",
|
|
3275
|
+
contentUrl: null,
|
|
3276
|
+
};
|
|
3277
|
+
}
|
|
3278
|
+
// The commit's answer can be lost after it landed; ghost answers a retry of
|
|
3279
|
+
// one that landed with `committed`, so a failed request is asked once more.
|
|
3280
|
+
async commitClassic(transitionId) {
|
|
3281
|
+
for (let attempt = 1;; attempt++) {
|
|
3282
|
+
try {
|
|
3283
|
+
return await this.transition("commit", transitionId);
|
|
3284
|
+
}
|
|
3285
|
+
catch {
|
|
3286
|
+
if (attempt >= 2)
|
|
3287
|
+
return "unavailable";
|
|
3288
|
+
}
|
|
3289
|
+
}
|
|
3290
|
+
}
|
|
3291
|
+
}
|
|
3292
|
+
function publicHubDoor(raw) {
|
|
3293
|
+
const doorId = asOptionalString(raw.doorId);
|
|
3294
|
+
const placementKind = raw.placementKind;
|
|
3295
|
+
if (!doorId ||
|
|
3296
|
+
(placementKind !== "layer" && placementKind !== "pad" && placementKind !== "exact")) {
|
|
3297
|
+
return [];
|
|
3298
|
+
}
|
|
3299
|
+
return [
|
|
3300
|
+
{
|
|
3301
|
+
doorId,
|
|
3302
|
+
launcherKind: asOptionalString(raw.launcherKind) ?? "door",
|
|
3303
|
+
placementKind,
|
|
3304
|
+
placement: raw.placement ?? {},
|
|
3305
|
+
padSize: asOptionalNumber(raw.padSize),
|
|
3306
|
+
enabled: raw.enabled === true,
|
|
3307
|
+
featured: raw.featured === true,
|
|
3308
|
+
status: {
|
|
3309
|
+
playersInside: asOptionalNumber(raw.status?.playersInside) ?? 0,
|
|
3310
|
+
friendsInside: asOptionalNumber(raw.status?.friendsInside) ?? 0,
|
|
3311
|
+
},
|
|
3312
|
+
},
|
|
3313
|
+
];
|
|
3314
|
+
}
|
|
3315
|
+
function publicHubFriend(raw) {
|
|
3316
|
+
if (typeof raw !== "object" || raw === null)
|
|
3317
|
+
return [];
|
|
3318
|
+
const record = raw;
|
|
3319
|
+
const userId = asOptionalString(record.userId);
|
|
3320
|
+
if (!userId)
|
|
3321
|
+
return [];
|
|
3322
|
+
// A door is one of this place's own ids, or nothing.
|
|
3323
|
+
const door = asOptionalString(record.door);
|
|
3324
|
+
return [
|
|
3325
|
+
{
|
|
3326
|
+
userId,
|
|
3327
|
+
username: asOptionalString(record.username),
|
|
3328
|
+
here: record.here === true,
|
|
3329
|
+
door: door && HUB_DOOR_ID_PATTERN.test(door) ? door : null,
|
|
3330
|
+
},
|
|
3331
|
+
];
|
|
3332
|
+
}
|
|
3333
|
+
function unavailableHubChat() {
|
|
3334
|
+
return { status: "unavailable", canChat: false, peers: null, messages: null, cursor: null };
|
|
3335
|
+
}
|
|
3336
|
+
function publicHubChat(raw) {
|
|
3337
|
+
if (typeof raw !== "object" || raw === null)
|
|
3338
|
+
return unavailableHubChat();
|
|
3339
|
+
const record = raw;
|
|
3340
|
+
const peers = Array.isArray(record.peers) ? record.peers : [];
|
|
3341
|
+
const messages = Array.isArray(record.messages) ? record.messages : [];
|
|
3342
|
+
return {
|
|
3343
|
+
status: "ok",
|
|
3344
|
+
canChat: record.canChat === true,
|
|
3345
|
+
peers: peers
|
|
3346
|
+
.filter((peer) => typeof peer === "string" && peer.length > 0)
|
|
3347
|
+
.slice(0, HUB_CHAT_PEERS_MAX),
|
|
3348
|
+
messages: messages.flatMap(publicHubChatMessage).slice(-HUB_CHAT_MESSAGES_MAX),
|
|
3349
|
+
cursor: asOptionalString(record.cursor),
|
|
3350
|
+
};
|
|
3351
|
+
}
|
|
3352
|
+
function publicHubChatMessage(raw) {
|
|
3353
|
+
if (typeof raw !== "object" || raw === null)
|
|
3354
|
+
return [];
|
|
3355
|
+
const record = raw;
|
|
3356
|
+
const id = asOptionalString(record.id);
|
|
3357
|
+
const fromUserId = asOptionalString(record.fromUserId);
|
|
3358
|
+
const text = asOptionalString(record.text);
|
|
3359
|
+
const at = asOptionalString(record.at);
|
|
3360
|
+
if (!id || !fromUserId || !text || !at)
|
|
3361
|
+
return [];
|
|
3362
|
+
return [
|
|
3363
|
+
{
|
|
3364
|
+
id,
|
|
3365
|
+
fromUserId,
|
|
3366
|
+
fromUsername: asOptionalString(record.fromUsername),
|
|
3367
|
+
text: text.slice(0, HUB_CHAT_TEXT_MAX),
|
|
3368
|
+
at,
|
|
3369
|
+
},
|
|
3370
|
+
];
|
|
3371
|
+
}
|
|
3372
|
+
function publicHubSay(raw) {
|
|
3373
|
+
const record = (typeof raw === "object" && raw !== null ? raw : {});
|
|
3374
|
+
const status = typeof record.status === "string" && HUB_SAY_STATUSES.has(record.status)
|
|
3375
|
+
? record.status
|
|
3376
|
+
: "refused";
|
|
3377
|
+
return { status, id: status === "sent" ? asOptionalString(record.id) : null };
|
|
3378
|
+
}
|
|
3379
|
+
function runHubCommand(ctx, command, params, caps, signal) {
|
|
3380
|
+
const hub = caps.hub;
|
|
3381
|
+
switch (command) {
|
|
3382
|
+
case "hub.go": {
|
|
3383
|
+
const doorId = asString(params.doorId, "doorId", command);
|
|
3384
|
+
return hub ? hub.go(doorId, ctx, signal) : Promise.resolve(unavailableHubHandoff());
|
|
3385
|
+
}
|
|
3386
|
+
case "hub.back":
|
|
3387
|
+
return hub ? hub.back(ctx, signal) : Promise.resolve(unavailableHubHandoff());
|
|
3388
|
+
case "hub.doors":
|
|
3389
|
+
return hub ? hub.doors(ctx) : Promise.resolve({ status: "unavailable", doors: null });
|
|
3390
|
+
case "hub.friends":
|
|
3391
|
+
return hub?.friends
|
|
3392
|
+
? hub.friends(ctx)
|
|
3393
|
+
: Promise.resolve({ status: "unavailable", friends: null });
|
|
3394
|
+
case "hub.jump": {
|
|
3395
|
+
const userId = asString(params.userId, "userId", command);
|
|
3396
|
+
return hub?.jump ? hub.jump(userId, ctx, signal) : Promise.resolve(unavailableHubHandoff());
|
|
3397
|
+
}
|
|
3398
|
+
case "hub.report": {
|
|
3399
|
+
const userId = asString(params.userId, "userId", command);
|
|
3400
|
+
const reason = asOptionalString(params.reason);
|
|
3401
|
+
return hub?.report
|
|
3402
|
+
? hub.report(userId, reason, ctx)
|
|
3403
|
+
: Promise.resolve({ status: "unavailable" });
|
|
3404
|
+
}
|
|
3405
|
+
case "hub.chat":
|
|
3406
|
+
return hub?.chat
|
|
3407
|
+
? hub.chat(asOptionalString(params.after), ctx)
|
|
3408
|
+
: Promise.resolve(unavailableHubChat());
|
|
3409
|
+
case "hub.say": {
|
|
3410
|
+
const text = asString(params.text, "text", command);
|
|
3411
|
+
return hub?.say ? hub.say(text, ctx) : Promise.resolve({ status: "unavailable", id: null });
|
|
3412
|
+
}
|
|
3413
|
+
case "hub.reportChat": {
|
|
3414
|
+
const messageId = asString(params.messageId, "messageId", command);
|
|
3415
|
+
return hub?.reportChat
|
|
3416
|
+
? hub.reportChat(messageId, ctx)
|
|
3417
|
+
: Promise.resolve({ status: "unavailable" });
|
|
3418
|
+
}
|
|
3419
|
+
case "hub.commit": {
|
|
3420
|
+
const nonce = asString(params.nonce, "nonce", command);
|
|
3421
|
+
return hub ? hub.commit(nonce, ctx) : Promise.resolve({ status: "unavailable" });
|
|
3422
|
+
}
|
|
3423
|
+
case "hub.cancel":
|
|
3424
|
+
return hub
|
|
3425
|
+
? hub.cancel(asOptionalString(params.nonce), ctx)
|
|
3426
|
+
: Promise.resolve({ status: "unavailable" });
|
|
3427
|
+
}
|
|
3428
|
+
}
|