@pretty-chitty/core 2.0.0 → 2.1.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/README.md +8 -0
- package/dist/components/ActionLogDisplay.d.ts.map +1 -1
- package/dist/components/ActionLogDisplay.js +1 -1
- package/dist/components/ActionLogDisplay.js.map +1 -1
- package/dist/components/BottomBarButton.d.ts +3 -1
- package/dist/components/BottomBarButton.d.ts.map +1 -1
- package/dist/components/BottomBarButton.js +3 -2
- package/dist/components/BottomBarButton.js.map +1 -1
- package/dist/components/ContextGalleryDisplay.d.ts.map +1 -1
- package/dist/components/ContextGalleryDisplay.js +1 -1
- package/dist/components/ContextGalleryDisplay.js.map +1 -1
- package/dist/components/DemoWrapper.d.ts +30 -2
- package/dist/components/DemoWrapper.d.ts.map +1 -1
- package/dist/components/DemoWrapper.js +79 -36
- package/dist/components/DemoWrapper.js.map +1 -1
- package/dist/components/FullScreenGalleryDisplay.d.ts.map +1 -1
- package/dist/components/FullScreenGalleryDisplay.js +2 -2
- package/dist/components/FullScreenGalleryDisplay.js.map +1 -1
- package/dist/components/Gallery/BuiltItem.d.ts +6 -2
- package/dist/components/Gallery/BuiltItem.d.ts.map +1 -1
- package/dist/components/Gallery/BuiltItem.js +28 -9
- package/dist/components/Gallery/BuiltItem.js.map +1 -1
- package/dist/components/Gallery/GalleryController.d.ts +18 -0
- package/dist/components/Gallery/GalleryController.d.ts.map +1 -1
- package/dist/components/Gallery/GalleryController.js +67 -0
- package/dist/components/Gallery/GalleryController.js.map +1 -1
- package/dist/components/Gallery/GalleryViewer.d.ts +6 -1
- package/dist/components/Gallery/GalleryViewer.d.ts.map +1 -1
- package/dist/components/Gallery/GalleryViewer.js +48 -10
- package/dist/components/Gallery/GalleryViewer.js.map +1 -1
- package/dist/components/Gallery/LayoutManager.d.ts +6 -0
- package/dist/components/Gallery/LayoutManager.d.ts.map +1 -1
- package/dist/components/Gallery/LayoutManager.js +23 -3
- package/dist/components/Gallery/LayoutManager.js.map +1 -1
- package/dist/components/Gallery/constants.d.ts +2 -0
- package/dist/components/Gallery/constants.d.ts.map +1 -1
- package/dist/components/Gallery/constants.js +2 -0
- package/dist/components/Gallery/constants.js.map +1 -1
- package/dist/components/Gallery/registry.d.ts +34 -0
- package/dist/components/Gallery/registry.d.ts.map +1 -0
- package/dist/components/Gallery/registry.js +17 -0
- package/dist/components/Gallery/registry.js.map +1 -0
- package/dist/components/GameDesigner.d.ts.map +1 -1
- package/dist/components/GameDesigner.js +42 -10
- package/dist/components/GameDesigner.js.map +1 -1
- package/dist/components/InlineGalleryDisplay.d.ts.map +1 -1
- package/dist/components/InlineGalleryDisplay.js +2 -2
- package/dist/components/InlineGalleryDisplay.js.map +1 -1
- package/dist/components/LiveButton.d.ts.map +1 -1
- package/dist/components/LiveButton.js +5 -2
- package/dist/components/LiveButton.js.map +1 -1
- package/dist/components/MatchViewer.d.ts +6 -1
- package/dist/components/MatchViewer.d.ts.map +1 -1
- package/dist/components/MatchViewer.js +9 -3
- package/dist/components/MatchViewer.js.map +1 -1
- package/dist/components/Panel/MultiPanel.d.ts.map +1 -1
- package/dist/components/Panel/MultiPanel.js +18 -0
- package/dist/components/Panel/MultiPanel.js.map +1 -1
- package/dist/components/Panel/PanelContents.d.ts.map +1 -1
- package/dist/components/Panel/PanelContents.js +92 -23
- package/dist/components/Panel/PanelContents.js.map +1 -1
- package/dist/components/Panel/PanelTabStack.d.ts.map +1 -1
- package/dist/components/Panel/PanelTabStack.js +1 -1
- package/dist/components/Panel/PanelTabStack.js.map +1 -1
- package/dist/components/PanelSpark.d.ts.map +1 -1
- package/dist/components/PanelSpark.js +3 -2
- package/dist/components/PanelSpark.js.map +1 -1
- package/dist/components/Playground.d.ts.map +1 -1
- package/dist/components/Playground.js +10 -4
- package/dist/components/Playground.js.map +1 -1
- package/dist/components/PromptControls.d.ts.map +1 -1
- package/dist/components/PromptControls.js +4 -2
- package/dist/components/PromptControls.js.map +1 -1
- package/dist/components/SparkLineChart.d.ts.map +1 -1
- package/dist/components/SparkLineChart.js +22 -26
- package/dist/components/SparkLineChart.js.map +1 -1
- package/dist/components/TimeControlBar.d.ts.map +1 -1
- package/dist/components/TimeControlBar.js +5 -3
- package/dist/components/TimeControlBar.js.map +1 -1
- package/dist/components/TokenizedMarkdown.d.ts +23 -0
- package/dist/components/TokenizedMarkdown.d.ts.map +1 -0
- package/dist/components/TokenizedMarkdown.js +144 -0
- package/dist/components/TokenizedMarkdown.js.map +1 -0
- package/dist/components/TokenizedMessage.d.ts +6 -0
- package/dist/components/TokenizedMessage.d.ts.map +1 -1
- package/dist/components/TokenizedMessage.js +20 -19
- package/dist/components/TokenizedMessage.js.map +1 -1
- package/dist/components/TopBarPlayers.d.ts.map +1 -1
- package/dist/components/TopBarPlayers.js +1 -1
- package/dist/components/TopBarPlayers.js.map +1 -1
- package/dist/components/Tutorial/BrowserNarration.d.ts +55 -0
- package/dist/components/Tutorial/BrowserNarration.d.ts.map +1 -0
- package/dist/components/Tutorial/BrowserNarration.js +240 -0
- package/dist/components/Tutorial/BrowserNarration.js.map +1 -0
- package/dist/components/Tutorial/TutorialBox.d.ts +26 -0
- package/dist/components/Tutorial/TutorialBox.d.ts.map +1 -0
- package/dist/components/Tutorial/TutorialBox.js +239 -0
- package/dist/components/Tutorial/TutorialBox.js.map +1 -0
- package/dist/components/Tutorial/TutorialDevPanel.d.ts +9 -0
- package/dist/components/Tutorial/TutorialDevPanel.d.ts.map +1 -0
- package/dist/components/Tutorial/TutorialDevPanel.js +92 -0
- package/dist/components/Tutorial/TutorialDevPanel.js.map +1 -0
- package/dist/components/Tutorial/TutorialOverlay.d.ts +7 -0
- package/dist/components/Tutorial/TutorialOverlay.d.ts.map +1 -0
- package/dist/components/Tutorial/TutorialOverlay.js +1132 -0
- package/dist/components/Tutorial/TutorialOverlay.js.map +1 -0
- package/dist/components/Tutorial/targetRects.d.ts +67 -0
- package/dist/components/Tutorial/targetRects.d.ts.map +1 -0
- package/dist/components/Tutorial/targetRects.js +241 -0
- package/dist/components/Tutorial/targetRects.js.map +1 -0
- package/dist/game/Chit.d.ts +7 -0
- package/dist/game/Chit.d.ts.map +1 -1
- package/dist/game/Chit.js +20 -5
- package/dist/game/Chit.js.map +1 -1
- package/dist/game/ClientTimeState.d.ts +14 -0
- package/dist/game/ClientTimeState.d.ts.map +1 -1
- package/dist/game/ClientTimeState.js +17 -0
- package/dist/game/ClientTimeState.js.map +1 -1
- package/dist/game/Game.d.ts +6 -0
- package/dist/game/Game.d.ts.map +1 -1
- package/dist/game/GameTheme.d.ts +3 -0
- package/dist/game/GameTheme.d.ts.map +1 -1
- package/dist/game/GameTheme.js +2 -0
- package/dist/game/GameTheme.js.map +1 -1
- package/dist/game/Match.d.ts +8 -2
- package/dist/game/Match.d.ts.map +1 -1
- package/dist/game/Match.js +4 -1
- package/dist/game/Match.js.map +1 -1
- package/dist/game/Pick.d.ts +2 -0
- package/dist/game/Pick.d.ts.map +1 -1
- package/dist/game/Pick.js +17 -1
- package/dist/game/Pick.js.map +1 -1
- package/dist/game/PlayerPromptStatusChit.d.ts +8 -0
- package/dist/game/PlayerPromptStatusChit.d.ts.map +1 -1
- package/dist/game/PlayerPromptStatusChit.js +5 -1
- package/dist/game/PlayerPromptStatusChit.js.map +1 -1
- package/dist/game/Prompt.d.ts +15 -0
- package/dist/game/Prompt.d.ts.map +1 -1
- package/dist/game/Prompt.js +16 -0
- package/dist/game/Prompt.js.map +1 -1
- package/dist/game/Turn.d.ts +15 -4
- package/dist/game/Turn.d.ts.map +1 -1
- package/dist/game/Turn.js +173 -60
- package/dist/game/Turn.js.map +1 -1
- package/dist/game/TurnState.d.ts +12 -1
- package/dist/game/TurnState.d.ts.map +1 -1
- package/dist/game/TurnState.js +15 -3
- package/dist/game/TurnState.js.map +1 -1
- package/dist/game/ai/AiClientPrompts.d.ts +20 -3
- package/dist/game/ai/AiClientPrompts.d.ts.map +1 -1
- package/dist/game/ai/AiClientPrompts.js +42 -6
- package/dist/game/ai/AiClientPrompts.js.map +1 -1
- package/dist/game/ai/AiPlayerView.js +2 -0
- package/dist/game/ai/AiPlayerView.js.map +1 -1
- package/dist/game/clientTransport/ClientPrompts.d.ts +20 -1
- package/dist/game/clientTransport/ClientPrompts.d.ts.map +1 -1
- package/dist/game/clientTransport/ClientPrompts.js +30 -1
- package/dist/game/clientTransport/ClientPrompts.js.map +1 -1
- package/dist/game/clientTransport/ClientTime.d.ts +13 -0
- package/dist/game/clientTransport/ClientTime.d.ts.map +1 -1
- package/dist/game/clientTransport/ClientTime.js +51 -11
- package/dist/game/clientTransport/ClientTime.js.map +1 -1
- package/dist/game/tutorial/TutorialController.d.ts +300 -0
- package/dist/game/tutorial/TutorialController.d.ts.map +1 -0
- package/dist/game/tutorial/TutorialController.js +1182 -0
- package/dist/game/tutorial/TutorialController.js.map +1 -0
- package/dist/game/tutorial/TutorialRuntime.d.ts +89 -0
- package/dist/game/tutorial/TutorialRuntime.d.ts.map +1 -0
- package/dist/game/tutorial/TutorialRuntime.js +261 -0
- package/dist/game/tutorial/TutorialRuntime.js.map +1 -0
- package/dist/game/tutorial/TutorialTheme.d.ts +28 -0
- package/dist/game/tutorial/TutorialTheme.d.ts.map +1 -0
- package/dist/game/tutorial/TutorialTheme.js +39 -0
- package/dist/game/tutorial/TutorialTheme.js.map +1 -0
- package/dist/game/tutorial/allowed.d.ts +2 -0
- package/dist/game/tutorial/allowed.d.ts.map +1 -0
- package/dist/game/tutorial/allowed.js +119 -0
- package/dist/game/tutorial/allowed.js.map +1 -0
- package/dist/game/tutorial/checks.d.ts +58 -0
- package/dist/game/tutorial/checks.d.ts.map +1 -0
- package/dist/game/tutorial/checks.js +144 -0
- package/dist/game/tutorial/checks.js.map +1 -0
- package/dist/game/tutorial/createDemoMatch.d.ts +47 -0
- package/dist/game/tutorial/createDemoMatch.d.ts.map +1 -0
- package/dist/game/tutorial/createDemoMatch.js +67 -0
- package/dist/game/tutorial/createDemoMatch.js.map +1 -0
- package/dist/game/tutorial/defineTutorial.d.ts +353 -0
- package/dist/game/tutorial/defineTutorial.d.ts.map +1 -0
- package/dist/game/tutorial/defineTutorial.js +86 -0
- package/dist/game/tutorial/defineTutorial.js.map +1 -0
- package/dist/game/tutorial/runTutorial.d.ts +63 -0
- package/dist/game/tutorial/runTutorial.d.ts.map +1 -0
- package/dist/game/tutorial/runTutorial.js +273 -0
- package/dist/game/tutorial/runTutorial.js.map +1 -0
- package/dist/hooks/usePanelNavigation.d.ts +28 -0
- package/dist/hooks/usePanelNavigation.d.ts.map +1 -0
- package/dist/hooks/usePanelNavigation.js +36 -0
- package/dist/hooks/usePanelNavigation.js.map +1 -0
- package/dist/hooks/useTimeController.d.ts +2 -1
- package/dist/hooks/useTimeController.d.ts.map +1 -1
- package/dist/hooks/useTimeController.js +6 -2
- package/dist/hooks/useTimeController.js.map +1 -1
- package/dist/hooks/useTutorial.d.ts +48 -0
- package/dist/hooks/useTutorial.d.ts.map +1 -0
- package/dist/hooks/useTutorial.js +16 -0
- package/dist/hooks/useTutorial.js.map +1 -0
- package/dist/index.d.ts +36 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.html +12 -0
- package/dist/index.js +19 -2
- package/dist/index.js.map +1 -1
- package/dist/main.0792d734cde2c688d5f7.hot-update.js +16991 -0
- package/dist/main.0792d734cde2c688d5f7.hot-update.js.map +1 -0
- package/dist/main.0792d734cde2c688d5f7.hot-update.json +1 -0
- package/dist/pretty-chitty.js +204513 -0
- package/dist/pretty-chitty.js.map +1 -0
- package/dist/rendering/CameraWrapperPerspective.d.ts +5 -0
- package/dist/rendering/CameraWrapperPerspective.d.ts.map +1 -1
- package/dist/rendering/CameraWrapperPerspective.js +16 -0
- package/dist/rendering/CameraWrapperPerspective.js.map +1 -1
- package/dist/rendering/ChitGalleryItemInstance.d.ts +1 -0
- package/dist/rendering/ChitGalleryItemInstance.d.ts.map +1 -1
- package/dist/rendering/ChitGalleryItemInstance.js +12 -0
- package/dist/rendering/ChitGalleryItemInstance.js.map +1 -1
- package/dist/rendering/ChitRenderInstance.d.ts +9 -1
- package/dist/rendering/ChitRenderInstance.d.ts.map +1 -1
- package/dist/rendering/ChitRenderInstance.js +28 -0
- package/dist/rendering/ChitRenderInstance.js.map +1 -1
- package/dist/rendering/RootChitRenderInstance.d.ts +2 -0
- package/dist/rendering/RootChitRenderInstance.d.ts.map +1 -1
- package/dist/rendering/RootChitRenderInstance.js +29 -0
- package/dist/rendering/RootChitRenderInstance.js.map +1 -1
- package/dist/utilities/Annotations.d.ts +15 -0
- package/dist/utilities/Annotations.d.ts.map +1 -1
- package/dist/utilities/Annotations.js +31 -7
- package/dist/utilities/Annotations.js.map +1 -1
- package/dist/utilities/CanvasStack/CanvasStack.d.ts +2 -0
- package/dist/utilities/CanvasStack/CanvasStack.d.ts.map +1 -1
- package/dist/utilities/CanvasStack/CanvasStack.js +4 -0
- package/dist/utilities/CanvasStack/CanvasStack.js.map +1 -1
- package/dist/utilities/TokenizedText.d.ts +6 -0
- package/dist/utilities/TokenizedText.d.ts.map +1 -1
- package/dist/utilities/TokenizedText.js.map +1 -1
- package/dist/utilities/zIndex.d.ts +1 -0
- package/dist/utilities/zIndex.d.ts.map +1 -1
- package/dist/utilities/zIndex.js +1 -0
- package/dist/utilities/zIndex.js.map +1 -1
- package/package.json +3 -2
- package/src/library/components/ActionLogDisplay.tsx +1 -0
- package/src/library/components/BottomBarButton.tsx +5 -0
- package/src/library/components/ContextGalleryDisplay.tsx +2 -0
- package/src/library/components/DemoWrapper.tsx +147 -44
- package/src/library/components/FullScreenGalleryDisplay.tsx +3 -0
- package/src/library/components/Gallery/BuiltItem.ts +30 -9
- package/src/library/components/Gallery/GalleryController.ts +72 -1
- package/src/library/components/Gallery/GalleryViewer.tsx +54 -10
- package/src/library/components/Gallery/LayoutManager.ts +30 -6
- package/src/library/components/Gallery/constants.ts +2 -0
- package/src/library/components/Gallery/registry.ts +44 -0
- package/src/library/components/GameDesigner.tsx +121 -12
- package/src/library/components/InlineGalleryDisplay.tsx +5 -0
- package/src/library/components/LiveButton.tsx +8 -5
- package/src/library/components/MatchViewer.tsx +14 -3
- package/src/library/components/Panel/MultiPanel.tsx +21 -0
- package/src/library/components/Panel/PanelContents.tsx +70 -1
- package/src/library/components/Panel/PanelTabStack.tsx +1 -0
- package/src/library/components/PanelSpark.tsx +9 -4
- package/src/library/components/Playground.tsx +48 -21
- package/src/library/components/PromptControls.tsx +7 -3
- package/src/library/components/SparkLineChart.tsx +23 -27
- package/src/library/components/TimeControlBar.tsx +5 -3
- package/src/library/components/TokenizedMarkdown.tsx +224 -0
- package/src/library/components/TokenizedMessage.tsx +28 -32
- package/src/library/components/TopBarPlayers.tsx +12 -9
- package/src/library/components/Tutorial/BrowserNarration.ts +233 -0
- package/src/library/components/Tutorial/TutorialBox.tsx +506 -0
- package/src/library/components/Tutorial/TutorialDevPanel.tsx +297 -0
- package/src/library/components/Tutorial/TutorialOverlay.tsx +1301 -0
- package/src/library/components/Tutorial/targetRects.ts +273 -0
- package/src/library/game/Chit.ts +21 -5
- package/src/library/game/ClientTimeState.ts +18 -0
- package/src/library/game/Game.ts +7 -0
- package/src/library/game/GameTheme.ts +4 -0
- package/src/library/game/Match.ts +12 -2
- package/src/library/game/Pick.ts +16 -1
- package/src/library/game/PlayerPromptStatusChit.ts +5 -1
- package/src/library/game/Prompt.ts +20 -0
- package/src/library/game/Turn.ts +179 -61
- package/src/library/game/TurnState.ts +26 -3
- package/src/library/game/ai/AI_GUIDE.md +10 -0
- package/src/library/game/ai/AiClientPrompts.ts +60 -11
- package/src/library/game/ai/AiPlayerView.ts +2 -0
- package/src/library/game/clientTransport/ClientPrompts.ts +31 -1
- package/src/library/game/clientTransport/ClientTime.ts +54 -13
- package/src/library/game/serialize.test.ts +275 -0
- package/src/library/game/tutorial/TUTORIAL_GUIDE.md +411 -0
- package/src/library/game/tutorial/TutorialController.test.ts +731 -0
- package/src/library/game/tutorial/TutorialController.ts +1286 -0
- package/src/library/game/tutorial/TutorialRuntime.ts +311 -0
- package/src/library/game/tutorial/TutorialTheme.ts +67 -0
- package/src/library/game/tutorial/allowed.ts +145 -0
- package/src/library/game/tutorial/checks.ts +186 -0
- package/src/library/game/tutorial/createDemoMatch.ts +115 -0
- package/src/library/game/tutorial/defineTutorial.ts +414 -0
- package/src/library/game/tutorial/runTutorial.ts +331 -0
- package/src/library/game/undo.test.ts +282 -0
- package/src/library/hooks/usePanelNavigation.tsx +65 -0
- package/src/library/hooks/useTimeController.tsx +6 -2
- package/src/library/hooks/useTutorial.tsx +76 -0
- package/src/library/index.ts +57 -2
- package/src/library/rendering/CameraWrapperPerspective.ts +17 -0
- package/src/library/rendering/ChitGalleryItemInstance.ts +12 -0
- package/src/library/rendering/ChitRenderInstance.ts +34 -0
- package/src/library/rendering/RootChitRenderInstance.ts +30 -1
- package/src/library/utilities/Annotations.ts +35 -7
- package/src/library/utilities/CanvasStack/CanvasStack.ts +6 -1
- package/src/library/utilities/TokenizedText.ts +6 -0
- package/src/library/utilities/zIndex.ts +1 -0
|
@@ -0,0 +1,331 @@
|
|
|
1
|
+
import { Game } from "../Game";
|
|
2
|
+
import { PlayerChit } from "../PlayerChit";
|
|
3
|
+
import { RootChit } from "../RootChit";
|
|
4
|
+
import { Chit } from "../Chit";
|
|
5
|
+
import { PickPrompt } from "../Prompt";
|
|
6
|
+
import { connectAiPlayer } from "../ai/connectAiPlayer";
|
|
7
|
+
import type { AiContext, AiOption } from "../ai/defineAi";
|
|
8
|
+
import { GalleryCommand, TutorialGalleryState, TutorialSpec, TutorialStep, selectChits } from "./defineTutorial";
|
|
9
|
+
import { isAllowed, promptMoves, shapeOfAiOption, stepsWithoutAllow } from "./allowed";
|
|
10
|
+
import { createDemoMatch } from "./createDemoMatch";
|
|
11
|
+
import { TutorialController, TutorialEvent, TutorialView } from "./TutorialController";
|
|
12
|
+
import type { TutorialRuntime } from "./TutorialRuntime";
|
|
13
|
+
|
|
14
|
+
export type RunTutorialOptions<P extends PlayerChit, R extends RootChit<P>> = {
|
|
15
|
+
/**
|
|
16
|
+
* Plays the human seat: given the step the tutorial is on and the legal options, return the index
|
|
17
|
+
* to play, or undefined to take the first option the step's `allow` list accepts (a player
|
|
18
|
+
* following instructions). A step that allows nothing is never answered.
|
|
19
|
+
*/
|
|
20
|
+
answer?: (
|
|
21
|
+
step: TutorialStep<P, R> | undefined,
|
|
22
|
+
options: { option: AiOption; score: number }[],
|
|
23
|
+
ctx: AiContext<P, R, any>,
|
|
24
|
+
) => number | undefined;
|
|
25
|
+
/** Give up on a step after this long. Default 10000 ms */
|
|
26
|
+
stepTimeoutMs?: number;
|
|
27
|
+
/**
|
|
28
|
+
* Does what the player would do outside the game itself when a step starts: open or switch a
|
|
29
|
+
* gallery (`ui.gallery({ of: (root, me) => me.hand })`). Headless runs have no gallery buttons, so a
|
|
30
|
+
* step that waits for one needs this.
|
|
31
|
+
*/
|
|
32
|
+
ui?: (step: TutorialStep<P, R>, ui: { gallery: (command: GalleryCommand<P, R>) => void }) => void;
|
|
33
|
+
/** Let the bots log. Default false */
|
|
34
|
+
log?: boolean;
|
|
35
|
+
/**
|
|
36
|
+
* Stop as soon as this step (an id) goes live, and keep going past steps that wait on the screen
|
|
37
|
+
* (a gallery button…) rather than the game: the run is there to reach the step, not to test the
|
|
38
|
+
* script. `DemoWrapper` uses it to skip ahead, from the run's `runtime` snapshots.
|
|
39
|
+
*/
|
|
40
|
+
until?: string;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export type RunTutorialResult<P extends PlayerChit = PlayerChit, R extends RootChit<P> = RootChit<P>> = {
|
|
44
|
+
outcome: "completed" | "stuck" | "error";
|
|
45
|
+
/** Step ids in the order they were entered */
|
|
46
|
+
visited: string[];
|
|
47
|
+
/** The step the run got stuck on, if any */
|
|
48
|
+
stuckOn?: string;
|
|
49
|
+
error?: string;
|
|
50
|
+
warnings: string[];
|
|
51
|
+
rngRemaining: number;
|
|
52
|
+
/** The run's runtime: its snapshots hold the match as each visited step went live */
|
|
53
|
+
runtime: TutorialRuntime<P, R>;
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Plays a tutorial headlessly: builds its match like `DemoWrapper` does, seats the bots, drives the
|
|
58
|
+
* human seat with `answer` (or the game's AI rules), and walks the steps, asserting every `waitFor`
|
|
59
|
+
* becomes satisfiable. Use it in a test so a rules change that breaks a script fails CI.
|
|
60
|
+
*
|
|
61
|
+
* ```ts
|
|
62
|
+
* test("intro tutorial can be completed", async () => {
|
|
63
|
+
* const result = await runTutorial(new MyGame(), intro);
|
|
64
|
+
* expect(result.outcome).toBe("completed");
|
|
65
|
+
* });
|
|
66
|
+
* ```
|
|
67
|
+
*
|
|
68
|
+
* @group Tutorials
|
|
69
|
+
*/
|
|
70
|
+
export async function runTutorial<P extends PlayerChit, R extends RootChit<P>>(
|
|
71
|
+
game: Game<P, R>,
|
|
72
|
+
spec: TutorialSpec<P, R>,
|
|
73
|
+
options: RunTutorialOptions<P, R> = {},
|
|
74
|
+
): Promise<RunTutorialResult<P, R>> {
|
|
75
|
+
const demo = await createDemoMatch(game, spec, {
|
|
76
|
+
immediateNotify: true,
|
|
77
|
+
ai: { thinkMs: 0, log: options.log ?? false },
|
|
78
|
+
});
|
|
79
|
+
const { match, playerId, runtime } = demo;
|
|
80
|
+
const visited: string[] = [];
|
|
81
|
+
let error: string | undefined;
|
|
82
|
+
|
|
83
|
+
// Headless galleries: no screen, just what is open, so gallery checks and commands behave
|
|
84
|
+
let galleries: TutorialGalleryState[] = [];
|
|
85
|
+
let uiListeners: (() => void)[] = [];
|
|
86
|
+
const simulateGallery = (command: GalleryCommand<P, R>): boolean => {
|
|
87
|
+
if (command === "close") {
|
|
88
|
+
galleries = [];
|
|
89
|
+
return false;
|
|
90
|
+
}
|
|
91
|
+
const root = view.root();
|
|
92
|
+
const me = view.me();
|
|
93
|
+
let state: TutorialGalleryState | undefined;
|
|
94
|
+
let opened = false;
|
|
95
|
+
if (command.of) {
|
|
96
|
+
const container = selectChits(command.of, root, me)[0];
|
|
97
|
+
if (container) {
|
|
98
|
+
state = { mode: "inline", of: container.id, chitIds: container.children.map((c) => c.id ?? "") };
|
|
99
|
+
opened = true;
|
|
100
|
+
}
|
|
101
|
+
} else if (command.items) {
|
|
102
|
+
const chits = selectChits(command.items, root, me);
|
|
103
|
+
if (chits.length > 0) {
|
|
104
|
+
state = { mode: "inline", chitIds: chits.map((c) => c.id ?? "") };
|
|
105
|
+
opened = true;
|
|
106
|
+
}
|
|
107
|
+
} else if (galleries[0]) {
|
|
108
|
+
state = { ...galleries[0] };
|
|
109
|
+
}
|
|
110
|
+
if (!state) return false;
|
|
111
|
+
if (command.mode === "fullscreen" || command.mode === "inline") state.mode = command.mode;
|
|
112
|
+
galleries = [state];
|
|
113
|
+
return opened;
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
const view: TutorialView<P, R> = {
|
|
117
|
+
root: () => match.turn.value?.rootChit as R | undefined,
|
|
118
|
+
me: () => match.turn.value?.rootChit.players.find((p) => p.id === playerId) as P | undefined,
|
|
119
|
+
prompt: () => {
|
|
120
|
+
const p = match.turn.value?.rootChit.players.find((pl) => pl.id === playerId);
|
|
121
|
+
const prompt = p?.promptStatus.latestPrompt.value;
|
|
122
|
+
return prompt instanceof PickPrompt ? prompt : undefined;
|
|
123
|
+
},
|
|
124
|
+
clock: () => match.turn.value?.clockDetails(playerId).clock ?? 0,
|
|
125
|
+
find: (id: string) => {
|
|
126
|
+
let found: Chit | undefined;
|
|
127
|
+
match.turn.value?.rootChit.walk((c) => {
|
|
128
|
+
if (c.id === id) found = c;
|
|
129
|
+
return !found;
|
|
130
|
+
});
|
|
131
|
+
return found;
|
|
132
|
+
},
|
|
133
|
+
latestLog: () => {
|
|
134
|
+
const logs = match.turn.value?.gameLog(playerId);
|
|
135
|
+
return logs?.[logs.length - 1]?.message;
|
|
136
|
+
},
|
|
137
|
+
live: () => true,
|
|
138
|
+
onChange: (cb) => {
|
|
139
|
+
uiListeners.push(cb);
|
|
140
|
+
const unsub = match.onChange(cb, false);
|
|
141
|
+
return () => {
|
|
142
|
+
unsub();
|
|
143
|
+
uiListeners = uiListeners.filter((l) => l !== cb);
|
|
144
|
+
};
|
|
145
|
+
},
|
|
146
|
+
galleries: () => galleries,
|
|
147
|
+
setGallery: (command) => {
|
|
148
|
+
const opened = simulateGallery(command);
|
|
149
|
+
uiListeners.forEach((l) => l());
|
|
150
|
+
return opened;
|
|
151
|
+
},
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
let finished = false;
|
|
155
|
+
let finishedEnd: TutorialEvent | undefined;
|
|
156
|
+
// The human seat is played by a bot that behaves like a player following instructions: it answers
|
|
157
|
+
// only while a "do it" step is waiting, and only with a move the step allows (the engine's Confirm
|
|
158
|
+
// included); any other prompt it holds until the script lets it act. `answer` can pick for it.
|
|
159
|
+
// eslint-disable-next-line prefer-const
|
|
160
|
+
let human: ReturnType<typeof connectAiPlayer> | undefined;
|
|
161
|
+
const controller = new TutorialController<P, R>({
|
|
162
|
+
spec,
|
|
163
|
+
view,
|
|
164
|
+
runtime,
|
|
165
|
+
ai: match.ai,
|
|
166
|
+
// Snapshots for skipping ahead (see `until`); restoring is never asked for headlessly
|
|
167
|
+
history: {
|
|
168
|
+
save: (stepId) => runtime.saveSnapshot(stepId, JSON.parse(JSON.stringify(match.state ?? {}))),
|
|
169
|
+
restore: async () => false,
|
|
170
|
+
},
|
|
171
|
+
satisfiedDelayMs: 0,
|
|
172
|
+
// Headless: narration takes no time, so a step's own narration never delays the run
|
|
173
|
+
readingSpeed: Number.POSITIVE_INFINITY,
|
|
174
|
+
onEvent: (e) => {
|
|
175
|
+
if (e.type === "step") {
|
|
176
|
+
visited.push(e.stepId);
|
|
177
|
+
const step = controller.step;
|
|
178
|
+
if (step && options.ui) {
|
|
179
|
+
options.ui(step, { gallery: (command) => view.setGallery?.(command) });
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
if (e.type === "completed" || e.type === "skipped") {
|
|
183
|
+
finished = true;
|
|
184
|
+
finishedEnd = e;
|
|
185
|
+
}
|
|
186
|
+
// The step or its state changed: a held prompt may now be answerable (or not)
|
|
187
|
+
human?.prompts.recheck();
|
|
188
|
+
},
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
human = connectAiPlayer(match, playerId, {
|
|
192
|
+
thinkMs: 0,
|
|
193
|
+
log: options.log ?? false,
|
|
194
|
+
temperature: 0,
|
|
195
|
+
choose: (scored, ctx) => {
|
|
196
|
+
const step = controller.step;
|
|
197
|
+
if (!step?.waitFor || controller.status.value !== "waiting") return null;
|
|
198
|
+
const chosen = options.answer?.(step, scored, ctx as AiContext<P, R, any>);
|
|
199
|
+
if (chosen !== undefined) return chosen;
|
|
200
|
+
const root = ctx.view as R;
|
|
201
|
+
const me = ctx.me as P;
|
|
202
|
+
const index = scored.findIndex((s) => isAllowed(step.allow, shapeOfAiOption(s.option), root, me));
|
|
203
|
+
return index >= 0 ? index : null;
|
|
204
|
+
},
|
|
205
|
+
});
|
|
206
|
+
// It plays the person, not a bot: keep it out of the bots' switch, so `pauseAi` and resuming the
|
|
207
|
+
// bots never pause it or let it act during narration
|
|
208
|
+
match.ai.unregister(human);
|
|
209
|
+
human.prompts.setEnabled(true);
|
|
210
|
+
stepsWithoutAllow(spec.steps).forEach((id) =>
|
|
211
|
+
console.warn(`[tutorial ${spec.id}] step "${id}" waits for the player but allows no move (set \`allow\`)`),
|
|
212
|
+
);
|
|
213
|
+
|
|
214
|
+
const run = match.start().catch((e) => {
|
|
215
|
+
error = e instanceof Error ? e.message : String(e);
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
// Wait for the board and for setup to reach its first prompt (so setup draws stay "setup"), then start
|
|
219
|
+
await waitUntil(() => !!match.turn.value?.rootChit || !!error, 5000);
|
|
220
|
+
await waitUntil(
|
|
221
|
+
() =>
|
|
222
|
+
!!error ||
|
|
223
|
+
!!match.errorState.value ||
|
|
224
|
+
!!match.turn.value?.rootChit.players.some((p) => !!p.promptStatus.latestPrompt.value),
|
|
225
|
+
10000,
|
|
226
|
+
);
|
|
227
|
+
controller.start();
|
|
228
|
+
|
|
229
|
+
const untilIndex = options.until === undefined ? -1 : spec.steps.findIndex((s) => s.id === options.until);
|
|
230
|
+
const stepTimeout = options.stepTimeoutMs ?? 10000;
|
|
231
|
+
let stuckOn: string | undefined;
|
|
232
|
+
while (!finished && !error && !match.errorState.value) {
|
|
233
|
+
const index = controller.index.value;
|
|
234
|
+
const step = controller.step;
|
|
235
|
+
if (!step) break;
|
|
236
|
+
if (untilIndex >= 0 && index >= untilIndex) break;
|
|
237
|
+
// Skipping ahead: a step that waits for a screen control, with no game move it allows on offer
|
|
238
|
+
// for a while (a prompt can arrive a moment late), is jumped past
|
|
239
|
+
if (untilIndex >= 0 && step.waitFor && allowsUiControl(step)) {
|
|
240
|
+
let onScreenSince: number | undefined;
|
|
241
|
+
const onScreenOnly = () => {
|
|
242
|
+
const prompt = view.prompt();
|
|
243
|
+
const root = view.root();
|
|
244
|
+
const me = view.me();
|
|
245
|
+
if (prompt && promptMoves(prompt).some((m) => isAllowed(step.allow, m.shape, root, me))) {
|
|
246
|
+
onScreenSince = undefined;
|
|
247
|
+
return false;
|
|
248
|
+
}
|
|
249
|
+
// Only screen controls allowed: nothing to wait for
|
|
250
|
+
if (!allowsGameMove(step)) return true;
|
|
251
|
+
onScreenSince ??= Date.now();
|
|
252
|
+
return Date.now() - onScreenSince >= 50;
|
|
253
|
+
};
|
|
254
|
+
const moved = await waitUntil(() => controller.index.value !== index || finished || !!error || onScreenOnly(), stepTimeout);
|
|
255
|
+
if (controller.index.value === index && !finished && !error && onScreenOnly()) controller.jumpTo(index + 1);
|
|
256
|
+
if (moved) continue;
|
|
257
|
+
stuckOn = step.id;
|
|
258
|
+
break;
|
|
259
|
+
}
|
|
260
|
+
// Narration steps advance when the reading clock ends; speed that up headlessly
|
|
261
|
+
if (!step.waitFor) {
|
|
262
|
+
await waitUntil(() => controller.status.value === "narrating" || finished, 1000);
|
|
263
|
+
controller.next();
|
|
264
|
+
continue;
|
|
265
|
+
}
|
|
266
|
+
const moved = await waitUntil(() => controller.index.value !== index || finished || !!error, stepTimeout);
|
|
267
|
+
if (!moved) {
|
|
268
|
+
stuckOn = step.id;
|
|
269
|
+
break;
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
const outcome: RunTutorialResult<P, R>["outcome"] = error || match.errorState.value ? "error" : stuckOn ? "stuck" : "completed";
|
|
274
|
+
controller.dispose();
|
|
275
|
+
// The match first: a run stopped mid-game (`until`) still has bots moving, and the player's
|
|
276
|
+
// connection must not be gone while the match still reports to it
|
|
277
|
+
match.dispose();
|
|
278
|
+
// and its replies already on their way get a moment to land
|
|
279
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
280
|
+
human?.dispose();
|
|
281
|
+
void run;
|
|
282
|
+
void finishedEnd;
|
|
283
|
+
|
|
284
|
+
return {
|
|
285
|
+
outcome,
|
|
286
|
+
visited,
|
|
287
|
+
stuckOn,
|
|
288
|
+
error: error ?? match.errorState.value,
|
|
289
|
+
warnings: runtime.warnings.value,
|
|
290
|
+
rngRemaining: runtime.rngRemaining,
|
|
291
|
+
runtime,
|
|
292
|
+
};
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/** Whether a step's `allow` names a screen control (a gallery button…), not only game moves */
|
|
296
|
+
function allowsUiControl(step: TutorialStep<any, any>) {
|
|
297
|
+
const list = step.allow === undefined ? [] : Array.isArray(step.allow) ? step.allow : [step.allow];
|
|
298
|
+
return list.some((a) => a !== "any" && a.ui !== undefined);
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/** Whether a step's `allow` lists a game move (anything but screen controls) */
|
|
302
|
+
function allowsGameMove(step: TutorialStep<any, any>) {
|
|
303
|
+
const list = step.allow === undefined ? [] : Array.isArray(step.allow) ? step.allow : [step.allow];
|
|
304
|
+
return list.some((a) => a === "any" || a.ui === undefined);
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
async function waitUntil(pred: () => boolean, timeoutMs: number): Promise<boolean> {
|
|
308
|
+
const until = Date.now() + timeoutMs;
|
|
309
|
+
while (Date.now() < until) {
|
|
310
|
+
if (pred()) return true;
|
|
311
|
+
await yieldToEvents();
|
|
312
|
+
}
|
|
313
|
+
return pred();
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
/**
|
|
317
|
+
* Lets pending work (the match, the bots) run, then resumes. Not `setTimeout`: browsers stretch
|
|
318
|
+
* repeated short timers to 4 ms or more, and a skip-ahead waits like this a few hundred times.
|
|
319
|
+
*/
|
|
320
|
+
function yieldToEvents(): Promise<void> {
|
|
321
|
+
if (typeof MessageChannel === "undefined") return new Promise((resolve) => setTimeout(resolve, 0));
|
|
322
|
+
return new Promise((resolve) => {
|
|
323
|
+
const channel = new MessageChannel();
|
|
324
|
+
channel.port1.onmessage = () => {
|
|
325
|
+
channel.port1.close();
|
|
326
|
+
resolve();
|
|
327
|
+
};
|
|
328
|
+
channel.port2.postMessage(undefined);
|
|
329
|
+
});
|
|
330
|
+
}
|
|
331
|
+
|
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
import { describe, expect, test } from "vitest";
|
|
2
|
+
|
|
3
|
+
import { Chit } from "./Chit";
|
|
4
|
+
import { AiPlayerView } from "./ai/AiPlayerView";
|
|
5
|
+
import { ClientStatus } from "./clientTransport/ClientStatus";
|
|
6
|
+
import { Connection } from "./Connection";
|
|
7
|
+
import { ConnectionObject } from "./ConnectionObject";
|
|
8
|
+
import { LocalConnectionTransport } from "./ConnectionTransport";
|
|
9
|
+
import { ClockDetails, samePasses } from "./ClockDetails";
|
|
10
|
+
import { Game } from "./Game";
|
|
11
|
+
import { GameButton } from "./GameButton";
|
|
12
|
+
import { Match } from "./Match";
|
|
13
|
+
import { EphemeralMatchStorage } from "./MatchStorage";
|
|
14
|
+
import { OrderedOutlet } from "./OrderedOutlet";
|
|
15
|
+
import { ButtonPick, ChitPick } from "./Pick";
|
|
16
|
+
import { PlayerChit } from "./PlayerChit";
|
|
17
|
+
import { PlayerInfo } from "./PlayerInfo";
|
|
18
|
+
import { PickPrompt } from "./Prompt";
|
|
19
|
+
import { RootChit } from "./RootChit";
|
|
20
|
+
import { Turn } from "./Turn";
|
|
21
|
+
|
|
22
|
+
//
|
|
23
|
+
// A player picks one of two options; the chosen option spawns a piece (which "comes from" the option, so it
|
|
24
|
+
// has a parentFallback) onto the first cell. The player then moves the piece to another cell and says they
|
|
25
|
+
// are done. Every step can be undone. In the "nested" game the spawn and the move happen in a sub-turn of
|
|
26
|
+
// the player's turn, so undoing the move's prompt bubbles up and restarts the player's turn.
|
|
27
|
+
//
|
|
28
|
+
|
|
29
|
+
class Piece extends Chit {}
|
|
30
|
+
class PieceA extends Piece {}
|
|
31
|
+
class PieceB extends Piece {}
|
|
32
|
+
|
|
33
|
+
class Option extends Chit {
|
|
34
|
+
spawn(): Piece {
|
|
35
|
+
const piece = this instanceof OptionA ? new PieceA() : new PieceB();
|
|
36
|
+
piece.parentFallback = this;
|
|
37
|
+
return piece;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
class OptionA extends Option {}
|
|
41
|
+
class OptionB extends Option {}
|
|
42
|
+
|
|
43
|
+
class Cell extends Chit {
|
|
44
|
+
pieces = new OrderedOutlet<Piece>("pieces", this);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
class Seat extends PlayerChit {}
|
|
48
|
+
|
|
49
|
+
class Board extends RootChit<Seat> {
|
|
50
|
+
minPlayers = 1;
|
|
51
|
+
options = new OrderedOutlet<Option>("options", this);
|
|
52
|
+
cells = new OrderedOutlet<Cell>("cells", this);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
class Done extends GameButton {
|
|
56
|
+
label = "Done";
|
|
57
|
+
}
|
|
58
|
+
class Again extends GameButton {
|
|
59
|
+
label = "Again";
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function makeGame(nested: boolean): Game<Seat, Board> {
|
|
63
|
+
return {
|
|
64
|
+
chitLibrary: { Root: Board, Player: Seat, Board, Seat, Cell, OptionA, OptionB, PieceA, PieceB },
|
|
65
|
+
buttonLibrary: { Done, Again },
|
|
66
|
+
canvasLibrary: {},
|
|
67
|
+
async run(setup: Turn<any, Seat, Board>, root: Board) {
|
|
68
|
+
root.options.add(new OptionA());
|
|
69
|
+
root.options.add(new OptionB());
|
|
70
|
+
for (let i = 0; i < 3; i++) {
|
|
71
|
+
root.cells.add(new Cell());
|
|
72
|
+
}
|
|
73
|
+
setup.flush();
|
|
74
|
+
|
|
75
|
+
for (let round = 0; round < 3; round++) {
|
|
76
|
+
for (const player of root.players.copy()) {
|
|
77
|
+
await setup.createTurn([root], player, async (turn) => {
|
|
78
|
+
let option: Option | undefined;
|
|
79
|
+
await turn.pick(
|
|
80
|
+
Chit.pick(root.options, (o: Option) => {
|
|
81
|
+
option = o;
|
|
82
|
+
}).message("choose an option"),
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
const play = async (turn: Turn<any, Seat, Board>) => {
|
|
86
|
+
const piece = option!.spawn();
|
|
87
|
+
root.cells.copy()[0].pieces.add(piece);
|
|
88
|
+
await turn.pick(
|
|
89
|
+
Chit.pick(root.cells.copy().slice(1), (cell: Cell) => cell.pieces.add(piece)).message("move the piece"),
|
|
90
|
+
);
|
|
91
|
+
await turn.pick([new Done(() => {}).pick().message("done"), new Again(() => {}).pick().message("again")]);
|
|
92
|
+
};
|
|
93
|
+
if (nested) {
|
|
94
|
+
await turn.createTurn([root], player, play);
|
|
95
|
+
} else {
|
|
96
|
+
await play(turn);
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return { winners: [] };
|
|
102
|
+
},
|
|
103
|
+
} as unknown as Game<Seat, Board>;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* A client as the browser has one: the engine's headless ClientTime stand-in (`AiPlayerView`, which applies
|
|
108
|
+
* each response exactly as ClientTime does) behind a real connection, plus ClientTime's clock walking on
|
|
109
|
+
* top: a live client steps through every clock, and jumps to the latest one on a new pass.
|
|
110
|
+
*/
|
|
111
|
+
class SteppingView extends AiPlayerView {
|
|
112
|
+
private target = 0;
|
|
113
|
+
private walking: Promise<void> = Promise.resolve();
|
|
114
|
+
/** Where every piece was after each response, oldest first */
|
|
115
|
+
readonly timeline: string[] = [];
|
|
116
|
+
|
|
117
|
+
override async newMaxClock(newMax: ClockDetails) {
|
|
118
|
+
const old = this.maxClock;
|
|
119
|
+
const isSamePass = samePasses(old, newMax);
|
|
120
|
+
if (isSamePass && old.clock === newMax.clock) return;
|
|
121
|
+
this.maxClock = newMax;
|
|
122
|
+
this.walking = this.walking.then(async () => {
|
|
123
|
+
if (!isSamePass) {
|
|
124
|
+
this.target = newMax.clock;
|
|
125
|
+
await this.step();
|
|
126
|
+
}
|
|
127
|
+
while (this.target < newMax.clock) {
|
|
128
|
+
this.target++;
|
|
129
|
+
await this.step();
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
await this.walking;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
private async step() {
|
|
136
|
+
await this.sync(this.target);
|
|
137
|
+
this.timeline.push(this.pieces());
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** Every piece on the client's board as "Type@parentId", sorted; "" when the board holds none */
|
|
141
|
+
pieces(): string {
|
|
142
|
+
const result: string[] = [];
|
|
143
|
+
this.root?.walk((c) => {
|
|
144
|
+
if (c instanceof Piece) result.push(`${c.chitTypeName().split("-")[0]}@${c.parent!.id}`);
|
|
145
|
+
});
|
|
146
|
+
return result.sort().join(" ");
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** The prompt side of a client: ServerPrompts hands it each prompt with the clock it was asked at */
|
|
151
|
+
class PromptSink extends ConnectionObject {
|
|
152
|
+
constructor(private view: SteppingView) {
|
|
153
|
+
super();
|
|
154
|
+
}
|
|
155
|
+
async setPromptForPlayer(_playerId: string, _prompt?: unknown, clockDetails?: ClockDetails) {
|
|
156
|
+
if (clockDetails) await this.view.newMaxClock(clockDetails);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function connectClient(match: Match<Seat, Board>, game: Game<Seat, Board>, playerId: string): SteppingView {
|
|
161
|
+
const serverSide = new LocalConnectionTransport(0);
|
|
162
|
+
const clientSide = new LocalConnectionTransport(0);
|
|
163
|
+
const connection = new Connection(clientSide);
|
|
164
|
+
const view = new SteppingView(connection, game);
|
|
165
|
+
connection.register(view, "ClientTime");
|
|
166
|
+
connection.register(new ClientStatus<any, any>(connection), "ClientStatus");
|
|
167
|
+
connection.register(new PromptSink(view), "ClientPrompts");
|
|
168
|
+
clientSide.connect(serverSide);
|
|
169
|
+
match.connect(serverSide, playerId);
|
|
170
|
+
return view;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
describe.each([
|
|
174
|
+
["in the player's turn", false],
|
|
175
|
+
["in a sub-turn of the player's turn", true],
|
|
176
|
+
])("undo, with the piece spawned %s", (_, nested) => {
|
|
177
|
+
test("an undone move puts the piece straight back; an undone choice removes it on both sides", async () => {
|
|
178
|
+
const game = makeGame(nested);
|
|
179
|
+
const players = [new PlayerInfo("p0", "Player 0")];
|
|
180
|
+
const match = new Match(game, players, new EphemeralMatchStorage(`undo-${nested}`), {});
|
|
181
|
+
await match.load();
|
|
182
|
+
match.immediateNotify = true;
|
|
183
|
+
|
|
184
|
+
const turn = () => match.turn.value!;
|
|
185
|
+
const root = () => turn().rootChit;
|
|
186
|
+
const player = () => root().players.copy()[0];
|
|
187
|
+
const client = connectClient(match, game, "p0");
|
|
188
|
+
const finished = match.start();
|
|
189
|
+
|
|
190
|
+
/** Waits until everything the server has sent has reached the client and been applied */
|
|
191
|
+
const settle = async () => {
|
|
192
|
+
for (let i = 0; i < 500; i++) {
|
|
193
|
+
await new Promise((resolve) => setTimeout(resolve, 2));
|
|
194
|
+
if (JSON.stringify(client.maxClock) === JSON.stringify(turn().clockDetails("p0"))) {
|
|
195
|
+
await client.sync();
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
throw new Error("Client never caught up");
|
|
200
|
+
};
|
|
201
|
+
const nextPrompt = async (previous?: PickPrompt): Promise<PickPrompt> => {
|
|
202
|
+
for (let i = 0; i < 200; i++) {
|
|
203
|
+
const prompt = player().promptStatus.latestPrompt.value as PickPrompt | undefined;
|
|
204
|
+
if (prompt && prompt !== previous) {
|
|
205
|
+
await settle();
|
|
206
|
+
return prompt;
|
|
207
|
+
}
|
|
208
|
+
await new Promise((resolve) => setTimeout(resolve, 1));
|
|
209
|
+
}
|
|
210
|
+
throw new Error("No prompt arrived");
|
|
211
|
+
};
|
|
212
|
+
const pickChit = (prompt: PickPrompt, chitType: string) => {
|
|
213
|
+
const pick = prompt.picks[0] as ChitPick<Chit>;
|
|
214
|
+
const chit = pick.chits.find((c) => c.chitTypeName() === chitType) ?? pick.chits[0];
|
|
215
|
+
prompt.resolvePick(pick, chit.id);
|
|
216
|
+
};
|
|
217
|
+
const pickButton = (prompt: PickPrompt, message: string) => {
|
|
218
|
+
const pick = prompt.picks.find((p) => p.messageContents === message) as ButtonPick;
|
|
219
|
+
prompt.resolvePick(pick, undefined);
|
|
220
|
+
};
|
|
221
|
+
const serverPieces = () => {
|
|
222
|
+
const result: string[] = [];
|
|
223
|
+
root().walk((c) => {
|
|
224
|
+
if (c instanceof Piece) result.push(`${c.chitTypeName().split("-")[0]}@${c.parent!.id}`);
|
|
225
|
+
});
|
|
226
|
+
return result.sort().join(" ");
|
|
227
|
+
};
|
|
228
|
+
const cell = (i: number) => root().cells.copy()[i].id!;
|
|
229
|
+
|
|
230
|
+
try {
|
|
231
|
+
// 1. choose option A: PieceA spawns onto the first cell
|
|
232
|
+
let prompt = await nextPrompt();
|
|
233
|
+
expect(prompt.message).toBe("choose an option");
|
|
234
|
+
pickChit(prompt, "OptionA");
|
|
235
|
+
prompt = await nextPrompt(prompt);
|
|
236
|
+
expect(prompt.message).toBe("move the piece");
|
|
237
|
+
expect(client.pieces()).toBe(`PieceA@${cell(0)}`);
|
|
238
|
+
|
|
239
|
+
// 2. move it to the second cell
|
|
240
|
+
pickChit(prompt, "Cell");
|
|
241
|
+
prompt = await nextPrompt(prompt);
|
|
242
|
+
expect(prompt.message).toBe("done or again");
|
|
243
|
+
expect(client.pieces()).toBe(`PieceA@${cell(1)}`);
|
|
244
|
+
|
|
245
|
+
// 3. undo the move: the piece goes straight back to the first cell, nowhere else on the way
|
|
246
|
+
let beforeUndo = client.timeline.length;
|
|
247
|
+
prompt.stepBack();
|
|
248
|
+
prompt = await nextPrompt(prompt);
|
|
249
|
+
expect(prompt.message).toBe("move the piece");
|
|
250
|
+
expect(serverPieces()).toBe(`PieceA@${cell(0)}`);
|
|
251
|
+
expect(client.pieces()).toBe(`PieceA@${cell(0)}`);
|
|
252
|
+
expect(client.timeline.slice(beforeUndo)).toEqual([`PieceA@${cell(0)}`]);
|
|
253
|
+
|
|
254
|
+
// 4. undo the choice: the piece is gone
|
|
255
|
+
beforeUndo = client.timeline.length;
|
|
256
|
+
prompt.stepBack();
|
|
257
|
+
prompt = await nextPrompt(prompt);
|
|
258
|
+
expect(prompt.message).toBe("choose an option");
|
|
259
|
+
expect(serverPieces()).toBe("");
|
|
260
|
+
expect(client.pieces()).toBe("");
|
|
261
|
+
expect(client.timeline.slice(beforeUndo)).toEqual([""]);
|
|
262
|
+
|
|
263
|
+
// 5. choose option B instead: only PieceB is on the board
|
|
264
|
+
pickChit(prompt, "OptionB");
|
|
265
|
+
prompt = await nextPrompt(prompt);
|
|
266
|
+
expect(prompt.message).toBe("move the piece");
|
|
267
|
+
expect(serverPieces()).toBe(`PieceB@${cell(0)}`);
|
|
268
|
+
expect(client.pieces()).toBe(`PieceB@${cell(0)}`);
|
|
269
|
+
|
|
270
|
+
// 6. and the game goes on
|
|
271
|
+
pickChit(prompt, "Cell");
|
|
272
|
+
prompt = await nextPrompt(prompt);
|
|
273
|
+
pickButton(prompt, "done");
|
|
274
|
+
prompt = await nextPrompt(prompt);
|
|
275
|
+
expect(prompt.message).toBe("confirm turn end");
|
|
276
|
+
expect(client.pieces()).toBe(`PieceB@${cell(1)}`);
|
|
277
|
+
} finally {
|
|
278
|
+
match.dispose();
|
|
279
|
+
await finished;
|
|
280
|
+
}
|
|
281
|
+
});
|
|
282
|
+
});
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import React, { createContext, useContext, useEffect, useState } from "react";
|
|
2
|
+
import { Chit } from "../game/Chit";
|
|
3
|
+
import { EventChannel } from "../utilities/EventChannel";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Lets something outside the panels (the tutorial overlay) bring a chit on screen: switch the tab stack
|
|
7
|
+
* that holds its panel, and leave a zoomed-in panel. Provided by `PanelContents`.
|
|
8
|
+
*/
|
|
9
|
+
export interface PanelNavigation {
|
|
10
|
+
/** Resolves once the panel holding the chit is in front (after the slide), or false if it has no panel. */
|
|
11
|
+
revealChit(chit: Chit): Promise<boolean>;
|
|
12
|
+
/** Panel roots (chits that own a viewer) currently laid out, in layout order */
|
|
13
|
+
panelRoots(): Chit[];
|
|
14
|
+
/** @internal MultiPanel registers how to bring each of its panels to the front */
|
|
15
|
+
registerSelector(chitId: string, select: () => void): () => void;
|
|
16
|
+
/** @internal */
|
|
17
|
+
isFront(chitId: string): boolean;
|
|
18
|
+
/**
|
|
19
|
+
* Keeps the panel holding `chit` in front for a while (it is in front already): its tab group does
|
|
20
|
+
* not jump to another panel just because something changed there.
|
|
21
|
+
*/
|
|
22
|
+
keepFront(chit: Chit): void;
|
|
23
|
+
/** The panel shown full screen, with every panel as a tab (the player zoomed in, or a tutorial focused it) */
|
|
24
|
+
focusedPanel(): Chit | undefined;
|
|
25
|
+
/**
|
|
26
|
+
* Shows the panel holding `chit` full screen, as zooming in does (undefined: back to the normal
|
|
27
|
+
* layout). Resolves after the slide, or false if the chit has no panel.
|
|
28
|
+
*/
|
|
29
|
+
focusPanelOf(chit: Chit | undefined): Promise<boolean>;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export const PanelNavigationContext = createContext<PanelNavigation | undefined>(undefined);
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Where `PanelContents` leaves its navigation for components that are not inside it (the tutorial
|
|
36
|
+
* overlay is a sibling of the panels, not a child). Provided by the match viewer.
|
|
37
|
+
*/
|
|
38
|
+
const PanelNavigationHolderContext = createContext<EventChannel<PanelNavigation | undefined> | undefined>(undefined);
|
|
39
|
+
|
|
40
|
+
/** @internal Wraps a viewer so that anything in it can reach the panels' navigation */
|
|
41
|
+
export function PanelNavigationHolder({ children }: { children: React.ReactNode }) {
|
|
42
|
+
const [holder] = useState(() => new EventChannel<PanelNavigation | undefined>(undefined));
|
|
43
|
+
return <PanelNavigationHolderContext.Provider value={holder}>{children}</PanelNavigationHolderContext.Provider>;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** @internal `PanelContents` publishes its navigation to the viewer's holder */
|
|
47
|
+
export function usePublishPanelNavigation(navigation: PanelNavigation) {
|
|
48
|
+
const holder = useContext(PanelNavigationHolderContext);
|
|
49
|
+
useEffect(() => {
|
|
50
|
+
if (!holder) return;
|
|
51
|
+
holder.value = navigation;
|
|
52
|
+
return () => {
|
|
53
|
+
if (holder.value === navigation) holder.value = undefined;
|
|
54
|
+
};
|
|
55
|
+
}, [holder, navigation]);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** The panels' navigation, from inside the panels or from anywhere else in the viewer */
|
|
59
|
+
export function usePanelNavigation(): PanelNavigation | undefined {
|
|
60
|
+
const direct = useContext(PanelNavigationContext);
|
|
61
|
+
const holder = useContext(PanelNavigationHolderContext);
|
|
62
|
+
const [held, setHeld] = useState(holder?.value);
|
|
63
|
+
useEffect(() => (holder ? holder.on(setHeld) : undefined), [holder]);
|
|
64
|
+
return direct ?? held;
|
|
65
|
+
}
|