diff --git a/app/appServer.js b/app/appServer.js index b6bf9fb..376e158 100644 --- a/app/appServer.js +++ b/app/appServer.js @@ -69,7 +69,7 @@ export default class AppServer { this.gameType = "against_bot" this.setupGame = (game) => { - const bot = new Bot(game.rightRobot, () => game.copy(), this.randomSeedString) + const bot = new Bot(game.rightRobot, () => game.copy(true), this.randomSeedString) bot.start() return { @@ -199,8 +199,8 @@ export default class AppServer { this.gameType = "with_two_bots" this.setupGame = (game) => { - const leftBot = new Bot(game.leftRobot, () => game.copy(), `${this.randomSeedString}-left`) - const rightBot = new Bot(game.rightRobot, () => game.copy(), `${this.randomSeedString}-left`) + const leftBot = new Bot(game.leftRobot, () => game.copy(true), `${this.randomSeedString}-left`) + const rightBot = new Bot(game.rightRobot, () => game.copy(true), `${this.randomSeedString}-left`) leftBot.start() rightBot.start() diff --git a/game/game.js b/game/game.js index 866269c..b27bed5 100644 --- a/game/game.js +++ b/game/game.js @@ -51,7 +51,12 @@ export default class Game { this._robotsUpdate() } - copy() { + copy(safe = false) { + const isLeftInput = this.leftRobot.state === ROBOT_STATE_INPUT; + const isRightInput = this.rightRobot.state === ROBOT_STATE_INPUT; + if (safe && !(isLeftInput && isRightInput || !isLeftInput && !isRightInput)) { + throw "Game state is inconsistent, cannot revert current actions and one robot may use that information" + } return Object.assign(Object.create(Game.prototype),{ ...this, eventManager: new EventManager(), @@ -60,8 +65,8 @@ export default class Game { _leftRobotUpdate: () => {}, _rightRobotUpdate: () => {}, _actionPhaseInfoUpdate: () => {}, - leftRobot: this.leftRobot.copy(), - rightRobot: this.rightRobot.copy(), + leftRobot: this.leftRobot.copy(safe), + rightRobot: this.rightRobot.copy(safe), }) } diff --git a/game/robot.js b/game/robot.js index fe11a5d..6f1cfcb 100644 --- a/game/robot.js +++ b/game/robot.js @@ -1,7 +1,7 @@ import Action from "./action.js"; import Bodypart from "./bodypart.js"; import Hand from "./hand.js"; -import {createDeckByTypes} from "./cards.js"; +import {createBlankCard, createDeckByTypes} from "./cards.js"; import RandomGenerator from "../utils/randomGenerator.js"; export const ROBOT_STATE_INPUT = "WAITING_FOR_INPUT" @@ -61,10 +61,10 @@ export default class Robot { this.leftHand = new Hand(robotOptions.leftHandPosition, 1, 7) } - copy() { - return Object.assign(Object.create(Robot.prototype),{ + copy(safe = false) { + const copy = Object.assign(Object.create(Robot.prototype),{ ...this, - _randomGenerator: new RandomGenerator("copy"), + _randomGenerator: Object.create(RandomGenerator.prototype, Object.getOwnPropertyDescriptors(this._randomGenerator)), _robotUpdate: () => {}, actions: this.actions.map(action => Object.create(Action.prototype, Object.getOwnPropertyDescriptors(action))), handCards: [...this.handCards], @@ -76,6 +76,17 @@ export default class Robot { rightHand: Object.create(Hand.prototype, Object.getOwnPropertyDescriptors(this.rightHand)), leftHand: Object.create(Hand.prototype, Object.getOwnPropertyDescriptors(this.leftHand)), }) + + if (safe) { + copy._randomGenerator = new RandomGenerator("copy") + if (copy.state === ROBOT_STATE_INPUT) { + copy.actions = copy.actions.map(() => new Action()) + } + copy.deckCards = copy.deckCards.map(() => createBlankCard()) + copy.discardedCards = copy.discardedCards.map(() => createBlankCard()) + } + + return copy } get robotInfo() {