-
Notifications
You must be signed in to change notification settings - Fork 2
/
game_helpers.js
146 lines (122 loc) · 3.84 KB
/
game_helpers.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
var cons = require('./server_constants');
var playerHasStruct = function( player, planetid, objecttype, game ) {
var structExists = false;
switch (objecttype) {
case cons.OBJ_MINE:
case cons.OBJ_FACTORY:
case cons.OBJ_EMBASSY:
return playerHasResourceStruct( player, planetid, objecttype, game );
case cons.OBJ_BASE:
var base = game.board.planets[planetid].base;
return ( base && base.player == player );
default:
return false;
}
return false;
};
/**
* addGameMessage appends a new message object to a gameInfo object
* @msg is either a string or an action object
*/
var addGameMessage = function(gameInfo, userid, msg) {
var newMsg = {
id: userid, // when userid is MSG_ACTION...
message: msg // msg is an action, client will show in english
};
gameInfo.messages.push(newMsg);
return newMsg;
};
/**
* addResourcePackage appends a new resourcePackage object to the game
* resourcePackages object for the corect player, defaults collected status
* to false. Expects array of all resource types with numbers to add
*/
var addResourcePackage = function(game, player, pkgtype, resources, message) {
var isnew = true;
var agent_pkgs = [cons.PKG_ENVOY, cons.PKG_MINER, cons.PKG_SPY, cons.PKG_SMUGGLER, cons.PKG_COLLECT];
if ( agent_pkgs.indexOf(pkgtype) != -1 ){
isnew = false; // set this so ui will draw the package (don't auto-collect)
}
var resourcePackage = {
pkgtype: pkgtype,
collected: false,
resources: resources,
message: message,
isnew: isnew
};
// automatically set collected to true for build packages. This is because
// build packages are only added for the animation. Resources are deducted
// when the build action is taken
if ( pkgtype == cons.PKG_BUILD ){
resourcePackage.collected = true;
}
game.resourcePackages[player].push( resourcePackage );
};
var addGameActionMessage = function(gameInfo, userid, action) {
var newMsg = addGameMessage( gameInfo,
cons.MSG_ACTION,
action );
return newMsg;
};
var addLobbyMessage = function(messages, userid, msg) {
var newMsg = {
id: userid, // -1 indicates a server message
message: msg
};
messages.push(newMsg);
return newMsg;
};
var playerHasResourceStruct = function( player, planetid, objecttype, game ) {
var planet = game.board.planets[planetid];
for ( var i = 0; i < planet.resources.length; i++ ){
var struct = planet.resources[i].structure;
if ( struct && struct.player == player && struct.kind == objecttype ) {
return true;
}
}
return false;
};
var clearPhaseDone = function( game ){
for (var i = 0; i < game.phaseDone.length; i++) {
game.phaseDone[i] = false;
}
};
// TODO: combine this, initializeViewed and several others
// into one generic function like 'initPlayerLengthList()'
var initializeSpyActions = function( game ) {
var spyActions = [];
for ( var i = 0; i < game.players.length; i++ ){
spyActions.push(cons.SPY_ACT_NULL);
}
return spyActions;
};
var initializeViewed = function( game ) {
var viewed = [];
for ( var i = 0; i < game.players.length; i++ ){
viewed.push(false);
}
return viewed;
};
var findGameToReconnect = function(u, gamesInfo){
for ( var i = 0; i < gamesInfo.length; i++ ){
gameInfo = gamesInfo[i];
if ( gameInfo.ready.indexOf(u) != -1 &&
gameInfo.status == cons.GAME_PROGRESS ) {
return i;
}
}
return -1;
};
(function() {
module.exports = {
playerHasStruct: playerHasStruct,
addGameMessage: addGameMessage,
addGameActionMessage: addGameActionMessage,
addResourcePackage: addResourcePackage,
addLobbyMessage: addLobbyMessage,
clearPhaseDone: clearPhaseDone,
initializeSpyActions: initializeSpyActions,
initializeViewed: initializeViewed,
findGameToReconnect: findGameToReconnect,
}
}());