GO: Alternate fix for race conditions (#1260)

This commit is contained in:
Snarling
2024-05-11 19:58:59 -04:00
committed by GitHub
parent 1b8205e9d5
commit e23db93c8b
8 changed files with 119 additions and 146 deletions
+15 -2
View File
@@ -2,13 +2,14 @@ import type { BoardState, OpponentStats, SimpleBoard } from "./Types";
import type { PartialRecord } from "../Types/Record";
import { Truthy } from "lodash";
import { GoColor, GoOpponent } from "@enums";
import { GoColor, GoOpponent, GoPlayType } from "@enums";
import { Go } from "./Go";
import { boardStateFromSimpleBoard, simpleBoardFromBoard } from "./boardAnalysis/boardAnalysis";
import { boardStateFromSimpleBoard, getPreviousMove, simpleBoardFromBoard } from "./boardAnalysis/boardAnalysis";
import { assertLoadingType } from "../utils/TypeAssertion";
import { getEnumHelper } from "../utils/EnumHelper";
import { boardSizes } from "./Constants";
import { isInteger, isNumber } from "../types";
import { makeAIMove } from "./boardAnalysis/goAI";
type PreviousGameSaveData = { ai: GoOpponent; board: SimpleBoard; previousPlayer: GoColor | null } | null;
type CurrentGameSaveData = PreviousGameSaveData & {
@@ -77,6 +78,18 @@ export function loadGo(data: unknown): boolean {
Go.currentGame = currentGame;
Go.previousGame = previousGame;
Go.stats = stats;
// If it's the AI's turn, initiate their turn, which will populate nextTurn
if (currentGame.previousPlayer === GoColor.black && currentGame.ai !== GoOpponent.none) makeAIMove(currentGame);
// If it's not the AI's turn and we're not in gameover status, initialize nextTurn promise based on the previous move/pass
else if (currentGame.previousPlayer) {
const previousMove = getPreviousMove();
Go.nextTurn = Promise.resolve(
previousMove
? { type: GoPlayType.move, x: previousMove[0], y: previousMove[1] }
: { type: GoPlayType.pass, x: null, y: null },
);
}
return true;
}