castle-web-cli 0.4.229 → 0.4.231
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 +860 -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/devSaveStorage.d.ts +36 -0
- package/dist/devSaveStorage.js +469 -0
- package/dist/devSessionChild.js +25 -13
- package/dist/devSessionServer.d.ts +14 -1
- package/dist/devSessionServer.js +237 -46
- package/dist/devSessionStorage.d.ts +9 -0
- package/dist/devSessionStorage.js +66 -1
- 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 +126 -9
- 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 +99 -14
- 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 +406 -53
- 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 +9 -0
- package/kits/base/sdk/castle.js +5 -0
- package/kits/base/sdk/commands.d.ts +223 -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 +158 -8
- package/kits/base/sdk/save.d.ts +197 -0
- package/kits/base/sdk/save.js +525 -0
- package/kits/base/sdk/saveCoordinator.d.ts +39 -0
- package/kits/base/sdk/saveCoordinator.js +252 -0
- 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 +58 -4
- package/kits/base/sdk/server/storage.js +25 -5
- package/kits/base/sdk/server/wrapper.d.ts +36 -2
- package/kits/base/sdk/server/wrapper.js +92 -11
- package/kits/base/sdk/store.d.ts +3 -2
- package/kits/base/sdk/store.js +5 -0
- 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 +149 -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 +1035 -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 +376 -0
- package/kits/hub-game/code/client/hookLayer.js +236 -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 +143 -0
- package/kits/hub-game/code/client/talk.js +233 -0
- package/kits/hub-game/code/client/tiers.js +480 -0
- package/kits/hub-game/code/client/together.js +440 -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 +248 -0
- package/kits/hub-game/code/server/history.js +96 -0
- package/kits/hub-game/code/server/hub.js +1998 -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 +554 -0
- package/kits/hub-game/code/server/npcs.js +733 -0
- package/kits/hub-game/code/server/party.js +439 -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 +2347 -0
- package/kits/hub-game/docs/content-pack.md +151 -0
- package/kits/hub-game/docs/contract.md +1209 -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-client.mjs +328 -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 +1518 -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 +270 -0
- package/kits/hub-game/scripts/test-no-forms.mjs +16 -0
- package/kits/hub-game/scripts/test-npcs.mjs +724 -0
- package/kits/hub-game/scripts/test-pack.mjs +424 -0
- package/kits/hub-game/scripts/test-party.mjs +369 -0
- package/kits/hub-game/scripts/test-say.mjs +149 -0
- package/kits/hub-game/scripts/test-tiers.mjs +666 -0
- package/kits/hub-game/scripts/test-together.mjs +215 -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/CLAUDE.md +2 -0
- 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 +117 -5
- package/kits/physics-2d/package.json +1 -1
- package/kits/physics-2d/scripts/test-skip-unchanged.mjs +254 -0
- package/kits/physics-3d/CLAUDE.md +285 -1
- package/kits/physics-3d/behaviors/Player.jsx +74 -1
- package/kits/physics-3d/castle.json +3 -3
- package/kits/physics-3d/engine3d/Scene3DPlayer.jsx +33 -5
- package/kits/physics-3d/engine3d/actornode.js +253 -0
- package/kits/physics-3d/engine3d/artobjects.js +2 -2
- package/kits/physics-3d/engine3d/materials.js +29 -1
- package/kits/physics-3d/engine3d/nearshadows.js +600 -0
- package/kits/physics-3d/engine3d/unchanged.js +153 -0
- package/kits/physics-3d/engine3d/world3d.js +527 -5
- package/kits/physics-3d/package.json +1 -1
- package/kits/physics-3d/scripts/test-camera-follow.mjs +533 -0
- package/kits/physics-3d/scripts/test-near-shadows.mjs +1073 -0
- package/kits/physics-3d/scripts/test-static-actors.mjs +662 -0
- package/kits/physics-3d/scripts/test-static-passes.mjs +459 -0
- package/kits/physics-3d/scripts/test-static-physics.mjs +128 -0
- package/kits/physics-3d/systems/physics3d.js +187 -5
- 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,1998 @@
|
|
|
1
|
+
// The hub layer every place's server runs (contract §4-§11): which place this
|
|
2
|
+
// session is, the avatar each player wears, arrival and exit, the declared HUD,
|
|
3
|
+
// actor visibility and labels, holding players still, and results. It wraps
|
|
4
|
+
// the deck's own hooks (`code/server/game.js`, or `places/<id>.js`) and hands
|
|
5
|
+
// each one `hub`, the place's context, which carries the session -- `step` can
|
|
6
|
+
// send without having captured anything first.
|
|
7
|
+
//
|
|
8
|
+
// It returns the multiplayer-3d hooks object `castle.real-time`'s session
|
|
9
|
+
// calls, so nothing below the kit changes for a hub game, plus
|
|
10
|
+
// `worldCharacters`: the session callbacks a place's NPCs answer in a world
|
|
11
|
+
// that carries shared characters (`npcs.js`).
|
|
12
|
+
|
|
13
|
+
import { readScene } from '@imports/castle.multiplayer-3d/code/server/sceneBodies.js';
|
|
14
|
+
import * as Actors from './actors.js';
|
|
15
|
+
import * as History from './history.js';
|
|
16
|
+
import * as Blocks from './blocks.js';
|
|
17
|
+
import * as Cabinets from './cabinets.js';
|
|
18
|
+
import * as Grab from './grab.js';
|
|
19
|
+
import * as Idle from './idle.js';
|
|
20
|
+
import * as Manifest from './manifest.js';
|
|
21
|
+
import { setLabel, wireLabel } from './labels.js';
|
|
22
|
+
import { makeNpcs } from './npcs.js';
|
|
23
|
+
import * as Party from './party.js';
|
|
24
|
+
import * as Place from './place.js';
|
|
25
|
+
import * as Say from './say.js';
|
|
26
|
+
import * as Wear from './wear.js';
|
|
27
|
+
import { doorPoint } from '../client/doorFrame.js';
|
|
28
|
+
|
|
29
|
+
// How many avatars the kit offers (`blueprints/avatar-<n>.scene`); contract §11.
|
|
30
|
+
export const AVATARS = 4;
|
|
31
|
+
|
|
32
|
+
// Players sent together by one `hub.go` arrive together: the first arrival of a
|
|
33
|
+
// group waits this long for the rest before the group is placed.
|
|
34
|
+
const GROUP_WAIT_MS = 1500;
|
|
35
|
+
// How far a held body may drift before it is clamped back.
|
|
36
|
+
const HOLD_SLACK = 0.05;
|
|
37
|
+
|
|
38
|
+
const DEG = Math.PI / 180;
|
|
39
|
+
|
|
40
|
+
const HUD_LIMITS = { title: 40, big: 12, text: 120 };
|
|
41
|
+
|
|
42
|
+
// Ghost forgets a gathering pad 30 s after its last update; refresh well inside it.
|
|
43
|
+
const PAD_REFRESH_MS = 20000;
|
|
44
|
+
// A door that stayed shut for a player is not tried again for them until they
|
|
45
|
+
// step off its pad (`hub.pad` says who stands there); a deck that never
|
|
46
|
+
// reports the pad gets this long instead.
|
|
47
|
+
const SHUT_HOLD_MS = 60000;
|
|
48
|
+
// How often a place re-reads its doors' boards.
|
|
49
|
+
const BOARD_EVERY_MS = 60000;
|
|
50
|
+
// The newest result of the game a player came back from counts as this trip's
|
|
51
|
+
// for this long: the longest visit (maxDurationSec 1800) and some slack.
|
|
52
|
+
const RESULT_FRESH_MS = 35 * 60 * 1000;
|
|
53
|
+
const MAX_BOARD_DOORS = 16;
|
|
54
|
+
// Cabinet boards in one read: each may be 44 KB, and a read answers in one
|
|
55
|
+
// frame of the host's channel (about 384 KB).
|
|
56
|
+
const CABINET_BOARDS_PER_READ = 6;
|
|
57
|
+
// Jump-to-door (the chrome's Doors list, in a town): how far in front of the
|
|
58
|
+
// door a player is put, how wide the spread across it, and how often one
|
|
59
|
+
// player may jump.
|
|
60
|
+
const JUMP_OUT = 7;
|
|
61
|
+
const JUMP_SPREAD = 2;
|
|
62
|
+
const JUMP_EVERY_MS = 1000;
|
|
63
|
+
// A player a client already sees counts as this much nearer when the nearest
|
|
64
|
+
// are picked, so two players at about the same distance do not swap in and
|
|
65
|
+
// out of its set every tick.
|
|
66
|
+
// Finding someone (the chrome's People list, in a town): how often one player
|
|
67
|
+
// may ask where others are, how many it may ask about at once, how near a
|
|
68
|
+
// door counts as "near" it, and how long a place's own words for a spot run.
|
|
69
|
+
const WHERE_EVERY_MS = 700;
|
|
70
|
+
const WHERE_MAX = 12;
|
|
71
|
+
const WHERE_NEAR_DOOR_M = 7;
|
|
72
|
+
const WHERE_TEXT_MAX = 40;
|
|
73
|
+
const KEEP_NEARER = 0.8;
|
|
74
|
+
// How often each client is sent other players' rows once the session filters
|
|
75
|
+
// by interest (HUB.md "Town sessions"), whatever the tick rate; a client's own
|
|
76
|
+
// corrections still go every tick.
|
|
77
|
+
const POSE_HZ = 10;
|
|
78
|
+
// How often a place with players in it logs its own process's load: CPU per
|
|
79
|
+
// tick and memory, read by the host's log stream (the only per-session
|
|
80
|
+
// numbers a production layer has).
|
|
81
|
+
const STATS_EVERY_MS = 30000;
|
|
82
|
+
const USER_ID = /^\d{1,20}$/;
|
|
83
|
+
|
|
84
|
+
// `layer(castleJson, deckHooks, placeHooks)`: the hooks object for one session
|
|
85
|
+
// process. `placeHooks` maps place id -> that place's own hooks module.
|
|
86
|
+
// A scene with no ground catches a fallen player this far down when its place
|
|
87
|
+
// names no `fallY`: above the world's floor (-30), below any pit worth having.
|
|
88
|
+
const NO_GROUND_FALL_Y = -20;
|
|
89
|
+
|
|
90
|
+
// `engine` is multiplayer-3d's world (`createBody`, `removeActor`), which
|
|
91
|
+
// builds and drops the bodies of actors a game spawns (`hub.spawn`).
|
|
92
|
+
export function layer(castleJson, deckHooks, placeHooks, sceneExists, sceneGround = () => null, devDecks = null, engine = null) {
|
|
93
|
+
const { errors, manifest } = Manifest.readManifest(castleJson, sceneExists);
|
|
94
|
+
if (!manifest) {
|
|
95
|
+
// A deck the check would refuse runs none of its game (contract §1): an
|
|
96
|
+
// empty room of its main scene, keeping nothing, its hooks never called
|
|
97
|
+
// -- so neither a client-named scene nor persistence comes back with them.
|
|
98
|
+
for (const error of errors) {
|
|
99
|
+
console.error(`[hub] ${error}`);
|
|
100
|
+
}
|
|
101
|
+
return { persist: false, sceneFor: () => undefined, worldCharacters: {} };
|
|
102
|
+
}
|
|
103
|
+
const h = newState(manifest);
|
|
104
|
+
h.devDecks = devDecks && typeof devDecks === 'object' ? devDecks : null;
|
|
105
|
+
h.engine = engine;
|
|
106
|
+
const tickRate = Number.isFinite(castleJson?.server?.tickRate) && castleJson.server.tickRate > 0 ? castleJson.server.tickRate : 20;
|
|
107
|
+
const poseEvery = Math.max(1, Math.round(tickRate / POSE_HZ));
|
|
108
|
+
|
|
109
|
+
return {
|
|
110
|
+
persist: false,
|
|
111
|
+
poseEvery,
|
|
112
|
+
poseIntervalMs: Math.round((poseEvery * 1000) / tickRate),
|
|
113
|
+
|
|
114
|
+
sceneFor(session) {
|
|
115
|
+
h.session = session;
|
|
116
|
+
const { id, source } = Place.resolvePlace(session, manifest);
|
|
117
|
+
h.placeId = id;
|
|
118
|
+
h.place = manifest.places[id];
|
|
119
|
+
h.hooks = placeHooks[id] ?? deckHooks;
|
|
120
|
+
h.noGround = sceneGround(h.place.scene)?.none === true;
|
|
121
|
+
h.fallY = h.place.fallY ?? (h.noGround ? NO_GROUND_FALL_Y : null);
|
|
122
|
+
console.log(`[hub] place ${id} (${source}): ${h.place.scene}`);
|
|
123
|
+
return h.place.scene;
|
|
124
|
+
},
|
|
125
|
+
|
|
126
|
+
ready(sim) {
|
|
127
|
+
h.sim = sim;
|
|
128
|
+
h.actors = readScene(sim.scenePath);
|
|
129
|
+
h.npcs.ready();
|
|
130
|
+
h.hooks.ready?.(h.ctx);
|
|
131
|
+
},
|
|
132
|
+
|
|
133
|
+
// Runs before the world exists too: a hello that lands first is kept and
|
|
134
|
+
// read when the player gets a body.
|
|
135
|
+
message(session, player, data) {
|
|
136
|
+
h.session ??= session;
|
|
137
|
+
if (data?.k === 'hub-hello') {
|
|
138
|
+
hello(h, player, data);
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
if (data?.k === 'hub-shut') {
|
|
142
|
+
doorShut(h, player, data);
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
if (data?.k === 'hub-door-jump') {
|
|
146
|
+
doorJump(h, player, data);
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
if (data?.k === 'hub-where') {
|
|
150
|
+
whereAsk(h, player, data);
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
if (data?.k === 'hub-goto') {
|
|
154
|
+
goTo(h, player, data);
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
if (data?.k === 'hub-say') {
|
|
158
|
+
Say.message({ h, blockedPairs, jumpDoors: doors }, player, data);
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
if (data?.k === 'hub-party') {
|
|
162
|
+
Party.message(partyDeps(h), player, data);
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
if (data?.k === 'hub-dev-classic') {
|
|
166
|
+
// The dev serve's stand-in for ghost's classic boards (`cabinets.js`).
|
|
167
|
+
if (h.sim && isDevSession(h)) {
|
|
168
|
+
(data.on === false ? Cabinets.fixtureOff : Cabinets.fixtureOn)(h, doors(h));
|
|
169
|
+
}
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
if (data?.k === 'hub-grab') {
|
|
173
|
+
Grab.message(h, player?.playerId, data);
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
if (data?.k === 'hub-emote') {
|
|
177
|
+
Idle.emoteMessage(h, player?.playerId, data);
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
if (data?.k === 'hub-sit') {
|
|
181
|
+
Idle.sitMessage(h, placeAt, player?.playerId, data);
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
if (data?.k === 'hub-stand') {
|
|
185
|
+
Idle.standMessage(h, player?.playerId);
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
if (h.sim && !h.npcs.message(player, data)) {
|
|
189
|
+
h.hooks.message?.(h.ctx, player, data);
|
|
190
|
+
}
|
|
191
|
+
},
|
|
192
|
+
|
|
193
|
+
// `handovers` is the session's list of ownership changes for the next
|
|
194
|
+
// broadcast; the handovers `hub.reset` made go into it (flushHandovers).
|
|
195
|
+
place(sim, handovers) {
|
|
196
|
+
h.sim = sim;
|
|
197
|
+
logStats(h);
|
|
198
|
+
const dt = h.lastPlaceAt === null ? 0 : Math.min(0.25, (sim.timeMs - h.lastPlaceAt) / 1000);
|
|
199
|
+
h.lastPlaceAt = sim.timeMs;
|
|
200
|
+
reconcile(h);
|
|
201
|
+
readResults(h);
|
|
202
|
+
arrivals(h);
|
|
203
|
+
enforceHolds(h);
|
|
204
|
+
enforceSettle(h);
|
|
205
|
+
Idle.tick(h);
|
|
206
|
+
enforceDuration(h);
|
|
207
|
+
catchFalls(h);
|
|
208
|
+
Grab.tick(h);
|
|
209
|
+
Actors.dropLostSpawns(h, h.engine);
|
|
210
|
+
Actors.driveMoves(h);
|
|
211
|
+
h.npcs.place(dt);
|
|
212
|
+
Actors.flushHandovers(h, handovers);
|
|
213
|
+
},
|
|
214
|
+
|
|
215
|
+
step(sim, handovers) {
|
|
216
|
+
const dt = h.lastStepAt === null ? 0 : (sim.timeMs - h.lastStepAt) / 1000;
|
|
217
|
+
h.lastStepAt = sim.timeMs;
|
|
218
|
+
h.hooks.step?.(h.ctx, dt);
|
|
219
|
+
Actors.flushTints(h);
|
|
220
|
+
sendHudOverrides(h);
|
|
221
|
+
Actors.applyTunes(h, h.engine?.maxSpeed?.() ?? 12);
|
|
222
|
+
sendLabelOverrides(h);
|
|
223
|
+
sendAvatarOverrides(h);
|
|
224
|
+
Party.tick(partyDeps(h));
|
|
225
|
+
Wear.sendOverrides(h, blockedPairs(h));
|
|
226
|
+
// Cabinet cards (v1.7): nothing in a place with no classic door.
|
|
227
|
+
Cabinets.send(h, (playerId) => namesOf(h, blockedPairs(h)?.get(playerId) ?? []));
|
|
228
|
+
Actors.flushHandovers(h, handovers);
|
|
229
|
+
},
|
|
230
|
+
|
|
231
|
+
// Each client is sent the place's nearest `nearestPlayers` others, not
|
|
232
|
+
// everyone (HUB.md "Town sessions"). Pads, holds and `hub.go` read the
|
|
233
|
+
// simulation, which still has every player.
|
|
234
|
+
// A pair a block hides from each other is taken out of both sets
|
|
235
|
+
// (`blocks.js`) before the nearest are picked, so a hidden player never
|
|
236
|
+
// takes one of a client's `nearestPlayers` places.
|
|
237
|
+
interest(sim, known) {
|
|
238
|
+
const pairs = blockedPairs(h);
|
|
239
|
+
const hidden = pairs ? (viewer, other) => Boolean(pairs.get(viewer)?.has(other)) : null;
|
|
240
|
+
return Blocks.withoutHidden(nearestPlayers(sim, known, h.place.nearestPlayers, hidden), pairs);
|
|
241
|
+
},
|
|
242
|
+
|
|
243
|
+
shutdown: () => shutdown(h),
|
|
244
|
+
|
|
245
|
+
join() {
|
|
246
|
+
return {
|
|
247
|
+
// `fallY` as this session catches it, so a falling client holds just
|
|
248
|
+
// under it rather than past the world's floor, where no pose is read.
|
|
249
|
+
hubPlace: { ...Manifest.publicPlace(h.placeId, h.place), fallY: h.fallY },
|
|
250
|
+
hubAv: avatars(h, blockedPairs(h)),
|
|
251
|
+
...wearingForJoin(h),
|
|
252
|
+
hud: hudForAll(h),
|
|
253
|
+
hidden: [...h.hidden],
|
|
254
|
+
held: [...h.held.keys()],
|
|
255
|
+
labels: labelsForAll(h),
|
|
256
|
+
tints: Actors.tintsOf(h),
|
|
257
|
+
marks: Actors.marksOf(h),
|
|
258
|
+
hubDoors: doorList(h),
|
|
259
|
+
spawned: Actors.spawnedOf(h),
|
|
260
|
+
spawnTemplates: Actors.templatesOf(h),
|
|
261
|
+
// v1.6, only in a place that turns idle play on.
|
|
262
|
+
...Idle.fields(h),
|
|
263
|
+
};
|
|
264
|
+
},
|
|
265
|
+
|
|
266
|
+
delta() {
|
|
267
|
+
const out = {};
|
|
268
|
+
latch(h, 'hubAv', avatars(h, blockedPairs(h)), out);
|
|
269
|
+
latchWearing(h, out);
|
|
270
|
+
latch(h, 'hud', hudForAll(h), out);
|
|
271
|
+
latch(h, 'hidden', [...h.hidden], out);
|
|
272
|
+
latch(h, 'held', [...h.held.keys()], out);
|
|
273
|
+
latch(h, 'labels', labelsForAll(h), out);
|
|
274
|
+
latch(h, 'tints', Actors.tintsOf(h), out);
|
|
275
|
+
latch(h, 'marks', Actors.marksOf(h), out);
|
|
276
|
+
latch(h, 'hubDoors', doorList(h), out);
|
|
277
|
+
latch(h, 'spawnTemplates', Actors.templatesOf(h), out);
|
|
278
|
+
latch(h, 'spawned', Actors.spawnedOf(h), out);
|
|
279
|
+
for (const [field, value] of Object.entries(Idle.fields(h))) {
|
|
280
|
+
latch(h, field, value, out);
|
|
281
|
+
}
|
|
282
|
+
return Object.keys(out).length ? out : null;
|
|
283
|
+
},
|
|
284
|
+
|
|
285
|
+
// Shared-character requests (a layer of the place, or a world) reach the
|
|
286
|
+
// place's NPCs; before the simulation is built there are none to answer,
|
|
287
|
+
// so they are refused.
|
|
288
|
+
worldCharacters: {
|
|
289
|
+
async onWorldCharacterTurn(session, turn) {
|
|
290
|
+
h.session ??= session;
|
|
291
|
+
if (!h.sim) {
|
|
292
|
+
turn.reject('HUB_NPC_NOT_READY');
|
|
293
|
+
return;
|
|
294
|
+
}
|
|
295
|
+
await h.npcs.onTurn(turn);
|
|
296
|
+
},
|
|
297
|
+
async onWorldCharacterCommand(session, request) {
|
|
298
|
+
h.session ??= session;
|
|
299
|
+
await h.npcs.onCommand(request);
|
|
300
|
+
},
|
|
301
|
+
async onWorldCharacterRead(session, request) {
|
|
302
|
+
h.session ??= session;
|
|
303
|
+
if (!h.sim) {
|
|
304
|
+
request.reject('HUB_NPC_NOT_READY');
|
|
305
|
+
return;
|
|
306
|
+
}
|
|
307
|
+
await h.npcs.onRead(request);
|
|
308
|
+
},
|
|
309
|
+
},
|
|
310
|
+
};
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
// playerId -> the Set of other players that client sees: all of them when
|
|
314
|
+
// they fit, otherwise the `n` nearest, each one it already sees counted
|
|
315
|
+
// `KEEP_NEARER` times its distance. `hidden(viewerId, otherId)`, when given,
|
|
316
|
+
// is true for a player that viewer must never see (a block): such a player is
|
|
317
|
+
// left out before the pick, so it takes none of the `n` places, and out of
|
|
318
|
+
// the everyone-fits set too.
|
|
319
|
+
export function nearestPlayers(sim, known, n, hidden = null) {
|
|
320
|
+
const ids = [...sim.players.keys()];
|
|
321
|
+
const sees = new Map();
|
|
322
|
+
const shown = (id, other) => other !== id && !hidden?.(id, other);
|
|
323
|
+
if (ids.length - 1 <= n) {
|
|
324
|
+
for (const id of ids) {
|
|
325
|
+
sees.set(id, new Set(ids.filter((other) => shown(id, other))));
|
|
326
|
+
}
|
|
327
|
+
return sees;
|
|
328
|
+
}
|
|
329
|
+
const at = ids.map((id) => sim.players.get(id).at);
|
|
330
|
+
for (let i = 0; i < ids.length; i += 1) {
|
|
331
|
+
const sent = known.get(ids[i]);
|
|
332
|
+
const scored = [];
|
|
333
|
+
for (let j = 0; j < ids.length; j += 1) {
|
|
334
|
+
if (!shown(ids[i], ids[j])) {
|
|
335
|
+
continue;
|
|
336
|
+
}
|
|
337
|
+
const dx = at[j].x - at[i].x;
|
|
338
|
+
const dy = at[j].y - at[i].y;
|
|
339
|
+
const dz = at[j].z - at[i].z;
|
|
340
|
+
const d = dx * dx + dy * dy + dz * dz;
|
|
341
|
+
scored.push([sent?.has(ids[j]) ? d * KEEP_NEARER * KEEP_NEARER : d, ids[j]]);
|
|
342
|
+
}
|
|
343
|
+
scored.sort((a, b) => a[0] - b[0]);
|
|
344
|
+
sees.set(ids[i], new Set(scored.slice(0, n).map(([, id]) => id)));
|
|
345
|
+
}
|
|
346
|
+
return sees;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
// `[hub] stats {...}` every STATS_EVERY_MS while anyone is here: players,
|
|
350
|
+
// ticks, CPU milliseconds per tick and as a share of one core, RSS and heap.
|
|
351
|
+
function logStats(h) {
|
|
352
|
+
if (typeof process === 'undefined' || typeof process.cpuUsage !== 'function') {
|
|
353
|
+
return;
|
|
354
|
+
}
|
|
355
|
+
const now = Date.now();
|
|
356
|
+
const s = h.stats;
|
|
357
|
+
if (s.at === null) {
|
|
358
|
+
Object.assign(s, { at: now, cpu: process.cpuUsage(), ticks: 0 });
|
|
359
|
+
return;
|
|
360
|
+
}
|
|
361
|
+
s.ticks += 1;
|
|
362
|
+
if (now - s.at < STATS_EVERY_MS) {
|
|
363
|
+
return;
|
|
364
|
+
}
|
|
365
|
+
const cpu = process.cpuUsage(s.cpu);
|
|
366
|
+
const cpuMs = (cpu.user + cpu.system) / 1000;
|
|
367
|
+
const players = h.sim?.players.size ?? 0;
|
|
368
|
+
if (players > 0) {
|
|
369
|
+
const mem = process.memoryUsage();
|
|
370
|
+
const round = (v) => Math.round(v * 100) / 100;
|
|
371
|
+
console.log(
|
|
372
|
+
`[hub] stats ${JSON.stringify({
|
|
373
|
+
players,
|
|
374
|
+
ticks: s.ticks,
|
|
375
|
+
cpuMsPerTick: round(cpuMs / Math.max(1, s.ticks)),
|
|
376
|
+
cpuPercent: round((cpuMs / (now - s.at)) * 100),
|
|
377
|
+
rssMb: Math.round(mem.rss / 1048576),
|
|
378
|
+
heapMb: Math.round(mem.heapUsed / 1048576),
|
|
379
|
+
})}`,
|
|
380
|
+
);
|
|
381
|
+
}
|
|
382
|
+
Object.assign(s, { at: now, cpu: process.cpuUsage(), ticks: 0 });
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
function newState(manifest) {
|
|
386
|
+
const h = {
|
|
387
|
+
manifest,
|
|
388
|
+
placeId: Manifest.MAIN_PLACE,
|
|
389
|
+
place: manifest.places[Manifest.MAIN_PLACE],
|
|
390
|
+
hooks: {},
|
|
391
|
+
session: null,
|
|
392
|
+
sim: null,
|
|
393
|
+
actors: [],
|
|
394
|
+
lastStepAt: null,
|
|
395
|
+
lastPlaceAt: null,
|
|
396
|
+
// the height below which a player has fallen, or null (`catchFalls`)
|
|
397
|
+
fallY: null,
|
|
398
|
+
// playerId -> when they last jumped to a door or to a player
|
|
399
|
+
jumpAt: new Map(),
|
|
400
|
+
// playerId -> when they last asked where others are (`hub-where`)
|
|
401
|
+
whereAt: new Map(),
|
|
402
|
+
// playerId -> { at, facing, until }: bodies kept where a jump put them (`settle`)
|
|
403
|
+
settling: new Map(),
|
|
404
|
+
// `logStats`: when the window began, the CPU reading then, ticks since
|
|
405
|
+
stats: { at: null, cpu: null, ticks: 0 },
|
|
406
|
+
// playerId -> { avatar, clientAvatar, from, fromDeckId, party, carry, read,
|
|
407
|
+
// hello, arrived, arrivedAt, helloAt, result, ended }
|
|
408
|
+
players: new Map(),
|
|
409
|
+
// door id -> the gathering pad sent to ghost: { padId, json, at }
|
|
410
|
+
pads: new Map(),
|
|
411
|
+
// playerId -> { door, at }: the door that last stayed shut for them
|
|
412
|
+
shut: new Map(),
|
|
413
|
+
// door id -> who the deck last said stands on its pad (`hub.pad`)
|
|
414
|
+
padDoors: new Map(),
|
|
415
|
+
// door id -> its board from ghost, and when the boards were last read
|
|
416
|
+
boards: new Map(),
|
|
417
|
+
// `${actorId}|${doorId}` -> the door's object `doors()` hands out
|
|
418
|
+
doorObjects: new Map(),
|
|
419
|
+
boardsAt: 0,
|
|
420
|
+
// `${op}:${code}` refusals already logged
|
|
421
|
+
refusals: new Set(),
|
|
422
|
+
hud: null,
|
|
423
|
+
// playerId -> spec, and what was last sent of each
|
|
424
|
+
hudMe: new Map(),
|
|
425
|
+
sentHudMe: new Map(),
|
|
426
|
+
hidden: new Set(),
|
|
427
|
+
// playerId -> { at, facing }
|
|
428
|
+
held: new Map(),
|
|
429
|
+
// actorId -> { text, style, until }
|
|
430
|
+
labels: new Map(),
|
|
431
|
+
// playerId -> Map(actorId -> label): words over an actor for some players
|
|
432
|
+
// only (an NPC's reply to the player it answers), and what was last sent
|
|
433
|
+
labelsMe: new Map(),
|
|
434
|
+
sentLabelsMe: new Map(),
|
|
435
|
+
// playerId -> JSON of the avatars last sent to that player alone
|
|
436
|
+
// (`sendAvatarOverrides`), and who a block hides from whom this tick
|
|
437
|
+
sentAvMe: new Map(),
|
|
438
|
+
blocked: { key: null, pairs: null },
|
|
439
|
+
// field -> JSON last sent, so a delta goes out only on change
|
|
440
|
+
sent: new Map(),
|
|
441
|
+
// moved and recoloured scene actors (`actors.js`)
|
|
442
|
+
...Actors.newActorState(),
|
|
443
|
+
// held and thrown objects (`grab.js`, v1.3)
|
|
444
|
+
...Grab.newGrabState(),
|
|
445
|
+
// what players wear on their avatars (`wear.js`, v1.4)
|
|
446
|
+
...Wear.newWearState(),
|
|
447
|
+
// emotes, seats and sounds (`idle.js`, v1.6)
|
|
448
|
+
...Idle.newIdleState(),
|
|
449
|
+
// arcade cabinets' cards (`cabinets.js`, v1.7)
|
|
450
|
+
...Cabinets.newCabinetState(),
|
|
451
|
+
// parties in a town (`party.js`, v1.10)
|
|
452
|
+
...Party.newPartyState(),
|
|
453
|
+
// quick reactions and phrases (`say.js`, v1.11)
|
|
454
|
+
...Say.newSayState(),
|
|
455
|
+
};
|
|
456
|
+
h.ctx = makeContext(h);
|
|
457
|
+
h.npcLabel = (actorId, text, options) => setLabel(h, isSceneActor(h, actorId) ? actorId : null, text, options, true);
|
|
458
|
+
h.blockedPairs = () => blockedPairs(h);
|
|
459
|
+
h.npcs = makeNpcs(h);
|
|
460
|
+
return h;
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
// The `hub` every deck hook is handed.
|
|
464
|
+
function makeContext(h) {
|
|
465
|
+
return {
|
|
466
|
+
get session() {
|
|
467
|
+
return h.session;
|
|
468
|
+
},
|
|
469
|
+
get sim() {
|
|
470
|
+
return h.sim;
|
|
471
|
+
},
|
|
472
|
+
get place() {
|
|
473
|
+
return { id: h.placeId, title: h.place.title, scene: h.place.scene, manifest: h.place };
|
|
474
|
+
},
|
|
475
|
+
get now() {
|
|
476
|
+
return h.sim?.timeMs ?? 0;
|
|
477
|
+
},
|
|
478
|
+
players() {
|
|
479
|
+
const out = [];
|
|
480
|
+
for (const [playerId, body] of h.sim?.players ?? []) {
|
|
481
|
+
out.push({
|
|
482
|
+
playerId,
|
|
483
|
+
username: usernameOf(h, playerId),
|
|
484
|
+
userId: userIdOf(h, playerId),
|
|
485
|
+
at: { ...body.at },
|
|
486
|
+
arrivedAt: h.players.get(playerId)?.arrivedAt ?? null,
|
|
487
|
+
});
|
|
488
|
+
}
|
|
489
|
+
return out;
|
|
490
|
+
},
|
|
491
|
+
actorsWith(name) {
|
|
492
|
+
return h.actors.filter((actor) => actor.components[name]).map((actor) => Actors.describe(actor, name));
|
|
493
|
+
},
|
|
494
|
+
username: (playerId) => usernameOf(h, playerId),
|
|
495
|
+
placeAt: (playerId, at, facingDeg) => placeAt(h, playerId, at, facingDeg === undefined ? undefined : facingDeg * DEG),
|
|
496
|
+
hold(playerId, at, facingDeg) {
|
|
497
|
+
const body = h.sim?.players.get(playerId);
|
|
498
|
+
if (!body) {
|
|
499
|
+
return;
|
|
500
|
+
}
|
|
501
|
+
const point = at ? { ...at } : { ...body.at };
|
|
502
|
+
const facing = facingDeg === undefined ? body.facing : facingDeg * DEG;
|
|
503
|
+
h.held.set(playerId, { at: point, facing });
|
|
504
|
+
placeAt(h, playerId, point, facing);
|
|
505
|
+
},
|
|
506
|
+
release(playerId) {
|
|
507
|
+
h.held.delete(playerId);
|
|
508
|
+
},
|
|
509
|
+
setHud(spec, playerId) {
|
|
510
|
+
const clean = cleanHud(spec);
|
|
511
|
+
if (playerId === undefined) {
|
|
512
|
+
h.hud = clean;
|
|
513
|
+
} else if (clean) {
|
|
514
|
+
h.hudMe.set(playerId, clean);
|
|
515
|
+
} else {
|
|
516
|
+
h.hudMe.delete(playerId);
|
|
517
|
+
}
|
|
518
|
+
},
|
|
519
|
+
hide(actorId) {
|
|
520
|
+
if (!isSceneActor(h, actorId) || h.hidden.has(actorId)) {
|
|
521
|
+
return;
|
|
522
|
+
}
|
|
523
|
+
h.hidden.add(actorId);
|
|
524
|
+
h.sim?.bodies.get(actorId)?.setEnabled(false);
|
|
525
|
+
},
|
|
526
|
+
show(actorId) {
|
|
527
|
+
if (!h.hidden.delete(actorId)) {
|
|
528
|
+
return;
|
|
529
|
+
}
|
|
530
|
+
h.sim?.bodies.get(actorId)?.setEnabled(true);
|
|
531
|
+
},
|
|
532
|
+
move: (actorId, to, options) => Actors.move(h, actorId, to, options),
|
|
533
|
+
tint: (actorId, color) => Actors.tint(h, actorId, color),
|
|
534
|
+
mark: (playerId, spec) => Actors.mark(h, playerId, spec),
|
|
535
|
+
results: (playerId) => History.results(h, playerId),
|
|
536
|
+
wear: (playerId, spec) => wear(h, playerId, spec),
|
|
537
|
+
tune: (playerId, spec) => Actors.tune(h, playerId, spec),
|
|
538
|
+
spawn: (fromActorId, at) => Actors.spawn(h, fromActorId, at, h.engine),
|
|
539
|
+
pose: (actorId) => Actors.pose(h, actorId),
|
|
540
|
+
reset: (actorId, at) => Actors.reset(h, actorId, at, h.engine),
|
|
541
|
+
despawn: (actorId) => Actors.despawn(h, actorId, h.engine),
|
|
542
|
+
// `to` limits the words to those players; for them they replace the
|
|
543
|
+
// label everyone else sees until they expire or are cleared the same way.
|
|
544
|
+
label: (actorId, text, options) => setLabel(h, isSceneActor(h, actorId) ? actorId : null, text, options, false),
|
|
545
|
+
npc: (actorId) => h.npcs.handle(actorId),
|
|
546
|
+
npcs: () => h.npcs.list(),
|
|
547
|
+
result: (playerId, value) => report(h, playerId, value),
|
|
548
|
+
drop: (playerId) => Grab.drop(h, playerId),
|
|
549
|
+
emote: (playerId, id) => Idle.emote(h, playerId, id),
|
|
550
|
+
seat: (playerId) => Idle.seat(h, playerId),
|
|
551
|
+
sound: (actorId, file, options) => Idle.sound(h, actorId, file, options),
|
|
552
|
+
holding: (playerId) => Grab.holding(h, playerId),
|
|
553
|
+
end(playerIds) {
|
|
554
|
+
const ids = playerIds ?? [...(h.sim?.players.keys() ?? [])];
|
|
555
|
+
for (const playerId of ids) {
|
|
556
|
+
end(h, playerId, 'end');
|
|
557
|
+
}
|
|
558
|
+
},
|
|
559
|
+
go: (door, playerIds, options) => go(h, door, playerIds, options),
|
|
560
|
+
pad: (door, playerIds) => gather(h, door, playerIds),
|
|
561
|
+
coming: (door) => Party.coming(h, door),
|
|
562
|
+
// v1.16: the other members of a player's party who are in this place now.
|
|
563
|
+
partyOf: (playerId) => Party.matesHere(h, playerId),
|
|
564
|
+
board: (door) => h.boards.get(door) ?? null,
|
|
565
|
+
doors: () => doors(h),
|
|
566
|
+
send: (playerId, data) => h.session?.send(playerId, data),
|
|
567
|
+
broadcast: (data) => h.session?.broadcast(data),
|
|
568
|
+
};
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
// The host's hub ops (`session.hub`): present only in a session ghost pinned
|
|
572
|
+
// to a hub place, never on the dev serve, which keeps its named keys.
|
|
573
|
+
function platformHub(h) {
|
|
574
|
+
return h.session?.hub ?? null;
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
// A client's hello: the avatar it wears, the door it came through, how many
|
|
578
|
+
// came with it, and what it carries back from a game (a town reads that). Where
|
|
579
|
+
// the host has hub ops, the door is ghost's arrival and the result and avatar
|
|
580
|
+
// are read from ghost (`readResults`); the client's word is not used for them.
|
|
581
|
+
function hello(h, player, data) {
|
|
582
|
+
const clientAvatar = Number.isInteger(data.avatar) && data.avatar >= 0 && data.avatar < AVATARS ? data.avatar : 0;
|
|
583
|
+
const party = Number.isInteger(data.party) && data.party >= 1 && data.party <= 24 ? data.party : 1;
|
|
584
|
+
const known = h.players.get(player.playerId);
|
|
585
|
+
const hub = platformHub(h);
|
|
586
|
+
const arrival = hub ? hub.arrival(player.playerId) : null;
|
|
587
|
+
const from = hub ? arrival?.doorId ?? null : typeof data.from === 'string' && data.from.length <= 32 ? data.from : null;
|
|
588
|
+
// A later hello with the same choice keeps the platform avatar a read found;
|
|
589
|
+
// a new choice wins, and outdates any read in flight (`avatarRev`).
|
|
590
|
+
const same = known?.hello && known.clientAvatar === clientAvatar;
|
|
591
|
+
// A hello that is not an avatar change is a client that joined afresh: it
|
|
592
|
+
// holds none of the cabinet cards it was sent (v1.7).
|
|
593
|
+
if (data.again !== true) {
|
|
594
|
+
Cabinets.forget(h, player.playerId);
|
|
595
|
+
}
|
|
596
|
+
h.players.set(player.playerId, {
|
|
597
|
+
// Who this is, kept for `leave`: by then the session has forgotten them.
|
|
598
|
+
userId: userIdOf(h, player.playerId) ?? known?.userId ?? null,
|
|
599
|
+
username: usernameOf(h, player.playerId),
|
|
600
|
+
avatar: same ? known.avatar : clientAvatar,
|
|
601
|
+
clientAvatar,
|
|
602
|
+
avatarRev: same ? known.avatarRev : (known?.avatarRev ?? 0) + 1,
|
|
603
|
+
from,
|
|
604
|
+
fromDeckId: arrival?.fromDeckId ?? null,
|
|
605
|
+
// A jump to a friend says whom (the client's word: it decides only where
|
|
606
|
+
// the player appears, beside someone already in this session).
|
|
607
|
+
near: typeof data.near === 'string' && USER_ID.test(data.near) ? data.near : (known?.near ?? null),
|
|
608
|
+
party,
|
|
609
|
+
carry: hub ? known?.carry ?? null : cleanCarry(data.carry),
|
|
610
|
+
read: known?.read ?? (hub ? 'want' : 'done'),
|
|
611
|
+
hello: true,
|
|
612
|
+
// A second hello (the avatar changed) keeps the player's place in an
|
|
613
|
+
// arrival group.
|
|
614
|
+
helloAt: known?.hello ? known.helloAt : Date.now(),
|
|
615
|
+
arrived: known?.arrived ?? false,
|
|
616
|
+
arrivedAt: known?.arrivedAt ?? null,
|
|
617
|
+
result: known?.result ?? null,
|
|
618
|
+
// ghost's results of this player for `hub.results`, kept across hellos
|
|
619
|
+
history: known?.history ?? null,
|
|
620
|
+
historyFrom: known?.historyFrom ?? null,
|
|
621
|
+
ended: known?.ended ?? false,
|
|
622
|
+
});
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
// What a returning client says it brings back: the result its game sent it.
|
|
626
|
+
// It is the client's word, display-only; read only where the host has no hub
|
|
627
|
+
// ops (the dev serve). Elsewhere a town reads the result from ghost (§7).
|
|
628
|
+
function cleanCarry(carry) {
|
|
629
|
+
const r = carry?.result;
|
|
630
|
+
if (!r || typeof r !== 'object' || typeof r.title !== 'string' || !outcomeOrNone(r.outcome)) {
|
|
631
|
+
return null;
|
|
632
|
+
}
|
|
633
|
+
return {
|
|
634
|
+
result: {
|
|
635
|
+
title: r.title.slice(0, 40),
|
|
636
|
+
outcome: r.outcome ?? null,
|
|
637
|
+
score: typeof r.score === 'number' && Number.isFinite(r.score) ? r.score : null,
|
|
638
|
+
unit: ['ms', 's', 'points'].includes(r.unit) ? r.unit : null,
|
|
639
|
+
verified: false,
|
|
640
|
+
},
|
|
641
|
+
};
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
// An outcome, or none: a result may carry only a score (contract §7, v1.1).
|
|
645
|
+
function outcomeOrNone(outcome) {
|
|
646
|
+
return outcome === undefined || outcome === null || Manifest.OUTCOMES.includes(outcome);
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
// Forget players the session no longer has, telling the deck first: its
|
|
650
|
+
// `leave` hook runs while the player is still known, so it may report their
|
|
651
|
+
// result for the visit (the host takes one for a player who has just left)
|
|
652
|
+
// and save their progress under the user id it is handed (v1.2).
|
|
653
|
+
function reconcile(h) {
|
|
654
|
+
for (const playerId of [...h.players.keys()]) {
|
|
655
|
+
const inSession = h.session?.players.some((p) => p.playerId === playerId);
|
|
656
|
+
if (!h.sim.players.has(playerId) && !inSession) {
|
|
657
|
+
forget(h, playerId);
|
|
658
|
+
}
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
function forget(h, playerId) {
|
|
663
|
+
const entry = h.players.get(playerId);
|
|
664
|
+
if (entry?.arrived) {
|
|
665
|
+
// A place's hook that throws still lets the player go, once.
|
|
666
|
+
try {
|
|
667
|
+
h.hooks.leave?.(h.ctx, playerId, { userId: entry.userId ?? null, username: entry.username ?? 'player' });
|
|
668
|
+
} catch (err) {
|
|
669
|
+
console.warn(`[hub] leave hook failed: ${err?.message ?? err}`);
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
Party.forget(h, playerId);
|
|
673
|
+
Say.forget(h, playerId);
|
|
674
|
+
h.players.delete(playerId);
|
|
675
|
+
h.held.delete(playerId);
|
|
676
|
+
h.hudMe.delete(playerId);
|
|
677
|
+
h.sentHudMe.delete(playerId);
|
|
678
|
+
h.tunes.delete(playerId);
|
|
679
|
+
h.sentTunes.delete(playerId);
|
|
680
|
+
h.labelsMe.delete(playerId);
|
|
681
|
+
h.sentLabelsMe.delete(playerId);
|
|
682
|
+
h.sentAvMe.delete(playerId);
|
|
683
|
+
h.marks.delete(playerId);
|
|
684
|
+
Wear.forget(h, playerId);
|
|
685
|
+
h.shut.delete(playerId);
|
|
686
|
+
h.jumpAt.delete(playerId);
|
|
687
|
+
h.whereAt.delete(playerId);
|
|
688
|
+
h.settling.delete(playerId);
|
|
689
|
+
Grab.forget(h, playerId);
|
|
690
|
+
Idle.forget(h, playerId);
|
|
691
|
+
Cabinets.forget(h, playerId);
|
|
692
|
+
h.npcs.leave(playerId);
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
// The session is stopping (its last player left, or Castle is replacing it):
|
|
696
|
+
// everyone still here leaves now, as `leave` would have told the deck on the
|
|
697
|
+
// next tick that never comes, and the deck's `shutdown` hook gets a moment
|
|
698
|
+
// to finish what it is writing (v1.2): inside the host's own shutdown grace,
|
|
699
|
+
// after which it stops taking storage writes.
|
|
700
|
+
const SHUTDOWN_DRAIN_MS = 1500;
|
|
701
|
+
|
|
702
|
+
async function shutdown(h) {
|
|
703
|
+
if (!h.sim) {
|
|
704
|
+
return;
|
|
705
|
+
}
|
|
706
|
+
for (const playerId of [...h.players.keys()]) {
|
|
707
|
+
forget(h, playerId);
|
|
708
|
+
}
|
|
709
|
+
let timer = null;
|
|
710
|
+
await Promise.race([
|
|
711
|
+
Promise.resolve()
|
|
712
|
+
.then(() => h.hooks.shutdown?.(h.ctx))
|
|
713
|
+
.catch((err) => console.warn(`[hub] shutdown hook failed: ${err?.message ?? err}`)),
|
|
714
|
+
new Promise((resolve) => {
|
|
715
|
+
timer = setTimeout(resolve, SHUTDOWN_DRAIN_MS);
|
|
716
|
+
}),
|
|
717
|
+
]);
|
|
718
|
+
clearTimeout(timer);
|
|
719
|
+
}
|
|
720
|
+
|
|
721
|
+
// Players with a body who said hello and have not arrived, placed as groups
|
|
722
|
+
// at the arrival (contract §5). A group is everyone who said hello inside one
|
|
723
|
+
// window; it is placed when it has as many as its members said were coming,
|
|
724
|
+
// or when the window closes.
|
|
725
|
+
function arrivals(h) {
|
|
726
|
+
const waiting = [];
|
|
727
|
+
for (const [playerId, entry] of h.players) {
|
|
728
|
+
if (entry.hello && !entry.arrived && h.sim.players.has(playerId)) {
|
|
729
|
+
waiting.push([playerId, entry]);
|
|
730
|
+
}
|
|
731
|
+
}
|
|
732
|
+
if (!waiting.length) {
|
|
733
|
+
return;
|
|
734
|
+
}
|
|
735
|
+
const first = Math.min(...waiting.map(([, e]) => e.helloAt));
|
|
736
|
+
const expected = Math.max(...waiting.map(([, e]) => e.party));
|
|
737
|
+
// A group also waits, inside the same window, for ghost's read of what its
|
|
738
|
+
// players bring back.
|
|
739
|
+
const reading = waiting.some(([, e]) => e.read !== 'done');
|
|
740
|
+
if ((waiting.length < expected || reading) && Date.now() - first < GROUP_WAIT_MS) {
|
|
741
|
+
// Whoever is in waits where they are, held, until the group is placed.
|
|
742
|
+
for (const [playerId] of waiting) {
|
|
743
|
+
if (!h.held.has(playerId)) {
|
|
744
|
+
const body = h.sim.players.get(playerId);
|
|
745
|
+
h.held.set(playerId, { at: { ...body.at }, facing: body.facing, waiting: true });
|
|
746
|
+
}
|
|
747
|
+
}
|
|
748
|
+
return;
|
|
749
|
+
}
|
|
750
|
+
const group = waiting.map(([playerId, entry]) => {
|
|
751
|
+
entry.arrived = true;
|
|
752
|
+
entry.arrivedAt = h.sim.timeMs;
|
|
753
|
+
if (h.held.get(playerId)?.waiting) {
|
|
754
|
+
h.held.delete(playerId);
|
|
755
|
+
}
|
|
756
|
+
return {
|
|
757
|
+
playerId,
|
|
758
|
+
username: usernameOf(h, playerId),
|
|
759
|
+
userId: entry.userId,
|
|
760
|
+
from: entry.from,
|
|
761
|
+
fromDeckId: entry.fromDeckId,
|
|
762
|
+
near: entry.near ?? null,
|
|
763
|
+
carry: entry.carry,
|
|
764
|
+
// How many went through the door together, by the client's word (the
|
|
765
|
+
// way back carries what the way out was sent with): 1 for anyone else.
|
|
766
|
+
party: entry.party ?? 1,
|
|
767
|
+
};
|
|
768
|
+
});
|
|
769
|
+
placeGroup(h, group.map((g) => g.playerId));
|
|
770
|
+
placeNearFriends(h, group);
|
|
771
|
+
h.hooks.arrive?.(h.ctx, group);
|
|
772
|
+
for (const { playerId } of group) {
|
|
773
|
+
const body = h.sim.players.get(playerId);
|
|
774
|
+
if (body) {
|
|
775
|
+
h.session?.send(playerId, { k: 'hub-arrived', facing: body.facing });
|
|
776
|
+
}
|
|
777
|
+
}
|
|
778
|
+
}
|
|
779
|
+
|
|
780
|
+
// A row at the arrival actor, facing the way the manifest says.
|
|
781
|
+
function placeGroup(h, playerIds) {
|
|
782
|
+
const arrival = h.place.arrival;
|
|
783
|
+
const actor = arrival.actor
|
|
784
|
+
? h.actors.find((a) => a.id === arrival.actor)
|
|
785
|
+
: h.actors.find((a) => a.components.Player);
|
|
786
|
+
const t = actor?.components.Transform ?? h.sim.spawn ?? { x: 0, y: 1, z: 0 };
|
|
787
|
+
let facing;
|
|
788
|
+
if (typeof arrival.face === 'number') {
|
|
789
|
+
facing = arrival.face * DEG;
|
|
790
|
+
} else if (arrival.face === 'center') {
|
|
791
|
+
facing = Math.atan2(-t.x, -t.z);
|
|
792
|
+
} else {
|
|
793
|
+
facing = (t.rotationY ?? 0) * DEG;
|
|
794
|
+
}
|
|
795
|
+
// Facing f looks along (sin f, cos f); the row runs across it.
|
|
796
|
+
const across = { x: Math.cos(facing), z: -Math.sin(facing) };
|
|
797
|
+
const n = playerIds.length;
|
|
798
|
+
playerIds.forEach((playerId, i) => {
|
|
799
|
+
const offset = (i - (n - 1) / 2) * arrival.spread;
|
|
800
|
+
placeAt(h, playerId, { x: t.x + across.x * offset, y: Math.max(t.y ?? 1, 1.2), z: t.z + across.z * offset }, facing);
|
|
801
|
+
});
|
|
802
|
+
}
|
|
803
|
+
|
|
804
|
+
// A player who jumped to a friend stands beside them, facing them, when the
|
|
805
|
+
// friend is in this session, a block hides neither from the other, and a spot
|
|
806
|
+
// beside them is clear (no solid actor there; on a scene with no ground, a
|
|
807
|
+
// solid underfoot); otherwise where the group was placed. Several jumping to
|
|
808
|
+
// one friend take the spots in turn.
|
|
809
|
+
const NEAR_STEP_M = 1.6;
|
|
810
|
+
// A standing player, for the clearance test: radius and half height.
|
|
811
|
+
const BODY_RADIUS_M = 0.45;
|
|
812
|
+
const BODY_HALF_M = 0.9;
|
|
813
|
+
|
|
814
|
+
function placeNearFriends(h, group) {
|
|
815
|
+
const used = new Map();
|
|
816
|
+
for (const { playerId, near } of group) {
|
|
817
|
+
if (!near) {
|
|
818
|
+
continue;
|
|
819
|
+
}
|
|
820
|
+
const friendId = [...h.players.entries()].find(
|
|
821
|
+
([id, entry]) => id !== playerId && entry.userId === near && entry.arrived && h.sim.players.has(id),
|
|
822
|
+
)?.[0];
|
|
823
|
+
if (friendId) {
|
|
824
|
+
placeBeside(h, playerId, friendId, used);
|
|
825
|
+
}
|
|
826
|
+
}
|
|
827
|
+
}
|
|
828
|
+
|
|
829
|
+
// Put a player beside another, facing them: true when a clear spot was found.
|
|
830
|
+
// `used` (other player id -> spots taken) lets several arriving at once take
|
|
831
|
+
// the spots in turn. Never across a block.
|
|
832
|
+
function placeBeside(h, playerId, otherId, used = new Map()) {
|
|
833
|
+
const other = h.sim.players.get(otherId);
|
|
834
|
+
if (!other?.at || blockedPairs(h)?.get(playerId)?.has(otherId)) {
|
|
835
|
+
return false;
|
|
836
|
+
}
|
|
837
|
+
const f = other.facing ?? 0;
|
|
838
|
+
// Facing f looks along (sin f, cos f): right, left, behind, ahead, then
|
|
839
|
+
// the same a step further out.
|
|
840
|
+
const dirs = [
|
|
841
|
+
[Math.cos(f), -Math.sin(f)],
|
|
842
|
+
[-Math.cos(f), Math.sin(f)],
|
|
843
|
+
[-Math.sin(f), -Math.cos(f)],
|
|
844
|
+
[Math.sin(f), Math.cos(f)],
|
|
845
|
+
];
|
|
846
|
+
const taken = used.get(otherId) ?? [];
|
|
847
|
+
// Never on or at the edge of a door's pad, theirs included (v1.19): a pad
|
|
848
|
+
// takes whoever stands on it into its game, and nobody goes into a game
|
|
849
|
+
// they did not choose. Beside a friend on a pad is a step or two further
|
|
850
|
+
// out, just off its edge and facing them, so joining them is that step.
|
|
851
|
+
for (const step of [1, 1.5, 2, 2.5, 3, 3.5]) {
|
|
852
|
+
const spot = dirs
|
|
853
|
+
.map(([dx, dz]) => ({ x: other.at.x + dx * NEAR_STEP_M * step, y: other.at.y, z: other.at.z + dz * NEAR_STEP_M * step }))
|
|
854
|
+
.find((at) => !taken.some((t) => Math.hypot(t.x - at.x, t.z - at.z) < 2 * BODY_RADIUS_M) && spotClear(h, at) && padAt(h, at, BODY_RADIUS_M) === null);
|
|
855
|
+
if (spot) {
|
|
856
|
+
taken.push(spot);
|
|
857
|
+
used.set(otherId, taken);
|
|
858
|
+
placeAt(h, playerId, spot, Math.atan2(other.at.x - spot.x, other.at.z - spot.z));
|
|
859
|
+
return true;
|
|
860
|
+
}
|
|
861
|
+
}
|
|
862
|
+
return false;
|
|
863
|
+
}
|
|
864
|
+
|
|
865
|
+
// The id of the pad actor a body at `at` stands on, or within `margin` of, or
|
|
866
|
+
// null. A pad takes a player whose middle is inside its radius.
|
|
867
|
+
function padAt(h, at, margin) {
|
|
868
|
+
for (const actor of h.actors) {
|
|
869
|
+
const pad = actor.components.HubPad;
|
|
870
|
+
const t = actor.components.Transform;
|
|
871
|
+
if (pad && t && !h.hidden.has(actor.id) && Math.hypot(at.x - t.x, at.z - t.z) <= (pad.radius ?? actor.components.Shape?.radius ?? 2.3) + margin) {
|
|
872
|
+
return actor.id;
|
|
873
|
+
}
|
|
874
|
+
}
|
|
875
|
+
return null;
|
|
876
|
+
}
|
|
877
|
+
|
|
878
|
+
// Whether a standing body fits at `at`: no solid scene actor overlaps it, and
|
|
879
|
+
// on a scene with no ground (`fallY`) a solid's top is under its feet.
|
|
880
|
+
function spotClear(h, at) {
|
|
881
|
+
let supported = h.noGround !== true;
|
|
882
|
+
for (const actor of h.actors) {
|
|
883
|
+
const c = actor.components;
|
|
884
|
+
const t = c.Transform;
|
|
885
|
+
const shape = c.Shape;
|
|
886
|
+
// Solid as physics reads it: a Solid component or a solid Shape.
|
|
887
|
+
if (!(c.Solid || shape?.solid === true) || !t || !shape) {
|
|
888
|
+
continue;
|
|
889
|
+
}
|
|
890
|
+
const half = shape.kind === 'sphere' ? (shape.radius ?? 0.5) : (shape.height ?? 1) / 2;
|
|
891
|
+
const top = (t.y ?? 0) + half;
|
|
892
|
+
const bottom = (t.y ?? 0) - half;
|
|
893
|
+
const inside = footprintHas(shape, t, at, BODY_RADIUS_M);
|
|
894
|
+
if (inside && top > at.y - BODY_HALF_M + 0.05 && bottom < at.y + BODY_HALF_M) {
|
|
895
|
+
return false;
|
|
896
|
+
}
|
|
897
|
+
if (!supported && footprintHas(shape, t, at, 0) && Math.abs(top - (at.y - BODY_HALF_M)) < 0.6) {
|
|
898
|
+
supported = true;
|
|
899
|
+
}
|
|
900
|
+
}
|
|
901
|
+
return supported;
|
|
902
|
+
}
|
|
903
|
+
|
|
904
|
+
// Whether a point lies within a shape's footprint, grown by `pad`.
|
|
905
|
+
function footprintHas(shape, t, at, pad) {
|
|
906
|
+
const dx = at.x - t.x;
|
|
907
|
+
const dz = at.z - t.z;
|
|
908
|
+
if (shape.kind === 'box') {
|
|
909
|
+
const r = ((t.rotationY ?? 0) * Math.PI) / 180;
|
|
910
|
+
const lx = dx * Math.cos(r) - dz * Math.sin(r);
|
|
911
|
+
const lz = dx * Math.sin(r) + dz * Math.cos(r);
|
|
912
|
+
return Math.abs(lx) <= (shape.width ?? 1) / 2 + pad && Math.abs(lz) <= (shape.depth ?? 1) / 2 + pad;
|
|
913
|
+
}
|
|
914
|
+
return Math.hypot(dx, dz) <= (shape.radius ?? 0.5) + pad;
|
|
915
|
+
}
|
|
916
|
+
|
|
917
|
+
// Move a body. One placed before the joiner's WORLD snapshot is where that
|
|
918
|
+
// snapshot starts them; a later one reaches the client as a clamp.
|
|
919
|
+
function placeAt(h, playerId, at, facing) {
|
|
920
|
+
const body = h.sim?.players.get(playerId);
|
|
921
|
+
if (!body || !at || ![at.x, at.y, at.z].every(Number.isFinite)) {
|
|
922
|
+
return;
|
|
923
|
+
}
|
|
924
|
+
body.at = { x: at.x, y: at.y, z: at.z };
|
|
925
|
+
if (facing !== undefined && Number.isFinite(facing)) {
|
|
926
|
+
body.facing = facing;
|
|
927
|
+
}
|
|
928
|
+
body.clamped = true;
|
|
929
|
+
body.body.setTranslation(body.at, true);
|
|
930
|
+
}
|
|
931
|
+
|
|
932
|
+
// After a jump (to a door's pad, beside a player) the body is kept where it
|
|
933
|
+
// was put for a moment. The client's move reports already on their way still
|
|
934
|
+
// name the spot it left, and the server's movement clamp lets each one pull
|
|
935
|
+
// the body a step back toward it: on a slow phone that dragged a player a
|
|
936
|
+
// metre or two off the pad they had just been put on, and the pad never
|
|
937
|
+
// counted them. This is not a hold (contract §8): the player is not in the
|
|
938
|
+
// `held` list, keeps what they carry and may jump again at once; the body is
|
|
939
|
+
// only put back each tick it drifts, until the client has caught up.
|
|
940
|
+
const SETTLE_MS = 600;
|
|
941
|
+
|
|
942
|
+
function settle(h, playerId) {
|
|
943
|
+
const body = h.sim?.players.get(playerId);
|
|
944
|
+
if (body) {
|
|
945
|
+
h.settling.set(playerId, { at: { ...body.at }, facing: body.facing, until: Date.now() + SETTLE_MS });
|
|
946
|
+
}
|
|
947
|
+
}
|
|
948
|
+
|
|
949
|
+
function enforceSettle(h) {
|
|
950
|
+
const nowMs = Date.now();
|
|
951
|
+
for (const [playerId, spot] of h.settling) {
|
|
952
|
+
const body = h.sim.players.get(playerId);
|
|
953
|
+
// Over, gone, or held by the place (a hold has its own spot).
|
|
954
|
+
if (!body || nowMs >= spot.until || h.held.has(playerId)) {
|
|
955
|
+
h.settling.delete(playerId);
|
|
956
|
+
continue;
|
|
957
|
+
}
|
|
958
|
+
if (Math.hypot(body.at.x - spot.at.x, body.at.z - spot.at.z) > HOLD_SLACK) {
|
|
959
|
+
placeAt(h, playerId, spot.at, spot.facing);
|
|
960
|
+
}
|
|
961
|
+
}
|
|
962
|
+
}
|
|
963
|
+
|
|
964
|
+
// A held body that drifted is put back, every tick it drifts (contract §8).
|
|
965
|
+
function enforceHolds(h) {
|
|
966
|
+
for (const [playerId, hold] of h.held) {
|
|
967
|
+
const body = h.sim.players.get(playerId);
|
|
968
|
+
if (!body) {
|
|
969
|
+
continue;
|
|
970
|
+
}
|
|
971
|
+
const drifted =
|
|
972
|
+
Math.hypot(body.at.x - hold.at.x, body.at.z - hold.at.z) > HOLD_SLACK || Math.abs(body.at.y - hold.at.y) > 0.5;
|
|
973
|
+
if (drifted) {
|
|
974
|
+
placeAt(h, playerId, hold.at, hold.facing);
|
|
975
|
+
} else if (body.facing !== hold.facing) {
|
|
976
|
+
// A move report carries facing even when the body stayed put.
|
|
977
|
+
body.facing = hold.facing;
|
|
978
|
+
}
|
|
979
|
+
}
|
|
980
|
+
}
|
|
981
|
+
|
|
982
|
+
// A player who fell below the place's `fallY` (contract §5, v1.1): the game's
|
|
983
|
+
// `fall` hook may put them somewhere; if it leaves them down there, the kit
|
|
984
|
+
// puts them back at the arrival.
|
|
985
|
+
function catchFalls(h) {
|
|
986
|
+
if (h.fallY === null) {
|
|
987
|
+
return;
|
|
988
|
+
}
|
|
989
|
+
for (const [playerId, entry] of h.players) {
|
|
990
|
+
const body = h.sim.players.get(playerId);
|
|
991
|
+
if (!body || !entry.arrived || entry.ended || h.held.has(playerId) || body.at.y >= h.fallY) {
|
|
992
|
+
continue;
|
|
993
|
+
}
|
|
994
|
+
h.hooks.fall?.(h.ctx, playerId);
|
|
995
|
+
if (h.sim.players.get(playerId)?.at.y < h.fallY && !h.held.has(playerId)) {
|
|
996
|
+
placeGroup(h, [playerId]);
|
|
997
|
+
}
|
|
998
|
+
}
|
|
999
|
+
}
|
|
1000
|
+
|
|
1001
|
+
// Nobody stays past the place's maximum visit (contract §5).
|
|
1002
|
+
function enforceDuration(h) {
|
|
1003
|
+
if (h.place.maxDurationSec === null) {
|
|
1004
|
+
return;
|
|
1005
|
+
}
|
|
1006
|
+
const limit = h.place.maxDurationSec * 1000;
|
|
1007
|
+
for (const [playerId, entry] of h.players) {
|
|
1008
|
+
if (entry.arrived && !entry.ended && h.sim.timeMs - entry.arrivedAt >= limit) {
|
|
1009
|
+
console.log(`[hub] ${usernameOf(h, playerId)} reached maxDurationSec`);
|
|
1010
|
+
end(h, playerId, 'time');
|
|
1011
|
+
}
|
|
1012
|
+
}
|
|
1013
|
+
}
|
|
1014
|
+
|
|
1015
|
+
// The player's result for this visit, at most once (contract §7).
|
|
1016
|
+
function report(h, playerId, value) {
|
|
1017
|
+
const entry = h.players.get(playerId);
|
|
1018
|
+
if (!entry) {
|
|
1019
|
+
console.warn(`[hub] result for ${playerId}, who is not here`);
|
|
1020
|
+
return false;
|
|
1021
|
+
}
|
|
1022
|
+
if (entry.result) {
|
|
1023
|
+
console.warn(`[hub] a second result for ${usernameOf(h, playerId)} was refused`);
|
|
1024
|
+
return false;
|
|
1025
|
+
}
|
|
1026
|
+
const { error, result } = Manifest.checkResult(h.place, value);
|
|
1027
|
+
if (error) {
|
|
1028
|
+
console.warn(`[hub] result refused: ${error}`);
|
|
1029
|
+
return false;
|
|
1030
|
+
}
|
|
1031
|
+
entry.result = result;
|
|
1032
|
+
const shown = {
|
|
1033
|
+
title: h.place.title,
|
|
1034
|
+
...result,
|
|
1035
|
+
unit: h.place.result.score?.unit ?? null,
|
|
1036
|
+
better: h.place.result.score?.better ?? null,
|
|
1037
|
+
};
|
|
1038
|
+
// Where the host has hub ops the result is kept: checked again against the
|
|
1039
|
+
// published shape and recorded by ghost for a signed-in player.
|
|
1040
|
+
const hub = platformHub(h);
|
|
1041
|
+
if (hub) {
|
|
1042
|
+
hub
|
|
1043
|
+
.reportResult(playerId, result)
|
|
1044
|
+
.then((receipt) => {
|
|
1045
|
+
if (!receipt?.recorded) {
|
|
1046
|
+
console.log(`[hub] result for ${usernameOf(h, playerId)} not recorded: ${receipt?.reason ?? 'unknown'}`);
|
|
1047
|
+
}
|
|
1048
|
+
})
|
|
1049
|
+
.catch((err) => refused(h, 'result', err));
|
|
1050
|
+
}
|
|
1051
|
+
h.session?.send(playerId, { k: 'hub-result', result: shown });
|
|
1052
|
+
return true;
|
|
1053
|
+
}
|
|
1054
|
+
|
|
1055
|
+
function end(h, playerId, why) {
|
|
1056
|
+
const entry = h.players.get(playerId);
|
|
1057
|
+
if (entry) {
|
|
1058
|
+
entry.ended = true;
|
|
1059
|
+
}
|
|
1060
|
+
h.held.delete(playerId);
|
|
1061
|
+
h.session?.send(playerId, { k: 'hub-end', why });
|
|
1062
|
+
}
|
|
1063
|
+
|
|
1064
|
+
// This place's doors (contract §4): in dev, the `HubDoor` scene actors whose
|
|
1065
|
+
// `to` is a place the deck declares, with what the destination's manifest says
|
|
1066
|
+
// about who may go through. The door table replaces the scene actors.
|
|
1067
|
+
// The doors a player in this place may jump to: a town's (a place with no
|
|
1068
|
+
// `result`) that are not hidden, as `[{ id, title }]`; a game has none.
|
|
1069
|
+
// Recomputed each time, so a door the place hides, or one whose destination
|
|
1070
|
+
// changes, leaves or changes the list (`latch` sends only a change).
|
|
1071
|
+
function jumpDoors(h) {
|
|
1072
|
+
if (h.place.result) {
|
|
1073
|
+
return [];
|
|
1074
|
+
}
|
|
1075
|
+
// A cabinet is not a door to walk up to from across the square (v1.7).
|
|
1076
|
+
return doors(h).filter((door) => !h.hidden.has(door.actor) && !Cabinets.isCabinetDoor(door.props) && !door.classic);
|
|
1077
|
+
}
|
|
1078
|
+
|
|
1079
|
+
// The Doors list's rows. An event gate leads to a game another door already
|
|
1080
|
+
// leads to, so it is listed only while its event is open, and as the event
|
|
1081
|
+
// (two rows reading "King of the Hill" was one too many).
|
|
1082
|
+
function doorList(h) {
|
|
1083
|
+
const out = [];
|
|
1084
|
+
for (const door of jumpDoors(h)) {
|
|
1085
|
+
const event = h.boards.get(door.id)?.event;
|
|
1086
|
+
if (event && !event.open) {
|
|
1087
|
+
continue;
|
|
1088
|
+
}
|
|
1089
|
+
const row = { id: door.id, title: event ? `Event · ${door.title}`.slice(0, 48) : door.title };
|
|
1090
|
+
// What a player picking a door wants to know: today's featured door (it
|
|
1091
|
+
// is listed first), one that takes a single player, how many it needs.
|
|
1092
|
+
if (h.boards.get(door.id)?.featured === true) {
|
|
1093
|
+
row.featured = true;
|
|
1094
|
+
}
|
|
1095
|
+
// Whether someone can still be followed in: a game under way takes late
|
|
1096
|
+
// joiners only where its door says so.
|
|
1097
|
+
if (door.lateJoin === true) {
|
|
1098
|
+
row.late = true;
|
|
1099
|
+
}
|
|
1100
|
+
if (door.maxPlayers === 1) {
|
|
1101
|
+
row.solo = true;
|
|
1102
|
+
} else if (door.minPlayers > 1) {
|
|
1103
|
+
row.need = door.minPlayers;
|
|
1104
|
+
}
|
|
1105
|
+
out.push(row);
|
|
1106
|
+
}
|
|
1107
|
+
// A stable sort: the featured doors first, each group in the place's order.
|
|
1108
|
+
return out.sort((a, b) => Number(b.featured === true) - Number(a.featured === true));
|
|
1109
|
+
}
|
|
1110
|
+
|
|
1111
|
+
// Put a player on the pad of a door of this town (in front of a door with no
|
|
1112
|
+
// pad), facing the door, from the chrome's Doors list. A held player, a game, or a door the place does not have is
|
|
1113
|
+
// ignored, and one player jumps at most once a second.
|
|
1114
|
+
function doorJump(h, player, data) {
|
|
1115
|
+
const door = h.sim ? jumpDoors(h).find((d) => d.id === data?.door) : null;
|
|
1116
|
+
const now = Date.now();
|
|
1117
|
+
// A sitter who jumps gets up first (v1.6); a player the place holds does not jump.
|
|
1118
|
+
if (door && now - (h.jumpAt.get(player.playerId) ?? 0) >= JUMP_EVERY_MS) {
|
|
1119
|
+
Idle.stand(h, player.playerId);
|
|
1120
|
+
}
|
|
1121
|
+
if (!door || h.held.has(player.playerId) || now - (h.jumpAt.get(player.playerId) ?? 0) < JUMP_EVERY_MS) {
|
|
1122
|
+
return;
|
|
1123
|
+
}
|
|
1124
|
+
h.jumpAt.set(player.playerId, now);
|
|
1125
|
+
// Onto the door's pad where it has one (v1.10), a little off its middle so
|
|
1126
|
+
// two who jump there do not land in one spot; else in front of the door.
|
|
1127
|
+
const at = Party.padPoint(h, door, Math.floor(Math.random() * 6), player.playerId) ?? frontOf(door);
|
|
1128
|
+
const facing = ((door.t.rotationY ?? 0) + 180) * DEG;
|
|
1129
|
+
placeAt(h, player.playerId, { x: at.x, y: 1.2, z: at.z }, facing);
|
|
1130
|
+
settle(h, player.playerId);
|
|
1131
|
+
h.session?.send(player.playerId, { k: 'hub-jumped', facing });
|
|
1132
|
+
}
|
|
1133
|
+
|
|
1134
|
+
// A spot in front of a door, for a jump to a door with no pad.
|
|
1135
|
+
function frontOf(door) {
|
|
1136
|
+
const t = door.t;
|
|
1137
|
+
return doorPoint({ x: t.x, z: t.z, rotationY: t.rotationY ?? 0 }, (Math.random() - 0.5) * JUMP_SPREAD, JUMP_OUT);
|
|
1138
|
+
}
|
|
1139
|
+
|
|
1140
|
+
// What `party.js` uses of this layer.
|
|
1141
|
+
function partyDeps(h) {
|
|
1142
|
+
// A party member put on a pad is settled there like any jump.
|
|
1143
|
+
const placeAndSettle = (state, playerId, at, facing) => {
|
|
1144
|
+
placeAt(state, playerId, at, facing);
|
|
1145
|
+
settle(state, playerId);
|
|
1146
|
+
};
|
|
1147
|
+
return { h, blockedPairs, usernameOf, placeAt: placeAndSettle, stand: Idle.stand, jumpDoors, front: frontOf };
|
|
1148
|
+
}
|
|
1149
|
+
|
|
1150
|
+
// Where the players a client asks about are (the chrome's People list and its
|
|
1151
|
+
// pointer to a friend, in a town): each one's spot on the ground and the
|
|
1152
|
+
// place's words for it. Only players in this session a block does not hide
|
|
1153
|
+
// from the asker; a game answers nothing.
|
|
1154
|
+
function whereAsk(h, player, data) {
|
|
1155
|
+
const playerId = player?.playerId;
|
|
1156
|
+
const now = Date.now();
|
|
1157
|
+
if (!h.sim || h.place.result || !playerId || !Array.isArray(data?.ids) || now - (h.whereAt.get(playerId) ?? 0) < WHERE_EVERY_MS) {
|
|
1158
|
+
return;
|
|
1159
|
+
}
|
|
1160
|
+
h.whereAt.set(playerId, now);
|
|
1161
|
+
const hidden = blockedPairs(h)?.get(playerId);
|
|
1162
|
+
const at = {};
|
|
1163
|
+
for (const id of data.ids.slice(0, WHERE_MAX)) {
|
|
1164
|
+
const body = typeof id === 'string' && id !== playerId ? h.sim.players.get(id) : null;
|
|
1165
|
+
if (!body?.at || !h.players.get(id)?.arrived || hidden?.has(id)) {
|
|
1166
|
+
continue;
|
|
1167
|
+
}
|
|
1168
|
+
at[id] = { x: Math.round(body.at.x * 10) / 10, z: Math.round(body.at.z * 10) / 10, ...whereIs(h, body.at) };
|
|
1169
|
+
}
|
|
1170
|
+
h.session?.send(playerId, { k: 'hub-where', at });
|
|
1171
|
+
}
|
|
1172
|
+
|
|
1173
|
+
// The place's words for a spot, `{ where, door }`: on a door's pad (`door`
|
|
1174
|
+
// names it, `on` true), else beside a door (`door` again), else what the
|
|
1175
|
+
// place's `where` hook calls it, else nothing.
|
|
1176
|
+
function whereIs(h, at) {
|
|
1177
|
+
const list = jumpDoors(h);
|
|
1178
|
+
for (const actor of h.actors) {
|
|
1179
|
+
const pad = actor.components.HubPad;
|
|
1180
|
+
const t = actor.components.Transform;
|
|
1181
|
+
const door = pad && t ? list.find((d) => d.id === pad.door) : null;
|
|
1182
|
+
if (door && !h.hidden.has(actor.id) && Math.hypot(at.x - t.x, at.z - t.z) <= (pad.radius ?? actor.components.Shape?.radius ?? 2.3)) {
|
|
1183
|
+
return { where: `on the ${door.title} pad`, door: door.id, on: true };
|
|
1184
|
+
}
|
|
1185
|
+
}
|
|
1186
|
+
let best = null;
|
|
1187
|
+
for (const door of list) {
|
|
1188
|
+
const d = Math.hypot(at.x - door.t.x, at.z - door.t.z);
|
|
1189
|
+
if (d <= WHERE_NEAR_DOOR_M && (!best || d < best.d)) {
|
|
1190
|
+
best = { d, door };
|
|
1191
|
+
}
|
|
1192
|
+
}
|
|
1193
|
+
if (best) {
|
|
1194
|
+
return { where: `near ${best.door.title}`, door: best.door.id };
|
|
1195
|
+
}
|
|
1196
|
+
let words = null;
|
|
1197
|
+
try {
|
|
1198
|
+
words = h.hooks.where?.(h.ctx, { x: at.x, y: at.y, z: at.z });
|
|
1199
|
+
} catch (err) {
|
|
1200
|
+
console.warn(`[hub] where hook failed: ${err?.message ?? err}`);
|
|
1201
|
+
}
|
|
1202
|
+
return { where: typeof words === 'string' && words.trim() ? words.trim().slice(0, WHERE_TEXT_MAX) : null, door: null };
|
|
1203
|
+
}
|
|
1204
|
+
|
|
1205
|
+
// Put a player beside another player of this town, facing them, from the
|
|
1206
|
+
// chrome's People list (Join). The same limits as a jump to a door: a town
|
|
1207
|
+
// only, never a held player, once a second; and never across a block.
|
|
1208
|
+
function goTo(h, player, data) {
|
|
1209
|
+
const playerId = player?.playerId;
|
|
1210
|
+
const otherId = data?.player;
|
|
1211
|
+
const now = Date.now();
|
|
1212
|
+
if (
|
|
1213
|
+
!h.sim || h.place.result || !playerId || typeof otherId !== 'string' || otherId === playerId ||
|
|
1214
|
+
!h.players.get(playerId)?.arrived || !h.players.get(otherId)?.arrived || !h.sim.players.has(otherId) ||
|
|
1215
|
+
now - (h.jumpAt.get(playerId) ?? 0) < JUMP_EVERY_MS ||
|
|
1216
|
+
// Before anything of this player's changes: a blocked player is as absent
|
|
1217
|
+
// as one who is not here.
|
|
1218
|
+
blockedPairs(h)?.get(playerId)?.has(otherId)
|
|
1219
|
+
) {
|
|
1220
|
+
return;
|
|
1221
|
+
}
|
|
1222
|
+
// A sitter who jumps gets up first (v1.6); a player the place holds does not jump.
|
|
1223
|
+
Idle.stand(h, playerId);
|
|
1224
|
+
if (h.held.has(playerId) || !placeBeside(h, playerId, otherId)) {
|
|
1225
|
+
return;
|
|
1226
|
+
}
|
|
1227
|
+
h.jumpAt.set(playerId, now);
|
|
1228
|
+
settle(h, playerId);
|
|
1229
|
+
h.session?.send(playerId, { k: 'hub-jumped', facing: h.sim.players.get(playerId).facing, to: otherId });
|
|
1230
|
+
}
|
|
1231
|
+
|
|
1232
|
+
// The players staying in a town are told who went through which door
|
|
1233
|
+
// (`hub-went`, v1.15), so a friend left behind can follow: each client shows
|
|
1234
|
+
// it only for its own friends and party. Never across a block, and not for a
|
|
1235
|
+
// cabinet (a classic deck is played alone).
|
|
1236
|
+
function tellWent(h, goingIds, door) {
|
|
1237
|
+
if (!h.sim || h.place.result || !goingIds.length) {
|
|
1238
|
+
return;
|
|
1239
|
+
}
|
|
1240
|
+
const going = new Set(goingIds);
|
|
1241
|
+
const pairs = blockedPairs(h);
|
|
1242
|
+
for (const [playerId, entry] of h.players) {
|
|
1243
|
+
if (!entry.arrived || going.has(playerId)) {
|
|
1244
|
+
continue;
|
|
1245
|
+
}
|
|
1246
|
+
const hidden = pairs?.get(playerId);
|
|
1247
|
+
const who = goingIds.filter((id) => !hidden?.has(id));
|
|
1248
|
+
if (who.length) {
|
|
1249
|
+
h.session?.send(playerId, { k: 'hub-went', players: who, door });
|
|
1250
|
+
}
|
|
1251
|
+
}
|
|
1252
|
+
}
|
|
1253
|
+
|
|
1254
|
+
function doors(h) {
|
|
1255
|
+
const out = [];
|
|
1256
|
+
for (const actor of h.actors) {
|
|
1257
|
+
const props = actor.components.HubDoor;
|
|
1258
|
+
const deck = props ? otherDeck(props.to) : null;
|
|
1259
|
+
const dest = deck ? deckDoorPlace(h, props, deck) : props ? h.manifest.places[props.to] : null;
|
|
1260
|
+
if (!dest || typeof props.id !== 'string') {
|
|
1261
|
+
continue;
|
|
1262
|
+
}
|
|
1263
|
+
// One object per door for the place's life, refreshed in place: a deck that
|
|
1264
|
+
// kept the list from `ready()` sees ghost's board once it is read.
|
|
1265
|
+
const key = `${actor.id}|${props.id}`;
|
|
1266
|
+
const door = h.doorObjects.get(key) ?? {};
|
|
1267
|
+
h.doorObjects.set(key, door);
|
|
1268
|
+
Object.assign(door, {
|
|
1269
|
+
id: props.id,
|
|
1270
|
+
to: props.to,
|
|
1271
|
+
actor: actor.id,
|
|
1272
|
+
t: actor.components.Transform,
|
|
1273
|
+
props,
|
|
1274
|
+
title: dest.title,
|
|
1275
|
+
minPlayers: dest.minPlayers,
|
|
1276
|
+
maxPlayers: dest.maxPlayers,
|
|
1277
|
+
lateJoin: dest.lateJoin,
|
|
1278
|
+
// v1.7: the door leads to a classic deck (ghost's board says so).
|
|
1279
|
+
classic: Cabinets.isClassic(h, props.id),
|
|
1280
|
+
});
|
|
1281
|
+
out.push(door);
|
|
1282
|
+
}
|
|
1283
|
+
return out;
|
|
1284
|
+
}
|
|
1285
|
+
|
|
1286
|
+
// A door's `to` naming another deck rather than a place of this one:
|
|
1287
|
+
// `deck:<name>`, a deck the dev serve was told about (castle.json `devDecks`).
|
|
1288
|
+
// Its content arrives as a content pack (docs/content-pack.md); with the door
|
|
1289
|
+
// table, the row names the deck and ghost the release.
|
|
1290
|
+
const OTHER_DECK = /^deck:([A-Za-z0-9_-]{1,40})$/;
|
|
1291
|
+
|
|
1292
|
+
function otherDeck(to) {
|
|
1293
|
+
return typeof to === 'string' ? (OTHER_DECK.exec(to)?.[1] ?? null) : null;
|
|
1294
|
+
}
|
|
1295
|
+
|
|
1296
|
+
// What this place knows of another deck's main place before going: on the dev
|
|
1297
|
+
// serve, the destination's own manifest (its castle.json, handed to this
|
|
1298
|
+
// bundle by the serve), so the door repeats none of it. With the door table,
|
|
1299
|
+
// ghost's board for the door (`readResults`): the destination's title and the
|
|
1300
|
+
// door row's group, so production doors repeat none of it either. Failing
|
|
1301
|
+
// both (before the first read, or a field the row leaves unsaid), the door's
|
|
1302
|
+
// props.
|
|
1303
|
+
function deckDoorPlace(h, props, deck) {
|
|
1304
|
+
const json = h.devDecks && Object.hasOwn(h.devDecks, deck) ? h.devDecks[deck] : null;
|
|
1305
|
+
const main = json ? Manifest.readManifest(json, () => true).manifest?.places[Manifest.MAIN_PLACE] : null;
|
|
1306
|
+
if (main) {
|
|
1307
|
+
return { title: main.title, minPlayers: main.minPlayers, maxPlayers: main.maxPlayers, lateJoin: main.lateJoin, scene: null };
|
|
1308
|
+
}
|
|
1309
|
+
const board = typeof props.id === 'string' ? (h.boards.get(props.id) ?? null) : null;
|
|
1310
|
+
const int = (v, d) => (Number.isInteger(v) && v >= 1 && v <= 24 ? v : d);
|
|
1311
|
+
const maxPlayers = int(board?.maxPlayers, int(props.maxPlayers, 8));
|
|
1312
|
+
// A cabinet's board is titled with its classic deck, which may have no name.
|
|
1313
|
+
const named = board?.classic ? Cabinets.deckTitle(board.title) : board?.title;
|
|
1314
|
+
const title = typeof named === 'string' && named ? named : props.title;
|
|
1315
|
+
return {
|
|
1316
|
+
title: typeof title === 'string' && title ? title.slice(0, 40) : deck,
|
|
1317
|
+
minPlayers: Math.min(int(board?.minPlayers, int(props.minPlayers, 1)), maxPlayers),
|
|
1318
|
+
maxPlayers,
|
|
1319
|
+
lateJoin: typeof board?.lateJoin === 'boolean' ? board.lateJoin : props.lateJoin === true,
|
|
1320
|
+
scene: null,
|
|
1321
|
+
};
|
|
1322
|
+
}
|
|
1323
|
+
|
|
1324
|
+
// Send players through one of this place's doors (contract §4), into a new
|
|
1325
|
+
// instance or, with `instance`, one this place sent players to before (a late
|
|
1326
|
+
// join). In dev the destination is a named session whose key names the place;
|
|
1327
|
+
// the door table makes this a host command and the instance ghost's.
|
|
1328
|
+
function go(h, door, playerIds, { instance } = {}) {
|
|
1329
|
+
const going = (playerIds ?? []).filter((playerId) => !isShut(h, playerId, door));
|
|
1330
|
+
if (going.length < (playerIds ?? []).length) {
|
|
1331
|
+
if (!going.length) {
|
|
1332
|
+
return null;
|
|
1333
|
+
}
|
|
1334
|
+
playerIds = going;
|
|
1335
|
+
}
|
|
1336
|
+
const hub = platformHub(h);
|
|
1337
|
+
if (Cabinets.isClassic(h, door)) {
|
|
1338
|
+
return goClassic(h, hub, door, playerIds);
|
|
1339
|
+
}
|
|
1340
|
+
if (hub && isLayerDoor(h, door)) {
|
|
1341
|
+
return goLayer(h, door, playerIds);
|
|
1342
|
+
}
|
|
1343
|
+
if (hub) {
|
|
1344
|
+
return firePad(h, hub, door, playerIds);
|
|
1345
|
+
}
|
|
1346
|
+
// Only the dev serve can place a session at a place from its key. Anywhere
|
|
1347
|
+
// else the destination's server would build the main place while the
|
|
1348
|
+
// client loaded another, so a door stays shut until the host places
|
|
1349
|
+
// sessions through the door table.
|
|
1350
|
+
const sessionId = String(h.session?.sessionId ?? '');
|
|
1351
|
+
if (!isDevSession(h)) {
|
|
1352
|
+
console.warn(`[hub] door ${door} stays shut: doors need the door table outside castle-web serve`);
|
|
1353
|
+
return null;
|
|
1354
|
+
}
|
|
1355
|
+
const found = doors(h).find((d) => d.id === door);
|
|
1356
|
+
if (!found) {
|
|
1357
|
+
console.warn(`[hub] no door "${door}" to a place this deck has`);
|
|
1358
|
+
return null;
|
|
1359
|
+
}
|
|
1360
|
+
// Another deck's door goes to its main place, in a session running that
|
|
1361
|
+
// deck's server; the client mounts its content pack. A place of this deck
|
|
1362
|
+
// stays in this deck's namespace: on the serve, a deck reached through a
|
|
1363
|
+
// door runs under `deck:<name>/`, and a key without it would start the
|
|
1364
|
+
// town's server.
|
|
1365
|
+
const deck = otherDeck(found.to);
|
|
1366
|
+
const to = deck ? 'main' : found.to;
|
|
1367
|
+
const here = /^dev:(deck:[A-Za-z0-9_-]{1,40}\/)/.exec(sessionId)?.[1] ?? '';
|
|
1368
|
+
const prefix = deck ? `deck:${deck}/` : here;
|
|
1369
|
+
const key =
|
|
1370
|
+
typeof instance === 'string' && instance.startsWith(`${prefix}hub.${to}.`) ? instance : prefix + Place.devKey(to);
|
|
1371
|
+
tellWent(h, playerIds, door);
|
|
1372
|
+
for (const playerId of playerIds) {
|
|
1373
|
+
h.session?.send(playerId, {
|
|
1374
|
+
k: 'hub-go',
|
|
1375
|
+
door,
|
|
1376
|
+
key,
|
|
1377
|
+
place: to,
|
|
1378
|
+
deck,
|
|
1379
|
+
scene: deck ? null : h.manifest.places[to].scene,
|
|
1380
|
+
title: found.title,
|
|
1381
|
+
party: playerIds.length,
|
|
1382
|
+
});
|
|
1383
|
+
}
|
|
1384
|
+
console.log(`[hub] door ${door}: ${playerIds.length} to ${key}`);
|
|
1385
|
+
return key;
|
|
1386
|
+
}
|
|
1387
|
+
|
|
1388
|
+
// The dev serve's sessions: `dev`, or a named key under `dev:`.
|
|
1389
|
+
function isDevSession(h) {
|
|
1390
|
+
const sessionId = String(h.session?.sessionId ?? '');
|
|
1391
|
+
return sessionId === 'dev' || sessionId.startsWith('dev:');
|
|
1392
|
+
}
|
|
1393
|
+
|
|
1394
|
+
// A door the place marks `"kind": "layer"` in its `HubDoor` (v1.11) leads to a
|
|
1395
|
+
// place ghost puts on public layers, like a town, rather than in an instance
|
|
1396
|
+
// per pad: a village, a district, another town. Ghost holds no pad for it.
|
|
1397
|
+
const LAYER = 'layer';
|
|
1398
|
+
|
|
1399
|
+
function isLayerDoor(h, door) {
|
|
1400
|
+
return doors(h).find((d) => d.id === door)?.props?.kind === LAYER;
|
|
1401
|
+
}
|
|
1402
|
+
|
|
1403
|
+
// `hub.go` on a layer door (v1.11): no pad fires. Each signed-in player named
|
|
1404
|
+
// is told to go; their client asks the host (`Hub.go`), and ghost steers them
|
|
1405
|
+
// to a layer of the destination with their party and friends. Returns the
|
|
1406
|
+
// door id, or null when nobody went.
|
|
1407
|
+
function goLayer(h, door, playerIds) {
|
|
1408
|
+
const found = doors(h).find((d) => d.id === door);
|
|
1409
|
+
const going = [...membersOf(h, playerIds).values()];
|
|
1410
|
+
if (!going.length) {
|
|
1411
|
+
console.warn(`[hub] door ${door}: only signed-in players go through a door`);
|
|
1412
|
+
return null;
|
|
1413
|
+
}
|
|
1414
|
+
for (const playerId of going) {
|
|
1415
|
+
h.session?.send(playerId, {
|
|
1416
|
+
k: 'hub-go',
|
|
1417
|
+
door,
|
|
1418
|
+
key: null,
|
|
1419
|
+
place: null,
|
|
1420
|
+
scene: null,
|
|
1421
|
+
title: found.title,
|
|
1422
|
+
party: going.length,
|
|
1423
|
+
});
|
|
1424
|
+
}
|
|
1425
|
+
console.log(`[hub] door ${door}: ${going.length} to a layer`);
|
|
1426
|
+
return door;
|
|
1427
|
+
}
|
|
1428
|
+
|
|
1429
|
+
// `hub.go` on a door to a classic deck (a cabinet, v1.7): no pad fires and no
|
|
1430
|
+
// session is placed -- ghost would refuse a pad on such a door. Each player is
|
|
1431
|
+
// told to go, alone; their client asks the host, which plays the classic deck
|
|
1432
|
+
// itself, and waits under the cover until Back. Where the host runs the hub
|
|
1433
|
+
// only signed-in players go, as through any door. On the dev serve (the
|
|
1434
|
+
// fixture) the client has no host to ask and is told so (`dev`). Returns the
|
|
1435
|
+
// door id, or null when nobody went.
|
|
1436
|
+
function goClassic(h, hub, door, playerIds) {
|
|
1437
|
+
const found = doors(h).find((d) => d.id === door);
|
|
1438
|
+
if (!found) {
|
|
1439
|
+
console.warn(`[hub] no door "${door}" in this place`);
|
|
1440
|
+
return null;
|
|
1441
|
+
}
|
|
1442
|
+
const going = hub ? [...membersOf(h, playerIds).values()] : (playerIds ?? []);
|
|
1443
|
+
if (!going.length) {
|
|
1444
|
+
console.warn(`[hub] door ${door}: only signed-in players go through a door`);
|
|
1445
|
+
return null;
|
|
1446
|
+
}
|
|
1447
|
+
for (const playerId of going) {
|
|
1448
|
+
h.session?.send(playerId, {
|
|
1449
|
+
k: 'hub-go',
|
|
1450
|
+
door,
|
|
1451
|
+
classic: true,
|
|
1452
|
+
key: null,
|
|
1453
|
+
place: null,
|
|
1454
|
+
scene: null,
|
|
1455
|
+
title: found.title,
|
|
1456
|
+
party: 1,
|
|
1457
|
+
...(hub ? {} : { dev: true }),
|
|
1458
|
+
});
|
|
1459
|
+
}
|
|
1460
|
+
console.log(`[hub] door ${door}: ${going.length} to a classic deck`);
|
|
1461
|
+
return door;
|
|
1462
|
+
}
|
|
1463
|
+
|
|
1464
|
+
// Signed-in players' user ids, which is all ghost holds a pad by:
|
|
1465
|
+
// userId -> playerId.
|
|
1466
|
+
function membersOf(h, playerIds) {
|
|
1467
|
+
const out = new Map();
|
|
1468
|
+
for (const playerId of playerIds ?? []) {
|
|
1469
|
+
const p = h.session?.players.find((q) => q.playerId === playerId);
|
|
1470
|
+
if (p && !p.isAnonymous && USER_ID.test(String(p.userId))) {
|
|
1471
|
+
out.set(String(p.userId), playerId);
|
|
1472
|
+
}
|
|
1473
|
+
}
|
|
1474
|
+
return out;
|
|
1475
|
+
}
|
|
1476
|
+
|
|
1477
|
+
function newPadId(door) {
|
|
1478
|
+
return `${door.slice(0, 40)}.${Math.random().toString(36).slice(2, 10)}`;
|
|
1479
|
+
}
|
|
1480
|
+
|
|
1481
|
+
// `hub.pad(door, playerIds)`, called as often as the deck likes (each step):
|
|
1482
|
+
// who stands on a pad door's pad, told to ghost when it changes and refreshed
|
|
1483
|
+
// before ghost forgets it. Nothing on the dev serve.
|
|
1484
|
+
function gather(h, door, playerIds) {
|
|
1485
|
+
if (typeof door !== 'string') {
|
|
1486
|
+
return;
|
|
1487
|
+
}
|
|
1488
|
+
const on = new Set(playerIds ?? []);
|
|
1489
|
+
h.padDoors.set(door, on);
|
|
1490
|
+
for (const [playerId, entry] of h.shut) {
|
|
1491
|
+
if (entry.door === door && !on.has(playerId)) {
|
|
1492
|
+
// Stepped off: the door may be tried again.
|
|
1493
|
+
unshut(h, playerId);
|
|
1494
|
+
}
|
|
1495
|
+
}
|
|
1496
|
+
const hub = platformHub(h);
|
|
1497
|
+
// Ghost holds no pad for a cabinet (v1.7): it is not a pad door.
|
|
1498
|
+
if (!hub || Cabinets.isClassic(h, door) || Cabinets.cabinetDoorIds(h).has(door) || isLayerDoor(h, door)) {
|
|
1499
|
+
return;
|
|
1500
|
+
}
|
|
1501
|
+
const users = membersOf(h, [...on].filter((playerId) => !isShut(h, playerId, door)));
|
|
1502
|
+
const members = [...users.keys()].sort();
|
|
1503
|
+
let pad = h.pads.get(door);
|
|
1504
|
+
if (pad?.firing) {
|
|
1505
|
+
// Until ghost answers the fire, this pad's group is ghost's to fix.
|
|
1506
|
+
return;
|
|
1507
|
+
}
|
|
1508
|
+
if (!pad) {
|
|
1509
|
+
if (!members.length) {
|
|
1510
|
+
return;
|
|
1511
|
+
}
|
|
1512
|
+
pad = { padId: newPadId(door), json: null, at: 0, firing: false, fireSent: false };
|
|
1513
|
+
h.pads.set(door, pad);
|
|
1514
|
+
}
|
|
1515
|
+
const json = JSON.stringify(members);
|
|
1516
|
+
const now = Date.now();
|
|
1517
|
+
if (json === pad.json && now - pad.at < PAD_REFRESH_MS) {
|
|
1518
|
+
return;
|
|
1519
|
+
}
|
|
1520
|
+
pad.json = json;
|
|
1521
|
+
pad.at = now;
|
|
1522
|
+
if (!members.length && !pad.fireSent) {
|
|
1523
|
+
// An empty pad is left for ghost to forget; the next gathering is new. One
|
|
1524
|
+
// whose fire went unanswered is kept: ghost may have fired it, and only
|
|
1525
|
+
// its own id can say so without firing a second pad.
|
|
1526
|
+
h.pads.delete(door);
|
|
1527
|
+
}
|
|
1528
|
+
hub
|
|
1529
|
+
.updatePad({ doorId: door, padId: pad.padId, memberUserIds: members })
|
|
1530
|
+
.then((answer) => {
|
|
1531
|
+
// Ghost fired this pad on a fire whose answer was lost.
|
|
1532
|
+
if (answer?.status === 'fired') {
|
|
1533
|
+
padFired(h, door, pad, users, answer.members);
|
|
1534
|
+
}
|
|
1535
|
+
})
|
|
1536
|
+
.catch((err) => refused(h, 'pad', err));
|
|
1537
|
+
}
|
|
1538
|
+
|
|
1539
|
+
// The client's word that a door this place sent it through stayed shut (its
|
|
1540
|
+
// trip failed and the player is still here): that player is left out of the
|
|
1541
|
+
// door's pad and of `hub.go` until they step off the pad, so a pad they stand
|
|
1542
|
+
// on does not fire the shut door again every countdown. The door's label says
|
|
1543
|
+
// so, to them alone.
|
|
1544
|
+
function doorShut(h, player, data) {
|
|
1545
|
+
const playerId = player?.playerId;
|
|
1546
|
+
const found = doors(h).find((d) => d.id === data?.door);
|
|
1547
|
+
if (!playerId || !found) {
|
|
1548
|
+
return;
|
|
1549
|
+
}
|
|
1550
|
+
unshut(h, playerId);
|
|
1551
|
+
if (h.padDoors.has(found.id) && !h.padDoors.get(found.id).has(playerId)) {
|
|
1552
|
+
// Already stepped off while the trip was failing.
|
|
1553
|
+
return;
|
|
1554
|
+
}
|
|
1555
|
+
h.shut.set(playerId, { door: found.id, at: Date.now(), actor: found.actor });
|
|
1556
|
+
// A sign: the player reads it from the pad, a pad's length from the door.
|
|
1557
|
+
setLabel(h, found.actor, 'Door shut — step off to retry', { to: [playerId], style: 'sign' }, false);
|
|
1558
|
+
}
|
|
1559
|
+
|
|
1560
|
+
function isShut(h, playerId, door) {
|
|
1561
|
+
const entry = h.shut.get(playerId);
|
|
1562
|
+
if (!entry || entry.door !== door) {
|
|
1563
|
+
return false;
|
|
1564
|
+
}
|
|
1565
|
+
if (h.padDoors.has(door) || Date.now() - entry.at < SHUT_HOLD_MS) {
|
|
1566
|
+
return true;
|
|
1567
|
+
}
|
|
1568
|
+
unshut(h, playerId);
|
|
1569
|
+
return false;
|
|
1570
|
+
}
|
|
1571
|
+
|
|
1572
|
+
function unshut(h, playerId) {
|
|
1573
|
+
const entry = h.shut.get(playerId);
|
|
1574
|
+
if (entry) {
|
|
1575
|
+
h.shut.delete(playerId);
|
|
1576
|
+
setLabel(h, entry.actor, null, { to: [playerId] }, false);
|
|
1577
|
+
}
|
|
1578
|
+
}
|
|
1579
|
+
|
|
1580
|
+
// `hub.go` where the host has hub ops: the pad door fires and ghost places the
|
|
1581
|
+
// group (a late join included, by the door's own rule); each member is told to
|
|
1582
|
+
// go through. Returns the pad's id.
|
|
1583
|
+
function firePad(h, hub, door, playerIds) {
|
|
1584
|
+
if (!doors(h).some((d) => d.id === door)) {
|
|
1585
|
+
console.warn(`[hub] no door "${door}" in this place`);
|
|
1586
|
+
return null;
|
|
1587
|
+
}
|
|
1588
|
+
const members = membersOf(h, playerIds);
|
|
1589
|
+
if (!members.size) {
|
|
1590
|
+
console.warn(`[hub] door ${door}: only signed-in players go through a door`);
|
|
1591
|
+
return null;
|
|
1592
|
+
}
|
|
1593
|
+
// The pad keeps its id until ghost answers the fire, so a retried go (or a
|
|
1594
|
+
// gathering update) after a lost answer names the same pad, which ghost
|
|
1595
|
+
// answers with the group it already fixed rather than firing a second one.
|
|
1596
|
+
const pad = h.pads.get(door) ?? { padId: newPadId(door), json: null, at: 0 };
|
|
1597
|
+
pad.firing = true;
|
|
1598
|
+
pad.fireSent = true;
|
|
1599
|
+
h.pads.set(door, pad);
|
|
1600
|
+
hub
|
|
1601
|
+
.updatePad({ doorId: door, padId: pad.padId, memberUserIds: [...members.keys()], fire: true })
|
|
1602
|
+
.then((answer) => {
|
|
1603
|
+
if (answer?.status === 'fired') {
|
|
1604
|
+
padFired(h, door, pad, members, answer.members);
|
|
1605
|
+
}
|
|
1606
|
+
})
|
|
1607
|
+
.catch((err) => refused(h, 'go', err))
|
|
1608
|
+
.finally(() => {
|
|
1609
|
+
pad.firing = false;
|
|
1610
|
+
});
|
|
1611
|
+
return pad.padId;
|
|
1612
|
+
}
|
|
1613
|
+
|
|
1614
|
+
// Ghost fixed a pad's group: each member here is told to go through, and the
|
|
1615
|
+
// next gathering on this door is a new pad. Once per pad.
|
|
1616
|
+
function padFired(h, door, pad, users, fired) {
|
|
1617
|
+
if (pad.done) {
|
|
1618
|
+
return;
|
|
1619
|
+
}
|
|
1620
|
+
pad.done = true;
|
|
1621
|
+
if (h.pads.get(door) === pad) {
|
|
1622
|
+
h.pads.delete(door);
|
|
1623
|
+
}
|
|
1624
|
+
const found = doors(h).find((d) => d.id === door);
|
|
1625
|
+
const group = Array.isArray(fired) ? fired : [];
|
|
1626
|
+
// The group is ghost's; anyone of it still here goes, on the pad now or not.
|
|
1627
|
+
const here = membersOf(h, h.session?.players.map((p) => p.playerId));
|
|
1628
|
+
tellWent(h, group.map((userId) => users.get(userId) ?? here.get(userId)).filter(Boolean), door);
|
|
1629
|
+
for (const userId of group) {
|
|
1630
|
+
const playerId = users.get(userId) ?? here.get(userId);
|
|
1631
|
+
if (playerId) {
|
|
1632
|
+
h.session?.send(playerId, {
|
|
1633
|
+
k: 'hub-go',
|
|
1634
|
+
door,
|
|
1635
|
+
pad: pad.padId,
|
|
1636
|
+
key: null,
|
|
1637
|
+
place: found?.to ?? null,
|
|
1638
|
+
scene: null,
|
|
1639
|
+
title: found?.title ?? null,
|
|
1640
|
+
party: group.length,
|
|
1641
|
+
});
|
|
1642
|
+
}
|
|
1643
|
+
}
|
|
1644
|
+
console.log(`[hub] door ${door}: pad ${pad.padId} fired with ${group.length}`);
|
|
1645
|
+
}
|
|
1646
|
+
|
|
1647
|
+
// Ghost's word on who the players are: the result each brings back from the
|
|
1648
|
+
// game they came from (for the arrival, the innkeeper and the ribbon) and the
|
|
1649
|
+
// platform avatar (§11); and the boards of this place's doors.
|
|
1650
|
+
function readResults(h) {
|
|
1651
|
+
const hub = platformHub(h);
|
|
1652
|
+
if (!hub) {
|
|
1653
|
+
return;
|
|
1654
|
+
}
|
|
1655
|
+
History.refresh(h, hub, membersOf, refused);
|
|
1656
|
+
const users = new Map();
|
|
1657
|
+
for (const [playerId, entry] of h.players) {
|
|
1658
|
+
if (entry.read !== 'want') {
|
|
1659
|
+
continue;
|
|
1660
|
+
}
|
|
1661
|
+
const userId = [...membersOf(h, [playerId]).keys()][0];
|
|
1662
|
+
if (!userId) {
|
|
1663
|
+
entry.read = 'done';
|
|
1664
|
+
continue;
|
|
1665
|
+
}
|
|
1666
|
+
entry.read = 'reading';
|
|
1667
|
+
users.set(userId, { playerId, avatarRev: entry.avatarRev, wearRev: h.wearRev.get(playerId) ?? 0, startedAt: Date.now() });
|
|
1668
|
+
}
|
|
1669
|
+
if (users.size) {
|
|
1670
|
+
hub
|
|
1671
|
+
.readResults({ userIds: [...users.keys()] })
|
|
1672
|
+
.then((read) => {
|
|
1673
|
+
for (const [userId, asked] of users) {
|
|
1674
|
+
applyRead(h, asked, read.players?.[userId]);
|
|
1675
|
+
}
|
|
1676
|
+
})
|
|
1677
|
+
.catch((err) => refused(h, 'results', err))
|
|
1678
|
+
.finally(() => {
|
|
1679
|
+
for (const { playerId } of users.values()) {
|
|
1680
|
+
const entry = h.players.get(playerId);
|
|
1681
|
+
if (entry?.read === 'reading') {
|
|
1682
|
+
entry.read = 'done';
|
|
1683
|
+
}
|
|
1684
|
+
}
|
|
1685
|
+
});
|
|
1686
|
+
}
|
|
1687
|
+
const now = Date.now();
|
|
1688
|
+
if (now - h.boardsAt < BOARD_EVERY_MS) {
|
|
1689
|
+
return;
|
|
1690
|
+
}
|
|
1691
|
+
// A cabinet's board carries its pictures (v1.7), and a read answers in one
|
|
1692
|
+
// frame of the host's channel: cabinets are read apart from the doors, a
|
|
1693
|
+
// few at a time. A place with none makes the one read it always did.
|
|
1694
|
+
const cabinets = Cabinets.cabinetDoorIds(h);
|
|
1695
|
+
const all = doors(h).map((d) => d.id);
|
|
1696
|
+
const doorIds = all.filter((id) => !cabinets.has(id)).slice(0, MAX_BOARD_DOORS);
|
|
1697
|
+
const batches = doorIds.length ? [doorIds] : [];
|
|
1698
|
+
const cabinetIds = all.filter((id) => cabinets.has(id)).slice(0, MAX_BOARD_DOORS);
|
|
1699
|
+
for (let i = 0; i < cabinetIds.length; i += CABINET_BOARDS_PER_READ) {
|
|
1700
|
+
batches.push(cabinetIds.slice(i, i + CABINET_BOARDS_PER_READ));
|
|
1701
|
+
}
|
|
1702
|
+
if (batches.length) {
|
|
1703
|
+
h.boardsAt = now;
|
|
1704
|
+
Promise.all(batches.map((ids) => hub.readResults({ doorIds: ids })))
|
|
1705
|
+
.then((reads) => {
|
|
1706
|
+
h.boards = new Map(reads.flatMap((read) => Object.entries(read.boards ?? {})));
|
|
1707
|
+
// Doors to other decks take their title and group from the boards,
|
|
1708
|
+
// and a cabinet its card.
|
|
1709
|
+
Cabinets.refresh(h, doors(h));
|
|
1710
|
+
})
|
|
1711
|
+
.catch((err) => refused(h, 'boards', err));
|
|
1712
|
+
}
|
|
1713
|
+
}
|
|
1714
|
+
|
|
1715
|
+
function applyRead(h, { playerId, avatarRev, wearRev, startedAt }, read) {
|
|
1716
|
+
const entry = h.players.get(playerId);
|
|
1717
|
+
if (!entry || !read) {
|
|
1718
|
+
return;
|
|
1719
|
+
}
|
|
1720
|
+
// Their results and records, for `hub.results` (v1.5).
|
|
1721
|
+
History.take(h, playerId, read, startedAt);
|
|
1722
|
+
// The wearable likewise, unless the game put one on since (v1.4).
|
|
1723
|
+
Wear.applyRead(h, playerId, wearRev, read);
|
|
1724
|
+
// The platform avatar, unless the client chose another since the read began.
|
|
1725
|
+
if (Number.isInteger(read.avatar) && read.avatar >= 0 && read.avatar < AVATARS && entry.avatarRev === avatarRev) {
|
|
1726
|
+
entry.avatar = read.avatar;
|
|
1727
|
+
}
|
|
1728
|
+
const row = entry.fromDeckId
|
|
1729
|
+
? (read.recent ?? []).find((r) => r.deckId === entry.fromDeckId && Date.now() - Date.parse(r.at) < RESULT_FRESH_MS)
|
|
1730
|
+
: null;
|
|
1731
|
+
if (!row || !outcomeOrNone(row.outcome)) {
|
|
1732
|
+
return;
|
|
1733
|
+
}
|
|
1734
|
+
const result = {
|
|
1735
|
+
title: String(row.title ?? '').slice(0, 40),
|
|
1736
|
+
outcome: row.outcome ?? null,
|
|
1737
|
+
score: typeof row.score === 'number' && Number.isFinite(row.score) ? row.score : null,
|
|
1738
|
+
unit: ['ms', 's', 'points'].includes(row.unit) ? row.unit : null,
|
|
1739
|
+
better: row.better === 'higher' || row.better === 'lower' ? row.better : null,
|
|
1740
|
+
verified: true,
|
|
1741
|
+
};
|
|
1742
|
+
entry.carry = { result };
|
|
1743
|
+
h.session?.send(playerId, { k: 'hub-result', result });
|
|
1744
|
+
// A read slower than the arrival window: the player arrived without it.
|
|
1745
|
+
if (entry.arrived) {
|
|
1746
|
+
h.hooks.carry?.(h.ctx, playerId, entry.carry);
|
|
1747
|
+
}
|
|
1748
|
+
}
|
|
1749
|
+
|
|
1750
|
+
// A hub op the host or ghost refused, logged once per kind.
|
|
1751
|
+
function refused(h, op, err) {
|
|
1752
|
+
const code = err?.code ?? 'error';
|
|
1753
|
+
if (!h.refusals.has(`${op}:${code}`)) {
|
|
1754
|
+
h.refusals.add(`${op}:${code}`);
|
|
1755
|
+
console.warn(`[hub] ${op} refused (${code}): ${err?.message ?? err}`);
|
|
1756
|
+
}
|
|
1757
|
+
}
|
|
1758
|
+
|
|
1759
|
+
// Per-player HUD overrides go to their player alone, when they change. Where
|
|
1760
|
+
// a block hides players, each player is also sent the shared HUD as they may
|
|
1761
|
+
// read it, when that names someone the shared one leaves out.
|
|
1762
|
+
function sendHudOverrides(h) {
|
|
1763
|
+
const hidden = blockedPairs(h);
|
|
1764
|
+
const all = hidden ? namesOf(h, hidden.keys()) : null;
|
|
1765
|
+
for (const playerId of h.sim.players.keys()) {
|
|
1766
|
+
let spec = h.hudMe.get(playerId) ?? null;
|
|
1767
|
+
if (hidden) {
|
|
1768
|
+
const names = namesOf(h, hidden.get(playerId) ?? []);
|
|
1769
|
+
if (spec) {
|
|
1770
|
+
spec = Blocks.redactHud(spec, names);
|
|
1771
|
+
} else {
|
|
1772
|
+
const mine = Blocks.redactHud(h.hud, names);
|
|
1773
|
+
spec = JSON.stringify(mine) === JSON.stringify(Blocks.redactHud(h.hud, all)) ? null : mine;
|
|
1774
|
+
}
|
|
1775
|
+
}
|
|
1776
|
+
const json = JSON.stringify(spec);
|
|
1777
|
+
if ((h.sentHudMe.get(playerId) ?? 'null') !== json) {
|
|
1778
|
+
h.sentHudMe.set(playerId, json);
|
|
1779
|
+
h.session?.send(playerId, { k: 'hub-hud', hud: spec });
|
|
1780
|
+
}
|
|
1781
|
+
}
|
|
1782
|
+
}
|
|
1783
|
+
|
|
1784
|
+
// Per-player labels go to their player alone, when they change. Where a block
|
|
1785
|
+
// hides players, each player is also sent the shared labels as they may see
|
|
1786
|
+
// them where those differ from what the shared block carries for everyone.
|
|
1787
|
+
function sendLabelOverrides(h) {
|
|
1788
|
+
const now = h.sim.timeMs;
|
|
1789
|
+
const hidden = blockedPairs(h);
|
|
1790
|
+
const shared = hidden ? liveLabels(h) : null;
|
|
1791
|
+
const all = hidden ? labelsForAll(h) : null;
|
|
1792
|
+
for (const playerId of h.sim.players.keys()) {
|
|
1793
|
+
const mine = h.labelsMe.get(playerId);
|
|
1794
|
+
const hiddenIds = hidden?.get(playerId) ?? null;
|
|
1795
|
+
const names = hidden ? namesOf(h, hiddenIds ?? []) : null;
|
|
1796
|
+
const out = {};
|
|
1797
|
+
for (const [id, label] of shared ?? []) {
|
|
1798
|
+
const view = Blocks.labelFor(label, wireLabel(label), hiddenIds, names);
|
|
1799
|
+
if (view && JSON.stringify(view) !== JSON.stringify(all[id] ?? null)) {
|
|
1800
|
+
out[id] = view;
|
|
1801
|
+
}
|
|
1802
|
+
}
|
|
1803
|
+
for (const [id, label] of mine ?? []) {
|
|
1804
|
+
if (label.until !== null && now >= label.until) {
|
|
1805
|
+
mine.delete(id);
|
|
1806
|
+
continue;
|
|
1807
|
+
}
|
|
1808
|
+
const view = hidden ? Blocks.labelFor(label, wireLabel(label), hiddenIds, names) : wireLabel(label);
|
|
1809
|
+
if (view) {
|
|
1810
|
+
out[id] = view;
|
|
1811
|
+
} else {
|
|
1812
|
+
delete out[id];
|
|
1813
|
+
}
|
|
1814
|
+
}
|
|
1815
|
+
const json = JSON.stringify(out);
|
|
1816
|
+
if ((h.sentLabelsMe.get(playerId) ?? '{}') !== json) {
|
|
1817
|
+
h.sentLabelsMe.set(playerId, json);
|
|
1818
|
+
h.session?.send(playerId, { k: 'hub-labels', labels: out });
|
|
1819
|
+
}
|
|
1820
|
+
}
|
|
1821
|
+
}
|
|
1822
|
+
|
|
1823
|
+
// The HUD as every player may read it: without the name of anyone a block
|
|
1824
|
+
// hides from someone here. The HUD itself where nobody is hidden.
|
|
1825
|
+
function hudForAll(h) {
|
|
1826
|
+
const hidden = blockedPairs(h);
|
|
1827
|
+
return hidden ? Blocks.redactHud(h.hud, namesOf(h, hidden.keys())) : h.hud;
|
|
1828
|
+
}
|
|
1829
|
+
|
|
1830
|
+
// The shared labels as every player may see them: none about a player a block
|
|
1831
|
+
// hides from someone here, and no such player's name in any.
|
|
1832
|
+
function labelsForAll(h) {
|
|
1833
|
+
const hidden = blockedPairs(h);
|
|
1834
|
+
if (!hidden) {
|
|
1835
|
+
return labelsOf(h);
|
|
1836
|
+
}
|
|
1837
|
+
const names = namesOf(h, hidden.keys());
|
|
1838
|
+
const ids = new Set(hidden.keys());
|
|
1839
|
+
const out = {};
|
|
1840
|
+
for (const [id, label] of liveLabels(h)) {
|
|
1841
|
+
const view = Blocks.labelFor(label, wireLabel(label), ids, names);
|
|
1842
|
+
if (view) {
|
|
1843
|
+
out[id] = view;
|
|
1844
|
+
}
|
|
1845
|
+
}
|
|
1846
|
+
return out;
|
|
1847
|
+
}
|
|
1848
|
+
|
|
1849
|
+
function liveLabels(h) {
|
|
1850
|
+
labelsOf(h);
|
|
1851
|
+
return h.labels;
|
|
1852
|
+
}
|
|
1853
|
+
|
|
1854
|
+
// The usernames of these players, for taking out of free text.
|
|
1855
|
+
function namesOf(h, playerIds) {
|
|
1856
|
+
const out = [];
|
|
1857
|
+
for (const playerId of playerIds) {
|
|
1858
|
+
const name = h.session?.players.find((p) => p.playerId === playerId)?.username;
|
|
1859
|
+
if (name) {
|
|
1860
|
+
out.push(name);
|
|
1861
|
+
}
|
|
1862
|
+
}
|
|
1863
|
+
return out;
|
|
1864
|
+
}
|
|
1865
|
+
|
|
1866
|
+
function cleanHud(spec) {
|
|
1867
|
+
if (!spec || typeof spec !== 'object') {
|
|
1868
|
+
return null;
|
|
1869
|
+
}
|
|
1870
|
+
const out = {};
|
|
1871
|
+
for (const [key, max] of Object.entries(HUD_LIMITS)) {
|
|
1872
|
+
if (spec[key] !== undefined && spec[key] !== null) {
|
|
1873
|
+
out[key] = String(spec[key]).slice(0, max);
|
|
1874
|
+
}
|
|
1875
|
+
}
|
|
1876
|
+
const timer = spec.timer;
|
|
1877
|
+
if (timer && Number.isFinite(timer.since)) {
|
|
1878
|
+
out.timer = { since: timer.since };
|
|
1879
|
+
} else if (timer && Number.isFinite(timer.until)) {
|
|
1880
|
+
out.timer = { until: timer.until };
|
|
1881
|
+
}
|
|
1882
|
+
return Object.keys(out).length ? out : null;
|
|
1883
|
+
}
|
|
1884
|
+
|
|
1885
|
+
function labelsOf(h) {
|
|
1886
|
+
const out = {};
|
|
1887
|
+
const now = h.sim?.timeMs ?? 0;
|
|
1888
|
+
for (const [id, label] of h.labels) {
|
|
1889
|
+
if (label.until !== null && now >= label.until) {
|
|
1890
|
+
h.labels.delete(id);
|
|
1891
|
+
continue;
|
|
1892
|
+
}
|
|
1893
|
+
out[id] = wireLabel(label);
|
|
1894
|
+
}
|
|
1895
|
+
return out;
|
|
1896
|
+
}
|
|
1897
|
+
|
|
1898
|
+
// Every helloed player's avatar, for everyone: all but the players a block
|
|
1899
|
+
// hides from someone here, whose avatars go to each player who may see them
|
|
1900
|
+
// alone (`sendAvatarOverrides`).
|
|
1901
|
+
function avatars(h, hidden) {
|
|
1902
|
+
const out = {};
|
|
1903
|
+
for (const [playerId, entry] of h.players) {
|
|
1904
|
+
if (entry.hello && !hidden?.has(playerId)) {
|
|
1905
|
+
out[playerId] = entry.avatar;
|
|
1906
|
+
}
|
|
1907
|
+
}
|
|
1908
|
+
return out;
|
|
1909
|
+
}
|
|
1910
|
+
|
|
1911
|
+
// Who a block hides from whom (`blocks.js`), asked of the host once a tick
|
|
1912
|
+
// and again when the roster changes.
|
|
1913
|
+
function blockedPairs(h) {
|
|
1914
|
+
const players = h.session?.players ?? [];
|
|
1915
|
+
const key = `${h.sim?.timeMs ?? ''}:${players.map((p) => p.playerId).join(',')}`;
|
|
1916
|
+
if (h.blocked.key !== key) {
|
|
1917
|
+
h.blocked = { key, pairs: Blocks.hiddenPairs(h.session, players.map((p) => p.playerId)) };
|
|
1918
|
+
}
|
|
1919
|
+
return h.blocked.pairs;
|
|
1920
|
+
}
|
|
1921
|
+
|
|
1922
|
+
// The avatars of players in a hidden pair, each to the players who may see
|
|
1923
|
+
// them, when they change. A session with no blocks sends nothing here.
|
|
1924
|
+
function sendAvatarOverrides(h) {
|
|
1925
|
+
const hidden = blockedPairs(h);
|
|
1926
|
+
for (const playerId of h.sim.players.keys()) {
|
|
1927
|
+
const out = {};
|
|
1928
|
+
for (const shrouded of hidden?.keys() ?? []) {
|
|
1929
|
+
const entry = h.players.get(shrouded);
|
|
1930
|
+
if (entry?.hello && !hidden.get(playerId)?.has(shrouded)) {
|
|
1931
|
+
out[shrouded] = entry.avatar;
|
|
1932
|
+
}
|
|
1933
|
+
}
|
|
1934
|
+
const json = JSON.stringify(out);
|
|
1935
|
+
if ((h.sentAvMe.get(playerId) ?? '{}') !== json) {
|
|
1936
|
+
h.sentAvMe.set(playerId, json);
|
|
1937
|
+
h.session?.send(playerId, { k: 'hub-av', hubAv: out });
|
|
1938
|
+
}
|
|
1939
|
+
}
|
|
1940
|
+
}
|
|
1941
|
+
|
|
1942
|
+
// `hub.wear` (v1.4): kept here and, where the host has hub ops, sent to ghost
|
|
1943
|
+
// for a signed-in player.
|
|
1944
|
+
function wear(h, playerId, spec) {
|
|
1945
|
+
return Wear.wear(h, playerId, spec, {
|
|
1946
|
+
platform: platformHub(h),
|
|
1947
|
+
userId: userIdOf(h, playerId),
|
|
1948
|
+
refused: (err) => refused(h, 'wear', err),
|
|
1949
|
+
});
|
|
1950
|
+
}
|
|
1951
|
+
|
|
1952
|
+
// `hubWear` rides the snapshot only once someone has worn something, so a
|
|
1953
|
+
// place nobody wears anything in sends what it always did.
|
|
1954
|
+
function wearingForJoin(h) {
|
|
1955
|
+
const wearing = Wear.wearingOf(h, blockedPairs(h));
|
|
1956
|
+
return Object.keys(wearing).length ? { hubWear: wearing } : {};
|
|
1957
|
+
}
|
|
1958
|
+
|
|
1959
|
+
function latchWearing(h, out) {
|
|
1960
|
+
const wearing = Wear.wearingOf(h, blockedPairs(h));
|
|
1961
|
+
if (Object.keys(wearing).length || h.sent.has('hubWear')) {
|
|
1962
|
+
latch(h, 'hubWear', wearing, out);
|
|
1963
|
+
}
|
|
1964
|
+
}
|
|
1965
|
+
|
|
1966
|
+
function latch(h, field, value, out) {
|
|
1967
|
+
const json = JSON.stringify(value);
|
|
1968
|
+
if (h.sent.get(field) !== json) {
|
|
1969
|
+
h.sent.set(field, json);
|
|
1970
|
+
out[field] = value;
|
|
1971
|
+
}
|
|
1972
|
+
}
|
|
1973
|
+
|
|
1974
|
+
// A scene actor of the place, or a copy the game spawned.
|
|
1975
|
+
function isSceneActor(h, id) {
|
|
1976
|
+
return Actors.isActor(h, id);
|
|
1977
|
+
}
|
|
1978
|
+
|
|
1979
|
+
// A signed-in player's user id (what `session.storage.user` and ghost key a
|
|
1980
|
+
// player by), or null for an anonymous one. On the dev serve its guests'
|
|
1981
|
+
// subjects (`dev-guest-<n>`) count, since their storage works there.
|
|
1982
|
+
const DEV_GUEST = /^dev-guest-\d{1,6}$/;
|
|
1983
|
+
|
|
1984
|
+
function userIdOf(h, playerId) {
|
|
1985
|
+
const p = h.session?.players.find((q) => q.playerId === playerId);
|
|
1986
|
+
if (!p) {
|
|
1987
|
+
return null;
|
|
1988
|
+
}
|
|
1989
|
+
if (!p.isAnonymous && USER_ID.test(String(p.userId))) {
|
|
1990
|
+
return String(p.userId);
|
|
1991
|
+
}
|
|
1992
|
+
const dev = String(h.session?.sessionId ?? '').startsWith('dev');
|
|
1993
|
+
return dev && DEV_GUEST.test(String(p.userId)) ? String(p.userId) : null;
|
|
1994
|
+
}
|
|
1995
|
+
|
|
1996
|
+
function usernameOf(h, playerId) {
|
|
1997
|
+
return h.session?.players.find((p) => p.playerId === playerId)?.username ?? 'player';
|
|
1998
|
+
}
|