IPVGO: Support scripts playing against each other as each color on "No AI" boards (#1917)

This is a big change with a *lot* of moving parts.

The largest part of it is enabling scripts to `playAsWhite` as a parameter to many Go functions. In the implementation, this involved a significant rewrite of `opponentNextTurn` promise handling.

A number of other changes and bugfixes are included:
* Fixes the issue where handicap stones are added on game load.
* Better typing for error callbacks.
* Throw errors instead of deadlocking on bad cheat usage.
* Return always-resolved gameOver promise after game end
* Added a new `resetStats` api function.

---------

Co-authored-by: David Walker <d0sboots@gmail.com>
This commit is contained in:
Michael Ficocelli
2025-02-02 23:47:16 -05:00
committed by GitHub
parent de6b202341
commit c8d2c9f769
30 changed files with 502 additions and 263 deletions

View File

@@ -24,9 +24,9 @@ import {
handlePassTurn,
makePlayerMove,
resetBoardState,
resetStats,
validateBoardState,
validateMove,
validateTurn,
} from "../Go/effects/netscriptGoImplementation";
import { getEnumHelper } from "../utils/EnumHelper";
import { errorMessage } from "../Netscript/ErrorMessages";
@@ -43,19 +43,20 @@ export function NetscriptGo(): InternalAPI<NSGo> {
return {
makeMove:
(ctx: NetscriptContext) =>
(_x, _y): Promise<Play> => {
(_x, _y, playAsWhite): Promise<Play> => {
const x = helpers.number(ctx, "x", _x);
const y = helpers.number(ctx, "y", _y);
validateMove(error(ctx), x, y, "makeMove");
return makePlayerMove(logger(ctx), error(ctx), x, y);
validateMove(error(ctx), x, y, "makeMove", { playAsWhite });
return makePlayerMove(logger(ctx), error(ctx), x, y, !!playAsWhite);
},
passTurn: (ctx: NetscriptContext) => async (): Promise<Play> => {
validateTurn(error(ctx), "passTurn()");
return handlePassTurn(logger(ctx));
},
opponentNextTurn: (ctx: NetscriptContext) => async (_logOpponentMove) => {
const logOpponentMove = typeof _logOpponentMove === "boolean" ? _logOpponentMove : true;
return getOpponentNextMove(logOpponentMove, logger(ctx));
passTurn:
(ctx: NetscriptContext) =>
(playAsWhite): Promise<Play> => {
validateMove(error(ctx), -1, -1, "passTurn", { playAsWhite, pass: true });
return handlePassTurn(logger(ctx), !!playAsWhite);
},
opponentNextTurn: (ctx: NetscriptContext) => async (logOpponentMove, playAsWhite) => {
return getOpponentNextMove(logger(ctx), !!logOpponentMove, !!playAsWhite);
},
getBoardState: () => () => {
return simpleBoardFromBoard(Go.currentGame.board);
@@ -79,9 +80,9 @@ export function NetscriptGo(): InternalAPI<NSGo> {
return resetBoardState(logger(ctx), error(ctx), opponent, boardSize);
},
analysis: {
getValidMoves: (ctx) => (_boardState, _priorBoardState) => {
getValidMoves: (ctx) => (_boardState, _priorBoardState, playAsWhite) => {
const State = validateBoardState(error(ctx), _boardState, _priorBoardState);
return getValidMoves(State);
return getValidMoves(State, !!playAsWhite);
},
getChains: (ctx) => (_boardState) => {
const State = validateBoardState(error(ctx), _boardState);
@@ -98,22 +99,27 @@ export function NetscriptGo(): InternalAPI<NSGo> {
getStats: () => () => {
return getStats();
},
resetStats:
() =>
(resetAll = false) => {
resetStats(!!resetAll);
},
},
cheat: {
getCheatSuccessChance:
(ctx: NetscriptContext) =>
(_cheatCount = Go.currentGame.cheatCount) => {
checkCheatApiAccess(error(ctx));
const cheatCount = helpers.number(ctx, "cheatCount", _cheatCount);
return cheatSuccessChance(cheatCount);
},
getCheatCount: (ctx: NetscriptContext) => () => {
getCheatSuccessChance: (ctx: NetscriptContext) => (_cheatCount, playAsWhite) => {
checkCheatApiAccess(error(ctx));
return Go.currentGame.cheatCount;
const normalizedCheatCount =
_cheatCount ?? (playAsWhite ? Go.currentGame.cheatCountForWhite : Go.currentGame.cheatCount);
const cheatCount = helpers.number(ctx, "cheatCount", normalizedCheatCount);
return cheatSuccessChance(cheatCount, !!playAsWhite);
},
getCheatCount: (ctx: NetscriptContext) => (playAsWhite) => {
checkCheatApiAccess(error(ctx));
return playAsWhite ? Go.currentGame.cheatCountForWhite : Go.currentGame.cheatCount;
},
removeRouter:
(ctx: NetscriptContext) =>
(_x, _y): Promise<Play> => {
(_x, _y, playAsWhite): Promise<Play> => {
checkCheatApiAccess(error(ctx));
const x = helpers.number(ctx, "x", _x);
const y = helpers.number(ctx, "y", _y);
@@ -122,32 +128,35 @@ export function NetscriptGo(): InternalAPI<NSGo> {
requireNonEmptyNode: true,
repeat: false,
suicide: false,
playAsWhite: playAsWhite,
});
return cheatRemoveRouter(logger(ctx), x, y);
return cheatRemoveRouter(logger(ctx), error(ctx), x, y, undefined, undefined, !!playAsWhite);
},
playTwoMoves:
(ctx: NetscriptContext) =>
(_x1, _y1, _x2, _y2): Promise<Play> => {
(_x1, _y1, _x2, _y2, playAsWhite): Promise<Play> => {
checkCheatApiAccess(error(ctx));
const x1 = helpers.number(ctx, "x", _x1);
const y1 = helpers.number(ctx, "y", _y1);
validateMove(error(ctx), x1, y1, "playTwoMoves", {
repeat: false,
suicide: false,
playAsWhite,
});
const x2 = helpers.number(ctx, "x", _x2);
const y2 = helpers.number(ctx, "y", _y2);
validateMove(error(ctx), x2, y2, "playTwoMoves", {
repeat: false,
suicide: false,
playAsWhite,
});
return cheatPlayTwoMoves(logger(ctx), x1, y1, x2, y2);
return cheatPlayTwoMoves(logger(ctx), error(ctx), x1, y1, x2, y2, undefined, undefined, !!playAsWhite);
},
repairOfflineNode:
(ctx: NetscriptContext) =>
(_x, _y): Promise<Play> => {
(_x, _y, playAsWhite): Promise<Play> => {
checkCheatApiAccess(error(ctx));
const x = helpers.number(ctx, "x", _x);
const y = helpers.number(ctx, "y", _y);
@@ -157,13 +166,14 @@ export function NetscriptGo(): InternalAPI<NSGo> {
onlineNode: false,
requireOfflineNode: true,
suicide: false,
playAsWhite,
});
return cheatRepairOfflineNode(logger(ctx), x, y);
return cheatRepairOfflineNode(logger(ctx), x, y, undefined, undefined, !!playAsWhite);
},
destroyNode:
(ctx: NetscriptContext) =>
(_x, _y): Promise<Play> => {
(_x, _y, playAsWhite): Promise<Play> => {
checkCheatApiAccess(error(ctx));
const x = helpers.number(ctx, "x", _x);
const y = helpers.number(ctx, "y", _y);
@@ -171,9 +181,10 @@ export function NetscriptGo(): InternalAPI<NSGo> {
repeat: false,
onlineNode: true,
suicide: false,
playAsWhite,
});
return cheatDestroyNode(logger(ctx), x, y);
return cheatDestroyNode(logger(ctx), x, y, undefined, undefined, !!playAsWhite);
},
},
};