@pretty-chitty/core 1.2.18 → 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/ClientTrustMatchViewer.d.ts +14 -1
- package/dist/components/ClientTrustMatchViewer.d.ts.map +1 -1
- package/dist/components/ClientTrustMatchViewer.js +25 -2
- package/dist/components/ClientTrustMatchViewer.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 -43
- 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 +31 -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 +70 -1
- 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 +46 -14
- 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 +8 -6
- package/dist/components/TokenizedMessage.d.ts.map +1 -1
- package/dist/components/TokenizedMessage.js +21 -54
- package/dist/components/TokenizedMessage.js.map +1 -1
- package/dist/components/TopBarPlayers.d.ts.map +1 -1
- package/dist/components/TopBarPlayers.js +2 -2
- 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 +22 -7
- 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/ConnectionTransport.d.ts +3 -0
- package/dist/game/ConnectionTransport.d.ts.map +1 -1
- package/dist/game/ConnectionTransport.js +13 -4
- package/dist/game/ConnectionTransport.js.map +1 -1
- package/dist/game/Game.d.ts +14 -1
- 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 +74 -2
- package/dist/game/Match.d.ts.map +1 -1
- package/dist/game/Match.js +155 -4
- package/dist/game/Match.js.map +1 -1
- package/dist/game/MatchStorage.d.ts +8 -1
- package/dist/game/MatchStorage.d.ts.map +1 -1
- package/dist/game/MatchStorage.js +11 -3
- package/dist/game/MatchStorage.js.map +1 -1
- package/dist/game/Pick.d.ts +16 -0
- package/dist/game/Pick.d.ts.map +1 -1
- package/dist/game/Pick.js +32 -1
- package/dist/game/Pick.js.map +1 -1
- package/dist/game/PlayerChit.d.ts +3 -0
- package/dist/game/PlayerChit.d.ts.map +1 -1
- package/dist/game/PlayerChit.js +3 -0
- package/dist/game/PlayerChit.js.map +1 -1
- package/dist/game/PlayerInfo.d.ts +8 -0
- package/dist/game/PlayerInfo.d.ts.map +1 -1
- package/dist/game/PlayerInfo.js +6 -3
- package/dist/game/PlayerInfo.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 +8 -2
- 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 +20 -4
- package/dist/game/Turn.d.ts.map +1 -1
- package/dist/game/Turn.js +191 -67
- package/dist/game/Turn.js.map +1 -1
- package/dist/game/TurnState.d.ts +20 -1
- package/dist/game/TurnState.d.ts.map +1 -1
- package/dist/game/TurnState.js +22 -3
- package/dist/game/TurnState.js.map +1 -1
- package/dist/game/ai/AiClientPrompts.d.ts +177 -0
- package/dist/game/ai/AiClientPrompts.d.ts.map +1 -0
- package/dist/game/ai/AiClientPrompts.js +561 -0
- package/dist/game/ai/AiClientPrompts.js.map +1 -0
- package/dist/game/ai/AiLiveView.d.ts +2 -0
- package/dist/game/ai/AiLiveView.d.ts.map +1 -0
- package/dist/game/ai/AiLiveView.js +48 -0
- package/dist/game/ai/AiLiveView.js.map +1 -0
- package/dist/game/ai/AiPlayerView.d.ts +2 -0
- package/dist/game/ai/AiPlayerView.d.ts.map +1 -0
- package/dist/game/ai/AiPlayerView.js +91 -0
- package/dist/game/ai/AiPlayerView.js.map +1 -0
- package/dist/game/ai/connectAiPlayer.d.ts +43 -0
- package/dist/game/ai/connectAiPlayer.d.ts.map +1 -0
- package/dist/game/ai/connectAiPlayer.js +58 -0
- package/dist/game/ai/connectAiPlayer.js.map +1 -0
- package/dist/game/ai/defineAi.d.ts +119 -0
- package/dist/game/ai/defineAi.d.ts.map +1 -0
- package/dist/game/ai/defineAi.js +122 -0
- package/dist/game/ai/defineAi.js.map +1 -0
- package/dist/game/ai/simulateMatches.d.ts +62 -0
- package/dist/game/ai/simulateMatches.d.ts.map +1 -0
- package/dist/game/ai/simulateMatches.js +108 -0
- package/dist/game/ai/simulateMatches.js.map +1 -0
- package/dist/game/badAiTransport/BadAIClientPrompts.d.ts +3 -0
- package/dist/game/badAiTransport/BadAIClientPrompts.d.ts.map +1 -1
- package/dist/game/badAiTransport/BadAIClientPrompts.js +3 -0
- package/dist/game/badAiTransport/BadAIClientPrompts.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/useTokenMap.d.ts +2 -4
- package/dist/hooks/useTokenMap.d.ts.map +1 -1
- package/dist/hooks/useTokenMap.js +2 -5
- package/dist/hooks/useTokenMap.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 +57 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.html +12 -0
- package/dist/index.js +27 -3
- 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 +13 -0
- package/dist/utilities/CanvasStack/CanvasStack.d.ts.map +1 -1
- package/dist/utilities/CanvasStack/CanvasStack.js +66 -15
- package/dist/utilities/CanvasStack/CanvasStack.js.map +1 -1
- package/dist/utilities/TokenizedText.d.ts +55 -0
- package/dist/utilities/TokenizedText.d.ts.map +1 -0
- package/dist/utilities/TokenizedText.js +47 -0
- package/dist/utilities/TokenizedText.js.map +1 -0
- 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/ClientTrustMatchViewer.tsx +32 -2
- package/src/library/components/ContextGalleryDisplay.tsx +2 -0
- package/src/library/components/DemoWrapper.tsx +147 -52
- package/src/library/components/FullScreenGalleryDisplay.tsx +3 -0
- package/src/library/components/Gallery/BuiltItem.ts +34 -9
- package/src/library/components/Gallery/GalleryController.ts +75 -2
- 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 +125 -33
- 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 +30 -95
- package/src/library/components/TopBarPlayers.tsx +18 -10
- 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 +23 -7
- package/src/library/game/ClientTimeState.ts +18 -0
- package/src/library/game/ConnectionTransport.ts +12 -3
- package/src/library/game/Game.ts +16 -1
- package/src/library/game/GameTheme.ts +4 -0
- package/src/library/game/Match.ts +174 -5
- package/src/library/game/MatchStorage.ts +14 -4
- package/src/library/game/Pick.ts +36 -1
- package/src/library/game/PlayerChit.ts +4 -1
- package/src/library/game/PlayerInfo.ts +13 -3
- package/src/library/game/PlayerPromptStatusChit.ts +8 -2
- package/src/library/game/Prompt.ts +20 -0
- package/src/library/game/Turn.ts +198 -68
- package/src/library/game/TurnState.ts +34 -3
- package/src/library/game/ai/AI_GUIDE.md +444 -0
- package/src/library/game/ai/AiClientPrompts.ts +730 -0
- package/src/library/game/ai/AiLiveView.ts +63 -0
- package/src/library/game/ai/AiPlayerView.ts +114 -0
- package/src/library/game/ai/ai.test.ts +475 -0
- package/src/library/game/ai/connectAiPlayer.ts +88 -0
- package/src/library/game/ai/defineAi.ts +230 -0
- package/src/library/game/ai/simulateMatches.ts +172 -0
- package/src/library/game/badAiTransport/BadAIClientPrompts.ts +3 -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/useTokenMap.ts +2 -6
- package/src/library/hooks/useTutorial.tsx +76 -0
- package/src/library/index.ts +91 -4
- 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 +74 -29
- package/src/library/utilities/TokenizedText.test.ts +32 -0
- package/src/library/utilities/TokenizedText.ts +103 -0
- package/src/library/utilities/zIndex.ts +1 -0
|
@@ -0,0 +1,1286 @@
|
|
|
1
|
+
import { EventChannel } from "../../utilities/EventChannel";
|
|
2
|
+
import { Chit } from "../Chit";
|
|
3
|
+
import { PlayerChit } from "../PlayerChit";
|
|
4
|
+
import { PickPrompt, PromptAction } from "../Prompt";
|
|
5
|
+
import { RootChit } from "../RootChit";
|
|
6
|
+
import { allowsUi, isAllowed, scriptedAnswer, shapeOfPromptAction } from "./allowed";
|
|
7
|
+
import {
|
|
8
|
+
BoxPlacement,
|
|
9
|
+
Checker,
|
|
10
|
+
Cue,
|
|
11
|
+
Focus,
|
|
12
|
+
GalleryCommand,
|
|
13
|
+
Pose,
|
|
14
|
+
TimelineCommand,
|
|
15
|
+
TutorialGalleryState,
|
|
16
|
+
TutorialContext,
|
|
17
|
+
TutorialSpec,
|
|
18
|
+
TutorialStep,
|
|
19
|
+
selectChits,
|
|
20
|
+
splitSentences,
|
|
21
|
+
} from "./defineTutorial";
|
|
22
|
+
import type { TutorialRuntime } from "./TutorialRuntime";
|
|
23
|
+
|
|
24
|
+
/** How the controller reads the game: the player's own view in the browser, the live match headlessly. @internal */
|
|
25
|
+
export interface TutorialView<P extends PlayerChit, R extends RootChit<P>> {
|
|
26
|
+
root(): R | undefined;
|
|
27
|
+
me(): P | undefined;
|
|
28
|
+
prompt(): PickPrompt | undefined;
|
|
29
|
+
clock(): number;
|
|
30
|
+
find(id: string): Chit | undefined;
|
|
31
|
+
/** Latest log line, if any */
|
|
32
|
+
latestLog(): string | undefined;
|
|
33
|
+
/** False while the player is scrubbing history */
|
|
34
|
+
live(): boolean;
|
|
35
|
+
/** Fires whenever any of the above may have changed */
|
|
36
|
+
onChange(cb: () => void): () => void;
|
|
37
|
+
/**
|
|
38
|
+
* Installs (or removes) the vet for the player's answers: only actions it approves reach the game.
|
|
39
|
+
* Headless views have no human input and may ignore it.
|
|
40
|
+
*/
|
|
41
|
+
setGuard?(guard: ((action: PromptAction) => boolean | "handled") | undefined): void;
|
|
42
|
+
/** Lets the game's Undo button act as the tutorial's Back (it is offered exactly as before) */
|
|
43
|
+
setUndo?(undo: { undo: () => void } | undefined): void;
|
|
44
|
+
/** Fires when the guard drops an attempted action */
|
|
45
|
+
onBlocked?(cb: (action: PromptAction) => void): () => void;
|
|
46
|
+
/** Galleries open right now (full-screen first). Views without galleries return [] or omit it. */
|
|
47
|
+
galleries?(): TutorialGalleryState[];
|
|
48
|
+
/** Opens, switches or closes a gallery. Returns true when it opened one. */
|
|
49
|
+
setGallery?(command: GalleryCommand<P, R>): boolean;
|
|
50
|
+
/** Changes a chit in this player's view only (see `Pose`). Without it, poses do nothing. */
|
|
51
|
+
pose?(chit: Chit, change: (chit: Chit) => void): void;
|
|
52
|
+
/** Puts a posed chit back as the game has it */
|
|
53
|
+
unpose?(chit: Chit): void;
|
|
54
|
+
/**
|
|
55
|
+
* The board's timeline, for `timeline` commands: the clock it shows (or is animating to), and a
|
|
56
|
+
* hold on how far it may play (undefined: follow the game). Without it, timeline commands do nothing.
|
|
57
|
+
*/
|
|
58
|
+
timeline?: { at(): number; hold(clock: number | undefined): void };
|
|
59
|
+
/** Presses a UI control by its `data-tutorial-target` name, for "do it for me". Returns whether it found one. */
|
|
60
|
+
pressUi?(name: string): boolean;
|
|
61
|
+
/** Rushes the board's animations while the tutorial fast-forwards */
|
|
62
|
+
setFastForward?(on: boolean): void;
|
|
63
|
+
/** Whether the board has caught up with the game and stopped animating (fast-forward waits for it) */
|
|
64
|
+
settled?(): boolean;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Plays narration. The browser implementation wraps an audio element and speech synthesis. @internal */
|
|
68
|
+
export interface NarrationPlayer {
|
|
69
|
+
/** Resolves when playback has started (or failed); `onTime` and `onSentence` then fire until `onEnd`. */
|
|
70
|
+
play(step: { audio: string; text: string; sentences: string[] }): Promise<void>;
|
|
71
|
+
stop(): void;
|
|
72
|
+
pause(): void;
|
|
73
|
+
resume(): void;
|
|
74
|
+
readonly playing: boolean;
|
|
75
|
+
readonly paused: boolean;
|
|
76
|
+
/**
|
|
77
|
+
* Starts loading a later step's recording, so it plays at once when its step comes (a phone
|
|
78
|
+
* otherwise waits for each file to download). Optional.
|
|
79
|
+
*/
|
|
80
|
+
preload?(audio: string): void;
|
|
81
|
+
/** Seconds since the narration started */
|
|
82
|
+
readonly time: number;
|
|
83
|
+
/** Total length in seconds, when known */
|
|
84
|
+
readonly duration: number | undefined;
|
|
85
|
+
/**
|
|
86
|
+
* Playback speed (1 = normal). `time` and `duration` stay in the recording's own seconds, so cues
|
|
87
|
+
* and `sentenceTimes` need no adjusting.
|
|
88
|
+
*/
|
|
89
|
+
setRate?(rate: number): void;
|
|
90
|
+
onTime(cb: (seconds: number) => void): () => void;
|
|
91
|
+
/** Sentence boundaries, when the player knows them (speech synthesis does) */
|
|
92
|
+
onSentence(cb: (index: number) => void): () => void;
|
|
93
|
+
onEnd(cb: () => void): () => void;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export type TutorialStatus = "idle" | "narrating" | "waiting" | "satisfied" | "done";
|
|
97
|
+
|
|
98
|
+
export type TutorialEvent =
|
|
99
|
+
| { type: "started" }
|
|
100
|
+
| { type: "step"; stepId: string; index: number }
|
|
101
|
+
/** A "do it" step's check passed (fired synchronously, before the tutorial moves on) */
|
|
102
|
+
| { type: "satisfied"; stepId: string }
|
|
103
|
+
| { type: "completed"; end: "continue" | "back" }
|
|
104
|
+
| { type: "skipped" };
|
|
105
|
+
|
|
106
|
+
export type TutorialControllerOptions<P extends PlayerChit, R extends RootChit<P>> = {
|
|
107
|
+
spec: TutorialSpec<P, R>;
|
|
108
|
+
view: TutorialView<P, R>;
|
|
109
|
+
runtime?: TutorialRuntime<P, R>;
|
|
110
|
+
narration?: NarrationPlayer;
|
|
111
|
+
/** The match's bot switch, for `pauseAi` */
|
|
112
|
+
ai?: { enabled: boolean };
|
|
113
|
+
/**
|
|
114
|
+
* Lets Back put the game back where it was: `save` is called when a step goes live, `restore`
|
|
115
|
+
* when the player goes back to it (the host then rebuilds the match and restarts the script at
|
|
116
|
+
* that step). Without it, Back only reviews the earlier step's text.
|
|
117
|
+
*/
|
|
118
|
+
history?: {
|
|
119
|
+
save: (stepId: string) => void;
|
|
120
|
+
/**
|
|
121
|
+
* Resolves true once the game shows the board as it was when `stepId` went live; false if it
|
|
122
|
+
* cannot. Also how `skipTo` reaches a step not played yet: `DemoWrapper` plays the script
|
|
123
|
+
* headlessly up to it first.
|
|
124
|
+
*/
|
|
125
|
+
restore: (stepId: string, index: number) => Promise<boolean>;
|
|
126
|
+
};
|
|
127
|
+
onEvent?: (e: TutorialEvent) => void;
|
|
128
|
+
/** Pause between a `waitFor` passing and moving on. Default 900 ms */
|
|
129
|
+
satisfiedDelayMs?: number;
|
|
130
|
+
/** Pause between a narration ending and moving on (steps without `pause`). Default 700 ms */
|
|
131
|
+
autoAdvanceDelayMs?: number;
|
|
132
|
+
/** Words per second for the reading clock used when a step has no audio. Default 3; Infinity = instant */
|
|
133
|
+
readingSpeed?: number;
|
|
134
|
+
/** Audio off: cues run on the reading clock. Default false */
|
|
135
|
+
muted?: boolean;
|
|
136
|
+
/** Narration speed: 1 (default), 1.25, 1.5 or 2. The reading clock runs at the same speed. */
|
|
137
|
+
rate?: number;
|
|
138
|
+
/** Timers: injectable for tests */
|
|
139
|
+
setTimeout?: (cb: () => void, ms: number) => any;
|
|
140
|
+
clearTimeout?: (handle: any) => void;
|
|
141
|
+
now?: () => number;
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
const DEFAULT_HINT_AFTER = 8;
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Runs a tutorial script against a game the player can see: steps, narration and cues, "do it" checks,
|
|
148
|
+
* hints and the end-of-tutorial handoff. UI-free, so it runs headlessly in tests; the overlay reads
|
|
149
|
+
* its channels and calls `next`/`back`/`skip`.
|
|
150
|
+
*
|
|
151
|
+
* @group Tutorials
|
|
152
|
+
*/
|
|
153
|
+
/** The pause between steps while fast-forwarding */
|
|
154
|
+
const FAST_BEAT_MS = 60;
|
|
155
|
+
/** How long the board must stay settled before a fast-forward moves on */
|
|
156
|
+
const FAST_SETTLE_MS = 150;
|
|
157
|
+
/** A fast-forward stuck this long on one step stops there */
|
|
158
|
+
const FAST_GIVE_UP_MS = 20000;
|
|
159
|
+
|
|
160
|
+
export class TutorialController<P extends PlayerChit = PlayerChit, R extends RootChit<P> = RootChit<P>> {
|
|
161
|
+
public readonly spec: TutorialSpec<P, R>;
|
|
162
|
+
public readonly index = new EventChannel<number>(-1);
|
|
163
|
+
public readonly status = new EventChannel<TutorialStatus>("idle");
|
|
164
|
+
public readonly focus = new EventChannel<Focus<P, R>[]>([]);
|
|
165
|
+
public readonly box = new EventChannel<BoxPlacement<P, R> | undefined>(undefined);
|
|
166
|
+
public readonly hint = new EventChannel<string | undefined>(undefined);
|
|
167
|
+
/** Index of the sentence being narrated, for emphasis */
|
|
168
|
+
public readonly sentence = new EventChannel<number>(-1);
|
|
169
|
+
public readonly narrationTime = new EventChannel<number>(0);
|
|
170
|
+
public readonly narrating = new EventChannel<boolean>(false);
|
|
171
|
+
/**
|
|
172
|
+
* The player stopped the flow (Pause, or Back): narration holds where it is, nothing advances by
|
|
173
|
+
* itself, the bots hold their moves, and moving between steps with Back/Next stays silent until
|
|
174
|
+
* `play()`. A "do it" step can still be completed while paused; doing it counts as resuming.
|
|
175
|
+
*/
|
|
176
|
+
public readonly paused = new EventChannel<boolean>(false);
|
|
177
|
+
/** A Back is putting the game back; input and Next are held until it lands */
|
|
178
|
+
public readonly rewinding = new EventChannel<boolean>(false);
|
|
179
|
+
public readonly muted: EventChannel<boolean>;
|
|
180
|
+
/** Narration speed (1 = normal) */
|
|
181
|
+
public readonly rate: EventChannel<number>;
|
|
182
|
+
/** Result of the current step's `waitFor` at the last evaluation */
|
|
183
|
+
public readonly check = new EventChannel<boolean | string | undefined>(undefined);
|
|
184
|
+
/**
|
|
185
|
+
* True while looking at a step behind the furthest one reached (after Back). A reviewed step
|
|
186
|
+
* narrates and highlights but does not check, pause the bots or auto-advance; Next walks forward
|
|
187
|
+
* until the live step resumes with its state intact.
|
|
188
|
+
*/
|
|
189
|
+
public readonly reviewing = new EventChannel<boolean>(false);
|
|
190
|
+
/** Index of the furthest step reached live */
|
|
191
|
+
public readonly frontier = new EventChannel<number>(-1);
|
|
192
|
+
/** Bumps each time the player tries a move the current step does not allow */
|
|
193
|
+
public readonly blocked = new EventChannel<number>(0);
|
|
194
|
+
/** The clock the script is holding the board at, or undefined while the board plays freely */
|
|
195
|
+
public readonly timelineHold = new EventChannel<number | undefined>(undefined);
|
|
196
|
+
/** The clock the board shows, for the workbench (counting moments for `{ advance: n }`) */
|
|
197
|
+
public readonly boardClock = new EventChannel<number>(0);
|
|
198
|
+
/** The step a fast-forward is playing through to, or undefined */
|
|
199
|
+
public readonly fastForwardTarget = new EventChannel<number | undefined>(undefined);
|
|
200
|
+
|
|
201
|
+
private readonly view: TutorialView<P, R>;
|
|
202
|
+
private readonly runtime?: TutorialRuntime<P, R>;
|
|
203
|
+
private readonly narration?: NarrationPlayer;
|
|
204
|
+
private readonly ai?: { enabled: boolean };
|
|
205
|
+
private readonly history?: TutorialControllerOptions<P, R>["history"];
|
|
206
|
+
private readonly onEvent?: (e: TutorialEvent) => void;
|
|
207
|
+
private readonly satisfiedDelayMs: number;
|
|
208
|
+
private readonly autoAdvanceDelayMs: number;
|
|
209
|
+
private readonly readingSpeed: number;
|
|
210
|
+
private readonly _setTimeout: (cb: () => void, ms: number) => any;
|
|
211
|
+
private readonly _clearTimeout: (handle: any) => void;
|
|
212
|
+
private readonly _now: () => number;
|
|
213
|
+
|
|
214
|
+
private unsubs: (() => void)[] = [];
|
|
215
|
+
private stepUnsubs: (() => void)[] = [];
|
|
216
|
+
private stepStartClock = 0;
|
|
217
|
+
private recentLog: string[] = [];
|
|
218
|
+
private lastLog: string | undefined;
|
|
219
|
+
private aiEnabledBeforePause: boolean | undefined;
|
|
220
|
+
private satisfiedTimer: any;
|
|
221
|
+
/** A step entered by acting ahead narrates after all if its check has not passed by then */
|
|
222
|
+
private aheadTimer: any;
|
|
223
|
+
private readingActive = false;
|
|
224
|
+
private advanceTimer: any;
|
|
225
|
+
private hintTimer: any;
|
|
226
|
+
private readingTimer: any;
|
|
227
|
+
private readingStartedAt = 0;
|
|
228
|
+
private readingPausedAt: number | undefined;
|
|
229
|
+
private appliedCues = new Set<number>();
|
|
230
|
+
private sentences: string[] = [];
|
|
231
|
+
private disposed = false;
|
|
232
|
+
private narrationEnded = false;
|
|
233
|
+
/** Steps whose `waitFor` baseline was taken; a live re-entry (after a review) keeps it */
|
|
234
|
+
private baselined = new Set<string>();
|
|
235
|
+
|
|
236
|
+
constructor(options: TutorialControllerOptions<P, R>) {
|
|
237
|
+
this.spec = options.spec;
|
|
238
|
+
this.view = options.view;
|
|
239
|
+
this.runtime = options.runtime;
|
|
240
|
+
this.narration = options.narration;
|
|
241
|
+
this.ai = options.ai;
|
|
242
|
+
this.history = options.history;
|
|
243
|
+
this.onEvent = options.onEvent;
|
|
244
|
+
this.satisfiedDelayMs = options.satisfiedDelayMs ?? 900;
|
|
245
|
+
this.autoAdvanceDelayMs = options.autoAdvanceDelayMs ?? 700;
|
|
246
|
+
this.readingSpeed = options.readingSpeed ?? 3;
|
|
247
|
+
this.muted = new EventChannel<boolean>(options.muted ?? false);
|
|
248
|
+
this.rate = new EventChannel<number>(options.rate ?? 1);
|
|
249
|
+
this.narration?.setRate?.(this.rate.value);
|
|
250
|
+
this._setTimeout = options.setTimeout ?? ((cb, ms) => setTimeout(cb, ms));
|
|
251
|
+
this._clearTimeout = options.clearTimeout ?? ((h) => clearTimeout(h));
|
|
252
|
+
this._now = options.now ?? (() => Date.now());
|
|
253
|
+
// Guard from the start: before Start is pressed the board is a picture, not a game
|
|
254
|
+
this.view.setGuard?.((action) => {
|
|
255
|
+
// The game's Undo is the tutorial's Back while the tutorial runs
|
|
256
|
+
if (action.type === "undo" && this.status.value !== "done") {
|
|
257
|
+
this.back();
|
|
258
|
+
return "handled";
|
|
259
|
+
}
|
|
260
|
+
if (this.allows(action)) return true;
|
|
261
|
+
// A move the next "do it" step asks for counts as moving on to that step
|
|
262
|
+
const ahead = this.aheadFor(action);
|
|
263
|
+
if (ahead === undefined) return false;
|
|
264
|
+
this.actAhead(ahead);
|
|
265
|
+
return true;
|
|
266
|
+
});
|
|
267
|
+
const unsubBlocked = this.view.onBlocked?.(() => {
|
|
268
|
+
this.blocked.value = this.blocked.value + 1;
|
|
269
|
+
});
|
|
270
|
+
if (unsubBlocked) this.unsubs.push(unsubBlocked);
|
|
271
|
+
// The first recording loads while the start card is up
|
|
272
|
+
this.preloadStep(0);
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/** Lets the narration player start loading step `index`'s recording ahead of time */
|
|
276
|
+
private preloadStep(index: number) {
|
|
277
|
+
const audio = this.spec.steps[index]?.audio;
|
|
278
|
+
if (audio && audio !== "tts") this.narration?.preload?.(audio);
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
get step(): TutorialStep<P, R> | undefined {
|
|
282
|
+
return this.spec.steps[this.index.value];
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
get steps() {
|
|
286
|
+
return this.spec.steps;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
get isLast() {
|
|
290
|
+
return this.index.value >= this.spec.steps.length - 1;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/** Whether Next is allowed right now */
|
|
294
|
+
get canAdvance() {
|
|
295
|
+
if (this.rewinding.value) return false;
|
|
296
|
+
if (this.reviewing.value) return true;
|
|
297
|
+
const s = this.status.value;
|
|
298
|
+
return s === "narrating" || s === "satisfied" || (s === "waiting" && this.check.value === true);
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/** Builds the context the checks and callbacks see. Undefined until the board exists. */
|
|
302
|
+
public context(): TutorialContext<P, R> | undefined {
|
|
303
|
+
const root = this.view.root();
|
|
304
|
+
const me = this.view.me();
|
|
305
|
+
if (!root || !me) return undefined;
|
|
306
|
+
return {
|
|
307
|
+
root,
|
|
308
|
+
me,
|
|
309
|
+
prompt: this.view.prompt(),
|
|
310
|
+
clock: this.view.clock(),
|
|
311
|
+
find: <C extends Chit = Chit>(id: string) => this.view.find(id) as C | undefined,
|
|
312
|
+
stepStartClock: this.stepStartClock,
|
|
313
|
+
recentLog: this.recentLog,
|
|
314
|
+
galleries: this.view.galleries?.() ?? [],
|
|
315
|
+
};
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
/** Starts at the first step (or the given one). Call once the board has loaded. */
|
|
319
|
+
public start(at = 0, options: { paused?: boolean } = {}) {
|
|
320
|
+
if (!this.begin(options)) return;
|
|
321
|
+
this.enter(at);
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
private began = false;
|
|
325
|
+
|
|
326
|
+
/** Starts watching the board (once): what `start` does before entering its first step */
|
|
327
|
+
private begin(options: { paused?: boolean } = {}): boolean {
|
|
328
|
+
if (this.disposed || this.began) return false;
|
|
329
|
+
this.began = true;
|
|
330
|
+
this.paused.value = !!options.paused;
|
|
331
|
+
this.unsubs.push(this.view.onChange(() => this.onViewChange()));
|
|
332
|
+
this.onEvent?.({ type: "started" });
|
|
333
|
+
this.lastLog = this.view.latestLog();
|
|
334
|
+
return true;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
public next() {
|
|
338
|
+
if (this.disposed || this.index.value < 0 || this.rewinding.value) return;
|
|
339
|
+
if (!this.canAdvance) return;
|
|
340
|
+
if (this.isLast) {
|
|
341
|
+
this.finish(this.step?.end ?? "continue");
|
|
342
|
+
} else {
|
|
343
|
+
this.enter(this.index.value + 1);
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
/**
|
|
348
|
+
* Previous step. With a history this rewinds the game to that step; otherwise it reviews its text.
|
|
349
|
+
* Either way the tutorial pauses there until the player presses Play, so pressing Back again goes
|
|
350
|
+
* further back instead of racing the narration. Presses during a rewind queue up.
|
|
351
|
+
*/
|
|
352
|
+
public back() {
|
|
353
|
+
if (this.disposed || this.index.value <= 0) return;
|
|
354
|
+
const index = this.index.value - 1;
|
|
355
|
+
this.holdFlow();
|
|
356
|
+
if (this.history) {
|
|
357
|
+
this.requestRewind(index);
|
|
358
|
+
return;
|
|
359
|
+
}
|
|
360
|
+
this.enter(index);
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
private rewindTarget?: number;
|
|
364
|
+
|
|
365
|
+
/** Show the target step straight away and put the game back to it; presses during a rewind queue up */
|
|
366
|
+
private requestRewind(index: number) {
|
|
367
|
+
if (!this.rewinding.value) {
|
|
368
|
+
this.leaveStep();
|
|
369
|
+
// the board is going back: a gallery the script opened would show the wrong moment
|
|
370
|
+
if (this.scriptOpenedGallery) this.applyGallery("close");
|
|
371
|
+
// and so would a chit the script changed; the step landed on poses its own again
|
|
372
|
+
this.releasePoses(true);
|
|
373
|
+
// the rewind waits for the board to catch up with the rerun; the step it lands on holds again
|
|
374
|
+
this.releaseTimeline();
|
|
375
|
+
this.rewinding.value = true;
|
|
376
|
+
// bots wait while the board goes back; entering the step decides whether they play
|
|
377
|
+
this.applyAi();
|
|
378
|
+
}
|
|
379
|
+
this.rewindTarget = index;
|
|
380
|
+
this.index.value = index;
|
|
381
|
+
this.publishUndo();
|
|
382
|
+
this.reviewing.value = false;
|
|
383
|
+
this.focus.value = [];
|
|
384
|
+
this.hint.value = undefined;
|
|
385
|
+
this.check.value = undefined;
|
|
386
|
+
this.narrating.value = false;
|
|
387
|
+
this.sentence.value = -1;
|
|
388
|
+
if (!this.rewindRunning) void this.runRewinds();
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
private rewindRunning = false;
|
|
392
|
+
|
|
393
|
+
private async runRewinds() {
|
|
394
|
+
this.rewindRunning = true;
|
|
395
|
+
try {
|
|
396
|
+
while (this.rewindTarget !== undefined && !this.disposed) {
|
|
397
|
+
const index = this.rewindTarget;
|
|
398
|
+
this.rewindTarget = undefined;
|
|
399
|
+
let ok = false;
|
|
400
|
+
try {
|
|
401
|
+
ok = (await this.history?.restore(this.spec.steps[index].id, index)) ?? false;
|
|
402
|
+
} catch (e) {
|
|
403
|
+
console.error(`[tutorial ${this.spec.id}] rewind to step ${index + 1} failed`, e);
|
|
404
|
+
}
|
|
405
|
+
if (this.disposed) return;
|
|
406
|
+
if (this.rewindTarget !== undefined) continue; // Back was pressed again meanwhile
|
|
407
|
+
if (ok) {
|
|
408
|
+
// The game is back where this step started: it is the live step again, and it and every
|
|
409
|
+
// later step will take fresh baselines when entered
|
|
410
|
+
this.frontier.value = index;
|
|
411
|
+
this.spec.steps.slice(index).forEach((s) => this.baselined.delete(s.id));
|
|
412
|
+
}
|
|
413
|
+
this.rewinding.value = false;
|
|
414
|
+
this.enter(index);
|
|
415
|
+
}
|
|
416
|
+
} finally {
|
|
417
|
+
this.rewindRunning = false;
|
|
418
|
+
if (this.rewinding.value && this.rewindTarget === undefined && !this.disposed) {
|
|
419
|
+
this.rewinding.value = false;
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
/** Whether Back rewinds the game (true) or only reviews text (false) */
|
|
425
|
+
get rewinds() {
|
|
426
|
+
return !!this.history;
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
/** Jump to any step (the dev panel). Forward jumps become the live step; the game is left as it is. */
|
|
430
|
+
public jumpTo(index: number) {
|
|
431
|
+
if (this.disposed || index < 0 || index >= this.spec.steps.length) return;
|
|
432
|
+
this.stopFastForward();
|
|
433
|
+
if (index < this.frontier.value && this.history) {
|
|
434
|
+
this.holdFlow();
|
|
435
|
+
this.requestRewind(index);
|
|
436
|
+
return;
|
|
437
|
+
}
|
|
438
|
+
if (index > this.frontier.value) this.frontier.value = index;
|
|
439
|
+
this.enter(index);
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
/**
|
|
443
|
+
* Go straight to any step with the game as it was when that step went live, and play on from there
|
|
444
|
+
* (the dev panel's step list). A step not played yet is played headlessly first (see `history`),
|
|
445
|
+
* so this works before Start too. Without a game history it is `jumpTo`.
|
|
446
|
+
*/
|
|
447
|
+
public skipTo(index: number) {
|
|
448
|
+
if (this.disposed || index < 0 || index >= this.spec.steps.length || this.status.value === "done") return;
|
|
449
|
+
if (!this.history) {
|
|
450
|
+
if (!this.began) this.start(index);
|
|
451
|
+
else this.jumpTo(index);
|
|
452
|
+
return;
|
|
453
|
+
}
|
|
454
|
+
this.begin();
|
|
455
|
+
this.stopFastForward();
|
|
456
|
+
this.paused.value = false;
|
|
457
|
+
this.requestRewind(index);
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
/** Leave a review and return to the live step */
|
|
461
|
+
public resume() {
|
|
462
|
+
if (this.reviewing.value) this.enter(this.frontier.value);
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
public skip() {
|
|
466
|
+
if (this.disposed) return;
|
|
467
|
+
this.onEvent?.({ type: "skipped" });
|
|
468
|
+
this.finish("continue", true);
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
/** While the tutorial runs, the game's Undo button (shown as usual) does the tutorial's Back */
|
|
472
|
+
private publishUndo() {
|
|
473
|
+
if (this.disposed || this.status.value === "done") {
|
|
474
|
+
this.view.setUndo?.(undefined);
|
|
475
|
+
return;
|
|
476
|
+
}
|
|
477
|
+
this.view.setUndo?.({ undo: () => this.back() });
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
/** Stop the flow: the narration holds where it is and nothing advances until `play()`. */
|
|
481
|
+
public pause() {
|
|
482
|
+
if (this.disposed || this.status.value === "done" || this.paused.value) return;
|
|
483
|
+
this.holdFlow();
|
|
484
|
+
// A step that was only finishing its narration moves on when the player presses Play again
|
|
485
|
+
if (this.narration?.playing && !this.narration.paused) this.narration.pause();
|
|
486
|
+
if (this.readingActive && this.readingPausedAt === undefined) this.readingPausedAt = this._now();
|
|
487
|
+
this.narrating.value = false;
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
/** Resume: continue a held narration, or play the step's narration from the top. */
|
|
491
|
+
public play() {
|
|
492
|
+
if (this.disposed || this.status.value === "done" || !this.step) return;
|
|
493
|
+
this.paused.value = false;
|
|
494
|
+
this.applyAi();
|
|
495
|
+
if (this.narration?.paused) {
|
|
496
|
+
this.narration.resume();
|
|
497
|
+
this.narrating.value = true;
|
|
498
|
+
return;
|
|
499
|
+
}
|
|
500
|
+
if (this.readingActive && this.readingPausedAt !== undefined) {
|
|
501
|
+
this.readingStartedAt += this._now() - this.readingPausedAt;
|
|
502
|
+
this.readingPausedAt = undefined;
|
|
503
|
+
this.narrating.value = true;
|
|
504
|
+
return;
|
|
505
|
+
}
|
|
506
|
+
this.startNarration();
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
/** The box's play/pause button */
|
|
510
|
+
public toggleNarration() {
|
|
511
|
+
if (this.paused.value || !this.narrating.value) this.play();
|
|
512
|
+
else this.pause();
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
private holdFlow() {
|
|
516
|
+
this.stopFastForward();
|
|
517
|
+
this.paused.value = true;
|
|
518
|
+
this._clearTimeout(this.advanceTimer);
|
|
519
|
+
this.advanceTimer = undefined;
|
|
520
|
+
this.applyAi();
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
/**
|
|
524
|
+
* Whether the bots may move: not while the flow is held (paused or going back), and not during a
|
|
525
|
+
* live step with `pauseAi`. Otherwise they play.
|
|
526
|
+
*/
|
|
527
|
+
private applyAi() {
|
|
528
|
+
if (!this.ai || this.status.value === "done") return;
|
|
529
|
+
const step = this.step;
|
|
530
|
+
const hold = this.paused.value || this.rewinding.value || (!!step?.pauseAi && !this.reviewing.value);
|
|
531
|
+
if (hold) {
|
|
532
|
+
if (this.aiEnabledBeforePause === undefined) this.aiEnabledBeforePause = this.ai.enabled;
|
|
533
|
+
this.ai.enabled = false;
|
|
534
|
+
} else {
|
|
535
|
+
this.restoreAi();
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
/** Narration for a step just entered: plays unless the flow is held, then it only shows the opening cues */
|
|
540
|
+
private beginNarration() {
|
|
541
|
+
if (this.paused.value) {
|
|
542
|
+
this.narrating.value = false;
|
|
543
|
+
this.appliedCues = new Set();
|
|
544
|
+
this.applyCuesAt(0);
|
|
545
|
+
return;
|
|
546
|
+
}
|
|
547
|
+
this.startNarration();
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
/** Speeds the narration up or down, from where it is; the reading clock follows */
|
|
551
|
+
public setRate(rate: number) {
|
|
552
|
+
if (!(rate > 0) || rate === this.rate.value) return;
|
|
553
|
+
if (this.readingActive) {
|
|
554
|
+
// keep the reading clock's position, change only how fast it moves from here
|
|
555
|
+
const at = this.readingPausedAt ?? this._now();
|
|
556
|
+
const t = ((at - this.readingStartedAt) / 1000) * this.rate.value;
|
|
557
|
+
this.readingStartedAt = at - (t * 1000) / rate;
|
|
558
|
+
}
|
|
559
|
+
this.rate.value = rate;
|
|
560
|
+
this.narration?.setRate?.(rate);
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
public setMuted(muted: boolean) {
|
|
564
|
+
if (this.muted.value === muted) return;
|
|
565
|
+
this.muted.value = muted;
|
|
566
|
+
this.narration?.stop();
|
|
567
|
+
this.narrating.value = false;
|
|
568
|
+
// Re-run the step's narration/clock from the top so cues stay consistent
|
|
569
|
+
if (this.step && this.status.value !== "done") {
|
|
570
|
+
this.beginNarration();
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
public dispose() {
|
|
575
|
+
this.disposed = true;
|
|
576
|
+
this.stopFastForward();
|
|
577
|
+
this.leaveStep();
|
|
578
|
+
this.releasePoses(true);
|
|
579
|
+
this.releaseTimeline();
|
|
580
|
+
this.view.setGuard?.(undefined);
|
|
581
|
+
this.view.setUndo?.(undefined);
|
|
582
|
+
this.unsubs.forEach((u) => u());
|
|
583
|
+
this.unsubs = [];
|
|
584
|
+
this.narration?.stop();
|
|
585
|
+
this.restoreAi();
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
// ---- what the player may do ---------------------------------------------------------------------
|
|
589
|
+
|
|
590
|
+
/**
|
|
591
|
+
* Whether the game should accept this action right now. Only a live "do it" step that is still
|
|
592
|
+
* waiting lets anything through, and then only what it lists in `allow` (the game's Undo never
|
|
593
|
+
* gets here: it is the tutorial's Back). Nothing is allowed before it starts; once it is over
|
|
594
|
+
* everything is allowed again.
|
|
595
|
+
*/
|
|
596
|
+
public allows(action: PromptAction): boolean {
|
|
597
|
+
if (this.status.value === "done") return true;
|
|
598
|
+
if (this.rewinding.value) return false;
|
|
599
|
+
const step = this.step;
|
|
600
|
+
if (!step || this.reviewing.value || this.status.value !== "waiting") return false;
|
|
601
|
+
if (action.type === "undo") return false; // handled as Back before this is asked
|
|
602
|
+
const move = shapeOfPromptAction(action);
|
|
603
|
+
if (!move) return false;
|
|
604
|
+
return isAllowed(step.allow, move, this.view.root(), this.view.me());
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
/**
|
|
608
|
+
* The "do it" step the player may act ahead into: the first later step that waits for something,
|
|
609
|
+
* reached only through steps that just talk. Only while the live step is talking (or already done).
|
|
610
|
+
*/
|
|
611
|
+
private aheadStep(): number | undefined {
|
|
612
|
+
if (this.status.value === "done" || this.rewinding.value || this.reviewing.value) return undefined;
|
|
613
|
+
if (this.status.value !== "narrating" && this.status.value !== "satisfied") return undefined;
|
|
614
|
+
for (let i = this.index.value + 1; i < this.spec.steps.length; i++) {
|
|
615
|
+
if (this.spec.steps[i].waitFor) return i;
|
|
616
|
+
}
|
|
617
|
+
return undefined;
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
/** The step a game move acts ahead into, if the next "do it" step allows it */
|
|
621
|
+
private aheadFor(action: PromptAction): number | undefined {
|
|
622
|
+
if (action.type === "undo") return undefined;
|
|
623
|
+
const ahead = this.aheadStep();
|
|
624
|
+
if (ahead === undefined) return undefined;
|
|
625
|
+
const move = shapeOfPromptAction(action);
|
|
626
|
+
if (!move) return undefined;
|
|
627
|
+
return isAllowed(this.spec.steps[ahead].allow, move, this.view.root(), this.view.me()) ? ahead : undefined;
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
/**
|
|
631
|
+
* The player used a UI control (by its `data-tutorial-target`). If the next "do it" step lists it in
|
|
632
|
+
* `allow`, that counts as moving on to that step, as a game move it allows does. Call it before the
|
|
633
|
+
* control acts, so the step's check takes its baseline from the board as it was.
|
|
634
|
+
*/
|
|
635
|
+
public usedUi(name: string) {
|
|
636
|
+
if (this.disposed) return;
|
|
637
|
+
const ahead = this.aheadStep();
|
|
638
|
+
if (ahead !== undefined && allowsUi(this.spec.steps[ahead].allow, name)) this.actAhead(ahead);
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
/**
|
|
642
|
+
* The player did what a later "do it" step asks for while an earlier step was still talking: move
|
|
643
|
+
* straight on to that step. The steps in between count as seen (snapshots for Back, their hooks and
|
|
644
|
+
* events), and the step itself stays quiet, since it would only ask for what was just done; it moves
|
|
645
|
+
* on after the usual short beat once its check passes, and narrates as usual if the check does not.
|
|
646
|
+
*/
|
|
647
|
+
private actAhead(index: number) {
|
|
648
|
+
this.leaveStep();
|
|
649
|
+
const ctx = this.context();
|
|
650
|
+
for (let i = this.index.value + 1; i < index; i++) {
|
|
651
|
+
const step = this.spec.steps[i];
|
|
652
|
+
if (i > this.frontier.value) this.frontier.value = i;
|
|
653
|
+
if (this.markSeen(step)) this.baselined.add(step.id);
|
|
654
|
+
if (ctx) {
|
|
655
|
+
step.onEnter?.(ctx);
|
|
656
|
+
step.onExit?.(ctx);
|
|
657
|
+
}
|
|
658
|
+
this.onEvent?.({ type: "step", stepId: step.id, index: i });
|
|
659
|
+
}
|
|
660
|
+
// Acting is as explicit as pressing Play
|
|
661
|
+
this.paused.value = false;
|
|
662
|
+
this.enter(index, { ahead: true });
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
/** First visit to a live step: its snapshot for Back and the baselines its check measures from */
|
|
666
|
+
private markSeen(step: TutorialStep<P, R>): boolean {
|
|
667
|
+
if (this.baselined.has(step.id)) return false;
|
|
668
|
+
this.stepStartClock = this.view.clock();
|
|
669
|
+
this.recentLog = [];
|
|
670
|
+
this.lastLog = this.view.latestLog();
|
|
671
|
+
this.history?.save(step.id);
|
|
672
|
+
return true;
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
// ---- steps -------------------------------------------------------------------------------------
|
|
676
|
+
|
|
677
|
+
/** `ahead`: entered by acting ahead (see `actAhead`), after the previous step was already left */
|
|
678
|
+
private enter(index: number, options: { ahead?: boolean } = {}) {
|
|
679
|
+
if (!options.ahead) this.leaveStep();
|
|
680
|
+
// A fast-forward ends on its step, which then plays as usual
|
|
681
|
+
if (this.fastForwardTarget.value !== undefined && index >= this.fastForwardTarget.value) this.stopFastForward();
|
|
682
|
+
const step = this.spec.steps[index];
|
|
683
|
+
if (!step) return;
|
|
684
|
+
const review = index < this.frontier.value;
|
|
685
|
+
if (!review && index > this.frontier.value) this.frontier.value = index;
|
|
686
|
+
this.index.value = index;
|
|
687
|
+
this.reviewing.value = review;
|
|
688
|
+
this.hint.value = undefined;
|
|
689
|
+
this.check.value = undefined;
|
|
690
|
+
this.appliedCues = new Set();
|
|
691
|
+
this.sentences = splitSentences(step.text);
|
|
692
|
+
this.sentence.value = -1;
|
|
693
|
+
this.narrationTime.value = 0;
|
|
694
|
+
|
|
695
|
+
if (step.box) {
|
|
696
|
+
this.box.value = step.box.anchor === "keep" ? this.box.value : step.box;
|
|
697
|
+
}
|
|
698
|
+
this.focus.value = step.focus ?? [];
|
|
699
|
+
if (step.gallery) this.applyGallery(step.gallery);
|
|
700
|
+
if (step.pose) this.applyPoses(step.pose);
|
|
701
|
+
this.releasePoses(false);
|
|
702
|
+
|
|
703
|
+
if (review) {
|
|
704
|
+
// Looking back: narrate and highlight, but leave the game, the bots and the checks alone
|
|
705
|
+
this.status.value = "narrating";
|
|
706
|
+
this.beginNarration();
|
|
707
|
+
this.publishUndo();
|
|
708
|
+
return;
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
this.runtime && (this.runtime.activeStep.value = step.id);
|
|
712
|
+
const firstTime = this.markSeen(step);
|
|
713
|
+
if (step.timeline) this.applyTimeline(step.timeline);
|
|
714
|
+
if (step.waitFor && this.timelineHold.value !== undefined && !step.timeline) {
|
|
715
|
+
console.warn(
|
|
716
|
+
`[tutorial ${this.spec.id}] step "${step.id}" waits with the board's timeline held at clock ` +
|
|
717
|
+
`${this.timelineHold.value}; if it needs the board to catch up, give it timeline: "play"`,
|
|
718
|
+
);
|
|
719
|
+
}
|
|
720
|
+
|
|
721
|
+
this.applyAi();
|
|
722
|
+
|
|
723
|
+
const ctx = this.context();
|
|
724
|
+
if (ctx) {
|
|
725
|
+
if (firstTime) {
|
|
726
|
+
step.waitFor?.reset?.(ctx);
|
|
727
|
+
this.baselined.add(step.id);
|
|
728
|
+
}
|
|
729
|
+
step.onEnter?.(ctx);
|
|
730
|
+
}
|
|
731
|
+
|
|
732
|
+
this.status.value = step.waitFor ? "waiting" : "narrating";
|
|
733
|
+
this.onEvent?.({ type: "step", stepId: step.id, index });
|
|
734
|
+
|
|
735
|
+
if (this.fastForwardTarget.value !== undefined) {
|
|
736
|
+
// Passing through: no narration, but the galleries end up as the cues would leave them
|
|
737
|
+
this.narrating.value = false;
|
|
738
|
+
this.sentence.value = -1;
|
|
739
|
+
step.cues?.forEach((cue) => cue.gallery && this.applyGallery(cue.gallery));
|
|
740
|
+
this.scheduleFastBeat();
|
|
741
|
+
} else if (options.ahead) {
|
|
742
|
+
// The move that brought us here has not landed yet. Once it does the check passes and the step
|
|
743
|
+
// moves on; if it does not pass soon after, narrate after all.
|
|
744
|
+
this.narrating.value = false;
|
|
745
|
+
this.sentence.value = -1;
|
|
746
|
+
this.aheadTimer = this._setTimeout(() => {
|
|
747
|
+
this.aheadTimer = undefined;
|
|
748
|
+
if (this.step === step && this.status.value === "waiting") this.beginNarration();
|
|
749
|
+
}, this.satisfiedDelayMs);
|
|
750
|
+
} else {
|
|
751
|
+
this.beginNarration();
|
|
752
|
+
}
|
|
753
|
+
this.evaluate();
|
|
754
|
+
if (step.waitFor) {
|
|
755
|
+
const after = typeof step.hint === "object" ? step.hint.after : DEFAULT_HINT_AFTER;
|
|
756
|
+
this.hintTimer = this._setTimeout(() => this.showHint(), after * 1000);
|
|
757
|
+
}
|
|
758
|
+
this.publishUndo();
|
|
759
|
+
}
|
|
760
|
+
|
|
761
|
+
private leaveStep() {
|
|
762
|
+
const step = this.step;
|
|
763
|
+
if (step && !this.reviewing.value) {
|
|
764
|
+
const ctx = this.context();
|
|
765
|
+
if (ctx) step.onExit?.(ctx);
|
|
766
|
+
}
|
|
767
|
+
this.stepUnsubs.forEach((u) => u());
|
|
768
|
+
this.stepUnsubs = [];
|
|
769
|
+
this._clearTimeout(this.satisfiedTimer);
|
|
770
|
+
this._clearTimeout(this.advanceTimer);
|
|
771
|
+
this._clearTimeout(this.hintTimer);
|
|
772
|
+
this._clearTimeout(this.readingTimer);
|
|
773
|
+
this._clearTimeout(this.aheadTimer);
|
|
774
|
+
this.satisfiedTimer = this.advanceTimer = this.hintTimer = this.readingTimer = this.aheadTimer = undefined;
|
|
775
|
+
this.advanceWhenNarrationEnds = false;
|
|
776
|
+
this.readingActive = false;
|
|
777
|
+
this.readingPausedAt = undefined;
|
|
778
|
+
this.narration?.stop();
|
|
779
|
+
this.narrating.value = false;
|
|
780
|
+
}
|
|
781
|
+
|
|
782
|
+
private restoreAi() {
|
|
783
|
+
if (this.ai && this.aiEnabledBeforePause !== undefined) {
|
|
784
|
+
this.ai.enabled = this.aiEnabledBeforePause;
|
|
785
|
+
this.aiEnabledBeforePause = undefined;
|
|
786
|
+
}
|
|
787
|
+
}
|
|
788
|
+
|
|
789
|
+
private finish(end: "continue" | "back", skipped = false) {
|
|
790
|
+
this.leaveStep();
|
|
791
|
+
this.stopFastForward();
|
|
792
|
+
// The game is the player's now: show it as it is
|
|
793
|
+
this.releasePoses(true);
|
|
794
|
+
this.releaseTimeline();
|
|
795
|
+
this.restoreAi();
|
|
796
|
+
this.view.setGuard?.(undefined);
|
|
797
|
+
this.view.setUndo?.(undefined);
|
|
798
|
+
this.runtime && (this.runtime.activeStep.value = "done");
|
|
799
|
+
this.focus.value = [];
|
|
800
|
+
this.hint.value = undefined;
|
|
801
|
+
this.status.value = "done";
|
|
802
|
+
if (!skipped) {
|
|
803
|
+
this.onEvent?.({ type: "completed", end });
|
|
804
|
+
}
|
|
805
|
+
}
|
|
806
|
+
|
|
807
|
+
// ---- narration and cues ------------------------------------------------------------------------
|
|
808
|
+
|
|
809
|
+
private startNarration() {
|
|
810
|
+
const step = this.step;
|
|
811
|
+
if (!step) return;
|
|
812
|
+
// Emphasise the first sentence from the start, not once the audio reports its first position
|
|
813
|
+
this.sentence.value = this.sentences.length > 0 ? 0 : -1;
|
|
814
|
+
this._clearTimeout(this.readingTimer);
|
|
815
|
+
this.readingActive = false;
|
|
816
|
+
this.readingPausedAt = undefined;
|
|
817
|
+
this.narration?.stop();
|
|
818
|
+
this.appliedCues = new Set();
|
|
819
|
+
this.narrationEnded = false;
|
|
820
|
+
this.applyCuesAt(0);
|
|
821
|
+
|
|
822
|
+
const useAudio = !!step.audio && !!this.narration && !this.muted.value;
|
|
823
|
+
// While this step talks, the next one's recording loads
|
|
824
|
+
if (useAudio) this.preloadStep(this.index.value + 1);
|
|
825
|
+
if (useAudio && this.narration) {
|
|
826
|
+
const narration = this.narration;
|
|
827
|
+
this.stepUnsubs.push(narration.onTime((t) => this.onNarrationTime(t)));
|
|
828
|
+
this.stepUnsubs.push(narration.onSentence((i) => this.onSentence(i)));
|
|
829
|
+
this.stepUnsubs.push(
|
|
830
|
+
narration.onEnd(() => {
|
|
831
|
+
this.narrationEnded = true;
|
|
832
|
+
this.narrating.value = false;
|
|
833
|
+
this.applyCuesAt(Number.POSITIVE_INFINITY);
|
|
834
|
+
this.sentence.value = this.emphasisAfterNarration();
|
|
835
|
+
this.afterNarration();
|
|
836
|
+
}),
|
|
837
|
+
);
|
|
838
|
+
this.narrating.value = true;
|
|
839
|
+
narration.play({ audio: step.audio!, text: step.text, sentences: this.sentences }).catch(() => {
|
|
840
|
+
this.narrating.value = false;
|
|
841
|
+
this.startReadingClock();
|
|
842
|
+
});
|
|
843
|
+
} else {
|
|
844
|
+
this.startReadingClock();
|
|
845
|
+
}
|
|
846
|
+
}
|
|
847
|
+
|
|
848
|
+
/** Without audio, time flows at the reading speed so `at` cues and emphasis still happen. */
|
|
849
|
+
private startReadingClock() {
|
|
850
|
+
const step = this.step;
|
|
851
|
+
if (!step) return;
|
|
852
|
+
const words = step.text.split(/\s+/).filter((w) => w).length;
|
|
853
|
+
// In "reading seconds" (normal speed); the clock below advances `rate` of them per real second
|
|
854
|
+
const duration = Number.isFinite(this.readingSpeed) ? Math.max(1, words / this.readingSpeed) : 0;
|
|
855
|
+
this.readingStartedAt = this._now();
|
|
856
|
+
this.readingPausedAt = undefined;
|
|
857
|
+
this.readingActive = true;
|
|
858
|
+
this.narrating.value = true;
|
|
859
|
+
const tick = () => {
|
|
860
|
+
if (this.readingPausedAt !== undefined) {
|
|
861
|
+
this.readingTimer = this._setTimeout(tick, 100);
|
|
862
|
+
return;
|
|
863
|
+
}
|
|
864
|
+
const t = ((this._now() - this.readingStartedAt) / 1000) * this.rate.value;
|
|
865
|
+
this.onNarrationTime(t, duration);
|
|
866
|
+
if (t >= duration) {
|
|
867
|
+
this.readingActive = false;
|
|
868
|
+
this.narrating.value = false;
|
|
869
|
+
this.sentence.value = this.emphasisAfterNarration();
|
|
870
|
+
this.narrationEnded = true;
|
|
871
|
+
this.applyCuesAt(Number.POSITIVE_INFINITY);
|
|
872
|
+
this.afterNarration();
|
|
873
|
+
return;
|
|
874
|
+
}
|
|
875
|
+
this.readingTimer = this._setTimeout(tick, 100);
|
|
876
|
+
};
|
|
877
|
+
tick();
|
|
878
|
+
}
|
|
879
|
+
|
|
880
|
+
/** A "do it" step whose check passed while it was still narrating */
|
|
881
|
+
private advanceWhenNarrationEnds = false;
|
|
882
|
+
|
|
883
|
+
private scheduleSatisfiedAdvance() {
|
|
884
|
+
this._clearTimeout(this.satisfiedTimer);
|
|
885
|
+
if (this.fastForwardTarget.value !== undefined) {
|
|
886
|
+
// Fast-forwarding moves on once the board settles, so a check that passed in passing is re-read
|
|
887
|
+
this.scheduleFastBeat();
|
|
888
|
+
return;
|
|
889
|
+
}
|
|
890
|
+
this.satisfiedTimer = this._setTimeout(
|
|
891
|
+
() => {
|
|
892
|
+
// The player just did what the step asked: that is as explicit as pressing Play
|
|
893
|
+
this.paused.value = false;
|
|
894
|
+
this.next();
|
|
895
|
+
},
|
|
896
|
+
this.satisfiedDelayMs,
|
|
897
|
+
);
|
|
898
|
+
}
|
|
899
|
+
|
|
900
|
+
/**
|
|
901
|
+
* Which sentence stays emphasised once the narration ends. A step about to move on by itself keeps
|
|
902
|
+
* its last sentence lit through the short beat before the next step, rather than flashing the whole
|
|
903
|
+
* paragraph back to full strength; a step that stays on screen shows the whole paragraph to read.
|
|
904
|
+
*/
|
|
905
|
+
private emphasisAfterNarration(): number {
|
|
906
|
+
const step = this.step;
|
|
907
|
+
if (!step || this.paused.value || this.status.value === "done") return -1;
|
|
908
|
+
const movesOn =
|
|
909
|
+
(this.advanceWhenNarrationEnds && this.status.value === "satisfied") ||
|
|
910
|
+
(!step.waitFor && !step.pause && !this.isLast);
|
|
911
|
+
return movesOn ? this.sentences.length - 1 : -1;
|
|
912
|
+
}
|
|
913
|
+
|
|
914
|
+
/** The step's narration (or reading clock) just ended */
|
|
915
|
+
private afterNarration() {
|
|
916
|
+
this.scheduleAutoAdvance();
|
|
917
|
+
if (this.advanceWhenNarrationEnds && this.status.value === "satisfied") {
|
|
918
|
+
this.advanceWhenNarrationEnds = false;
|
|
919
|
+
this.scheduleSatisfiedAdvance();
|
|
920
|
+
}
|
|
921
|
+
}
|
|
922
|
+
|
|
923
|
+
/**
|
|
924
|
+
* A narration step moves on by itself once its narration (or reading clock) ends, unless it sets
|
|
925
|
+
* `pause`. "Do it" steps wait for their check and the last step waits for Finish.
|
|
926
|
+
*/
|
|
927
|
+
private scheduleAutoAdvance() {
|
|
928
|
+
const step = this.step;
|
|
929
|
+
// Reviewing (Back without a game to rewind) flows too once the player presses Play
|
|
930
|
+
if (!step || step.waitFor || step.pause || this.isLast || this.status.value === "done") return;
|
|
931
|
+
if (this.paused.value) return;
|
|
932
|
+
this._clearTimeout(this.advanceTimer);
|
|
933
|
+
this.advanceTimer = this._setTimeout(() => {
|
|
934
|
+
if (this.step === step && this.status.value === "narrating" && !this.paused.value) this.next();
|
|
935
|
+
}, this.autoAdvanceDelayMs);
|
|
936
|
+
}
|
|
937
|
+
|
|
938
|
+
private onNarrationTime(t: number, estimatedDuration?: number) {
|
|
939
|
+
this.narrationTime.value = t;
|
|
940
|
+
this.applyCuesAt(t);
|
|
941
|
+
// Sentence emphasis: explicit times, else proportional to the (estimated) duration
|
|
942
|
+
const step = this.step;
|
|
943
|
+
if (!step || this.sentences.length === 0) return;
|
|
944
|
+
const times = step.sentenceTimes;
|
|
945
|
+
if (times && times.length > 0) {
|
|
946
|
+
let i = 0;
|
|
947
|
+
while (i + 1 < times.length && times[i + 1] <= t) i++;
|
|
948
|
+
this.sentence.value = i;
|
|
949
|
+
} else if (!this.narration?.onSentence || this.muted.value || !step.audio) {
|
|
950
|
+
const duration = estimatedDuration ?? this.narration?.duration;
|
|
951
|
+
if (duration && duration > 0) {
|
|
952
|
+
const total = this.sentences.reduce((a, s) => a + s.length, 0);
|
|
953
|
+
let acc = 0;
|
|
954
|
+
let i = 0;
|
|
955
|
+
for (; i < this.sentences.length; i++) {
|
|
956
|
+
acc += this.sentences[i].length;
|
|
957
|
+
if ((t / duration) * total < acc) break;
|
|
958
|
+
}
|
|
959
|
+
this.sentence.value = Math.min(i, this.sentences.length - 1);
|
|
960
|
+
}
|
|
961
|
+
}
|
|
962
|
+
}
|
|
963
|
+
|
|
964
|
+
private onSentence(i: number) {
|
|
965
|
+
this.sentence.value = i;
|
|
966
|
+
this.applySentenceCues(i);
|
|
967
|
+
}
|
|
968
|
+
|
|
969
|
+
private cueTime(cue: Cue<P, R>): number | undefined {
|
|
970
|
+
if (typeof cue.at === "number") return cue.at;
|
|
971
|
+
const step = this.step;
|
|
972
|
+
const times = step?.sentenceTimes;
|
|
973
|
+
if (times && times[cue.at.sentence] !== undefined) return times[cue.at.sentence];
|
|
974
|
+
// Proportional estimate when the narration has a known or estimated duration
|
|
975
|
+
const duration = this.narration?.duration;
|
|
976
|
+
if (step && duration && this.sentences.length) {
|
|
977
|
+
const total = this.sentences.reduce((a, s) => a + s.length, 0);
|
|
978
|
+
const before = this.sentences.slice(0, cue.at.sentence).reduce((a, s) => a + s.length, 0);
|
|
979
|
+
return (before / total) * duration;
|
|
980
|
+
}
|
|
981
|
+
return undefined;
|
|
982
|
+
}
|
|
983
|
+
|
|
984
|
+
private applyCue(i: number, cue: Cue<P, R>) {
|
|
985
|
+
if (this.appliedCues.has(i)) return;
|
|
986
|
+
this.appliedCues.add(i);
|
|
987
|
+
if (cue.focus) this.focus.value = cue.focus;
|
|
988
|
+
if (cue.box && cue.box.anchor !== "keep") this.box.value = cue.box;
|
|
989
|
+
if (cue.gallery) this.applyGallery(cue.gallery);
|
|
990
|
+
if (cue.pose) this.applyPoses(cue.pose);
|
|
991
|
+
if (cue.timeline) this.applyTimeline(cue.timeline);
|
|
992
|
+
}
|
|
993
|
+
|
|
994
|
+
/** Whether a gallery the script opened may still be showing (Back closes it) */
|
|
995
|
+
private scriptOpenedGallery = false;
|
|
996
|
+
|
|
997
|
+
private applyGallery(command: GalleryCommand<P, R>) {
|
|
998
|
+
if (!this.view.setGallery) return;
|
|
999
|
+
const opened = this.view.setGallery(command);
|
|
1000
|
+
if (command === "close") this.scriptOpenedGallery = false;
|
|
1001
|
+
else if (opened) this.scriptOpenedGallery = true;
|
|
1002
|
+
}
|
|
1003
|
+
|
|
1004
|
+
private applyCuesAt(t: number) {
|
|
1005
|
+
const step = this.step;
|
|
1006
|
+
if (!step?.cues) return;
|
|
1007
|
+
step.cues.forEach((cue, i) => {
|
|
1008
|
+
if (typeof cue.at === "number") {
|
|
1009
|
+
if (cue.at <= t) this.applyCue(i, cue);
|
|
1010
|
+
} else if (t === Number.POSITIVE_INFINITY) {
|
|
1011
|
+
this.applyCue(i, cue);
|
|
1012
|
+
} else {
|
|
1013
|
+
const at = this.cueTime(cue);
|
|
1014
|
+
if (at !== undefined && at <= t) this.applyCue(i, cue);
|
|
1015
|
+
else if (at === undefined && this.readingTimer && this.sentences.length) {
|
|
1016
|
+
// reading clock, no durations: use sentence index from emphasis
|
|
1017
|
+
if (this.sentence.value >= cue.at.sentence) this.applyCue(i, cue);
|
|
1018
|
+
}
|
|
1019
|
+
}
|
|
1020
|
+
});
|
|
1021
|
+
}
|
|
1022
|
+
|
|
1023
|
+
private applySentenceCues(sentence: number) {
|
|
1024
|
+
const step = this.step;
|
|
1025
|
+
step?.cues?.forEach((cue, i) => {
|
|
1026
|
+
if (typeof cue.at !== "number" && cue.at.sentence <= sentence) this.applyCue(i, cue);
|
|
1027
|
+
});
|
|
1028
|
+
}
|
|
1029
|
+
|
|
1030
|
+
// ---- doing it for the player --------------------------------------------------------------------
|
|
1031
|
+
|
|
1032
|
+
/**
|
|
1033
|
+
* Makes the move the live "do it" step is waiting for, as if the player had: the first entry of its
|
|
1034
|
+
* `allow` (in the order written) that the prompt can take, or the UI control an entry names. For
|
|
1035
|
+
* watching and testing a tutorial without playing it every time. Returns whether it did anything.
|
|
1036
|
+
*/
|
|
1037
|
+
public doItForMe(): boolean {
|
|
1038
|
+
if (this.disposed || this.rewinding.value || this.reviewing.value) return false;
|
|
1039
|
+
const step = this.step;
|
|
1040
|
+
if (!step?.waitFor || this.status.value !== "waiting") return false;
|
|
1041
|
+
const prompt = this.view.prompt();
|
|
1042
|
+
const answer = scriptedAnswer(step.allow, prompt, this.view.root(), this.view.me());
|
|
1043
|
+
if (!answer) return false;
|
|
1044
|
+
if ("ui" in answer) return this.view.pressUi?.(answer.ui) ?? false;
|
|
1045
|
+
if (!prompt) return false;
|
|
1046
|
+
prompt.resolvePick(answer.move.pick, answer.move.value);
|
|
1047
|
+
return true;
|
|
1048
|
+
}
|
|
1049
|
+
|
|
1050
|
+
/** The → key: Next when the step can move on, otherwise the step's move made for the player */
|
|
1051
|
+
public forward() {
|
|
1052
|
+
if (this.canAdvance) this.next();
|
|
1053
|
+
else this.doItForMe();
|
|
1054
|
+
}
|
|
1055
|
+
|
|
1056
|
+
/**
|
|
1057
|
+
* Plays on to a later step as fast as it can: narration skipped, the board's animations rushed, the
|
|
1058
|
+
* player's moves made for them (see `doItForMe`) and the bots playing as scripted. The step it lands
|
|
1059
|
+
* on plays as usual. Back, Pause or jumping elsewhere stops it.
|
|
1060
|
+
*/
|
|
1061
|
+
public fastForwardTo(index: number) {
|
|
1062
|
+
if (this.disposed || this.rewinding.value || this.status.value === "done") return;
|
|
1063
|
+
if (index <= this.index.value || index >= this.spec.steps.length) return;
|
|
1064
|
+
if (this.reviewing.value) {
|
|
1065
|
+
// From a review (Back without a game history): carry on from the live step
|
|
1066
|
+
this.enter(this.frontier.value);
|
|
1067
|
+
if (index <= this.index.value) return;
|
|
1068
|
+
}
|
|
1069
|
+
this.fastForwardTarget.value = index;
|
|
1070
|
+
this.view.setFastForward?.(true);
|
|
1071
|
+
this.releaseTimeline();
|
|
1072
|
+
this.paused.value = false;
|
|
1073
|
+
this.applyAi();
|
|
1074
|
+
// The step on screen stops talking and moves on (or gets its move made)
|
|
1075
|
+
this.narration?.stop();
|
|
1076
|
+
this._clearTimeout(this.readingTimer);
|
|
1077
|
+
this.readingActive = false;
|
|
1078
|
+
this.narrating.value = false;
|
|
1079
|
+
this.advanceWhenNarrationEnds = false;
|
|
1080
|
+
if (this.status.value === "satisfied") this.scheduleSatisfiedAdvance();
|
|
1081
|
+
this.scheduleFastBeat();
|
|
1082
|
+
}
|
|
1083
|
+
|
|
1084
|
+
private fastTimer: any;
|
|
1085
|
+
private fastWatchdog: any;
|
|
1086
|
+
|
|
1087
|
+
/** The next beat of a fast-forward: move on from a talking step, or make a waiting step's move */
|
|
1088
|
+
private scheduleFastBeat() {
|
|
1089
|
+
this._clearTimeout(this.fastTimer);
|
|
1090
|
+
this._clearTimeout(this.fastWatchdog);
|
|
1091
|
+
let settledSince: number | undefined;
|
|
1092
|
+
const beat = () => {
|
|
1093
|
+
this.fastTimer = undefined;
|
|
1094
|
+
if (this.fastForwardTarget.value === undefined) return;
|
|
1095
|
+
// Wait for the board to catch up and stay caught up: a prompt can arrive just after its clock
|
|
1096
|
+
if (this.view.settled?.() === false) settledSince = undefined;
|
|
1097
|
+
else settledSince ??= this._now();
|
|
1098
|
+
if (settledSince === undefined || this._now() - settledSince < FAST_SETTLE_MS) {
|
|
1099
|
+
this.fastTimer = this._setTimeout(beat, FAST_BEAT_MS);
|
|
1100
|
+
return;
|
|
1101
|
+
}
|
|
1102
|
+
this.evaluate();
|
|
1103
|
+
if (this.status.value === "narrating" || this.status.value === "satisfied") this.next();
|
|
1104
|
+
else if (this.status.value === "waiting") this.doItForMe();
|
|
1105
|
+
};
|
|
1106
|
+
this.fastTimer = this._setTimeout(beat, FAST_BEAT_MS);
|
|
1107
|
+
// A step that never gets anywhere (its move is not on offer, the bots never finish) ends it
|
|
1108
|
+
this.fastWatchdog = this._setTimeout(() => {
|
|
1109
|
+
const step = this.step;
|
|
1110
|
+
console.warn(`[tutorial ${this.spec.id}] fast-forward gave up on step "${step?.id}"`);
|
|
1111
|
+
this.stopFastForward();
|
|
1112
|
+
if (step) this.beginNarration();
|
|
1113
|
+
}, FAST_GIVE_UP_MS);
|
|
1114
|
+
}
|
|
1115
|
+
|
|
1116
|
+
private stopFastForward() {
|
|
1117
|
+
this._clearTimeout(this.fastTimer);
|
|
1118
|
+
this._clearTimeout(this.fastWatchdog);
|
|
1119
|
+
this.fastTimer = this.fastWatchdog = undefined;
|
|
1120
|
+
if (this.fastForwardTarget.value === undefined) return;
|
|
1121
|
+
this.fastForwardTarget.value = undefined;
|
|
1122
|
+
this.view.setFastForward?.(false);
|
|
1123
|
+
}
|
|
1124
|
+
|
|
1125
|
+
// ---- poses -------------------------------------------------------------------------------------
|
|
1126
|
+
|
|
1127
|
+
/** Chits the script changed in the player's view, with the step that changed them */
|
|
1128
|
+
private posed = new Map<Chit, { keep: boolean; step: number }>();
|
|
1129
|
+
|
|
1130
|
+
private applyPoses(poses: Pose<P, R>[]) {
|
|
1131
|
+
const root = this.view.root();
|
|
1132
|
+
const me = this.view.me();
|
|
1133
|
+
// Passing through on a fast-forward: the next step would only put them back
|
|
1134
|
+
if (!root || !me || !this.view.pose || this.fastForwardTarget.value !== undefined) return;
|
|
1135
|
+
for (const pose of poses) {
|
|
1136
|
+
for (const chit of selectChits<P, R>(pose.chit, root, me)) {
|
|
1137
|
+
const set = pose.set;
|
|
1138
|
+
if (!set) {
|
|
1139
|
+
this.releasePose(chit);
|
|
1140
|
+
continue;
|
|
1141
|
+
}
|
|
1142
|
+
try {
|
|
1143
|
+
this.view.pose(chit, (c) => (typeof set === "function" ? set(c) : Object.assign(c, set)));
|
|
1144
|
+
} catch (e) {
|
|
1145
|
+
console.error(`[tutorial ${this.spec.id}] a pose in step "${this.step?.id}" threw`, e);
|
|
1146
|
+
}
|
|
1147
|
+
this.posed.set(chit, { keep: !!pose.keep, step: this.index.value });
|
|
1148
|
+
}
|
|
1149
|
+
}
|
|
1150
|
+
}
|
|
1151
|
+
|
|
1152
|
+
private releasePose(chit: Chit) {
|
|
1153
|
+
if (!this.posed.delete(chit)) return;
|
|
1154
|
+
try {
|
|
1155
|
+
this.view.unpose?.(chit);
|
|
1156
|
+
} catch (e) {
|
|
1157
|
+
console.error(`[tutorial ${this.spec.id}] putting a posed chit back threw`, e);
|
|
1158
|
+
}
|
|
1159
|
+
}
|
|
1160
|
+
|
|
1161
|
+
/**
|
|
1162
|
+
* Puts posed chits back: all of them, or (on entering a step) those an earlier step posed for its
|
|
1163
|
+
* own length. A step that poses the same chit again keeps it posed without a flicker in between.
|
|
1164
|
+
*/
|
|
1165
|
+
private releasePoses(all: boolean) {
|
|
1166
|
+
for (const [chit, p] of [...this.posed]) {
|
|
1167
|
+
if (all || (!p.keep && p.step !== this.index.value)) this.releasePose(chit);
|
|
1168
|
+
}
|
|
1169
|
+
}
|
|
1170
|
+
|
|
1171
|
+
// ---- the board's timeline ----------------------------------------------------------------------
|
|
1172
|
+
|
|
1173
|
+
/** A `{ until }` command still playing toward its moment, and where its log lines start */
|
|
1174
|
+
private timelineUntil?: { check: Checker<P, R>; logFrom: number };
|
|
1175
|
+
|
|
1176
|
+
private applyTimeline(command: TimelineCommand<P, R>) {
|
|
1177
|
+
const timeline = this.view.timeline;
|
|
1178
|
+
// Looking back at a step's text leaves the board alone, as it does the game and the bots
|
|
1179
|
+
// A fast-forward lets the board play; the step it lands on holds it again
|
|
1180
|
+
if (!timeline || this.reviewing.value || this.fastForwardTarget.value !== undefined) return;
|
|
1181
|
+
this.timelineUntil = undefined;
|
|
1182
|
+
if (command === "play") {
|
|
1183
|
+
this.holdTimeline(undefined);
|
|
1184
|
+
} else if (command === "hold") {
|
|
1185
|
+
this.holdTimeline(timeline.at());
|
|
1186
|
+
} else if ("advance" in command) {
|
|
1187
|
+
this.holdTimeline(timeline.at() + Math.max(0, Math.floor(command.advance)));
|
|
1188
|
+
} else {
|
|
1189
|
+
const ctx = this.context();
|
|
1190
|
+
if (ctx) command.until.reset?.(ctx);
|
|
1191
|
+
this.timelineUntil = { check: command.until, logFrom: this.recentLog.length };
|
|
1192
|
+
this.holdTimeline(undefined);
|
|
1193
|
+
this.checkTimelineUntil();
|
|
1194
|
+
}
|
|
1195
|
+
}
|
|
1196
|
+
|
|
1197
|
+
/** Stops the board at the first moment that passes a pending `{ until }` command */
|
|
1198
|
+
private checkTimelineUntil() {
|
|
1199
|
+
const until = this.timelineUntil;
|
|
1200
|
+
if (!until || !this.view.timeline) return;
|
|
1201
|
+
const ctx = this.context();
|
|
1202
|
+
if (!ctx) return;
|
|
1203
|
+
let passed = false;
|
|
1204
|
+
try {
|
|
1205
|
+
passed = until.check({ ...ctx, recentLog: this.recentLog.slice(until.logFrom) }) === true;
|
|
1206
|
+
} catch (e) {
|
|
1207
|
+
console.error(`[tutorial ${this.spec.id}] a timeline "until" check in step "${this.step?.id}" threw`, e);
|
|
1208
|
+
}
|
|
1209
|
+
if (!passed) return;
|
|
1210
|
+
this.timelineUntil = undefined;
|
|
1211
|
+
// Change events arrive once the moment has loaded and before the board moves on from it
|
|
1212
|
+
this.holdTimeline(this.view.clock());
|
|
1213
|
+
}
|
|
1214
|
+
|
|
1215
|
+
private holdTimeline(clock: number | undefined) {
|
|
1216
|
+
this.timelineHold.value = clock;
|
|
1217
|
+
this.view.timeline?.hold(clock);
|
|
1218
|
+
}
|
|
1219
|
+
|
|
1220
|
+
private releaseTimeline() {
|
|
1221
|
+
this.timelineUntil = undefined;
|
|
1222
|
+
if (this.timelineHold.value !== undefined) this.holdTimeline(undefined);
|
|
1223
|
+
}
|
|
1224
|
+
|
|
1225
|
+
// ---- checks ------------------------------------------------------------------------------------
|
|
1226
|
+
|
|
1227
|
+
private onViewChange() {
|
|
1228
|
+
if (this.disposed) return;
|
|
1229
|
+
const latest = this.view.latestLog();
|
|
1230
|
+
if (latest !== undefined && latest !== this.lastLog) {
|
|
1231
|
+
this.recentLog.push(latest);
|
|
1232
|
+
this.lastLog = latest;
|
|
1233
|
+
}
|
|
1234
|
+
this.boardClock.value = this.view.clock();
|
|
1235
|
+
this.checkTimelineUntil();
|
|
1236
|
+
this.evaluate();
|
|
1237
|
+
// Fast-forwarding through a "do it" step: make the move as soon as the prompt offers it
|
|
1238
|
+
if (this.fastForwardTarget.value !== undefined && this.status.value === "waiting") this.doItForMe();
|
|
1239
|
+
}
|
|
1240
|
+
|
|
1241
|
+
private evaluate() {
|
|
1242
|
+
const step = this.step;
|
|
1243
|
+
if (!step || this.status.value === "done" || this.reviewing.value) return;
|
|
1244
|
+
if (!step.waitFor) {
|
|
1245
|
+
this.check.value = undefined;
|
|
1246
|
+
return;
|
|
1247
|
+
}
|
|
1248
|
+
const ctx = this.context();
|
|
1249
|
+
if (!ctx) return;
|
|
1250
|
+
let result: boolean | string;
|
|
1251
|
+
try {
|
|
1252
|
+
result = step.waitFor(ctx);
|
|
1253
|
+
} catch (e) {
|
|
1254
|
+
console.error(`[tutorial ${this.spec.id}] waitFor of step "${step.id}" threw`, e);
|
|
1255
|
+
result = false;
|
|
1256
|
+
}
|
|
1257
|
+
this.check.value = result;
|
|
1258
|
+
if (result === true) {
|
|
1259
|
+
if (this.status.value !== "satisfied") {
|
|
1260
|
+
this.status.value = "satisfied";
|
|
1261
|
+
this.hint.value = undefined;
|
|
1262
|
+
this._clearTimeout(this.hintTimer);
|
|
1263
|
+
this._clearTimeout(this.satisfiedTimer);
|
|
1264
|
+
this.onEvent?.({ type: "satisfied", stepId: step.id });
|
|
1265
|
+
// Let the step finish what it is saying first; Next still moves on straight away
|
|
1266
|
+
if (this.narrating.value) this.advanceWhenNarrationEnds = true;
|
|
1267
|
+
else this.scheduleSatisfiedAdvance();
|
|
1268
|
+
}
|
|
1269
|
+
} else if (this.status.value === "satisfied") {
|
|
1270
|
+
// Un-satisfied again (an undo): keep waiting
|
|
1271
|
+
this._clearTimeout(this.satisfiedTimer);
|
|
1272
|
+
this.advanceWhenNarrationEnds = false;
|
|
1273
|
+
this.status.value = "waiting";
|
|
1274
|
+
}
|
|
1275
|
+
}
|
|
1276
|
+
|
|
1277
|
+
private showHint() {
|
|
1278
|
+
const step = this.step;
|
|
1279
|
+
if (!step || this.status.value !== "waiting") return;
|
|
1280
|
+
const configured = typeof step.hint === "object" ? step.hint.text : step.hint;
|
|
1281
|
+
const fromCheck = typeof this.check.value === "string" ? this.check.value : undefined;
|
|
1282
|
+
this.hint.value = fromCheck ?? configured;
|
|
1283
|
+
// keep the hint fresh as the check's reason changes
|
|
1284
|
+
this.hintTimer = this._setTimeout(() => this.showHint(), 1000);
|
|
1285
|
+
}
|
|
1286
|
+
}
|