-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.pde
91 lines (71 loc) · 2.17 KB
/
Player.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
enum Roles {
ANGEL, HUMAN
}
class Player {
Roles myRole;
String myName;
ArrayList<Votes> myVotes;
boolean isAlive = true;
boolean isKiller = false;
//Only here for the listify method
Player() {}
Player(String myName) {
this.myName = myName;
}
void setRole(Roles role) {
myRole = role;
}
void setLiving(boolean isAlive) {
this.isAlive = isAlive;
}
void setKiller(boolean isKiller) {
this.isKiller = isKiller;
}
String getName() {
return myName;
}
boolean isAngel() {
return myRole == Roles.ANGEL;
}
boolean isAlive() {
return isAlive;
}
/*
* Builds a player object from their equivalent JSON
* Omitting the votes as they may not be required
*/
Player fromJSON(JSONObject source) {
Player retPlayer = new Player(source.getString("name"));
retPlayer.setRole((source.getString("role").equals("ANGEL"))
? Roles.ANGEL : Roles.HUMAN);
retPlayer.setLiving(source.getBoolean("isAlive"));
retPlayer.setKiller(source.getBoolean("isKiller"));
return retPlayer;
}
Player fromJSON(String source) {
return fromJSON(parseJSONObject(source));
}
JSONObject toJSON() {
JSONObject retObj = new JSONObject();
retObj.setString("name", myName);
retObj.setBoolean("isAlive", isAlive);
retObj.setBoolean("isKiller", isKiller);
retObj.setString("role", myRole.name());
return retObj;
}
void kill() {
isAlive = false;
}
/*
* Puts all of the specified players into a single JSON Array to be stored server-side
* I want to make this static but Processing won't let me
*/
JSONObject listify(HashMap<String, Player> allPlayers) {
JSONObject retObj = new JSONObject();
JSONArray playerArray = new JSONArray();
for (Player player : allPlayers.values())
playerArray.append(player.toJSON());
retObj.setJSONArray("players", playerArray);
return retObj;
}
}