mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2026-04-30 12:57:06 +02:00
IPVGO: Consistently return error() calls to make sure it is obvious the function halts at that point (#2335)
This commit is contained in:
committed by
GitHub
parent
370424af2d
commit
0d1f5f3eeb
@@ -37,13 +37,6 @@ import {
|
||||
import { getEnumHelper } from "../utils/EnumHelper";
|
||||
import { errorMessage } from "../Netscript/ErrorMessages";
|
||||
|
||||
const logger = (ctx: NetscriptContext) => (message: string) => helpers.log(ctx, () => message);
|
||||
const error =
|
||||
(ctx: NetscriptContext) =>
|
||||
(message: string): never => {
|
||||
throw errorMessage(ctx, message);
|
||||
};
|
||||
|
||||
/**
|
||||
* Go API implementation
|
||||
*/
|
||||
@@ -55,20 +48,20 @@ export function NetscriptGo(): InternalAPI<NSGo> {
|
||||
const x = helpers.number(ctx, "x", _x);
|
||||
const y = helpers.number(ctx, "y", _y);
|
||||
const playAsWhite = helpers.boolean(ctx, "playAsWhite", _playAsWhite ?? false);
|
||||
validateMove(error(ctx), x, y, "makeMove", { playAsWhite });
|
||||
return makePlayerMove(logger(ctx), error(ctx), x, y, playAsWhite);
|
||||
validateMove(ctx, x, y, "makeMove", { playAsWhite });
|
||||
return makePlayerMove(ctx, x, y, playAsWhite);
|
||||
},
|
||||
passTurn:
|
||||
(ctx: NetscriptContext) =>
|
||||
(_playAsWhite): Promise<Play> => {
|
||||
const playAsWhite = helpers.boolean(ctx, "playAsWhite", _playAsWhite ?? false);
|
||||
validateMove(error(ctx), -1, -1, "passTurn", { playAsWhite, pass: true });
|
||||
return handlePassTurn(logger(ctx), playAsWhite);
|
||||
validateMove(ctx, -1, -1, "passTurn", { playAsWhite, pass: true });
|
||||
return handlePassTurn(ctx, playAsWhite);
|
||||
},
|
||||
opponentNextTurn: (ctx: NetscriptContext) => async (_logOpponentMove, _playAsWhite) => {
|
||||
const logOpponentMove = helpers.boolean(ctx, "logOpponentMove", _logOpponentMove ?? false);
|
||||
const playAsWhite = helpers.boolean(ctx, "playAsWhite", _playAsWhite ?? false);
|
||||
return getOpponentNextMove(logger(ctx), logOpponentMove, playAsWhite);
|
||||
return getOpponentNextMove(ctx, logOpponentMove, playAsWhite);
|
||||
},
|
||||
getBoardState: () => () => {
|
||||
return simpleBoardFromBoard(Go.currentGame.board);
|
||||
@@ -89,7 +82,7 @@ export function NetscriptGo(): InternalAPI<NSGo> {
|
||||
const opponent = getEnumHelper("GoOpponent").nsGetMember(ctx, _opponent);
|
||||
const boardSize = helpers.number(ctx, "boardSize", _boardSize);
|
||||
|
||||
return resetBoardState(logger(ctx), error(ctx), opponent, boardSize);
|
||||
return resetBoardState(ctx, opponent, boardSize);
|
||||
},
|
||||
analysis: {
|
||||
getValidMoves: (ctx) => (_boardState, _priorBoardState, _playAsWhite) => {
|
||||
@@ -97,19 +90,19 @@ export function NetscriptGo(): InternalAPI<NSGo> {
|
||||
return getValidMoves(undefined, true);
|
||||
}
|
||||
const playAsWhite = helpers.boolean(ctx, "playAsWhite", _playAsWhite ?? false);
|
||||
const State = validateBoardState(error(ctx), _boardState, _priorBoardState, playAsWhite);
|
||||
const State = validateBoardState(ctx, _boardState, _priorBoardState, playAsWhite);
|
||||
return getValidMoves(State, playAsWhite);
|
||||
},
|
||||
getChains: (ctx) => (_boardState) => {
|
||||
const State = validateBoardState(error(ctx), _boardState);
|
||||
const State = validateBoardState(ctx, _boardState);
|
||||
return getChains(State?.board);
|
||||
},
|
||||
getLiberties: (ctx) => (_boardState) => {
|
||||
const State = validateBoardState(error(ctx), _boardState);
|
||||
const State = validateBoardState(ctx, _boardState);
|
||||
return getLiberties(State?.board);
|
||||
},
|
||||
getControlledEmptyNodes: (ctx) => (_boardState) => {
|
||||
const State = validateBoardState(error(ctx), _boardState);
|
||||
const State = validateBoardState(ctx, _boardState);
|
||||
return getControlledEmptyNodes(State?.board);
|
||||
},
|
||||
getStats: () => () => {
|
||||
@@ -122,13 +115,12 @@ export function NetscriptGo(): InternalAPI<NSGo> {
|
||||
resetStats(resetAll);
|
||||
},
|
||||
setTestingBoardState: (ctx) => (_boardState, _komi) => {
|
||||
const State = validateBoardState(error(ctx), _boardState);
|
||||
const State = validateBoardState(ctx, _boardState);
|
||||
if (!State) {
|
||||
error(ctx)("Invalid board state passed to setTestingBoardState()");
|
||||
return;
|
||||
throw errorMessage(ctx, "Invalid board state passed to setTestingBoardState()");
|
||||
}
|
||||
const komi: number | undefined = _komi !== undefined ? helpers.number(ctx, "komi", _komi) : undefined;
|
||||
return setTestingBoardState(State.board, komi);
|
||||
return setTestingBoardState(ctx, State.board, komi);
|
||||
},
|
||||
highlightPoint: (ctx) => (_x, _y, _color, _text) => {
|
||||
const x = helpers.number(ctx, "x", _x);
|
||||
@@ -146,7 +138,7 @@ export function NetscriptGo(): InternalAPI<NSGo> {
|
||||
},
|
||||
cheat: {
|
||||
getCheatSuccessChance: (ctx: NetscriptContext) => (_cheatCount, _playAsWhite) => {
|
||||
checkCheatApiAccess(error(ctx));
|
||||
checkCheatApiAccess(ctx);
|
||||
const playAsWhite = helpers.boolean(ctx, "playAsWhite", _playAsWhite ?? false);
|
||||
const normalizedCheatCount =
|
||||
_cheatCount ?? (playAsWhite ? Go.currentGame.cheatCountForWhite : Go.currentGame.cheatCount);
|
||||
@@ -154,18 +146,18 @@ export function NetscriptGo(): InternalAPI<NSGo> {
|
||||
return cheatSuccessChance(cheatCount, playAsWhite);
|
||||
},
|
||||
getCheatCount: (ctx: NetscriptContext) => (_playAsWhite) => {
|
||||
checkCheatApiAccess(error(ctx));
|
||||
checkCheatApiAccess(ctx);
|
||||
const playAsWhite = helpers.boolean(ctx, "playAsWhite", _playAsWhite ?? false);
|
||||
return playAsWhite ? Go.currentGame.cheatCountForWhite : Go.currentGame.cheatCount;
|
||||
},
|
||||
removeRouter:
|
||||
(ctx: NetscriptContext) =>
|
||||
(_x, _y, _playAsWhite): Promise<Play> => {
|
||||
checkCheatApiAccess(error(ctx));
|
||||
checkCheatApiAccess(ctx);
|
||||
const x = helpers.number(ctx, "x", _x);
|
||||
const y = helpers.number(ctx, "y", _y);
|
||||
const playAsWhite = helpers.boolean(ctx, "playAsWhite", _playAsWhite ?? false);
|
||||
validateMove(error(ctx), x, y, "removeRouter", {
|
||||
validateMove(ctx, x, y, "removeRouter", {
|
||||
emptyNode: false,
|
||||
requireNonEmptyNode: true,
|
||||
repeat: false,
|
||||
@@ -173,38 +165,38 @@ export function NetscriptGo(): InternalAPI<NSGo> {
|
||||
playAsWhite: playAsWhite,
|
||||
});
|
||||
|
||||
return cheatRemoveRouter(logger(ctx), error(ctx), x, y, undefined, undefined, playAsWhite);
|
||||
return cheatRemoveRouter(ctx, x, y, undefined, undefined, playAsWhite);
|
||||
},
|
||||
playTwoMoves:
|
||||
(ctx: NetscriptContext) =>
|
||||
(_x1, _y1, _x2, _y2, _playAsWhite): Promise<Play> => {
|
||||
checkCheatApiAccess(error(ctx));
|
||||
checkCheatApiAccess(ctx);
|
||||
const x1 = helpers.number(ctx, "x", _x1);
|
||||
const y1 = helpers.number(ctx, "y", _y1);
|
||||
const playAsWhite = helpers.boolean(ctx, "playAsWhite", _playAsWhite ?? false);
|
||||
validateMove(error(ctx), x1, y1, "playTwoMoves", {
|
||||
validateMove(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", {
|
||||
validateMove(ctx, x2, y2, "playTwoMoves", {
|
||||
repeat: false,
|
||||
suicide: false,
|
||||
playAsWhite,
|
||||
});
|
||||
|
||||
return cheatPlayTwoMoves(logger(ctx), error(ctx), x1, y1, x2, y2, undefined, undefined, playAsWhite);
|
||||
return cheatPlayTwoMoves(ctx, x1, y1, x2, y2, undefined, undefined, playAsWhite);
|
||||
},
|
||||
repairOfflineNode:
|
||||
(ctx: NetscriptContext) =>
|
||||
(_x, _y, _playAsWhite): Promise<Play> => {
|
||||
checkCheatApiAccess(error(ctx));
|
||||
checkCheatApiAccess(ctx);
|
||||
const x = helpers.number(ctx, "x", _x);
|
||||
const y = helpers.number(ctx, "y", _y);
|
||||
const playAsWhite = helpers.boolean(ctx, "playAsWhite", _playAsWhite ?? false);
|
||||
validateMove(error(ctx), x, y, "repairOfflineNode", {
|
||||
validateMove(ctx, x, y, "repairOfflineNode", {
|
||||
emptyNode: false,
|
||||
repeat: false,
|
||||
onlineNode: false,
|
||||
@@ -213,23 +205,23 @@ export function NetscriptGo(): InternalAPI<NSGo> {
|
||||
playAsWhite,
|
||||
});
|
||||
|
||||
return cheatRepairOfflineNode(logger(ctx), x, y, undefined, undefined, playAsWhite);
|
||||
return cheatRepairOfflineNode(ctx, x, y, undefined, undefined, playAsWhite);
|
||||
},
|
||||
destroyNode:
|
||||
(ctx: NetscriptContext) =>
|
||||
(_x, _y, _playAsWhite): Promise<Play> => {
|
||||
checkCheatApiAccess(error(ctx));
|
||||
checkCheatApiAccess(ctx);
|
||||
const x = helpers.number(ctx, "x", _x);
|
||||
const y = helpers.number(ctx, "y", _y);
|
||||
const playAsWhite = helpers.boolean(ctx, "playAsWhite", _playAsWhite ?? false);
|
||||
validateMove(error(ctx), x, y, "destroyNode", {
|
||||
validateMove(ctx, x, y, "destroyNode", {
|
||||
repeat: false,
|
||||
onlineNode: true,
|
||||
suicide: false,
|
||||
playAsWhite,
|
||||
});
|
||||
|
||||
return cheatDestroyNode(logger(ctx), x, y, undefined, undefined, playAsWhite);
|
||||
return cheatDestroyNode(ctx, x, y, undefined, undefined, playAsWhite);
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user