IPVGO: Add support to netscript API for game state, current player, and alternate ways to check/wait on AI turn (#1142)

This commit is contained in:
Michael Ficocelli
2024-03-19 14:07:15 -04:00
committed by GitHub
parent 6aaeb6b59e
commit d81358c80f
22 changed files with 541 additions and 308 deletions

View File

@@ -8,12 +8,13 @@ import {
cheatRepairOfflineNode,
getChains,
getControlledEmptyNodes,
getGameState,
getLiberties,
getValidMoves,
handlePassTurn,
invalidMoveResponse,
makePlayerMove,
resetBoardState,
validateMove,
} from "../../../src/Go/effects/netscriptGoImplementation";
import { PlayerObject } from "../../../src/PersonObjects/Player/PlayerObject";
import "../../../src/Faction/Factions";
@@ -31,11 +32,13 @@ describe("Netscript Go API unit tests", () => {
const board = ["XOO..", ".....", ".....", ".....", "....."];
Go.currentGame = boardStateFromSimpleBoard(board, GoOpponent.Daedalus, GoColor.white);
const mockLogger = jest.fn();
const mockError = jest.fn(() => {
throw new Error("Invalid");
});
const result = await makePlayerMove(mockLogger, 0, 0);
await makePlayerMove(mockLogger, mockError, 0, 0).catch((_) => _);
expect(result).toEqual(invalidMoveResponse);
expect(mockLogger).toHaveBeenCalledWith("ERROR: Invalid move: That node is already occupied by a piece");
expect(mockError).toHaveBeenCalledWith("Invalid move: 0 0. That node is already occupied by a piece.");
});
it("should update the board with valid player moves", async () => {
@@ -43,10 +46,10 @@ describe("Netscript Go API unit tests", () => {
const boardState = boardStateFromSimpleBoard(board, GoOpponent.Daedalus, GoColor.white);
Go.currentGame = boardState;
const mockLogger = jest.fn();
const mockError = jest.fn();
const result = await makePlayerMove(mockLogger, 1, 0);
await makePlayerMove(mockLogger, mockError, 1, 0);
expect(result.success).toEqual(true);
expect(mockLogger).toHaveBeenCalledWith("Go move played: 1, 0");
expect(boardState.board[1]?.[0]?.color).toEqual(GoColor.black);
expect(boardState.board[0]?.[0]?.color).toEqual(GoColor.empty);
@@ -59,7 +62,6 @@ describe("Netscript Go API unit tests", () => {
const result = await handlePassTurn(mockLogger);
expect(result.success).toEqual(true);
expect(result.type).toEqual(GoPlayType.move);
});
});
@@ -75,6 +77,24 @@ describe("Netscript Go API unit tests", () => {
});
});
describe("getGameState() tests", () => {
it("should correctly retrieve the current game state", async () => {
const board = ["OXX..", ".....", ".....", "...XX", "...X."];
const boardState = boardStateFromSimpleBoard(board, GoOpponent.Daedalus, GoColor.black);
boardState.previousBoard = ["OX..", ".....", ".....", "...XX", "...X."];
Go.currentGame = boardState;
const result = getGameState();
expect(result).toEqual({
currentPlayer: GoColor.white,
whiteScore: 6.5,
blackScore: 6,
previousMove: [0, 2],
});
});
});
describe("resetBoardState() tests", () => {
it("should set the player's board to the requested size and opponent", () => {
const board = ["OXX..", ".....", ".....", ".....", "..###"];
@@ -170,12 +190,14 @@ describe("Netscript Go API unit tests", () => {
it("should handle invalid moves", async () => {
const board = ["XOO..", ".....", ".....", ".....", "....."];
Go.currentGame = boardStateFromSimpleBoard(board, GoOpponent.Daedalus, GoColor.white);
const mockLogger = jest.fn();
const result = await cheatPlayTwoMoves(mockLogger, 0, 0, 1, 0, 0, 0);
expect(result).toEqual(invalidMoveResponse);
expect(mockLogger).toHaveBeenCalledWith("The point 0,0 is not empty, so you cannot place a router there.");
const mockError = jest.fn();
validateMove(mockError, 0, 0, "playTwoMoves", {
repeat: false,
suicide: false,
});
expect(mockError).toHaveBeenCalledWith(
"The point 0,0 is occupied by a router, so you cannot place a router there",
);
});
it("should update the board with both player moves if nodes are unoccupied and cheat is successful", async () => {
@@ -183,9 +205,8 @@ describe("Netscript Go API unit tests", () => {
Go.currentGame = boardStateFromSimpleBoard(board, GoOpponent.Daedalus, GoColor.white);
const mockLogger = jest.fn();
const result = await cheatPlayTwoMoves(mockLogger, 4, 3, 3, 4, 0, 0);
await cheatPlayTwoMoves(mockLogger, 4, 3, 3, 4, 0, 0);
expect(mockLogger).toHaveBeenCalledWith("Cheat successful. Two go moves played: 4,3 and 3,4");
expect(result.success).toEqual(true);
expect(Go.currentGame.board[4]?.[3]?.color).toEqual(GoColor.black);
expect(Go.currentGame.board[3]?.[4]?.color).toEqual(GoColor.black);
expect(Go.currentGame.board[4]?.[4]?.color).toEqual(GoColor.empty);
@@ -196,9 +217,8 @@ describe("Netscript Go API unit tests", () => {
Go.currentGame = boardStateFromSimpleBoard(board, GoOpponent.Daedalus, GoColor.white);
const mockLogger = jest.fn();
const result = await cheatPlayTwoMoves(mockLogger, 4, 3, 3, 4, 2, 1);
await cheatPlayTwoMoves(mockLogger, 4, 3, 3, 4, 2, 1);
expect(mockLogger).toHaveBeenCalledWith("Cheat failed. Your turn has been skipped.");
expect(result.success).toEqual(false);
expect(Go.currentGame.board[4]?.[3]?.color).toEqual(GoColor.empty);
expect(Go.currentGame.board[3]?.[4]?.color).toEqual(GoColor.empty);
expect(Go.currentGame.board[4]?.[4]?.color).toEqual(GoColor.white);
@@ -210,9 +230,8 @@ describe("Netscript Go API unit tests", () => {
Go.currentGame.cheatCount = 1;
const mockLogger = jest.fn();
const result = await cheatPlayTwoMoves(mockLogger, 4, 3, 3, 4, 1, 0);
await cheatPlayTwoMoves(mockLogger, 4, 3, 3, 4, 1, 0);
expect(mockLogger).toHaveBeenCalledWith("Cheat failed! You have been ejected from the subnet.");
expect(result.success).toEqual(false);
expect(Go.currentGame.previousBoard).toEqual(null);
});
});
@@ -220,12 +239,14 @@ describe("Netscript Go API unit tests", () => {
it("should handle invalid moves", async () => {
const board = ["XOO..", ".....", ".....", ".....", "....."];
Go.currentGame = boardStateFromSimpleBoard(board, GoOpponent.Daedalus, GoColor.white);
const mockLogger = jest.fn();
const result = await cheatRemoveRouter(mockLogger, 1, 0, 0, 0);
expect(result).toEqual(invalidMoveResponse);
expect(mockLogger).toHaveBeenCalledWith(
const mockError = jest.fn();
validateMove(mockError, 1, 0, "removeRouter", {
emptyNode: false,
requireNonEmptyNode: true,
repeat: false,
suicide: false,
});
expect(mockError).toHaveBeenCalledWith(
"The point 1,0 does not have a router on it, so you cannot clear this point with removeRouter().",
);
});
@@ -235,9 +256,8 @@ describe("Netscript Go API unit tests", () => {
Go.currentGame = boardStateFromSimpleBoard(board, GoOpponent.Daedalus, GoColor.white);
const mockLogger = jest.fn();
const result = await cheatRemoveRouter(mockLogger, 0, 0, 0, 0);
await cheatRemoveRouter(mockLogger, 0, 0, 0, 0);
expect(result.success).toEqual(true);
expect(mockLogger).toHaveBeenCalledWith("Cheat successful. The point 0,0 was cleared.");
expect(Go.currentGame.board[0][0]?.color).toEqual(GoColor.empty);
});
@@ -248,9 +268,8 @@ describe("Netscript Go API unit tests", () => {
Go.currentGame.cheatCount = 1;
const mockLogger = jest.fn();
const result = await cheatRemoveRouter(mockLogger, 0, 0, 1, 0);
await cheatRemoveRouter(mockLogger, 0, 0, 1, 0);
expect(mockLogger).toHaveBeenCalledWith("Cheat failed! You have been ejected from the subnet.");
expect(result.success).toEqual(false);
expect(Go.currentGame.previousBoard).toEqual(null);
});
});
@@ -258,12 +277,16 @@ describe("Netscript Go API unit tests", () => {
it("should handle invalid moves", async () => {
const board = ["XOO..", ".....", ".....", ".....", "....#"];
Go.currentGame = boardStateFromSimpleBoard(board, GoOpponent.Daedalus, GoColor.white);
const mockLogger = jest.fn();
const mockError = jest.fn();
validateMove(mockError, 0, 0, "repairOfflineNode", {
emptyNode: false,
repeat: false,
onlineNode: false,
requireOfflineNode: true,
suicide: false,
});
const result = await cheatRepairOfflineNode(mockLogger, 0, 0);
expect(result).toEqual(invalidMoveResponse);
expect(mockLogger).toHaveBeenCalledWith("The node 0,0 is not offline, so you cannot repair the node.");
expect(mockError).toHaveBeenCalledWith("The node 0,0 is not offline, so you cannot repair the node.");
});
it("should update the board with the repaired node if the cheat is successful", async () => {
@@ -271,9 +294,8 @@ describe("Netscript Go API unit tests", () => {
Go.currentGame = boardStateFromSimpleBoard(board, GoOpponent.Daedalus, GoColor.white);
const mockLogger = jest.fn();
const result = await cheatRepairOfflineNode(mockLogger, 4, 4, 0, 0);
await cheatRepairOfflineNode(mockLogger, 4, 4, 0, 0);
expect(mockLogger).toHaveBeenCalledWith("Cheat successful. The point 4,4 was repaired.");
expect(result.success).toEqual(true);
expect(Go.currentGame.board[4]?.[4]?.color).toEqual(GoColor.empty);
});
});