-
Notifications
You must be signed in to change notification settings - Fork 5
/
server.js
85 lines (74 loc) · 2.16 KB
/
server.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
const express = require('express');
const fs = require('fs');
const moment = require('moment-timezone');
const app = express();
app.set('view engine', 'ejs');
app.use(express.static('public'));
function truncateLongNames(ranks) {
ranks.forEach((player, i) => {
const maxNameLength = 20;
if (player.name.length > maxNameLength) {
ranks[i].name = player.name.substring(0, maxNameLength);
}
});
return ranks;
}
function loadUpdateData() {
const fileContents = fs.readFileSync('public/update.json');
const json = JSON.parse(fileContents);
return json;
}
app.get('/', (req, res) => {
const data = loadUpdateData();
const t = moment.unix(data.timestamp);
res.render('index', {
numRankings: data.ranks.length,
pageName: 'Home',
pvpZone: data.pvpZone,
ranks: truncateLongNames(data.ranks).slice(0, 10),
updateDuration: t.from(moment()),
updateTime: t.tz('America/Los_Angeles').format('MMM Do h:mm A'),
});
});
app.get('/rankings', (req, res) => {
const data = loadUpdateData();
const t = moment.unix(data.timestamp);
res.render('rankings', {
numRankings: data.ranks.length,
pageName: 'Rankings',
ranks: truncateLongNames(data.ranks),
updateDuration: t.from(moment()),
updateTime: t.tz('America/Los_Angeles').format('MMM Do h:mm A'),
});
});
app.get('/map', (req, res) => {
const data = loadUpdateData();
const t = moment.unix(data.timestamp);
res.render('map', {
pageName: 'Map',
pvpZone: data.pvpZone,
updateDuration: t.from(moment()),
updateTime: t.tz('America/Los_Angeles').format('MMM Do h:mm A'),
});
});
app.get('/join', (req, res) => {
const data = loadUpdateData();
const t = moment.unix(data.timestamp);
res.render('join', {
pageName: 'Join',
updateDuration: t.from(moment()),
updateTime: t.tz('America/Los_Angeles').format('MMM Do h:mm A'),
});
});
app.get('/about', (req, res) => {
const data = loadUpdateData();
const t = moment.unix(data.timestamp);
res.render('about', {
pageName: 'About',
updateDuration: t.from(moment()),
updateTime: t.tz('America/Los_Angeles').format('MMM Do h:mm A'),
});
});
port = 80
app.listen(port);
console.log(`Serving on port ${port}`);