-
Notifications
You must be signed in to change notification settings - Fork 17
/
gimp.js
200 lines (177 loc) · 4.81 KB
/
gimp.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
/**
* @typedef {Object} Location
* @property {number} x
* @property {number} y
* @property {number} plane
*/
const DEFAULT_CURRENT_HP = 10;
const DEFAULT_MAX_HP = 10;
const DEFAULT_CURRENT_PRAYER = 1;
const DEFAULT_MAX_PRAYER = 1;
class GroupIronmanPlayer {
/**
* @member {string}
*/
name = "";
/**
* @member {Location}
*/
location = {};
/**
* @member {number}
*/
hp = DEFAULT_CURRENT_HP;
/**
* @member {number}
*/
maxHp = DEFAULT_MAX_HP;
/**
* @member {number}
*/
prayer = DEFAULT_CURRENT_PRAYER;
/**
* @member {number}
*/
maxPrayer = DEFAULT_MAX_PRAYER;
/**
* @member {boolean} ghostMode
*/
ghostMode = false;
/**
* @member {string} lastActivity;
*/
lastActivity = "";
/**
* @member {string} notes
*/
notes = "";
/**
* @param {string} name
*/
constructor(name) {
this.name = name;
}
getData() {
const data = {
name: this.name,
location: this.location,
hp: this.hp,
maxHp: this.maxHp,
prayer: this.prayer,
maxPrayer: this.maxPrayer,
ghostMode: this.ghostMode,
lastActivity: this.lastActivity,
notes: this.notes
};
return data;
}
/**
* Scrubs data from the object if ghost mode is enabled.
*
* @param {Object<string, Object>} data
*/
scrubData(data) {
if (this.ghostMode) {
// Always include the name (and ghostMode if its included)
return { name: data.name || this.name, ghostMode: data.ghostMode };
}
return data;
}
update(data) {
if (data.location) {
this.updateLocation(data.location);
}
// Must support a value of 0
if (typeof data.hp === "number") {
this.updateHp(data.hp);
}
// Must support a value of 0
if (typeof data.maxHp === "number") {
this.updateMaxHp(data.maxHp);
}
// Must support a value of 0
if (typeof data.prayer === "number") {
this.updatePrayer(data.prayer);
}
// Must support a value of 0
if (typeof data.maxPrayer === "number") {
this.updateMaxPrayer(data.maxPrayer);
}
// Must support empty string
if (typeof data.notes === "string") {
this.updateNotes(data.notes);
}
// Must accept a false value
if (typeof data.ghostMode === "boolean") {
this.handleGhostMode(data.ghostMode);
}
if (data.lastActivity) {
this.updateLastActivity(data.lastActivity);
}
}
updateLocation(location) {
if (typeof location.x !== "number") {
throw new Error("Invalid x coordinate: " + location.x);
}
if (typeof location.y !== "number") {
throw new Error("Invalid y coordinate: " + location.y);
}
if (typeof location.plane !== "number") {
throw new Error("Invalid plane coordinate: " + location.plane);
}
this.location = {
x: parseInt(location.x, 10),
y: parseInt(location.y, 10),
plane: parseInt(location.plane, 10)
};
}
updateHp(hp) {
if (typeof hp !== "number") {
throw new Error("Invalid HP value: " + hp);
}
this.hp = hp;
}
updateMaxHp(hp) {
if (typeof hp !== "number") {
throw new Error("Invalid HP max: " + hp);
}
this.maxHp = hp;
}
updatePrayer(prayer) {
if (typeof prayer !== "number") {
throw new Error("Invalid prayer value: " + prayer);
}
this.prayer = prayer;
}
updateMaxPrayer(prayer) {
if (typeof prayer !== "number") {
throw new Error("Invalid prayer max: " + prayer);
}
this.maxPrayer = prayer;
}
updateNotes(notes) {
if (typeof notes !== "string") {
throw new Error("Invalid notes: " + notes);
}
const MAX_NOTE_LENGTH = 2000;
this.notes = notes.substring(0, MAX_NOTE_LENGTH);
}
handleGhostMode(ghostMode) {
if (typeof ghostMode !== "boolean") {
throw new Error("Invalid ghost mode value: " + ghostMode);
}
this.ghostMode = ghostMode;
// Remove current location if ghost mode is enabled,
// client will no longer send location updates
if (this.ghostMode) {
delete this.location;
}
}
updateLastActivity(activity) {
if (typeof activity !== "string") {
throw new Error("Invalid activity value: " + activity);
}
this.lastActivity = activity;
}
}
module.exports = GroupIronmanPlayer;