castle-web-cli 0.4.239 → 0.4.241
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.js +5 -1
- package/dist/api.d.ts +20 -0
- package/dist/api.js +57 -0
- package/dist/bundle.d.ts +38 -1
- package/dist/bundle.js +309 -13
- package/dist/castle-host/host.js +163 -11
- package/dist/castleJson.d.ts +10 -0
- package/dist/castleJson.js +46 -2
- package/dist/check.js +5 -2
- package/dist/devSessionServer.js +4 -1
- package/dist/devSessionStorage.d.ts +1 -0
- package/dist/devSessionStorage.js +32 -7
- package/dist/importOrder.d.ts +2 -0
- package/dist/importOrder.js +29 -0
- package/dist/index.js +6 -3
- package/dist/init.js +24 -2
- package/dist/multiplayerDoc.d.ts +1 -1
- package/dist/multiplayerDoc.js +2 -0
- package/dist/platformDoc.js +3 -2
- package/dist/playPagePolicy.js +5 -1
- package/dist/resetLeaderboards.d.ts +8 -1
- package/dist/resetLeaderboards.js +43 -8
- package/dist/roomLogs.d.ts +1 -0
- package/dist/roomLogs.js +30 -6
- package/dist/save-deck.d.ts +32 -2
- package/dist/save-deck.js +240 -39
- package/kits/base/CLAUDE.md +1 -1
- package/kits/base/castle.json +1 -1
- package/kits/base/sdk/README.md +174 -6
- package/kits/base/sdk/analytics.d.ts +8 -0
- package/kits/base/sdk/analytics.js +92 -0
- package/kits/base/sdk/castle.d.ts +5 -1
- package/kits/base/sdk/castle.js +2 -0
- package/kits/base/sdk/commands.d.ts +48 -0
- package/kits/base/sdk/commands.js +1 -0
- package/kits/base/sdk/debugConsole.d.ts +17 -0
- package/kits/base/sdk/debugConsole.js +154 -0
- package/kits/base/sdk/experiments.js +24 -1
- package/kits/base/sdk/hostRetry.d.ts +7 -0
- package/kits/base/sdk/hostRetry.js +57 -0
- package/kits/base/sdk/hub.d.ts +20 -2
- package/kits/base/sdk/hub.js +99 -13
- package/kits/base/sdk/multiplayer.d.ts +2 -0
- package/kits/base/sdk/multiplayer.js +168 -26
- package/kits/base/sdk/reconnectBudget.d.ts +6 -0
- package/kits/base/sdk/reconnectBudget.js +41 -1
- package/kits/base/sdk/runtime.js +36 -19
- package/kits/base/sdk/server/hub.d.ts +19 -0
- package/kits/base/sdk/server/hub.js +12 -0
- package/kits/base/sdk/server/platformHandle.d.ts +1 -0
- package/kits/base/sdk/server/wrapper.d.ts +1 -1
- package/kits/base/sdk/server/wrapper.js +26 -1
- package/kits/base/sdk/transport.d.ts +2 -1
- package/kits/base/sdk/transport.js +35 -0
- package/kits/hub-game/CLAUDE.md +66 -19
- package/kits/hub-game/code/client/act.js +9 -6
- package/kits/hub-game/code/client/actCore.js +26 -6
- package/kits/hub-game/code/client/actGateLog.js +36 -0
- package/kits/hub-game/code/client/actionOverflow.js +29 -8
- package/kits/hub-game/code/client/actionRailSurface.js +186 -0
- package/kits/hub-game/code/client/actionRailTree.js +173 -0
- package/kits/hub-game/code/client/askSurface.js +85 -0
- package/kits/hub-game/code/client/askTree.js +51 -0
- package/kits/hub-game/code/client/attention.js +31 -6
- package/kits/hub-game/code/client/ballot.js +42 -5
- package/kits/hub-game/code/client/ballotSurface.js +115 -0
- package/kits/hub-game/code/client/ballotTree.js +180 -0
- package/kits/hub-game/code/client/catchingUp.js +19 -0
- package/kits/hub-game/code/client/celebrate.js +66 -0
- package/kits/hub-game/code/client/chrome.js +481 -57
- package/kits/hub-game/code/client/companion.js +93 -23
- package/kits/hub-game/code/client/companionDress.js +1 -1
- package/kits/hub-game/code/client/cornerSurface.js +106 -0
- package/kits/hub-game/code/client/cornerTree.js +83 -0
- package/kits/hub-game/code/client/decor.js +20 -11
- package/kits/hub-game/code/client/deviceStorage.js +121 -0
- package/kits/hub-game/code/client/disguise.js +23 -0
- package/kits/hub-game/code/client/disguiseDraw.js +87 -0
- package/kits/hub-game/code/client/dockSurface.js +206 -0
- package/kits/hub-game/code/client/dockTree.js +232 -0
- package/kits/hub-game/code/client/earned.js +29 -0
- package/kits/hub-game/code/client/emoteSurface.js +188 -0
- package/kits/hub-game/code/client/emoteTree.js +264 -0
- package/kits/hub-game/code/client/farAvatars.js +4 -3
- package/kits/hub-game/code/client/fpsTelemetry.js +3 -1
- package/kits/hub-game/code/client/grab.js +121 -50
- package/kits/hub-game/code/client/holdShow.js +19 -0
- package/kits/hub-game/code/client/hookLayer.js +96 -12
- package/kits/hub-game/code/client/hubControls.js +7 -1
- package/kits/hub-game/code/client/hudSurface.js +11 -3
- package/kits/hub-game/code/client/hudTree.js +7 -4
- package/kits/hub-game/code/client/idle.js +21 -3
- package/kits/hub-game/code/client/invites.js +24 -3
- package/kits/hub-game/code/client/keys.js +15 -0
- package/kits/hub-game/code/client/labelsNear.js +8 -1
- package/kits/hub-game/code/client/layoutCache.js +57 -8
- package/kits/hub-game/code/client/loadingCover.js +53 -4
- package/kits/hub-game/code/client/menuIcons.js +15 -0
- package/kits/hub-game/code/client/menuSurface.js +102 -0
- package/kits/hub-game/code/client/menuTree.js +135 -0
- package/kits/hub-game/code/client/modelLook.js +41 -0
- package/kits/hub-game/code/client/padCues.js +11 -7
- package/kits/hub-game/code/client/padReadability.js +3 -3
- package/kits/hub-game/code/client/pickerSurface.js +152 -0
- package/kits/hub-game/code/client/pickerTree.js +163 -0
- package/kits/hub-game/code/client/pushVisit.js +4 -2
- package/kits/hub-game/code/client/quality.js +188 -29
- package/kits/hub-game/code/client/qualityEntry.js +10 -1
- package/kits/hub-game/code/client/quickChatSurface.js +222 -0
- package/kits/hub-game/code/client/quickChatTree.js +201 -0
- package/kits/hub-game/code/client/rehello.js +75 -0
- package/kits/hub-game/code/client/renderVisibility.js +17 -0
- package/kits/hub-game/code/client/replay.js +118 -0
- package/kits/hub-game/code/client/role.js +3 -0
- package/kits/hub-game/code/client/safeLayout.js +441 -56
- package/kits/hub-game/code/client/safeStyle.js +13 -10
- package/kits/hub-game/code/client/say.js +2 -0
- package/kits/hub-game/code/client/soundSurface.js +77 -0
- package/kits/hub-game/code/client/soundTree.js +74 -0
- package/kits/hub-game/code/client/soundscape.js +12 -9
- package/kits/hub-game/code/client/soundscapeCues.js +1 -1
- package/kits/hub-game/code/client/state.js +67 -1
- package/kits/hub-game/code/client/stuckSurface.js +76 -0
- package/kits/hub-game/code/client/stuckTree.js +67 -0
- package/kits/hub-game/code/client/talk.js +42 -12
- package/kits/hub-game/code/client/talkTree.js +4 -2
- package/kits/hub-game/code/client/textAskSurface.js +147 -0
- package/kits/hub-game/code/client/textAskTree.js +67 -0
- package/kits/hub-game/code/client/tiers.js +252 -27
- package/kits/hub-game/code/client/together.js +4 -1
- package/kits/hub-game/code/client/uiDom.js +86 -8
- package/kits/hub-game/code/client/uiTree.js +33 -4
- package/kits/hub-game/code/client/vehicle.js +72 -51
- package/kits/hub-game/code/client/weakestTier.js +75 -31
- package/kits/hub-game/code/client/wear.js +159 -5
- package/kits/hub-game/code/client/worldLabels.js +205 -25
- package/kits/hub-game/code/companionSpec.js +4 -2
- package/kits/hub-game/code/server/act.js +3 -0
- package/kits/hub-game/code/server/actGate.js +83 -0
- package/kits/hub-game/code/server/actions.js +71 -15
- package/kits/hub-game/code/server/actors.js +20 -5
- package/kits/hub-game/code/server/analytics.js +35 -3
- package/kits/hub-game/code/server/ask.js +39 -3
- package/kits/hub-game/code/server/ballot.js +167 -4
- package/kits/hub-game/code/server/celebrate.js +64 -0
- package/kits/hub-game/code/server/companion.js +17 -2
- package/kits/hub-game/code/server/decor.js +11 -2
- package/kits/hub-game/code/server/disguise.js +63 -0
- package/kits/hub-game/code/server/earned.js +6 -2
- package/kits/hub-game/code/server/friendBests.js +13 -4
- package/kits/hub-game/code/server/friendsAmong.js +67 -8
- package/kits/hub-game/code/server/grab.js +12 -2
- package/kits/hub-game/code/server/heldOffers.js +142 -0
- package/kits/hub-game/code/server/holdShow.js +98 -0
- package/kits/hub-game/code/server/hub.js +991 -72
- package/kits/hub-game/code/server/idle.js +110 -9
- package/kits/hub-game/code/server/index.js +9 -0
- package/kits/hub-game/code/server/labels.js +13 -3
- package/kits/hub-game/code/server/manifest.js +94 -6
- package/kits/hub-game/code/server/menu.js +26 -3
- package/kits/hub-game/code/server/npcs.js +55 -9
- package/kits/hub-game/code/server/observe.js +67 -6
- package/kits/hub-game/code/server/players.js +14 -2
- package/kits/hub-game/code/server/replay.js +135 -0
- package/kits/hub-game/code/server/standing.js +7 -3
- package/kits/hub-game/code/server/storageLimit.js +4 -1
- package/kits/hub-game/code/server/townDay.js +74 -0
- package/kits/hub-game/code/server/vehicle.js +3 -0
- package/kits/hub-game/code/server/vis.js +26 -4
- package/kits/hub-game/code/server/wallet.js +677 -0
- package/kits/hub-game/code/server/wear.js +13 -3
- package/kits/hub-game/code/server/writerLease.js +191 -0
- package/kits/hub-game/code/shared/act.js +1 -1
- package/kits/hub-game/code/shared/expression.js +6 -0
- package/kits/hub-game/code/shared/replay.js +37 -0
- package/kits/hub-game/code/systems/hub.js +429 -55
- package/kits/hub-game/docs/analytics.md +16 -2
- package/kits/hub-game/docs/contract.md +956 -101
- package/kits/hub-game/docs/home-pieces.md +5 -6
- package/kits/hub-game/docs/observed-progress.md +49 -1
- package/kits/hub-game/docs/vehicle-preset.md +23 -0
- package/kits/hub-game/index.html +1 -0
- package/kits/hub-game/models/hats/baker-cap.pxmodel +3 -3
- package/kits/hub-game/models/hats/bat-clip.pxmodel +3 -3
- package/kits/hub-game/models/hats/beekeeper.pxmodel +3 -3
- package/kits/hub-game/models/hats/cap.pxmodel +3 -3
- package/kits/hub-game/models/hats/captain.pxmodel +3 -3
- package/kits/hub-game/models/hats/chef-toque.pxmodel +3 -3
- package/kits/hub-game/models/hats/crooked-top.pxmodel +3 -3
- package/kits/hub-game/models/hats/delivery-cap.pxmodel +3 -3
- package/kits/hub-game/models/hats/fire-helmet.pxmodel +3 -3
- package/kits/hub-game/models/hats/flower-crown.pxmodel +3 -3
- package/kits/hub-game/models/hats/foam-crown.pxmodel +3 -3
- package/kits/hub-game/models/hats/hard-hat.pxmodel +3 -3
- package/kits/hub-game/models/hats/lantern.pxmodel +3 -3
- package/kits/hub-game/models/hats/laurel.pxmodel +3 -3
- package/kits/hub-game/models/hats/lighthouse.pxmodel +3 -3
- package/kits/hub-game/models/hats/nurse-cap.pxmodel +3 -3
- package/kits/hub-game/models/hats/police-cap.pxmodel +3 -3
- package/kits/hub-game/models/hats/sailor.pxmodel +3 -3
- package/kits/hub-game/models/hats/souwester.pxmodel +3 -3
- package/kits/hub-game/models/hats/straw.pxmodel +3 -3
- package/kits/hub-game/models/hats/sunflower.pxmodel +3 -3
- package/kits/hub-game/models/hats/sweatband.pxmodel +3 -3
- package/kits/hub-game/models/hats/teacher-beret.pxmodel +3 -3
- package/kits/hub-game/models/hats/witch.pxmodel +3 -3
- package/kits/hub-game/models/hold-show/daisy.pxmodel +231 -0
- package/kits/hub-game/models/hold-show/moonflower.pxmodel +171 -0
- package/kits/hub-game/models/hold-show/rose.pxmodel +191 -0
- package/kits/hub-game/models/hold-show/square/daisy.pxmodel +233 -0
- package/kits/hub-game/models/hold-show/square/moonflower.pxmodel +173 -0
- package/kits/hub-game/models/hold-show/square/rose.pxmodel +193 -0
- package/kits/hub-game/models/hold-show/square/sunflower.pxmodel +313 -0
- package/kits/hub-game/models/hold-show/square/tulip.pxmodel +133 -0
- package/kits/hub-game/models/hold-show/sunflower.pxmodel +311 -0
- package/kits/hub-game/models/hold-show/tulip.pxmodel +131 -0
- package/kits/hub-game/package-lock.json +3 -3
- package/kits/hub-game/package.json +4 -3
- package/kits/hub-game/scripts/fixtures/engine-models-stub.mjs +1 -0
- package/kits/hub-game/scripts/fixtures/hud-surface.json +12 -12
- package/kits/hub-game/scripts/fixtures/talk-surface.json +8 -8
- package/kits/hub-game/scripts/gen-hats.mjs +1 -1
- package/kits/hub-game/scripts/gen-hold-show.mjs +27 -0
- package/kits/hub-game/scripts/pack.mjs +89 -23
- package/kits/hub-game/scripts/test-act-client.mjs +5 -0
- package/kits/hub-game/scripts/test-act-core.mjs +32 -0
- package/kits/hub-game/scripts/test-act-gate.mjs +186 -0
- package/kits/hub-game/scripts/test-action-rail-crowded-dom.mjs +7 -1
- package/kits/hub-game/scripts/test-action-rail-default-dom.mjs +102 -0
- package/kits/hub-game/scripts/test-action-rail-never-silent-dom.mjs +50 -0
- package/kits/hub-game/scripts/test-action-rail-surface.mjs +368 -0
- package/kits/hub-game/scripts/test-action-rail-town-jobs-dom.mjs +118 -0
- package/kits/hub-game/scripts/test-actions-client.mjs +5 -3
- package/kits/hub-game/scripts/test-actions.mjs +24 -0
- package/kits/hub-game/scripts/test-actors.mjs +19 -0
- package/kits/hub-game/scripts/test-again-corner-dom.mjs +59 -0
- package/kits/hub-game/scripts/test-analytics.mjs +79 -0
- package/kits/hub-game/scripts/test-arrival-pose-server.mjs +25 -8
- package/kits/hub-game/scripts/test-ask-client.mjs +9 -0
- package/kits/hub-game/scripts/test-ask-surface.mjs +188 -0
- package/kits/hub-game/scripts/test-attention.mjs +43 -1
- package/kits/hub-game/scripts/test-ballot-sheet-dom.mjs +71 -0
- package/kits/hub-game/scripts/test-ballot-surface.mjs +276 -0
- package/kits/hub-game/scripts/test-ballot.mjs +421 -4
- package/kits/hub-game/scripts/test-ban-join.mjs +1 -1
- package/kits/hub-game/scripts/test-celebrate-client.mjs +62 -0
- package/kits/hub-game/scripts/test-celebrate.mjs +116 -0
- package/kits/hub-game/scripts/test-chat.mjs +73 -0
- package/kits/hub-game/scripts/test-companion-client.mjs +203 -21
- package/kits/hub-game/scripts/test-companion.mjs +110 -3
- package/kits/hub-game/scripts/test-control-layout-dom.mjs +57 -10
- package/kits/hub-game/scripts/test-corner-surface.mjs +268 -0
- package/kits/hub-game/scripts/test-decor-client.mjs +59 -3
- package/kits/hub-game/scripts/test-decor.mjs +27 -0
- package/kits/hub-game/scripts/test-device-class.mjs +4 -1
- package/kits/hub-game/scripts/test-disguise-client.mjs +281 -0
- package/kits/hub-game/scripts/test-disguise.mjs +176 -0
- package/kits/hub-game/scripts/test-dock-surface.mjs +404 -0
- package/kits/hub-game/scripts/test-door-stay-client.mjs +602 -0
- package/kits/hub-game/scripts/test-door-stay.mjs +613 -0
- package/kits/hub-game/scripts/test-earned-client.mjs +119 -0
- package/kits/hub-game/scripts/test-emote-surface.mjs +616 -0
- package/kits/hub-game/scripts/test-fps-telemetry.mjs +39 -4
- package/kits/hub-game/scripts/test-friend-bests.mjs +38 -12
- package/kits/hub-game/scripts/test-ghost-click-dom.mjs +75 -0
- package/kits/hub-game/scripts/test-grab-client.mjs +29 -2
- package/kits/hub-game/scripts/test-grab-orbit.mjs +260 -11
- package/kits/hub-game/scripts/test-grab.mjs +10 -1
- package/kits/hub-game/scripts/test-hats.mjs +28 -0
- package/kits/hub-game/scripts/test-held-offers.mjs +394 -0
- package/kits/hub-game/scripts/test-high-only.mjs +3 -3
- package/kits/hub-game/scripts/test-hold-show-client.mjs +175 -0
- package/kits/hub-game/scripts/test-hold-show.mjs +355 -0
- package/kits/hub-game/scripts/test-hub-controls.mjs +3 -1
- package/kits/hub-game/scripts/test-hub-idle.mjs +348 -0
- package/kits/hub-game/scripts/test-hub-leaving.mjs +21 -0
- package/kits/hub-game/scripts/test-hub-ops.mjs +119 -0
- package/kits/hub-game/scripts/test-hub-trip.mjs +240 -4
- package/kits/hub-game/scripts/test-hud-clear.mjs +83 -0
- package/kits/hub-game/scripts/test-hud-rate.mjs +2 -1
- package/kits/hub-game/scripts/test-idle.mjs +160 -0
- package/kits/hub-game/scripts/test-invites-client.mjs +53 -0
- package/kits/hub-game/scripts/test-labels.mjs +11 -0
- package/kits/hub-game/scripts/test-landscape-layout.mjs +20 -0
- package/kits/hub-game/scripts/test-loading-cover.mjs +67 -1
- package/kits/hub-game/scripts/test-manifest.mjs +27 -0
- package/kits/hub-game/scripts/test-menu-client.mjs +54 -5
- package/kits/hub-game/scripts/test-menu-surface.mjs +236 -0
- package/kits/hub-game/scripts/test-npcs.mjs +128 -0
- package/kits/hub-game/scripts/test-observed.mjs +171 -3
- package/kits/hub-game/scripts/test-pack.mjs +166 -0
- package/kits/hub-game/scripts/test-pad-cues.mjs +39 -0
- package/kits/hub-game/scripts/test-picker-surface.mjs +321 -0
- package/kits/hub-game/scripts/test-placeat-server.mjs +648 -0
- package/kits/hub-game/scripts/test-placement-cache.mjs +1 -1
- package/kits/hub-game/scripts/test-pointer-clear-dom.mjs +82 -0
- package/kits/hub-game/scripts/test-progress-reset.mjs +184 -6
- package/kits/hub-game/scripts/test-push-visit.mjs +9 -0
- package/kits/hub-game/scripts/test-quality.mjs +312 -9
- package/kits/hub-game/scripts/test-quick-chat-surface.mjs +511 -0
- package/kits/hub-game/scripts/test-rehello.mjs +242 -3
- package/kits/hub-game/scripts/test-rejoin-yaw.mjs +138 -0
- package/kits/hub-game/scripts/test-relations-among.mjs +13 -7
- package/kits/hub-game/scripts/test-relations-refresh.mjs +90 -2
- package/kits/hub-game/scripts/test-replay-client.mjs +110 -0
- package/kits/hub-game/scripts/test-replay.mjs +178 -0
- package/kits/hub-game/scripts/test-resume-held.mjs +13 -0
- package/kits/hub-game/scripts/test-resume-pose-server.mjs +43 -5
- package/kits/hub-game/scripts/test-role-client.mjs +2 -1
- package/kits/hub-game/scripts/test-safe-act.mjs +233 -0
- package/kits/hub-game/scripts/test-safe-caption.mjs +364 -0
- package/kits/hub-game/scripts/test-safe-layout.mjs +79 -0
- package/kits/hub-game/scripts/test-safe-prompt.mjs +163 -0
- package/kits/hub-game/scripts/test-sound-surface.mjs +256 -0
- package/kits/hub-game/scripts/test-soundscape.mjs +85 -2
- package/kits/hub-game/scripts/test-standing.mjs +135 -2
- package/kits/hub-game/scripts/test-stuck-surface.mjs +174 -0
- package/kits/hub-game/scripts/test-stuck.mjs +36 -1
- package/kits/hub-game/scripts/test-talk-surface.mjs +23 -6
- package/kits/hub-game/scripts/test-talk-walk-dom.mjs +123 -0
- package/kits/hub-game/scripts/test-text-ask-surface.mjs +273 -0
- package/kits/hub-game/scripts/test-tiers.mjs +453 -3
- package/kits/hub-game/scripts/test-toast-no-row-dom.mjs +71 -0
- package/kits/hub-game/scripts/test-together.mjs +11 -0
- package/kits/hub-game/scripts/test-town-day.mjs +90 -0
- package/kits/hub-game/scripts/test-try-on.mjs +39 -0
- package/kits/hub-game/scripts/test-ui-dom-dom.mjs +49 -2
- package/kits/hub-game/scripts/test-ui-tree.mjs +28 -0
- package/kits/hub-game/scripts/test-vehicle-client.mjs +68 -1
- package/kits/hub-game/scripts/test-vis.mjs +145 -0
- package/kits/hub-game/scripts/test-wallet.mjs +650 -0
- package/kits/hub-game/scripts/test-weakest-tier.mjs +135 -1
- package/kits/hub-game/scripts/test-world-labels.mjs +340 -0
- package/kits/hub-game/scripts/test-writer-lease-server.mjs +234 -0
- package/kits/hub-game/scripts/test-writer-lease.mjs +281 -0
- package/kits/hub-game/scripts/writer-storage-fixture.mjs +37 -0
- package/kits/hub-game/tools/hubScene/export.mjs +6 -5
- package/kits/hub-game/tools/hubScene/hubdata.mjs +10 -0
- package/kits/hub-game/tools/hubScene/test/export-fixture.mjs +36 -2
- package/kits/hub-game/tools/hubScene/validate-manifest.mjs +10 -2
- package/kits/multiplayer-3d/castle.json +5 -5
- package/kits/multiplayer-3d/code/client/resumeGate.js +66 -2
- package/kits/multiplayer-3d/code/server/ownership.js +25 -6
- package/kits/multiplayer-3d/code/server/world.js +19 -4
- package/kits/multiplayer-3d/code/systems/multiplayer.js +108 -18
- package/kits/multiplayer-3d/package-lock.json +1 -1
- package/kits/multiplayer-3d/package.json +2 -1
- package/kits/multiplayer-3d/scripts/test-ground-half.mjs +56 -0
- package/kits/multiplayer-3d/scripts/test-join-watchdog.mjs +54 -3
- package/kits/multiplayer-3d/scripts/test-owned-moves.mjs +190 -0
- package/kits/multiplayer-3d/scripts/test-resume-gate.mjs +417 -5
- package/kits/multiplayer-3d/scripts/test-resync-reconnect.mjs +68 -0
- package/kits/physics-2d/castle.json +2 -2
- package/kits/physics-2d/engine/scene.js +10 -2
- package/kits/physics-3d/CLAUDE.md +14 -0
- package/kits/physics-3d/behaviors/Player.jsx +39 -3
- package/kits/physics-3d/castle.json +3 -3
- package/kits/physics-3d/docs/engine-model-cache.md +109 -0
- package/kits/physics-3d/docs/engine-player.md +76 -0
- package/kits/physics-3d/docs/engine-skip-work.md +16 -0
- package/kits/physics-3d/docs/pxmodel-format.md +5 -1
- package/kits/physics-3d/engine3d/gait.js +16 -8
- package/kits/physics-3d/engine3d/groundsize.js +23 -0
- package/kits/physics-3d/engine3d/kithooks.js +50 -0
- package/kits/physics-3d/engine3d/look.js +110 -17
- package/kits/physics-3d/engine3d/lookbake.js +19 -1
- package/kits/physics-3d/engine3d/materialpresets.js +3 -2
- package/kits/physics-3d/engine3d/modelcache.js +280 -0
- package/kits/physics-3d/engine3d/modelcachedigest.js +37 -0
- package/kits/physics-3d/engine3d/modelcachegate.js +69 -0
- package/kits/physics-3d/engine3d/modelrefs.js +13 -0
- package/kits/physics-3d/engine3d/optimize.js +20 -9
- package/kits/physics-3d/engine3d/playerRun.js +26 -1
- package/kits/physics-3d/engine3d/pxmodel.js +7 -3
- package/kits/physics-3d/engine3d/templatecache.js +722 -0
- package/kits/physics-3d/engine3d/world3d.js +211 -17
- package/kits/physics-3d/package-lock.json +1 -1
- package/kits/physics-3d/package.json +2 -1
- package/kits/physics-3d/play/behaviors/Player.js +43 -3
- package/kits/physics-3d/scripts/render.mjs +56 -8
- package/kits/physics-3d/scripts/template-cache-fixtures.mjs +37 -0
- package/kits/physics-3d/scripts/template-cache-v1-bytes.json +49 -0
- package/kits/physics-3d/scripts/test-coaxial-escape.mjs +176 -0
- package/kits/physics-3d/scripts/test-gait.mjs +53 -9
- package/kits/physics-3d/scripts/test-ground-half.mjs +120 -0
- package/kits/physics-3d/scripts/test-model-cache.mjs +600 -0
- package/kits/physics-3d/scripts/test-orbit-shadow-fade.mjs +1 -0
- package/kits/physics-3d/scripts/test-sleeping-models.mjs +384 -0
- package/kits/physics-3d/scripts/test-template-cache-minified.mjs +106 -0
- package/kits/physics-3d/scripts/test-template-cache.mjs +519 -0
- package/kits/physics-3d/scripts/test-wall-slide.mjs +272 -0
- package/kits/physics-3d/systems/physics3d.js +59 -2
- package/kits/real-time/castle.json +2 -2
- package/kits/real-time/code/client/connection.js +3 -0
- package/package.json +2 -2
- package/vendor/three/examples/jsm/libs/fflate.module.js +2672 -0
|
@@ -0,0 +1,650 @@
|
|
|
1
|
+
// v1.185 `hub.wallet`: coins on conditional storage, never a whole-save set.
|
|
2
|
+
// A stand-in for the SDK's session storage keeps a revision per prefix, refuses
|
|
3
|
+
// a stale revision with `committed: false` and the current snapshot, refuses a
|
|
4
|
+
// stale reset generation, answers `operation(id)` and identical replays from
|
|
5
|
+
// its receipts, and can lose a reply after committing. `npm run check` runs it.
|
|
6
|
+
|
|
7
|
+
import assert from 'node:assert/strict';
|
|
8
|
+
import { register } from 'node:module';
|
|
9
|
+
|
|
10
|
+
register(`data:text/javascript,${encodeURIComponent(`
|
|
11
|
+
export async function resolve(specifier, context, next) {
|
|
12
|
+
if (specifier === '@imports/castle.multiplayer-3d/code/server/sceneBodies.js') return { url: 'data:text/javascript,export const readScene = () => globalThis.__hubTestActors;', shortCircuit: true };
|
|
13
|
+
if (context.parentURL?.includes('/kits/base/sdk/') && specifier.startsWith('.') && !specifier.endsWith('.js')) return next(specifier + '.js', context);
|
|
14
|
+
return next(specifier, context);
|
|
15
|
+
}
|
|
16
|
+
`)}`);
|
|
17
|
+
const { layer } = await import('../code/server/hub.js');
|
|
18
|
+
const { readManifest } = await import('../code/server/manifest.js');
|
|
19
|
+
const Wallet = await import('../code/server/wallet.js');
|
|
20
|
+
Wallet.tuning.retryMs = [0, 0, 0];
|
|
21
|
+
|
|
22
|
+
const CONFLICT = 'CAULDRON_STORAGE_REVISION_CONFLICT';
|
|
23
|
+
const STALE_RESET = 'CAULDRON_STORAGE_STALE_RESET_GENERATION';
|
|
24
|
+
const UNAVAILABLE = 'CAULDRON_STORAGE_UNAVAILABLE';
|
|
25
|
+
const saveError = (code, extra = {}) => Object.assign(new Error(code), { code, committed: false, retryable: false, ...extra });
|
|
26
|
+
const copy = (v) => JSON.parse(JSON.stringify(v));
|
|
27
|
+
const deferred = () => { let resolve; const promise = new Promise((r) => { resolve = r; }); return { promise, resolve }; };
|
|
28
|
+
|
|
29
|
+
// The platform's deck storage, shared by every server of the deck.
|
|
30
|
+
function makeStore({ flag = true } = {}) {
|
|
31
|
+
const st = {
|
|
32
|
+
flag,
|
|
33
|
+
targets: new Map(), // prefix -> { revision, rows }
|
|
34
|
+
receipts: new Map(), // operationId -> { bytes, receipt }
|
|
35
|
+
generations: new Map(),
|
|
36
|
+
access: 'access-1',
|
|
37
|
+
rev: 0,
|
|
38
|
+
commits: 0,
|
|
39
|
+
writes: [], // the bytes of every conditionalWrite call
|
|
40
|
+
sets: 0,
|
|
41
|
+
conflicts: 0,
|
|
42
|
+
loseReply: 0, // commit, then throw an unknown outcome
|
|
43
|
+
failSnapshot: 0,
|
|
44
|
+
operationAnswers: true,
|
|
45
|
+
beforeWrite: null, // async (input) => void, before comparing
|
|
46
|
+
beforeRefuse: null, // async (input) => void, a conflict's reply held back
|
|
47
|
+
};
|
|
48
|
+
const view = (target) => {
|
|
49
|
+
const t = st.targets.get(target.prefix);
|
|
50
|
+
return { target: { ...target }, revision: t?.revision ?? null, rows: copy(t?.rows ?? {}), complete: true };
|
|
51
|
+
};
|
|
52
|
+
st.rowOf = (prefix, key) => st.targets.get(prefix)?.rows[key];
|
|
53
|
+
// Another writer (or an ordinary admin edit) changes a row and its revision.
|
|
54
|
+
st.poke = (prefix, rows) => {
|
|
55
|
+
st.targets.set(prefix, { revision: `r${++st.rev}`, rows: copy(rows) });
|
|
56
|
+
};
|
|
57
|
+
// An administrator's progress reset of an account, as ghost-server does it
|
|
58
|
+
// for a door deck (cauldronDeckProgressReset.ts): a new generation, and no
|
|
59
|
+
// deck-scope row deleted -- deck cleanup is only a town's or the house
|
|
60
|
+
// deck's own key lists, which have no wallet prefix.
|
|
61
|
+
st.reset = (userId) => {
|
|
62
|
+
st.generations.set(userId, (st.generations.get(userId) ?? 0) + 1);
|
|
63
|
+
};
|
|
64
|
+
st.storage = () => {
|
|
65
|
+
const deckScope = {
|
|
66
|
+
async get() { return {}; },
|
|
67
|
+
async set() { st.sets += 1; },
|
|
68
|
+
async remove() { st.sets += 1; },
|
|
69
|
+
};
|
|
70
|
+
return {
|
|
71
|
+
deck: deckScope,
|
|
72
|
+
async resetGeneration(userId) {
|
|
73
|
+
assert.match(userId, /^\d+$/);
|
|
74
|
+
return st.generations.get(userId) ?? 0;
|
|
75
|
+
},
|
|
76
|
+
async snapshot({ targets }) {
|
|
77
|
+
if (st.failSnapshot > 0) {
|
|
78
|
+
st.failSnapshot -= 1;
|
|
79
|
+
throw Object.assign(new Error('timeout'), { code: 'CAULDRON_STORAGE_TIMEOUT', committed: 'unknown', retryable: true });
|
|
80
|
+
}
|
|
81
|
+
return { access: st.access, targets: targets.map(({ target }) => view(target)) };
|
|
82
|
+
},
|
|
83
|
+
async recentOperations({ limit = 16 } = {}) {
|
|
84
|
+
if (!st.flag) {throw saveError(UNAVAILABLE);}
|
|
85
|
+
return { operations: [...st.receipts.entries()].slice(-limit).map(([operationId, r]) => ({ operationId, committedAt: '', receipt: copy(r.receipt) })), cursor: null };
|
|
86
|
+
},
|
|
87
|
+
async operation(id) {
|
|
88
|
+
if (!st.operationAnswers) {throw Object.assign(new Error('offline'), { code: 'CAULDRON_TRANSPORT', committed: 'unknown', retryable: true });}
|
|
89
|
+
const r = st.receipts.get(id);
|
|
90
|
+
return r ? { state: 'committed', receipt: copy(r.receipt) } : { state: 'unknown' };
|
|
91
|
+
},
|
|
92
|
+
async conditionalWrite(input) {
|
|
93
|
+
const bytes = JSON.stringify(input);
|
|
94
|
+
st.writes.push(bytes);
|
|
95
|
+
if (!st.flag) {throw saveError(UNAVAILABLE);}
|
|
96
|
+
const known = st.receipts.get(input.operationId);
|
|
97
|
+
if (known) {
|
|
98
|
+
assert.equal(bytes, known.bytes, 'an operation id is only ever replayed with identical bytes');
|
|
99
|
+
if (st.loseReply > 0) {
|
|
100
|
+
st.loseReply -= 1;
|
|
101
|
+
throw Object.assign(new Error('socket closed'), { code: 'CAULDRON_TRANSPORT', committed: 'unknown', retryable: true });
|
|
102
|
+
}
|
|
103
|
+
return copy(known.receipt);
|
|
104
|
+
}
|
|
105
|
+
await st.beforeWrite?.(input);
|
|
106
|
+
assert.ok(input.changes.length >= 1 && input.changes.length <= 8);
|
|
107
|
+
for (const fence of input.resetFences ?? []) {
|
|
108
|
+
const now = st.generations.get(fence.playerId) ?? 0;
|
|
109
|
+
if (now !== fence.resetGeneration) {
|
|
110
|
+
throw saveError(STALE_RESET, { playerId: fence.playerId, expectedResetGeneration: fence.resetGeneration, resetGeneration: now });
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
for (const change of input.changes) {
|
|
114
|
+
if ((st.targets.get(change.target.prefix)?.revision ?? null) !== change.expectedRevision) {
|
|
115
|
+
st.conflicts += 1;
|
|
116
|
+
const current = { access: st.access, targets: input.changes.map((c) => view(c.target)) };
|
|
117
|
+
await st.beforeRefuse?.(input);
|
|
118
|
+
throw saveError(CONFLICT, { target: change.target, current });
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
const revisions = input.changes.map((change) => {
|
|
122
|
+
const t = st.targets.get(change.target.prefix) ?? { rows: {} };
|
|
123
|
+
const rows = change.replace ? copy(change.replace) : { ...t.rows, ...copy(change.put ?? {}) };
|
|
124
|
+
for (const k of change.remove ?? []) {delete rows[k];}
|
|
125
|
+
const revision = `r${++st.rev}`;
|
|
126
|
+
st.targets.set(change.target.prefix, { revision, rows });
|
|
127
|
+
return { target: { ...change.target }, revision };
|
|
128
|
+
});
|
|
129
|
+
const receipt = { operationId: input.operationId, access: st.access, revisions };
|
|
130
|
+
st.receipts.set(input.operationId, { bytes, receipt });
|
|
131
|
+
st.commits += 1;
|
|
132
|
+
if (st.loseReply > 0) {
|
|
133
|
+
st.loseReply -= 1;
|
|
134
|
+
throw Object.assign(new Error('socket closed'), { code: 'CAULDRON_TRANSPORT', committed: 'unknown', retryable: true });
|
|
135
|
+
}
|
|
136
|
+
return copy(receipt);
|
|
137
|
+
},
|
|
138
|
+
};
|
|
139
|
+
};
|
|
140
|
+
return st;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const PLAYERS = [
|
|
144
|
+
{ playerId: 'p1', userId: '101', username: 'ada', isAnonymous: false },
|
|
145
|
+
{ playerId: 'p2', userId: '102', username: 'bo', isAnonymous: false },
|
|
146
|
+
{ playerId: 'p3', userId: 'anon-3', username: 'guest', isAnonymous: true },
|
|
147
|
+
];
|
|
148
|
+
const castleJson = (hubMore = {}) => ({ deckId: 'saved-test', server: { maxPlayers: 8 }, hub: { contract: 1, title: 'Shop', ...hubMore } });
|
|
149
|
+
const PREFIX = 'wallet:coins:101:';
|
|
150
|
+
const KEY = `${PREFIX}state`;
|
|
151
|
+
|
|
152
|
+
// One server of the deck: a hub layer with its own session over `storage`.
|
|
153
|
+
function server({ store = null, storage = store?.storage(), hub = { wallet: { name: 'coins', start: 30 } }, sessionId = 's-1' } = {}) {
|
|
154
|
+
globalThis.__hubTestActors = [{ id: 'player', components: { Player: {}, Transform: { x: 0, y: 1, z: 0 } } }];
|
|
155
|
+
let ctx = null;
|
|
156
|
+
const srv = layer(castleJson(hub), { ready(h) { ctx = h; } }, {}, () => true, () => null, null, {});
|
|
157
|
+
const sent = [];
|
|
158
|
+
const session = {
|
|
159
|
+
sessionId,
|
|
160
|
+
players: PLAYERS,
|
|
161
|
+
storage,
|
|
162
|
+
send: (id, msg) => sent.push([id, structuredClone(msg)]),
|
|
163
|
+
broadcast: (msg) => sent.push(['all', structuredClone(msg)]),
|
|
164
|
+
};
|
|
165
|
+
const world = { timeMs: 0, scenePath: 'scenes/main.scene', objects: new Map(), bodies: new Map(), players: new Map(PLAYERS.map((p) => [p.playerId, { at: { x: 0, y: 1, z: 0 }, facing: 0, body: { setTranslation() {} } }])) };
|
|
166
|
+
srv.sceneFor(session);
|
|
167
|
+
srv.ready(world);
|
|
168
|
+
return { srv, session, world, sent, ctx, wallet: ctx.wallet };
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// --- Review regressions (each failed on f5191715b's wallet.js) ---------------
|
|
172
|
+
// WALLET_ONLY=<name> runs just that one.
|
|
173
|
+
const direct = (store) => Wallet.createWallet({
|
|
174
|
+
name: 'coins',
|
|
175
|
+
start: 30,
|
|
176
|
+
storage: () => store.storage(),
|
|
177
|
+
identify: (id) => (id === 'p1' ? { stored: true, userId: '101' } : null),
|
|
178
|
+
});
|
|
179
|
+
const REGRESSIONS = {
|
|
180
|
+
// A progress reset deletes no door-deck row: the wallet written before it
|
|
181
|
+
// must read as a fresh one afterwards, and is never carried into the new
|
|
182
|
+
// generation.
|
|
183
|
+
async resetSurvival() {
|
|
184
|
+
const store = makeStore();
|
|
185
|
+
const w = direct(store);
|
|
186
|
+
assert.deepEqual(await w.api.seed('p1', { coins: 700, owned: ['boat'] }), { ok: true, coins: 700 });
|
|
187
|
+
store.reset('101');
|
|
188
|
+
assert.deepEqual(await direct(store).api.read('p1'), { coins: 30, owned: [], data: {} }, 'another server reads the reset wallet');
|
|
189
|
+
w.reset('101');
|
|
190
|
+
assert.deepEqual(await w.api.read('p1'), { coins: 30, owned: [], data: {} });
|
|
191
|
+
assert.deepEqual(await w.api.earn('p1', 1), { ok: true, coins: 31 });
|
|
192
|
+
assert.deepEqual(store.rowOf(PREFIX, KEY), { v: 1, gen: 1, coins: 31, owned: [], data: {} });
|
|
193
|
+
// Even without the kit's hook, a server holding the old wallet is fenced.
|
|
194
|
+
const old = direct(store);
|
|
195
|
+
assert.deepEqual(await old.api.read('p1'), { coins: 31, owned: [], data: {} });
|
|
196
|
+
store.reset('101');
|
|
197
|
+
assert.deepEqual(await old.api.earn('p1', 5), { ok: false, why: 'busy' }, 'its fence refuses the write');
|
|
198
|
+
assert.deepEqual(await old.api.earn('p1', 5), { ok: true, coins: 35 }, 'reads generation 2: the gen-1 row is stale');
|
|
199
|
+
assert.deepEqual(store.rowOf(PREFIX, KEY), { v: 1, gen: 2, coins: 35, owned: [], data: {} });
|
|
200
|
+
},
|
|
201
|
+
// A conflict reply held back across a reset: the operation is abandoned,
|
|
202
|
+
// not re-applied against the reset wallet.
|
|
203
|
+
async conflictAcrossReset() {
|
|
204
|
+
const store = makeStore();
|
|
205
|
+
const a = direct(store);
|
|
206
|
+
const b = direct(store);
|
|
207
|
+
await a.api.read('p1'); // no row yet
|
|
208
|
+
assert.deepEqual(await b.api.earn('p1', 1), { ok: true, coins: 31 });
|
|
209
|
+
const gate = deferred();
|
|
210
|
+
const reached = deferred();
|
|
211
|
+
store.beforeRefuse = async () => { reached.resolve(); await gate.promise; };
|
|
212
|
+
const seeding = a.api.seed('p1', { coins: 900, owned: ['old'] });
|
|
213
|
+
await reached.promise;
|
|
214
|
+
store.reset('101');
|
|
215
|
+
a.reset('101');
|
|
216
|
+
store.beforeRefuse = null;
|
|
217
|
+
gate.resolve();
|
|
218
|
+
assert.deepEqual(await seeding, { ok: false, why: 'busy' });
|
|
219
|
+
assert.equal(store.rowOf(PREFIX, KEY).coins, 31, 'the old seed did not land');
|
|
220
|
+
assert.deepEqual(await a.api.read('p1'), { coins: 30, owned: [], data: {} });
|
|
221
|
+
},
|
|
222
|
+
// A call queued before a reset never runs after it.
|
|
223
|
+
async queuedAcrossReset() {
|
|
224
|
+
const store = makeStore();
|
|
225
|
+
const w = direct(store);
|
|
226
|
+
await w.api.read('p1');
|
|
227
|
+
const gate = deferred();
|
|
228
|
+
const reached = deferred();
|
|
229
|
+
store.beforeWrite = async () => { reached.resolve(); await gate.promise; };
|
|
230
|
+
const earning = w.api.earn('p1', 1);
|
|
231
|
+
const seeding = w.api.seed('p1', { coins: 800, owned: ['old'] });
|
|
232
|
+
await reached.promise;
|
|
233
|
+
store.reset('101');
|
|
234
|
+
w.reset('101');
|
|
235
|
+
store.beforeWrite = null;
|
|
236
|
+
gate.resolve();
|
|
237
|
+
assert.deepEqual(await earning, { ok: false, why: 'busy' });
|
|
238
|
+
assert.deepEqual(await seeding, { ok: false, why: 'busy' });
|
|
239
|
+
assert.equal(store.rowOf(PREFIX, KEY), undefined, 'neither old call wrote');
|
|
240
|
+
assert.deepEqual(await w.api.read('p1'), { coins: 30, owned: [], data: {} });
|
|
241
|
+
},
|
|
242
|
+
// While a write's outcome is unknown, no balance is reported.
|
|
243
|
+
async readWhileUnsettled() {
|
|
244
|
+
const store = makeStore();
|
|
245
|
+
const w = direct(store);
|
|
246
|
+
await w.api.seed('p1', { coins: 50 });
|
|
247
|
+
store.loseReply = 10;
|
|
248
|
+
store.operationAnswers = false;
|
|
249
|
+
assert.deepEqual(await w.api.spend('p1', { cost: 10 }), { ok: false, why: 'busy' });
|
|
250
|
+
assert.equal(store.rowOf(PREFIX, KEY).coins, 40, 'it committed');
|
|
251
|
+
store.loseReply = 10;
|
|
252
|
+
assert.equal(await w.api.read('p1'), null, 'not the stale 50');
|
|
253
|
+
store.loseReply = 0;
|
|
254
|
+
assert.deepEqual(await w.api.read('p1'), { coins: 40, owned: [], data: {} });
|
|
255
|
+
},
|
|
256
|
+
// A call admitted while the first load is holding its snapshot (generation
|
|
257
|
+
// 0 already read), then a platform reset the kit's hook never saw: the
|
|
258
|
+
// load's second read sees generation 1 and retries; the queued call was
|
|
259
|
+
// bound to 0 and answers busy without writing.
|
|
260
|
+
async admittedDuringFirstLoad() {
|
|
261
|
+
const store = makeStore();
|
|
262
|
+
store.poke(PREFIX, { [KEY]: { v: 1, gen: 0, coins: 5, owned: [], data: {} } });
|
|
263
|
+
const api = store.storage();
|
|
264
|
+
const gate = deferred();
|
|
265
|
+
const reached = deferred();
|
|
266
|
+
let held = true;
|
|
267
|
+
const wrapped = { ...api, async snapshot(input) {
|
|
268
|
+
if (held) { held = false; reached.resolve(); await gate.promise; }
|
|
269
|
+
return api.snapshot(input);
|
|
270
|
+
} };
|
|
271
|
+
const w2 = Wallet.createWallet({ name: 'coins', start: 30, storage: () => wrapped, identify: (id) => (id === 'p1' ? { stored: true, userId: '101' } : null) });
|
|
272
|
+
const reading = w2.api.read('p1');
|
|
273
|
+
await reached.promise;
|
|
274
|
+
const seeding = w2.api.seed('p1', { coins: 900, owned: ['old'] });
|
|
275
|
+
store.reset('101');
|
|
276
|
+
gate.resolve();
|
|
277
|
+
assert.deepEqual(await reading, { coins: 30, owned: [], data: {} }, 'the load retried at generation 1');
|
|
278
|
+
assert.deepEqual(await seeding, { ok: false, why: 'busy' });
|
|
279
|
+
assert.deepEqual(store.rowOf(PREFIX, KEY), { v: 1, gen: 0, coins: 5, owned: [], data: {} }, 'nothing written');
|
|
280
|
+
assert.deepEqual(await w2.api.seed('p1', { coins: 900, owned: ['old'] }), { ok: true, coins: 900 }, 'a call made after the reset was seen is bound to 1');
|
|
281
|
+
},
|
|
282
|
+
// The reset falls between the two generation reads of the call's own first
|
|
283
|
+
// load: the call is bound to the first (0) and answers busy.
|
|
284
|
+
async resetBetweenGenerationReads() {
|
|
285
|
+
const store = makeStore();
|
|
286
|
+
const api = store.storage();
|
|
287
|
+
let reads = 0;
|
|
288
|
+
const wrapped = { ...api, async resetGeneration(userId) {
|
|
289
|
+
const value = await api.resetGeneration(userId);
|
|
290
|
+
reads += 1;
|
|
291
|
+
if (reads === 1) {store.reset('101');}
|
|
292
|
+
return value;
|
|
293
|
+
} };
|
|
294
|
+
const w = Wallet.createWallet({ name: 'coins', start: 30, storage: () => wrapped, identify: (id) => (id === 'p1' ? { stored: true, userId: '101' } : null) });
|
|
295
|
+
assert.deepEqual(await w.api.seed('p1', { coins: 900, owned: ['old'] }), { ok: false, why: 'busy' });
|
|
296
|
+
assert.equal(store.rowOf(PREFIX, KEY), undefined, 'nothing written');
|
|
297
|
+
assert.deepEqual(await w.api.seed('p1', { coins: 900, owned: ['old'] }), { ok: true, coins: 900 });
|
|
298
|
+
assert.equal(store.rowOf(PREFIX, KEY).gen, 1);
|
|
299
|
+
},
|
|
300
|
+
// The writer lease's fence runs before every write: a lost writer locks the
|
|
301
|
+
// account with nothing sent; a fence that only failed leaves the intent to
|
|
302
|
+
// send again.
|
|
303
|
+
async writerFence() {
|
|
304
|
+
const store = makeStore();
|
|
305
|
+
let fail = null;
|
|
306
|
+
const w = Wallet.createWallet({ name: 'coins', start: 30, storage: () => store.storage(), identify: (id) => (id === 'p1' ? { stored: true, userId: '101' } : null),
|
|
307
|
+
fence: async (userId) => { assert.equal(userId, '101'); if (fail) {throw fail;} } });
|
|
308
|
+
assert.deepEqual(await w.api.earn('p1', 1), { ok: true, coins: 31 });
|
|
309
|
+
fail = Object.assign(new Error('blip'), { code: 'CAULDRON_STORAGE_UNAVAILABLE', retryable: true });
|
|
310
|
+
assert.deepEqual(await w.api.earn('p1', 2), { ok: false, why: 'busy' });
|
|
311
|
+
assert.equal(store.rowOf(PREFIX, KEY).coins, 31, 'nothing sent past a failed fence');
|
|
312
|
+
fail = null;
|
|
313
|
+
assert.deepEqual(await w.api.earn('p1', 4), { ok: true, coins: 37 }, 'the held intent is sent first');
|
|
314
|
+
fail = Object.assign(new Error('lost'), { code: 'CAULDRON_STORAGE_WRITER_LOST' });
|
|
315
|
+
const lost = await w.api.earn('p1', 5);
|
|
316
|
+
assert.equal(lost.ok, false);
|
|
317
|
+
assert.notEqual(lost.why, 'busy', 'a lost writer is locked, not busy');
|
|
318
|
+
assert.equal(store.rowOf(PREFIX, KEY).coins, 37, 'a lost writer writes nothing');
|
|
319
|
+
// The lease is lost between a refused attempt and its retry: the retry
|
|
320
|
+
// waits on the fence too, and sends nothing.
|
|
321
|
+
fail = null;
|
|
322
|
+
let fences = 0;
|
|
323
|
+
const w2 = Wallet.createWallet({ name: 'coins', start: 30, storage: () => store.storage(), identify: (id) => (id === 'p1' ? { stored: true, userId: '101' } : null),
|
|
324
|
+
fence: async () => { fences += 1; if (fail) {throw fail;} } });
|
|
325
|
+
store.beforeWrite = () => {
|
|
326
|
+
store.beforeWrite = null;
|
|
327
|
+
fail = Object.assign(new Error('lost'), { code: 'CAULDRON_STORAGE_WRITER_LOST' });
|
|
328
|
+
throw Object.assign(new Error('slow down'), { code: 'CAULDRON_STORAGE_RATE_LIMITED', committed: false, retryable: true });
|
|
329
|
+
};
|
|
330
|
+
const retried = await w2.api.earn('p1', 3);
|
|
331
|
+
assert.equal(retried.why, 'locked', JSON.stringify(retried));
|
|
332
|
+
assert.equal(fences, 2, 'the retry was fenced');
|
|
333
|
+
assert.equal(store.rowOf(PREFIX, KEY).coins, 37, 'the retry sent nothing');
|
|
334
|
+
},
|
|
335
|
+
};
|
|
336
|
+
if (process.env.WALLET_ONLY) {
|
|
337
|
+
await REGRESSIONS[process.env.WALLET_ONLY]();
|
|
338
|
+
console.log(`test-wallet ${process.env.WALLET_ONLY}: ok`);
|
|
339
|
+
process.exit(0);
|
|
340
|
+
}
|
|
341
|
+
for (const run of Object.values(REGRESSIONS)) {
|
|
342
|
+
await run();
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
// --- Manifest ---------------------------------------------------------------
|
|
346
|
+
{
|
|
347
|
+
const read = (hub) => readManifest(castleJson(hub), () => true);
|
|
348
|
+
assert.equal(Object.hasOwn(read({}).manifest, 'wallet'), false, 'absent: no wallet in the manifest');
|
|
349
|
+
assert.deepEqual(read({ wallet: { name: 'coins' } }).manifest.wallet, { name: 'coins', start: 0 });
|
|
350
|
+
assert.deepEqual(read({ wallet: { name: 'shop_2-x', start: 50 } }).manifest.wallet, { name: 'shop_2-x', start: 50 });
|
|
351
|
+
assert.deepEqual(read({ wallet: { name: 'c', start: 0 } }).manifest.wallet, { name: 'c', start: 0 });
|
|
352
|
+
for (const wallet of [true, 'coins', null, [], {}, { name: 'Coins' }, { name: '1coins' }, { name: 'a'.repeat(25) }, { name: '' },
|
|
353
|
+
{ name: 'coins', start: -1 }, { name: 'coins', start: 1.5 }, { name: 'coins', start: '5' }, { name: 'coins', start: 2 ** 53 },
|
|
354
|
+
{ name: 'coins', extra: 1 }]) {
|
|
355
|
+
const out = read({ wallet });
|
|
356
|
+
assert.equal(out.manifest, null, JSON.stringify(wallet));
|
|
357
|
+
assert.ok(out.errors.some((e) => e.startsWith('hub.wallet')), JSON.stringify(out.errors));
|
|
358
|
+
}
|
|
359
|
+
assert.deepEqual(read({ wallet: { name: 'coins', extra: 1 } }).errors, ['hub.wallet.extra is not part of the hub contract v1.']);
|
|
360
|
+
// A place does not declare one: the deck does.
|
|
361
|
+
const placed = readManifest({ ...castleJson({ places: { room: { title: 'Room', scene: 'scenes/room.scene', wallet: { name: 'coins' } } } }) }, () => true);
|
|
362
|
+
assert.equal(placed.manifest, null);
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
// --- Absent: no API, and the same join as before ------------------------------
|
|
366
|
+
{
|
|
367
|
+
const store = makeStore();
|
|
368
|
+
const without = server({ store, hub: {} });
|
|
369
|
+
assert.equal(without.ctx.wallet, undefined);
|
|
370
|
+
assert.equal('wallet' in without.ctx, false);
|
|
371
|
+
const withIt = server({ store, hub: { wallet: { name: 'coins' } } });
|
|
372
|
+
assert.equal(typeof withIt.ctx.wallet.spend, 'function');
|
|
373
|
+
assert.deepEqual(Object.keys(withIt.ctx).filter((k) => k !== 'wallet'), Object.keys(without.ctx));
|
|
374
|
+
for (const s of [without, withIt]) {
|
|
375
|
+
s.srv.message(s.session, { playerId: 'p1' }, { k: 'hub-hello', avatar: 0, party: 1 });
|
|
376
|
+
s.world.timeMs += 50; s.srv.place(s.world); s.srv.step(s.world, []);
|
|
377
|
+
}
|
|
378
|
+
assert.equal(JSON.stringify(withIt.srv.join()), JSON.stringify(without.srv.join()), 'the wallet is never on the wire');
|
|
379
|
+
assert.equal(JSON.stringify(withIt.sent), JSON.stringify(without.sent));
|
|
380
|
+
assert.equal(store.writes.length, 0);
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
// --- Read, seed, spend, earn ------------------------------------------------
|
|
384
|
+
{
|
|
385
|
+
const store = makeStore();
|
|
386
|
+
const { wallet } = server({ store });
|
|
387
|
+
assert.deepEqual(await wallet.read('p1'), { coins: 30, owned: [], data: {} }, 'a new account starts at `start`');
|
|
388
|
+
assert.equal(store.rowOf(PREFIX, KEY), undefined, 'a read writes nothing');
|
|
389
|
+
assert.deepEqual(await wallet.seed('p1', { coins: 40, owned: ['hat'] }), { ok: true, coins: 40 });
|
|
390
|
+
assert.equal(JSON.parse(store.writes.at(-1)).changes[0].expectedRevision, null, 'seed creates the row');
|
|
391
|
+
assert.deepEqual(await wallet.seed('p1', { coins: 999, owned: [] }), { ok: false, why: 'seeded' });
|
|
392
|
+
assert.deepEqual(await wallet.spend('p1', { cost: 10, add: 'cape', data: { treatDay: 7 } }), { ok: true, coins: 30 });
|
|
393
|
+
assert.deepEqual(await wallet.spend('p1', { cost: 1, add: 'cape' }), { ok: false, why: 'owned' });
|
|
394
|
+
assert.deepEqual(await wallet.spend('p1', { cost: 31 }), { ok: false, why: 'short' });
|
|
395
|
+
assert.deepEqual(await wallet.earn('p1', 5, { data: { streak: 2 } }), { ok: true, coins: 35 });
|
|
396
|
+
assert.deepEqual(await wallet.read('p1'), { coins: 35, owned: ['hat', 'cape'], data: { treatDay: 7, streak: 2 } });
|
|
397
|
+
assert.deepEqual(store.rowOf(PREFIX, KEY), { v: 1, gen: 0, coins: 35, owned: ['hat', 'cape'], data: { treatDay: 7, streak: 2 } });
|
|
398
|
+
// Every write is conditional and fenced by the generation read before the snapshot.
|
|
399
|
+
for (const bytes of store.writes) {
|
|
400
|
+
const w = JSON.parse(bytes);
|
|
401
|
+
assert.equal(w.expectedAccess, 'access-1');
|
|
402
|
+
assert.deepEqual(w.resetFences, [{ playerId: '101', resetGeneration: 0 }]);
|
|
403
|
+
assert.deepEqual(w.changes[0].target, { scope: 'deck', prefix: PREFIX });
|
|
404
|
+
assert.deepEqual(Object.keys(w.changes[0].put), [KEY]);
|
|
405
|
+
}
|
|
406
|
+
assert.equal(new Set(store.writes.map((b) => JSON.parse(b).operationId)).size, store.writes.length, 'a fresh operation id each');
|
|
407
|
+
assert.equal(store.sets, 0);
|
|
408
|
+
// Earning a fresh account creates it; a later seed is refused.
|
|
409
|
+
assert.deepEqual(await wallet.earn('p2', 3), { ok: true, coins: 33 });
|
|
410
|
+
assert.deepEqual(await wallet.seed('p2', { coins: 100 }), { ok: false, why: 'seeded' });
|
|
411
|
+
// Bad arguments are the caller's bug.
|
|
412
|
+
await assert.rejects(wallet.spend('p1', { cost: -1 }), TypeError);
|
|
413
|
+
await assert.rejects(wallet.spend('p1', { cost: 1, add: '' }), TypeError);
|
|
414
|
+
await assert.rejects(wallet.earn('p1', 1.5), TypeError);
|
|
415
|
+
await assert.rejects(wallet.seed('p1', { coins: 1, owned: 'hat' }), TypeError);
|
|
416
|
+
await assert.rejects(wallet.spend('p1', { cost: 1, data: { big: 'x'.repeat(5000) } }), TypeError);
|
|
417
|
+
assert.equal(await wallet.read('nobody'), null);
|
|
418
|
+
assert.deepEqual(await wallet.spend('nobody', { cost: 1 }), { ok: false, why: 'locked' });
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
// --- Two servers race a spend and an earn: neither is lost -------------------
|
|
422
|
+
{
|
|
423
|
+
const store = makeStore();
|
|
424
|
+
const a = server({ store, sessionId: 'room-a' }).wallet;
|
|
425
|
+
const b = server({ store, sessionId: 'room-b' }).wallet;
|
|
426
|
+
await a.read('p1');
|
|
427
|
+
await b.read('p1');
|
|
428
|
+
const [spent, earned] = await Promise.all([a.spend('p1', { cost: 10, add: 'sword' }), b.earn('p1', 7)]);
|
|
429
|
+
assert.equal(spent.ok, true);
|
|
430
|
+
assert.equal(earned.ok, true);
|
|
431
|
+
assert.ok(store.conflicts >= 1, 'one of them met the other');
|
|
432
|
+
assert.deepEqual(await a.read('p1'), { coins: 27, owned: ['sword'], data: {} });
|
|
433
|
+
assert.deepEqual(await b.read('p1'), { coins: 27, owned: ['sword'], data: {} });
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
// --- Through the kit's storage queue (`storageLimit.js`), as a host session --
|
|
437
|
+
{
|
|
438
|
+
const { limitSession } = await import('../code/server/storageLimit.js');
|
|
439
|
+
const store = makeStore();
|
|
440
|
+
const limited = (id) => limitSession({ sessionId: id, storage: store.storage() }).storage;
|
|
441
|
+
const a = server({ store, storage: limited('room-a'), sessionId: 'room-a' }).wallet;
|
|
442
|
+
const b = server({ store, storage: limited('room-b'), sessionId: 'room-b' }).wallet;
|
|
443
|
+
const results = await Promise.all([a.spend('p1', { cost: 4, add: 'x' }), b.spend('p1', { cost: 4, add: 'y' }), a.earn('p1', 2), b.earn('p1', 2)]);
|
|
444
|
+
assert.ok(results.every((r) => r.ok), JSON.stringify(results));
|
|
445
|
+
assert.deepEqual(await a.read('p1'), { coins: 26, owned: store.rowOf(PREFIX, KEY).owned, data: {} });
|
|
446
|
+
assert.deepEqual([...store.rowOf(PREFIX, KEY).owned].sort(), ['x', 'y']);
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
// --- A stale whole read cannot erase a purchase -------------------------------
|
|
450
|
+
{
|
|
451
|
+
const store = makeStore();
|
|
452
|
+
const a = server({ store, sessionId: 'room-a' }).wallet;
|
|
453
|
+
const b = server({ store, sessionId: 'room-b' }).wallet;
|
|
454
|
+
assert.deepEqual(await b.read('p1'), { coins: 30, owned: [], data: {} }); // room B holds the old wallet
|
|
455
|
+
assert.deepEqual(await a.spend('p1', { cost: 20, add: 'boat' }), { ok: true, coins: 10 });
|
|
456
|
+
// Room B's next change is built on its stale copy: refused, adopted, applied again.
|
|
457
|
+
assert.deepEqual(await b.earn('p1', 1, { data: { visits: 2 } }), { ok: true, coins: 11 });
|
|
458
|
+
assert.deepEqual(store.rowOf(PREFIX, KEY), { v: 1, gen: 0, coins: 11, owned: ['boat'], data: { visits: 2 } });
|
|
459
|
+
assert.equal(store.sets, 0);
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
// --- A conflict re-applies once against the stored wallet: still short -------
|
|
463
|
+
{
|
|
464
|
+
const store = makeStore();
|
|
465
|
+
const a = server({ store, sessionId: 'room-a' }).wallet;
|
|
466
|
+
const b = server({ store, sessionId: 'room-b' }).wallet;
|
|
467
|
+
await b.read('p1'); // 30
|
|
468
|
+
assert.deepEqual(await a.spend('p1', { cost: 25 }), { ok: true, coins: 5 });
|
|
469
|
+
const writes = store.writes.length;
|
|
470
|
+
assert.deepEqual(await b.spend('p1', { cost: 10, add: 'kite' }), { ok: false, why: 'short' });
|
|
471
|
+
assert.equal(store.writes.length, writes + 1, 'one refused write, nothing after the short re-apply');
|
|
472
|
+
assert.deepEqual(await b.read('p1'), { coins: 5, owned: [], data: {} });
|
|
473
|
+
// The same against an item another room bought meanwhile: owned.
|
|
474
|
+
await b.read('p1');
|
|
475
|
+
assert.deepEqual(await a.spend('p1', { cost: 1, add: 'pin' }), { ok: true, coins: 4 });
|
|
476
|
+
assert.deepEqual(await b.spend('p1', { cost: 1, add: 'pin' }), { ok: false, why: 'owned' });
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
// --- Two conflicts in a row: busy, and the next call works ------------------
|
|
480
|
+
{
|
|
481
|
+
const store = makeStore();
|
|
482
|
+
const { wallet } = server({ store });
|
|
483
|
+
await wallet.seed('p1', { coins: 50 });
|
|
484
|
+
let pokes = 2;
|
|
485
|
+
store.beforeWrite = async () => {
|
|
486
|
+
if (pokes-- > 0) {
|
|
487
|
+
const row = store.rowOf(PREFIX, KEY);
|
|
488
|
+
store.poke(PREFIX, { [KEY]: { ...row, coins: row.coins + 1 } }); // another room earns 1
|
|
489
|
+
}
|
|
490
|
+
};
|
|
491
|
+
assert.deepEqual(await wallet.spend('p1', { cost: 5 }), { ok: false, why: 'busy' });
|
|
492
|
+
assert.equal(store.rowOf(PREFIX, KEY).coins, 52, 'both other earns kept, the spend not made');
|
|
493
|
+
store.beforeWrite = null;
|
|
494
|
+
assert.deepEqual(await wallet.spend('p1', { cost: 5 }), { ok: true, coins: 47 });
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
// --- The reset fence refuses a write in flight across a reset ----------------
|
|
498
|
+
{
|
|
499
|
+
const store = makeStore();
|
|
500
|
+
const { wallet } = server({ store });
|
|
501
|
+
await wallet.seed('p1', { coins: 80, owned: ['crown'] });
|
|
502
|
+
const gate = deferred();
|
|
503
|
+
const reached = deferred();
|
|
504
|
+
store.beforeWrite = async () => { reached.resolve(); await gate.promise; };
|
|
505
|
+
const spending = wallet.spend('p1', { cost: 10, add: 'ring' });
|
|
506
|
+
await reached.promise;
|
|
507
|
+
store.reset('101'); // an administrator resets the account while the write is on the wire
|
|
508
|
+
store.beforeWrite = null;
|
|
509
|
+
gate.resolve();
|
|
510
|
+
assert.deepEqual(await spending, { ok: false, why: 'busy' });
|
|
511
|
+
assert.deepEqual(store.rowOf(PREFIX, KEY), { v: 1, gen: 0, coins: 80, owned: ['crown'], data: {} }, 'the refused write changed nothing');
|
|
512
|
+
assert.deepEqual(await wallet.read('p1'), { coins: 30, owned: [], data: {} }, 'the cache was dropped: the reset wallet');
|
|
513
|
+
assert.deepEqual(await wallet.spend('p1', { cost: 10 }), { ok: true, coins: 20 });
|
|
514
|
+
assert.deepEqual(JSON.parse(store.writes.at(-1)).resetFences, [{ playerId: '101', resetGeneration: 1 }]);
|
|
515
|
+
assert.deepEqual(store.rowOf(PREFIX, KEY), { v: 1, gen: 1, coins: 20, owned: [], data: {} });
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
// --- A lost reply: the operation is looked up, or resent with identical bytes -
|
|
519
|
+
{
|
|
520
|
+
const store = makeStore();
|
|
521
|
+
const { wallet } = server({ store });
|
|
522
|
+
await wallet.seed('p1', { coins: 50 });
|
|
523
|
+
// The reply is lost after the commit; operation(id) finds the receipt.
|
|
524
|
+
store.loseReply = 1;
|
|
525
|
+
const commits = store.commits;
|
|
526
|
+
assert.deepEqual(await wallet.spend('p1', { cost: 10, add: 'hat' }), { ok: true, coins: 40 });
|
|
527
|
+
assert.equal(store.commits, commits + 1);
|
|
528
|
+
// operation(id) cannot answer either: the identical request is sent again.
|
|
529
|
+
store.loseReply = 2;
|
|
530
|
+
store.operationAnswers = false;
|
|
531
|
+
const before = store.writes.length;
|
|
532
|
+
assert.deepEqual(await wallet.spend('p1', { cost: 5, add: 'cap' }), { ok: true, coins: 35 });
|
|
533
|
+
const sent = store.writes.slice(before);
|
|
534
|
+
assert.equal(sent.length, 3);
|
|
535
|
+
assert.equal(new Set(sent).size, 1, 'every resend carries the same id and bytes');
|
|
536
|
+
assert.equal(store.commits, commits + 2, 'committed once');
|
|
537
|
+
assert.deepEqual(store.rowOf(PREFIX, KEY), { v: 1, gen: 0, coins: 35, owned: ['hat', 'cap'], data: {} });
|
|
538
|
+
// Never settled within the resends: busy, kept, and settled first next time.
|
|
539
|
+
store.loseReply = 10;
|
|
540
|
+
assert.deepEqual(await wallet.spend('p1', { cost: 5 }), { ok: false, why: 'busy' });
|
|
541
|
+
const pendingBytes = store.writes.at(-1);
|
|
542
|
+
store.loseReply = 0;
|
|
543
|
+
assert.deepEqual(await wallet.earn('p1', 1), { ok: true, coins: 31 });
|
|
544
|
+
assert.ok(store.writes.includes(pendingBytes));
|
|
545
|
+
assert.equal(store.writes[store.writes.indexOf(pendingBytes) + 4], pendingBytes, 'the old write went again first, unchanged');
|
|
546
|
+
assert.deepEqual(store.rowOf(PREFIX, KEY).coins, 31);
|
|
547
|
+
store.operationAnswers = true;
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
// --- A failed read or an unreadable row locks; nothing is written ------------
|
|
551
|
+
{
|
|
552
|
+
const store = makeStore();
|
|
553
|
+
const { wallet } = server({ store });
|
|
554
|
+
store.failSnapshot = 1;
|
|
555
|
+
assert.equal(await wallet.read('p1'), null);
|
|
556
|
+
assert.deepEqual(await wallet.spend('p1', { cost: 1 }), { ok: false, why: 'locked' });
|
|
557
|
+
assert.deepEqual(await wallet.earn('p1', 5), { ok: false, why: 'locked' });
|
|
558
|
+
assert.deepEqual(await wallet.seed('p1', { coins: 5 }), { ok: false, why: 'locked' });
|
|
559
|
+
assert.equal(await wallet.read('p1'), null, 'locked for the visit');
|
|
560
|
+
assert.equal(store.writes.length, 0);
|
|
561
|
+
|
|
562
|
+
store.poke('wallet:coins:102:', { 'wallet:coins:102:state': 'not json{' });
|
|
563
|
+
assert.equal(await wallet.read('p2'), null);
|
|
564
|
+
assert.deepEqual(await wallet.earn('p2', 5), { ok: false, why: 'locked' });
|
|
565
|
+
store.poke('wallet:coins:102:', { 'wallet:coins:102:state': { v: 2, coins: 5 } });
|
|
566
|
+
const other = server({ store, sessionId: 'room-c' }).wallet;
|
|
567
|
+
assert.equal(await other.read('p2'), null, 'a newer version is not read as empty');
|
|
568
|
+
assert.deepEqual(await other.spend('p2', { cost: 0 }), { ok: false, why: 'locked' });
|
|
569
|
+
assert.deepEqual(store.rowOf('wallet:coins:102:', 'wallet:coins:102:state'), { v: 2, coins: 5 }, 'never overwritten');
|
|
570
|
+
assert.equal(store.writes.length, 0);
|
|
571
|
+
assert.equal(store.sets, 0);
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
// --- Flag off or no API: locked, and never an ordinary set -------------------
|
|
575
|
+
{
|
|
576
|
+
const store = makeStore({ flag: false });
|
|
577
|
+
const { wallet } = server({ store });
|
|
578
|
+
assert.equal(await wallet.read('p1'), null);
|
|
579
|
+
assert.deepEqual(await wallet.spend('p1', { cost: 1 }), { ok: false, why: 'locked' });
|
|
580
|
+
assert.deepEqual(await wallet.earn('p1', 1), { ok: false, why: 'locked' });
|
|
581
|
+
assert.deepEqual(await wallet.seed('p1', { coins: 1 }), { ok: false, why: 'locked' });
|
|
582
|
+
assert.equal(store.sets, 0, 'no ordinary set');
|
|
583
|
+
assert.equal(store.writes.length, 0);
|
|
584
|
+
// Guests keep their session wallet either way.
|
|
585
|
+
assert.deepEqual(await wallet.spend('p3', { cost: 10 }), { ok: true, coins: 20 });
|
|
586
|
+
|
|
587
|
+
// A host or the dev serve without the API at all.
|
|
588
|
+
const bare = makeStore();
|
|
589
|
+
const old = bare.storage();
|
|
590
|
+
delete old.conditionalWrite;
|
|
591
|
+
const legacy = server({ storage: old }).wallet;
|
|
592
|
+
assert.equal(await legacy.read('p1'), null);
|
|
593
|
+
assert.deepEqual(await legacy.earn('p1', 1), { ok: false, why: 'locked' });
|
|
594
|
+
assert.equal(bare.sets, 0);
|
|
595
|
+
// No storage at all.
|
|
596
|
+
const none = server({ storage: undefined }).wallet;
|
|
597
|
+
assert.equal(await none.read('p1'), null);
|
|
598
|
+
assert.deepEqual(await none.spend('p1', { cost: 1 }), { ok: false, why: 'locked' });
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
// --- Anonymous players and dev guests: a session-only wallet -----------------
|
|
602
|
+
{
|
|
603
|
+
const store = makeStore();
|
|
604
|
+
const a = server({ store }).wallet;
|
|
605
|
+
assert.deepEqual(await a.read('p3'), { coins: 30, owned: [], data: {} });
|
|
606
|
+
assert.deepEqual(await a.spend('p3', { cost: 12, add: 'hat' }), { ok: true, coins: 18 });
|
|
607
|
+
assert.deepEqual(await a.spend('p3', { cost: 1, add: 'hat' }), { ok: false, why: 'owned' });
|
|
608
|
+
assert.deepEqual(await a.seed('p3', { coins: 1 }), { ok: false, why: 'seeded' });
|
|
609
|
+
assert.deepEqual(await a.read('p3'), { coins: 18, owned: ['hat'], data: {} });
|
|
610
|
+
assert.equal(store.writes.length, 0, 'never stored');
|
|
611
|
+
const b = server({ store, sessionId: 'room-b' }).wallet;
|
|
612
|
+
assert.deepEqual(await b.read('p3'), { coins: 30, owned: [], data: {} }, 'another session starts again');
|
|
613
|
+
// A dev guest on the dev serve.
|
|
614
|
+
const dev = { playerId: 'd1', userId: 'dev-guest-1', username: 'guest', isAnonymous: false };
|
|
615
|
+
PLAYERS.push(dev);
|
|
616
|
+
try {
|
|
617
|
+
const d = server({ store, sessionId: 'dev-1' }).wallet;
|
|
618
|
+
assert.deepEqual(await d.earn('d1', 5), { ok: true, coins: 35 });
|
|
619
|
+
assert.equal(store.writes.length, 0);
|
|
620
|
+
} finally {
|
|
621
|
+
PLAYERS.pop();
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
// --- A progress reset drops the cache and a lock -----------------------------
|
|
626
|
+
{
|
|
627
|
+
const store = makeStore();
|
|
628
|
+
const w = Wallet.createWallet({
|
|
629
|
+
name: 'coins',
|
|
630
|
+
start: 30,
|
|
631
|
+
storage: () => store.storage(),
|
|
632
|
+
identify: (id) => (id === 'p1' ? { stored: true, userId: '101' } : null),
|
|
633
|
+
});
|
|
634
|
+
store.failSnapshot = 1;
|
|
635
|
+
assert.equal(await w.api.read('p1'), null);
|
|
636
|
+
w.reset('101');
|
|
637
|
+
assert.deepEqual(await w.api.read('p1'), { coins: 30, owned: [], data: {} });
|
|
638
|
+
assert.deepEqual(await w.api.spend('p1', { cost: 5 }), { ok: true, coins: 25 });
|
|
639
|
+
store.reset('101');
|
|
640
|
+
w.reset('101');
|
|
641
|
+
assert.deepEqual(await w.api.spend('p1', { cost: 5 }), { ok: true, coins: 25 }, 'built on the reset wallet');
|
|
642
|
+
await w.settled();
|
|
643
|
+
// An account whose player left is dropped once idle.
|
|
644
|
+
w.forget('101', () => false);
|
|
645
|
+
await w.settled();
|
|
646
|
+
await new Promise((r) => setImmediate(r));
|
|
647
|
+
assert.equal(w.accounts.has('101'), false);
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
console.log('test-wallet: ok');
|