aboutsummaryrefslogtreecommitdiff
path: root/build/constants.js
diff options
context:
space:
mode:
authormat <27899617+mat-1@users.noreply.github.com>2021-02-28 01:23:18 -0600
committermat <27899617+mat-1@users.noreply.github.com>2021-02-28 01:23:18 -0600
commit6dadf95683a8b8574976c9d024b0b148521012f7 (patch)
treeedd88502959e8b74c76d47a82a5b98a646b26eb4 /build/constants.js
parent78198ac4812f6f33f412bdc62216567aa08d8199 (diff)
downloadskyblock-api-6dadf95683a8b8574976c9d024b0b148521012f7.tar.gz
skyblock-api-6dadf95683a8b8574976c9d024b0b148521012f7.tar.bz2
skyblock-api-6dadf95683a8b8574976c9d024b0b148521012f7.zip
Add leaderboards
Diffstat (limited to 'build/constants.js')
-rw-r--r--build/constants.js111
1 files changed, 111 insertions, 0 deletions
diff --git a/build/constants.js b/build/constants.js
new file mode 100644
index 0000000..23d2b75
--- /dev/null
+++ b/build/constants.js
@@ -0,0 +1,111 @@
+"use strict";
+/**
+ * Fetch and edit constants from the skyblock-constants repo
+ */
+var __importDefault = (this && this.__importDefault) || function (mod) {
+ return (mod && mod.__esModule) ? mod : { "default": mod };
+};
+Object.defineProperty(exports, "__esModule", { value: true });
+exports.addStats = exports.fetchStats = void 0;
+const node_fetch_1 = __importDefault(require("node-fetch"));
+const https_1 = require("https");
+const node_cache_1 = __importDefault(require("node-cache"));
+const httpsAgent = new https_1.Agent({
+ keepAlive: true
+});
+const githubApiBase = 'https://api.github.com';
+const owner = 'skyblockstats';
+const repo = 'skyblock-constants';
+/**
+ * Send a request to the GitHub API
+ * @param method The HTTP method, for example GET, PUT, POST, etc
+ * @param route The route to send the request to
+ * @param headers The extra headers
+ * @param json The JSON body, only applicable for some types of methods
+ */
+async function fetchGithubApi(method, route, headers, json) {
+ return await node_fetch_1.default(githubApiBase + route, {
+ agent: () => httpsAgent,
+ body: json ? JSON.stringify(json) : null,
+ method,
+ headers: Object.assign({
+ 'Authorization': `token ${process.env.github_token}`
+ }, headers),
+ });
+}
+// cache files for a day
+const fileCache = new node_cache_1.default({
+ stdTTL: 60 * 60 * 24,
+ checkperiod: 60,
+ useClones: false,
+});
+/**
+ * Fetch a file from skyblock-constants
+ * @param path The file path, for example stats.json
+ */
+async function fetchFile(path) {
+ if (fileCache.has(path))
+ return fileCache.get(path);
+ const r = await fetchGithubApi('GET', `/repos/${owner}/${repo}/contents/${path}`, { 'Accept': 'application/vnd.github.v3+json' });
+ const data = await r.json();
+ return {
+ path: data.path,
+ content: Buffer.from(data.content, data.encoding).toString(),
+ sha: data.sha
+ };
+}
+/**
+ * Edit a file on skyblock-constants
+ * @param file The GithubFile you got from fetchFile
+ * @param message The commit message
+ * @param newContent The new content in the file
+ */
+async function editFile(file, message, newContent) {
+ fileCache.set(file.path, newContent);
+ await fetchGithubApi('PUT', `/repos/${owner}/${repo}/contents/${file.path}`, { 'Content-Type': 'application/json' }, {
+ message: message,
+ content: Buffer.from(newContent).toString('base64'),
+ sha: file.sha,
+ branch: 'main'
+ });
+}
+/** Fetch all the known SkyBlock stats as an array of strings */
+async function fetchStats() {
+ const file = await fetchFile('stats.json');
+ try {
+ return JSON.parse(file.content);
+ }
+ catch {
+ // probably invalid json, return an empty array
+ return [];
+ }
+}
+exports.fetchStats = fetchStats;
+/** Add stats to skyblock-constants. This has caching so it's fine to call many times */
+async function addStats(addingStats) {
+ if (addingStats.length === 0)
+ return; // no stats provided, just return
+ const file = await fetchFile('stats.json');
+ if (!file.path)
+ return;
+ let oldStats;
+ try {
+ oldStats = JSON.parse(file.content);
+ }
+ catch {
+ // invalid json, set it as an empty array
+ oldStats = [];
+ }
+ const updatedStats = oldStats
+ .concat(addingStats)
+ // remove duplicates
+ .filter((value, index, array) => array.indexOf(value) === index)
+ .sort((a, b) => a.localeCompare(b));
+ const newStats = updatedStats.filter(value => !oldStats.includes(value));
+ // there's not actually any new stats, just return
+ if (newStats.length === 0)
+ return;
+ const commitMessage = newStats.length >= 2 ? `Add ${newStats.length} new stats` : `Add '${newStats[0]}'`;
+ await editFile(file, commitMessage, JSON.stringify(updatedStats, null, 2));
+}
+exports.addStats = addStats;