forked from coridrew/nodejs-against-humanity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.js
More file actions
237 lines (203 loc) · 5.8 KB
/
game.js
File metadata and controls
237 lines (203 loc) · 5.8 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
var _ = require('underscore');
var cards = require('./cards.js');
var gameList = [];
function getDeck() {
return cards.getDeck();
}
function removeFromArray(array, item) {
var index = array.indexOf(item);
if(index !== -1) {
array.splice(index, 1);
}
}
function list() {
return toInfo(_.filter(gameList, function(x) {
return x.players.length < 4 && !x.isStarted
}));
}
function listAll() {
return toInfo(gameList);
}
function toInfo(fullGameList) {
return _.map(fullGameList, function(game) {
return { id: game.id, name: game.name, players: game.players.length };
});
}
function addGame(game) {
game.players = [];
game.history = [];
game.isOver = false;
game.winnerId = null;
game.winningCardId = null;
game.isStarted = false;
game.deck = getDeck();
game.currentBlackCard = "";
game.isReadyForScoring = false;
game.isReadyForReview = false;
game.pointsToWin = 5;
gameList.push(game);
return game;
}
function getGame(gameId) {
return _.find(gameList, function(x) { return x.id === gameId; }) || undefined;
}
function joinGame(game, player) {
var joiningPlayer = {
id: player.id,
name: player.name,
isReady: false,
cards : [],
selectedWhiteCardId: null,
awesomePoints: 0,
isCzar: false
};
for(var i = 0; i < 7; i++) {
drawWhiteCard(game, joiningPlayer);
}
game.players.push(joiningPlayer);
if(game.players.length === 4) {
if(!game.isStarted){
startGame(game);
} else {
//someone may have dropped and rejoined. If it was the Czar, we need to re-elect the re-joining player
var currentCzar = _.find(game.players, function(p) {
return p.isCzar == true;
});
if(!currentCzar){
game.players[game.players.length - 1].isCzar = true;
}
}
}
return game;
}
function departGame(gameId, playerId) {
var game = getGame(gameId);
if(game){
console.info('depart game: ' + game.name);
var departingPlayer = _.find(game.players, function(p){
return p.id === playerId;
});
removeFromArray(game.players, departingPlayer);
if(game.players.length === 0){
//kill the game
removeFromArray(gameList, game);
}
}
}
function startGame(game) {
game.isStarted = true;
setCurrentBlackCard(game);
game.players[0].isCzar = true;
}
function roundEnded(game) {
game.winnerId = null;
game.winningCardId = null;
game.isReadyForScoring = false;
game.isReadyForReview = false;
setCurrentBlackCard(game);
_.each(game.players, function(player) {
if(!player.isCzar) {
removeFromArray(player.cards, player.selectedWhiteCardId);
drawWhiteCard(game, player);
}
player.isReady = false;
player.selectedWhiteCardId = null;
});
if(game.players[0].isCzar === true) {
game.players[0].isCzar = false;
game.players[1].isCzar = true;
game.players[1].isReady = false;
}
else if(game.players[1].isCzar === true) {
game.players[1].isCzar = false;
game.players[2].isCzar = true;
game.players[2].isReady = false;
}
else if(game.players[2].isCzar === true) {
game.players[2].isCzar = false;
game.players[3].isCzar = true;
game.players[3].isReady = false;
}
else if(game.players[3].isCzar === true) {
game.players[3].isCzar = false;
game.players[0].isCzar = true;
game.players[0].isReady = false;
}
if(game.isOver){
_.map(game.players, function(p) {
p.awesomePoints = 0;
});
game.isOver = false;
}
}
function drawWhiteCard(game, player) {
var whiteIndex = Math.floor(Math.random() * game.deck.white.length);
player.cards.push(game.deck.white[whiteIndex]);
game.deck.white.splice(whiteIndex, 1);
}
function setCurrentBlackCard(game) {
var index = Math.floor(Math.random() * game.deck.black.length);
game.currentBlackCard = game.deck.black[index];
game.deck.black.splice(index, 1);
}
function getPlayer(gameId, playerId) {
var game = getGame(gameId);
return _.find(game.players, function(x) { return x.id === playerId; });
}
function getPlayerByCardId(gameId, cardId) {
var game = getGame(gameId);
return _.findWhere(game.players, { selectedWhiteCardId: cardId });
}
function readyForNextRound(gameId, playerId) {
var player = getPlayer(gameId, playerId);
player.isReady = true;
var game = getGame(gameId);
var allReady = _.every(game.players, function(x) {
return x.isReady;
});
if(allReady) {
roundEnded(game);
}
}
function selectCard(gameId, playerId, whiteCardId) {
var player = getPlayer(gameId, playerId);
player.selectedWhiteCardId = whiteCardId;
player.isReady = false;
var game = getGame(gameId);
var readyPlayers = _.filter(game.players, function (x) {
return x.selectedWhiteCardId;
});
if(readyPlayers.length === 3) {
game.isReadyForScoring = true;
}
}
function selectWinner(gameId, cardId) {
var player = getPlayerByCardId(gameId, cardId);
var game = getGame(gameId);
game.winningCardId = cardId;
game.isReadyForReview = true;
player.awesomePoints = player.awesomePoints + 1;
game.history.push({ black: game.currentBlackCard, white: cardId, winner: player.name });
if(player.awesomePoints === game.pointsToWin) {
game = getGame(gameId);
game.isOver = true;
game.winnerId = player.id;
}
}
function reset(){
gameList = [];
}
exports.list = list;
exports.listAll = listAll;
exports.addGame = addGame;
exports.getGame = getGame;
exports.getPlayer = getPlayer;
exports.joinGame = joinGame;
exports.departGame = departGame;
exports.readyForNextRound = readyForNextRound;
exports.reset = reset;
exports.roundEnded = roundEnded;
exports.selectCard = selectCard;
exports.selectWinner = selectWinner;
exports.removeFromArray = removeFromArray;
exports.getDeck = getDeck;