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
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
// Blocks (cauldron-game-server HUB.md "Trust and safety"): two players a block
|
|
2
|
+
// hides from each other, in either direction, never see each other in a hub
|
|
3
|
+
// place -- no pose rows, no avatar, no name. The host knows the pairs and the
|
|
4
|
+
// SDK answers them (`session.hub.hiddenPair`); the block lists never reach the
|
|
5
|
+
// kit. A session without it (the dev serve, a deck with no hub, an older SDK)
|
|
6
|
+
// hides nobody, and every function here is then the identity.
|
|
7
|
+
|
|
8
|
+
// playerId -> Set of the playerIds hidden from it, or null when nobody is.
|
|
9
|
+
export function hiddenPairs(session, playerIds) {
|
|
10
|
+
const hub = session?.hub;
|
|
11
|
+
if (typeof hub?.hiddenPair !== 'function') {
|
|
12
|
+
return null;
|
|
13
|
+
}
|
|
14
|
+
const ids = [...new Set(playerIds)];
|
|
15
|
+
let out = null;
|
|
16
|
+
for (let i = 0; i < ids.length; i += 1) {
|
|
17
|
+
for (let j = i + 1; j < ids.length; j += 1) {
|
|
18
|
+
if (hub.hiddenPair(ids[i], ids[j])) {
|
|
19
|
+
out ??= new Map();
|
|
20
|
+
add(out, ids[i], ids[j]);
|
|
21
|
+
add(out, ids[j], ids[i]);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return out;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function add(map, a, b) {
|
|
29
|
+
let set = map.get(a);
|
|
30
|
+
if (!set) {
|
|
31
|
+
set = new Set();
|
|
32
|
+
map.set(a, set);
|
|
33
|
+
}
|
|
34
|
+
set.add(b);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// An interest map (playerId -> Set of players it is sent) with every hidden
|
|
38
|
+
// pair taken out of it, in place.
|
|
39
|
+
export function withoutHidden(sees, hidden) {
|
|
40
|
+
if (!hidden || !sees) {
|
|
41
|
+
return sees;
|
|
42
|
+
}
|
|
43
|
+
for (const [playerId, set] of sees) {
|
|
44
|
+
for (const other of hidden.get(playerId) ?? []) {
|
|
45
|
+
set.delete(other);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return sees;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// What a game or NPC writes is free text, and may name a player: `names` is
|
|
52
|
+
// the usernames that must not reach this recipient. Each one, as a whole word,
|
|
53
|
+
// becomes "someone". No names, the same text.
|
|
54
|
+
const WORD = /[\p{L}\p{N}_]/u;
|
|
55
|
+
export function redactText(text, names) {
|
|
56
|
+
if (typeof text !== 'string' || !names || names.length === 0) {
|
|
57
|
+
return text;
|
|
58
|
+
}
|
|
59
|
+
let out = text;
|
|
60
|
+
for (const name of names) {
|
|
61
|
+
if (!name) {
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
let from = 0;
|
|
65
|
+
for (;;) {
|
|
66
|
+
const at = out.indexOf(name, from);
|
|
67
|
+
if (at < 0) {
|
|
68
|
+
break;
|
|
69
|
+
}
|
|
70
|
+
const end = at + name.length;
|
|
71
|
+
if ((at > 0 && WORD.test(out[at - 1])) || (end < out.length && WORD.test(out[end]))) {
|
|
72
|
+
from = at + 1;
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
out = `${out.slice(0, at)}someone${out.slice(end)}`;
|
|
76
|
+
from = at + 'someone'.length;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return out;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// A HUD spec with `names` taken out of its words.
|
|
83
|
+
export function redactHud(spec, names) {
|
|
84
|
+
if (!spec || !names || names.length === 0) {
|
|
85
|
+
return spec;
|
|
86
|
+
}
|
|
87
|
+
const out = { ...spec };
|
|
88
|
+
for (const key of ['title', 'big', 'text']) {
|
|
89
|
+
if (typeof out[key] === 'string') {
|
|
90
|
+
out[key] = redactText(out[key], names);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return out;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// A wire label as one recipient may see it: null when it is about a player
|
|
97
|
+
// hidden from them (`label.about`), else its words without `names`.
|
|
98
|
+
export function labelFor(label, wire, hiddenIds, names) {
|
|
99
|
+
if (label.about && hiddenIds?.has(label.about)) {
|
|
100
|
+
return null;
|
|
101
|
+
}
|
|
102
|
+
return names && names.length ? { ...wire, text: redactText(wire.text, names) } : wire;
|
|
103
|
+
}
|
|
@@ -0,0 +1,309 @@
|
|
|
1
|
+
// Arcade cabinets (contract v1.7, §4 and §12): a door whose board from ghost
|
|
2
|
+
// carries `classic` leads to a classic Castle deck the host plays itself. The
|
|
3
|
+
// place ships no client code, so everything a player sees of a cabinet is
|
|
4
|
+
// sent from here: its card -- title, label, cover, the top scores with each
|
|
5
|
+
// player's photo and frame -- which the hub client draws.
|
|
6
|
+
//
|
|
7
|
+
// A card's pictures are data URIs (a published deck's page may load an image
|
|
8
|
+
// from nowhere else), so a card is tens of kilobytes. The host refuses a frame
|
|
9
|
+
// to a client over 64 KB and drops frames for the whole session past its send
|
|
10
|
+
// budget (1 MB/s), so a card never rides the WORLD snapshot, a delta or a
|
|
11
|
+
// broadcast: it goes to one player, one card a frame, when that player comes
|
|
12
|
+
// within RANGE_M of the cabinet, once per version of the card; a changed card
|
|
13
|
+
// goes again only to players in range of it then. Everything sent here
|
|
14
|
+
// together stays inside BYTES_PER_SECOND, so a crowd arriving at once is
|
|
15
|
+
// served over a few seconds rather than in a burst.
|
|
16
|
+
|
|
17
|
+
// A data URI of an image, printable ASCII only.
|
|
18
|
+
const IMAGE = /^data:image\/[a-z0-9.+-]{1,20}[;,][\x20-\x7e]*$/i;
|
|
19
|
+
// The most one picture may be. Ghost keeps a whole card near 44 KB.
|
|
20
|
+
const IMAGE_MAX = 48000;
|
|
21
|
+
// A card's frame on the wire is at most this, envelope included: over it,
|
|
22
|
+
// pictures are dropped -- frames, then photos, from the bottom row up, then
|
|
23
|
+
// the cover -- as ghost does.
|
|
24
|
+
export const FRAME_MAX_BYTES = 60000;
|
|
25
|
+
const TOP_MAX = 5;
|
|
26
|
+
// A player this near a cabinet is sent its card. The client draws it nearer.
|
|
27
|
+
export const RANGE_M = 14;
|
|
28
|
+
// What cards may cost the session's send budget, and the most held in hand.
|
|
29
|
+
const BYTES_PER_SECOND = 300000;
|
|
30
|
+
const BURST_BYTES = 180000;
|
|
31
|
+
// A door the town marks as a cabinet (`HubDoor.kind`): it does not swing, is
|
|
32
|
+
// not in the Doors list, and is never a pad ghost holds.
|
|
33
|
+
export const CABINET = 'cabinet';
|
|
34
|
+
|
|
35
|
+
export function newCabinetState() {
|
|
36
|
+
return {
|
|
37
|
+
cab: {
|
|
38
|
+
// door actor id -> { door, at, card, json }
|
|
39
|
+
cards: new Map(),
|
|
40
|
+
// bumped whenever a card changes
|
|
41
|
+
rev: 0,
|
|
42
|
+
// playerId -> Map(door actor id -> the card's JSON as sent to them)
|
|
43
|
+
sentTo: new Map(),
|
|
44
|
+
// the send budget: bytes in hand, and when it was last topped up
|
|
45
|
+
bytes: BURST_BYTES,
|
|
46
|
+
at: null,
|
|
47
|
+
// the dev serve's stand-in boards are on (`fixtureOn`)
|
|
48
|
+
fixture: false,
|
|
49
|
+
},
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function isCabinetDoor(props) {
|
|
54
|
+
return props?.kind === CABINET;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// The ids of the place's cabinet doors, read once from its scene.
|
|
58
|
+
export function cabinetDoorIds(h) {
|
|
59
|
+
if (h.cab.doorIds?.actors !== h.actors) {
|
|
60
|
+
const ids = new Set();
|
|
61
|
+
for (const actor of h.actors) {
|
|
62
|
+
const props = actor.components.HubDoor;
|
|
63
|
+
if (isCabinetDoor(props) && typeof props.id === 'string') {
|
|
64
|
+
ids.add(props.id);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
h.cab.doorIds = Object.assign(ids, { actors: h.actors });
|
|
68
|
+
}
|
|
69
|
+
return h.cab.doorIds;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// Whether a door leads to a classic deck: ghost's board says so.
|
|
73
|
+
export function isClassic(h, doorId) {
|
|
74
|
+
const classic = h.boards.get(doorId)?.classic;
|
|
75
|
+
return Boolean(classic && typeof classic === 'object');
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function text(value, max) {
|
|
79
|
+
return typeof value === 'string' ? value.slice(0, max) : '';
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// A classic deck its creator never titled carries Castle's placeholder
|
|
83
|
+
// (`deck-` and the first four characters of its id), which is not a name: the
|
|
84
|
+
// place's own title for the cabinet stands instead.
|
|
85
|
+
const UNTITLED = /^deck-[A-Za-z0-9_-]{4}$/;
|
|
86
|
+
export function deckTitle(value) {
|
|
87
|
+
return typeof value === 'string' && !UNTITLED.test(value) ? value : '';
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function image(value) {
|
|
91
|
+
return typeof value === 'string' && value.length <= IMAGE_MAX && IMAGE.test(value) ? value : null;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const encoder = new TextEncoder();
|
|
95
|
+
|
|
96
|
+
// The bytes of a card's frame as the host measures it.
|
|
97
|
+
function frameBytes(actorId, door, card) {
|
|
98
|
+
return encoder.encode(JSON.stringify({ t: 'm', d: wire(actorId, door, card) })).length;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function wire(actorId, door, card) {
|
|
102
|
+
return { k: 'hub-cabinet', actor: actorId, door, card };
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// A board's `classic` as the card a client is sent: bounded words, and
|
|
106
|
+
// pictures that are images. Anything malformed is left out, and pictures are
|
|
107
|
+
// dropped until the card's frame fits.
|
|
108
|
+
function cardOf(actorId, door, board, doorTitle) {
|
|
109
|
+
const classic = board.classic;
|
|
110
|
+
const top = [];
|
|
111
|
+
for (const row of Array.isArray(classic.top) ? classic.top.slice(0, TOP_MAX) : []) {
|
|
112
|
+
if (!row || typeof row !== 'object') {
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
top.push({
|
|
116
|
+
username: text(row.username, 32) || '?',
|
|
117
|
+
score: text(typeof row.score === 'number' ? String(row.score) : row.score, 16),
|
|
118
|
+
photo: image(row.photo),
|
|
119
|
+
frame: image(row.frame),
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
const card = {
|
|
123
|
+
title: text(deckTitle(classic.title), 60) || text(deckTitle(board.title), 60) || text(doorTitle, 60),
|
|
124
|
+
label: text(classic.label, 24),
|
|
125
|
+
cover: image(classic.cover),
|
|
126
|
+
top,
|
|
127
|
+
};
|
|
128
|
+
const drops = [
|
|
129
|
+
...top.map((row) => () => (row.frame = null)).reverse(),
|
|
130
|
+
...top.map((row) => () => (row.photo = null)).reverse(),
|
|
131
|
+
() => (card.cover = null),
|
|
132
|
+
];
|
|
133
|
+
while (drops.length && frameBytes(actorId, door, card) > FRAME_MAX_BYTES) {
|
|
134
|
+
drops.shift()();
|
|
135
|
+
}
|
|
136
|
+
return card;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// The cards as the boards now have them: called when the boards were read
|
|
140
|
+
// (and when the dev fixture is turned on). `doors` is `doors(h)`.
|
|
141
|
+
export function refresh(h, doors) {
|
|
142
|
+
const cab = h.cab;
|
|
143
|
+
const seen = new Set();
|
|
144
|
+
for (const door of doors) {
|
|
145
|
+
if (!isClassic(h, door.id)) {
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
seen.add(door.actor);
|
|
149
|
+
const card = cardOf(door.actor, door.id, h.boards.get(door.id), door.title);
|
|
150
|
+
const json = JSON.stringify(card);
|
|
151
|
+
if (cab.cards.get(door.actor)?.json !== json) {
|
|
152
|
+
cab.cards.set(door.actor, { door: door.id, at: door.t, card, json });
|
|
153
|
+
cab.rev += 1;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
for (const actorId of [...cab.cards.keys()]) {
|
|
157
|
+
if (!seen.has(actorId)) {
|
|
158
|
+
cab.cards.delete(actorId);
|
|
159
|
+
cab.rev += 1;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// A card as one player may read it: a row naming a player a block hides from
|
|
165
|
+
// them reads "someone", with no pictures (contract §12, Blocks).
|
|
166
|
+
function cardFor(entry, names) {
|
|
167
|
+
if (!names.length || !entry.card.top.some((row) => names.includes(row.username))) {
|
|
168
|
+
return entry;
|
|
169
|
+
}
|
|
170
|
+
const card = {
|
|
171
|
+
...entry.card,
|
|
172
|
+
top: entry.card.top.map((row) => (names.includes(row.username) ? { username: 'someone', score: row.score, photo: null, frame: null } : row)),
|
|
173
|
+
};
|
|
174
|
+
return { card, json: JSON.stringify(card) };
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// Each arrived player within RANGE_M of a cabinet is sent its card if they do
|
|
178
|
+
// not have this version of it, one card a frame, while the budget lasts; the
|
|
179
|
+
// rest wait for a later tick. A card that is gone is taken back from whoever
|
|
180
|
+
// has it (a few bytes). `hiddenNames(playerId)` is the usernames a block
|
|
181
|
+
// hides from that player.
|
|
182
|
+
export function send(h, hiddenNames = () => []) {
|
|
183
|
+
const cab = h.cab;
|
|
184
|
+
if (!cab.cards.size && !cab.sentTo.size) {
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
const now = Date.now();
|
|
188
|
+
cab.bytes = Math.min(BURST_BYTES, cab.bytes + (cab.at === null ? 0 : ((now - cab.at) / 1000) * BYTES_PER_SECOND));
|
|
189
|
+
cab.at = now;
|
|
190
|
+
for (const [playerId, body] of h.sim.players) {
|
|
191
|
+
if (!h.players.get(playerId)?.arrived) {
|
|
192
|
+
continue;
|
|
193
|
+
}
|
|
194
|
+
let sent = cab.sentTo.get(playerId);
|
|
195
|
+
for (const actorId of [...(sent?.keys() ?? [])]) {
|
|
196
|
+
if (!cab.cards.has(actorId)) {
|
|
197
|
+
sent.delete(actorId);
|
|
198
|
+
h.session?.send(playerId, wire(actorId, null, null));
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
let names = null;
|
|
202
|
+
for (const [actorId, entry] of cab.cards) {
|
|
203
|
+
const t = entry.at;
|
|
204
|
+
if (!t || Math.hypot(body.at.x - t.x, body.at.z - t.z) > RANGE_M) {
|
|
205
|
+
continue;
|
|
206
|
+
}
|
|
207
|
+
names ??= hiddenNames(playerId);
|
|
208
|
+
const mine = cardFor(entry, names);
|
|
209
|
+
if (sent?.get(actorId) === mine.json) {
|
|
210
|
+
continue;
|
|
211
|
+
}
|
|
212
|
+
const bytes = mine.json.length + 96;
|
|
213
|
+
if (bytes > cab.bytes) {
|
|
214
|
+
// Out of budget this tick: everyone still waiting is served later.
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
cab.bytes -= bytes;
|
|
218
|
+
if (!sent) {
|
|
219
|
+
sent = new Map();
|
|
220
|
+
cab.sentTo.set(playerId, sent);
|
|
221
|
+
}
|
|
222
|
+
sent.set(actorId, mine.json);
|
|
223
|
+
h.session?.send(playerId, wire(actorId, entry.door, mine.card));
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
// A player who left, or who joined again (a hello that is not an avatar
|
|
229
|
+
// change): their client holds nothing.
|
|
230
|
+
export function forget(h, playerId) {
|
|
231
|
+
h.cab.sentTo.delete(playerId);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
// The dev serve has no ghost, so no boards: a cabinet there is inert. A test
|
|
235
|
+
// turns this on (`hub-dev-classic`, honoured only for a `dev` session id) to
|
|
236
|
+
// give every cabinet door of the place a stand-in board, so the card, the
|
|
237
|
+
// away trip and Back can be exercised without ghost. Never in production.
|
|
238
|
+
export function fixtureOn(h, doors) {
|
|
239
|
+
h.cab.fixture = true;
|
|
240
|
+
doors.forEach((door, n) => {
|
|
241
|
+
if (isCabinetDoor(door.props)) {
|
|
242
|
+
h.boards.set(door.id, fixtureBoard(door, n));
|
|
243
|
+
}
|
|
244
|
+
});
|
|
245
|
+
refresh(h, doors);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
// `{ k: 'hub-dev-classic', on: false }`: the cabinets are inert again.
|
|
249
|
+
export function fixtureOff(h, doors) {
|
|
250
|
+
if (!h.cab.fixture) {
|
|
251
|
+
return;
|
|
252
|
+
}
|
|
253
|
+
h.cab.fixture = false;
|
|
254
|
+
for (const door of doors) {
|
|
255
|
+
if (isCabinetDoor(door.props)) {
|
|
256
|
+
h.boards.delete(door.id);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
refresh(h, doors);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
const svg = (body, w, hgt) =>
|
|
263
|
+
`data:image/svg+xml,${encodeURIComponent(`<svg xmlns="http://www.w3.org/2000/svg" width="${w}" height="${hgt}" viewBox="0 0 ${w} ${hgt}">${body}</svg>`)}`;
|
|
264
|
+
|
|
265
|
+
const FIXTURE_NAMES = ['rowan', 'mabel', 'pip', 'tamsin', 'quill'];
|
|
266
|
+
const FIXTURE_COLORS = ['#e86a9a', '#3fb8c9', '#e8b04a', '#8fd16a', '#b07cf0'];
|
|
267
|
+
const FIXTURE_FRAME = svg(
|
|
268
|
+
'<circle cx="40" cy="40" r="34" fill="none" stroke="#f2c94c" stroke-width="7"/><circle cx="40" cy="6" r="6" fill="#ff6b9a"/><circle cx="12" cy="60" r="5" fill="#6bd3ff"/><circle cx="68" cy="60" r="5" fill="#6bd3ff"/>',
|
|
269
|
+
80,
|
|
270
|
+
80,
|
|
271
|
+
);
|
|
272
|
+
|
|
273
|
+
function fixtureBoard(door, n) {
|
|
274
|
+
const title = typeof door.props.title === 'string' && door.props.title ? door.props.title : door.id;
|
|
275
|
+
const color = FIXTURE_COLORS[n % FIXTURE_COLORS.length];
|
|
276
|
+
const daily = n % 2 === 0;
|
|
277
|
+
const lower = n % 3 === 1;
|
|
278
|
+
const cover = svg(
|
|
279
|
+
`<rect width="256" height="358" fill="#1b1430"/><rect x="18" y="18" width="220" height="322" rx="14" fill="${color}"/><circle cx="128" cy="150" r="62" fill="#1b1430"/><rect x="48" y="252" width="160" height="22" rx="11" fill="#1b1430"/><rect x="78" y="288" width="100" height="16" rx="8" fill="#1b1430"/>`,
|
|
280
|
+
256,
|
|
281
|
+
358,
|
|
282
|
+
);
|
|
283
|
+
const top = FIXTURE_NAMES.map((username, i) => ({
|
|
284
|
+
username,
|
|
285
|
+
score: lower ? String(3 + i) : `${(48.6 - i * 7.3).toFixed(1)}K`,
|
|
286
|
+
photo: svg(
|
|
287
|
+
`<rect width="64" height="64" fill="${FIXTURE_COLORS[(n + i + 1) % FIXTURE_COLORS.length]}"/><circle cx="32" cy="26" r="12" fill="#fff"/><rect x="12" y="42" width="40" height="30" rx="15" fill="#fff"/>`,
|
|
288
|
+
64,
|
|
289
|
+
64,
|
|
290
|
+
),
|
|
291
|
+
// Every other player wears a frame, as in a real board.
|
|
292
|
+
frame: i % 2 === 0 ? FIXTURE_FRAME : null,
|
|
293
|
+
}));
|
|
294
|
+
return {
|
|
295
|
+
toDeckId: 'dev-classic',
|
|
296
|
+
title,
|
|
297
|
+
unit: null,
|
|
298
|
+
better: lower ? 'lower' : 'higher',
|
|
299
|
+
top: [],
|
|
300
|
+
featured: false,
|
|
301
|
+
minPlayers: 1,
|
|
302
|
+
maxPlayers: 1,
|
|
303
|
+
lateJoin: false,
|
|
304
|
+
playersInside: 0,
|
|
305
|
+
instancesOpen: 0,
|
|
306
|
+
event: null,
|
|
307
|
+
classic: { deckId: 'dev-classic', title, label: daily ? (lower ? 'Daily Tries' : 'Daily Score') : 'Score', daily, better: lower ? 'lower' : 'higher', cover, top },
|
|
308
|
+
};
|
|
309
|
+
}
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
// Flag Dash: the hub-game starter, and the whole game. A hub game ships no
|
|
2
|
+
// client code (Tier 0): the track is scene data, and this server hook is the
|
|
3
|
+
// rules. What the player sees of them is the HUD it declares, the gate it
|
|
4
|
+
// hides, and the result it reports. The hub draws all of it.
|
|
5
|
+
//
|
|
6
|
+
// gathering arrivals are held on the line; the countdown starts once the
|
|
7
|
+
// first group is in
|
|
8
|
+
// countdown 3, 2, 1, still held
|
|
9
|
+
// racing the gate is gone and everyone is let go; reaching the gold
|
|
10
|
+
// goal records a time
|
|
11
|
+
// over everyone finished or time ran out: results, then back
|
|
12
|
+
// through the door
|
|
13
|
+
//
|
|
14
|
+
// `hub` is the place's context (docs/contract.md §4): it carries the session,
|
|
15
|
+
// so any hook can send, hold, hide or report.
|
|
16
|
+
|
|
17
|
+
const GATHER_MS = 1500;
|
|
18
|
+
const COUNTDOWN_MS = 3000;
|
|
19
|
+
const FINISH_GRACE_MS = 15000;
|
|
20
|
+
const RACE_LIMIT_MS = 90000;
|
|
21
|
+
const OVER_HOLD_MS = 4000;
|
|
22
|
+
|
|
23
|
+
const state = {
|
|
24
|
+
phase: 'gathering',
|
|
25
|
+
phaseAt: 0,
|
|
26
|
+
firstArrivalAt: null,
|
|
27
|
+
startedAt: null,
|
|
28
|
+
goalZ: -30,
|
|
29
|
+
goalDepth: 4,
|
|
30
|
+
// playerId -> { name, startedAt, finishedAt }
|
|
31
|
+
racers: new Map(),
|
|
32
|
+
finished: [],
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
function setPhase(hub, phase) {
|
|
36
|
+
state.phase = phase;
|
|
37
|
+
state.phaseAt = hub.now;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function seconds(ms) {
|
|
41
|
+
return `${(ms / 1000).toFixed(1)}s`;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function hud(hub) {
|
|
45
|
+
const top = state.finished
|
|
46
|
+
.slice(0, 3)
|
|
47
|
+
.map((r, i) => `${i + 1}. ${r.name} ${seconds(r.time)}`)
|
|
48
|
+
.join(' ');
|
|
49
|
+
switch (state.phase) {
|
|
50
|
+
case 'gathering':
|
|
51
|
+
return { title: 'Flag Dash', text: 'waiting for everyone…' };
|
|
52
|
+
case 'countdown':
|
|
53
|
+
return {
|
|
54
|
+
title: 'Flag Dash',
|
|
55
|
+
big: String(Math.max(1, Math.ceil((COUNTDOWN_MS - (hub.now - state.phaseAt)) / 1000))),
|
|
56
|
+
text: 'get ready',
|
|
57
|
+
};
|
|
58
|
+
case 'racing':
|
|
59
|
+
return { title: 'Flag Dash', text: top || 'run to the gold!', timer: { since: state.startedAt } };
|
|
60
|
+
default:
|
|
61
|
+
return { title: 'Flag Dash', big: 'FINISH', text: top || 'nobody made it' };
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export default {
|
|
66
|
+
ready(hub) {
|
|
67
|
+
const goal = hub.actorsWith('HubGoal')[0];
|
|
68
|
+
if (goal) {
|
|
69
|
+
state.goalZ = goal.t.z;
|
|
70
|
+
state.goalDepth = goal.props.depth ?? 4;
|
|
71
|
+
}
|
|
72
|
+
hub.setHud(hud(hub));
|
|
73
|
+
},
|
|
74
|
+
|
|
75
|
+
// The hub has already put the group at the arrival, facing down the track.
|
|
76
|
+
// Before the start they are held there; a late arrival runs on their own
|
|
77
|
+
// clock.
|
|
78
|
+
arrive(hub, group) {
|
|
79
|
+
for (const { playerId, username } of group) {
|
|
80
|
+
state.racers.set(playerId, {
|
|
81
|
+
name: username,
|
|
82
|
+
startedAt: state.phase === 'racing' ? hub.now : null,
|
|
83
|
+
finishedAt: null,
|
|
84
|
+
});
|
|
85
|
+
if (state.phase === 'gathering' || state.phase === 'countdown') {
|
|
86
|
+
hub.hold(playerId);
|
|
87
|
+
} else if (state.phase === 'over' || state.phase === 'done') {
|
|
88
|
+
hub.end([playerId]);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
state.firstArrivalAt ??= hub.now;
|
|
92
|
+
},
|
|
93
|
+
|
|
94
|
+
leave(_hub, playerId) {
|
|
95
|
+
state.racers.delete(playerId);
|
|
96
|
+
},
|
|
97
|
+
|
|
98
|
+
step(hub) {
|
|
99
|
+
const now = hub.now;
|
|
100
|
+
if (state.phase === 'gathering' && state.firstArrivalAt !== null && now - state.firstArrivalAt >= GATHER_MS) {
|
|
101
|
+
setPhase(hub, 'countdown');
|
|
102
|
+
} else if (state.phase === 'countdown' && now - state.phaseAt >= COUNTDOWN_MS) {
|
|
103
|
+
setPhase(hub, 'racing');
|
|
104
|
+
state.startedAt = now;
|
|
105
|
+
hub.hide('start-gate');
|
|
106
|
+
for (const [playerId, racer] of state.racers) {
|
|
107
|
+
racer.startedAt ??= now;
|
|
108
|
+
hub.release(playerId);
|
|
109
|
+
}
|
|
110
|
+
} else if (state.phase === 'racing') {
|
|
111
|
+
for (const { playerId, at } of hub.players()) {
|
|
112
|
+
const racer = state.racers.get(playerId);
|
|
113
|
+
if (racer && racer.finishedAt === null && racer.startedAt !== null && at.z <= state.goalZ + state.goalDepth / 2) {
|
|
114
|
+
racer.finishedAt = now;
|
|
115
|
+
const time = now - racer.startedAt;
|
|
116
|
+
state.finished.push({ playerId, name: racer.name, time, at: now });
|
|
117
|
+
hub.result(playerId, { outcome: state.finished.length === 1 ? 'win' : 'lose', score: time });
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
const racing = [...state.racers.values()];
|
|
121
|
+
const allDone = racing.length > 0 && racing.every((r) => r.finishedAt !== null);
|
|
122
|
+
const firstAt = state.finished[0]?.at ?? null;
|
|
123
|
+
if (allDone || (firstAt !== null && now - firstAt >= FINISH_GRACE_MS) || now - state.startedAt >= RACE_LIMIT_MS) {
|
|
124
|
+
setPhase(hub, 'over');
|
|
125
|
+
for (const [playerId, racer] of state.racers) {
|
|
126
|
+
if (racer.finishedAt === null) {
|
|
127
|
+
hub.result(playerId, { outcome: 'lose' });
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
} else if (state.phase === 'over' && now - state.phaseAt >= OVER_HOLD_MS) {
|
|
132
|
+
hub.end();
|
|
133
|
+
setPhase(hub, 'done');
|
|
134
|
+
}
|
|
135
|
+
hub.setHud(hud(hub));
|
|
136
|
+
},
|
|
137
|
+
};
|