-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay.pde
342 lines (284 loc) · 11.6 KB
/
Day.pde
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
class Day {
int dayNum = 1;
int timer;
int subTimer = 0;
int numGuilty = 0, numInno = 0;
boolean nomFlag = false;
boolean voteFlag = false;
boolean resetFlag = false;
boolean angelWin = false;
//The player who has been convicted of murder
Player nomPlayer;
//The player to be executed at the end of a day.
Player exePlayer;
//The angel to murder another player at night.
Player killer;
void draw() {
switch (myGame.getPlayState()) {
case NEWS:
myGame.drawText("Did someone die? IDK.");
if (!resetFlag) {
updateGameDay();
nomPlayer = null;
if (killer != null) resetKiller();
//Ensuring the new killer is set before night,
//and they know who they are before then
selectKiller();
}
//Give enough time to display the news
if (millis() - timer > 10e3)
// if (millis() - timer > 10e3)
changePlayState(PlayState.NOMINATION);
break;
case NOMINATION:
if (!nomFlag) myGame.drawText("Nominate a player to be executed.");
//Allowing 120 seconds for a nomination phase
if (millis() - timer > 10e3) {
// if (millis() - timer > 120e3) {
closeNominations();
nominationResults();
}
break;
case INVEST:
//TODO add this
break;
case VOTING:
if (!voteFlag) myGame.drawText("Vote to decide on the fate of player to be executed.");
//Allowing 30 seconds to decide on the player to execute
if (millis() - timer > 10e3) {
// if (millis() - timer > 3e3) {
// if (millis() - timer > 30e3) {
//Ensuring that the votes are not retrieved an excess amount of times
stopVoting();
votingResults();
}
break;
case LYNCHING:
myGame.drawText(String.format("%s has been executed!", exePlayer.getName()));
//TODO determine the end of the game and move into the RESULTS game state
if (millis() - timer > 15e3) {
// exePlayer.kill();
myGame.killPlayer(exePlayer.getName());
changePlayState(PlayState.NIGHT);
}
break;
case NIGHT:
myGame.drawText("It's the night time, everyone goes to sleep, apart from one angel...");
//Give the angel(s) enough time to decide who to kill
if (millis() - timer > 15e3) {
// update the players for myGame such that the murdered player (if any) dies
resetFlag = false;
//Get the player that died here
changePlayState(PlayState.NEWS);
}
break;
}
}
/*
* Queries the server for the current state of all the connected players
* and stores all the players into and ArrayList
*/
ArrayList<Player> getAllPlayers() {
ArrayList<String> playerJSONs = myGame.sendRequest("players/states");
ArrayList<Player> allPlayers = new ArrayList<Player>();
//If an array of players was returned
if (playerJSONs.get(0).startsWith("[")) {
JSONArray playerArray = parseJSONArray(playerJSONs.get(0));
for (int i = 0; i < playerArray.size(); i++) {
JSONObject json = playerArray.getJSONObject(i);
Player foo = new Player().fromJSON(json);
allPlayers.add(foo);
}
}
else {
System.out.println(playerJSONs.get(0));
Player foo = new Player().fromJSON(playerJSONs.get(0));
allPlayers.add(foo);
}
return allPlayers;
}
/*
* Helper method to retrieve all of the votes submitted by players
*/
Votes retrieveVotes() {
//Retrieve all of the votes from players.
ArrayList<String> votes = myGame.sendRequest("admin/votes");
Votes retVotes = new Votes();
//If the response yielded votes, parse and return them
if (hasVotes(votes))
for (String voteJSON : votes)
retVotes.add(new Vote().fromJSON(voteJSON));
return retVotes;
}
HashMap<Player, Integer> getNominations() {
Votes allNoms = retrieveVotes();
HashMap<Player, Integer> nomCount = new HashMap<Player, Integer>();
//If the response yielded votes, determine the most nominated player
//If multiple are equally nominated, randomly select one
if (!allNoms.isEmpty()) {
//Counting nominations for all players.
for (Vote nom : allNoms) {
if (nom.isNomination()) {
Player current = nom.getDefendant();
int total = (nomCount.containsKey(current))
? nomCount.get(current) : 0;
nomCount.put(current, ++total);
}
}
}
return nomCount;
}
void closeNominations() {
if (!nomFlag) {
HashMap<Player, Integer> nomCount = getNominations();
if (!nomCount.isEmpty()) {
int mostNoms = -1;
for (Map.Entry<Player, Integer> entry : nomCount.entrySet())
if (entry.getValue() > mostNoms) nomPlayer = entry.getKey();
}
subTimer = millis();
nomFlag = true;
}
}
void nominationResults() {
boolean nominated = nomPlayer != null;
HashMap<Player, Integer> allNoms = getNominations();
if (!nominated)
myGame.drawText(String.format("No one will be executed today as "
+ "nobody was convicted."));
else
myGame.drawText(String.format("%s has been convicted.\nThere were %d "
+ "convictions against them.", nomPlayer.getName(), allNoms.get(nomPlayer)));
//The results will be displayed for 10 seconds before switching
//to the next state of play
if (millis() - subTimer > 2e3) {
// if (millis() - subTimer > 10e3) {
nomFlag = false;
if (nominated)
changePlayState(PlayState.VOTING);
else
changePlayState(PlayState.NIGHT);
}
}
/*
* Gets all the votes from the server, totals them and determines whether the
* player will be lynched
*/
void stopVoting() {
if (!voteFlag) {
Votes allVotes = retrieveVotes();
//If the response yielded votes, determine whether the nominated player is innocent
if (!allVotes.isEmpty()) {
//Counting all of the guilty and innocent votes.
for (Vote vote : allVotes) {
if (!vote.isNomination()) {
if (vote.isGuilty()) numGuilty++;
else numInno++;
}
}
exePlayer = allVotes.get(0).getDefendant();
}
else {
numGuilty = -1;
}
subTimer = millis();
voteFlag = true;
}
}
/*
* Displaying the results of the voting on the host screen
*/
void votingResults() {
// myGame.drawText(String.format("%s will not be executed today as "
// + "nobody voted.", exePlayer.getName()));
if (numGuilty == -1)
myGame.drawText(String.format("No one will be executed today as "
+ "nobody voted."));
else if (numGuilty > numInno)
myGame.drawText(String.format("%s will be executed.\nThere were %d "
+ "guilty votes and %d innocent votes.", exePlayer.getName(),
numGuilty, numInno));
else
myGame.drawText(String.format("%s has been pardoned.\nThere were %d "
+ "innocent votes and %d guilty votes.", exePlayer.getName(),
numInno, numGuilty));
//The results will be displayed for 10 seconds before switching
//to the next state of play
if (millis() - subTimer > 2e3) {
// if (millis() - subTimer > 10e3) {
voteFlag = false;
if (numGuilty > numInno)
changePlayState(PlayState.LYNCHING);
else
changePlayState(PlayState.NIGHT);
}
}
/*
* Selects an Angel at random to be the Angel who decides on which
* player to kill that night
*/
void selectKiller() {
ArrayList<Player> allAngels = getAllPlayers();
Iterator<Player> playerIter = allAngels.iterator();
while (playerIter.hasNext())
if (!playerIter.next().isAngel()) playerIter.remove();
//This is the only line where the selection is done,
//the previous just filters the players
killer = allAngels.get((int) random(allAngels.size()));
killer.setKiller(true);
myGame.postData("admin/killer", killer.toJSON());
}
/*
* Sets the killer flag for the previous day's killer to false
* and reflects the changes in the server
*/
void resetKiller() {
killer.setKiller(false);
myGame.postData("admin/killer", killer.toJSON());
}
void updateMurderVictim() {
}
/*
* Determines if either of the groups of players have won the game
*/
void checkGameWon() {
ArrayList<Player> allPlayers = getAllPlayers();
int angelPop = 0, humanPop = 0;
System.out.println("\n" + myGame.myPlayState);
for (Player player : allPlayers) {
if (player.isAngel() && player.isAlive()) angelPop++;
else if (!player.isAngel() && player.isAlive()) humanPop++;
if (!player.isAngel()) System.out.println(player.toJSON().toString());
}
System.out.println("Num Angels " + angelPop);
System.out.println("Num Humans " + humanPop);
if (angelPop == 0 || humanPop == 0) {
myGame.updateGameState(GameState.RESULTS);
//This is fine as this statement won't ever be executed if there are
//both humans and angels alive
myGame.setAngelWin(humanPop == 0);
}
}
void startTimer() {
timer = millis();
}
void changePlayState(PlayState newPlayState) {
myGame.updatePlayState(newPlayState);
checkGameWon();
timer = millis();
}
boolean hasVotes(ArrayList<String> voteResponse) {
//This is only found in the response when there are no votes for the day
return !voteResponse.get(0).contains("NoSuchKey");
}
/*
* Updates the current day number stored on the server and locally
*/
void updateGameDay() {
JSONObject reqBody = new JSONObject();
reqBody.setInt("day", dayNum);
myGame.postData("admin/state", reqBody);
dayNum++;
resetFlag = true;
}
}