mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2026-04-20 16:22:56 +02:00
GO: Various changes before 2.6.0 (#1120)
This commit is contained in:
@@ -1,4 +1,7 @@
|
||||
import { getBoardFromSimplifiedBoardState, getSimplifiedBoardState } from "../../../src/Go/boardAnalysis/boardAnalysis";
|
||||
import { setPlayer } from "@player";
|
||||
import { GoOpponent, GoColor, GoPlayType } from "@enums";
|
||||
import { Go } from "../../../src/Go/Go";
|
||||
import { boardStateFromSimpleBoard, simpleBoardFromBoard } from "../../../src/Go/boardAnalysis/boardAnalysis";
|
||||
import {
|
||||
cheatPlayTwoMoves,
|
||||
cheatRemoveRouter,
|
||||
@@ -12,10 +15,8 @@ import {
|
||||
makePlayerMove,
|
||||
resetBoardState,
|
||||
} from "../../../src/Go/effects/netscriptGoImplementation";
|
||||
import { Player, setPlayer } from "@player";
|
||||
import { PlayerObject } from "../../../src/PersonObjects/Player/PlayerObject";
|
||||
import "../../../src/Faction/Factions";
|
||||
import { opponents, playerColors, playTypes } from "../../../src/Go/boardState/goConstants";
|
||||
import { getNewBoardState } from "../../../src/Go/boardState/boardState";
|
||||
|
||||
jest.mock("../../../src/Faction/Factions", () => ({
|
||||
@@ -28,7 +29,7 @@ describe("Netscript Go API unit tests", () => {
|
||||
describe("makeMove() tests", () => {
|
||||
it("should handle invalid moves", async () => {
|
||||
const board = ["XOO..", ".....", ".....", ".....", "....."];
|
||||
Player.go.boardState = getBoardFromSimplifiedBoardState(board, opponents.Daedalus, playerColors.white);
|
||||
Go.currentGame = boardStateFromSimpleBoard(board, GoOpponent.Daedalus, GoColor.white);
|
||||
const mockLogger = jest.fn();
|
||||
|
||||
const result = await makePlayerMove(mockLogger, 0, 0);
|
||||
@@ -39,35 +40,36 @@ describe("Netscript Go API unit tests", () => {
|
||||
|
||||
it("should update the board with valid player moves", async () => {
|
||||
const board = ["OXX..", ".....", ".....", ".....", "....."];
|
||||
Player.go.boardState = getBoardFromSimplifiedBoardState(board, opponents.Daedalus, playerColors.white);
|
||||
const boardState = boardStateFromSimpleBoard(board, GoOpponent.Daedalus, GoColor.white);
|
||||
Go.currentGame = boardState;
|
||||
const mockLogger = jest.fn();
|
||||
|
||||
const result = await makePlayerMove(mockLogger, 1, 0);
|
||||
|
||||
expect(result.success).toEqual(true);
|
||||
expect(mockLogger).toHaveBeenCalledWith("Go move played: 1, 0");
|
||||
expect(Player.go.boardState.board[1]?.[0]?.player).toEqual(playerColors.black);
|
||||
expect(Player.go.boardState.board[0]?.[0]?.player).toEqual(playerColors.empty);
|
||||
expect(boardState.board[1]?.[0]?.color).toEqual(GoColor.black);
|
||||
expect(boardState.board[0]?.[0]?.color).toEqual(GoColor.empty);
|
||||
});
|
||||
});
|
||||
describe("passTurn() tests", () => {
|
||||
it("should handle pass attempts", async () => {
|
||||
Player.go.boardState = getNewBoardState(7);
|
||||
Go.currentGame = getNewBoardState(7);
|
||||
const mockLogger = jest.fn();
|
||||
|
||||
const result = await handlePassTurn(mockLogger);
|
||||
|
||||
expect(result.success).toEqual(true);
|
||||
expect(result.type).toEqual(playTypes.move);
|
||||
expect(result.type).toEqual(GoPlayType.move);
|
||||
});
|
||||
});
|
||||
|
||||
describe("getBoardState() tests", () => {
|
||||
it("should correctly return a string version of the bard state", () => {
|
||||
const board = ["OXX..", ".....", ".....", ".....", "..###"];
|
||||
const boardState = getBoardFromSimplifiedBoardState(board);
|
||||
const boardState = boardStateFromSimpleBoard(board);
|
||||
|
||||
const result = getSimplifiedBoardState(boardState.board);
|
||||
const result = simpleBoardFromBoard(boardState.board);
|
||||
|
||||
expect(result).toEqual(board);
|
||||
});
|
||||
@@ -76,18 +78,19 @@ describe("Netscript Go API unit tests", () => {
|
||||
describe("resetBoardState() tests", () => {
|
||||
it("should set the player's board to the requested size and opponent", () => {
|
||||
const board = ["OXX..", ".....", ".....", ".....", "..###"];
|
||||
Player.go.boardState = getBoardFromSimplifiedBoardState(board);
|
||||
Go.currentGame = boardStateFromSimpleBoard(board);
|
||||
const mockError = jest.fn();
|
||||
|
||||
const newBoard = resetBoardState(mockError, opponents.SlumSnakes, 9);
|
||||
const newBoard = resetBoardState(mockError, GoOpponent.SlumSnakes, 9);
|
||||
|
||||
expect(newBoard?.[0].length).toEqual(9);
|
||||
expect(Player.go.boardState.board.length).toEqual(9);
|
||||
expect(Player.go.boardState.ai).toEqual(opponents.SlumSnakes);
|
||||
expect(Go.currentGame.board.length).toEqual(9);
|
||||
expect(Go.currentGame.ai).toEqual(GoOpponent.SlumSnakes);
|
||||
});
|
||||
/* This typechecking is now done prior to calling resetBoardState (it's checked in the ns function via getEnumHelper("GoOpponent".nsGetMember()))
|
||||
it("should throw an error if an invalid opponent is requested", () => {
|
||||
const board = ["OXX..", ".....", ".....", ".....", "..###"];
|
||||
Player.go.boardState = getBoardFromSimplifiedBoardState(board);
|
||||
Go.boardState = getBoardFromSimplifiedBoardState(board);
|
||||
const mockError = jest.fn();
|
||||
|
||||
resetBoardState(mockError, "fake opponent", 9);
|
||||
@@ -96,13 +99,13 @@ describe("Netscript Go API unit tests", () => {
|
||||
"Invalid opponent requested (fake opponent), valid options are Netburners, Slum Snakes, The Black Hand, Tetrads, Daedalus, Illuminati",
|
||||
);
|
||||
});
|
||||
|
||||
*/
|
||||
it("should throw an error if an invalid size is requested", () => {
|
||||
const board = ["OXX..", ".....", ".....", ".....", "..###"];
|
||||
Player.go.boardState = getBoardFromSimplifiedBoardState(board);
|
||||
Go.currentGame = boardStateFromSimpleBoard(board);
|
||||
const mockError = jest.fn();
|
||||
|
||||
resetBoardState(mockError, opponents.TheBlackHand, 31337);
|
||||
resetBoardState(mockError, GoOpponent.TheBlackHand, 31337);
|
||||
|
||||
expect(mockError).toHaveBeenCalledWith("Invalid subnet size requested (31337, size must be 5, 7, 9, or 13");
|
||||
});
|
||||
@@ -111,7 +114,7 @@ describe("Netscript Go API unit tests", () => {
|
||||
describe("getValidMoves() unit tests", () => {
|
||||
it("should return all valid and invalid moves on the board", () => {
|
||||
const board = ["XXO.#", "XO.O.", ".OOOO", "XXXXX", "X.X.X"];
|
||||
Player.go.boardState = getBoardFromSimplifiedBoardState(board, opponents.Daedalus, playerColors.white);
|
||||
Go.currentGame = boardStateFromSimpleBoard(board, GoOpponent.Daedalus, GoColor.white);
|
||||
|
||||
const result = getValidMoves();
|
||||
|
||||
@@ -128,7 +131,7 @@ describe("Netscript Go API unit tests", () => {
|
||||
describe("getChains() unit tests", () => {
|
||||
it("should assign an ID to all contiguous chains on the board", () => {
|
||||
const board = ["XXO.#", "XO.O.", ".OOOO", "XXXXX", "X.X.X"];
|
||||
Player.go.boardState = getBoardFromSimplifiedBoardState(board, opponents.Daedalus, playerColors.white);
|
||||
Go.currentGame = boardStateFromSimpleBoard(board, GoOpponent.Daedalus, GoColor.white);
|
||||
|
||||
const result = getChains();
|
||||
|
||||
@@ -142,7 +145,7 @@ describe("Netscript Go API unit tests", () => {
|
||||
describe("getLiberties() unit tests", () => {
|
||||
it("should display the number of connected empty nodes for each chain on the board", () => {
|
||||
const board = ["XXO.#", "XO.O.", ".OOOO", "XXXXX", "X.X.X"];
|
||||
Player.go.boardState = getBoardFromSimplifiedBoardState(board, opponents.Daedalus, playerColors.white);
|
||||
Go.currentGame = boardStateFromSimpleBoard(board, GoOpponent.Daedalus, GoColor.white);
|
||||
|
||||
const result = getLiberties();
|
||||
|
||||
@@ -158,7 +161,7 @@ describe("Netscript Go API unit tests", () => {
|
||||
describe("getControlledEmptyNodes() unit tests", () => {
|
||||
it("should show the owner of each empty node, if a single player has fully encircled it", () => {
|
||||
const board = ["XXO.#", "XO.O.", ".OOOO", "XXXXX", "X.X.X"];
|
||||
Player.go.boardState = getBoardFromSimplifiedBoardState(board, opponents.Daedalus, playerColors.white);
|
||||
Go.currentGame = boardStateFromSimpleBoard(board, GoOpponent.Daedalus, GoColor.white);
|
||||
|
||||
const result = getControlledEmptyNodes();
|
||||
|
||||
@@ -168,7 +171,7 @@ describe("Netscript Go API unit tests", () => {
|
||||
describe("cheatPlayTwoMoves() tests", () => {
|
||||
it("should handle invalid moves", async () => {
|
||||
const board = ["XOO..", ".....", ".....", ".....", "....."];
|
||||
Player.go.boardState = getBoardFromSimplifiedBoardState(board, opponents.Daedalus, playerColors.white);
|
||||
Go.currentGame = boardStateFromSimpleBoard(board, GoOpponent.Daedalus, GoColor.white);
|
||||
const mockLogger = jest.fn();
|
||||
|
||||
const result = await cheatPlayTwoMoves(mockLogger, 0, 0, 1, 0, 0, 0);
|
||||
@@ -179,48 +182,48 @@ describe("Netscript Go API unit tests", () => {
|
||||
|
||||
it("should update the board with both player moves if nodes are unoccupied and cheat is successful", async () => {
|
||||
const board = ["OXX..", ".....", ".....", ".....", "....O"];
|
||||
Player.go.boardState = getBoardFromSimplifiedBoardState(board, opponents.Daedalus, playerColors.white);
|
||||
Go.currentGame = boardStateFromSimpleBoard(board, GoOpponent.Daedalus, GoColor.white);
|
||||
const mockLogger = jest.fn();
|
||||
|
||||
const result = 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(Player.go.boardState.board[4]?.[3]?.player).toEqual(playerColors.black);
|
||||
expect(Player.go.boardState.board[3]?.[4]?.player).toEqual(playerColors.black);
|
||||
expect(Player.go.boardState.board[4]?.[4]?.player).toEqual(playerColors.empty);
|
||||
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);
|
||||
});
|
||||
|
||||
it("should pass player turn to AI if the cheat is unsuccessful but player is not ejected", async () => {
|
||||
const board = ["OXX..", ".....", ".....", ".....", "....O"];
|
||||
Player.go.boardState = getBoardFromSimplifiedBoardState(board, opponents.Daedalus, playerColors.white);
|
||||
Go.currentGame = boardStateFromSimpleBoard(board, GoOpponent.Daedalus, GoColor.white);
|
||||
const mockLogger = jest.fn();
|
||||
|
||||
const result = await cheatPlayTwoMoves(mockLogger, 4, 3, 3, 4, 2, 1);
|
||||
console.log(result);
|
||||
expect(mockLogger).toHaveBeenCalledWith("Cheat failed. Your turn has been skipped.");
|
||||
expect(result.success).toEqual(false);
|
||||
expect(Player.go.boardState.board[4]?.[3]?.player).toEqual(playerColors.empty);
|
||||
expect(Player.go.boardState.board[3]?.[4]?.player).toEqual(playerColors.empty);
|
||||
expect(Player.go.boardState.board[4]?.[4]?.player).toEqual(playerColors.white);
|
||||
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);
|
||||
});
|
||||
|
||||
it("should reset the board if the cheat is unsuccessful and the player is ejected", async () => {
|
||||
const board = ["OXX..", ".....", ".....", ".....", "....O"];
|
||||
Player.go.boardState = getBoardFromSimplifiedBoardState(board, opponents.Daedalus, playerColors.white);
|
||||
Player.go.boardState.cheatCount = 1;
|
||||
Go.currentGame = boardStateFromSimpleBoard(board, GoOpponent.Daedalus, GoColor.white);
|
||||
Go.currentGame.cheatCount = 1;
|
||||
const mockLogger = jest.fn();
|
||||
|
||||
const result = await cheatPlayTwoMoves(mockLogger, 4, 3, 3, 4, 1, 0);
|
||||
console.log(result);
|
||||
expect(mockLogger).toHaveBeenCalledWith("Cheat failed! You have been ejected from the subnet.");
|
||||
expect(result.success).toEqual(false);
|
||||
expect(Player.go.boardState.history.length).toEqual(0);
|
||||
expect(Go.currentGame.previousBoard).toEqual(null);
|
||||
});
|
||||
});
|
||||
describe("cheatRemoveRouter() tests", () => {
|
||||
it("should handle invalid moves", async () => {
|
||||
const board = ["XOO..", ".....", ".....", ".....", "....."];
|
||||
Player.go.boardState = getBoardFromSimplifiedBoardState(board, opponents.Daedalus, playerColors.white);
|
||||
Go.currentGame = boardStateFromSimpleBoard(board, GoOpponent.Daedalus, GoColor.white);
|
||||
const mockLogger = jest.fn();
|
||||
|
||||
const result = await cheatRemoveRouter(mockLogger, 1, 0, 0, 0);
|
||||
@@ -233,33 +236,33 @@ describe("Netscript Go API unit tests", () => {
|
||||
|
||||
it("should remove the router if the move is valid", async () => {
|
||||
const board = ["XOO..", ".....", ".....", ".....", "....."];
|
||||
Player.go.boardState = getBoardFromSimplifiedBoardState(board, opponents.Daedalus, playerColors.white);
|
||||
Go.currentGame = boardStateFromSimpleBoard(board, GoOpponent.Daedalus, GoColor.white);
|
||||
const mockLogger = jest.fn();
|
||||
|
||||
const result = await cheatRemoveRouter(mockLogger, 0, 0, 0, 0);
|
||||
|
||||
expect(result.success).toEqual(true);
|
||||
expect(mockLogger).toHaveBeenCalledWith("Cheat successful. The point 0,0 was cleared.");
|
||||
expect(Player.go.boardState.board[0][0]?.player).toEqual(playerColors.empty);
|
||||
expect(Go.currentGame.board[0][0]?.color).toEqual(GoColor.empty);
|
||||
});
|
||||
|
||||
it("should reset the board if the cheat is unsuccessful and the player is ejected", async () => {
|
||||
const board = ["OXX..", ".....", ".....", ".....", "....O"];
|
||||
Player.go.boardState = getBoardFromSimplifiedBoardState(board, opponents.Daedalus, playerColors.white);
|
||||
Player.go.boardState.cheatCount = 1;
|
||||
Go.currentGame = boardStateFromSimpleBoard(board, GoOpponent.Daedalus, GoColor.white);
|
||||
Go.currentGame.cheatCount = 1;
|
||||
const mockLogger = jest.fn();
|
||||
|
||||
const result = await cheatRemoveRouter(mockLogger, 0, 0, 1, 0);
|
||||
console.log(result);
|
||||
expect(mockLogger).toHaveBeenCalledWith("Cheat failed! You have been ejected from the subnet.");
|
||||
expect(result.success).toEqual(false);
|
||||
expect(Player.go.boardState.history.length).toEqual(0);
|
||||
expect(Go.currentGame.previousBoard).toEqual(null);
|
||||
});
|
||||
});
|
||||
describe("cheatRepairOfflineNode() tests", () => {
|
||||
it("should handle invalid moves", async () => {
|
||||
const board = ["XOO..", ".....", ".....", ".....", "....#"];
|
||||
Player.go.boardState = getBoardFromSimplifiedBoardState(board, opponents.Daedalus, playerColors.white);
|
||||
Go.currentGame = boardStateFromSimpleBoard(board, GoOpponent.Daedalus, GoColor.white);
|
||||
const mockLogger = jest.fn();
|
||||
|
||||
const result = await cheatRepairOfflineNode(mockLogger, 0, 0);
|
||||
@@ -270,13 +273,13 @@ describe("Netscript Go API unit tests", () => {
|
||||
|
||||
it("should update the board with the repaired node if the cheat is successful", async () => {
|
||||
const board = ["OXX..", ".....", ".....", ".....", "....#"];
|
||||
Player.go.boardState = getBoardFromSimplifiedBoardState(board, opponents.Daedalus, playerColors.white);
|
||||
Go.currentGame = boardStateFromSimpleBoard(board, GoOpponent.Daedalus, GoColor.white);
|
||||
const mockLogger = jest.fn();
|
||||
|
||||
const result = await cheatRepairOfflineNode(mockLogger, 4, 4, 0, 0);
|
||||
expect(mockLogger).toHaveBeenCalledWith("Cheat successful. The point 4,4 was repaired.");
|
||||
expect(result.success).toEqual(true);
|
||||
expect(Player.go.boardState.board[4]?.[4]?.player).toEqual(playerColors.empty);
|
||||
expect(Go.currentGame.board[4]?.[4]?.color).toEqual(GoColor.empty);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user