@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,730 @@
|
|
|
1
|
+
import { Chit } from "../Chit";
|
|
2
|
+
import { ClockDetails } from "../ClockDetails";
|
|
3
|
+
import { Connection } from "../Connection";
|
|
4
|
+
import { ConnectionObject } from "../ConnectionObject";
|
|
5
|
+
import { Game } from "../Game";
|
|
6
|
+
import { Confirm, GameButton } from "../GameButton";
|
|
7
|
+
import { PickSerialization, PickType } from "../Pick";
|
|
8
|
+
import { PlayerChit } from "../PlayerChit";
|
|
9
|
+
import { PromptSerialization } from "../Prompt";
|
|
10
|
+
import { RootChit } from "../RootChit";
|
|
11
|
+
import { ServerPrompts } from "../serverTransport/ServerPrompts";
|
|
12
|
+
import type { AiView } from "./AiLiveView";
|
|
13
|
+
import {
|
|
14
|
+
AiContext,
|
|
15
|
+
AiOption,
|
|
16
|
+
AiPickInfo,
|
|
17
|
+
AiRule,
|
|
18
|
+
NormalizedScore,
|
|
19
|
+
applyPlan,
|
|
20
|
+
chooseIndex,
|
|
21
|
+
chooseSoftmax,
|
|
22
|
+
chooseWeighted,
|
|
23
|
+
createRandom,
|
|
24
|
+
hashString,
|
|
25
|
+
normalizeScore,
|
|
26
|
+
} from "./defineAi";
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Where a bot keeps its plan memory between prompts. `connectAiPlayer` defaults to a store inside the
|
|
30
|
+
* match's saved state, so memory survives a reload or a re-seat in another process. Pass your own to
|
|
31
|
+
* keep it elsewhere, or `false` for in-process only.
|
|
32
|
+
*/
|
|
33
|
+
export type AiMemoryStore<M = any> = {
|
|
34
|
+
read(): M | undefined;
|
|
35
|
+
write(memory: M): void;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
export type AiPlayerOptions = {
|
|
39
|
+
/** 0–1. Lower values add more noise to scores so the bot plays less predictably. Default 0.85 */
|
|
40
|
+
difficulty?: number;
|
|
41
|
+
/** Pause before answering, in ms: a fixed value or a [min, max] range. Default 0 */
|
|
42
|
+
thinkMs?: number | [number, number];
|
|
43
|
+
/** Seed for the bot's random number generator */
|
|
44
|
+
seed?: number;
|
|
45
|
+
/** Log untagged prompts and warnings to the console. Default true */
|
|
46
|
+
log?: boolean;
|
|
47
|
+
/** Warn if the server hasn't answered within this many ms after the bot responds. Default 10000 */
|
|
48
|
+
stallWarningMs?: number;
|
|
49
|
+
/** Anything the game's rules should read as `ctx.params`: per-seat weights, a personality, … */
|
|
50
|
+
params?: unknown;
|
|
51
|
+
/**
|
|
52
|
+
* Choose by softmax over scores at this temperature, in score units (1 = an option worth one
|
|
53
|
+
* point less is chosen e≈2.7× less often). Replaces the `difficulty` noise, which scales with
|
|
54
|
+
* the spread of each prompt's scores. Unset: use `difficulty`.
|
|
55
|
+
*/
|
|
56
|
+
temperature?: number;
|
|
57
|
+
/**
|
|
58
|
+
* Override the choice: return the index into `scored` to take, undefined for the normal choice,
|
|
59
|
+
* or null to leave the prompt unanswered for now (call `recheck()` when that may have changed).
|
|
60
|
+
* Consulted for every prompt, the engine's Confirm included. For harnesses (forced moves in
|
|
61
|
+
* playouts, scripted openings, tutorials), not for game rules.
|
|
62
|
+
*/
|
|
63
|
+
choose?: (
|
|
64
|
+
scored: { option: AiOption; score: number }[],
|
|
65
|
+
ctx: AiContext<any, any, any>,
|
|
66
|
+
) => number | null | undefined;
|
|
67
|
+
/**
|
|
68
|
+
* While this returns true for the current prompt, the bot leaves it unanswered (call `recheck()`
|
|
69
|
+
* when that may have changed). For harnesses that hold a bot back, like a tutorial waiting for the
|
|
70
|
+
* step that scripts its next move. It sees the prompt so it can let some through (the engine's own
|
|
71
|
+
* Confirm, which ends a turn the script never mentions).
|
|
72
|
+
*/
|
|
73
|
+
hold?: (prompt: PromptSerialization) => boolean;
|
|
74
|
+
/** Called after every decision with each option's score and the chosen index (for logging and audits). */
|
|
75
|
+
onDecision?: (decision: AiDecision) => void;
|
|
76
|
+
/**
|
|
77
|
+
* Where plan memory is kept (see `AiMemoryStore`). `connectAiPlayer` defaults this to the match's
|
|
78
|
+
* saved state; `false` keeps memory in-process only, so it resets whenever the bot is re-seated.
|
|
79
|
+
*/
|
|
80
|
+
memoryStore?: AiMemoryStore | false;
|
|
81
|
+
/** Start paused (see `setEnabled`). Default false */
|
|
82
|
+
disabled?: boolean;
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
/** One decision, as reported to `AiPlayerOptions.onDecision`. */
|
|
86
|
+
export type AiDecision = {
|
|
87
|
+
playerId: string;
|
|
88
|
+
prompt: { message?: string; help?: string };
|
|
89
|
+
scored: { option: AiOption; score: number }[];
|
|
90
|
+
chosen: number;
|
|
91
|
+
/** The context the rules saw (its `view` is live in the default view mode: read it now, don't keep it) */
|
|
92
|
+
ctx: AiContext<any, any, any>;
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
export type AiPlayerStats = {
|
|
96
|
+
promptsAnswered: number;
|
|
97
|
+
stepBacks: number;
|
|
98
|
+
rejections: number;
|
|
99
|
+
stalls: number;
|
|
100
|
+
ruleErrors: number;
|
|
101
|
+
/** Prompts the bot could not get out of (no valid moves and nothing left to undo) */
|
|
102
|
+
stuck: number;
|
|
103
|
+
/** How often each untagged pick (keyed by its message) was scored without a rule */
|
|
104
|
+
untagged: { [message: string]: number };
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
type QueuedPrompt = { prompt: PromptSerialization; clockDetails?: ClockDetails; seq: number };
|
|
108
|
+
type HistoryEntry<M> = { fingerprint: string; choice: string; memoryBefore: M };
|
|
109
|
+
type Scored<M> = { option: AiOption } & NormalizedScore<M>;
|
|
110
|
+
|
|
111
|
+
const MAX_BLIND_STEP_BACKS = 3;
|
|
112
|
+
const MAX_EXHAUSTED_VISITS = 3;
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* An AI player, registered as "ClientPrompts" on its own connection (see `connectAiPlayer`).
|
|
116
|
+
*
|
|
117
|
+
* It only has the Game (for its rules and chit library) and its view of the board: by default the
|
|
118
|
+
* match's live objects (AiLiveView), or, when the game asks for it, exactly what the server sends
|
|
119
|
+
* its seat (AiPlayerView). Create one with `connectAiPlayer`.
|
|
120
|
+
*
|
|
121
|
+
* @group AI
|
|
122
|
+
*/
|
|
123
|
+
export class AiClientPrompts<P extends PlayerChit, R extends RootChit<P>, M = any> extends ConnectionObject {
|
|
124
|
+
private serverPrompts: ServerPrompts<P, R>;
|
|
125
|
+
private latest?: QueuedPrompt;
|
|
126
|
+
private seq = 0;
|
|
127
|
+
private handledSeq = -1;
|
|
128
|
+
private running = false;
|
|
129
|
+
private disposed = false;
|
|
130
|
+
private _stalled = false;
|
|
131
|
+
private _enabled = true;
|
|
132
|
+
private memoryStore?: AiMemoryStore<M>;
|
|
133
|
+
|
|
134
|
+
private memory: M;
|
|
135
|
+
private history: HistoryEntry<M>[] = [];
|
|
136
|
+
private dead = new Map<string, Set<string>>();
|
|
137
|
+
private exhaustedVisits = new Map<string, number>();
|
|
138
|
+
private blindStepBacks = 0;
|
|
139
|
+
private warned = new Set<string>();
|
|
140
|
+
private random: () => number;
|
|
141
|
+
|
|
142
|
+
public stats: AiPlayerStats = {
|
|
143
|
+
promptsAnswered: 0,
|
|
144
|
+
stepBacks: 0,
|
|
145
|
+
rejections: 0,
|
|
146
|
+
stalls: 0,
|
|
147
|
+
ruleErrors: 0,
|
|
148
|
+
stuck: 0,
|
|
149
|
+
untagged: {},
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
/** @internal */
|
|
153
|
+
constructor(
|
|
154
|
+
private playerId: string,
|
|
155
|
+
connection: Connection,
|
|
156
|
+
private game: Game<P, R>,
|
|
157
|
+
private view: AiView,
|
|
158
|
+
private options: AiPlayerOptions = {},
|
|
159
|
+
) {
|
|
160
|
+
super();
|
|
161
|
+
this.serverPrompts = connection.get<ServerPrompts<P, R>>("ServerPrompts");
|
|
162
|
+
this.random = createRandom(options.seed);
|
|
163
|
+
this.memoryStore = options.memoryStore || undefined;
|
|
164
|
+
this._enabled = !options.disabled;
|
|
165
|
+
this.memory = deepFreeze(this.loadMemory());
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
override dispose() {
|
|
169
|
+
this.disposed = true;
|
|
170
|
+
super.dispose();
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** The bot's current (frozen) memory, for debugging and tests */
|
|
174
|
+
public get memorySnapshot(): Readonly<M> {
|
|
175
|
+
return this.memory;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** True when the bot is not working on anything */
|
|
179
|
+
public get idle() {
|
|
180
|
+
return !this.running;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/** False while paused: prompts are kept but not answered (see `setEnabled`) */
|
|
184
|
+
public get enabled() {
|
|
185
|
+
return this._enabled;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Pause or resume the bot without losing its memory. While paused it still receives prompts and
|
|
190
|
+
* answers the latest one when resumed. Hosts use this to make sure only one copy of a match drives
|
|
191
|
+
* its bots when several copies exist (every browser tab in client-trust mode, or two bundle
|
|
192
|
+
* versions of one match on a server). On resume the memory is re-read from the store, since
|
|
193
|
+
* another copy may have moved the plan on.
|
|
194
|
+
*/
|
|
195
|
+
public setEnabled(enabled: boolean) {
|
|
196
|
+
if (this._enabled === enabled) {
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
this._enabled = enabled;
|
|
200
|
+
if (enabled) {
|
|
201
|
+
this.memory = deepFreeze(this.loadMemory());
|
|
202
|
+
this.history = [];
|
|
203
|
+
this.handledSeq = -1;
|
|
204
|
+
void this.pump();
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
private loadMemory(): M {
|
|
209
|
+
const stored = this.memoryStore?.read();
|
|
210
|
+
if (stored !== undefined) {
|
|
211
|
+
return stored;
|
|
212
|
+
}
|
|
213
|
+
return (this.game.ai?.initialMemory?.() ?? {}) as M;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/** Commits new memory and mirrors it to the store so it survives a reload. */
|
|
217
|
+
private setMemory(memory: M) {
|
|
218
|
+
this.memory = deepFreeze(memory);
|
|
219
|
+
try {
|
|
220
|
+
this.memoryStore?.write(this.memory);
|
|
221
|
+
} catch (e) {
|
|
222
|
+
this.warnOnce("memoryStore", "could not persist memory", e);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/** True while an answer has been sent but the server has not reacted within `stallWarningMs` */
|
|
227
|
+
public get stalled() {
|
|
228
|
+
return this._stalled;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/** @internal Called by ServerPrompts for every player's prompt changes */
|
|
232
|
+
public async setPromptForPlayer(playerId: string, prompt?: PromptSerialization, clockDetails?: ClockDetails) {
|
|
233
|
+
if (playerId !== this.playerId || this.disposed) {
|
|
234
|
+
return;
|
|
235
|
+
}
|
|
236
|
+
this.seq++;
|
|
237
|
+
this.latest = prompt ? { prompt, clockDetails, seq: this.seq } : undefined;
|
|
238
|
+
void this.pump();
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
private async pump() {
|
|
242
|
+
if (this.running) {
|
|
243
|
+
return;
|
|
244
|
+
}
|
|
245
|
+
this.running = true;
|
|
246
|
+
try {
|
|
247
|
+
while (
|
|
248
|
+
this.latest &&
|
|
249
|
+
this.latest.seq !== this.handledSeq &&
|
|
250
|
+
!this.disposed &&
|
|
251
|
+
this._enabled &&
|
|
252
|
+
!this.options.hold?.(this.latest.prompt)
|
|
253
|
+
) {
|
|
254
|
+
const current = this.latest;
|
|
255
|
+
this.handledSeq = current.seq;
|
|
256
|
+
try {
|
|
257
|
+
await this.handle(current);
|
|
258
|
+
} catch (e) {
|
|
259
|
+
if (!this.disposed) {
|
|
260
|
+
console.error(`[AI ${this.playerId}] failed to handle prompt "${current.prompt.id}"`, e);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
if (!this._enabled && this.latest?.seq === current.seq) {
|
|
264
|
+
// Paused mid-prompt: leave it unanswered, and answer it when resumed
|
|
265
|
+
this.handledSeq = -1;
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
} finally {
|
|
269
|
+
this.running = false;
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/** Seq of a prompt `choose` chose to leave unanswered */
|
|
274
|
+
private heldSeq = -1;
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* Looks at the current prompt again: answers it if `hold` no longer holds the bot and `choose` no
|
|
278
|
+
* longer leaves it unanswered. Does nothing to a prompt the bot already answered.
|
|
279
|
+
*/
|
|
280
|
+
public recheck() {
|
|
281
|
+
if (this.disposed) return;
|
|
282
|
+
if (this.latest && this.heldSeq === this.latest.seq) {
|
|
283
|
+
this.heldSeq = -1;
|
|
284
|
+
this.handledSeq = -1;
|
|
285
|
+
}
|
|
286
|
+
void this.pump();
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
private isCurrent(entry: QueuedPrompt) {
|
|
290
|
+
return !this.disposed && this._enabled && this.latest?.seq === entry.seq;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
private async handle(entry: QueuedPrompt) {
|
|
294
|
+
const { prompt } = entry;
|
|
295
|
+
|
|
296
|
+
if (!prompt.canReset) {
|
|
297
|
+
// Nothing before this prompt can be undone (rng, player switch, start of game)
|
|
298
|
+
this.history = [];
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
await this.view.sync(entry.clockDetails?.clock);
|
|
302
|
+
if (!this.isCurrent(entry)) {
|
|
303
|
+
return;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
if (prompt.type === "NoValidMovesPrompt") {
|
|
307
|
+
await this.backtrack(prompt, "no valid moves");
|
|
308
|
+
return;
|
|
309
|
+
}
|
|
310
|
+
if (prompt.type !== "PickPrompt") {
|
|
311
|
+
return;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
const fingerprint = this.fingerprint(prompt, this.view.signature);
|
|
315
|
+
if (!prompt.canReset) {
|
|
316
|
+
this.pruneDeadEndsExcept(fingerprint);
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
const options = this.enumerate(prompt);
|
|
320
|
+
if (options.length === 0) {
|
|
321
|
+
this.warnOnce(
|
|
322
|
+
`empty:${prompt.id}`,
|
|
323
|
+
`could not map any options for "${promptMessage(prompt)}"; answering blindly`,
|
|
324
|
+
);
|
|
325
|
+
await this.answerBlindly(prompt);
|
|
326
|
+
return;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
const dead = this.dead.get(fingerprint);
|
|
330
|
+
let live = dead ? options.filter((o) => !dead.has(choiceKey(o))) : options;
|
|
331
|
+
if (live.length === 0) {
|
|
332
|
+
const visits = (this.exhaustedVisits.get(fingerprint) ?? 0) + 1;
|
|
333
|
+
this.exhaustedVisits.set(fingerprint, visits);
|
|
334
|
+
if (prompt.canReset && this.history.length > 0) {
|
|
335
|
+
await this.backtrack(prompt, "every option leads to a dead end");
|
|
336
|
+
return;
|
|
337
|
+
}
|
|
338
|
+
if (visits > MAX_EXHAUSTED_VISITS) {
|
|
339
|
+
this.stats.stuck++;
|
|
340
|
+
console.error(
|
|
341
|
+
`[AI ${this.playerId}] stuck at "${promptMessage(prompt)}": every option leads to no valid moves and it cannot undo further. Giving up on this prompt.`,
|
|
342
|
+
);
|
|
343
|
+
return;
|
|
344
|
+
}
|
|
345
|
+
this.warnOnce(`exhausted:${fingerprint}`, `every option at "${promptMessage(prompt)}" failed; retrying anyway`);
|
|
346
|
+
this.dead.delete(fingerprint);
|
|
347
|
+
live = options;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
await this.think();
|
|
351
|
+
if (!this.isCurrent(entry)) {
|
|
352
|
+
return;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
const rejected = new Set<string>();
|
|
356
|
+
for (;;) {
|
|
357
|
+
const candidates = live.filter((o) => !rejected.has(choiceKey(o)));
|
|
358
|
+
if (candidates.length === 0) {
|
|
359
|
+
this.stats.stuck++;
|
|
360
|
+
console.error(`[AI ${this.playerId}] the server rejected every option for "${promptMessage(prompt)}"`);
|
|
361
|
+
return;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
const chosen = this.choose(prompt, candidates);
|
|
365
|
+
if (!chosen) {
|
|
366
|
+
// The harness wants this prompt left alone for now (see `recheck`)
|
|
367
|
+
this.heldSeq = entry.seq;
|
|
368
|
+
return;
|
|
369
|
+
}
|
|
370
|
+
const memoryBefore = this.memory;
|
|
371
|
+
// Written before the answer is sent, so the save that follows the answer carries it
|
|
372
|
+
this.setMemory(applyPlan(memoryBefore, chosen.plan));
|
|
373
|
+
|
|
374
|
+
const result = await this.withStallWarning(
|
|
375
|
+
this.serverPrompts.resolvePrompt(prompt.id, chosen.option.response),
|
|
376
|
+
prompt,
|
|
377
|
+
);
|
|
378
|
+
|
|
379
|
+
if (result && result.id === prompt.id) {
|
|
380
|
+
// ServerPrompts rejected the answer and sent the same prompt back
|
|
381
|
+
this.setMemory(memoryBefore);
|
|
382
|
+
this.stats.rejections++;
|
|
383
|
+
rejected.add(choiceKey(chosen.option));
|
|
384
|
+
continue;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
this.history.push({ fingerprint, choice: choiceKey(chosen.option), memoryBefore });
|
|
388
|
+
this.stats.promptsAnswered++;
|
|
389
|
+
this.blindStepBacks = 0;
|
|
390
|
+
return;
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
/** Lists every legal answer to the prompt, with chits taken from this player's view. */
|
|
395
|
+
private enumerate(prompt: PromptSerialization): AiOption[] {
|
|
396
|
+
const picks: PickSerialization[] = prompt.details?.picks ?? [];
|
|
397
|
+
const find = this.view.findChit;
|
|
398
|
+
const options: AiOption[] = [];
|
|
399
|
+
|
|
400
|
+
picks.forEach((sp, idx) => {
|
|
401
|
+
const pick: AiPickInfo = {
|
|
402
|
+
index: idx,
|
|
403
|
+
type: sp.type,
|
|
404
|
+
message: sp.message,
|
|
405
|
+
help: sp.help,
|
|
406
|
+
context: sp.context ? find(sp.context) : undefined,
|
|
407
|
+
};
|
|
408
|
+
const base = { key: sp.ai?.k, data: sp.ai?.d, pick };
|
|
409
|
+
|
|
410
|
+
switch (sp.type) {
|
|
411
|
+
case "ChitPick": {
|
|
412
|
+
(sp.details.c as string[]).forEach((id) => {
|
|
413
|
+
const chit = find(id);
|
|
414
|
+
if (chit) {
|
|
415
|
+
options.push({ ...base, kind: "chit", chit, response: response(idx, "ChitPick", id) });
|
|
416
|
+
}
|
|
417
|
+
});
|
|
418
|
+
break;
|
|
419
|
+
}
|
|
420
|
+
case "DragPick": {
|
|
421
|
+
const seen = new Set<string>();
|
|
422
|
+
const targets = (sp.details.d as string[][]).flat();
|
|
423
|
+
(sp.details.c as string[]).forEach((chitId) => {
|
|
424
|
+
const chit = find(chitId);
|
|
425
|
+
targets.forEach((targetChitId) => {
|
|
426
|
+
const target = find(targetChitId);
|
|
427
|
+
const k = `${chitId}->${targetChitId}`;
|
|
428
|
+
if (chit && target && !seen.has(k)) {
|
|
429
|
+
seen.add(k);
|
|
430
|
+
options.push({
|
|
431
|
+
...base,
|
|
432
|
+
kind: "drag",
|
|
433
|
+
chit,
|
|
434
|
+
target,
|
|
435
|
+
response: response(idx, "DragPick", { chitId, targetChitId }),
|
|
436
|
+
});
|
|
437
|
+
}
|
|
438
|
+
});
|
|
439
|
+
});
|
|
440
|
+
break;
|
|
441
|
+
}
|
|
442
|
+
case "ButtonPick": {
|
|
443
|
+
const buttonType = sp.details?.__buttonType as string | undefined;
|
|
444
|
+
pick.buttonType = buttonType;
|
|
445
|
+
options.push({
|
|
446
|
+
...base,
|
|
447
|
+
kind: "button",
|
|
448
|
+
button: this.makeButton(buttonType, sp.details),
|
|
449
|
+
response: response(idx, "ButtonPick", null),
|
|
450
|
+
});
|
|
451
|
+
break;
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
});
|
|
455
|
+
|
|
456
|
+
return options;
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
private makeButton(buttonType: string | undefined, details: any): GameButton {
|
|
460
|
+
const ButtonType =
|
|
461
|
+
(buttonType && this.game.buttonLibrary?.[buttonType]) || (buttonType === "Confirm" ? Confirm : undefined);
|
|
462
|
+
const button = ButtonType ? new ButtonType() : new GameButton();
|
|
463
|
+
try {
|
|
464
|
+
button.deserialize(details, (id: string) => this.view.findChit(id) as Chit);
|
|
465
|
+
} catch {
|
|
466
|
+
// the button is informational only; a failed deserialize shouldn't stop the bot
|
|
467
|
+
}
|
|
468
|
+
return button;
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
private choose(prompt: PromptSerialization, candidates: AiOption[]): Scored<M> | null {
|
|
472
|
+
const ai = this.game.ai;
|
|
473
|
+
const rules: { [key: string]: AiRule<P, R, M> } = (ai?.rules as any) ?? {};
|
|
474
|
+
const defaultRule = ai?.default as AiRule<P, R, M> | undefined;
|
|
475
|
+
const ruleFor = (o: AiOption) => (o.key ? rules[o.key] : undefined) ?? defaultRule;
|
|
476
|
+
|
|
477
|
+
const root = this.view.root as R;
|
|
478
|
+
const ctx: AiContext<P, R, M> = {
|
|
479
|
+
view: root,
|
|
480
|
+
me: root?.players.find((p) => p.id === this.playerId) as P,
|
|
481
|
+
options: candidates,
|
|
482
|
+
prompt: { message: promptMessage(prompt), help: prompt.details?.help },
|
|
483
|
+
random: this.random,
|
|
484
|
+
memory: this.memory,
|
|
485
|
+
find: this.view.findChit,
|
|
486
|
+
params: this.options.params,
|
|
487
|
+
};
|
|
488
|
+
|
|
489
|
+
if (!candidates.some((o) => ruleFor(o))) {
|
|
490
|
+
// No rule applies (an untagged pick, or the engine's own Confirm): the harness still decides
|
|
491
|
+
candidates.forEach((o) => this.noteUntagged(o));
|
|
492
|
+
const unscored: Scored<M>[] = candidates.map((option) => ({ option, score: 0 }));
|
|
493
|
+
const forced = this.options.choose?.(unscored, ctx);
|
|
494
|
+
if (forced === null) return null;
|
|
495
|
+
if (forced !== undefined && forced >= 0 && forced < unscored.length) {
|
|
496
|
+
this.options.onDecision?.({ playerId: this.playerId, prompt: ctx.prompt, scored: unscored, chosen: forced, ctx });
|
|
497
|
+
return unscored[forced];
|
|
498
|
+
}
|
|
499
|
+
return { option: this.chooseLegacy(prompt, candidates), score: 0 };
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
const scored: Scored<M>[] = candidates.map((option) => {
|
|
503
|
+
const rule = ruleFor(option);
|
|
504
|
+
if (!rule) {
|
|
505
|
+
this.noteUntagged(option);
|
|
506
|
+
return { option, score: 0 };
|
|
507
|
+
}
|
|
508
|
+
try {
|
|
509
|
+
return { option, ...normalizeScore(rule(option, ctx)) };
|
|
510
|
+
} catch (e) {
|
|
511
|
+
this.stats.ruleErrors++;
|
|
512
|
+
this.warnOnce(`rule:${option.key ?? "default"}`, `rule "${option.key ?? "default"}" threw; scoring 0`, e);
|
|
513
|
+
return { option, score: 0 };
|
|
514
|
+
}
|
|
515
|
+
});
|
|
516
|
+
|
|
517
|
+
const scores = scored.map((s) => s.score);
|
|
518
|
+
const forced = this.options.choose?.(scored, ctx);
|
|
519
|
+
if (forced === null) return null;
|
|
520
|
+
let index = forced;
|
|
521
|
+
if (index === undefined || index < 0 || index >= scored.length) {
|
|
522
|
+
const temperature = this.options.temperature ?? this.game.ai?.temperature;
|
|
523
|
+
index =
|
|
524
|
+
temperature !== undefined
|
|
525
|
+
? chooseSoftmax(scores, temperature, this.random)
|
|
526
|
+
: chooseIndex(scores, 1 - clamp(this.options.difficulty ?? 0.85, 0, 1), this.random);
|
|
527
|
+
}
|
|
528
|
+
this.options.onDecision?.({ playerId: this.playerId, prompt: ctx.prompt, scored, chosen: index, ctx });
|
|
529
|
+
return scored[index];
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
/**
|
|
533
|
+
* The old demo behaviour, which games tune with `Game.demoPickWeight`: choose a pick weighted by
|
|
534
|
+
* (number of choices × demoPickWeight), then an option within it uniformly. For a DragPick the
|
|
535
|
+
* DragTarget group is chosen uniformly first, so splitting targets into groups steers drops.
|
|
536
|
+
*/
|
|
537
|
+
private chooseLegacy(prompt: PromptSerialization, candidates: AiOption[]): AiOption {
|
|
538
|
+
const picks: PickSerialization[] = prompt.details?.picks ?? [];
|
|
539
|
+
const pickIndexes = [...new Set(candidates.map((o) => o.pick.index))];
|
|
540
|
+
const weights = pickIndexes.map((i) => {
|
|
541
|
+
const multiplier = this.game.demoPickWeight ? this.game.demoPickWeight(picks, picks[i]) : 1;
|
|
542
|
+
return pickWeight(picks[i]) * multiplier;
|
|
543
|
+
});
|
|
544
|
+
const chosen = pickIndexes[chooseWeighted(weights, this.random)];
|
|
545
|
+
const inPick = candidates.filter((o) => o.pick.index === chosen);
|
|
546
|
+
const pickRandom = <T>(items: T[]) => items[Math.floor(this.random() * items.length)];
|
|
547
|
+
|
|
548
|
+
const sp = picks[chosen];
|
|
549
|
+
if (sp?.type === "DragPick") {
|
|
550
|
+
const groups = (sp.details.d as string[][]).filter((g) => g.length > 0);
|
|
551
|
+
if (groups.length > 0) {
|
|
552
|
+
const targetId = pickRandom(pickRandom(groups));
|
|
553
|
+
const matching = inPick.filter((o) => o.kind === "drag" && o.target.id === targetId);
|
|
554
|
+
if (matching.length > 0) {
|
|
555
|
+
return pickRandom(matching);
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
return pickRandom(inPick);
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
/** Last resort when no options could be mapped to chits in the view. */
|
|
563
|
+
private async answerBlindly(prompt: PromptSerialization) {
|
|
564
|
+
const picks: PickSerialization[] = prompt.details?.picks ?? [];
|
|
565
|
+
if (picks.length === 0) {
|
|
566
|
+
return;
|
|
567
|
+
}
|
|
568
|
+
const idx = Math.floor(this.random() * picks.length);
|
|
569
|
+
const sp = picks[idx];
|
|
570
|
+
const pickRandom = <T>(items: T[]) => items[Math.floor(this.random() * items.length)];
|
|
571
|
+
let value: any = null;
|
|
572
|
+
if (sp.type === "ChitPick") {
|
|
573
|
+
value = pickRandom(sp.details.c);
|
|
574
|
+
} else if (sp.type === "DragPick") {
|
|
575
|
+
value = { chitId: pickRandom(sp.details.c), targetChitId: pickRandom((sp.details.d as string[][]).flat()) };
|
|
576
|
+
}
|
|
577
|
+
await this.withStallWarning(this.serverPrompts.resolvePrompt(prompt.id, response(idx, sp.type, value)), prompt);
|
|
578
|
+
this.stats.promptsAnswered++;
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
/** Undo the bot's last answer, remembering it as a dead end for that exact situation. */
|
|
582
|
+
private async backtrack(prompt: PromptSerialization, reason: string) {
|
|
583
|
+
if (!prompt.canReset) {
|
|
584
|
+
this.stats.stuck++;
|
|
585
|
+
console.error(`[AI ${this.playerId}] ${reason} at "${promptMessage(prompt)}" and nothing can be undone`);
|
|
586
|
+
return;
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
const last = this.history.pop();
|
|
590
|
+
if (last) {
|
|
591
|
+
const dead = this.dead.get(last.fingerprint) ?? new Set<string>();
|
|
592
|
+
dead.add(last.choice);
|
|
593
|
+
this.dead.set(last.fingerprint, dead);
|
|
594
|
+
this.setMemory(last.memoryBefore);
|
|
595
|
+
} else {
|
|
596
|
+
// We don't know what we're undoing (e.g. the bot was reconnected), so cap blind undos
|
|
597
|
+
if (++this.blindStepBacks > MAX_BLIND_STEP_BACKS) {
|
|
598
|
+
this.stats.stuck++;
|
|
599
|
+
console.error(`[AI ${this.playerId}] ${reason}; giving up after ${MAX_BLIND_STEP_BACKS} blind undos`);
|
|
600
|
+
return;
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
this.stats.stepBacks++;
|
|
605
|
+
await this.serverPrompts.stepBackPrompt(false);
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
private fingerprint(prompt: PromptSerialization, viewSignature: string) {
|
|
609
|
+
return hashString(`${JSON.stringify(prompt)}|${viewSignature}`);
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
private pruneDeadEndsExcept(fingerprint: string) {
|
|
613
|
+
const keep = this.dead.get(fingerprint);
|
|
614
|
+
this.dead.clear();
|
|
615
|
+
if (keep) {
|
|
616
|
+
this.dead.set(fingerprint, keep);
|
|
617
|
+
}
|
|
618
|
+
const visits = this.exhaustedVisits.get(fingerprint);
|
|
619
|
+
this.exhaustedVisits.clear();
|
|
620
|
+
if (visits) {
|
|
621
|
+
this.exhaustedVisits.set(fingerprint, visits);
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
private noteUntagged(option: AiOption) {
|
|
626
|
+
if (option.kind === "button" && option.pick.buttonType === "Confirm") {
|
|
627
|
+
return; // engine-inserted confirms don't need rules
|
|
628
|
+
}
|
|
629
|
+
const message = option.pick.message || `(untagged ${option.pick.type})`;
|
|
630
|
+
const isNew = this.stats.untagged[message] === undefined;
|
|
631
|
+
this.stats.untagged[message] = (this.stats.untagged[message] ?? 0) + 1;
|
|
632
|
+
if (isNew && this.game.ai) {
|
|
633
|
+
this.log(`no rule for "${message}"${option.key ? ` (key "${option.key}")` : ""}; scoring it 0`);
|
|
634
|
+
} else if (isNew && !this.warned.has("noai")) {
|
|
635
|
+
this.warned.add("noai");
|
|
636
|
+
this.log("this game has no `ai`; playing at random. Tag picks with .ai(key) and define rules with defineAi()");
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
private async think() {
|
|
641
|
+
const t = this.options.thinkMs;
|
|
642
|
+
const ms = Array.isArray(t) ? t[0] + this.random() * Math.max(0, t[1] - t[0]) : (t ?? 0);
|
|
643
|
+
if (ms > 0) {
|
|
644
|
+
await new Promise((resolve) => setTimeout(resolve, ms));
|
|
645
|
+
}
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
/**
|
|
649
|
+
* Awaits the server's reaction to an answer. If it takes longer than `stallWarningMs`, warns and
|
|
650
|
+
* counts a stall, then keeps waiting: the answer is in flight, so the bot cannot know whether it
|
|
651
|
+
* was accepted until the server says so.
|
|
652
|
+
*/
|
|
653
|
+
private async withStallWarning<T>(p: Promise<T>, prompt: PromptSerialization): Promise<T> {
|
|
654
|
+
const ms = this.options.stallWarningMs ?? 10000;
|
|
655
|
+
const timer = setTimeout(() => {
|
|
656
|
+
this._stalled = true;
|
|
657
|
+
this.stats.stalls++;
|
|
658
|
+
console.warn(
|
|
659
|
+
`[AI ${this.playerId}] answered "${promptMessage(prompt)}" but the game hasn't changed after ${ms}ms. ` +
|
|
660
|
+
"If that pick's callback can leave state untouched, the match waits forever.",
|
|
661
|
+
);
|
|
662
|
+
}, ms);
|
|
663
|
+
try {
|
|
664
|
+
return await p;
|
|
665
|
+
} finally {
|
|
666
|
+
clearTimeout(timer);
|
|
667
|
+
this._stalled = false;
|
|
668
|
+
}
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
private log(message: string) {
|
|
672
|
+
if (this.options.log !== false) {
|
|
673
|
+
console.info(`[AI ${this.playerId}] ${message}`);
|
|
674
|
+
}
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
private warnOnce(key: string, message: string, error?: unknown) {
|
|
678
|
+
if (this.warned.has(key)) {
|
|
679
|
+
return;
|
|
680
|
+
}
|
|
681
|
+
this.warned.add(key);
|
|
682
|
+
if (this.options.log !== false) {
|
|
683
|
+
console.warn(`[AI ${this.playerId}] ${message}`, ...(error ? [error] : []));
|
|
684
|
+
}
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
function response(idx: number, pickType: PickType, value: any) {
|
|
689
|
+
return { idx, pickType, value };
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
function choiceKey(option: AiOption) {
|
|
693
|
+
return JSON.stringify(option.response);
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
function pickWeight(sp: PickSerialization | undefined) {
|
|
697
|
+
switch (sp?.type) {
|
|
698
|
+
case "ChitPick":
|
|
699
|
+
return sp.details.c.length;
|
|
700
|
+
case "DragPick":
|
|
701
|
+
return (sp.details.d as string[][]).reduce((sum, targets) => sum + targets.length, 0);
|
|
702
|
+
default:
|
|
703
|
+
return 1;
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
function promptMessage(prompt: PromptSerialization): string {
|
|
708
|
+
if (prompt.details?.message) {
|
|
709
|
+
return prompt.details.message;
|
|
710
|
+
}
|
|
711
|
+
const picks: PickSerialization[] = prompt.details?.picks ?? [];
|
|
712
|
+
return picks
|
|
713
|
+
.map((p) => p.message)
|
|
714
|
+
.filter((m) => m)
|
|
715
|
+
.join(" or ");
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
function clamp(n: number, min: number, max: number) {
|
|
719
|
+
return Math.min(max, Math.max(min, n));
|
|
720
|
+
}
|
|
721
|
+
|
|
722
|
+
function deepFreeze<T>(value: T): T {
|
|
723
|
+
const isPlain =
|
|
724
|
+
Array.isArray(value) || (value && typeof value === "object" && Object.getPrototypeOf(value) === Object.prototype);
|
|
725
|
+
if (isPlain && !Object.isFrozen(value)) {
|
|
726
|
+
Object.freeze(value);
|
|
727
|
+
Object.values(value as object).forEach((v) => deepFreeze(v));
|
|
728
|
+
}
|
|
729
|
+
return value;
|
|
730
|
+
}
|