-
Notifications
You must be signed in to change notification settings - Fork 1
/
Quills.js
239 lines (214 loc) · 7.27 KB
/
Quills.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
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
const ROOM_ID = "thelibrary";
const SAVE_TIMER = 15 * 60 * 1000 // Force save quill data every 15 minutes
/*
Quills module
Handles the virtual currency of The Library
*/
class Shop {
#items = {};
constructor() {
if (!FS.existsSync('./data/shop.json')) this.#items = {};
else this.#items = JSON.parse(FS.readFileSync('./data/shop.json'));
}
get() {
return Object.assign({}, this.#items);
}
save() {
if (Saver.Shop) return;
logger.emit('save-start', 'Shop');
const shopData = this.get();
FS.writeFile('./data/shop-temp.json', JSON.stringify(shopData, null, 2), () => {
FS.rename('./data/shop-temp.json', './data/shop.json', () => {
logger.emit('save-end', 'Shop');
})
})
}
getItem(name) {
return this.addItem(name);
}
addItem(name, description, price, count) {
let id = toId(name);
if (this.#items[id]) return this.#items[id];
if (!description) return false;
this.#items[id] = {
name,
desc: description,
price,
count
}
this.save();
return this.#items[id];
}
removeItem(name) {
let id = toId(name);
if (!this.#items[id]) return false;
delete this.#items[id];
this.save();
return true;
}
display(user) {
let quills = Quills.getQuills(user);
let header = `
<div class="pm-log-add" style="border: none; border-bottom:2px solid; width:auto; background:none; font-size:16px; padding:4px">
<b>${Quills.name} Shop</b>\
<span style="float:right;margin-right:27px;font-size:16px;border-bottom:1px solid" title="Your ${Quills.name}">
$${quills}
</span></div>`;
let items = [];
for (let i in this.#items) {
let item = this.#items[i];
let colour = item.price > quills ? "rgba(255, 100, 100, .5)" : "rgba(50, 255, 50, .4)";
items.push(
[
`<button class="pm-log-add" name="send" value="/msg ${Config.username}, ${Config.char}buyitem ${i}" style="border: none; border-bottom:1px solid;width:100%;background:${colour};text-align:left">
<div style="position:relative;bottom:-2px">
<span title="${item.desc}">${item.name}</span>
<span style="float:right;margin-right:22px">${item.price} ${Quills.name}</span>
</div>
</button>`,
item.price
]
);
}
items.sort((a, b) => {return a[1] - b[1]});
return (header + items.map(x => x[0]).join('<br>')).replace(/(\s\s\s\s|\t|\n)/gmi, '');
}
}
class QuillManager {
#quills = {};
#inventory = {};
saving = {};
constructor() {
global.Quills = this;
this.room = false; // Will be loaded when bot joins a room
this.shop = new Shop();
this.name = 'Quills';
}
getQuills(user) {
if (typeof user === "string") user = {id: toId(user)};
if (user.quills) return user.quills.current;
if (!this.#quills[user.id]) return 0;
return this.#quills[user.id].current;
}
getQuillObj(user) {
if (typeof user === "string") user = {id: toId(user), name: user};
if (user.quills) return user.quills;
if (!this.#quills[user.id]) return {current: 0, total: 0, name: user.name};
return this.#quills[user.id];
}
addQuills(target, amount) {
if (!this.#quills[target]) this.#quills[target] = {name:target, current:0, total:0};
if (this.#quills[target].current + amount < 0) return false;
this.#quills[target].current += amount;
this.#quills[target].total += amount;
if (Users[target]) Users[target].quills = this.#quills[target];
this.save(1);
return this.#quills[target].current;
}
getInventory(user) {
if (typeof user === "string") user = {id: toId(user)};
if (user.inventory) return user.inventory;
if (!this.#inventory[user.id]) return {};
return this.#inventory[user.id];
}
setInventory(user, data) {
if (typeof user !== 'string') {
user.inventory = data;
user = user.id;
}
this.#inventory[toId(user)] = data;
this.save(2);
}
buyItem(user, item) {
if (user.id) user = user.id; // Just incase someone passes this a user object
item = toId(item);
let quills = this.getQuills(user);
let itemData = this.shop.getItem(item);
if (!itemData) return false;
if (quills < itemData.price) return false;
let inventory = this.getInventory(user);
if (!inventory[item]) inventory[item] = 0;
if (itemData.count === true) inventory[item] = true;
else inventory[item] += itemData.count;
this.addQuills(user, -itemData.price);
this.setInventory(user, inventory);
this.save(1);
return itemData;
}
giveItem(user, item, amount = 1) {
if (user.id) user = user.id; // Just incase someone passes this a user object
item = toId(item);
let itemData = this.shop.getItem(item);
if (!itemData) return false; // Item doesn't exist. Should never happen though
let inventory = this.getInventory(user); // Returns inventory if user exists, creates user inventory otherwise
if (!inventory[item]) inventory[item] = 0;
if (itemData.count === true) {
if (amount >= 0) inventory[item] = true;
else inventory[item] = false;
}
else inventory[item] += amount;
this.setInventory(user, inventory);
return inventory[item];
}
deleteItem(name) {
let id = toId(name);
for (let i in this.#inventory) {
delete this.#inventory[i][id];
if (Users[i] && Users[i].inventory) delete Users[i].inventory[id];
}
this.shop.removeItem(id);
this.save(2);
}
loadRoom(room) {
if (room.id === ROOM_ID) this.room = room;
}
loadQuills() {
if (!FS.existsSync('./data/quills.json')) this.#quills = {};
else this.#quills = JSON.parse(FS.readFileSync('./data/quills.json'));
if (!FS.existsSync('./data/inventory.json')) this.#inventory = {};
else this.#inventory = JSON.parse(FS.readFileSync('./data/inventory.json'));
this.saveHandler = setInterval(() => {this.save()}, SAVE_TIMER);
}
save(n = 0) {
FS.writeFileSync('./data/quills.json', JSON.stringify(this.#quills, null, 2));
FS.writeFileSync('./data/inventory.json', JSON.stringify(this.#inventory, null, 2));
if (n === 3 || !n) this.shop.save();
return;
if (!Saver.Quills && (n === 1 || !n)) {
logger.emit('save-start', 'Quills');
FS.writeFile('./data/quills-temp.json', JSON.stringify(this.#quills, null, 2), () => {
FS.rename('./data/quills-temp.json', './data/quills.json', () => {
logger.emit('save-end', 'Quills');
})
})
}
if (!Saver.Inventory && (n === 2 || !n)) {
logger.emit('save-start', 'Inventory');
FS.writeFile('./data/inventory-temp.json', JSON.stringify(this.#inventory, null, 2), () => {
FS.rename('./data/inventory-temp.json', './data/inventory.json', () => {
logger.emit('save-end', 'Inventory');
})
})
}
if (n === 3 || !n) this.shop.save();
}
getLeaderboard() {
let html = `<center style="margin:35px"><table style="width:100%;text-align:center;border:1px solid">`
html += `<tr><th>#</th><th>Username</th><th>Current Quills</th><th>Total Earned</th></tr>`;
let quilldata = Object.values(JSON.parse(JSON.stringify(this.#quills))) // make a hard copy of the quills data to avoid fucking with it because I'm paranoid.
quilldata = quilldata.sort((a, b) => {
if (b.total === a.total) {
if (b.current === a.current) return 0.5 - Math.random();
return b.current - a.current;
}
return b.total - a.total;
});
for (let i = 0; i < quilldata.length; i++) {
let dt = quilldata[i];
html += `<tr><td>${i+1}</td><td>${dt.name}</td><td>${dt.current}</td><td>${dt.total}</td><td></td></tr>`;
}
html += `</table></center>`;
return html;
}
}
module.exports = QuillManager;