-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
70 lines (66 loc) · 3.29 KB
/
index.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
const express = require("express");
const app = express();
const fs = require("fs");
const config = require("./config.json");
const SplatAPI = require("./structures/SplatAPI.js");
const pkgfile = require("./package.json");
new SplatAPI(config.splatoon.iksm_token, config.splatoon.base_url);
const Database = require("./structures/Database.js");
new Database();
const moment = require("moment");
const morgan = require("morgan");
app.use(morgan("dev"));
app.set("json spaces", 4);
let updatePosted = false;
async function updateStats() {
config.endpoints = { map_schedules: await SplatAPI.getSchedules(), coop_schedules: await SplatAPI.getCoopSchedules(), stages: await SplatAPI.getStages(), merchandises: await SplatAPI.getMerch(), festivals: { active: await SplatAPI.getFestivals("active"), pasts: await SplatAPI.getFestivals("pasts") } };
config.endpoints.current_map_schedule = SplatAPI.currentMatches(config.endpoints.map_schedules);
config.endpoints.current_coop_schedule = SplatAPI.currentCoop(config.endpoints.coop_schedules);
// Checking for available API updates
if (!updatePosted) {
require("node-fetch")("https://raw.githubusercontent.com/splat2api/api/master/package.json").then(res => res.json()).then(res => {
if (res.version > pkgfile.version) {
console.log(`\n>>> There is a new update out for the API! Update to version ${res.version} now by running git pull. <<<`);
updatePosted = true;
};
});
};
return "ok";
};
app.listen(config.port, async () => {
require('child_process').exec("node build-docs.js"); // Updating documentation every time the script is started.
console.log("Listening on port " + config.port + ".");
await updateStats();
let newex = false;
let juststarted = true;
let routes = fs.readdirSync("./routes/");
if (!routes[0]) {
console.log("No routes detected. Quitting process...");
process.exit(0);
};
console.log("Endpoints:");
for (let file of routes) {
var f = require("./routes/" + file);
if (f[0]) {
for (var use of f) {
if (!use.method) { use.method = "get" }
app[use.method.toLowerCase()](use.name, use.export);
console.log(" " + use.name + " (" + use.method.toUpperCase() + ")");
};
};
};
setInterval(async () => {
const MapSets = await Database.Maps.findOne();
if (!MapSets) { newex = true; await Database.Maps.create({ latest: 100 }); }
const Maps = await Database.Maps.findOne({ where: { latest: { [require("sequelize").Op.lte] : Date.now() }}});
if (!Maps && juststarted) { juststarted = false }
if (!Maps) return;
let newtime = new Date(moment(config.endpoints.current_map_schedule.regular.end_time*1000).add(2, "hours")).getTime();
if (newex) { newtime = config.endpoints.current_map_schedule.regular.end_time*1000; newex = false }
if (juststarted) { newtime = config.endpoints.current_map_schedule.regular.end_time*1000 }
await Database.Maps.update({ latest: newtime }, { where: { id: Maps.id }});
if (juststarted) { juststarted = false; return }
console.log(moment().format("LLL") + " > Updating information...");
await updateStats();
}, 1000);
});