Skip to content

Commit

Permalink
Prevent Bot from accessing hidden information
Browse files Browse the repository at this point in the history
  • Loading branch information
PetrHeinz committed Feb 26, 2023
1 parent 43517fd commit 238a032
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
6 changes: 3 additions & 3 deletions app/appServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
Expand Down
11 changes: 8 additions & 3 deletions game/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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),
})
}

Expand Down
19 changes: 15 additions & 4 deletions game/robot.js
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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],
Expand All @@ -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() {
Expand Down

0 comments on commit 238a032

Please sign in to comment.