-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.js
662 lines (559 loc) · 20.6 KB
/
main.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
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
import express from 'express'
import cors from 'cors'
import bcrypt from "bcrypt"
import { writeFile, readFile } from "fs/promises"
import axios from 'axios';
const app = express();
const port = 3014;
app.use(cors());
const auth = (options) => {
const requirePassword = options?.requirePassword == false ? false : true
return async (req, res, next) => {
const authHeader = req.headers.authorization;
if(!authHeader || !authHeader.startsWith("Basic ")) {
return res.json({
ok: false,
msg: "missing authorization header"
});
}
const [username, password] = Buffer.from(authHeader.replace("Basic ", ""), "base64").toString().split(":");
if(!users[username]) {
return res.json({
ok: false,
msg: "user doesn't exist"
});
}
if(requirePassword && !(await bcrypt.compare(password, users[username].passwordHash))) {
return res.json({
ok: false,
msg: "wrong password"
});
}
req.user = username;
req.stead = users[username].stead
next();
}
}
app.get('/', (req, res) => {
res.send('Hello World!')
})
/* exclusive of max */
const randrange = (min, max) => min + Math.floor(Math.random() * (max - min));
const choose = arr => arr[Math.floor(Math.random() * arr.length)];
const XP_PER_SEC = 10;
const lvls = [
120, 280, 480,
720, 1400, 1700, 2100,
2700, 3500, 6800, 7700,
8800, 10100, 11600, 22000,
24000, 26500, 29500, 33000,
37000, 41500, 46500, 52000,
99991,
];
const lvlFromXp = xp => {
let i = 0;
while (xp > 0 && i < lvls.length) xp -= lvls[i++];
return { xp_to_go: Math.abs(xp), lvl: i };
};
const xpPerYield = xp => {
const { lvl } = lvlFromXp(xp);
return 900*(1 - (lvl / (lvls.length + 2)));
};
const takeItem = (stead, needs) => {
for (const item in needs)
if (!stead.inv[item] || stead.inv[item] < needs[item])
return false;
for (const item in needs)
stead.inv[item] -= +needs[item];
return true;
};
const giveItem = (stead, items) => {
for (const item in items) {
stead.inv[item] ??= 0;
stead.inv[item] += +items[item];
}
};
const plantStatuses = (stead, plant) => {
const status_items = [ "cyl_item", "bbc_item", "hvv_item" ];
return stead.ephemeral_statuses.concat(
Object.entries(stead.inv)
.filter(([kind, n]) => status_items.includes(kind))
.flatMap(([kind, n]) => [...Array(n)].map(x => ({ kind, xp_multiplier: 1.5 })))
.filter(status => {
const c3 = str => str.substr(0, 3);
return (["hvv", "bbc", "cyl"].includes(c3(status.kind)) &&
c3(status.kind) == c3(plant.kind)) ;
})
);
}
const plantXpMultiplier = (stead, plant) => {
let xp_multiplier = 1;
for (const status of plantStatuses(stead, plant))
xp_multiplier += status.xp_multiplier;
return xp_multiplier;
};
const makeStead = (passwordHash) => ({
passwordHash,
stead: {
ephemeral_statuses: [],
plants: [
{
kind: "dirt",
id: 0,
xp: 0,
},
],
inv: {
"nest_egg": 1,
"bbc_seed": 1,
"hvv_seed": 1,
"cyl_seed": 1,
}
},
webhooks: [],
});
let users = {};
try {
users = JSON.parse(await readFile("users.json"))
} catch(e) {}
let activity = [];
const activityPush = (kind, obj) => {
console.log(kind, obj);
const action = { ts: Date.now(), kind, ...obj };
activity.push(action);
// Send webhooks
for (const url of getWebhooks()) {
sendWebhook(url, action);
}
};
const activityPrune = () => {
const fivemin = 1000 * 60 * 5;
activity = activity.filter(t => (Date.now() - t.ts) < fivemin);
};
const registerWebhook = async (user, url) => {
let cleanUrl;
try {
cleanUrl = new URL(url).toString();
} catch (e) {
return false;
}
// Webhook already exists
if (getWebhooks().includes(cleanUrl)) return true;
// Verify webhook is valid
const ok = await sendWebhook(cleanUrl, {
message:
'This is a validation test for hkgi webhooks. This endpoint must return a 200 status code to be registered as a webhook url.',
});
if (!ok) return false;
// Register webhook
if (!users[user].webhooks) users[user].webhooks = [];
users[user].webhooks.push(cleanUrl);
return true;
};
const deleteWebhook = (user, url) => {
if (!users[user].webhooks) return false;
let cleanUrl;
try {
cleanUrl = new URL(url).toString();
} catch (e) {
return false;
}
const i = users[user].webhooks.indexOf(cleanUrl);
if (i == -1) return false;
users[user].webhooks.splice(i, 1);
return true;
};
const sendWebhook = async (url, data) => {
try {
console.log('sending webhook', url, data);
const resp = await axios.post(url, data);
const ok = resp.status >= 200 && resp.status < 300;
return ok;
} catch (e) {
return false;
}
};
const getWebhooks = () => {
return Object.values(users)
.map((u) => u.webhooks)
.flat()
.filter((u) => u);
};
const SECS_PER_TICK = 0.5;
setInterval(() => {
writeFile("users.json", JSON.stringify(users))
for(const user in users) {
const stead = users[user].stead;
stead.ephemeral_statuses = stead.ephemeral_statuses.filter(status => {
status.tt_expire -= SECS_PER_TICK * 1000;
return (status.tt_expire > 0);
});
for (const plant of stead.plants) {
if (plant.kind == "dirt") continue;
let xp_multiplier = plantXpMultiplier(stead, plant);
/* I don't trust this logic to work with multiplier > 1 */
for (let i = xp_multiplier; i > 0; i--) {
const mult = Math.min(i, 1.0);
const xp_per_tick = XP_PER_SEC * SECS_PER_TICK;
const xppy = xpPerYield(plant.xp);
plant.xp += xp_per_tick * mult;
const xp_since_yield = plant.xp % xppy;
if (xp_since_yield <= xp_per_tick) {
giveItem(stead, { [plant.kind + "_essence"]: 1 });
if (Math.random() < 0.004) {
const { lvl } = lvlFromXp(plant.xp);
if (lvl > 5) giveItem(stead, { [plant.kind + "_bag_t1"]: 1 });
}
}
}
}
}
activityPrune();
}, SECS_PER_TICK*1000);
const serializeStead = stead => {
const sPlant = (plant) => {
const { kind, xp } = plant;
if (kind == "dirt") return { kind: "dirt", lvl: 0 };
const { lvl, xp_to_go } = lvlFromXp(xp);
const xppy = xpPerYield(xp);
const xpm = plantXpMultiplier(stead, plant);
return {
statuses: plantStatuses(stead, plant).map(status => {
const ret = {};
ret.kind = status.kind;
ret.xp_multiplier = status.xp_multiplier;
if (status.tt_expire) {
ret.expire_prog = status.tt_expire / status.tt_expire_max;
ret.tt_expire = status.tt_expire;
}
return ret;
}),
kind,
lvl,
tt_yield: (xppy - (xp%xppy)) / XP_PER_SEC * 1000 / xpm,
yield_prog: (xp%xppy) / xppy,
tt_lvlup: xp_to_go / XP_PER_SEC * 1000 / xpm,
lvlup_prog: xp_to_go / lvls[lvl]
};
};
return {
plants: stead.plants.map(sPlant),
inv: stead.inv,
};
};
app.get('/getstead', auth({ requirePassword: false }), (req, res) => {
return res.json(serializeStead(req.stead));
});
app.get('/activity', (req, res) => res.json(activity));
app.get('/users', (req, res) => res.json(Object.keys(users)));
const manifest = {
items: {
"bbc_item": { name: "Rolling Pin",
desc: "Makes all your bracti grow more! Probably ethical!" },
"bbc_egg": { name: "Bread Egg",
usable: true,
desc: "Contains hacker and coffee themed goodies, maybe dirt!" },
"bbc_compressence": { name: "bressence",
desc: "EGGREDIENT. More bread than is safe to consume at once." },
"bbc_essence": { name: "Bread Essence",
desc: "COMPRESSABLE. Baked by a cactus. Tad undercooked." },
"bbc_seed": { name: "Bractus Seed",
desc: "Doughy, yet somehow still prickly." },
"hvv_item": { name: "VINEB0RD",
desc: "Makes all your HVVs grow more! CLACK. CL4CK. CLACK." },
"hvv_egg": { name: "H4CKER 3GG",
usable: true,
desc: "Contains bread and coffee themed goodies, maybe dirt!" },
"hvv_compressence": { name: "hacksprit",
desc: "EGGREDIENT. Hacker Spirit compressed with duct tape!" },
"hvv_essence": { name: "H4CK3R SP1RIT",
desc: "COMPRESSABLE. Makes you wanna make a thing! " },
"hvv_seed": { name: "HVV S33D",
desc: "Grows into 1337 h4x0r v1n3!" },
"cyl_item": { name: "Cyl Wand",
desc: "Makes all your cyls grow more! Magic coffee stirrer!" },
"cyl_egg": { name: "Cyl Egg",
usable: true,
desc: "Contains bread and hacker themed goodies, maybe dirt!" },
"cyl_compressence": { name: "crystcyl",
desc: "EGGREDIENT. Hums faintly. Can roast marshmallows!" },
"cyl_essence": { name: "Cyl Crystal",
desc: "COMPRESSABLE. Glows with aromatic orange energy!" },
"cyl_seed": { name: "Cyl Seed",
desc: "Do normal coffee beans glow orange in the dark?" },
"nest_egg": { name: "Nest Egg",
usable: true,
desc: "OPEN ME. Some stuff to help you get started :)" },
"powder_t1": { name: "Warp Powder",
usable: true,
desc: "Sparkles, glitters, glows, accelerates plant growth!" },
"powder_t2": { name: "Rift Powder",
usable: true,
desc: "x10 better Warp Powder! Rips open space time." },
"powder_t3": { name: "Wormhole Powder",
usable: true,
desc: "x100 better Warp Powder! May summon time worm." },
"land_deed": { name: "Land Deed",
usable: true,
desc: "Lets you grow more things! Basically dirt paper!" },
/* bagling update */
"cyl_bag_t1": { name: "Crystalline Buzzwing Bagling",
desc: "Bag. Orange. Small. Flies. Seems a bit seedy." },
"hvv_bag_t1": { name: "Spirited Buzzwing Bagling",
desc: "Bag. Green. Small. Flies. Seems a bit seedy." },
"bbc_bag_t1": { name: "Doughy Buzzwing Bagling",
desc: "Bag. Brown. Small. Flies. Seems a bit seedy." },
"cyl_bag_t2": { name: "Crystalline Buzzwing Boxlet",
desc: "Box. Orange. Small. Flies. Seems seedy." },
"hvv_bag_t2": { name: "Spirited Buzzwing Boxlet",
desc: "Box. Green. Small. Flies. Seems seedy." },
"bbc_bag_t2": { name: "Doughy Buzzwing Boxlet",
desc: "Box. Brown. Small. Flies. Seems seedy." },
"cyl_bag_t3": { name: "Crystalline Buzzwing Megabox",
desc: "Chest. Orange. Winged. Too fat to fly. Quite seedy." },
"hvv_bag_t3": { name: "Spirited Buzzwing Megabox",
desc: "Chest. Green. Winged. Too fat to fly. Quite seedy." },
"bbc_bag_t3": { name: "Doughy Buzzwing Megabox",
desc: "Chest. Brown. Winged. Too fat to fly. Quite seedy." },
"bag_egg_t1": { name: "Bagling Egg",
usable: true,
desc: "Boons birthed of a Bagling!" },
"bag_egg_t2": { name: "Boxlet Egg",
usable: true,
desc: "Boons birthed of a Boxlet! Bountiful!" },
"bag_egg_t3": { name: "Megabox Egg",
usable: true,
desc: "The best boons born by a winged vessel!" },
},
plant_titles: {
"dirt": "Dirt",
"bbc": "Bractus",
"cyl": "Coffea Cyl Plant",
"hvv": "H4CK3R V1B3Z V1N3",
},
plant_recipes: (() => {
const trioPlant = (slug, frens) => {
const compressence = {
lvl: 0,
needs: { [slug + "_essence"]: 5 },
make_item: slug + "_compressence"
};
const egg = { lvl: 0, needs: {}, change_plant_to: "dirt", make_item: slug + "_egg" };
egg.needs[frens[0] + "_compressence"] = 4;
egg.needs[frens[1] + "_compressence"] = 4;
const bag_t1 = {
lvl: 14,
needs: { [slug + "_compressence"]: 10, [slug + "_bag_t1"]: 3 },
make_item: {
one_of: [
[0.40, "bag_egg_t1"],
[0.18, frens[0] + "_bag_t2"],
[0.18, frens[1] + "_bag_t2"],
[0.12, frens[0] + "_bag_t1"],
[0.12, frens[1] + "_bag_t1"],
]
}
};
const bag_t2 = {
lvl: 17,
needs: { [slug + "_compressence"]: 50, [slug + "_bag_t2"]: 2 },
make_item: {
one_of: [
[0.32, "bag_egg_t2"],
[0.15, frens[0] + "_bag_t3"],
[0.15, frens[1] + "_bag_t3"],
[0.19, frens[0] + "_bag_t2"],
[0.19, frens[1] + "_bag_t2"],
]
}
};
const bag_t3 = {
lvl: 21,
needs: { [slug + "_compressence"]: 200, [slug + "_bag_t3"]: 1 },
make_item: {
one_of: [
[0.16, "bag_egg_t3"],
[0.42, frens[0] + "_bag_t3"],
[0.42, frens[1] + "_bag_t3"],
]
}
};
return [compressence, egg, bag_t1, bag_t2, bag_t3];
};
return {
"dirt": [
{ lvl: 0, needs: { "bbc_seed": 1 }, change_plant_to: "bbc" },
{ lvl: 0, needs: { "hvv_seed": 1 }, change_plant_to: "hvv" },
{ lvl: 0, needs: { "cyl_seed": 1 }, change_plant_to: "cyl" },
],
bbc: trioPlant("bbc", [ "cyl", "hvv"]),
cyl: trioPlant("cyl", ["bbc", "hvv"]),
hvv: trioPlant("hvv", ["bbc", "cyl", ]),
};
})(),
};
app.get('/manifest', (req, res) => {
return res.json(manifest);
});
app.use(express.json());
app.post('/useitem', auth(), (req, res) => {
const stead = users[req.user].stead;
const { item } = req.body;
if (item == undefined)
return res.json({ ok: false, msg: "learn how to use the api noob" });
if (manifest.items[item] == undefined)
return res.json({ ok: false, msg: "no such item found" });
if (!manifest.items[item].usable)
return res.json({ ok: false, msg: "that's not an item you can use!" });
if (!takeItem(stead, { [item]: 1 }))
return res.json({ ok: false, msg: "you can't afford that!" });
console.log("pretend I'm using an " + item);
activityPush('useitem', { who: req.user, what: item });
const megaboxDrop = () => ({
[choose([ "bbc_seed", "hvv_seed", "cyl_seed" ])]:
choose([ 33, 33, 33, 33, 33,
38, 38, 38, 38, 38,
45, 45, 45, 45, 45,
87.0 ]),
powder_t2: choose([3, 3, 3, 3, 3, 3, 3, 5, 5, 5, 5, 8]),
powder_t3: choose([0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 3])
});
const scaleDrop = (drop, scale) => {
for (const key in drop)
drop[key] = Math.floor(drop[key] * scale);
return drop;
}
if (item == "bag_egg_t1") giveItem(stead, scaleDrop(megaboxDrop(), 0.2));
if (item == "bag_egg_t2") giveItem(stead, scaleDrop(megaboxDrop(), 0.6));
if (item == "bag_egg_t3") giveItem(stead, megaboxDrop() );
if (item == "bbc_egg" ||
item == "hvv_egg" ||
item == "cyl_egg") {
const invert_type = {
bbc_egg: ["hvv_item", "cyl_item"],
cyl_egg: ["bbc_item", "hvv_item"],
hvv_egg: ["cyl_item", "bbc_item"],
}
let reward;
const random = Math.random();
if (random < 0.1)
reward = { land_deed: 1 };
else if (random < (0.1 + 0.4))
reward = { [choose(invert_type[item])]: 1 };
else if (random < (0.1 + 0.4 + 0.05))
reward = { powder_t3: 1 };
else
reward = { powder_t2: 1 };
giveItem(stead, reward);
}
if (item == "nest_egg") {
giveItem(stead, {
"powder_t1": choose([4, 5, 5, 5, 5, 6, 6, 6, 7, 7, 8]),
"powder_t2": choose([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]),
"bbc_seed": 4 + choose([0, 0, 0, 0, 0, 1]),
"hvv_seed": 4 + choose([0, 0, 0, 0, 0, 1]),
"cyl_seed": 4 + choose([0, 0, 0, 0, 0, 1]),
"bbc_essence": 5 + choose([0, 0, 0, 1, 1, 2]),
"hvv_essence": 5 + choose([0, 0, 0, 1, 1, 2]),
"cyl_essence": 5 + choose([0, 0, 0, 1, 1, 2]),
});
}
const powder_status = (kind, time_m, xp_m) => {
const ms = 1000 * (50 + Math.random() * 30);
const tt_expire = Math.floor(ms * time_m);
return { kind, xp_multiplier: 2 * xp_m, tt_expire, tt_expire_max: tt_expire };
}
if (item == "powder_t1") stead.ephemeral_statuses.push(powder_status(item, 1 , 1 ));
if (item == "powder_t2") stead.ephemeral_statuses.push(powder_status(item, 1.2 , 8.5));
if (item == "powder_t3") stead.ephemeral_statuses.push(powder_status(item, 1.35, 75 ));
if (item == "land_deed")
stead.plants.push({
kind: "dirt",
xp: 0,
});
res.json({ ok: true });
});
app.post('/craft', auth(), (req, res) => {
const stead = users[req.user].stead;
if (req.body.recipe_index == undefined || req.body.plot_index == undefined)
return res.json({ ok: false, msg: "learn how to use the api noob" });
const { recipe_index, plot_index } = req.body;
const plant = stead.plants[plot_index];
const recipe = manifest.plant_recipes[plant.kind][recipe_index];
if (!(lvlFromXp(plant.xp).lvl >= recipe.lvl))
return res.json({ ok: false, msg: "come back when you're older, plant!" });
/* TODO: make sure you actually have the necessary items and fail gracefully */
if (!takeItem(stead, recipe.needs))
return res.json({ ok: false, msg: "you can't afford that!" });
activityPush('craft', { who: req.user, what: { recipe_index, plot_index } });
if (recipe.change_plant_to)
stead.plants[plot_index] = { kind: recipe.change_plant_to, xp: 0 };
if (recipe.make_item) {
if (typeof recipe.make_item == "string")
giveItem(stead, { [recipe.make_item]: 1 });
else {
const one_of = [...recipe.make_item.one_of];
let r = Math.random();
let chance, item;
do {
[chance, item] = one_of.shift();
r -= chance;
} while (r > 0);
giveItem(stead, { [item]: 1 });
}
}
return res.json({ ok: true });
});
app.post('/signup', async (req, res) => {
const { user, pass } = req.body;
/* important decoration (DO NOT DELETE) */
// : "$2b$10$9vl9sbBwTAus1hxbdYjsSeY.OsQFW.yX64kTeR1atekJiB89L1Yve",
if (user == "") return res.json({ ok: false, msg: "nah mate not having that username" });
if (user in users) return res.json({ ok: false, msg: "already done diddly doned it" });
const pwhash = await bcrypt.hash(pass, 10);
users[user] = makeStead(pwhash);
activityPush('signup', { who: user });
res.json({ ok: true });
});
app.post('/gib', auth(), (req, res) => {
const { person, item, amount } = req.body;
if (person == undefined) return res.json({ ok: false, msg: "no person to gib" });
if (item == undefined) return res.json({ ok: false, msg: "no item to gib" });
if (amount == undefined) return res.json({ ok: false, msg: "no amount to gib" });
if (!(person in users)) return res.json({ ok: false, msg: "who dat?" });
const needs = { [item]: amount };
const stead = users[req.user].stead;
if (!takeItem(stead, needs)) return res.json({ ok: false, msg: "you can't afford that!" });
giveItem(users[person].stead, needs);
activityPush('gib', { from: req.user, to: person, item, amount });
return res.send({ ok: true });
});
app.post('/testauth', auth(), (req, res) => {
res.json({ ok: true });
});
app.post('/webhook/register', auth(), async (req, res) => {
const { url } = req.body;
if (url == undefined)
return res.json({ ok: false, msg: 'You must provide a "url" in the body' });
const { user } = req;
const registered = await registerWebhook(user, url);
if (!registered)
return res.json({
ok: false,
msg: 'Webhook url is invalid. The endpoint must return a 200 status code when receiving a POST request to be registered as a webhook url.',
});
res.json({ ok: true });
});
app.delete('/webhook', auth(), (req, res) => {
const user = req.user;
const { url } = req.body;
if (url == undefined)
return res.json({ ok: false, msg: 'You must provide a "url" in the body' });
const deleted = deleteWebhook(user, url);
if (!deleted)
return res.json({ ok: false, msg: 'That webhook url was not registered' });
res.json({ ok: true });
});
app.listen(port, () => {
console.log(`hkgi-ing away on port ${port}`)
})