-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathindex.js
134 lines (118 loc) · 3.66 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
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
require("dotenv").config({ path: "variables.env" });
const express = require("express");
const bodyParser = require("body-parser");
const path = require("path");
const app = express();
var fs = require("fs");
const webpush = require("./service/webpush.service.js");
var players = require("./subscribers/players.json");
var cors = require("cors");
app.use(bodyParser.json());
app.use(cors());
// app.use(express.static(path.join(__dirname, "dist/ballistica-web")));
var request = require("request");
var discordAvailable = false;
if (
process.env.DISCORD_LIVE_CHANNELID &&
process.env.DISCORD_BOT_TOKEN &&
false
) {
discordAvailable = true;
var discord = require("./service/discord.service");
}
const publicVapidKey = process.env.PUBLIC_VAPID_KEY;
const API = "http://140.238.162.79";
const port = 3000;
var stats;
var playersInGame = {};
var leaderboard = {};
var top200 = {};
setInterval(() => {
updateStats();
}, 10 * 1000);
setInterval(() => {
updateLeaderboard();
}, 30 * 60 * 1000);
function updateLeaderboard() {
request(API + "/getLeaderboard", function (err, res, body) {
if (!err && res.statusCode == 200) {
leaderboard = JSON.parse(body);
// discord.updateLeaderboard(leaderboard);
}
});
request(API + "/getTop200", function (err, res, body) {
if (!err && res.statusCode == 200) {
top200 = JSON.parse(body);
}
});
// discord.updateLeaderboard(leaderboard);
}
updateLeaderboard(); //call it once on server boot , then scheduler will handle it
function updateStats() {
request(API + "/getStats", function (err, res, body) {
if (!err && res.statusCode == 200) {
data = JSON.parse(body);
// console.log(data)
// discord.updateStats(data);
stats = data;
livePlayers = data["roster"];
processSubscription(livePlayers);
}
});
}
updateStats();
function processSubscription(livePlayers) {
for (player in livePlayers) {
if (!(player in playersInGame)) {
// so new player joined the server
if (player in players) {
//someone subscribed to this player
last_seen = new Date(players[player]["last_seen"]);
now = new Date();
diff = (now - last_seen) / (1000 * 60); //in minutes
if (diff >= 60) {
//ok he playing back after an hour
webpush.notifyFor(player); // inform his subscribers , that he is playinng now
if (discordAvailable) discord.notifyFor(player);
}
//update his last seen
players[player]["last_seen"] = new Date();
fs.writeFile(
"./subscribers/players.json",
JSON.stringify(players, null, 4),
function (err) {
console.log(err);
}
);
}
}
}
playersInGame = livePlayers;
}
// ============= APIs ================================
app.post("/subscribe", (req, res) => {
console.log("received subs request on backend");
const subscription = req.body["subscription"];
webpush.subscribe(subscription, req.body["player_id"], req.body["name"]);
res.status(201).json({});
});
app.get("/live", (req, res) => {
res.status(200).json(stats);
});
app.get("/top200", (req, res) => {
res.status(200).json(top200);
});
app.get("/leaderboard", (re, res) => {
res.status(200).json(leaderboard);
});
app.get("/discord", (req, res) => {
res.status(200).json({ url: process.env.DISCORD_INVITE });
});
app.get("/vapidkey", (req, res) => {
res.status(200).json({ key: process.env.PUBLIC_VAPID_KEY });
});
app.get("/serverName", (req, res) => {
res.status(200).json({ name: process.env.SERVER_NAME });
});
app.listen(port, () => console.log(`Server started at port ${port}`));
app.get("/", (req, res) => res.send("hello world!"));