-
Notifications
You must be signed in to change notification settings - Fork 0
/
classes.ts
249 lines (224 loc) · 7.44 KB
/
classes.ts
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
import * as db from './db';
import * as CONSTANTS from './constants';
export class User {
id: string;
rawData: any;
inventory: any;
coins: number;
used_mail: boolean;
stats: any;
skill: any; // This is not yet used, but seen in Jakes source code
mail_reward: any;
clearance_level: number;
constructor(user_id: string) {
this.id = user_id;
this.rawData = {};
this.inventory = {};
this.stats = {};
this.skill = {};
this.used_mail = false;
this.coins = 0;
this.mail_reward = {};
this.clearance_level = 0;
}
private overlayJson(
obj1: pylon.JsonObject,
obj2: pylon.JsonObject
): pylon.JsonObject {
const res: any = {};
for (const key in obj1) {
res[key] = obj1[key];
if (obj2[key]) {
res[key] += obj2[key];
}
}
for (const [key, val] of Object.entries(obj2)) {
if (!res.hasOwnProperty(key)) {
res[key] = val;
}
}
return res;
}
private substractJson(
obj1: pylon.JsonObject,
obj2: pylon.JsonObject
): pylon.JsonObject {
const res: any = {};
for (const key in obj1) {
res[key] = obj1[key];
if (obj2[key]) {
res[key] = res[key] - (obj2[key] as any);
if (res[key] <= 0) {
delete res[key];
}
}
}
return res;
}
async getData(): Promise<User> {
//This basically sets all the properties as the constructor is not asyncronous which is needed for the db
// if the user does not exist, it adds the default values
let data = await db.get(this.id);
if (!data) data = await this.addDefault();
this.rawData = data;
this.used_mail = (data as any).used_mail;
this.inventory = (data as any).inventory;
this.coins = (data as any).coins;
this.stats = (data as any).stats;
this.skill = (data as any).skill;
this.mail_reward = (data as any).mail_reward;
this.clearance_level = (data as any).clearance_level;
return this;
}
async addDefault(): Promise<pylon.JsonObject> {
// Adds the default values if a user is not in the database
await db.save(this.id, CONSTANTS.DEFAULT_VALUES);
return CONSTANTS.DEFAULT_VALUES;
}
hasItem(item: number): boolean {
// Checks if a user has a certain item
return (this.inventory as any).hasOwnProperty(item);
}
isEquipped(item: number): boolean {
// Checks if a user has a certain item equipped
return (this.inventory as any)[item].equipped;
}
async addCoins(amount: number): Promise<void> {
// adds coins to a Users account
this.rawData.coins = this.coins + amount;
await db.transact(this.id, () => this.rawData);
}
async removeCoins(amount: number): Promise<void> {
// removes coins from a Users account
if (this.coins < 1) throw new Error('User does not have any coins');
this.rawData.coins = this.coins - amount;
await db.transact(this.id, () => this.rawData);
}
async equip(
item: number,
equip: boolean,
m: discord.GuildMember
): Promise<void> {
// Equips or unequips an item
this.rawData.inventory[item] = !equip;
// This looks for if an item is already equipped, if it is it removes that item
let old_equip = Object.keys(this.inventory).find(
(key) =>
CONSTANTS.ITEMS[key].slot === CONSTANTS.ITEMS[item].slot &&
this.inventory[item] == true &&
item !== +key
);
if (old_equip) {
this.rawData.inventory[old_equip] = false;
this.rawData.stats = this.substractJson(
this.stats,
CONSTANTS.ITEMS[old_equip].stats
);
if (CONSTANTS.ITEMS[old_equip].type == 'badge') {
await m.removeRole(CONSTANTS.ITEMS[old_equip].role).catch((e) => {});
}
}
if (!equip) {
this.rawData.stats = this.overlayJson(
this.rawData.stats,
CONSTANTS.ITEMS[item].stats
);
} else {
this.rawData.stats = this.substractJson(
this.rawData.stats,
CONSTANTS.ITEMS[item].stats
);
}
if (CONSTANTS.ITEMS[item].type == 'badge') {
!equip
? await m.addRole(CONSTANTS.ITEMS[item].role).catch((e) => {})
: await m.removeRole(CONSTANTS.ITEMS[item].role).catch((e) => {});
}
await db.transact(this.id, () => this.rawData);
}
async addItem(item: number, amount: number = 1): Promise<void> {
if (CONSTANTS.ITEMS[item].type == 'collectable') {
// if it is a collectable, it adds one more of it
this.rawData['inventory'][item] =
(this.rawData['inventory'][item] ?? 0) + amount;
} else {
// if it is not a collectable, it gets added as a not equipped item
this.rawData['inventory'][item] = false;
}
await db.transact(this.id, () => this.rawData);
}
async removeItem(item: number, amount: number = 1): Promise<void> {
if (!(await this.hasItem(item)))
throw new Error('The user does not own this item');
if (this.rawData['inventory'][item] < amount)
throw new Error('The user does not own this item');
if (CONSTANTS.ITEMS[item].type == 'collectable') {
// I added this check in case I ever implement non-collectables being removable
this.rawData['inventory'][item] =
this.rawData['inventory'][item] - amount;
if (this.rawData['inventory'][item] == 0) {
delete this.rawData['inventory'][item];
}
}
await db.transact(this.id, () => this.rawData);
}
async addStock(): Promise<void> {
// Adds a stock to a users inventory. These behave different from normal items
this.rawData.inventory.stocks = (this.inventory.stocks ?? 0) + 1;
await db.transact(this.id, () => this.rawData);
}
async removeStock(): Promise<void> {
// Removes a stock from the inventory
if ((this.inventory.stocks ?? 0) < 1) throw new Error('No stocks mate');
this.rawData.inventory.stocks = this.inventory.stocks - 1;
await db.transact(this.id, () => this.rawData);
}
async increaseSkill(skill: string, amount: number = 1): Promise<void> {
// Increases a certain skill
this.rawData['skill'][skill] = (this.rawData['skill'][skill] ?? 0) + amount;
await db.transact(this.id, () => this.rawData);
}
async craft(): Promise<void> {
// Crafts a new advanced testing permit or pc
if (
this.hasItem(15) &&
this.hasItem(16) &&
this.hasItem(17) &&
this.hasItem(18)
) {
for await (let i of [15, 16, 17, 18]) await this.removeItem(i);
await this.addItem(19);
await this.increaseSkill('crafting');
return;
}
await this.removeItem(1, 5);
await this.addItem(2);
await this.increaseSkill('crafting');
}
async emptyMailbox(): Promise<string> {
// Empties the mailbox
await this.addItem(this.mail_reward.item);
let text = this.mail_reward.message;
this.rawData.used_mail = true;
this.rawData.mail_reward = null;
await db.transact(this.id, () => this.rawData);
return text;
}
async addMail(mail: number, message: string): Promise<void> {
this.rawData.used_mail = false;
this.rawData.mail_reward = { item: mail, message: message };
await db.transact(this.id, () => this.rawData);
}
isAdvanced(): boolean {
// Checks if the user can conduct advanced tests
return (this.inventory as any).hasOwnProperty(2);
}
getStat(stat: string): number {
// Get the stats in a certain field
return this.stats[stat] ?? 0;
}
isAuthorised(required: number): boolean {
// Checks if a user is authorised with a certain badge
return (this.stats.authorisation ?? 0) >= required;
}
}