castle-web-cli 0.4.157 → 0.4.158
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-failures.d.ts +1 -0
- package/dist/agent-failures.js +6 -4
- package/dist/agent-prompts.js +4 -0
- package/dist/agent.d.ts +11 -0
- package/dist/agent.js +175 -15
- package/dist/bundle.js +8 -2
- package/dist/castle-host/host.js +4 -2
- package/dist/castleJson.js +6 -1
- package/dist/check.d.ts +1 -0
- package/dist/check.js +25 -0
- package/dist/devSessionServer.js +31 -7
- package/dist/imports.js +1 -19
- package/dist/index.js +15 -1
- package/dist/init.d.ts +2 -2
- package/dist/init.js +78 -42
- package/dist/mcpPlaytest.d.ts +1 -0
- package/dist/mcpPlaytest.js +182 -0
- package/dist/metering.d.ts +24 -0
- package/dist/metering.js +87 -0
- package/dist/sdkResolve.d.ts +3 -0
- package/dist/sdkResolve.js +55 -0
- package/dist/serve.js +50 -4
- package/dist/serverBundle.js +8 -3
- package/dist/shell/assets/{index-DsDkYFvP.js → index-BuSYLy-C.js} +64 -64
- package/dist/shell/assets/{index-DwezawBs.css → index-DfZmu3XC.css} +1 -1
- package/dist/shell/index.html +2 -2
- package/dist/vitePlugins.d.ts +1 -0
- package/dist/vitePlugins.js +20 -0
- package/kits/base/CLAUDE.md +196 -0
- package/kits/{blank → base}/castle.json +5 -2
- package/kits/base/editorHost.js +172 -0
- package/kits/{blank → base}/node_modules/.package-lock.json +3 -2
- package/kits/{blank → base}/package-lock.json +7 -4
- package/kits/{blank → base}/package.json +4 -2
- package/kits/base/sdk/README.md +569 -0
- package/kits/base/sdk/castle.d.ts +27 -0
- package/kits/base/sdk/castle.js +16 -0
- package/kits/base/sdk/commands.d.ts +181 -0
- package/kits/base/sdk/commands.js +16 -0
- package/kits/base/sdk/context.d.ts +17 -0
- package/kits/base/sdk/context.js +15 -0
- package/kits/base/sdk/errors.d.ts +22 -0
- package/kits/base/sdk/errors.js +16 -0
- package/kits/base/sdk/files.d.ts +15 -0
- package/kits/base/sdk/files.js +52 -0
- package/kits/base/sdk/haptics.d.ts +6 -0
- package/kits/base/sdk/haptics.js +36 -0
- package/kits/base/sdk/leaderboard.d.ts +26 -0
- package/kits/base/sdk/leaderboard.js +296 -0
- package/kits/base/sdk/lifecycle.d.ts +4 -0
- package/kits/base/sdk/lifecycle.js +13 -0
- package/kits/base/sdk/multiplayer.d.ts +43 -0
- package/kits/base/sdk/multiplayer.js +464 -0
- package/kits/base/sdk/multiplayerProtocol.d.ts +51 -0
- package/kits/base/sdk/multiplayerProtocol.js +5 -0
- package/kits/base/sdk/passes.d.ts +7 -0
- package/kits/base/sdk/passes.js +84 -0
- package/kits/base/sdk/portal.d.ts +7 -0
- package/kits/base/sdk/portal.js +35 -0
- package/kits/base/sdk/runtime.d.ts +62 -0
- package/kits/base/sdk/runtime.js +970 -0
- package/kits/base/sdk/saveQueue.d.ts +27 -0
- package/kits/base/sdk/saveQueue.js +205 -0
- package/kits/base/sdk/server/platformHandle.d.ts +25 -0
- package/kits/base/sdk/server/platformHandle.js +1 -0
- package/kits/base/sdk/server/wrapper.d.ts +28 -0
- package/kits/base/sdk/server/wrapper.js +109 -0
- package/kits/base/sdk/storage.d.ts +16 -0
- package/kits/base/sdk/storage.js +323 -0
- package/kits/base/sdk/time.d.ts +17 -0
- package/kits/base/sdk/time.js +120 -0
- package/kits/base/sdk/transport.d.ts +17 -0
- package/kits/base/sdk/transport.js +141 -0
- package/kits/base/sdk/types.d.ts +3 -0
- package/kits/base/sdk/types.js +1 -0
- package/kits/base/sdk/user.d.ts +10 -0
- package/kits/base/sdk/user.js +45 -0
- package/kits/multiplayer-2d/.prettierrc +5 -0
- package/kits/multiplayer-2d/CLAUDE.md +129 -0
- package/kits/multiplayer-2d/behaviors/Box.jsx +92 -0
- package/kits/multiplayer-2d/behaviors/Player.jsx +86 -0
- package/kits/multiplayer-2d/blueprints/crate.scene +13 -0
- package/kits/multiplayer-2d/blueprints/other-player.scene +13 -0
- package/kits/multiplayer-2d/blueprints/player.scene +15 -0
- package/kits/multiplayer-2d/blueprints/wall.scene +12 -0
- package/kits/multiplayer-2d/castle.json +39 -0
- package/kits/multiplayer-2d/code/client/avatars.js +79 -0
- package/kits/multiplayer-2d/code/client/poses.js +168 -0
- package/kits/multiplayer-2d/code/server/index.js +26 -0
- package/kits/multiplayer-2d/code/server/limits.js +99 -0
- package/kits/multiplayer-2d/code/server/ownership.js +177 -0
- package/kits/multiplayer-2d/code/server/players.js +127 -0
- package/kits/multiplayer-2d/code/server/sceneBodies.js +98 -0
- package/kits/multiplayer-2d/code/server/world.js +337 -0
- package/kits/multiplayer-2d/code/systems/multiplayer.js +424 -0
- package/kits/multiplayer-2d/eslint.config.js +21 -0
- package/kits/multiplayer-2d/fonts.generated.js +9 -0
- package/kits/multiplayer-2d/index.html +25 -0
- package/kits/multiplayer-2d/main.jsx +10 -0
- package/kits/multiplayer-2d/package-lock.json +351 -0
- package/kits/multiplayer-2d/package.json +30 -0
- package/kits/multiplayer-2d/scenes/main.scene +20 -0
- package/kits/multiplayer-2d/test/two-client-test.mjs +341 -0
- package/kits/multiplayer-2d/theme.style +4 -0
- package/kits/multiplayer-3d/.prettierrc +5 -0
- package/kits/multiplayer-3d/CLAUDE.md +215 -0
- package/kits/multiplayer-3d/blueprints/ball.scene +21 -0
- package/kits/multiplayer-3d/blueprints/other-player.scene +21 -0
- package/kits/multiplayer-3d/castle.json +43 -0
- package/kits/multiplayer-3d/code/client/avatars.js +136 -0
- package/kits/multiplayer-3d/code/client/nameTags.js +101 -0
- package/kits/multiplayer-3d/code/client/poses.js +220 -0
- package/kits/multiplayer-3d/code/server/index.js +23 -0
- package/kits/multiplayer-3d/code/server/limits.js +107 -0
- package/kits/multiplayer-3d/code/server/ownership.js +157 -0
- package/kits/multiplayer-3d/code/server/players.js +114 -0
- package/kits/multiplayer-3d/code/server/sceneBodies.js +88 -0
- package/kits/multiplayer-3d/code/server/world.js +306 -0
- package/kits/multiplayer-3d/code/systems/multiplayer.js +427 -0
- package/kits/multiplayer-3d/eslint.config.js +20 -0
- package/kits/multiplayer-3d/fonts.generated.js +9 -0
- package/kits/multiplayer-3d/index.html +22 -0
- package/kits/multiplayer-3d/main.jsx +9 -0
- package/kits/multiplayer-3d/package-lock.json +368 -0
- package/kits/multiplayer-3d/package.json +32 -0
- package/kits/multiplayer-3d/scenes/main.scene +409 -0
- package/kits/multiplayer-3d/theme.style +3 -0
- package/kits/physics-2d/CLAUDE.md +6 -0
- package/kits/physics-2d/castle.json +7 -1
- package/kits/physics-2d/editors/behaviorRegistry.js +5 -0
- package/kits/physics-2d/editors/editorRegistry.jsx +3 -1
- package/kits/physics-2d/engine/files.js +9 -5
- package/kits/physics-2d/engine/systemRegistry.js +2 -0
- package/kits/physics-2d/engine/ui.module.css +11 -0
- package/kits/physics-2d/package-lock.json +8 -2
- package/kits/physics-2d/package.json +2 -2
- package/kits/physics-3d/CLAUDE.md +4 -4
- package/kits/physics-3d/behaviors/Shape.jsx +1 -1
- package/kits/physics-3d/blueprints/crate.scene +1 -1
- package/kits/physics-3d/blueprints/door.scene +4 -4
- package/kits/physics-3d/blueprints/pillar.scene +1 -1
- package/kits/physics-3d/blueprints/platform.scene +1 -1
- package/kits/physics-3d/blueprints/statue.scene +1 -1
- package/kits/physics-3d/blueprints/wall.scene +1 -1
- package/kits/physics-3d/castle.json +8 -11
- package/kits/physics-3d/docs/pxmodel-format.md +1 -1
- package/kits/physics-3d/engine3d/materials.js +3 -3
- package/kits/physics-3d/engine3d/modelFiles.js +4 -2
- package/kits/physics-3d/engine3d/world3d.js +1 -1
- package/kits/physics-3d/fonts.generated.js +9 -0
- package/kits/physics-3d/main.jsx +1 -1
- package/kits/physics-3d/package-lock.json +85 -2
- package/kits/physics-3d/package.json +6 -2
- package/kits/physics-3d/scenes/main.scene +1 -1
- package/kits/physics-3d/scenes/model-lab.scene +1 -1
- package/kits/real-time/.prettierrc +5 -0
- package/kits/real-time/CLAUDE.md +60 -0
- package/kits/real-time/castle.json +19 -0
- package/kits/real-time/code/client/clock.js +68 -0
- package/kits/real-time/code/client/connection.js +134 -0
- package/kits/real-time/code/client/gameHooks.js +50 -0
- package/kits/real-time/code/client/interpolate.js +82 -0
- package/kits/real-time/code/client/messages.js +55 -0
- package/kits/real-time/code/client/ownership.js +94 -0
- package/kits/real-time/code/client/smooth.js +54 -0
- package/kits/real-time/code/server/blockDelta.js +48 -0
- package/kits/real-time/code/server/gameHooks.js +47 -0
- package/kits/real-time/code/server/limits.js +35 -0
- package/kits/real-time/code/server/session.js +251 -0
- package/kits/real-time/eslint.config.js +22 -0
- package/kits/real-time/index.html +45 -0
- package/kits/real-time/package.json +14 -0
- package/kits/{tabletop → turn-based}/CLAUDE.md +42 -49
- package/kits/turn-based/blueprints/board.scene +15 -0
- package/kits/turn-based/blueprints/square.scene +25 -0
- package/kits/turn-based/castle.json +37 -0
- package/kits/turn-based/code/behaviors/Board.jsx +169 -0
- package/kits/turn-based/code/behaviors/Square.jsx +26 -0
- package/kits/turn-based/code/server/index.js +152 -0
- package/kits/turn-based/drawings/square.sprite +25 -0
- package/kits/turn-based/eslint.config.js +12 -0
- package/kits/turn-based/fonts.generated.js +9 -0
- package/kits/{tabletop → turn-based}/index.html +5 -3
- package/kits/{tabletop → turn-based}/main.jsx +2 -2
- package/kits/{tabletop → turn-based}/package-lock.json +17 -4
- package/kits/{tabletop → turn-based}/package.json +5 -2
- package/kits/turn-based/room.js +289 -0
- package/kits/turn-based/scenes/main.scene +227 -0
- package/kits/{tabletop → turn-based}/testing.js +131 -99
- package/kits/turn-based/theme.style +3 -0
- package/package.json +1 -1
- package/kits/blank/CLAUDE.md +0 -198
- package/kits/tabletop/castle.json +0 -34
- package/kits/tabletop/room.js +0 -269
- /package/kits/{blank → base}/index.html +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/README.md +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/catalog.d.ts +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/catalog.js +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/face.d.ts +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/face.js +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/Bore.d.ts +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/Bore.js +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/CourierPrime.d.ts +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/CourierPrime.js +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/DMSans.d.ts +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/DMSans.js +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/Glacier.d.ts +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/Glacier.js +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/HelicoCentrica.d.ts +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/HelicoCentrica.js +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/Piazzolla.d.ts +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/Piazzolla.js +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/Synco.d.ts +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/Synco.js +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/Tektur.d.ts +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/Tektur.js +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/YatraOne.d.ts +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/YatraOne.js +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/index.d.ts +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/index.js +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/licenses/CC-BY-ND-4.0.txt +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/licenses/FONTS.md +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/licenses/OFL-1.1.txt +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/package.json +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/woff2/Bore.woff2 +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/woff2/CourierPrime.woff2 +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/woff2/DMSans.woff2 +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/woff2/Glacier.woff2 +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/woff2/HelicoCentrica.woff2 +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/woff2/Piazzolla.woff2 +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/woff2/SOURCE.txt +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/woff2/Synco.woff2 +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/woff2/Tektur.woff2 +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/woff2/YatraOne.woff2 +0 -0
- /package/kits/physics-3d/drawings/{crate.pxart → crate.sprite} +0 -0
- /package/kits/physics-3d/drawings/{door-edge.pxart → door-edge.sprite} +0 -0
- /package/kits/physics-3d/drawings/{door.pxart → door.sprite} +0 -0
- /package/kits/physics-3d/drawings/{face.pxart → face.sprite} +0 -0
- /package/kits/physics-3d/drawings/{floor.pxart → floor.sprite} +0 -0
- /package/kits/physics-3d/drawings/{platform.pxart → platform.sprite} +0 -0
- /package/kits/physics-3d/drawings/{statue.pxart → statue.sprite} +0 -0
- /package/kits/physics-3d/drawings/{wall.pxart → wall.sprite} +0 -0
- /package/kits/{tabletop → turn-based}/.prettierrc +0 -0
|
@@ -0,0 +1,464 @@
|
|
|
1
|
+
import { CastleError } from "./errors";
|
|
2
|
+
import { MAX_CLIENT_FRAME_BYTES, } from "./multiplayerProtocol";
|
|
3
|
+
import { hostRequest } from "./transport";
|
|
4
|
+
const KEEPALIVE_INTERVAL_MS = 25_000;
|
|
5
|
+
const HELLO_TIMEOUT_MS = 10_000;
|
|
6
|
+
const RECONNECT_BUDGET_MS = 30_000;
|
|
7
|
+
const RECONNECT_DELAYS_MS = [500, 1_000, 2_000, 4_000, 8_000];
|
|
8
|
+
let activeConnection = null;
|
|
9
|
+
export const Multiplayer = { join };
|
|
10
|
+
async function join(options) {
|
|
11
|
+
const params = joinParams(options);
|
|
12
|
+
activeConnection?.leave();
|
|
13
|
+
const connection = new MultiplayerConnectionImpl(params, () => {
|
|
14
|
+
if (activeConnection === connection)
|
|
15
|
+
activeConnection = null;
|
|
16
|
+
});
|
|
17
|
+
activeConnection = connection;
|
|
18
|
+
await connection.start();
|
|
19
|
+
return connection;
|
|
20
|
+
}
|
|
21
|
+
class MultiplayerConnectionImpl {
|
|
22
|
+
params;
|
|
23
|
+
onClosed;
|
|
24
|
+
currentState = "acquiring";
|
|
25
|
+
currentPlayerId = "";
|
|
26
|
+
currentSessionId = "";
|
|
27
|
+
solo = false;
|
|
28
|
+
baseUrl = "";
|
|
29
|
+
reconnectToken = "";
|
|
30
|
+
socket = null;
|
|
31
|
+
socketAttempt = 0;
|
|
32
|
+
keepalive = null;
|
|
33
|
+
reAcquisitionUsed = false;
|
|
34
|
+
recovering = false;
|
|
35
|
+
roster = null;
|
|
36
|
+
messages = new Set();
|
|
37
|
+
rosters = new Set();
|
|
38
|
+
states = new Set();
|
|
39
|
+
constructor(params, onClosed) {
|
|
40
|
+
this.params = params;
|
|
41
|
+
this.onClosed = onClosed;
|
|
42
|
+
}
|
|
43
|
+
get state() {
|
|
44
|
+
return this.currentState;
|
|
45
|
+
}
|
|
46
|
+
get playerId() {
|
|
47
|
+
return this.currentPlayerId;
|
|
48
|
+
}
|
|
49
|
+
get sessionId() {
|
|
50
|
+
return this.currentSessionId;
|
|
51
|
+
}
|
|
52
|
+
get isSolo() {
|
|
53
|
+
return this.solo;
|
|
54
|
+
}
|
|
55
|
+
async start() {
|
|
56
|
+
try {
|
|
57
|
+
await this.acquire();
|
|
58
|
+
}
|
|
59
|
+
catch (error) {
|
|
60
|
+
const castleError = asCastleError(error);
|
|
61
|
+
if (this.currentState !== "closed") {
|
|
62
|
+
this.finish({ type: "error", error: castleError });
|
|
63
|
+
}
|
|
64
|
+
throw castleError;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
// Unserializable data is a mistake in the deck and still throws. A missing
|
|
68
|
+
// socket is not: connections drop whenever a phone sleeps or changes network,
|
|
69
|
+
// and the session recovers on its own. A realtime deck sends every frame and
|
|
70
|
+
// has no useful branch to take meanwhile, so those messages are dropped.
|
|
71
|
+
// State arrives in whole snapshots, so the next send past recovery is current.
|
|
72
|
+
send(data) {
|
|
73
|
+
const text = serializeClientMessage(data);
|
|
74
|
+
if (this.currentState === "closed") {
|
|
75
|
+
throw multiplayerError("MULTIPLAYER_NOT_CONNECTED", "Multiplayer.send requires a connected session.", "Multiplayer.send");
|
|
76
|
+
}
|
|
77
|
+
if (this.solo)
|
|
78
|
+
return;
|
|
79
|
+
if (!this.socket || this.socket.readyState !== WebSocket.OPEN)
|
|
80
|
+
return;
|
|
81
|
+
this.socket.send(text);
|
|
82
|
+
}
|
|
83
|
+
onMessage(callback) {
|
|
84
|
+
this.messages.add(callback);
|
|
85
|
+
return () => this.messages.delete(callback);
|
|
86
|
+
}
|
|
87
|
+
onRoster(callback) {
|
|
88
|
+
this.rosters.add(callback);
|
|
89
|
+
if (this.roster)
|
|
90
|
+
callListener(callback, this.roster);
|
|
91
|
+
return () => this.rosters.delete(callback);
|
|
92
|
+
}
|
|
93
|
+
onStateChange(callback) {
|
|
94
|
+
this.states.add(callback);
|
|
95
|
+
callStateListener(callback, this.currentState);
|
|
96
|
+
return () => this.states.delete(callback);
|
|
97
|
+
}
|
|
98
|
+
leave() {
|
|
99
|
+
if (this.currentState === "closed")
|
|
100
|
+
return;
|
|
101
|
+
this.finish({ type: "left" });
|
|
102
|
+
}
|
|
103
|
+
async acquire() {
|
|
104
|
+
this.transition("acquiring");
|
|
105
|
+
const result = await hostRequest("multiplayer.getSession", this.params);
|
|
106
|
+
this.assertOpen();
|
|
107
|
+
if (result.status === "solo") {
|
|
108
|
+
this.connectSolo(result);
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
if (result.status !== "ok" || !result.url || !result.nonce) {
|
|
112
|
+
throw multiplayerError("MULTIPLAYER_UNAVAILABLE", "Multiplayer is unavailable for this deck.");
|
|
113
|
+
}
|
|
114
|
+
this.solo = false;
|
|
115
|
+
this.baseUrl = result.url;
|
|
116
|
+
this.transition("connecting");
|
|
117
|
+
await this.openSocket("nonce", result.nonce);
|
|
118
|
+
}
|
|
119
|
+
connectSolo(result) {
|
|
120
|
+
this.solo = true;
|
|
121
|
+
this.currentPlayerId = "solo";
|
|
122
|
+
this.currentSessionId = result.sessionId ?? "solo";
|
|
123
|
+
const you = {
|
|
124
|
+
playerId: this.currentPlayerId,
|
|
125
|
+
userId: "solo",
|
|
126
|
+
username: "You",
|
|
127
|
+
isAnonymous: true,
|
|
128
|
+
};
|
|
129
|
+
this.setRoster({ players: [you], you });
|
|
130
|
+
this.transition("connected");
|
|
131
|
+
}
|
|
132
|
+
openSocket(credential, value) {
|
|
133
|
+
const attempt = ++this.socketAttempt;
|
|
134
|
+
const socket = new WebSocket(authenticatedUrl(this.baseUrl, credential, value));
|
|
135
|
+
this.socket = socket;
|
|
136
|
+
return new Promise((resolve, reject) => {
|
|
137
|
+
let greeted = false;
|
|
138
|
+
const timeout = setTimeout(() => {
|
|
139
|
+
reject(new SocketClosedError(1006));
|
|
140
|
+
closeSocket(socket, 1000, "hello timeout");
|
|
141
|
+
}, HELLO_TIMEOUT_MS);
|
|
142
|
+
socket.onmessage = (event) => {
|
|
143
|
+
if (this.socketAttempt !== attempt || this.currentState === "closed")
|
|
144
|
+
return;
|
|
145
|
+
const frame = parsePlatformFrame(event.data);
|
|
146
|
+
if (!frame)
|
|
147
|
+
return;
|
|
148
|
+
if (frame.t === "hello") {
|
|
149
|
+
greeted = true;
|
|
150
|
+
clearTimeout(timeout);
|
|
151
|
+
this.acceptHello(frame);
|
|
152
|
+
resolve();
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
this.handleFrame(frame);
|
|
156
|
+
};
|
|
157
|
+
socket.onclose = (event) => {
|
|
158
|
+
clearTimeout(timeout);
|
|
159
|
+
if (this.socketAttempt !== attempt) {
|
|
160
|
+
if (this.currentState === "closed") {
|
|
161
|
+
reject(new SocketClosedError(event.code));
|
|
162
|
+
}
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
if (this.socket === socket)
|
|
166
|
+
this.socket = null;
|
|
167
|
+
this.stopKeepalive();
|
|
168
|
+
if (!greeted) {
|
|
169
|
+
reject(new SocketClosedError(event.code));
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
if (this.currentState === "connected")
|
|
173
|
+
this.beginRecovery();
|
|
174
|
+
};
|
|
175
|
+
socket.onerror = () => {
|
|
176
|
+
// Browsers provide actionable close information through `onclose`.
|
|
177
|
+
};
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
acceptHello(frame) {
|
|
181
|
+
this.currentPlayerId = frame.playerId;
|
|
182
|
+
this.currentSessionId = frame.sessionId;
|
|
183
|
+
this.reconnectToken = frame.reconnectToken;
|
|
184
|
+
// Re-acquisition is limited to one per outage, not one per session. A phone
|
|
185
|
+
// is backgrounded many times in a play, and each of those is its own outage;
|
|
186
|
+
// without this the second one finds the budget spent and closes for good.
|
|
187
|
+
this.reAcquisitionUsed = false;
|
|
188
|
+
this.transition("connected");
|
|
189
|
+
this.startKeepalive();
|
|
190
|
+
}
|
|
191
|
+
handleFrame(frame) {
|
|
192
|
+
switch (frame.t) {
|
|
193
|
+
case "roster":
|
|
194
|
+
this.acceptRoster(frame);
|
|
195
|
+
break;
|
|
196
|
+
case "m":
|
|
197
|
+
for (const callback of this.messages)
|
|
198
|
+
callListener(callback, frame.d);
|
|
199
|
+
break;
|
|
200
|
+
case "replaced":
|
|
201
|
+
this.finish({ type: "replaced" });
|
|
202
|
+
break;
|
|
203
|
+
case "bye":
|
|
204
|
+
this.finish({ type: "bye", reason: frame.reason });
|
|
205
|
+
break;
|
|
206
|
+
case "hello":
|
|
207
|
+
this.acceptHello(frame);
|
|
208
|
+
break;
|
|
209
|
+
case "pong":
|
|
210
|
+
break;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
acceptRoster(frame) {
|
|
214
|
+
const players = frame.players.slice();
|
|
215
|
+
const you = players.find((player) => player.playerId === this.currentPlayerId) ?? null;
|
|
216
|
+
this.setRoster({ players, you });
|
|
217
|
+
}
|
|
218
|
+
setRoster(roster) {
|
|
219
|
+
this.roster = roster;
|
|
220
|
+
for (const callback of this.rosters)
|
|
221
|
+
callListener(callback, roster);
|
|
222
|
+
}
|
|
223
|
+
beginRecovery() {
|
|
224
|
+
if (this.recovering || this.currentState === "closed")
|
|
225
|
+
return;
|
|
226
|
+
this.recovering = true;
|
|
227
|
+
this.transition("reconnecting", { type: "connectionLost" });
|
|
228
|
+
void this.recover()
|
|
229
|
+
.catch((error) => {
|
|
230
|
+
if (this.currentState === "closed")
|
|
231
|
+
return;
|
|
232
|
+
const castleError = asCastleError(error);
|
|
233
|
+
this.finish({ type: "error", error: castleError });
|
|
234
|
+
})
|
|
235
|
+
.finally(() => {
|
|
236
|
+
this.recovering = false;
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
async recover() {
|
|
240
|
+
const startedAt = Date.now();
|
|
241
|
+
let attempt = 0;
|
|
242
|
+
while (Date.now() - startedAt < RECONNECT_BUDGET_MS) {
|
|
243
|
+
const delay = RECONNECT_DELAYS_MS[Math.min(attempt, RECONNECT_DELAYS_MS.length - 1)];
|
|
244
|
+
attempt += 1;
|
|
245
|
+
await pause(delay);
|
|
246
|
+
this.assertOpen();
|
|
247
|
+
try {
|
|
248
|
+
await this.openSocket("reconnect", this.reconnectToken);
|
|
249
|
+
return;
|
|
250
|
+
}
|
|
251
|
+
catch (error) {
|
|
252
|
+
if (error instanceof SocketClosedError && error.code === 1008)
|
|
253
|
+
break;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
await this.reacquireOnce();
|
|
257
|
+
}
|
|
258
|
+
async reacquireOnce() {
|
|
259
|
+
if (this.reAcquisitionUsed) {
|
|
260
|
+
throw this.closeAfterRecovery();
|
|
261
|
+
}
|
|
262
|
+
this.reAcquisitionUsed = true;
|
|
263
|
+
try {
|
|
264
|
+
await this.acquire();
|
|
265
|
+
}
|
|
266
|
+
catch (error) {
|
|
267
|
+
const castleError = asCastleError(error);
|
|
268
|
+
this.finish({ type: "error", error: castleError });
|
|
269
|
+
throw castleError;
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
closeAfterRecovery() {
|
|
273
|
+
const error = multiplayerError("MULTIPLAYER_CONNECTION_LOST", "The multiplayer connection could not be restored.");
|
|
274
|
+
this.finish({ type: "error", error });
|
|
275
|
+
return error;
|
|
276
|
+
}
|
|
277
|
+
startKeepalive() {
|
|
278
|
+
this.stopKeepalive();
|
|
279
|
+
this.keepalive = setInterval(() => {
|
|
280
|
+
if (this.socket?.readyState === WebSocket.OPEN) {
|
|
281
|
+
try {
|
|
282
|
+
this.socket.send('{"t":"ping"}');
|
|
283
|
+
}
|
|
284
|
+
catch {
|
|
285
|
+
closeSocket(this.socket, 1000, "keepalive failed");
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
}, KEEPALIVE_INTERVAL_MS);
|
|
289
|
+
}
|
|
290
|
+
stopKeepalive() {
|
|
291
|
+
if (this.keepalive === null)
|
|
292
|
+
return;
|
|
293
|
+
clearInterval(this.keepalive);
|
|
294
|
+
this.keepalive = null;
|
|
295
|
+
}
|
|
296
|
+
transition(state, metadata) {
|
|
297
|
+
if (this.currentState === "closed" || this.currentState === state)
|
|
298
|
+
return;
|
|
299
|
+
this.currentState = state;
|
|
300
|
+
for (const callback of this.states)
|
|
301
|
+
callStateListener(callback, state, metadata);
|
|
302
|
+
}
|
|
303
|
+
finish(metadata) {
|
|
304
|
+
if (this.currentState === "closed")
|
|
305
|
+
return;
|
|
306
|
+
this.stopKeepalive();
|
|
307
|
+
const socket = this.socket;
|
|
308
|
+
this.socket = null;
|
|
309
|
+
this.socketAttempt += 1;
|
|
310
|
+
closeSocket(socket, 1000, "session closed");
|
|
311
|
+
this.currentState = "closed";
|
|
312
|
+
for (const callback of this.states)
|
|
313
|
+
callStateListener(callback, "closed", metadata);
|
|
314
|
+
this.onClosed();
|
|
315
|
+
}
|
|
316
|
+
assertOpen() {
|
|
317
|
+
if (this.currentState === "closed") {
|
|
318
|
+
throw multiplayerError("MULTIPLAYER_CONNECTION_CLOSED", "The multiplayer connection is closed.");
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
class SocketClosedError extends Error {
|
|
323
|
+
code;
|
|
324
|
+
constructor(code) {
|
|
325
|
+
super("Multiplayer WebSocket closed before hello.");
|
|
326
|
+
this.code = code;
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
function joinParams(options) {
|
|
330
|
+
if (!options || typeof options !== "object") {
|
|
331
|
+
throw invalidJoinOptions();
|
|
332
|
+
}
|
|
333
|
+
if (options.mode === "named") {
|
|
334
|
+
if (typeof options.key !== "string" || options.key.length === 0) {
|
|
335
|
+
throw invalidJoinOptions();
|
|
336
|
+
}
|
|
337
|
+
return { mode: "named", key: options.key };
|
|
338
|
+
}
|
|
339
|
+
if (options.mode === "party" || options.mode === "public") {
|
|
340
|
+
return { mode: options.mode, key: null };
|
|
341
|
+
}
|
|
342
|
+
throw invalidJoinOptions();
|
|
343
|
+
}
|
|
344
|
+
function invalidJoinOptions() {
|
|
345
|
+
return multiplayerError("INVALID_ARGUMENT", "Multiplayer.join requires named, party, or public options.");
|
|
346
|
+
}
|
|
347
|
+
function serializeClientMessage(data) {
|
|
348
|
+
let text;
|
|
349
|
+
try {
|
|
350
|
+
const serializedData = JSON.stringify(data);
|
|
351
|
+
if (serializedData === undefined)
|
|
352
|
+
throw new Error("not serializable");
|
|
353
|
+
const serializedFrame = JSON.stringify({ t: "m", d: data });
|
|
354
|
+
if (serializedFrame === undefined)
|
|
355
|
+
throw new Error("not serializable");
|
|
356
|
+
text = serializedFrame;
|
|
357
|
+
}
|
|
358
|
+
catch {
|
|
359
|
+
throw multiplayerError("INVALID_ARGUMENT", "Multiplayer.send data must be JSON-serializable.", "Multiplayer.send");
|
|
360
|
+
}
|
|
361
|
+
if (new TextEncoder().encode(text).byteLength > MAX_CLIENT_FRAME_BYTES) {
|
|
362
|
+
throw multiplayerError("MULTIPLAYER_MESSAGE_TOO_LARGE", `Multiplayer.send messages are limited to ${MAX_CLIENT_FRAME_BYTES} bytes.`, "Multiplayer.send");
|
|
363
|
+
}
|
|
364
|
+
return text;
|
|
365
|
+
}
|
|
366
|
+
function authenticatedUrl(baseUrl, credential, value) {
|
|
367
|
+
const fallback = typeof window === "undefined" ? undefined : window.location.href;
|
|
368
|
+
const url = new URL(baseUrl, fallback);
|
|
369
|
+
url.searchParams.delete(credential === "nonce" ? "reconnect" : "nonce");
|
|
370
|
+
url.searchParams.set(credential, value);
|
|
371
|
+
return url.toString();
|
|
372
|
+
}
|
|
373
|
+
function parsePlatformFrame(value) {
|
|
374
|
+
if (typeof value !== "string")
|
|
375
|
+
return null;
|
|
376
|
+
let frame;
|
|
377
|
+
try {
|
|
378
|
+
frame = JSON.parse(value);
|
|
379
|
+
}
|
|
380
|
+
catch {
|
|
381
|
+
return null;
|
|
382
|
+
}
|
|
383
|
+
if (!isRecord(frame) || typeof frame.t !== "string")
|
|
384
|
+
return null;
|
|
385
|
+
switch (frame.t) {
|
|
386
|
+
case "hello":
|
|
387
|
+
return isHelloFrame(frame) ? frame : null;
|
|
388
|
+
case "roster":
|
|
389
|
+
return isRosterFrame(frame) ? frame : null;
|
|
390
|
+
case "m":
|
|
391
|
+
return Object.prototype.hasOwnProperty.call(frame, "d")
|
|
392
|
+
? frame
|
|
393
|
+
: null;
|
|
394
|
+
case "replaced":
|
|
395
|
+
case "pong":
|
|
396
|
+
return frame;
|
|
397
|
+
case "bye":
|
|
398
|
+
return frame.reason === undefined || typeof frame.reason === "string"
|
|
399
|
+
? frame
|
|
400
|
+
: null;
|
|
401
|
+
default:
|
|
402
|
+
return null;
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
function isHelloFrame(frame) {
|
|
406
|
+
return (typeof frame.playerId === "string" &&
|
|
407
|
+
typeof frame.reconnectToken === "string" &&
|
|
408
|
+
typeof frame.sessionId === "string" &&
|
|
409
|
+
isMode(frame.mode));
|
|
410
|
+
}
|
|
411
|
+
function isRosterFrame(frame) {
|
|
412
|
+
return Array.isArray(frame.players) && frame.players.every(isPlayer);
|
|
413
|
+
}
|
|
414
|
+
function isPlayer(value) {
|
|
415
|
+
return (isRecord(value) &&
|
|
416
|
+
typeof value.playerId === "string" &&
|
|
417
|
+
typeof value.userId === "string" &&
|
|
418
|
+
typeof value.username === "string" &&
|
|
419
|
+
typeof value.isAnonymous === "boolean" &&
|
|
420
|
+
(value.scopedUserId === undefined || typeof value.scopedUserId === "string"));
|
|
421
|
+
}
|
|
422
|
+
function isMode(value) {
|
|
423
|
+
return value === "named" || value === "party" || value === "public";
|
|
424
|
+
}
|
|
425
|
+
function isRecord(value) {
|
|
426
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
427
|
+
}
|
|
428
|
+
function pause(milliseconds) {
|
|
429
|
+
return new Promise((resolve) => setTimeout(resolve, milliseconds));
|
|
430
|
+
}
|
|
431
|
+
function closeSocket(socket, code, reason) {
|
|
432
|
+
if (!socket || socket.readyState >= WebSocket.CLOSING)
|
|
433
|
+
return;
|
|
434
|
+
try {
|
|
435
|
+
socket.close(code, reason);
|
|
436
|
+
}
|
|
437
|
+
catch {
|
|
438
|
+
// The socket is already unusable.
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
function callListener(callback, value) {
|
|
442
|
+
try {
|
|
443
|
+
callback(value);
|
|
444
|
+
}
|
|
445
|
+
catch {
|
|
446
|
+
// One deck listener must not break the connection or other listeners.
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
function callStateListener(callback, state, metadata) {
|
|
450
|
+
try {
|
|
451
|
+
callback(state, metadata);
|
|
452
|
+
}
|
|
453
|
+
catch {
|
|
454
|
+
// One deck listener must not break the connection or other listeners.
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
function multiplayerError(code, message, operation = "Multiplayer.join") {
|
|
458
|
+
return new CastleError({ code, message, operation });
|
|
459
|
+
}
|
|
460
|
+
function asCastleError(error) {
|
|
461
|
+
if (error instanceof CastleError)
|
|
462
|
+
return error;
|
|
463
|
+
return multiplayerError("MULTIPLAYER_CONNECTION_FAILED", error instanceof Error ? error.message : "The multiplayer connection failed.");
|
|
464
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
export declare const MAX_CLIENT_FRAME_BYTES: number;
|
|
2
|
+
export declare const MAX_PLAYERS_CAP = 24;
|
|
3
|
+
export type SessionType = "named" | "party" | "public";
|
|
4
|
+
export interface PlayerIdentity {
|
|
5
|
+
userId: string;
|
|
6
|
+
username: string;
|
|
7
|
+
isAnonymous: boolean;
|
|
8
|
+
scopedUserId?: string;
|
|
9
|
+
}
|
|
10
|
+
export interface SessionPlayer extends PlayerIdentity {
|
|
11
|
+
playerId: string;
|
|
12
|
+
}
|
|
13
|
+
export interface ClientHelloFrame {
|
|
14
|
+
t: "hello";
|
|
15
|
+
playerId: string;
|
|
16
|
+
reconnectToken: string;
|
|
17
|
+
sessionId: string;
|
|
18
|
+
mode: SessionType;
|
|
19
|
+
}
|
|
20
|
+
export interface ClientRosterFrame {
|
|
21
|
+
t: "roster";
|
|
22
|
+
players: SessionPlayer[];
|
|
23
|
+
}
|
|
24
|
+
export interface ClientMessageFrame {
|
|
25
|
+
t: "m";
|
|
26
|
+
d: unknown;
|
|
27
|
+
}
|
|
28
|
+
export interface ClientReplacedFrame {
|
|
29
|
+
t: "replaced";
|
|
30
|
+
}
|
|
31
|
+
export interface ClientByeFrame {
|
|
32
|
+
t: "bye";
|
|
33
|
+
reason?: string;
|
|
34
|
+
}
|
|
35
|
+
export interface ClientPongFrame {
|
|
36
|
+
t: "pong";
|
|
37
|
+
}
|
|
38
|
+
export type PlatformToClientFrame = ClientHelloFrame | ClientRosterFrame | ClientMessageFrame | ClientReplacedFrame | ClientByeFrame | ClientPongFrame;
|
|
39
|
+
export interface ClientToPlatformMessageFrame {
|
|
40
|
+
t: "m";
|
|
41
|
+
d: unknown;
|
|
42
|
+
}
|
|
43
|
+
export interface ClientPingFrame {
|
|
44
|
+
t: "ping";
|
|
45
|
+
}
|
|
46
|
+
export type ClientToPlatformFrame = ClientToPlatformMessageFrame | ClientPingFrame;
|
|
47
|
+
export type MultiplayerMode = SessionType;
|
|
48
|
+
export type MultiplayerPlayer = SessionPlayer;
|
|
49
|
+
export type MultiplayerHelloFrame = ClientHelloFrame;
|
|
50
|
+
export type MultiplayerRosterFrame = ClientRosterFrame;
|
|
51
|
+
export type PlatformToMultiplayerClientFrame = PlatformToClientFrame;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
// Client-facing Cauldron multiplayer wire contract. This intentionally mirrors
|
|
2
|
+
// cauldron-game-server/shared without importing it: the SDK is independently
|
|
3
|
+
// published and must not pull the server package into deck bundles.
|
|
4
|
+
export const MAX_CLIENT_FRAME_BYTES = 16 * 1024;
|
|
5
|
+
export const MAX_PLAYERS_CAP = 24;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { PassOfferResult } from "./commands";
|
|
2
|
+
export type { PassOfferResult, PassOfferStatus } from "./commands";
|
|
3
|
+
export interface CastlePassApi {
|
|
4
|
+
has(passId: string): Promise<boolean>;
|
|
5
|
+
offer(passId: string): Promise<PassOfferResult>;
|
|
6
|
+
}
|
|
7
|
+
export declare const Pass: CastlePassApi;
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
// Pass — a deck-facing capability for selling a creator "pass" to the player.
|
|
2
|
+
//
|
|
3
|
+
// The deck stays capability-AGNOSTIC: it just offers a pass to the player and
|
|
4
|
+
// gets back one normalized outcome regardless of platform. The host decides what
|
|
5
|
+
// UI to show and whether a real transaction can happen:
|
|
6
|
+
// - mobile app : renders the native bricks purchase sheet over the deck
|
|
7
|
+
// - web player : shows an "open in the app" upsell, returns `unavailable`
|
|
8
|
+
// - dev CLI : no host UI surface, so the SDK shows a minimal in-page
|
|
9
|
+
// notice itself (kept deliberately tiny), returns `unavailable`
|
|
10
|
+
// There is no capability check for the deck to make — every platform returns a
|
|
11
|
+
// PassOfferResult, so a single code path handles them all.
|
|
12
|
+
import { CastleError } from "./errors";
|
|
13
|
+
import { getCommandChannel, hostRequest } from "./transport";
|
|
14
|
+
export const Pass = {
|
|
15
|
+
has,
|
|
16
|
+
offer,
|
|
17
|
+
};
|
|
18
|
+
// Pure read — does the current player already own this pass? No UI, every
|
|
19
|
+
// platform answers it the same way (a GraphQL query). Use it to gate content or
|
|
20
|
+
// to decide whether to bother calling `offer`.
|
|
21
|
+
async function has(passId) {
|
|
22
|
+
if (typeof passId !== "string" || passId.length === 0) {
|
|
23
|
+
throw new CastleError({
|
|
24
|
+
code: "INVALID_ARGUMENT",
|
|
25
|
+
message: "Pass.has requires a passId.",
|
|
26
|
+
operation: "Pass.has",
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
const { hasPass } = await hostRequest("pass.has", { passId });
|
|
30
|
+
return hasPass;
|
|
31
|
+
}
|
|
32
|
+
async function offer(passId) {
|
|
33
|
+
if (typeof passId !== "string" || passId.length === 0) {
|
|
34
|
+
throw new CastleError({
|
|
35
|
+
code: "INVALID_ARGUMENT",
|
|
36
|
+
message: "Pass.offer requires a passId.",
|
|
37
|
+
operation: "Pass.offer",
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
const result = await hostRequest("pass.offer", { passId });
|
|
41
|
+
// On the dev server there's no host chrome to explain why nothing happened,
|
|
42
|
+
// so surface a small built-in notice. The mobile/web hosts render their own
|
|
43
|
+
// UI, so the SDK stays silent there.
|
|
44
|
+
if (result.status === "unavailable" && getCommandChannel() === "local") {
|
|
45
|
+
showDevUnavailableNotice();
|
|
46
|
+
}
|
|
47
|
+
return result;
|
|
48
|
+
}
|
|
49
|
+
let devNoticeEl = null;
|
|
50
|
+
let devNoticeTimer = null;
|
|
51
|
+
// Minimal, dependency-free toast. Dev-only affordance — not the place for a
|
|
52
|
+
// designed purchase UI.
|
|
53
|
+
function showDevUnavailableNotice() {
|
|
54
|
+
if (typeof document === "undefined")
|
|
55
|
+
return;
|
|
56
|
+
if (!devNoticeEl) {
|
|
57
|
+
devNoticeEl = document.createElement("div");
|
|
58
|
+
devNoticeEl.textContent = "Passes can only be purchased in the Castle app.";
|
|
59
|
+
devNoticeEl.style.cssText = [
|
|
60
|
+
"position:fixed",
|
|
61
|
+
"left:50%",
|
|
62
|
+
"bottom:24px",
|
|
63
|
+
"transform:translateX(-50%)",
|
|
64
|
+
"max-width:80vw",
|
|
65
|
+
"padding:10px 16px",
|
|
66
|
+
"border-radius:8px",
|
|
67
|
+
"background:rgba(0,0,0,0.82)",
|
|
68
|
+
"color:#fff",
|
|
69
|
+
"font:500 13px/1.4 system-ui,sans-serif",
|
|
70
|
+
"text-align:center",
|
|
71
|
+
"z-index:2147483647",
|
|
72
|
+
"pointer-events:none",
|
|
73
|
+
"transition:opacity 0.3s ease",
|
|
74
|
+
].join(";");
|
|
75
|
+
document.body.appendChild(devNoticeEl);
|
|
76
|
+
}
|
|
77
|
+
devNoticeEl.style.opacity = "1";
|
|
78
|
+
if (devNoticeTimer)
|
|
79
|
+
clearTimeout(devNoticeTimer);
|
|
80
|
+
devNoticeTimer = setTimeout(() => {
|
|
81
|
+
if (devNoticeEl)
|
|
82
|
+
devNoticeEl.style.opacity = "0";
|
|
83
|
+
}, 3200);
|
|
84
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { PortalOpenResult, PortalPrefetchResult } from "./commands";
|
|
2
|
+
export type { PortalOpenResult, PortalOpenStatus, PortalPrefetchResult, PortalPrefetchStatus, } from "./commands";
|
|
3
|
+
export interface CastlePortalApi {
|
|
4
|
+
open(deckId: string): Promise<PortalOpenResult>;
|
|
5
|
+
prefetch(deckId: string): Promise<PortalPrefetchResult>;
|
|
6
|
+
}
|
|
7
|
+
export declare const Portal: CastlePortalApi;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
// Portal — a deck-facing capability for sending the player to another Castle
|
|
2
|
+
// deck ("following a portal"). The host owns the transition: in the Castle feed
|
|
3
|
+
// it swipes to the target deck (which may itself be a web deck or a normal
|
|
4
|
+
// engine deck); elsewhere (the website, the dev server, the editor) there's no
|
|
5
|
+
// feed to navigate, so it resolves `unavailable` and nothing happens.
|
|
6
|
+
//
|
|
7
|
+
// Navigation is effectively fire-and-forget: once the host accepts it, the
|
|
8
|
+
// source deck is torn down as the feed moves to the target, so a deck should
|
|
9
|
+
// not rely on code running after a successful `open`.
|
|
10
|
+
import { CastleError } from "./errors";
|
|
11
|
+
import { hostRequest } from "./transport";
|
|
12
|
+
export const Portal = {
|
|
13
|
+
open,
|
|
14
|
+
prefetch,
|
|
15
|
+
};
|
|
16
|
+
async function open(deckId) {
|
|
17
|
+
if (typeof deckId !== "string" || deckId.length === 0) {
|
|
18
|
+
throw new CastleError({
|
|
19
|
+
code: "INVALID_ARGUMENT",
|
|
20
|
+
message: "Portal.open requires a deckId.",
|
|
21
|
+
operation: "Portal.open",
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
return hostRequest("portal.open", { targetDeckId: deckId });
|
|
25
|
+
}
|
|
26
|
+
async function prefetch(deckId) {
|
|
27
|
+
if (typeof deckId !== "string" || deckId.length === 0) {
|
|
28
|
+
throw new CastleError({
|
|
29
|
+
code: "INVALID_ARGUMENT",
|
|
30
|
+
message: "Portal.prefetch requires a deckId.",
|
|
31
|
+
operation: "Portal.prefetch",
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
return hostRequest("portal.prefetch", { targetDeckId: deckId });
|
|
35
|
+
}
|