Skip to content

Commit b0477f5

Browse files
committed
Merge pull request JavascriptBattle#110 from gjtrowbridge/master
feature(local, plan-games): Fix local run, add random map support
2 parents 1a7ad2c + e7abb40 commit b0477f5

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

game_logic/plan-all-games.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
11
var createGameFromMap = require('./create-game-from-map.js');
22
var secrets = require('../secrets.js');
33

4+
//Helper function for generating random indices
5+
var randomIndex = function(maxExcl) {
6+
return Math.floor(Math.random(Date.now()) * maxExcl);
7+
};
8+
9+
var pickMap = function(map) {
10+
if (Array.isArray(map)) {
11+
return map[randomIndex(map.length)];
12+
} else {
13+
return map;
14+
}
15+
};
16+
417
//Synchronous, returns an array of all games that need
518
//to be run and a lookup for finding user info
619
var planAllGames = function(users) {
@@ -10,11 +23,6 @@ var planAllGames = function(users) {
1023
//Makes it so the passed-in users array is not mutated
1124
var users = users.slice()
1225

13-
//Helper function for generating random indices
14-
var randomIndex = function(maxExcl) {
15-
return Math.floor(Math.random(Date.now()) * maxExcl);
16-
};
17-
1826
//Set up game
1927
var boardSize = 12;
2028

@@ -34,8 +42,9 @@ var planAllGames = function(users) {
3442

3543
//Create games
3644
for (var gameIndex=0; gameIndex<numberOfGames; gameIndex++) {
45+
var map = pickMap(secrets.map);
3746
var game = createGameFromMap(secrets.rootDirectory +
38-
'/game_logic/maps/' + secrets.map + '.txt');
47+
'/game_logic/maps/' + map + '.txt');
3948
game.maxTurn = 1250;
4049
games.push(game);
4150

local-run/run-fake-games-local.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ var runGame = function() {
7373
game.date = date;
7474

7575
//Manually set the ID so Mongo doesn't just keep writing to the same document
76-
game._id = '0' + '|' + game.turn + '|' + date;
76+
// game._id = '0' + '|' + game.turn + '|' + date;
77+
game.baseId = '0|' + game.date;
78+
game._id = game.baseId + '|' + game.turn.toString();
7779

7880
//Open up the database connection
7981
var openDatabasePromise = openGameDatabase();
@@ -119,7 +121,7 @@ var runGame = function() {
119121
game.handleHeroTurn(direction);
120122

121123
//Manually set the ID so Mongo doesn't just keep writing to the same document
122-
game._id = '0' + '|' + game.turn + '|' + game.date;
124+
game._id = game.baseId + '|' + game.turn.toString();
123125

124126
return resolveGameAndSaveTurnsToDB(game);
125127
}

0 commit comments

Comments
 (0)