-
Notifications
You must be signed in to change notification settings - Fork 2
/
endGame.js
62 lines (54 loc) · 2.35 KB
/
endGame.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
/**
Copyright 2014 Andrew P. Sillers
This file is part of Lords of the Fey.
Lords of the Fey is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Lords of the Fey is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with Lords of the Fey. If not, see <http://www.gnu.org/licenses/>.
*/
exports.checkForVictory = function checkForVictory(game, collections, callback) {
var result = { victory: false };
var teamsWithCommanders = {};
collections.units.find({ gameId: game._id, isCommander: true }, function(err, cursor) {
cursor.toArray(function (err, commanderList) {
commanderList.forEach(function(commander) {
teamsWithCommanders[commander.team] = true;
});
var winningAlliance = null;
var survivingTeamList = Object.getOwnPropertyNames(teamsWithCommanders);
console.log(survivingTeamList);
for(var i=0; i < survivingTeamList.length; ++i) {
var team = survivingTeamList[i];
var thisAlliance = game.players[team-1].alliance;
if(winningAlliance == null) { winningAlliance = thisAlliance }
if(thisAlliance != winningAlliance) {
winningAlliance = null;
break;
}
}
if(winningAlliance !== null) {
result.victory = true;
result.alliance = winningAlliance;
}
callback(result);
});
});
}
exports.concludeGame = function(result, game, collections, callback) {
game.over = true;
game.winner = result.alliance;
collections.units.find({ gameId: game._id.toString() }, function(unitCursor) {
unitCursor.next(function stripGameId(u) {
if(!u) { collections.units.save(unitCursor); }
else {
delete u.gameId;
}
});
});
}