@dreamboard-games/sdk 0.2.0
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/LICENSE.md +96 -0
- package/README.md +12 -0
- package/dist/HandView-ncJIVLhN.d.ts +193 -0
- package/dist/ResourceCounter-CTREyF73.d.ts +102 -0
- package/dist/ThemeProvider-fy0_QzgO.d.ts +99 -0
- package/dist/bundle-TIZcw8LB.d.ts +281 -0
- package/dist/cards-Sl3b40Mv.d.ts +13 -0
- package/dist/chunk-7YAHLYBR.js +481 -0
- package/dist/chunk-7YAHLYBR.js.map +1 -0
- package/dist/chunk-FDNZTDD6.js +8085 -0
- package/dist/chunk-FDNZTDD6.js.map +1 -0
- package/dist/chunk-GKKBPPSW.js +598 -0
- package/dist/chunk-GKKBPPSW.js.map +1 -0
- package/dist/chunk-I46YJSOD.js +1 -0
- package/dist/chunk-I46YJSOD.js.map +1 -0
- package/dist/chunk-KAELH4KC.js +104 -0
- package/dist/chunk-KAELH4KC.js.map +1 -0
- package/dist/chunk-PZ5AY32C.js +10 -0
- package/dist/chunk-PZ5AY32C.js.map +1 -0
- package/dist/chunk-T3ZKNUZ7.js +1 -0
- package/dist/chunk-T3ZKNUZ7.js.map +1 -0
- package/dist/chunk-T52J5RMF.js +1 -0
- package/dist/chunk-T52J5RMF.js.map +1 -0
- package/dist/chunk-TDSWKVZ4.js +5401 -0
- package/dist/chunk-TDSWKVZ4.js.map +1 -0
- package/dist/chunk-U5C6BONG.js +34 -0
- package/dist/chunk-U5C6BONG.js.map +1 -0
- package/dist/chunk-VDXOF4FW.js +69 -0
- package/dist/chunk-VDXOF4FW.js.map +1 -0
- package/dist/chunk-VFTAA4WO.js +115 -0
- package/dist/chunk-VFTAA4WO.js.map +1 -0
- package/dist/chunk-WN74KVNY.js +17 -0
- package/dist/chunk-WN74KVNY.js.map +1 -0
- package/dist/chunk-WYPQ3GG5.js +10990 -0
- package/dist/chunk-WYPQ3GG5.js.map +1 -0
- package/dist/components-D5ZRE2Hl.d.ts +1451 -0
- package/dist/generated/runtime/primitives.d.ts +12 -0
- package/dist/generated/runtime/primitives.js +180 -0
- package/dist/generated/runtime/primitives.js.map +1 -0
- package/dist/generated/runtime-api.d.ts +3 -0
- package/dist/generated/runtime-api.js +2 -0
- package/dist/generated/runtime-api.js.map +1 -0
- package/dist/generated/runtime.d.ts +14 -0
- package/dist/generated/runtime.js +18 -0
- package/dist/generated/runtime.js.map +1 -0
- package/dist/generated/workspace-contract.d.ts +14 -0
- package/dist/generated/workspace-contract.js +14 -0
- package/dist/generated/workspace-contract.js.map +1 -0
- package/dist/hex-board-view-D_07hO6O.d.ts +933 -0
- package/dist/hex-color-MhOyuY-o.d.ts +8 -0
- package/dist/index-BwqPQtBu.d.ts +1433 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +12 -0
- package/dist/index.js.map +1 -0
- package/dist/infrastructure/reducer-bundle-abi.d.ts +1083 -0
- package/dist/infrastructure/reducer-bundle-abi.js +14 -0
- package/dist/infrastructure/reducer-bundle-abi.js.map +1 -0
- package/dist/infrastructure/workspace-codegen.d.ts +53 -0
- package/dist/infrastructure/workspace-codegen.js +44 -0
- package/dist/infrastructure/workspace-codegen.js.map +1 -0
- package/dist/manifest-contract-BNHVGFtU.d.ts +9 -0
- package/dist/package-set.d.ts +13 -0
- package/dist/package-set.js +12 -0
- package/dist/package-set.js.map +1 -0
- package/dist/primitive-props-DpKs-GCr.d.ts +11 -0
- package/dist/reducer.d.ts +3786 -0
- package/dist/reducer.js +8131 -0
- package/dist/reducer.js.map +1 -0
- package/dist/runtime/primitives.d.ts +226 -0
- package/dist/runtime/primitives.js +180 -0
- package/dist/runtime/primitives.js.map +1 -0
- package/dist/runtime/types/runtime-api.d.ts +1 -0
- package/dist/runtime/types/runtime-api.js +2 -0
- package/dist/runtime/types/runtime-api.js.map +1 -0
- package/dist/runtime/workspace-contract.d.ts +172 -0
- package/dist/runtime/workspace-contract.js +14 -0
- package/dist/runtime/workspace-contract.js.map +1 -0
- package/dist/runtime-api-3dshj6kK.d.ts +101 -0
- package/dist/runtime-api-DWxvTr-O.d.ts +379 -0
- package/dist/runtime.d.ts +58 -0
- package/dist/runtime.js +13 -0
- package/dist/runtime.js.map +1 -0
- package/dist/slots-1GPGihk8.d.ts +8 -0
- package/dist/testing.d.ts +149 -0
- package/dist/testing.js +513 -0
- package/dist/testing.js.map +1 -0
- package/dist/types.d.ts +496 -0
- package/dist/types.js +28 -0
- package/dist/types.js.map +1 -0
- package/dist/ui/components.d.ts +16 -0
- package/dist/ui/components.js +192 -0
- package/dist/ui/components.js.map +1 -0
- package/dist/ui/defaults.d.ts +19 -0
- package/dist/ui/defaults.js +104 -0
- package/dist/ui/defaults.js.map +1 -0
- package/dist/ui/plugin-styles.css +250 -0
- package/dist/ui/types/player-state.d.ts +365 -0
- package/dist/ui/types/player-state.js +1 -0
- package/dist/ui/types/player-state.js.map +1 -0
- package/dist/ui-contract-iQfTtUSL.d.ts +1161 -0
- package/dist/ui.d.ts +320 -0
- package/dist/ui.js +253 -0
- package/dist/ui.js.map +1 -0
- package/package.json +199 -0
- package/src/generated/reducer-contract/builders.ts +90 -0
- package/src/generated/reducer-contract/version.ts +9 -0
- package/src/generated/reducer-contract/wire.ts +100 -0
- package/src/generated/reducer-contract/zod.ts +101 -0
- package/src/generated/runtime/primitives.ts +2 -0
- package/src/generated/runtime-api.ts +5 -0
- package/src/generated/runtime.ts +35 -0
- package/src/generated/workspace-contract.ts +2 -0
- package/src/index.ts +7 -0
- package/src/infrastructure/reducer-bundle-abi.ts +8 -0
- package/src/infrastructure/reducer-contract/bundle.ts +37 -0
- package/src/infrastructure/workspace-codegen/hex-geometry.ts +69 -0
- package/src/infrastructure/workspace-codegen/index.ts +64 -0
- package/src/infrastructure/workspace-codegen/manifest-contract.ts +6632 -0
- package/src/infrastructure/workspace-codegen/manifest-validation.ts +795 -0
- package/src/infrastructure/workspace-codegen/ownership.ts +131 -0
- package/src/infrastructure/workspace-codegen/preset-card-sets.ts +169 -0
- package/src/infrastructure/workspace-codegen/seeds.ts +1705 -0
- package/src/infrastructure/workspace-codegen.ts +1 -0
- package/src/package-set.ts +19 -0
- package/src/reducer/authoring/contract.ts +157 -0
- package/src/reducer/authoring/effect.ts +224 -0
- package/src/reducer/authoring/game.ts +23 -0
- package/src/reducer/authoring/interaction.ts +98 -0
- package/src/reducer/authoring/phase.ts +300 -0
- package/src/reducer/authoring/types.ts +70 -0
- package/src/reducer/authoring/validation.ts +382 -0
- package/src/reducer/authoring/view-stage.ts +68 -0
- package/src/reducer/authoring.ts +29 -0
- package/src/reducer/bundle/ingress-bundle.ts +491 -0
- package/src/reducer/bundle/trusted/engine-instruction-resolver.ts +254 -0
- package/src/reducer/bundle/trusted/flow-instruction-resolver.ts +73 -0
- package/src/reducer/bundle/trusted/instruction-runner.ts +414 -0
- package/src/reducer/bundle/trusted/interaction-authorization.ts +137 -0
- package/src/reducer/bundle/trusted/interaction-collectors.ts +859 -0
- package/src/reducer/bundle/trusted/interaction-decision.ts +747 -0
- package/src/reducer/bundle/trusted/interaction-resolver.ts +95 -0
- package/src/reducer/bundle/trusted/interaction-types.ts +171 -0
- package/src/reducer/bundle/trusted/lifecycle-runner.ts +427 -0
- package/src/reducer/bundle/trusted/projection-builder.ts +356 -0
- package/src/reducer/bundle/trusted/projection-context.ts +39 -0
- package/src/reducer/bundle/trusted/rng-sampler.ts +150 -0
- package/src/reducer/bundle/trusted/runtime-registry.ts +120 -0
- package/src/reducer/bundle/trusted/runtime-scope.ts +336 -0
- package/src/reducer/bundle/trusted/simultaneous-player.ts +97 -0
- package/src/reducer/bundle/trusted/stage-resolver.ts +87 -0
- package/src/reducer/bundle/trusted/static-projection.ts +116 -0
- package/src/reducer/bundle/trusted/trusted-runtime-args.ts +97 -0
- package/src/reducer/bundle/trusted/trusted-runtime-result.ts +39 -0
- package/src/reducer/bundle/trusted/trusted-setup-profiles.ts +43 -0
- package/src/reducer/bundle/trusted/trusted-state-codec.ts +48 -0
- package/src/reducer/bundle/trusted-bundle.ts +97 -0
- package/src/reducer/bundle/types.ts +171 -0
- package/src/reducer/bundle.ts +2 -0
- package/src/reducer/client-param-schemas.ts +57 -0
- package/src/reducer/compose.ts +34 -0
- package/src/reducer/core/runtime-input.ts +30 -0
- package/src/reducer/core/runtime-instruction.ts +59 -0
- package/src/reducer/core/types.ts +62 -0
- package/src/reducer/definition-index.ts +277 -0
- package/src/reducer/derived.ts +106 -0
- package/src/reducer/effects.ts +92 -0
- package/src/reducer/engine/runtime-instruction-engine.ts +155 -0
- package/src/reducer/ingress/decode-runtime-input.ts +7 -0
- package/src/reducer/ingress/decode-session-state.ts +9 -0
- package/src/reducer/ingress/encode-session-state.ts +6 -0
- package/src/reducer/ingress/input-codec.ts +18 -0
- package/src/reducer/ingress/phase-schemas.ts +62 -0
- package/src/reducer/ingress/raw-types.ts +107 -0
- package/src/reducer/ingress/runtime-codec.ts +14 -0
- package/src/reducer/ingress/runtime-payload.ts +13 -0
- package/src/reducer/ingress/session-codec.ts +392 -0
- package/src/reducer/ingress/types.ts +6 -0
- package/src/reducer/inputs/boardInput.ts +217 -0
- package/src/reducer/inputs/boardTarget.ts +190 -0
- package/src/reducer/inputs/cardInput.ts +86 -0
- package/src/reducer/inputs/cardTarget.ts +101 -0
- package/src/reducer/inputs/choiceTarget.ts +104 -0
- package/src/reducer/inputs/defineInputs.ts +71 -0
- package/src/reducer/inputs/formInput.ts +809 -0
- package/src/reducer/inputs/many.ts +120 -0
- package/src/reducer/inputs/promptInput.ts +87 -0
- package/src/reducer/inputs/rngInput.ts +58 -0
- package/src/reducer/inputs/targetRule.ts +123 -0
- package/src/reducer/inputs.ts +41 -0
- package/src/reducer/model/definition.ts +1072 -0
- package/src/reducer/model/extract.ts +745 -0
- package/src/reducer/model/manifest.ts +570 -0
- package/src/reducer/model/queries.ts +641 -0
- package/src/reducer/model/runtime.ts +264 -0
- package/src/reducer/model/spec.ts +1386 -0
- package/src/reducer/model/table.ts +260 -0
- package/src/reducer/model.ts +7 -0
- package/src/reducer/ops.ts +1034 -0
- package/src/reducer/parse-utils.ts +28 -0
- package/src/reducer/per-player.ts +422 -0
- package/src/reducer/rng.ts +69 -0
- package/src/reducer/schema-helpers.ts +185 -0
- package/src/reducer/setup-bootstrap-helpers.ts +171 -0
- package/src/reducer/setup-bootstrap.ts +481 -0
- package/src/reducer/table-ops.ts +2671 -0
- package/src/reducer/table-queries.ts +372 -0
- package/src/reducer/transaction.ts +120 -0
- package/src/reducer.ts +314 -0
- package/src/runtime/primitives.ts +1 -0
- package/src/runtime/types/runtime-api.ts +1 -0
- package/src/runtime/workspace-contract.ts +32 -0
- package/src/runtime-internal/components/InteractionForm.tsx +1309 -0
- package/src/runtime-internal/components/PluginRuntime.tsx +103 -0
- package/src/runtime-internal/components/board/target-layer.ts +70 -0
- package/src/runtime-internal/context/ClientParamSchemaContext.tsx +44 -0
- package/src/runtime-internal/context/InteractionDraftContext.tsx +279 -0
- package/src/runtime-internal/context/PluginSessionContext.tsx +47 -0
- package/src/runtime-internal/context/PluginStateContext.tsx +262 -0
- package/src/runtime-internal/context/RuntimeContext.tsx +96 -0
- package/src/runtime-internal/defaults/components.tsx +409 -0
- package/src/runtime-internal/defaults/index.ts +11 -0
- package/src/runtime-internal/errors/ValidationError.ts +29 -0
- package/src/runtime-internal/hooks/useActivePlayers.ts +33 -0
- package/src/runtime-internal/hooks/useBoardInteractions.ts +665 -0
- package/src/runtime-internal/hooks/useGameSelector.ts +105 -0
- package/src/runtime-internal/hooks/useGameView.ts +9 -0
- package/src/runtime-internal/hooks/useInteractionByKey.ts +354 -0
- package/src/runtime-internal/hooks/useInteractionHandle.ts +438 -0
- package/src/runtime-internal/hooks/useIsMyTurn.ts +20 -0
- package/src/runtime-internal/hooks/useLobby.ts +76 -0
- package/src/runtime-internal/hooks/useMe.ts +48 -0
- package/src/runtime-internal/hooks/usePlayerInfo.ts +28 -0
- package/src/runtime-internal/hooks/usePlayerTurnOrder.ts +23 -0
- package/src/runtime-internal/hooks/usePluginRuntime.ts +147 -0
- package/src/runtime-internal/hooks/useSeatInbox.ts +61 -0
- package/src/runtime-internal/hooks/useSimultaneousPhase.ts +10 -0
- package/src/runtime-internal/index.ts +42 -0
- package/src/runtime-internal/internal.ts +43 -0
- package/src/runtime-internal/plugin-styles.css +250 -0
- package/src/runtime-internal/primitives/board.tsx +459 -0
- package/src/runtime-internal/primitives/dialog-lifecycle.ts +58 -0
- package/src/runtime-internal/primitives/dice.tsx +79 -0
- package/src/runtime-internal/primitives/game-ui-provider.tsx +35 -0
- package/src/runtime-internal/primitives/game.tsx +387 -0
- package/src/runtime-internal/primitives/hand-intent-adapter.ts +147 -0
- package/src/runtime-internal/primitives/hand-surface.tsx +594 -0
- package/src/runtime-internal/primitives/index.ts +196 -0
- package/src/runtime-internal/primitives/interaction-form-binding.tsx +56 -0
- package/src/runtime-internal/primitives/interaction-submit.ts +90 -0
- package/src/runtime-internal/primitives/interaction.tsx +987 -0
- package/src/runtime-internal/primitives/phase.tsx +43 -0
- package/src/runtime-internal/primitives/player-roster.tsx +302 -0
- package/src/runtime-internal/primitives/primitive-props.tsx +101 -0
- package/src/runtime-internal/primitives/prompt.tsx +255 -0
- package/src/runtime-internal/primitives/ui.tsx +60 -0
- package/src/runtime-internal/primitives/zone.tsx +791 -0
- package/src/runtime-internal/reducer.ts +30 -0
- package/src/runtime-internal/runtime/createPluginRuntimeAPI.ts +605 -0
- package/src/runtime-internal/types/plugin-state.ts +508 -0
- package/src/runtime-internal/types/reducer-state.ts +24 -0
- package/src/runtime-internal/types/runtime-api.ts +114 -0
- package/src/runtime-internal/ui-contract.ts +519 -0
- package/src/runtime-internal/utils/card-intent-adapter.ts +546 -0
- package/src/runtime-internal/utils/interaction-inputs.ts +492 -0
- package/src/runtime-internal/utils/interaction-labels.ts +23 -0
- package/src/runtime-internal/utils/interaction-router.ts +273 -0
- package/src/runtime-internal/utils/interaction-status.ts +74 -0
- package/src/runtime-internal/workspace-contract.ts +1170 -0
- package/src/runtime.ts +34 -0
- package/src/testing/create-expect-api.ts +352 -0
- package/src/testing/create-test-runtime.ts +381 -0
- package/src/testing/definitions.ts +127 -0
- package/src/testing/index.ts +3 -0
- package/src/testing.ts +1 -0
- package/src/type-stubs/manifest-contract.d.ts +42 -0
- package/src/type-stubs/manifest-contract.js +72 -0
- package/src/type-stubs/ui-contract.d.ts +5 -0
- package/src/type-stubs/ui-contract.js +1 -0
- package/src/types/authoring-card-properties.type-test.ts +266 -0
- package/src/types/authoring.ts +1282 -0
- package/src/types/cards.ts +19 -0
- package/src/types/contracts.ts +1550 -0
- package/src/types/generated-helpers.ts +35 -0
- package/src/types/index.ts +147 -0
- package/src/types/slots.ts +11 -0
- package/src/types.ts +1 -0
- package/src/ui/components/ActionButton.tsx +97 -0
- package/src/ui/components/ActionPanel.tsx +315 -0
- package/src/ui/components/Card.tsx +378 -0
- package/src/ui/components/CardDragSurface.tsx +1076 -0
- package/src/ui/components/ChromeSuppressionContext.tsx +70 -0
- package/src/ui/components/CostDisplay.tsx +145 -0
- package/src/ui/components/DiceRoller.tsx +581 -0
- package/src/ui/components/Drawer.tsx +180 -0
- package/src/ui/components/ErrorBoundary.tsx +275 -0
- package/src/ui/components/GameEndDisplay.tsx +398 -0
- package/src/ui/components/GameSkeleton.tsx +260 -0
- package/src/ui/components/Hand.tsx +468 -0
- package/src/ui/components/HandDock.tsx +299 -0
- package/src/ui/components/HandView.tsx +441 -0
- package/src/ui/components/MobileHandTray.tsx +381 -0
- package/src/ui/components/MoreActions.tsx +143 -0
- package/src/ui/components/PhaseIndicator.tsx +341 -0
- package/src/ui/components/PlayArea.tsx +146 -0
- package/src/ui/components/PrimaryActionButton.tsx +336 -0
- package/src/ui/components/PrimaryButton.tsx +45 -0
- package/src/ui/components/ResourceCounter.tsx +270 -0
- package/src/ui/components/StagingZone.tsx +134 -0
- package/src/ui/components/ThemedButton.tsx +113 -0
- package/src/ui/components/Toast.tsx +264 -0
- package/src/ui/components/board/HexGrid.tsx +1294 -0
- package/src/ui/components/board/NetworkGraph.tsx +476 -0
- package/src/ui/components/board/SlotSystem.tsx +388 -0
- package/src/ui/components/board/SquareGrid.tsx +1165 -0
- package/src/ui/components/board/TrackBoard.tsx +496 -0
- package/src/ui/components/board/ZoneMap.tsx +448 -0
- package/src/ui/components/board/hex-board-view.ts +123 -0
- package/src/ui/components/board/index.ts +142 -0
- package/src/ui/components/board/interaction-accessibility.ts +21 -0
- package/src/ui/components/board/target-layer.ts +66 -0
- package/src/ui/components/card-render-content.type-test.ts +27 -0
- package/src/ui/components/hand-layout-math.ts +163 -0
- package/src/ui/components/hand-pointer-engine.ts +413 -0
- package/src/ui/components/index.ts +245 -0
- package/src/ui/components.ts +1 -0
- package/src/ui/defaults/components.tsx +106 -0
- package/src/ui/defaults/index.ts +8 -0
- package/src/ui/defaults.ts +1 -0
- package/src/ui/errors/ValidationError.ts +29 -0
- package/src/ui/helpers/cards.ts +19 -0
- package/src/ui/helpers/track-board.ts +211 -0
- package/src/ui/hooks/useBoardTopology.ts +316 -0
- package/src/ui/hooks/useCards.ts +10 -0
- package/src/ui/hooks/useHandCardPointer.ts +381 -0
- package/src/ui/hooks/useHandLayout.ts +378 -0
- package/src/ui/hooks/useHandPresentation.ts +121 -0
- package/src/ui/hooks/useHexBoard.ts +74 -0
- package/src/ui/hooks/useHexGrid.ts +185 -0
- package/src/ui/hooks/useIsMobile.ts +35 -0
- package/src/ui/hooks/usePanZoom.ts +278 -0
- package/src/ui/hooks/useSquareBoard.ts +124 -0
- package/src/ui/hooks/useSquareGrid.ts +328 -0
- package/src/ui/index.ts +98 -0
- package/src/ui/internal/ui/alert.tsx +51 -0
- package/src/ui/internal/ui/button.tsx +58 -0
- package/src/ui/internal/ui/dialog.tsx +134 -0
- package/src/ui/internal/ui/input.tsx +21 -0
- package/src/ui/internal/ui/label.tsx +21 -0
- package/src/ui/internal/ui/select.tsx +129 -0
- package/src/ui/internal/ui/tooltip.tsx +54 -0
- package/src/ui/internal/ui/utils.ts +5 -0
- package/src/ui/plugin-styles.css +250 -0
- package/src/ui/primitives/dialog-lifecycle.ts +58 -0
- package/src/ui/primitives/dice.tsx +79 -0
- package/src/ui/primitives/primitive-props.tsx +101 -0
- package/src/ui/theme/ThemeProvider.tsx +252 -0
- package/src/ui/theme/board.ts +61 -0
- package/src/ui/theme/css-vars.ts +105 -0
- package/src/ui/theme/derive.ts +240 -0
- package/src/ui/theme/index.ts +61 -0
- package/src/ui/theme/presets/arcade.ts +261 -0
- package/src/ui/theme/presets/studio.ts +261 -0
- package/src/ui/theme/presets/tabletop.ts +266 -0
- package/src/ui/theme/tokens.ts +392 -0
- package/src/ui/types/hex-color.ts +20 -0
- package/src/ui/types/player-state.ts +463 -0
- package/src/ui/types/tiled-board.ts +785 -0
- package/src/ui/types/visual-state.ts +137 -0
- package/src/ui.ts +1 -0
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
|
|
3
|
+
export function formatIssue(
|
|
4
|
+
label: string,
|
|
5
|
+
issue: { path: PropertyKey[]; message: string },
|
|
6
|
+
): string {
|
|
7
|
+
const path = issue.path
|
|
8
|
+
.map((segment) =>
|
|
9
|
+
typeof segment === "symbol" ? segment.toString() : String(segment),
|
|
10
|
+
)
|
|
11
|
+
.join(".");
|
|
12
|
+
return `${path || label}: ${issue.message}`;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function safeParseOrThrow<Schema extends z.ZodTypeAny>(
|
|
16
|
+
schema: Schema,
|
|
17
|
+
value: unknown,
|
|
18
|
+
label: string,
|
|
19
|
+
): z.output<Schema> {
|
|
20
|
+
const parsed = schema.safeParse(value);
|
|
21
|
+
if (!parsed.success) {
|
|
22
|
+
const message = parsed.error.issues
|
|
23
|
+
.map((issue) => formatIssue(label, issue))
|
|
24
|
+
.join("; ");
|
|
25
|
+
throw new Error(message || `Invalid ${label}`);
|
|
26
|
+
}
|
|
27
|
+
return parsed.data;
|
|
28
|
+
}
|
|
@@ -0,0 +1,422 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import type { Brand } from "./model/table";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Opaque brand applied to a runtime player identifier.
|
|
6
|
+
*
|
|
7
|
+
* Generators produce a workspace-specific `PlayerId` alias (e.g.
|
|
8
|
+
* `Brand<string, "PlayerId">` in `shared/manifest-contract.ts`). Authors
|
|
9
|
+
* obtain `PlayerId` values only through:
|
|
10
|
+
* - `q.player.order()` / `q.player.current()` (reducer queries),
|
|
11
|
+
* - engine-injected callback arguments (actions, phases, prompts),
|
|
12
|
+
* - `perPlayerKeys(...)` / entries iteration,
|
|
13
|
+
* - `asPlayerId(raw)` as an explicit escape hatch (e.g. ingress parsing).
|
|
14
|
+
*
|
|
15
|
+
* The brand is a phantom type: at runtime a `PlayerId` is just a string.
|
|
16
|
+
* Do not JSON-serialize the brand marker; it exists purely at the type
|
|
17
|
+
* level to block accidental literal comparisons such as
|
|
18
|
+
* `playerId === "player-1"`.
|
|
19
|
+
*/
|
|
20
|
+
export type PlayerId = Brand<string, "PlayerId">;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Explicit conversion from a raw string to a branded `PlayerId`.
|
|
24
|
+
*
|
|
25
|
+
* Use at trust boundaries only (wire ingress, tests, fixtures). Inside
|
|
26
|
+
* reducer logic, obtain `PlayerId` values from the engine instead of
|
|
27
|
+
* constructing them yourself.
|
|
28
|
+
*/
|
|
29
|
+
export function asPlayerId(raw: string): PlayerId {
|
|
30
|
+
return raw as PlayerId;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Type guard that narrows `unknown` to `PlayerId` when the value is a
|
|
35
|
+
* non-empty string. Does not validate against a specific player roster;
|
|
36
|
+
* use `perPlayerSchema`/ingress parsing for that.
|
|
37
|
+
*/
|
|
38
|
+
export function isPlayerId(value: unknown): value is PlayerId {
|
|
39
|
+
return typeof value === "string" && value.length > 0;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* A runtime-accurate per-player map.
|
|
44
|
+
*
|
|
45
|
+
* `PerPlayer<Value>` is an opaque, ordered container whose entries are
|
|
46
|
+
* exactly the players that exist at runtime (the seats passed to
|
|
47
|
+
* `initialize`).
|
|
48
|
+
*
|
|
49
|
+
* The `__perPlayer` discriminator keeps the structural type nominal-ish
|
|
50
|
+
* without depending on a symbol (symbols do not round-trip through JSON,
|
|
51
|
+
* which this shape must).
|
|
52
|
+
*
|
|
53
|
+
* Construct with `perPlayer(ids, init)` and read with the accessors in
|
|
54
|
+
* this module; do not reach into `entries` directly unless you need
|
|
55
|
+
* ordered iteration.
|
|
56
|
+
*/
|
|
57
|
+
export interface PerPlayer<Value, Id extends PlayerId = PlayerId> {
|
|
58
|
+
readonly __perPlayer: true;
|
|
59
|
+
readonly entries: ReadonlyArray<readonly [Id, Value]>;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Construct a `PerPlayer<Value>` with one entry per `id` in `ids`.
|
|
64
|
+
*
|
|
65
|
+
* `ids` is treated as the authoritative runtime seat list: the returned
|
|
66
|
+
* `PerPlayer` contains exactly `ids.length` entries, in the same order.
|
|
67
|
+
* Duplicate ids throw.
|
|
68
|
+
*/
|
|
69
|
+
export function perPlayer<Value, Id extends PlayerId = PlayerId>(
|
|
70
|
+
ids: readonly Id[],
|
|
71
|
+
init: (id: Id, index: number) => Value,
|
|
72
|
+
): PerPlayer<Value, Id> {
|
|
73
|
+
const seen = new Set<string>();
|
|
74
|
+
const entries: Array<readonly [Id, Value]> = [];
|
|
75
|
+
for (const [index, id] of ids.entries()) {
|
|
76
|
+
if (seen.has(id)) {
|
|
77
|
+
throw new Error(`perPlayer: duplicate player id '${id}'`);
|
|
78
|
+
}
|
|
79
|
+
seen.add(id);
|
|
80
|
+
entries.push([id, init(id, index)] as const);
|
|
81
|
+
}
|
|
82
|
+
return { __perPlayer: true, entries };
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** Ordered list of seat ids present in the `PerPlayer`. */
|
|
86
|
+
export function perPlayerKeys<Id extends PlayerId>(
|
|
87
|
+
value: PerPlayer<unknown, Id>,
|
|
88
|
+
): Id[] {
|
|
89
|
+
return value.entries.map((entry) => entry[0]);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** Ordered list of values present in the `PerPlayer`. */
|
|
93
|
+
export function perPlayerValues<Value>(
|
|
94
|
+
value: PerPlayer<Value, PlayerId>,
|
|
95
|
+
): Value[] {
|
|
96
|
+
return value.entries.map((entry) => entry[1]);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Ordered `[id, value]` pairs. Returns the backing array as-is; callers
|
|
101
|
+
* must not mutate it.
|
|
102
|
+
*/
|
|
103
|
+
export function perPlayerEntries<Value, Id extends PlayerId>(
|
|
104
|
+
value: PerPlayer<Value, Id>,
|
|
105
|
+
): ReadonlyArray<readonly [Id, Value]> {
|
|
106
|
+
return value.entries;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** Number of seats in the `PerPlayer`. */
|
|
110
|
+
export function perPlayerSize(value: PerPlayer<unknown, PlayerId>): number {
|
|
111
|
+
return value.entries.length;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** Whether `id` has a value. */
|
|
115
|
+
export function perPlayerHas<Id extends PlayerId>(
|
|
116
|
+
value: PerPlayer<unknown, Id>,
|
|
117
|
+
id: Id,
|
|
118
|
+
): boolean {
|
|
119
|
+
for (const [candidate] of value.entries) {
|
|
120
|
+
if (candidate === id) {
|
|
121
|
+
return true;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return false;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** Lookup with no fallback; returns `undefined` for missing seats. */
|
|
128
|
+
export function perPlayerGet<Value, Id extends PlayerId>(
|
|
129
|
+
value: PerPlayer<Value, Id>,
|
|
130
|
+
id: Id,
|
|
131
|
+
): Value | undefined {
|
|
132
|
+
for (const [candidate, v] of value.entries) {
|
|
133
|
+
if (candidate === id) {
|
|
134
|
+
return v;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
return undefined;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Lookup that throws if `id` is not present.
|
|
142
|
+
*
|
|
143
|
+
* Use when the caller has already established (via `q.player.order()`,
|
|
144
|
+
* an action context, etc.) that `id` is an active runtime seat.
|
|
145
|
+
*/
|
|
146
|
+
export function perPlayerRequire<Value, Id extends PlayerId>(
|
|
147
|
+
value: PerPlayer<Value, Id>,
|
|
148
|
+
id: Id,
|
|
149
|
+
): Value {
|
|
150
|
+
const found = perPlayerGet(value, id);
|
|
151
|
+
if (found === undefined && !perPlayerHas(value, id)) {
|
|
152
|
+
throw new Error(`perPlayerRequire: missing entry for player id '${id}'`);
|
|
153
|
+
}
|
|
154
|
+
return found as Value;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Return a new `PerPlayer` with `id`'s value replaced (or added if the
|
|
159
|
+
* id is not present). Preserves entry order; new entries are appended.
|
|
160
|
+
*/
|
|
161
|
+
export function perPlayerSet<Value, Id extends PlayerId>(
|
|
162
|
+
value: PerPlayer<Value, Id>,
|
|
163
|
+
id: Id,
|
|
164
|
+
next: Value,
|
|
165
|
+
): PerPlayer<Value, Id> {
|
|
166
|
+
const entries = value.entries.slice();
|
|
167
|
+
const index = entries.findIndex((entry) => entry[0] === id);
|
|
168
|
+
if (index >= 0) {
|
|
169
|
+
entries[index] = [id, next] as const;
|
|
170
|
+
} else {
|
|
171
|
+
entries.push([id, next] as const);
|
|
172
|
+
}
|
|
173
|
+
return { __perPlayer: true, entries };
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Return a new `PerPlayer` where each value is replaced by `f(value, id)`.
|
|
178
|
+
* Seat order is preserved.
|
|
179
|
+
*/
|
|
180
|
+
export function perPlayerMap<Value, Next, Id extends PlayerId>(
|
|
181
|
+
value: PerPlayer<Value, Id>,
|
|
182
|
+
f: (v: Value, id: Id, index: number) => Next,
|
|
183
|
+
): PerPlayer<Next, Id> {
|
|
184
|
+
return {
|
|
185
|
+
__perPlayer: true,
|
|
186
|
+
entries: value.entries.map(
|
|
187
|
+
([id, v], index) => [id, f(v, id, index)] as const,
|
|
188
|
+
),
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Structural check for a `PerPlayer` without a Zod schema. Useful in
|
|
194
|
+
* debug paths and as a fast pre-filter before schema validation.
|
|
195
|
+
*/
|
|
196
|
+
export function isPerPlayer(value: unknown): value is PerPlayer<unknown> {
|
|
197
|
+
if (typeof value !== "object" || value === null) {
|
|
198
|
+
return false;
|
|
199
|
+
}
|
|
200
|
+
const candidate = value as { __perPlayer?: unknown; entries?: unknown };
|
|
201
|
+
if (candidate.__perPlayer !== true) {
|
|
202
|
+
return false;
|
|
203
|
+
}
|
|
204
|
+
if (!Array.isArray(candidate.entries)) {
|
|
205
|
+
return false;
|
|
206
|
+
}
|
|
207
|
+
return candidate.entries.every(
|
|
208
|
+
(entry) =>
|
|
209
|
+
Array.isArray(entry) &&
|
|
210
|
+
entry.length === 2 &&
|
|
211
|
+
typeof entry[0] === "string",
|
|
212
|
+
);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Options accepted by `perPlayerSchema`.
|
|
217
|
+
*
|
|
218
|
+
* Setting `players` locks the schema to an exact seat list: parse fails
|
|
219
|
+
* if the input is missing any seat or has an unknown id. Leaving
|
|
220
|
+
* `players` unset yields a schema that accepts any non-empty string as
|
|
221
|
+
* a key, which is the right default for generic SDK types.
|
|
222
|
+
*
|
|
223
|
+
* `playerIdSchema` lets callers feed a manifest-scoped Zod id schema in
|
|
224
|
+
* so parse errors carry the same message as other branded ids.
|
|
225
|
+
*/
|
|
226
|
+
export type PerPlayerSchemaOptions<Id extends PlayerId> = {
|
|
227
|
+
readonly playerIdSchema?: z.ZodType<Id>;
|
|
228
|
+
readonly players?: readonly Id[];
|
|
229
|
+
};
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Build a Zod schema that validates a wire-shaped `PerPlayer<Value>`.
|
|
233
|
+
*
|
|
234
|
+
* When `options.players` is supplied the schema enforces that entries
|
|
235
|
+
* match that exact set (same cardinality, same ids, any order). This is
|
|
236
|
+
* the mechanism that catches the class of bug where a 3-player session
|
|
237
|
+
* produced a view shape the old `Record<PlayerId, T>` type claimed had
|
|
238
|
+
* a `player-4` key.
|
|
239
|
+
*/
|
|
240
|
+
export function perPlayerSchema<Value, Id extends PlayerId = PlayerId>(
|
|
241
|
+
valueSchema: z.ZodType<Value>,
|
|
242
|
+
options: PerPlayerSchemaOptions<Id> = {},
|
|
243
|
+
): z.ZodType<PerPlayer<Value, Id>> {
|
|
244
|
+
const keySchema =
|
|
245
|
+
options.playerIdSchema ?? (z.string().min(1) as unknown as z.ZodType<Id>);
|
|
246
|
+
|
|
247
|
+
const base = z
|
|
248
|
+
.object({
|
|
249
|
+
__perPlayer: z.literal(true),
|
|
250
|
+
entries: z.array(z.tuple([keySchema, valueSchema])),
|
|
251
|
+
})
|
|
252
|
+
.transform(
|
|
253
|
+
(value): PerPlayer<Value, Id> => ({
|
|
254
|
+
__perPlayer: true as const,
|
|
255
|
+
entries: value.entries as ReadonlyArray<readonly [Id, Value]>,
|
|
256
|
+
}),
|
|
257
|
+
);
|
|
258
|
+
|
|
259
|
+
if (!options.players) {
|
|
260
|
+
return base as unknown as z.ZodType<PerPlayer<Value, Id>>;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
const expected = options.players;
|
|
264
|
+
return base.superRefine((value, ctx) => {
|
|
265
|
+
const seen = new Set<string>();
|
|
266
|
+
for (const [id] of value.entries) {
|
|
267
|
+
if (seen.has(id)) {
|
|
268
|
+
ctx.addIssue({
|
|
269
|
+
code: "custom",
|
|
270
|
+
message: `Duplicate player id '${id}'`,
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
seen.add(id);
|
|
274
|
+
}
|
|
275
|
+
for (const expectedId of expected) {
|
|
276
|
+
if (!seen.has(expectedId)) {
|
|
277
|
+
ctx.addIssue({
|
|
278
|
+
code: "custom",
|
|
279
|
+
message: `Missing entry for player id '${expectedId}'`,
|
|
280
|
+
});
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
for (const id of seen) {
|
|
284
|
+
if (!expected.some((expectedId) => expectedId === id)) {
|
|
285
|
+
ctx.addIssue({
|
|
286
|
+
code: "custom",
|
|
287
|
+
message: `Unexpected player id '${id}' (allowed: ${expected.join(", ")})`,
|
|
288
|
+
});
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
}) as unknown as z.ZodType<PerPlayer<Value, Id>>;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
// ---------------------------------------------------------------------------
|
|
295
|
+
// BoardRef: replacement for flat `"board:player-N"` literal unions.
|
|
296
|
+
// ---------------------------------------------------------------------------
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* Reference to a board by its authored `baseId`, plus an optional seat
|
|
300
|
+
* for per-player boards.
|
|
301
|
+
*
|
|
302
|
+
* Replaces the old generated `"ring:player-1" | "ring:player-2" | ...`
|
|
303
|
+
* flat unions whose keys pretended to be static but were actually
|
|
304
|
+
* derived from `maxPlayers` and therefore misaligned with the runtime
|
|
305
|
+
* seat list.
|
|
306
|
+
*
|
|
307
|
+
* The discriminant is the *presence* of `seat`, not a `scope` field, so
|
|
308
|
+
* authors can destructure and pass the ref directly without needing a
|
|
309
|
+
* discriminator check for shared boards.
|
|
310
|
+
*/
|
|
311
|
+
export type BoardRef<
|
|
312
|
+
BaseId extends string = string,
|
|
313
|
+
Id extends PlayerId = PlayerId,
|
|
314
|
+
> = SharedBoardRef<BaseId> | PerPlayerBoardRef<BaseId, Id>;
|
|
315
|
+
|
|
316
|
+
export interface SharedBoardRef<BaseId extends string = string> {
|
|
317
|
+
readonly baseId: BaseId;
|
|
318
|
+
readonly seat?: undefined;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
export interface PerPlayerBoardRef<
|
|
322
|
+
BaseId extends string = string,
|
|
323
|
+
Id extends PlayerId = PlayerId,
|
|
324
|
+
> {
|
|
325
|
+
readonly baseId: BaseId;
|
|
326
|
+
readonly seat: Id;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
/** Construct a shared board ref. */
|
|
330
|
+
export function sharedBoardRef<BaseId extends string>(
|
|
331
|
+
baseId: BaseId,
|
|
332
|
+
): SharedBoardRef<BaseId> {
|
|
333
|
+
return { baseId };
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/** Construct a per-player board ref. */
|
|
337
|
+
export function perPlayerBoardRef<BaseId extends string, Id extends PlayerId>(
|
|
338
|
+
baseId: BaseId,
|
|
339
|
+
seat: Id,
|
|
340
|
+
): PerPlayerBoardRef<BaseId, Id> {
|
|
341
|
+
return { baseId, seat };
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
/**
|
|
345
|
+
* Construct a `BoardRef` without knowing the scope ahead of time. Pass
|
|
346
|
+
* `seat` for per-player boards; omit for shared boards.
|
|
347
|
+
*/
|
|
348
|
+
export function boardRef<BaseId extends string, Id extends PlayerId>(
|
|
349
|
+
baseId: BaseId,
|
|
350
|
+
seat?: Id,
|
|
351
|
+
): BoardRef<BaseId, Id> {
|
|
352
|
+
if (seat === undefined) {
|
|
353
|
+
return { baseId };
|
|
354
|
+
}
|
|
355
|
+
return { baseId, seat };
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
/** Stable string key for Maps/Records keyed by a `BoardRef`. */
|
|
359
|
+
export function boardRefKey(ref: BoardRef): string {
|
|
360
|
+
return ref.seat === undefined ? ref.baseId : `${ref.baseId}:${ref.seat}`;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
/**
|
|
364
|
+
* Inverse of `boardRefKey`. Parses `"base"` as a shared ref and
|
|
365
|
+
* `"base:player-N"` as a per-player ref. Returns `null` for malformed
|
|
366
|
+
* input.
|
|
367
|
+
*/
|
|
368
|
+
export function parseBoardRefKey(key: string): BoardRef | null {
|
|
369
|
+
if (!key.length) {
|
|
370
|
+
return null;
|
|
371
|
+
}
|
|
372
|
+
const colon = key.indexOf(":");
|
|
373
|
+
if (colon < 0) {
|
|
374
|
+
return { baseId: key };
|
|
375
|
+
}
|
|
376
|
+
const baseId = key.slice(0, colon);
|
|
377
|
+
const seat = key.slice(colon + 1);
|
|
378
|
+
if (!baseId.length || !seat.length) {
|
|
379
|
+
return null;
|
|
380
|
+
}
|
|
381
|
+
return { baseId, seat: seat as PlayerId };
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
/**
|
|
385
|
+
* Zod schema for a `BoardRef` with free-form base and seat ids.
|
|
386
|
+
*
|
|
387
|
+
* Feed a manifest-scoped `baseIdSchema` and `playerIdSchema` to bind
|
|
388
|
+
* the ref to a specific workspace.
|
|
389
|
+
*/
|
|
390
|
+
export function boardRefSchema<
|
|
391
|
+
BaseId extends string = string,
|
|
392
|
+
Id extends PlayerId = PlayerId,
|
|
393
|
+
>(
|
|
394
|
+
options: {
|
|
395
|
+
readonly baseIdSchema?: z.ZodType<BaseId>;
|
|
396
|
+
readonly playerIdSchema?: z.ZodType<Id>;
|
|
397
|
+
} = {},
|
|
398
|
+
): z.ZodType<BoardRef<BaseId, Id>> {
|
|
399
|
+
const baseSchema =
|
|
400
|
+
options.baseIdSchema ?? (z.string().min(1) as unknown as z.ZodType<BaseId>);
|
|
401
|
+
const playerSchema =
|
|
402
|
+
options.playerIdSchema ?? (z.string().min(1) as unknown as z.ZodType<Id>);
|
|
403
|
+
const shared = z.strictObject({ baseId: baseSchema });
|
|
404
|
+
const perPlayer = z.strictObject({ baseId: baseSchema, seat: playerSchema });
|
|
405
|
+
return z.union([perPlayer, shared]) as unknown as z.ZodType<
|
|
406
|
+
BoardRef<BaseId, Id>
|
|
407
|
+
>;
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
/** True when `ref` targets a shared board (no seat). */
|
|
411
|
+
export function isSharedBoardRef<BaseId extends string>(
|
|
412
|
+
ref: BoardRef<BaseId, PlayerId>,
|
|
413
|
+
): ref is SharedBoardRef<BaseId> {
|
|
414
|
+
return ref.seat === undefined;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
/** True when `ref` targets a per-player board (has a seat). */
|
|
418
|
+
export function isPerPlayerBoardRef<BaseId extends string, Id extends PlayerId>(
|
|
419
|
+
ref: BoardRef<BaseId, Id>,
|
|
420
|
+
): ref is PerPlayerBoardRef<BaseId, Id> {
|
|
421
|
+
return ref.seat !== undefined;
|
|
422
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import type { RuntimeRngState } from "./model";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Deterministic pseudo-random integer generator for reducer-native games.
|
|
5
|
+
*
|
|
6
|
+
* The public contract is `(rng.seed, rng.cursor) -> value`, with the cursor
|
|
7
|
+
* advanced by one per call. Same `(seed, cursor)` tuple always produces the
|
|
8
|
+
* same output, which is what makes session replay deterministic without
|
|
9
|
+
* having to persist anything beyond the seed and cursor.
|
|
10
|
+
*
|
|
11
|
+
* Internally we hash `(seed, cursor)` through a Murmur3-style 32-bit
|
|
12
|
+
* finalizer rather than computing a linear combination of the cursor. The
|
|
13
|
+
* older `(seed * A + C + cursor) & 0x7fffffff` form was linear in `cursor`,
|
|
14
|
+
* so consecutive samples differed by a fixed step — for a d6 that meant
|
|
15
|
+
* every roll produced a simple `+1` counter through the faces. Mixing
|
|
16
|
+
* `(seed, cursor)` with an avalanche finalizer is still pure and still
|
|
17
|
+
* cheap, but now nearby cursors yield well-distributed outputs.
|
|
18
|
+
*
|
|
19
|
+
* Distribution note: the `% bound` step is biased when `bound` is not a
|
|
20
|
+
* power of two. For gameplay-level bounds (≤ 64) the bias is negligible
|
|
21
|
+
* and we leave it in to keep replay fixtures stable against future bound
|
|
22
|
+
* changes. If a caller ever needs strict uniformity we can add an
|
|
23
|
+
* explicit `nextUniformInt` without touching the existing consumers.
|
|
24
|
+
*/
|
|
25
|
+
export function nextRandomInt(
|
|
26
|
+
bound: number,
|
|
27
|
+
rng: RuntimeRngState,
|
|
28
|
+
): [number, RuntimeRngState] {
|
|
29
|
+
if (bound <= 0) {
|
|
30
|
+
throw new Error("Random bound must be positive.");
|
|
31
|
+
}
|
|
32
|
+
const seed = rng.seed ?? 1;
|
|
33
|
+
const raw = hashSeedCursor(seed, rng.cursor);
|
|
34
|
+
const value = raw % bound;
|
|
35
|
+
return [
|
|
36
|
+
value,
|
|
37
|
+
{
|
|
38
|
+
...rng,
|
|
39
|
+
cursor: rng.cursor + 1,
|
|
40
|
+
trace: [
|
|
41
|
+
...rng.trace,
|
|
42
|
+
`cursor=${rng.cursor};bound=${bound};value=${value}`,
|
|
43
|
+
],
|
|
44
|
+
},
|
|
45
|
+
];
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Mix `(seed, cursor)` into a uniformly-distributed uint32 using a
|
|
50
|
+
* Murmur3-style finalizer. The seed is split into its low and high 32-bit
|
|
51
|
+
* halves so large 53-bit JS-integer seeds (including the negative seeds
|
|
52
|
+
* the backend derives from its own 64-bit generator) don't collapse onto
|
|
53
|
+
* the same bucket after a `| 0` truncation.
|
|
54
|
+
*/
|
|
55
|
+
function hashSeedCursor(seed: number, cursor: number): number {
|
|
56
|
+
const seedLo = seed | 0;
|
|
57
|
+
// `Math.floor(seed / 2^32) | 0` keeps the upper bits of large seeds.
|
|
58
|
+
// For small seeds this is 0 (or -1 for negatives), so the stir below is
|
|
59
|
+
// still dominated by `seedLo` + `cursor` as you'd expect.
|
|
60
|
+
const seedHi = Math.floor(seed / 0x1_0000_0000) | 0;
|
|
61
|
+
let x = Math.imul(seedLo, 0x9e3779b1);
|
|
62
|
+
x = (x + Math.imul(seedHi, 0x85ebca77)) | 0;
|
|
63
|
+
x = (x + Math.imul(cursor | 0, 0xc2b2ae3d)) | 0;
|
|
64
|
+
// Murmur3 32-bit finalizer.
|
|
65
|
+
x = Math.imul(x ^ (x >>> 16), 0x85ebca6b);
|
|
66
|
+
x = Math.imul(x ^ (x >>> 13), 0xc2b2ae35);
|
|
67
|
+
x = x ^ (x >>> 16);
|
|
68
|
+
return x >>> 0;
|
|
69
|
+
}
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import type { SchemaLike } from "./model";
|
|
3
|
+
|
|
4
|
+
export type SparseMap<Key extends string, Value> = Partial<Record<Key, Value>>;
|
|
5
|
+
export type SparseCounts<Key extends string> = SparseMap<Key, number>;
|
|
6
|
+
|
|
7
|
+
type SparseSchemaMetadata = {
|
|
8
|
+
keySchema: z.ZodType<string>;
|
|
9
|
+
valueSchema: z.ZodTypeAny;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
const sparseSchemas = new WeakMap<object, SparseSchemaMetadata>();
|
|
13
|
+
|
|
14
|
+
export function sparseMap<
|
|
15
|
+
KeySchema extends z.ZodType<string>,
|
|
16
|
+
ValueSchema extends z.ZodTypeAny,
|
|
17
|
+
>(
|
|
18
|
+
keySchema: KeySchema,
|
|
19
|
+
valueSchema: ValueSchema,
|
|
20
|
+
): z.ZodType<SparseMap<z.infer<KeySchema>, z.infer<ValueSchema>>> {
|
|
21
|
+
const schema = z.partialRecord(keySchema, valueSchema) as z.ZodType<
|
|
22
|
+
SparseMap<z.infer<KeySchema>, z.infer<ValueSchema>>
|
|
23
|
+
>;
|
|
24
|
+
sparseSchemas.set(schema as unknown as object, {
|
|
25
|
+
keySchema,
|
|
26
|
+
valueSchema,
|
|
27
|
+
});
|
|
28
|
+
return schema;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function sparseCounts<KeySchema extends z.ZodType<string>>(
|
|
32
|
+
keySchema: KeySchema,
|
|
33
|
+
): z.ZodType<SparseCounts<z.infer<KeySchema>>> {
|
|
34
|
+
return sparseMap(keySchema, z.number().int().min(0)) as z.ZodType<
|
|
35
|
+
SparseCounts<z.infer<KeySchema>>
|
|
36
|
+
>;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function getZodDef(schema: unknown): {
|
|
40
|
+
type?: string;
|
|
41
|
+
} & Record<string, unknown> {
|
|
42
|
+
const anySchema = schema as {
|
|
43
|
+
_zod?: { def?: Record<string, unknown> };
|
|
44
|
+
def?: Record<string, unknown>;
|
|
45
|
+
};
|
|
46
|
+
const fromInternal = anySchema?._zod?.def;
|
|
47
|
+
if (fromInternal && typeof fromInternal === "object") {
|
|
48
|
+
return fromInternal;
|
|
49
|
+
}
|
|
50
|
+
const fromLegacy = anySchema?.def;
|
|
51
|
+
if (fromLegacy && typeof fromLegacy === "object") {
|
|
52
|
+
return fromLegacy;
|
|
53
|
+
}
|
|
54
|
+
return {};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function unwrapWrappers(schema: unknown): unknown {
|
|
58
|
+
let current = schema;
|
|
59
|
+
while (true) {
|
|
60
|
+
const def = getZodDef(current);
|
|
61
|
+
const innerType = def.innerType;
|
|
62
|
+
if (
|
|
63
|
+
(def.type === "nullable" ||
|
|
64
|
+
def.type === "optional" ||
|
|
65
|
+
def.type === "default" ||
|
|
66
|
+
def.type === "readonly" ||
|
|
67
|
+
def.type === "catch") &&
|
|
68
|
+
innerType
|
|
69
|
+
) {
|
|
70
|
+
current = innerType;
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
return current;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function isPlainZodString(schema: unknown): boolean {
|
|
78
|
+
return getZodDef(schema).type === "string";
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function isZodArray(schema: unknown): boolean {
|
|
82
|
+
return getZodDef(schema).type === "array";
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export function isEnumKeyedRecordSchema(schema: unknown): boolean {
|
|
86
|
+
const def = getZodDef(schema);
|
|
87
|
+
const keyType = def.keyType;
|
|
88
|
+
return (
|
|
89
|
+
def.type === "record" &&
|
|
90
|
+
typeof keyType === "object" &&
|
|
91
|
+
keyType !== null &&
|
|
92
|
+
getZodDef(keyType).type === "enum"
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export function isSparseMapSchema(schema: unknown): boolean {
|
|
97
|
+
const inner = unwrapWrappers(schema);
|
|
98
|
+
return (
|
|
99
|
+
typeof inner === "object" &&
|
|
100
|
+
inner !== null &&
|
|
101
|
+
sparseSchemas.has(inner as object)
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function getSparseSchemaMetadata(
|
|
106
|
+
schema: unknown,
|
|
107
|
+
): SparseSchemaMetadata | undefined {
|
|
108
|
+
const inner = unwrapWrappers(schema);
|
|
109
|
+
if (typeof inner !== "object" || inner === null) {
|
|
110
|
+
return undefined;
|
|
111
|
+
}
|
|
112
|
+
return sparseSchemas.get(inner as object);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function isPlainObject(value: unknown): value is Record<string, unknown> {
|
|
116
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export function getObjectShape(
|
|
120
|
+
schema: unknown,
|
|
121
|
+
): Record<string, unknown> | null {
|
|
122
|
+
const def = getZodDef(schema);
|
|
123
|
+
if (def.type !== "object") {
|
|
124
|
+
return null;
|
|
125
|
+
}
|
|
126
|
+
const shape = def.shape;
|
|
127
|
+
if (typeof shape === "function") {
|
|
128
|
+
const resolved = shape();
|
|
129
|
+
return resolved && typeof resolved === "object"
|
|
130
|
+
? (resolved as Record<string, unknown>)
|
|
131
|
+
: null;
|
|
132
|
+
}
|
|
133
|
+
return shape && typeof shape === "object"
|
|
134
|
+
? (shape as Record<string, unknown>)
|
|
135
|
+
: null;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function normalizeSchemaInput(schema: unknown, input: unknown): unknown {
|
|
139
|
+
const inner = unwrapWrappers(schema);
|
|
140
|
+
const sparse = getSparseSchemaMetadata(inner);
|
|
141
|
+
if (sparse && isPlainObject(input)) {
|
|
142
|
+
const filtered = Object.fromEntries(
|
|
143
|
+
Object.entries(input)
|
|
144
|
+
.filter(
|
|
145
|
+
([key, value]) =>
|
|
146
|
+
typeof value !== "undefined" &&
|
|
147
|
+
sparse.keySchema.safeParse(key).success,
|
|
148
|
+
)
|
|
149
|
+
.map(([key, value]) => [
|
|
150
|
+
key,
|
|
151
|
+
normalizeSchemaInput(sparse.valueSchema, value),
|
|
152
|
+
]),
|
|
153
|
+
);
|
|
154
|
+
return filtered;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
const shape = getObjectShape(inner);
|
|
158
|
+
if (shape && isPlainObject(input)) {
|
|
159
|
+
return Object.fromEntries(
|
|
160
|
+
Object.entries(input).map(([key, value]) => {
|
|
161
|
+
const fieldSchema = shape[key];
|
|
162
|
+
return [
|
|
163
|
+
key,
|
|
164
|
+
typeof fieldSchema === "undefined"
|
|
165
|
+
? value
|
|
166
|
+
: normalizeSchemaInput(fieldSchema, value),
|
|
167
|
+
];
|
|
168
|
+
}),
|
|
169
|
+
);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
const def = getZodDef(inner);
|
|
173
|
+
if (def.type === "array" && Array.isArray(input) && def.element) {
|
|
174
|
+
return input.map((value) => normalizeSchemaInput(def.element, value));
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
return input;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
export function normalizeCommandParams<Output>(
|
|
181
|
+
schema: SchemaLike<Output>,
|
|
182
|
+
input: unknown,
|
|
183
|
+
): Output {
|
|
184
|
+
return schema.parse(normalizeSchemaInput(schema, input));
|
|
185
|
+
}
|