forked from SkyCryptWebsite/SkyCrypt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapiv2.js
More file actions
180 lines (151 loc) · 4.62 KB
/
Copy pathapiv2.js
File metadata and controls
180 lines (151 loc) · 4.62 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
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
import cors from "cors";
import express from "express";
import sanitize from "mongo-sanitize";
import leaderboard from "../leaderboards.js";
import * as helper from "../helper.js";
import { getCompletePacks } from "../custom-resources.js";
import { db } from "../mongo.js";
import { redisClient } from "../redis.js";
import { router as bazaarRouter } from "./apiv2/bazaar.js";
import { router as coinsRouter } from "./apiv2/coins.js";
import { router as dungeonsRouter } from "./apiv2/dungeons.js";
import { router as leaderboardRouter } from "./apiv2/leaderboard.js";
import { router as profileRouter } from "./apiv2/profile.js";
import { router as slayersRouter } from "./apiv2/slayers.js";
import { router as talismansRouter } from "./apiv2/talismans.js";
import { router as guildRouter } from "./apiv2/guild.js";
const router = express.Router();
router.use(cors());
// Checks if there's an API key and if valid, disables cacheOnly
router.use(async (req, res, next) => {
req.apiKey = false;
if (req.query.key) {
const doc = await db.collection("apiKeys").findOne({ key: sanitize(req.query.key) });
if (doc != null) {
req.apiKey = true;
}
}
req.cacheOnly = !req.apiKey;
req.options = {
cacheOnly: req.cacheOnly,
};
next();
});
/**
* @deprecated Not meant to be used by public.
* @description Endpoint for getting currently processed packs
*
* - Undocumented endpoint
* - Requires API key
*
* @todo Make an endpoint that actually returns just a list of available packs. Because why not.
*/
router.get("/packs", async (req, res) => {
if (req.apiKey) {
helper.sendMetric("endpoint_apiv2_packs_success");
res.json(getCompletePacks());
} else {
helper.sendMetric("endpoint_apiv2_packs_fail");
res.status(404).json({ error: "This endpoint isn't available to the public." });
}
});
/**
* @deprecated Not meant to be used by public.
* @description Endpoint for getting all available leaderboards.
*
* - Undocumented endpoint
*
* @todo Remake how leaderboards work in their entirety.
*/
router.get("/leaderboards", async (req, res) => {
helper.sendMetric("endpoint_apiv2_leaderboards");
res.json(leaderboards);
});
// Routes for all available /api/v2 endpoints. Duh.
router.use("/bazaar", bazaarRouter);
router.use("/coins", coinsRouter);
router.use("/dungeons", dungeonsRouter);
router.use("/leaderboard", leaderboardRouter);
router.use("/profile", profileRouter);
router.use("/slayers", slayersRouter);
router.use("/talismans", talismansRouter);
router.use("/guild", guildRouter);
// Handler of non-existing endpoints
router.get("/*", async (req, res) => {
helper.sendMetric("endpoint_apiv2_fail_notfound");
handleError(res, new Error("Endpoint was not found."), 404, false);
});
// Handler of unsupported methods
router.all("/*", async (req, res) => {
helper.sendMetric("endpoint_apiv2_fail_onlyget");
handleError(res, new Error("API v2 only supports GET requests."), 405, false);
});
// Error handler for all /api/v2 endpoints
// Meant to be a safenet if some endpoint returns an error.
router.use((err, req, res, next) => {
helper.sendMetric("endpoint_apiv2_fail");
handleError(res, err);
});
export const productInfo = {};
export const leaderboards = [];
/**
* Prepares productInfo for /api/v2/bazaar
* @returns void
*/
async function prepareProductInfo() {
try {
const bazaarProducts = await db.collection("bazaar").find().toArray();
const itemInfo = await db
.collection("items")
.find({ id: { $in: bazaarProducts.map((a) => a.productId) } })
.toArray();
for (const product of bazaarProducts) {
const info = itemInfo.filter((a) => a.id == product.productId);
if (info.length > 0) {
productInfo[product.productId] = info[0];
}
}
return;
} catch (e) {
console.error(e);
}
}
/**
* Prepares leaderboards for /api/v2 leaderboard endpoints
* @returns void
*/
async function prepareLeaderboards() {
try {
const keys = await redisClient.keys("lb_*");
for (const key of keys) {
const lb = leaderboard(key);
if (lb.mappedBy == "uuid" && !lb.key.startsWith("collection_enchanted")) {
leaderboards.push(lb);
}
}
leaderboards.sort((a, b) => {
return a.key.localeCompare(b.key);
});
return;
} catch (e) {
console.error(e);
}
}
/**
* Initializes prepare functions.
* @returns void
*/
export async function init() {
await prepareProductInfo();
await prepareLeaderboards();
return;
}
export function handleError(res, err, status = 500, logged = true) {
if (logged) {
console.error(err);
}
res.status(status).json({
error: err.message,
});
}
export { router };