-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgame.js
151 lines (126 loc) · 4.21 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
let gamestages = require('./data/gamestages');
module.exports = class Game {
constructor(client, author, hostChannel, players, embed) {
/**
* Meta
*/
this.client = client;
this.author = author;
this.hostChannel = hostChannel;
this.players = players;
this.embed = embed;
this.playerInfo = this.givePlayersRoles(players);
this.playerOrder = this.playerInfo.playerOrder;
this.dead = [];
this.hitler = this.playerInfo.hitler;
this.hitlerKnows = false;
this.cardCount = {
liberal: 8,
fascist: 11
}
this.deck = this.shuffleDeck(8, 11);
this.discard = { liberal: 0, fascist: 0 }
this.factionCount = {
liberal: [3, 4, 4, 5, 5, 6][players.length - 5],
fascist: [2, 2, 3, 3, 4, 4][players.length - 5],
}
this.board = {
// there are 5 liberal slots
liberalOnBoard: 0,
// there are 6 fascist slots, veto unlocks once there are 5 fascist cards taken
fascistOnBoard: 0,
resetCounters: 0,
fascistRuleset: this.pickFascistRuleset(players)
}
/**
* VOTING: VOTING for president / chancellor
*/
this.gameStage = gamestages.PCHOOSE;
this.presidentIndex = 0;
this.president = this.playerOrder[this.presidentIndex];
this.chancellor = null;
this.chancellorCandidate = undefined;
// elected president and chancellor
this.lastPresident = {id: undefined}
this.lastChancellor = {id: undefined}
this.getLiberalPlayers = _ => this.playerInfo.liberals;
this.getFascistPlayers = _ => this.playerInfo.fascists;
for (let p of this.playerInfo.liberals) {
p.role = "L"
}
for (let p of this.playerInfo.fascists) {
p.role = "F"
}
}
/**
*
* PI: president investigates
* PE: president examines top 3 cards
* PP: president picks next president
* PK: president kills player
*/
pickFascistRuleset = players => {
switch(players.length) {
case(5 || 6):
this.hitlerKnows = true;
return [' + ', ' + ', 'PE', 'PK', 'PK', ' + ']
case(7 || 8):
return [' + ', 'PI', 'PP', 'PK', 'PK', ' + ']
case(9 || 10):
return ['PI', 'PI', 'PP', 'PK', 'PK', ' + ']
default:
break;
}
}
givePlayersRoles = players => {
let playersCpy = players.slice(0);
let playerOrder = playersCpy.slice(0);
shuffleArray(playerOrder);
let fascists = [];
let liberals = [];
let fascCount = [2, 2, 3, 3, 4, 4][players.length - 5];
while (fascists.length < fascCount) {
let rand = Math.floor(Math.random() * playersCpy.length);
fascists.push(playersCpy[rand])
playersCpy.splice(rand, 1)
}
liberals = playersCpy.map(p => p);
function shuffleArray(array) {
for (var i = array.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
let hitler = fascists[Math.floor(Math.random() * fascists.length)];
return {
liberals: liberals,
fascists: fascists,
playerOrder,
hitler
}
}
shuffleDeck = (lib, fasc) => {
let deck = [...Array(lib + fasc)].map((x, i) => {
if (i < lib) {
return "L";
} else {
return "F";
}
})
for (var i = deck.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = deck[i];
deck[i] = deck[j];
deck[j] = temp;
}
return deck;
}
rotatePresidentToFront = () => {
while (this.playerOrder[0].id != this.president.id) {
let first = this.playerOrder.shift();
this.playerOrder.push(first);
}
}
}