-
Notifications
You must be signed in to change notification settings - Fork 1
/
web.js
92 lines (90 loc) · 3.51 KB
/
web.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
const express = require('express');
const bodyParser = require('body-parser');
const stats = require(`revive-stats.js`)
let app = express();
app.use(bodyParser.json())
app.all('*', function (req, res, next) {
res.setHeader("Access-Control-Allow-Origin", "*");
return next();
});
app.get('/:game/getplayer', function (req, res) {
let game;
if (req.params.game.toString().trim() === 'bf2')
game = stats.bf2;
else if (req.params.game.toString().trim() === 'bf2142')
game = stats.bf2142;
if (!game) {
res.end("{\"error\":\"" + "INVALID GAME" + "\"}", () => console.log("An Invalid Game was provided by " + (req.headers['x-forwarded-for'] || req.connection.remoteAddress)));
return;
}
//console.log("Executing getplayer " + req.param('pid'));
if (!req.query['pid']) {
res.end("{\"error\":\"" + "INVALID PID" + "\"}", () => console.log("An Invalid PID was provided by " + (req.headers['x-forwarded-for'] || req.connection.remoteAddress)));
}
game.getPlayer(req.query['pid']).then(JSON.stringify).then(js => res.end(js, () => {
console.log("Done");
return;
})).catch(err => {
console.log(err + "\n" + err.stack);
res.end("{\"error\":\"" + err + "\"}");
})
});
app.get('/:game/getplayers', function (req, res) {
console.log("Executing getplayers");
let game;
if (req.params.game.toString().trim() === 'bf2')
game = stats.bf2;
else if (req.params.game.toString().trim() === 'bf2142')
game = stats.bf2142;
if (!game) {
res.end("{\"error\":\"" + "INVALID GAME" + "\"}", () => console.log("An Invalid Game was provided by " + (req.headers['x-forwarded-for'] || req.connection.remoteAddress)));
return;
}
//console.log(req.param('nick'));
if (!req.query['nick']) {
res.end("{\"error\":\"" + "INVALID NICK" + "\"}", () => console.log("An Invalid NICK was provided by " + (req.headers['x-forwarded-for'] || req.connection.remoteAddress)));
}
game.getPlayers(req.query['nick']).then(JSON.stringify).then(p => {
console.log(p);
return p;
}).then(js => res.end(js, () => {
console.log("Done");
return;
})).catch(err => {
console.log(err + "\n" + err.stack);
res.end("{\"error\":\"" + err + "\"}");
})
});
app.get('/:game/getleaderboard', function (req, res) {
console.log("Executing getleaderboard");
let game;
if (req.params.game.toString().trim() === 'bf2')
game = stats.bf2;
else if (req.params.game.toString().trim() === 'bf2142')
game = stats.bf2142;
if (!game) {
res.end("{\"error\":\"" + "INVALID GAME" + "\"}", () => console.log("An Invalid Game was provided by " + (req.headers['x-forwarded-for'] || req.connection.remoteAddress)));
return;
}
game.getLeaderBoard(req.query['type'], req.query['id'], req.query['n']).then(JSON.stringify).then(p => {
console.log(p);
return p;
}).then(js => res.end(js, () => {
console.log("Done");
return;
})).catch(err => {
console.log(err + "\n" + err.stack);
res.end("{\"error\":\"" + err + "\"}");
})
});
app.get('/auth/:id', function (req, res) {
res.end(stats.auth.getToken(req.params.id));
});
var listener = app.listen((process.env.PORT || 80), function () {
console.log('API now running on port: ' + listener.address().port);
});
process.on('uncaughtException', (err) => {
console.log('Uncaught Exception:' + err);
console.log(err.stack);
});
module.exports = app;