diff --git a/src/Go/Go.ts b/src/Go/Go.ts index cca6cbd53..c21fd6698 100644 --- a/src/Go/Go.ts +++ b/src/Go/Go.ts @@ -2,7 +2,7 @@ import type { BoardState, OpponentStats } from "./Types"; import type { GoOpponent } from "@enums"; import { getRecordKeys, PartialRecord } from "../Types/Record"; -import { resetAI } from "./boardAnalysis/goAI"; +import { resetGoPromises } from "./boardAnalysis/goAI"; import { getNewBoardState } from "./boardState/boardState"; import { EventEmitter } from "../utils/EventEmitter"; import { newOpponentStats } from "./Constants"; @@ -20,10 +20,10 @@ export class GoObject { } } prestigeSourceFile() { - resetAI(); this.previousGame = null; this.currentGame = getNewBoardState(7); this.stats = {}; + resetGoPromises(); } /** diff --git a/src/Go/boardAnalysis/goAI.ts b/src/Go/boardAnalysis/goAI.ts index bfaff1f8e..21775f9f6 100644 --- a/src/Go/boardAnalysis/goAI.ts +++ b/src/Go/boardAnalysis/goAI.ts @@ -27,7 +27,7 @@ type PlayerPromise = { resolver: ((play?: Play) => void) | null; }; -const gameOver = { type: GoPlayType.gameOver, x: null, y: null } as const; +const gameOver: Play = { type: GoPlayType.gameOver, x: null, y: null } as const; const playerPromises: Record = { [GoColor.black]: { nextTurn: Promise.resolve(gameOver), resolver: null }, [GoColor.white]: { nextTurn: Promise.resolve(gameOver), resolver: null }, @@ -40,6 +40,11 @@ export function getNextTurn(color: GoColor.black | GoColor.white): Promise return playerPromises[color].nextTurn; } +export function resetGoPromises(): void { + resetAI(); + handleNextTurn().catch((error) => exceptionAlert(error, true)); +} + /** * Does common processing in response to a move being made. * @@ -51,7 +56,7 @@ export function getNextTurn(color: GoColor.black | GoColor.white): Promise * handling and dispatches common events. * @returns the nextTurn promise for the player who just moved */ -export function handleNextTurn(boardState: BoardState, useOfflineCycles = true): Promise { +export function handleNextTurn(boardState: BoardState = Go.currentGame, useOfflineCycles = true): Promise { const previousColor = boardState.previousPlayer; if (previousColor === null) { // The game is over. We shouldn't get here in most circumstances, diff --git a/src/Go/effects/netscriptGoImplementation.ts b/src/Go/effects/netscriptGoImplementation.ts index 4b94d0f22..ac8230d9f 100644 --- a/src/Go/effects/netscriptGoImplementation.ts +++ b/src/Go/effects/netscriptGoImplementation.ts @@ -10,7 +10,7 @@ import { passTurn, updateCaptures, } from "../boardState/boardState"; -import { getNextTurn, handleNextTurn, resetAI } from "../boardAnalysis/goAI"; +import { getNextTurn, handleNextTurn, resetGoPromises } from "../boardAnalysis/goAI"; import { evaluateIfMoveIsValid, getControlledSpace, @@ -22,7 +22,6 @@ import { endGoGame, getOpponentStats, getScore, resetWinstreak } from "../boardA import { WHRNG } from "../../Casino/RNG"; import { getRecordKeys } from "../../Types/Record"; import { CalculateEffect, getEffectTypeForFaction } from "./effect"; -import { exceptionAlert } from "../../utils/helpers/exceptionAlert"; import { newOpponentStats } from "../Constants"; /** @@ -342,9 +341,8 @@ export function resetBoardState( resetWinstreak(oldBoardState.ai, false); } - resetAI(); Go.currentGame = getNewBoardState(boardSize, opponent, true); - handleNextTurn(Go.currentGame).catch((error) => exceptionAlert(error)); + resetGoPromises(); logger(`New game started: ${opponent}, ${boardSize}x${boardSize}`); return simpleBoardFromBoard(Go.currentGame.board); } diff --git a/src/Go/ui/GoGameboardWrapper.tsx b/src/Go/ui/GoGameboardWrapper.tsx index 9fb82b8b7..66f1a67ca 100644 --- a/src/Go/ui/GoGameboardWrapper.tsx +++ b/src/Go/ui/GoGameboardWrapper.tsx @@ -18,7 +18,7 @@ import { GoScoreModal } from "./GoScoreModal"; import { GoGameboard } from "./GoGameboard"; import { GoSubnetSearch } from "./GoSubnetSearch"; import { CorruptableText } from "../../ui/React/CorruptableText"; -import { handleNextTurn, resetAI } from "../boardAnalysis/goAI"; +import { handleNextTurn, resetGoPromises } from "../boardAnalysis/goAI"; import { GoScoreExplanation } from "./GoScoreExplanation"; import { exceptionAlert } from "../../utils/helpers/exceptionAlert"; @@ -133,9 +133,8 @@ export function GoGameboardWrapper({ showInstructions }: GoGameboardWrapperProps resetWinstreak(boardState.ai, false); } - resetAI(); Go.currentGame = getNewBoardState(newBoardSize, newOpponent, true); - handleNextTurn(Go.currentGame).catch((error) => exceptionAlert(error)); + resetGoPromises(); } function getPriorMove() { diff --git a/src/engine.tsx b/src/engine.tsx index 0759d75c8..84f1efb61 100644 --- a/src/engine.tsx +++ b/src/engine.tsx @@ -47,6 +47,7 @@ import { SaveData } from "./types"; import { Go } from "./Go/Go"; import { EventEmitter } from "./utils/EventEmitter"; import { Companies } from "./Company/Companies"; +import { resetGoPromises } from "./Go/boardAnalysis/goAI"; declare global { // This property is only available in the dev build @@ -391,6 +392,7 @@ const Engine: { Player.init(); initForeignServers(Player.getHomeComputer()); Player.reapplyAllAugmentations(); + resetGoPromises(); // Start interactive tutorial iTutorialStart();