-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi.js
More file actions
109 lines (94 loc) · 4.11 KB
/
api.js
File metadata and controls
109 lines (94 loc) · 4.11 KB
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
require('dotenv').config();
const express = require('express');
const { getProfile, getAllItems, getStats, getPrices, getAuctions, formatDetailedNetworth } = require('./util');
const app = express();
if (process.env.SCAMMER_API) {
const { scammer_collection } = require('./database');
const { apiKey } = require('./middleware');
app.get('/scammer', apiKey, async (req, res) => {
const doc = await scammer_collection.findOne({ _id: req.query.uuid });
if (!doc) return res
.status(404)
.json({ scammer: false });
res
.status(200)
.json({
scammer: true,
uuid: doc._id,
reason: doc.reason,
operated_staff: doc.mod,
});
});
}
app.get('/prices', (req, res) => {
res.json(getPrices());
})
app.get('/auctions', (req, res) => {
res.json({ auctions: getAuctions() });
})
app.get('/stats/:profile', async (req, res) => {
try {
const { profile, allProfiles } = await getProfile(req.params.profile, null, req.query.key, { cacheOnly: req.cacheOnly });
const output = { profiles: {} };
for (const singleProfile of allProfiles) {
if (singleProfile.members[profile.uuid] == undefined) allProfiles.splice(allProfiles.indexOf(singleProfile), 1);
}
for (const singleProfile of allProfiles) {
const userProfile = singleProfile.members[profile.uuid];
if (userProfile == undefined) {
allProfiles.splice(allProfiles.indexOf(singleProfile), 1);
continue
};
const items = await getAllItems(userProfile, req.query.pack);
const data = await getStats(singleProfile, allProfiles, items);
const networth = {}
const detailed_networth = {};
const NETWORTH_INVS = ["armor", "wardrobe_inventory", "inventory", "enderchest", "talisman_bag", "fishing_bag", "quiver", "potion_bag", "backpacks"];
Object.keys(items).forEach(inv => {
if (NETWORTH_INVS.includes(inv)) {
networth[inv] = 0;
for (let item of items[inv]) {
networth[inv] += item.coin_value || 0;
}
detailed_networth[inv] = formatDetailedNetworth(items[inv]);
}
});
networth.pets = data.pets.reduce((a, b) => a + (b.coin_value ? b.coin_value : 0), 0);
detailed_networth.pets = formatDetailedNetworth(data.pets, x => `→ [lvl ${x.level.level}] ${x.display_name} - ${x.coin_value.toLocaleString()}`);
networth.total = Object.values(networth).reduce((a, b) => a + b, 0);
const stats = data.stats;
output.success = true;
output.profiles[singleProfile.profile_id] = {
profile_id: singleProfile.profile_id,
cute_name: singleProfile.cute_name,
current: Math.max(...allProfiles.map(a => a.members[profile.uuid].last_save)) == userProfile.last_save,
last_save: userProfile.last_save,
// raw: userProfile,
// items,
networth,
detailed_networth,
data: {
stats
}
};
}
res.json(output);
} catch (e) {
console.log(e);
// if (e == "Request to Hypixel API failed. Please try again!") {
// res.json({"success": false, "message": "invalid api key/hypixel api error"})
// }
switch (e) {
case "Request to Hypixel API failed. Please try again!":
res.json({ "success": false, "message": "invalid api key/hypixel api error" })
break;
case "must be uuid":
res.json({ "success": false, "message": "invalid uuid format" })
// case "Player has no SkyBlock profiles":
// res.json({"success": false, "message": "invalid uuid"})
default:
break;
}
}
});
app.listen(3000, () => console.log('api listening on port 3000'));