-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
255 lines (221 loc) · 8.15 KB
/
game.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
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
"use strict";
var _ = require('underscore'); //TODO add to dependencies //TODO uncomment
const ROLES = ["Duke", "Assassin", "Captain", "Ambassador", "Contessa"];
const MAX_PLAYERS = 4; //TODO change to 6
const MIN_PLAYERS = 2;
const STARTING_COINS = 2;
const MUST_COUP_AMOUNT = 10;
const INCOME_BOON = 1;
const FOREIGN_AID_BOON = 2;
const TAX_BOON = 3;
const COUP_COST = 7;
const ASSASSINATION_COST = 3;
const STEAL_AMOUNT = 2;
var Player = function(username, initialCards) {
this.username = username;
this.coins = STARTING_COINS;
this.influence = initialCards.map(x => ({role: x, alive: true}));
};
Player.prototype.acquireCoins = function(num) {
this.coins += num;
return num;
};
//returns the number of coins actually lost
Player.prototype.relinquishCoins = function(num) {
if (this.coins < num) num = this.coins;
this.coins -= num;
return num;
};
//pass this function "Duke" to have player lose (one of) their Duke(s)
Player.prototype.loseInfluence = function(role) {
if (!this.influence.some(x => {
if (x.alive && x.role === role) {
x.alive = false;
return true;
}
})) throw "Player does not have specified role!" + role
return this;
};
Player.prototype.isOut = function() {
return !this.influence.some(x => x.alive);
};
Player.prototype.hasRole = function(role) {
return this.influence.some(x => (x.role === role && x.alive));
}
//TODO make sure all functions check if game is started
var Game = function() {
this.isStarted = false;
this.isOver = false;
this.currentTurn = 0;
this.players = [];
this.deck = _.shuffle([...ROLES, ...ROLES, ...ROLES]);
};
Game.prototype.addPlayer = function(username) {
if (this.isStarted) throw "game already started!";
if (!username) throw "no username given!";
if (this.players.some(x => x.username === username)) throw "username already taken!";
if (this.players.length >= MAX_PLAYERS) throw "Max players in game already!"
this.players.push(new Player(username, this.drawFromCourtDeck(2)));
};
Game.prototype.drawFromCourtDeck = function(num) {
var dealt = [];
for(let i = 0; i < num; i++) {
dealt.push(this.deck.pop());
}
return dealt;
};
Game.prototype.currentPlayer = function() {
return this.players[this.currentTurn % this.players.length];
};
Game.prototype.nextPlayer = function() {
var next = null;
do {
next = this.players[++this.currentTurn % this.players.length];
} while(next.isOut());
return next;
};
Game.prototype.getPlayer = function(username) {
var filtered = this.players.filter(x => x.username === username);
if (filtered.length === 0) throw "No player with that username!";
return filtered[0];
}
Game.prototype.startGame = function() {
if (this.isStarted) throw "Game already started!";
if (this.isOver) throw "Can't restart game!"; //TODO ditch isOver and allow game restart
if (this.players < MIN_PLAYERS) throw "Not enough players in game yet!";
this.isStarted = true;
this.isOver = false;
return this.currentPlayer();
};
//will return winning player and end the game if able, otherwise returns null
Game.prototype.getWinner = function() {
var remainingPlayers = this.players.filter(x => !x.isOut());
if (remainingPlayers.length === 1) {
this.isStarted = false;
this.isOver = true;
return remainingPlayers[0];
} else {
return null;
}
};
//This file does not handle managing BS calls and block opportunities.
//So a steal action should only be passed into here after it is sure it will happen
//App will be responsible for making couped player lose an influence before moving on
// and such before moving on by calling game.nextPlayer()
//actionObj should have keys "player", "action", and "targetPlayer" if applicable
// player is the string player username
// action is a string all caps like "TAX" or "FOREIGN AID"
// targetPlayer is also a string username
Game.prototype.takeAction = function(actionObj) {
if (this.currentPlayer().username != actionObj.player) throw "Not your turn!";
if (this.currentPlayer().coins >= MUST_COUP_AMOUNT && actionObj.action != "COUP") throw "You must coup with that many coins!";
if (this.targetPlayer && (actionObj.targetPlayer === actionObj.player || this.getPlayer(actionObj.targetPlayer).isOut)) throw "Invalid action target!";
switch(actionObj.action) {
case "INCOME":
this.currentPlayer().acquireCoins(INCOME_BOON);
break;
case "FOREIGN AID":
this.currentPlayer().acquireCoins(FOREIGN_AID_BOON);
break;
case "COUP":
if (this.currentPlayer().coins < COUP_COST) throw "Not enough coins to coup!"
this.currentPlayer().relinquishCoins(COUP_COST);
break;
case "TAX":
this.currentPlayer().acquireCoins(TAX_BOON);
break;
case "ASSASSINATE":
if (this.currentPlayer().coins < ASSASSINATION_COST) throw "Not enough coins to coup!"
this.currentPlayer().relinquishCoins(ASSASSINATION_COST);
break;
case "STEAL":
this.currentPlayer().acquireCoins(this.getPlayer(actionObj.targetPlayer).relinquishCoins(STEAL_AMOUNT));
break;
case "EXCHANGE":
//TODO needs to draw 2 cards and return them somehow
break;
default:
throw "Not a valid action!"
}
};
//call with a username and a role name. If user has that role the shuffle it in and get a new card
Game.prototype.returnAndReplace = function(username, role) {
var thePlayer = this.getPlayer(username);
if (!ROLES.includes(role)) throw "Not a valid role!";
if (!thePlayer.influence.some((x, i) => {
if (x.alive && x.role === role) {
thePlayer.influence.splice(i,1);
return true;
}
})) throw "Player does not have specified role!";
this.deck.push(role);
this.deck = _.shuffle(this.deck);
thePlayer.influence.push({role: this.drawFromCourtDeck(1)[0], alive: true}); //TODO shuffle player influence
return thePlayer; //TODO return player?
};
Game.prototype.numPlayers = function() {
return this.players.length;
};
//returns challenge loser AND reshuffles the card if it was revealed
Game.prototype.whoLostChallenge = function(caller, claimer, claimedCharacter) {
var loser = this.getPlayer(claimer).hasRole(claimedCharacter) ? caller : claimer;
if (loser === caller) {
this.returnAndReplace(claimer, claimedCharacter); //return the revealed card
}
return loser;
};
Game.prototype.getPlayerPerspective = function(viewer) {
return this.players.map(player => {
//function that takes a player and returns a player with hidden alive cards
var copy = Object.assign({}, player);
if (copy.username !== viewer) {
copy.influence = copy.influence.map(card => {
var cardCopy = Object.assign({}, card);
if (cardCopy.alive) {
cardCopy.role = "Facedown";
}
return cardCopy;
});
}
return copy;
});
};
Game.prototype.ambassadorDecision = function(playerName, keptInfluence, returnedRoles) {
if (!this.players.some(x => {
if (x.username === playerName) {
x.influence = keptInfluence; //TODO shuffle player influence?
this.deck.concat(returnedRoles);
this.deck = _.shuffle(this.deck);
return true;
}
return false;
})) {
throw "no player with that name for ambassador!";
}
};
Game.prototype.isAlive = function(playerName) {
return !this.getPlayer(playerName).isOut();
};
Game.prototype.numAlivePlayers = function() {
return this.players.filter(x => !x.isOut()).length;
};
Game.prototype.actionString = function(action) {
switch(action.action) {
case "INCOME":
return action.player + " gained 1 coin from Income.";
case "FOREIGN AID":
return action.player + " gained 2 coins from Foreign Aid.";
case "COUP":
return action.player + " paid 7 coins to Coup " + action.targetPlayer + ".";
case "TAX":
return action.player + " gained 3 coins from Tax.";
case "STEAL":
return action.player + " took (up to) 2 coins from " + action.targetPlayer + " with Steal.";
case "ASSASSINATE":
return action.player + " paid 3 coins to Assassinate " + action.targetPlayer + ".";
case "EXCHANGE":
return action.player + " drew 2 cards and kept their choice with Exchange."
}
}
//TODO and refactor other code to use hasRole
module.exports = Game; //TODO uncomment