castle-web-cli 0.4.232 → 0.4.233
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/agent-failures.d.ts +8 -0
- package/dist/agent-failures.js +19 -2
- package/dist/agent-prompts.d.ts +1 -1
- package/dist/agent-prompts.js +10 -3
- package/dist/agent.d.ts +64 -3
- package/dist/agent.js +170 -81
- package/dist/castle-host/host.d.ts +1 -0
- package/dist/castle-host/host.js +51 -3
- package/dist/castleJson.d.ts +4 -2
- package/dist/castleJson.js +2 -0
- package/dist/check.d.ts +3 -1
- package/dist/check.js +11 -1
- package/dist/devSessionStorage.js +47 -1
- package/dist/importPinMismatch.d.ts +22 -0
- package/dist/importPinMismatch.js +77 -0
- package/dist/importRegression.js +3 -15
- package/dist/imports.d.ts +10 -3
- package/dist/imports.js +88 -20
- package/dist/index.js +5 -4
- package/dist/init.js +1 -1
- package/dist/platformDoc.js +22 -0
- package/dist/routerFences.d.ts +5 -0
- package/dist/routerFences.js +12 -2
- package/dist/save-deck.d.ts +5 -0
- package/dist/save-deck.js +37 -4
- package/dist/serve.js +3 -0
- package/dist/shell/assets/{index-D2tWM896.js → index-BjOxR-lW.js} +4 -4
- package/dist/shell/assets/index-MVLfNYd9.css +1 -0
- package/dist/shell/index.html +2 -2
- package/dist/taskSteer.d.ts +13 -0
- package/dist/taskSteer.js +22 -2
- package/kits/base/castle.json +1 -1
- package/kits/base/package-lock.json +1 -1
- package/kits/base/sdk/README.md +79 -2
- package/kits/base/sdk/avatar.d.ts +6 -2
- package/kits/base/sdk/avatar.js +12 -4
- package/kits/base/sdk/castle.d.ts +1 -1
- package/kits/base/sdk/commands.d.ts +14 -0
- package/kits/base/sdk/hub.d.ts +6 -2
- package/kits/base/sdk/hub.js +13 -0
- package/kits/base/sdk/multiplayer.js +17 -1
- package/kits/base/sdk/save.js +2 -1
- package/kits/base/sdk/server/hub.d.ts +87 -0
- package/kits/base/sdk/server/hub.js +70 -0
- package/kits/base/sdk/server/platformHandle.d.ts +31 -1
- package/kits/base/sdk/server/storage.d.ts +13 -0
- package/kits/base/sdk/server/storage.js +46 -0
- package/kits/base/sdk/server/wrapper.d.ts +36 -5
- package/kits/base/sdk/server/wrapper.js +83 -8
- package/kits/hub-game/CLAUDE.md +329 -11
- package/kits/hub-game/blueprints/avatar-10.scene +28 -0
- package/kits/hub-game/blueprints/avatar-11.scene +28 -0
- package/kits/hub-game/blueprints/avatar-12.scene +28 -0
- package/kits/hub-game/blueprints/avatar-13.scene +28 -0
- package/kits/hub-game/blueprints/avatar-14.scene +28 -0
- package/kits/hub-game/blueprints/avatar-15.scene +28 -0
- package/kits/hub-game/blueprints/avatar-4.scene +28 -0
- package/kits/hub-game/blueprints/avatar-5.scene +28 -0
- package/kits/hub-game/blueprints/avatar-6.scene +28 -0
- package/kits/hub-game/blueprints/avatar-7.scene +28 -0
- package/kits/hub-game/blueprints/avatar-8.scene +28 -0
- package/kits/hub-game/blueprints/avatar-9.scene +28 -0
- package/kits/hub-game/code/client/actorBudget.js +47 -0
- package/kits/hub-game/code/client/attention.js +11 -2
- package/kits/hub-game/code/client/avatar.js +309 -35
- package/kits/hub-game/code/client/avatarCap.js +228 -0
- package/kits/hub-game/code/client/avatarColors.js +70 -0
- package/kits/hub-game/code/client/avatarFaces.js +21 -0
- package/kits/hub-game/code/client/avatarShare.js +251 -0
- package/kits/hub-game/code/client/ballot.js +467 -0
- package/kits/hub-game/code/client/build.js +22 -0
- package/kits/hub-game/code/client/chrome.js +1144 -50
- package/kits/hub-game/code/client/companion.js +716 -0
- package/kits/hub-game/code/client/companionDress.js +99 -0
- package/kits/hub-game/code/client/decor.js +509 -0
- package/kits/hub-game/code/client/earned.js +131 -0
- package/kits/hub-game/code/client/emoteBubble.js +75 -0
- package/kits/hub-game/code/client/grab.js +339 -20
- package/kits/hub-game/code/client/hookLayer.js +299 -13
- package/kits/hub-game/code/client/idle.js +163 -22
- package/kits/hub-game/code/client/interestCap.js +81 -0
- package/kits/hub-game/code/client/keys.js +36 -0
- package/kits/hub-game/code/client/labelsNear.js +205 -0
- package/kits/hub-game/code/client/menuIcons.js +34 -0
- package/kits/hub-game/code/client/pack.js +6 -2
- package/kits/hub-game/code/client/packFormat.js +3 -3
- package/kits/hub-game/code/client/rehello.js +167 -0
- package/kits/hub-game/code/client/shot.js +92 -0
- package/kits/hub-game/code/client/sound.js +63 -8
- package/kits/hub-game/code/client/soundscape.js +1151 -0
- package/kits/hub-game/code/client/soundscapeActions.js +99 -0
- package/kits/hub-game/code/client/soundscapeAmbience.js +453 -0
- package/kits/hub-game/code/client/soundscapeBaked.js +143 -0
- package/kits/hub-game/code/client/soundscapeBusker.js +217 -0
- package/kits/hub-game/code/client/soundscapeCourt.js +200 -0
- package/kits/hub-game/code/client/soundscapeCrowd.js +331 -0
- package/kits/hub-game/code/client/soundscapeCues.js +362 -0
- package/kits/hub-game/code/client/soundscapeMap.js +181 -0
- package/kits/hub-game/code/client/soundscapeMarket.js +176 -0
- package/kits/hub-game/code/client/soundscapeMusic.js +126 -0
- package/kits/hub-game/code/client/soundscapeMusicEngine.js +460 -0
- package/kits/hub-game/code/client/soundscapeSimlish.js +737 -0
- package/kits/hub-game/code/client/soundscapeSongs.js +127 -0
- package/kits/hub-game/code/client/soundscapeSongsRound3.js +63 -0
- package/kits/hub-game/code/client/soundscapeSynthesis.js +168 -0
- package/kits/hub-game/code/client/soundscapeUi.js +211 -0
- package/kits/hub-game/code/client/soundscapeVoice.js +1209 -0
- package/kits/hub-game/code/client/soundscapeWaves.js +206 -0
- package/kits/hub-game/code/client/spin.js +101 -0
- package/kits/hub-game/code/client/state.js +109 -1
- package/kits/hub-game/code/client/stuck.js +259 -0
- package/kits/hub-game/code/client/talk.js +538 -47
- package/kits/hub-game/code/client/tiers.js +516 -27
- package/kits/hub-game/code/client/toastBand.js +37 -0
- package/kits/hub-game/code/client/together.js +32 -0
- package/kits/hub-game/code/client/visibility.js +43 -0
- package/kits/hub-game/code/client/wear.js +461 -170
- package/kits/hub-game/code/client/yours.js +199 -0
- package/kits/hub-game/code/companionSpec.js +45 -0
- package/kits/hub-game/code/privateText.js +7 -0
- package/kits/hub-game/code/server/actions.js +133 -0
- package/kits/hub-game/code/server/actors.js +9 -1
- package/kits/hub-game/code/server/ask.js +155 -0
- package/kits/hub-game/code/server/ballot.js +539 -0
- package/kits/hub-game/code/server/cells.js +100 -0
- package/kits/hub-game/code/server/companion.js +82 -0
- package/kits/hub-game/code/server/cues.js +70 -0
- package/kits/hub-game/code/server/decor.js +269 -0
- package/kits/hub-game/code/server/earned.js +461 -0
- package/kits/hub-game/code/server/friendBests.js +166 -0
- package/kits/hub-game/code/server/friendsAmong.js +232 -0
- package/kits/hub-game/code/server/history.js +5 -0
- package/kits/hub-game/code/server/hub.js +1254 -50
- package/kits/hub-game/code/server/idle.js +30 -11
- package/kits/hub-game/code/server/index.js +2 -2
- package/kits/hub-game/code/server/labels.js +28 -2
- package/kits/hub-game/code/server/manifest.js +378 -7
- package/kits/hub-game/code/server/menu.js +133 -0
- package/kits/hub-game/code/server/npcs.js +830 -80
- package/kits/hub-game/code/server/party.js +2 -2
- package/kits/hub-game/code/server/players.js +22 -0
- package/kits/hub-game/code/server/shot.js +110 -0
- package/kits/hub-game/code/server/standing.js +124 -0
- package/kits/hub-game/code/server/vis.js +113 -0
- package/kits/hub-game/code/server/wear.js +12 -0
- package/kits/hub-game/code/server/yours.js +59 -0
- package/kits/hub-game/code/shared/cues.js +9 -0
- package/kits/hub-game/code/shared/decor.js +41 -0
- package/kits/hub-game/code/shared/shot.js +19 -0
- package/kits/hub-game/code/shared/spin.js +57 -0
- package/kits/hub-game/code/systems/hub.js +845 -95
- package/kits/hub-game/docs/content-pack.md +1 -1
- package/kits/hub-game/docs/contract.md +2244 -61
- package/kits/hub-game/docs/home-pieces.md +64 -0
- package/kits/hub-game/models/avatar-10.pxmodel +28 -0
- package/kits/hub-game/models/avatar-11.pxmodel +28 -0
- package/kits/hub-game/models/avatar-12.pxmodel +27 -0
- package/kits/hub-game/models/avatar-13.pxmodel +24 -0
- package/kits/hub-game/models/avatar-14.pxmodel +29 -0
- package/kits/hub-game/models/avatar-15.pxmodel +24 -0
- package/kits/hub-game/models/avatar-4.pxmodel +29 -0
- package/kits/hub-game/models/avatar-5.pxmodel +27 -0
- package/kits/hub-game/models/avatar-6.pxmodel +26 -0
- package/kits/hub-game/models/avatar-7.pxmodel +25 -0
- package/kits/hub-game/models/avatar-8.pxmodel +24 -0
- package/kits/hub-game/models/avatar-9.pxmodel +30 -0
- package/kits/hub-game/models/earned/badge-round.pxmodel +100 -0
- package/kits/hub-game/models/earned/badge-shield.pxmodel +117 -0
- package/kits/hub-game/models/earned/badge-star.pxmodel +87 -0
- package/kits/hub-game/models/earned/belt-plain.pxmodel +72 -0
- package/kits/hub-game/models/earned/held-ball.pxmodel +43 -0
- package/kits/hub-game/models/earned/held-lantern.pxmodel +85 -0
- package/kits/hub-game/models/earned/held-net.pxmodel +57 -0
- package/kits/hub-game/models/earned/held-trophy.pxmodel +109 -0
- package/kits/hub-game/models/earned/pin-round.pxmodel +62 -0
- package/kits/hub-game/models/earned/sash-plain.pxmodel +66 -0
- package/kits/hub-game/models/earned/square/belt-plain.pxmodel +104 -0
- package/kits/hub-game/models/earned/square/sash-plain.pxmodel +98 -0
- package/kits/hub-game/models/hats/bat-clip.pxmodel +206 -0
- package/kits/hub-game/models/hats/beekeeper.pxmodel +149 -0
- package/kits/hub-game/models/hats/cap.pxmodel +162 -0
- package/kits/hub-game/models/hats/captain.pxmodel +178 -0
- package/kits/hub-game/models/hats/crooked-top.pxmodel +172 -0
- package/kits/hub-game/models/hats/flower-crown.pxmodel +451 -0
- package/kits/hub-game/models/hats/foam-crown.pxmodel +179 -0
- package/kits/hub-game/models/hats/lantern.pxmodel +230 -0
- package/kits/hub-game/models/hats/laurel.pxmodel +533 -0
- package/kits/hub-game/models/hats/lighthouse.pxmodel +96 -0
- package/kits/hub-game/models/hats/sailor.pxmodel +141 -0
- package/kits/hub-game/models/hats/souwester.pxmodel +131 -0
- package/kits/hub-game/models/hats/straw.pxmodel +139 -0
- package/kits/hub-game/models/hats/sunflower.pxmodel +406 -0
- package/kits/hub-game/models/hats/sweatband.pxmodel +112 -0
- package/kits/hub-game/models/hats/witch.pxmodel +147 -0
- package/kits/hub-game/package-lock.json +2 -0
- package/kits/hub-game/package.json +2 -1
- package/kits/hub-game/scripts/check.mjs +78 -11
- package/kits/hub-game/scripts/companion-follower-wire.json +1 -0
- package/kits/hub-game/scripts/fixtures/decor-keyless-draw.json +121 -0
- package/kits/hub-game/scripts/fixtures/decor-keyless.json +293 -0
- package/kits/hub-game/scripts/fixtures/engine-models-stub.mjs +23 -0
- package/kits/hub-game/scripts/fixtures/fake-dom.mjs +107 -0
- package/kits/hub-game/scripts/fixtures/interest-legacy.json +385 -0
- package/kits/hub-game/scripts/fixtures/npc-notes-v1.77.jsonl +4 -0
- package/kits/hub-game/scripts/fixtures/vis-shared-wire.json +1 -0
- package/kits/hub-game/scripts/gen-avatars.mjs +933 -16
- package/kits/hub-game/scripts/gen-earned.mjs +285 -0
- package/kits/hub-game/scripts/gen-hats.mjs +827 -0
- package/kits/hub-game/scripts/grab.before-orbit.mjs +400 -0
- package/kits/hub-game/scripts/soundscape-wave-analysis.mjs +125 -0
- package/kits/hub-game/scripts/test-actions-client.mjs +488 -0
- package/kits/hub-game/scripts/test-actions.mjs +298 -0
- package/kits/hub-game/scripts/test-actor-budget.mjs +44 -0
- package/kits/hub-game/scripts/test-actors.mjs +19 -17
- package/kits/hub-game/scripts/test-ask-client.mjs +358 -0
- package/kits/hub-game/scripts/test-ask.mjs +269 -0
- package/kits/hub-game/scripts/test-attention.mjs +10 -1
- package/kits/hub-game/scripts/test-avatar-cap.mjs +379 -0
- package/kits/hub-game/scripts/test-avatar-colors.mjs +72 -0
- package/kits/hub-game/scripts/test-avatar-models.mjs +181 -0
- package/kits/hub-game/scripts/test-avatar-share.mjs +318 -0
- package/kits/hub-game/scripts/test-avatar-wire.mjs +753 -0
- package/kits/hub-game/scripts/test-avatar.mjs +540 -7
- package/kits/hub-game/scripts/test-ballot.mjs +1001 -0
- package/kits/hub-game/scripts/test-build.mjs +31 -0
- package/kits/hub-game/scripts/test-cells.mjs +71 -0
- package/kits/hub-game/scripts/test-chat.mjs +662 -0
- package/kits/hub-game/scripts/test-classic.mjs +2 -0
- package/kits/hub-game/scripts/test-companion-chat.mjs +198 -0
- package/kits/hub-game/scripts/test-companion-client.mjs +1167 -0
- package/kits/hub-game/scripts/test-companion.mjs +259 -0
- package/kits/hub-game/scripts/test-decor-client.mjs +303 -0
- package/kits/hub-game/scripts/test-decor.mjs +457 -0
- package/kits/hub-game/scripts/test-earned-client.mjs +459 -0
- package/kits/hub-game/scripts/test-earned.mjs +1194 -0
- package/kits/hub-game/scripts/test-emote-bubble.mjs +110 -0
- package/kits/hub-game/scripts/test-friend-bests.mjs +435 -0
- package/kits/hub-game/scripts/test-grab-orbit.mjs +918 -0
- package/kits/hub-game/scripts/test-grab.mjs +7 -0
- package/kits/hub-game/scripts/test-hats.mjs +422 -168
- package/kits/hub-game/scripts/test-hub-leaving.mjs +203 -0
- package/kits/hub-game/scripts/test-hub-ops.mjs +439 -2
- package/kits/hub-game/scripts/test-hub-trip.mjs +716 -2
- package/kits/hub-game/scripts/test-hud-clear.mjs +328 -4
- package/kits/hub-game/scripts/test-idle.mjs +361 -1
- package/kits/hub-game/scripts/test-interest-cap.mjs +36 -0
- package/kits/hub-game/scripts/test-interest-cells.mjs +493 -0
- package/kits/hub-game/scripts/test-interest-client.mjs +118 -0
- package/kits/hub-game/scripts/test-keys.mjs +46 -1
- package/kits/hub-game/scripts/test-labels.mjs +596 -0
- package/kits/hub-game/scripts/test-manifest.mjs +207 -0
- package/kits/hub-game/scripts/test-menu-client.mjs +480 -0
- package/kits/hub-game/scripts/test-menu.mjs +296 -0
- package/kits/hub-game/scripts/test-npcs.mjs +1718 -12
- package/kits/hub-game/scripts/test-object-entry.mjs +76 -0
- package/kits/hub-game/scripts/test-pack.mjs +7 -0
- package/kits/hub-game/scripts/test-picker.mjs +314 -0
- package/kits/hub-game/scripts/test-rehello.mjs +1039 -0
- package/kits/hub-game/scripts/test-relations-among.mjs +222 -0
- package/kits/hub-game/scripts/test-relations-refresh.mjs +188 -0
- package/kits/hub-game/scripts/test-shot.mjs +756 -0
- package/kits/hub-game/scripts/test-soundscape.mjs +5432 -0
- package/kits/hub-game/scripts/test-spin.mjs +314 -0
- package/kits/hub-game/scripts/test-standing.mjs +416 -0
- package/kits/hub-game/scripts/test-stuck.mjs +1903 -0
- package/kits/hub-game/scripts/test-tier-cells.mjs +706 -0
- package/kits/hub-game/scripts/test-tiers-render.mjs +339 -0
- package/kits/hub-game/scripts/test-tiers.mjs +477 -2
- package/kits/hub-game/scripts/test-toast-band.mjs +61 -0
- package/kits/hub-game/scripts/test-try-on.mjs +22 -22
- package/kits/hub-game/scripts/test-vis.mjs +343 -0
- package/kits/hub-game/scripts/test-visibility.mjs +134 -0
- package/kits/hub-game/scripts/test-yours-chrome.mjs +166 -0
- package/kits/hub-game/scripts/test-yours.mjs +372 -0
- package/kits/multiplayer-3d/CLAUDE.md +50 -0
- package/kits/multiplayer-3d/castle.json +5 -5
- package/kits/multiplayer-3d/code/client/avatars.js +7 -0
- package/kits/multiplayer-3d/code/client/gait.js +65 -0
- package/kits/multiplayer-3d/code/client/nameTags.js +59 -4
- package/kits/multiplayer-3d/code/options.js +5 -0
- package/kits/multiplayer-3d/code/server/ownership.js +10 -2
- package/kits/multiplayer-3d/code/server/sceneBodies.js +12 -0
- package/kits/multiplayer-3d/code/server/world.js +7 -0
- package/kits/multiplayer-3d/code/systems/multiplayer.js +74 -4
- package/kits/multiplayer-3d/package.json +2 -1
- package/kits/multiplayer-3d/scripts/fixtures/README.md +11 -0
- package/kits/multiplayer-3d/scripts/fixtures/ownership-legacy.json +2 -0
- package/kits/multiplayer-3d/scripts/ownership-harness.mjs +102 -0
- package/kits/multiplayer-3d/scripts/ownership-loader.mjs +46 -0
- package/kits/multiplayer-3d/scripts/ownership-worker.mjs +15 -0
- package/kits/multiplayer-3d/scripts/record-ownership-session.mjs +98 -0
- package/kits/multiplayer-3d/scripts/test-eligible-claims.mjs +167 -0
- package/kits/multiplayer-3d/scripts/test-own-name-tag.mjs +247 -0
- package/kits/physics-2d/CLAUDE.md +9 -0
- package/kits/physics-2d/castle.json +2 -2
- package/kits/physics-2d/engine/scene.js +253 -1
- package/kits/physics-2d/engine/skyKey.js +129 -0
- package/kits/physics-2d/engine/stillClock.js +106 -0
- package/kits/physics-2d/engine/updateBudget.js +21 -0
- package/kits/physics-2d/scripts/test-skip-unchanged.mjs +326 -1
- package/kits/physics-3d/CLAUDE.md +1169 -30
- package/kits/physics-3d/behaviors/Player.jsx +629 -72
- package/kits/physics-3d/castle.json +3 -3
- package/kits/physics-3d/docs/pxmodel-format.md +8 -0
- package/kits/physics-3d/engine3d/Scene3DPlayer.jsx +69 -13
- package/kits/physics-3d/engine3d/actornode.js +86 -0
- package/kits/physics-3d/engine3d/animationBudget.js +23 -0
- package/kits/physics-3d/engine3d/editorWorld.js +6 -0
- package/kits/physics-3d/engine3d/emptynodes.js +232 -0
- package/kits/physics-3d/engine3d/framecap.js +96 -0
- package/kits/physics-3d/engine3d/gait.js +118 -0
- package/kits/physics-3d/engine3d/kithooks.js +47 -0
- package/kits/physics-3d/engine3d/look.js +507 -48
- package/kits/physics-3d/engine3d/lookbake.js +208 -0
- package/kits/physics-3d/engine3d/materialpresets.js +2 -0
- package/kits/physics-3d/engine3d/motion.js +39 -10
- package/kits/physics-3d/engine3d/nearshadows.js +37 -19
- package/kits/physics-3d/engine3d/optimize.js +9 -5
- package/kits/physics-3d/engine3d/orbitinput.js +487 -9
- package/kits/physics-3d/engine3d/orbitlens.js +48 -0
- package/kits/physics-3d/engine3d/ownmodel.js +213 -0
- package/kits/physics-3d/engine3d/pxmodel.js +42 -11
- package/kits/physics-3d/engine3d/shadowfade.js +882 -0
- package/kits/physics-3d/engine3d/sky.js +547 -0
- package/kits/physics-3d/engine3d/skylife.js +306 -0
- package/kits/physics-3d/engine3d/unchanged.js +11 -0
- package/kits/physics-3d/engine3d/viewrange.js +52 -0
- package/kits/physics-3d/engine3d/world3d.js +796 -69
- package/kits/physics-3d/main.jsx +5 -0
- package/kits/physics-3d/package.json +1 -1
- package/kits/physics-3d/scripts/look-baseline.mjs +155 -0
- package/kits/physics-3d/scripts/test-camera-orbit.mjs +2563 -80
- package/kits/physics-3d/scripts/test-drop-empty-parts.mjs +1 -1
- package/kits/physics-3d/scripts/test-frame-cap-bob.mjs +435 -0
- package/kits/physics-3d/scripts/test-frame-cap.mjs +452 -0
- package/kits/physics-3d/scripts/test-gait.mjs +387 -0
- package/kits/physics-3d/scripts/test-look-bake.mjs +1056 -0
- package/kits/physics-3d/scripts/test-model-parse-cache.mjs +92 -0
- package/kits/physics-3d/scripts/test-near-shadows.mjs +3 -0
- package/kits/physics-3d/scripts/test-orbit-shadow-fade.mjs +1920 -0
- package/kits/physics-3d/scripts/test-orbit-shift-lock.mjs +545 -0
- package/kits/physics-3d/scripts/test-orbit-shot.mjs +583 -0
- package/kits/physics-3d/scripts/test-orbit-view.mjs +230 -0
- package/kits/physics-3d/scripts/test-skip-empty-nodes.mjs +845 -0
- package/kits/physics-3d/scripts/test-sky.mjs +2470 -0
- package/kits/physics-3d/scripts/test-static-actors.mjs +8 -1
- package/kits/physics-3d/scripts/test-static-scenery.mjs +2171 -0
- package/kits/physics-3d/scripts/test-steady-hover.mjs +2 -0
- package/kits/physics-3d/scripts/test-view-range.mjs +260 -0
- package/kits/physics-3d/systems/physics3d.js +383 -6
- package/kits/real-time/castle.json +2 -2
- package/kits/real-time/code/server/gameHooks.js +7 -0
- package/kits/real-time/code/server/session.js +97 -19
- package/package.json +2 -2
- package/dist/shell/assets/index-Cf61cNfT.css +0 -1
|
@@ -0,0 +1,539 @@
|
|
|
1
|
+
// The choice card (contract v1.68, `hub.ballot`): a card of players' faces,
|
|
2
|
+
// the deck's fixed lines and a few options, for a meeting, a vote, a reveal.
|
|
3
|
+
// Only a place whose manifest declares `ballot` has any of it: no state, no
|
|
4
|
+
// message and no work otherwise.
|
|
5
|
+
//
|
|
6
|
+
// No free text: a player's taps reach the server as ids only -- a face (a
|
|
7
|
+
// player id on the card), a line (an id of the deck's manifest lines), an
|
|
8
|
+
// option (an id of the card's options) -- and every word on the card is
|
|
9
|
+
// either the deck's own (its server's title, text, notes and options, and its
|
|
10
|
+
// manifest's lines) or the kit's (usernames). Nothing a player types is ever
|
|
11
|
+
// carried.
|
|
12
|
+
//
|
|
13
|
+
// Two rules every path here obeys (contract §6, "The choice card"):
|
|
14
|
+
//
|
|
15
|
+
// (A) Each player's card has a version `v`. A `hub.ballot` call that changes
|
|
16
|
+
// what that player can tap -- the mode, the faces offered and their
|
|
17
|
+
// `off`, the lines offered, the option ids, `picked` (`actionKey`) --
|
|
18
|
+
// gives their card a new version (an opaque string: this server's own
|
|
19
|
+
// epoch and a counter, so a version from an earlier server for the same
|
|
20
|
+
// layer never equals one of this server's); a call that
|
|
21
|
+
// changes only title, text, until, notes, votes, marks, option words or
|
|
22
|
+
// the feed keeps it. A tap carries the `v` of the card it was made on,
|
|
23
|
+
// and is taken only if that `v` is the version last delivered to the
|
|
24
|
+
// player AND the version of their current card, and its ids are on that
|
|
25
|
+
// card and allowed by it (`allows`). The same version means the same
|
|
26
|
+
// taps allowed, so a refresh of the words never refuses a tap, and a
|
|
27
|
+
// changed or undelivered card never takes an old one.
|
|
28
|
+
//
|
|
29
|
+
// (B) Keys absent means no ballot code path runs at all, client or server:
|
|
30
|
+
// no allocation, no per-frame call, no disposal work, no listener. The
|
|
31
|
+
// only thing a place without `ballot` does is read the key and find it
|
|
32
|
+
// missing: here `h.place.ballot` before a message, `h.ballotState`
|
|
33
|
+
// (made by the first `hub.ballot` call) before a tick's send and a
|
|
34
|
+
// hello's or a leave's cleanup, and `manifest.ballot` before giving the
|
|
35
|
+
// deck `hub.ballot` at all.
|
|
36
|
+
//
|
|
37
|
+
// Ids index only Maps, arrays and objects made with no prototype: an id
|
|
38
|
+
// ("constructor", "__proto__") is never looked up through Object.prototype.
|
|
39
|
+
|
|
40
|
+
import { warnOnce } from './actors.js';
|
|
41
|
+
import { BALLOT_LINE_ID, cardText } from './manifest.js';
|
|
42
|
+
import * as Blocks from './blocks.js';
|
|
43
|
+
|
|
44
|
+
export const MODES = ['talk', 'vote', 'reveal'];
|
|
45
|
+
export const MAX_FACES = 24;
|
|
46
|
+
export const MAX_OPTIONS = 6;
|
|
47
|
+
export const MAX_FEED = 6;
|
|
48
|
+
export const MAX_VOTES = 99;
|
|
49
|
+
const TITLE_MAX = 40;
|
|
50
|
+
const TEXT_MAX = 120;
|
|
51
|
+
const NOTE_MAX = 24;
|
|
52
|
+
const OPTION_MAX = 32;
|
|
53
|
+
const MAX_TARGETS = 64;
|
|
54
|
+
const COLOR = /^#[0-9a-fA-F]{6}$/;
|
|
55
|
+
const SPEC_KEYS = ['title', 'text', 'until', 'mode', 'faces', 'options', 'lines', 'feed', 'picked'];
|
|
56
|
+
const FACE_KEYS = ['playerId', 'note', 'off', 'votes', 'mark'];
|
|
57
|
+
const MESSAGE_KEYS = ['k', 'v', 'face', 'line', 'option'];
|
|
58
|
+
// One player's taps: a burst of three, then one every 1.5 s (as `say.js`).
|
|
59
|
+
const BURST = 3;
|
|
60
|
+
const EVERY_MS = 1500;
|
|
61
|
+
|
|
62
|
+
function refuse(h, key, text) {
|
|
63
|
+
warnOnce(h, `ballot:${key}`, `hub.ballot: ${text}`);
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const isObject = (value) => value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
68
|
+
|
|
69
|
+
// A server's version prefix: its start time and a random part, base 36.
|
|
70
|
+
function newEpoch() {
|
|
71
|
+
return `${Date.now().toString(36)}${Math.random().toString(36).slice(2, 8)}`;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function state(h) {
|
|
75
|
+
// shared: the card for everyone here; me: playerId -> their own card, or
|
|
76
|
+
// a follower copy of the shared card carrying their own v (`of`, always
|
|
77
|
+
// the current `shared`: a player whose own card was cleared keeps or
|
|
78
|
+
// renews their version by the card they had, rule A);
|
|
79
|
+
// sent: playerId -> JSON of the view last delivered; views: playerId ->
|
|
80
|
+
// that view (rule A's first half); rate: playerId -> { tokens, at };
|
|
81
|
+
// player's feed rows and name stay hidden.
|
|
82
|
+
// version: the last version given to a card (rule A).
|
|
83
|
+
// epoch: this server's own, so no version of an earlier server for the
|
|
84
|
+
// same layer (a replacement restarts the counter) can equal one of ours.
|
|
85
|
+
h.ballotState ??= { epoch: newEpoch(), version: 0, shared: null, me: new Map(), sent: new Map(), views: new Map(), rate: new Map() };
|
|
86
|
+
return h.ballotState;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// A deck's spec, checked: `{ card }` or `{ error }`. `lines` are the place's
|
|
90
|
+
// manifest lines.
|
|
91
|
+
export function cleanSpec(spec, lines) {
|
|
92
|
+
if (!isObject(spec)) {
|
|
93
|
+
return { error: 'a card is an object (or null to clear it)' };
|
|
94
|
+
}
|
|
95
|
+
for (const key of Object.keys(spec)) {
|
|
96
|
+
if (!SPEC_KEYS.includes(key)) {
|
|
97
|
+
return { error: `"${key}" is not part of a card` };
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
const card = { mode: spec.mode ?? 'talk' };
|
|
101
|
+
if (!MODES.includes(card.mode)) {
|
|
102
|
+
return { error: `mode must be one of ${MODES.join(', ')}` };
|
|
103
|
+
}
|
|
104
|
+
for (const [key, max] of [['title', TITLE_MAX], ['text', TEXT_MAX]]) {
|
|
105
|
+
if (spec[key] !== undefined) {
|
|
106
|
+
const text = cardText(spec[key], max);
|
|
107
|
+
if (text === null) {
|
|
108
|
+
return { error: `${key} must be 1-${max} characters on one line` };
|
|
109
|
+
}
|
|
110
|
+
card[key] = text;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
if (spec.until !== undefined) {
|
|
114
|
+
if (typeof spec.until !== 'number' || !Number.isFinite(spec.until)) {
|
|
115
|
+
return { error: 'until must be a finite simulation time in ms (hub.now)' };
|
|
116
|
+
}
|
|
117
|
+
card.until = spec.until;
|
|
118
|
+
}
|
|
119
|
+
const faces = spec.faces ?? [];
|
|
120
|
+
if (!Array.isArray(faces) || faces.length > MAX_FACES) {
|
|
121
|
+
return { error: `faces must be a list of at most ${MAX_FACES}` };
|
|
122
|
+
}
|
|
123
|
+
card.faces = [];
|
|
124
|
+
const faceIds = new Set();
|
|
125
|
+
for (const face of faces) {
|
|
126
|
+
if (!isObject(face) || Object.keys(face).some((key) => !FACE_KEYS.includes(key))) {
|
|
127
|
+
return { error: `a face is { ${FACE_KEYS.join(', ')} }` };
|
|
128
|
+
}
|
|
129
|
+
if (typeof face.playerId !== 'string' || !face.playerId || faceIds.has(face.playerId)) {
|
|
130
|
+
return { error: 'each face names a different playerId' };
|
|
131
|
+
}
|
|
132
|
+
faceIds.add(face.playerId);
|
|
133
|
+
const out = { playerId: face.playerId };
|
|
134
|
+
if (face.note !== undefined) {
|
|
135
|
+
const note = cardText(face.note, NOTE_MAX);
|
|
136
|
+
if (note === null) {
|
|
137
|
+
return { error: `a face's note must be 1-${NOTE_MAX} characters` };
|
|
138
|
+
}
|
|
139
|
+
out.note = note;
|
|
140
|
+
}
|
|
141
|
+
if (face.off !== undefined && typeof face.off !== 'boolean') {
|
|
142
|
+
return { error: "a face's off must be true or false" };
|
|
143
|
+
}
|
|
144
|
+
if (face.off === true) {
|
|
145
|
+
out.off = true;
|
|
146
|
+
}
|
|
147
|
+
if (face.votes !== undefined) {
|
|
148
|
+
if (!Number.isInteger(face.votes) || face.votes < 0 || face.votes > MAX_VOTES) {
|
|
149
|
+
return { error: `a face's votes must be a whole number from 0 to ${MAX_VOTES}` };
|
|
150
|
+
}
|
|
151
|
+
out.votes = face.votes;
|
|
152
|
+
}
|
|
153
|
+
if (face.mark !== undefined) {
|
|
154
|
+
if (typeof face.mark !== 'string' || !COLOR.test(face.mark)) {
|
|
155
|
+
return { error: "a face's mark must be '#rrggbb'" };
|
|
156
|
+
}
|
|
157
|
+
out.mark = face.mark.toLowerCase();
|
|
158
|
+
}
|
|
159
|
+
card.faces.push(out);
|
|
160
|
+
}
|
|
161
|
+
const options = spec.options ?? [];
|
|
162
|
+
if (!Array.isArray(options) || options.length > MAX_OPTIONS) {
|
|
163
|
+
return { error: `options must be a list of at most ${MAX_OPTIONS}` };
|
|
164
|
+
}
|
|
165
|
+
card.options = [];
|
|
166
|
+
const optionIds = new Set();
|
|
167
|
+
for (const option of options) {
|
|
168
|
+
if (!isObject(option) || Object.keys(option).some((key) => key !== 'id' && key !== 'text')) {
|
|
169
|
+
return { error: 'an option is { id, text }' };
|
|
170
|
+
}
|
|
171
|
+
const text = cardText(option.text, OPTION_MAX);
|
|
172
|
+
if (typeof option.id !== 'string' || !BALLOT_LINE_ID.test(option.id) || optionIds.has(option.id) || text === null) {
|
|
173
|
+
return { error: `an option has a different id ([a-z0-9-], 1-24) and text of 1-${OPTION_MAX} characters` };
|
|
174
|
+
}
|
|
175
|
+
optionIds.add(option.id);
|
|
176
|
+
card.options.push({ id: option.id, text });
|
|
177
|
+
}
|
|
178
|
+
const byId = new Map(lines.map((line) => [line.id, line]));
|
|
179
|
+
if (spec.lines === undefined) {
|
|
180
|
+
card.lines = lines.map((line) => line.id);
|
|
181
|
+
} else {
|
|
182
|
+
if (!Array.isArray(spec.lines) || spec.lines.some((id) => !byId.has(id)) || new Set(spec.lines).size !== spec.lines.length) {
|
|
183
|
+
return { error: "lines must be different ids of the place's manifest ballot.lines" };
|
|
184
|
+
}
|
|
185
|
+
card.lines = [...spec.lines];
|
|
186
|
+
}
|
|
187
|
+
const feed = spec.feed ?? [];
|
|
188
|
+
if (!Array.isArray(feed) || feed.length > MAX_FEED) {
|
|
189
|
+
return { error: `feed must be a list of at most ${MAX_FEED}` };
|
|
190
|
+
}
|
|
191
|
+
card.feed = [];
|
|
192
|
+
for (const row of feed) {
|
|
193
|
+
if (!isObject(row) || Object.keys(row).some((key) => !['from', 'to', 'line'].includes(key))) {
|
|
194
|
+
return { error: 'a feed row is { from, to, line }' };
|
|
195
|
+
}
|
|
196
|
+
const line = byId.get(row.line);
|
|
197
|
+
const to = row.to ?? null;
|
|
198
|
+
if (typeof row.from !== 'string' || !row.from || !line || (to !== null && (typeof to !== 'string' || !to))) {
|
|
199
|
+
return { error: "a feed row names a player (from), a manifest line, and a player (to) or null" };
|
|
200
|
+
}
|
|
201
|
+
if ((line.about === 'face') !== (to !== null)) {
|
|
202
|
+
return { error: `feed line "${line.id}" is about ${line.about === 'face' ? 'a face: it needs to' : 'the speaker: it takes no to'}` };
|
|
203
|
+
}
|
|
204
|
+
card.feed.push({ from: row.from, to, line: line.id });
|
|
205
|
+
}
|
|
206
|
+
if (spec.picked !== undefined && spec.picked !== null) {
|
|
207
|
+
const picked = spec.picked;
|
|
208
|
+
if (!isObject(picked) || Object.keys(picked).some((key) => key !== 'face' && key !== 'option')) {
|
|
209
|
+
return { error: 'picked is { face } or { option }' };
|
|
210
|
+
}
|
|
211
|
+
if ((picked.face === undefined) === (picked.option === undefined)) {
|
|
212
|
+
return { error: 'picked names one face or one option' };
|
|
213
|
+
}
|
|
214
|
+
if (picked.face !== undefined && !faceIds.has(picked.face)) {
|
|
215
|
+
return { error: 'picked.face must be one of the faces' };
|
|
216
|
+
}
|
|
217
|
+
if (picked.option !== undefined && !optionIds.has(picked.option)) {
|
|
218
|
+
return { error: 'picked.option must be one of the options' };
|
|
219
|
+
}
|
|
220
|
+
card.picked = picked.face !== undefined ? { face: picked.face } : { option: picked.option };
|
|
221
|
+
}
|
|
222
|
+
return { card };
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
// What can be tapped on a card, as one string: its mode, the faces offered
|
|
226
|
+
// and which are `off`, the lines offered, the option ids and the lock. Two
|
|
227
|
+
// cards with the same key allow exactly the same taps; title, text, until,
|
|
228
|
+
// notes, votes, marks, option words and the feed are not in it.
|
|
229
|
+
export function actionKey(card) {
|
|
230
|
+
return JSON.stringify([
|
|
231
|
+
card.mode,
|
|
232
|
+
card.faces.map((face) => [face.playerId, face.off === true]),
|
|
233
|
+
card.lines,
|
|
234
|
+
card.options.map((option) => option.id),
|
|
235
|
+
card.picked ?? null,
|
|
236
|
+
]);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
// `hub.ballot(playerIds | null, spec | null)`.
|
|
240
|
+
export function ballot(h, playerIds, spec) {
|
|
241
|
+
if (!h.place?.ballot) {
|
|
242
|
+
return refuse(h, 'off', 'this place declares no `ballot` in its hub manifest');
|
|
243
|
+
}
|
|
244
|
+
let targets = null;
|
|
245
|
+
if (playerIds !== null && playerIds !== undefined) {
|
|
246
|
+
if (!Array.isArray(playerIds) || playerIds.length > MAX_TARGETS || playerIds.some((id) => typeof id !== 'string')) {
|
|
247
|
+
return refuse(h, 'targets', 'playerIds must be a list of player ids, or null for everyone here');
|
|
248
|
+
}
|
|
249
|
+
targets = playerIds;
|
|
250
|
+
}
|
|
251
|
+
let card = null;
|
|
252
|
+
if (spec !== null && spec !== undefined) {
|
|
253
|
+
const checked = cleanSpec(spec, h.place.ballot.lines);
|
|
254
|
+
if (checked.error) {
|
|
255
|
+
return refuse(h, `spec:${checked.error}`, checked.error);
|
|
256
|
+
}
|
|
257
|
+
card = checked.card;
|
|
258
|
+
}
|
|
259
|
+
const s = state(h);
|
|
260
|
+
// Rule A: a card keeps the version of the one it replaces for a player
|
|
261
|
+
// while what can be tapped on it is the same (`actionKey`); anything else
|
|
262
|
+
// is a new version. A pure comparison with that current card.
|
|
263
|
+
const next = (current, to) => {
|
|
264
|
+
if (current && current.key === to.key) {
|
|
265
|
+
return current.v;
|
|
266
|
+
}
|
|
267
|
+
s.version += 1;
|
|
268
|
+
return `${s.epoch}-${s.version}`;
|
|
269
|
+
};
|
|
270
|
+
if (card) {
|
|
271
|
+
card.key = actionKey(card);
|
|
272
|
+
}
|
|
273
|
+
if (targets === null) {
|
|
274
|
+
if (card) {
|
|
275
|
+
card.v = next(s.shared, card);
|
|
276
|
+
// A follower of the old shared card keeps its own version while the
|
|
277
|
+
// new one allows the same taps, else it takes the new card's.
|
|
278
|
+
for (const [playerId, entry] of s.me) {
|
|
279
|
+
if (!entry.of) {
|
|
280
|
+
continue;
|
|
281
|
+
}
|
|
282
|
+
if (entry.key === card.key) {
|
|
283
|
+
s.me.set(playerId, { ...card, v: entry.v, of: card });
|
|
284
|
+
} else {
|
|
285
|
+
s.me.delete(playerId);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
s.shared = card;
|
|
290
|
+
// Clearing for everyone takes down every player's own card too.
|
|
291
|
+
if (!card) {
|
|
292
|
+
s.me.clear();
|
|
293
|
+
}
|
|
294
|
+
return true;
|
|
295
|
+
}
|
|
296
|
+
for (const playerId of targets) {
|
|
297
|
+
if (!h.sim?.players.has(playerId)) {
|
|
298
|
+
continue;
|
|
299
|
+
}
|
|
300
|
+
const prior = s.me.get(playerId);
|
|
301
|
+
if (card) {
|
|
302
|
+
s.me.set(playerId, { ...card, v: next(prior ?? s.shared, card) });
|
|
303
|
+
} else if (!s.shared) {
|
|
304
|
+
s.me.delete(playerId);
|
|
305
|
+
} else if (prior) {
|
|
306
|
+
// Back to the shared card, by rule A against the card they had: a
|
|
307
|
+
// follower copy carrying their own version.
|
|
308
|
+
s.me.set(playerId, { ...s.shared, v: next(prior, s.shared), of: s.shared });
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
return true;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
// What one viewer is shown of a card: names and avatars are the kit's, from
|
|
315
|
+
// the session, so a player not here now is not on it (no face, no feed row
|
|
316
|
+
// from or to them); a player a block hides from the viewer is not on it
|
|
317
|
+
// either, and their name is taken out of every word the card shows (only a
|
|
318
|
+
// name of a player here now: a departed player's name in the deck's own words
|
|
319
|
+
// is not, as with the HUD and labels):
|
|
320
|
+
// the deck's title, text, notes and options here, and the manifest's lines
|
|
321
|
+
// as `words` (line id -> the line as this viewer reads it, only for a line
|
|
322
|
+
// the redaction changes). A lock on a face they cannot see stays a lock,
|
|
323
|
+
// `picked: {}`, without naming whom.
|
|
324
|
+
// `d` is `{ usernameOf(playerId) | null, avatarOf(playerId) | null, hidden: Set | null, names: [usernames], lines: [manifest lines] }`.
|
|
325
|
+
export function viewFor(card, viewer, d) {
|
|
326
|
+
if (!card) {
|
|
327
|
+
return null;
|
|
328
|
+
}
|
|
329
|
+
const hidden = d.hidden;
|
|
330
|
+
const redact = (text) => (d.names.length ? Blocks.redactText(text, d.names) : text);
|
|
331
|
+
const who = Object.create(null);
|
|
332
|
+
const know = (playerId) => {
|
|
333
|
+
if (Object.hasOwn(who, playerId)) {
|
|
334
|
+
return true;
|
|
335
|
+
}
|
|
336
|
+
const name = d.usernameOf(playerId);
|
|
337
|
+
if (!name) {
|
|
338
|
+
return false;
|
|
339
|
+
}
|
|
340
|
+
const avatar = d.avatarOf(playerId);
|
|
341
|
+
who[playerId] = Number.isInteger(avatar) ? { name, avatar } : { name };
|
|
342
|
+
return true;
|
|
343
|
+
};
|
|
344
|
+
const view = { v: card.v, mode: card.mode };
|
|
345
|
+
if (card.title !== undefined) {
|
|
346
|
+
view.title = redact(card.title);
|
|
347
|
+
}
|
|
348
|
+
if (card.text !== undefined) {
|
|
349
|
+
view.text = redact(card.text);
|
|
350
|
+
}
|
|
351
|
+
if (card.until !== undefined) {
|
|
352
|
+
view.until = card.until;
|
|
353
|
+
}
|
|
354
|
+
view.faces = [];
|
|
355
|
+
for (const face of card.faces) {
|
|
356
|
+
if (hidden?.has(face.playerId) || !know(face.playerId)) {
|
|
357
|
+
continue;
|
|
358
|
+
}
|
|
359
|
+
const out = { id: face.playerId };
|
|
360
|
+
if (face.note !== undefined) {
|
|
361
|
+
out.note = redact(face.note);
|
|
362
|
+
}
|
|
363
|
+
for (const key of ['off', 'votes', 'mark']) {
|
|
364
|
+
if (face[key] !== undefined) {
|
|
365
|
+
out[key] = face[key];
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
view.faces.push(out);
|
|
369
|
+
}
|
|
370
|
+
view.options = card.options.map((option) => ({ id: option.id, text: redact(option.text) }));
|
|
371
|
+
view.lines = [...card.lines];
|
|
372
|
+
view.feed = [];
|
|
373
|
+
for (const row of card.feed) {
|
|
374
|
+
// Only rows between players here now, and none a block hides.
|
|
375
|
+
const absent = !d.usernameOf(row.from) || (row.to !== null && !d.usernameOf(row.to));
|
|
376
|
+
if (absent || hidden?.has(row.from) || (row.to !== null && hidden?.has(row.to))) {
|
|
377
|
+
continue;
|
|
378
|
+
}
|
|
379
|
+
know(row.from);
|
|
380
|
+
if (row.to !== null) {
|
|
381
|
+
know(row.to);
|
|
382
|
+
}
|
|
383
|
+
view.feed.push(row.to === null ? { from: row.from, line: row.line } : { from: row.from, to: row.to, line: row.line });
|
|
384
|
+
}
|
|
385
|
+
if (card.picked) {
|
|
386
|
+
view.picked = card.picked.option !== undefined || view.faces.some((face) => face.id === card.picked.face) ? { ...card.picked } : {};
|
|
387
|
+
}
|
|
388
|
+
if (d.names.length) {
|
|
389
|
+
const words = Object.create(null);
|
|
390
|
+
for (const line of d.lines ?? []) {
|
|
391
|
+
const text = redact(line.text);
|
|
392
|
+
if (text !== line.text) {
|
|
393
|
+
words[line.id] = text;
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
if (Object.keys(words).length) {
|
|
397
|
+
view.words = words;
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
view.who = who;
|
|
401
|
+
return view;
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
// The view of the card a player has now (theirs, else the shared one): a
|
|
405
|
+
// pure function of the card and of who is connected now. Blocks are the
|
|
406
|
+
// session's pairs among the players here (`hiddenPair` answers nothing about
|
|
407
|
+
// a player who has left), so nothing is remembered between sends.
|
|
408
|
+
function currentView(h, playerId, deps, hidden) {
|
|
409
|
+
const s = h.ballotState;
|
|
410
|
+
const card = s.me.get(playerId) ?? s.shared;
|
|
411
|
+
const hiddenIds = hidden?.get(playerId) ?? null;
|
|
412
|
+
return viewFor(card, playerId, {
|
|
413
|
+
usernameOf: deps.usernameOf,
|
|
414
|
+
avatarOf: deps.avatarOf,
|
|
415
|
+
hidden: hiddenIds,
|
|
416
|
+
names: hiddenIds ? deps.namesOf(hiddenIds) : [],
|
|
417
|
+
lines: h.place.ballot.lines,
|
|
418
|
+
});
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
// Each player's card, to them alone, when it changes (and again to a client
|
|
422
|
+
// that joined afresh). Nothing in a place that has never shown one. Whatever
|
|
423
|
+
// is kept for a connection no longer in the world goes, whether or not it
|
|
424
|
+
// ever said hello (`forget` runs only for players who did).
|
|
425
|
+
export function send(h, deps) {
|
|
426
|
+
const s = h.ballotState;
|
|
427
|
+
if (!s) {
|
|
428
|
+
return;
|
|
429
|
+
}
|
|
430
|
+
const players = h.sim.players;
|
|
431
|
+
for (const map of [s.me, s.sent, s.views, s.rate]) {
|
|
432
|
+
for (const playerId of map.keys()) {
|
|
433
|
+
if (!players.has(playerId)) {
|
|
434
|
+
map.delete(playerId);
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
const hidden = deps.blockedPairs();
|
|
439
|
+
for (const playerId of players.keys()) {
|
|
440
|
+
const view = currentView(h, playerId, deps, hidden);
|
|
441
|
+
const json = JSON.stringify(view);
|
|
442
|
+
if ((s.sent.get(playerId) ?? 'null') !== json) {
|
|
443
|
+
s.sent.set(playerId, json);
|
|
444
|
+
if (view) {
|
|
445
|
+
s.views.set(playerId, view);
|
|
446
|
+
} else {
|
|
447
|
+
s.views.delete(playerId);
|
|
448
|
+
}
|
|
449
|
+
h.session?.send(playerId, { k: 'hub-ballot-card', card: view });
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
// Whether the card `view` allows a tap of `{ face, line, option }` (ids or
|
|
455
|
+
// null): the ids are on it and its mode and lock take them.
|
|
456
|
+
function allows(view, face, line, option, lines) {
|
|
457
|
+
if (!view || view.mode === 'reveal') {
|
|
458
|
+
return false;
|
|
459
|
+
}
|
|
460
|
+
if (option !== null) {
|
|
461
|
+
// An option alone, and one this card offers.
|
|
462
|
+
if (face !== null || line !== null || !view.options.some((o) => o.id === option)) {
|
|
463
|
+
return false;
|
|
464
|
+
}
|
|
465
|
+
} else if (face !== null && !view.faces.some((f) => f.id === face && !f.off)) {
|
|
466
|
+
return false;
|
|
467
|
+
}
|
|
468
|
+
if (view.mode === 'vote') {
|
|
469
|
+
// A face or an option, no line, and no lock (`picked`, `{}` included).
|
|
470
|
+
return line === null && (face !== null || option !== null) && !view.picked;
|
|
471
|
+
}
|
|
472
|
+
if (option !== null) {
|
|
473
|
+
return true;
|
|
474
|
+
}
|
|
475
|
+
// Talk: a line this card offers; a line about a face takes one, a line
|
|
476
|
+
// about the speaker takes none.
|
|
477
|
+
const said = line !== null && view.lines.includes(line) ? lines.find((l) => l.id === line) : null;
|
|
478
|
+
return Boolean(said) && (said.about === 'face') === (face !== null);
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
// A tap on the card: `{ k: 'hub-ballot', v, face?, line?, option? }`, ids
|
|
482
|
+
// only. Rule A: `v` is the version last delivered to this player and the
|
|
483
|
+
// version of the card they have now, and the ids are allowed by that card as
|
|
484
|
+
// delivered; then the tap is limited and handed to the deck's `ballot` hook.
|
|
485
|
+
// Anything else is dropped unheard.
|
|
486
|
+
export function message(h, player, data, lines) {
|
|
487
|
+
const playerId = player?.playerId;
|
|
488
|
+
const s = h.ballotState;
|
|
489
|
+
const delivered = s?.views.get(playerId) ?? null;
|
|
490
|
+
if (!delivered || !h.sim?.players.has(playerId) || !h.players.get(playerId)?.arrived) {
|
|
491
|
+
return;
|
|
492
|
+
}
|
|
493
|
+
if (!isObject(data) || Object.keys(data).some((key) => !MESSAGE_KEYS.includes(key))) {
|
|
494
|
+
return;
|
|
495
|
+
}
|
|
496
|
+
const current = s.me.get(playerId) ?? s.shared;
|
|
497
|
+
if (data.v !== delivered.v || current?.v !== delivered.v) {
|
|
498
|
+
return;
|
|
499
|
+
}
|
|
500
|
+
const face = data.face ?? null;
|
|
501
|
+
const line = data.line ?? null;
|
|
502
|
+
const option = data.option ?? null;
|
|
503
|
+
for (const id of [face, line, option]) {
|
|
504
|
+
if (id !== null && typeof id !== 'string') {
|
|
505
|
+
return;
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
if (!allows(delivered, face, line, option, lines)) {
|
|
509
|
+
return;
|
|
510
|
+
}
|
|
511
|
+
const now = Date.now();
|
|
512
|
+
const r = s.rate.get(playerId) ?? { tokens: BURST, at: now };
|
|
513
|
+
r.tokens = Math.min(BURST, r.tokens + (now - r.at) / EVERY_MS);
|
|
514
|
+
r.at = now;
|
|
515
|
+
s.rate.set(playerId, r);
|
|
516
|
+
if (r.tokens < 1) {
|
|
517
|
+
return;
|
|
518
|
+
}
|
|
519
|
+
r.tokens -= 1;
|
|
520
|
+
try {
|
|
521
|
+
h.hooks.ballot?.(h.ctx, playerId, { face, line, option, mode: delivered.mode });
|
|
522
|
+
} catch (err) {
|
|
523
|
+
console.warn(`[hub] ballot hook failed: ${err?.message ?? err}`);
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
// A client that joined afresh holds no card: it is sent its card again.
|
|
528
|
+
export function joinedAfresh(h, playerId) {
|
|
529
|
+
h.ballotState.sent.delete(playerId);
|
|
530
|
+
h.ballotState.views.delete(playerId);
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
export function forget(h, playerId) {
|
|
534
|
+
const s = h.ballotState;
|
|
535
|
+
s.me.delete(playerId);
|
|
536
|
+
s.sent.delete(playerId);
|
|
537
|
+
s.views.delete(playerId);
|
|
538
|
+
s.rate.delete(playerId);
|
|
539
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
// Scene-authored ground cells. Geometry stays independent of physics and I/O.
|
|
2
|
+
export const VIEW_RADIUS_M = 10;
|
|
3
|
+
|
|
4
|
+
export function readCells(scene) {
|
|
5
|
+
const source = scene?.cells;
|
|
6
|
+
const bad = (reason) => ({ table: null, reason });
|
|
7
|
+
if (!source || source.version !== 1 || !Array.isArray(source.cells) || !source.cells.length) {
|
|
8
|
+
return bad('must have version 1 and a nonempty cells array');
|
|
9
|
+
}
|
|
10
|
+
const ids = new Set();
|
|
11
|
+
for (const cell of source.cells) {
|
|
12
|
+
if (!cell || typeof cell.id !== 'string' || !cell.id || ids.has(cell.id)) {
|
|
13
|
+
return bad('ids must be unique strings');
|
|
14
|
+
}
|
|
15
|
+
ids.add(cell.id);
|
|
16
|
+
if (
|
|
17
|
+
!Array.isArray(cell.outline) ||
|
|
18
|
+
cell.outline.length < 3 ||
|
|
19
|
+
!cell.outline.every((p) => Array.isArray(p) && p.length === 2 && p.every(Number.isFinite))
|
|
20
|
+
) {
|
|
21
|
+
return bad('outlines must have at least three finite [x,z] pairs');
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
for (const cell of source.cells) {
|
|
25
|
+
if (
|
|
26
|
+
!Array.isArray(cell.sees) ||
|
|
27
|
+
!cell.sees.includes(cell.id) ||
|
|
28
|
+
!cell.sees.every((id) => ids.has(id))
|
|
29
|
+
) {
|
|
30
|
+
return bad('sees must include its own id and contain only known ids');
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
if (
|
|
34
|
+
source.hysteresis !== undefined &&
|
|
35
|
+
(!Number.isFinite(source.hysteresis) || source.hysteresis < 0)
|
|
36
|
+
) {
|
|
37
|
+
return bad('hysteresis must be finite and nonnegative');
|
|
38
|
+
}
|
|
39
|
+
return {
|
|
40
|
+
table: {
|
|
41
|
+
...source,
|
|
42
|
+
hysteresis: source.hysteresis ?? 2.5,
|
|
43
|
+
byId: new Map(source.cells.map((c) => [c.id, c])),
|
|
44
|
+
},
|
|
45
|
+
reason: null,
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function edgeDistance(x, z, a, b) {
|
|
50
|
+
const dx = b[0] - a[0];
|
|
51
|
+
const dz = b[1] - a[1];
|
|
52
|
+
const length = dx * dx + dz * dz;
|
|
53
|
+
const t = length ? Math.max(0, Math.min(1, ((x - a[0]) * dx + (z - a[1]) * dz) / length)) : 0;
|
|
54
|
+
return Math.hypot(x - a[0] - t * dx, z - a[1] - t * dz);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function distance(cell, x, z) {
|
|
58
|
+
let inside = false;
|
|
59
|
+
let near = Infinity;
|
|
60
|
+
const points = cell.outline;
|
|
61
|
+
for (let i = 0, j = points.length - 1; i < points.length; j = i++) {
|
|
62
|
+
const a = points[j];
|
|
63
|
+
const b = points[i];
|
|
64
|
+
near = Math.min(near, edgeDistance(x, z, a, b));
|
|
65
|
+
if (a[1] > z !== b[1] > z && x < ((b[0] - a[0]) * (z - a[1])) / (b[1] - a[1]) + a[0]) {
|
|
66
|
+
inside = !inside;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return inside ? 0 : near;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function cellAt(table, x, z, previous = null) {
|
|
73
|
+
if (!Number.isFinite(x) || !Number.isFinite(z)) {
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
76
|
+
const old = table.byId.get(previous);
|
|
77
|
+
if (old && distance(old, x, z) <= table.hysteresis) {
|
|
78
|
+
return previous;
|
|
79
|
+
}
|
|
80
|
+
return table.cells.find((cell) => distance(cell, x, z) <= 1e-9)?.id ?? null;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export function cellsNear(table, x, z, radius) {
|
|
84
|
+
return new Set(
|
|
85
|
+
table.cells.filter((cell) => distance(cell, x, z) <= radius).map((cell) => cell.id),
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export function visibleCells(table, x, z) {
|
|
90
|
+
if (cellAt(table, x, z) === null) {
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
const visible = new Set();
|
|
94
|
+
for (const id of cellsNear(table, x, z, VIEW_RADIUS_M)) {
|
|
95
|
+
for (const seen of table.byId.get(id).sees) {
|
|
96
|
+
visible.add(seen);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return visible;
|
|
100
|
+
}
|