-
Notifications
You must be signed in to change notification settings - Fork 1
/
simulate.js
executable file
·116 lines (96 loc) · 3.23 KB
/
simulate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/env nodejs
var path = require('path');
var sinon = require('sinon');
var constants = require('./public/js/constants.js');
var Player = require('./player.js');
var Game = require('./game.js');
if (process.argv.length < 4) {
console.log(`Usage: ${process.argv[0]} ${process.argv[1]} <bot 1 script> <bot 2 script>`);
process.exit();
}
var Bot1 = require(path.resolve(process.argv[2]));
var Bot2 = require(path.resolve(process.argv[3]));
constants.game.BOT_DELAY = 0;
var iters = 10000;
var botOneWins = 0;
var botTwoWins = 0;
var count = 0;
var turns = 0;
var start;
function done() {
console.log();
console.log('=== Simulation Finished ===');
console.log(`Total Games: ${iters.toLocaleString()}`);
console.log(`Bot 1 Wins: ${botOneWins.toLocaleString()} (${(botOneWins/iters*100).toFixed(2)}%)`);
console.log(`Bot 2 Wins: ${botTwoWins.toLocaleString()} (${(botTwoWins/iters*100).toFixed(2)}%)`);
console.log(`Average Turns per Game: ${(turns/iters).toFixed(2)}`);
var end = Date.now();
console.log(`Time Taken: ${((end-start)/1000).toFixed(2)} sec`);
Game.prototype.end.restore();
Game.prototype.switchTurns.restore();
}
var originalEnd = Game.prototype.end;
sinon.stub(Game.prototype, 'end').callsFake(function(winner) {
if (winner.isBotOne) {
botOneWins++;
}
else {
botTwoWins++;
}
count++;
if (count % (iters / 10) == 0) {
console.log(`${count.toLocaleString()} games done!`);
}
if (count >= iters) {
done();
}
turns += winner.game.turnCounter;
originalEnd.call(winner.game, winner);
});
sinon.stub(Bot1.prototype, 'handleError').callsFake(function(msg) {
throw new Error(msg);
});
if (Bot1 !== Bot2) {
sinon.stub(Bot2.prototype, 'handleError').callsFake(function(msg) {
throw new Error(msg);
});
}
var originalSwitchTurns = Game.prototype.switchTurns;
sinon.stub(Game.prototype, 'switchTurns').callsFake(function(playerId) {
const game = Player.get(playerId).game;
if (game.turnCounter >= 100) {
console.log('Warning: Game has gone on for more than 100 turns!');
[[1, game.p1], [2, game.p2]].forEach(function(val) {
const num = val[0];
const plr = val[1];
console.log();
console.log(`Bot ${num} Deck: [${plr.getDeck()}]`);
console.log(`Bot ${num} Remaining Deck: [${plr.deck}] -> [${plr.deck.map((x) => constants.cards[x].name)}]`);
console.log(`Bot ${num} Health: ${plr.health}, Bot 1 Hand: [${plr.hand}] -> [${plr.hand.map((x) => constants.cards[x].name)}]`);
console.log(`Bot ${num} Minions: [${plr.minions.map((x) => x.name)}]`);
});
console.log();
console.log('Terminating game...');
originalEnd.call(game, Player.get(playerId));
count++;
if (count >= iters) {
done();
}
}
else {
originalSwitchTurns.call(game, playerId);
}
});
function playGame() {
var bot1 = new Bot1();
bot1.isBotOne = true;
var bot2 = new Bot2();
bot2.isBotOne = false;
var game = new Game(bot1, bot2);
game.init();
}
console.log("=== Simulation Started ===");
start = Date.now();
for (var i = 0; i < iters; i++) {
playGame();
}