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,296 @@
|
|
|
1
|
+
import { isEdit } from "./context";
|
|
2
|
+
import { CastleError } from "./errors";
|
|
3
|
+
import { hostRequest } from "./transport";
|
|
4
|
+
const LEADERBOARD_FLUSH_INTERVAL_MS = 5000;
|
|
5
|
+
// Host stamps deckId, so one deck session = one set of leaderboards; key by
|
|
6
|
+
// variable+scope only and keep the best high/low score per key until flush.
|
|
7
|
+
const leaderboardWrites = new Map();
|
|
8
|
+
let leaderboardFlushTimer = null;
|
|
9
|
+
let leaderboardFlushPromise = null;
|
|
10
|
+
let leaderboardUnloadHooked = false;
|
|
11
|
+
export const Leaderboard = {
|
|
12
|
+
write(variable, score, options = {}) {
|
|
13
|
+
writeLeaderboard(variable, score, options);
|
|
14
|
+
},
|
|
15
|
+
fetch(variable, type, options = {}) {
|
|
16
|
+
return fetchLeaderboardData(variable, type, options);
|
|
17
|
+
},
|
|
18
|
+
show(variable, type, options = {}) {
|
|
19
|
+
return showLeaderboard(variable, type, options);
|
|
20
|
+
},
|
|
21
|
+
};
|
|
22
|
+
function writeLeaderboard(variable, score, options) {
|
|
23
|
+
if (isEdit())
|
|
24
|
+
return;
|
|
25
|
+
try {
|
|
26
|
+
bufferLeaderboardWrite(variable, score, options);
|
|
27
|
+
}
|
|
28
|
+
catch (error) {
|
|
29
|
+
reportLeaderboardError(error);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
async function fetchLeaderboardData(variable, type, options) {
|
|
33
|
+
assertLeaderboardVariable(variable, "Leaderboard.fetch");
|
|
34
|
+
assertLeaderboardType(type, "Leaderboard.fetch");
|
|
35
|
+
const scope = leaderboardScope(options);
|
|
36
|
+
const record = pendingLeaderboardWrite(variable, scope);
|
|
37
|
+
const score = bufferedScore(record, type);
|
|
38
|
+
const { leaderboard, currentUserId } = await hostRequest("leaderboard.fetch", {
|
|
39
|
+
variable,
|
|
40
|
+
type,
|
|
41
|
+
scope,
|
|
42
|
+
...(score === null ? {} : { score }),
|
|
43
|
+
});
|
|
44
|
+
if (record && score !== null) {
|
|
45
|
+
clearLeaderboardDirtyAfterFetch(record, type, score);
|
|
46
|
+
}
|
|
47
|
+
return normalizeLeaderboard(leaderboard, currentUserId);
|
|
48
|
+
}
|
|
49
|
+
// Opening the host's leaderboard UI takes the same write-through shortcut a
|
|
50
|
+
// fetch does: the buffered score rides along so the player's own just-played
|
|
51
|
+
// score is on the board immediately, matching what opening the classic
|
|
52
|
+
// leaderboard does. The panel and its data stay host-side — a deck that calls
|
|
53
|
+
// this receives no leaderboard rows, only whether it was presented.
|
|
54
|
+
async function showLeaderboard(variable, type, options) {
|
|
55
|
+
assertLeaderboardVariable(variable, "Leaderboard.show");
|
|
56
|
+
assertLeaderboardType(type, "Leaderboard.show");
|
|
57
|
+
const scope = leaderboardScope(options);
|
|
58
|
+
const record = pendingLeaderboardWrite(variable, scope);
|
|
59
|
+
const score = bufferedScore(record, type);
|
|
60
|
+
const label = typeof options.label === "string" ? options.label : null;
|
|
61
|
+
let result;
|
|
62
|
+
try {
|
|
63
|
+
result = await hostRequest("leaderboard.show", {
|
|
64
|
+
variable,
|
|
65
|
+
type,
|
|
66
|
+
scope,
|
|
67
|
+
...(label === null ? {} : { label }),
|
|
68
|
+
...(score === null ? {} : { score }),
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
catch (error) {
|
|
72
|
+
// An app build older than this command rejects it outright; that is
|
|
73
|
+
// capability divergence, not a failure the deck should have to handle.
|
|
74
|
+
if (error instanceof CastleError && error.code === "UNKNOWN_COMMAND") {
|
|
75
|
+
return { status: "unavailable" };
|
|
76
|
+
}
|
|
77
|
+
throw error;
|
|
78
|
+
}
|
|
79
|
+
// Only a host that actually presented the board wrote the score, so only
|
|
80
|
+
// then is the buffered write settled; otherwise the periodic flush delivers
|
|
81
|
+
// it and a duplicate write (double streak credit) is avoided either way.
|
|
82
|
+
if (result.status === "shown" && record && score !== null) {
|
|
83
|
+
clearLeaderboardDirtyAfterFetch(record, type, score);
|
|
84
|
+
}
|
|
85
|
+
return result;
|
|
86
|
+
}
|
|
87
|
+
function pendingLeaderboardWrite(variable, scope) {
|
|
88
|
+
return leaderboardWrites.get(leaderboardWriteKey(variable, scope));
|
|
89
|
+
}
|
|
90
|
+
// The value to send along with a read: if the deck has written a score for this
|
|
91
|
+
// variable+scope this session, the host writes-and-reads via leaderboardV2 so
|
|
92
|
+
// the player's own score shows up immediately. Mirrors getLeaderboard in
|
|
93
|
+
// core/src/leaderboards.cpp — presence of a buffered score, not dirtiness,
|
|
94
|
+
// gates the write-through. No buffered score → a plain read of the settled board.
|
|
95
|
+
function bufferedScore(record, type) {
|
|
96
|
+
if (!record)
|
|
97
|
+
return null;
|
|
98
|
+
return type === "high" ? record.highScore : record.lowScore;
|
|
99
|
+
}
|
|
100
|
+
// After a write-through fetch, clear the dirty flag for the side we just sent
|
|
101
|
+
// (so the periodic flush won't re-send it via saveVariableToLeaderboard). If
|
|
102
|
+
// the other side's buffered value matches what we sent (the common single-score
|
|
103
|
+
// case where high == low), clear it too. The equality guards skip clearing if a
|
|
104
|
+
// concurrent write bumped the buffered score while the fetch was in flight —
|
|
105
|
+
// that newer score still needs flushing. Mirrors leaderboards.cpp.
|
|
106
|
+
function clearLeaderboardDirtyAfterFetch(record, type, sentScore) {
|
|
107
|
+
if (type === "high") {
|
|
108
|
+
if (record.highScore === sentScore) {
|
|
109
|
+
record.isHighDirty = false;
|
|
110
|
+
if (record.lowScore === sentScore)
|
|
111
|
+
record.isLowDirty = false;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
else {
|
|
115
|
+
if (record.lowScore === sentScore) {
|
|
116
|
+
record.isLowDirty = false;
|
|
117
|
+
if (record.highScore === sentScore)
|
|
118
|
+
record.isHighDirty = false;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
function bufferLeaderboardWrite(variable, score, options) {
|
|
123
|
+
assertLeaderboardVariable(variable, "Leaderboard.write");
|
|
124
|
+
assertLeaderboardScore(score, "Leaderboard.write");
|
|
125
|
+
const scope = leaderboardScope(options);
|
|
126
|
+
const key = leaderboardWriteKey(variable, scope);
|
|
127
|
+
const record = leaderboardWrites.get(key);
|
|
128
|
+
if (record) {
|
|
129
|
+
updatePendingLeaderboardWrite(record, score);
|
|
130
|
+
}
|
|
131
|
+
else {
|
|
132
|
+
leaderboardWrites.set(key, newPendingLeaderboardWrite(variable, scope, score));
|
|
133
|
+
}
|
|
134
|
+
ensureLeaderboardUnloadFlush();
|
|
135
|
+
scheduleLeaderboardFlush();
|
|
136
|
+
}
|
|
137
|
+
function newPendingLeaderboardWrite(variable, scope, score) {
|
|
138
|
+
return {
|
|
139
|
+
variable,
|
|
140
|
+
scope,
|
|
141
|
+
highScore: score,
|
|
142
|
+
lowScore: score,
|
|
143
|
+
isHighDirty: true,
|
|
144
|
+
isLowDirty: true,
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
function updatePendingLeaderboardWrite(record, score) {
|
|
148
|
+
if (score > record.highScore) {
|
|
149
|
+
record.highScore = score;
|
|
150
|
+
record.isHighDirty = true;
|
|
151
|
+
}
|
|
152
|
+
if (score < record.lowScore) {
|
|
153
|
+
record.lowScore = score;
|
|
154
|
+
record.isLowDirty = true;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
function scheduleLeaderboardFlush() {
|
|
158
|
+
if (leaderboardFlushTimer)
|
|
159
|
+
return;
|
|
160
|
+
leaderboardFlushTimer = setTimeout(() => {
|
|
161
|
+
leaderboardFlushTimer = null;
|
|
162
|
+
void flushLeaderboardWrites().catch(reportLeaderboardError);
|
|
163
|
+
}, LEADERBOARD_FLUSH_INTERVAL_MS);
|
|
164
|
+
}
|
|
165
|
+
function ensureLeaderboardUnloadFlush() {
|
|
166
|
+
if (leaderboardUnloadHooked || typeof window === "undefined")
|
|
167
|
+
return;
|
|
168
|
+
leaderboardUnloadHooked = true;
|
|
169
|
+
window.addEventListener("pagehide", () => {
|
|
170
|
+
void flushLeaderboardWrites().catch(reportLeaderboardError);
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
async function flushLeaderboardWrites() {
|
|
174
|
+
if (leaderboardFlushPromise)
|
|
175
|
+
return leaderboardFlushPromise;
|
|
176
|
+
leaderboardFlushPromise = flushLeaderboardWritesOnce().finally(() => {
|
|
177
|
+
leaderboardFlushPromise = null;
|
|
178
|
+
if (hasDirtyLeaderboardWrites())
|
|
179
|
+
scheduleLeaderboardFlush();
|
|
180
|
+
});
|
|
181
|
+
return leaderboardFlushPromise;
|
|
182
|
+
}
|
|
183
|
+
async function flushLeaderboardWritesOnce() {
|
|
184
|
+
for (const job of leaderboardWriteJobs()) {
|
|
185
|
+
await saveLeaderboardScore(job.record, job.score);
|
|
186
|
+
markLeaderboardWriteClean(job);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
function leaderboardWriteJobs() {
|
|
190
|
+
const jobs = [];
|
|
191
|
+
for (const record of leaderboardWrites.values()) {
|
|
192
|
+
if (record.isHighDirty &&
|
|
193
|
+
record.isLowDirty &&
|
|
194
|
+
record.highScore === record.lowScore) {
|
|
195
|
+
jobs.push({ record, type: "both", score: record.highScore });
|
|
196
|
+
}
|
|
197
|
+
else {
|
|
198
|
+
if (record.isHighDirty)
|
|
199
|
+
jobs.push({ record, type: "high", score: record.highScore });
|
|
200
|
+
if (record.isLowDirty)
|
|
201
|
+
jobs.push({ record, type: "low", score: record.lowScore });
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
return jobs;
|
|
205
|
+
}
|
|
206
|
+
function markLeaderboardWriteClean(job) {
|
|
207
|
+
const { record, score, type } = job;
|
|
208
|
+
if ((type === "high" || type === "both") && record.highScore === score) {
|
|
209
|
+
record.isHighDirty = false;
|
|
210
|
+
}
|
|
211
|
+
if ((type === "low" || type === "both") && record.lowScore === score) {
|
|
212
|
+
record.isLowDirty = false;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
function hasDirtyLeaderboardWrites() {
|
|
216
|
+
for (const record of leaderboardWrites.values()) {
|
|
217
|
+
if (record.isHighDirty || record.isLowDirty)
|
|
218
|
+
return true;
|
|
219
|
+
}
|
|
220
|
+
return false;
|
|
221
|
+
}
|
|
222
|
+
async function saveLeaderboardScore(record, score) {
|
|
223
|
+
await hostRequest("leaderboard.save", {
|
|
224
|
+
variable: record.variable,
|
|
225
|
+
score,
|
|
226
|
+
scope: record.scope,
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
function normalizeLeaderboard(leaderboard, userId) {
|
|
230
|
+
const list = (leaderboard.list ?? []).map(normalizeLeaderboardEntry);
|
|
231
|
+
const playerRank = playerRankFromList(list, userId);
|
|
232
|
+
const playerValue = scoreNumber(leaderboard.yourScore?.score);
|
|
233
|
+
return {
|
|
234
|
+
list,
|
|
235
|
+
...(playerRank === undefined ? {} : { playerRank }),
|
|
236
|
+
...(playerValue === undefined ? {} : { playerValue }),
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
function normalizeLeaderboardEntry(entry) {
|
|
240
|
+
const userId = entry.user?.userId ?? undefined;
|
|
241
|
+
return {
|
|
242
|
+
place: scoreNumber(entry.place) ?? 0,
|
|
243
|
+
value: scoreNumber(entry.score) ?? 0,
|
|
244
|
+
username: entry.user?.username ?? "",
|
|
245
|
+
...(userId ? { userId } : {}),
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
function playerRankFromList(list, userId) {
|
|
249
|
+
return userId
|
|
250
|
+
? list.find((entry) => entry.userId === userId)?.place
|
|
251
|
+
: undefined;
|
|
252
|
+
}
|
|
253
|
+
function scoreNumber(value) {
|
|
254
|
+
if (typeof value === "number")
|
|
255
|
+
return Number.isFinite(value) ? value : undefined;
|
|
256
|
+
if (typeof value !== "string")
|
|
257
|
+
return undefined;
|
|
258
|
+
const parsed = Number.parseFloat(value);
|
|
259
|
+
return Number.isFinite(parsed) ? parsed : undefined;
|
|
260
|
+
}
|
|
261
|
+
function leaderboardWriteKey(variable, scope) {
|
|
262
|
+
return `${variable}${scope ? `::${scope}` : ""}`;
|
|
263
|
+
}
|
|
264
|
+
function leaderboardScope(options) {
|
|
265
|
+
return options.scope ?? null;
|
|
266
|
+
}
|
|
267
|
+
function assertLeaderboardVariable(variable, operation) {
|
|
268
|
+
if (variable.trim().length > 0)
|
|
269
|
+
return;
|
|
270
|
+
throw new CastleError({
|
|
271
|
+
code: "INVALID_LEADERBOARD_VARIABLE",
|
|
272
|
+
message: "Leaderboard variable must be a non-empty string.",
|
|
273
|
+
operation,
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
function assertLeaderboardScore(score, operation) {
|
|
277
|
+
if (Number.isFinite(score))
|
|
278
|
+
return;
|
|
279
|
+
throw new CastleError({
|
|
280
|
+
code: "INVALID_LEADERBOARD_SCORE",
|
|
281
|
+
message: "Leaderboard score must be a finite number.",
|
|
282
|
+
operation,
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
function assertLeaderboardType(type, operation) {
|
|
286
|
+
if (type === "high" || type === "low")
|
|
287
|
+
return;
|
|
288
|
+
throw new CastleError({
|
|
289
|
+
code: "INVALID_LEADERBOARD_TYPE",
|
|
290
|
+
message: "Leaderboard type must be high or low.",
|
|
291
|
+
operation,
|
|
292
|
+
});
|
|
293
|
+
}
|
|
294
|
+
function reportLeaderboardError(error) {
|
|
295
|
+
console.warn("Castle leaderboard request failed", error);
|
|
296
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// Deck-side lifecycle signals. The feed reveals the deck when it reports `ready`
|
|
2
|
+
// (its first presentable frame) instead of waiting out a fixed timer.
|
|
3
|
+
import { hostNotify } from "./transport";
|
|
4
|
+
let readySent = false;
|
|
5
|
+
function ready() {
|
|
6
|
+
if (readySent)
|
|
7
|
+
return;
|
|
8
|
+
readySent = true;
|
|
9
|
+
hostNotify("ready");
|
|
10
|
+
}
|
|
11
|
+
export const Lifecycle = {
|
|
12
|
+
ready,
|
|
13
|
+
};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { CastleError } from "./errors";
|
|
2
|
+
import { type MultiplayerPlayer } from "./multiplayerProtocol";
|
|
3
|
+
export type MultiplayerJoinOptions = {
|
|
4
|
+
mode: "named";
|
|
5
|
+
key: string;
|
|
6
|
+
} | {
|
|
7
|
+
mode: "party";
|
|
8
|
+
} | {
|
|
9
|
+
mode: "public";
|
|
10
|
+
};
|
|
11
|
+
export type MultiplayerConnectionState = "acquiring" | "connecting" | "connected" | "reconnecting" | "closed";
|
|
12
|
+
export type MultiplayerStateMetadata = {
|
|
13
|
+
type: "connectionLost";
|
|
14
|
+
} | {
|
|
15
|
+
type: "replaced";
|
|
16
|
+
} | {
|
|
17
|
+
type: "bye";
|
|
18
|
+
reason?: string;
|
|
19
|
+
} | {
|
|
20
|
+
type: "left";
|
|
21
|
+
} | {
|
|
22
|
+
type: "error";
|
|
23
|
+
error: CastleError;
|
|
24
|
+
};
|
|
25
|
+
export interface MultiplayerRoster {
|
|
26
|
+
players: MultiplayerPlayer[];
|
|
27
|
+
you: MultiplayerPlayer | null;
|
|
28
|
+
}
|
|
29
|
+
export interface MultiplayerConnection {
|
|
30
|
+
readonly state: MultiplayerConnectionState;
|
|
31
|
+
readonly playerId: string;
|
|
32
|
+
readonly sessionId: string;
|
|
33
|
+
readonly isSolo: boolean;
|
|
34
|
+
send(data: unknown): void;
|
|
35
|
+
onMessage(callback: (data: unknown) => void): () => void;
|
|
36
|
+
onRoster(callback: (roster: MultiplayerRoster) => void): () => void;
|
|
37
|
+
onStateChange(callback: (state: MultiplayerConnectionState, metadata?: MultiplayerStateMetadata) => void): () => void;
|
|
38
|
+
leave(): void;
|
|
39
|
+
}
|
|
40
|
+
export interface CastleMultiplayerApi {
|
|
41
|
+
join(options: MultiplayerJoinOptions): Promise<MultiplayerConnection>;
|
|
42
|
+
}
|
|
43
|
+
export declare const Multiplayer: CastleMultiplayerApi;
|