This repository has been archived by the owner on Jul 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.js
56 lines (49 loc) · 1.59 KB
/
api.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
const express = require('express');
const database = require('./database');
const countries = require('country-data').countries;
// Build a custom router to query the API
var api = new express.Router();
/**
* Gets all server countries with their counts
*/
api.get('/server/countries', (req, res) => {
database.connect().then((Db) => {
let collection = Db.collection('games');
collection.aggregate([ { $group: {_id: "$country", total: { $sum: 1 } } } ], function (err, data) {
dataset = []
data.forEach((obj, index) => {
if (typeof(countries[obj._id]) != 'undefined') { // Somehow it happens that the sauertracker API cannot identify the contry for all players
obj._id = countries[obj._id].alpha3;
dataset.push(obj);
}
})
res.json(dataset)
Db.close()
})
})
})
/**
* Gets all player countries with their counts
*/
api.get('/player/countries', (req, res) => {
database.connect().then((Db) => {
let collection = Db.collection('games');
collection.aggregate([
{ $unwind: "$players" },
{ $group: {_id: "$players.country", total: { $sum: 1 } } },
{ $sort: { total: -1 } }
], function(err, data) {
if (err) res.status(500).json(err)
dataset = []
data.forEach((obj, index) => {
if (typeof(countries[obj._id]) != 'undefined') { // Somehow it happens that the sauertracker API cannot identify the contry for all players
obj._id = countries[obj._id].alpha3;
dataset.push(obj);
}
})
res.json(dataset)
Db.close()
})
})
})
module.exports = api;