2018-03-12 09:26:44 +01:00
|
|
|
'use strict';
|
2017-10-01 13:12:55 +02:00
|
|
|
|
|
|
|
// modules
|
|
|
|
const express = require('express');
|
|
|
|
|
|
|
|
// HTTP status codes by name
|
|
|
|
const codes = require('./http-codes');
|
|
|
|
|
|
|
|
const routerHandling = require('../middleware/router-handling');
|
|
|
|
|
|
|
|
// Mongoose Model using mongoDB
|
|
|
|
const CampaignModel = require('../models/campaign');
|
|
|
|
const PlayerModel = require('../models/player');
|
|
|
|
const WarModel = require('../models/war');
|
|
|
|
|
2018-03-03 19:15:33 +01:00
|
|
|
// Util
|
|
|
|
const isSteamUUID = require('../tools/util').isSteamUUID;
|
|
|
|
|
2018-03-12 10:39:56 +01:00
|
|
|
const campaignPlayer = new express.Router();
|
2017-10-01 13:12:55 +02:00
|
|
|
|
|
|
|
// routes **********************
|
2017-12-22 07:56:26 +01:00
|
|
|
campaignPlayer.route('/ranking/:campaignId')
|
2018-02-26 09:04:27 +01:00
|
|
|
.get((req, res, next) => {
|
|
|
|
WarModel.find({campaign: req.params.campaignId}, '_id', (err, wars) => {
|
|
|
|
if (err) return next(err);
|
|
|
|
const warIds = wars.map((obj) => {
|
|
|
|
return obj._id;
|
|
|
|
});
|
2018-03-12 09:26:44 +01:00
|
|
|
PlayerModel.find({warId: {'$in': warIds}}, (err, items) => {
|
2018-02-26 09:04:27 +01:00
|
|
|
if (err) return next(err);
|
|
|
|
if (!items || items.length === 0) {
|
|
|
|
const err = new Error('No players for given campaignId');
|
|
|
|
err.status = codes.notfound;
|
2018-03-12 09:26:44 +01:00
|
|
|
return next(err);
|
2018-02-26 09:04:27 +01:00
|
|
|
}
|
2017-12-22 07:56:26 +01:00
|
|
|
|
2018-02-26 09:04:27 +01:00
|
|
|
const rankingItems = [];
|
2018-03-04 10:55:59 +01:00
|
|
|
|
|
|
|
// check only first player to have valid steamUUID - then decide if tracked by name or by ID
|
|
|
|
const usesUUID = isSteamUUID(items[0].steamUUID);
|
|
|
|
|
2018-03-12 09:26:44 +01:00
|
|
|
new Set(items.map(usesUUID ? (x) => x.steamUUID : (x) => x.name))
|
|
|
|
.forEach((player) => {
|
2018-03-12 09:59:43 +01:00
|
|
|
const playerInstances = items.filter(
|
|
|
|
usesUUID ? (p) => p.steamUUID === player : (p) => p.name === player);
|
2018-03-04 10:55:59 +01:00
|
|
|
const resItem = {
|
|
|
|
name: usesUUID ? playerInstances[playerInstances.length - 1].name : player,
|
|
|
|
kill: 0,
|
|
|
|
vehicle: 0,
|
|
|
|
death: 0,
|
|
|
|
friendlyFire: 0,
|
|
|
|
revive: 0,
|
|
|
|
respawn: 0,
|
2018-03-12 09:26:44 +01:00
|
|
|
flagTouch: 0,
|
2018-03-04 10:55:59 +01:00
|
|
|
};
|
|
|
|
for (let i = 0; i < playerInstances.length; i++) {
|
|
|
|
resItem.kill += playerInstances[i].kill;
|
|
|
|
resItem.death += playerInstances[i].death;
|
|
|
|
resItem.friendlyFire += playerInstances[i].friendlyFire;
|
|
|
|
resItem.vehicle += playerInstances[i].vehicle;
|
|
|
|
resItem.revive += playerInstances[i].revive;
|
|
|
|
resItem.respawn += playerInstances[i].respawn;
|
|
|
|
resItem.flagTouch += playerInstances[i].flagTouch;
|
|
|
|
}
|
|
|
|
resItem.fraction = playerInstances[playerInstances.length - 1].fraction;
|
|
|
|
rankingItems.push(resItem);
|
|
|
|
});
|
2017-12-22 07:56:26 +01:00
|
|
|
|
2018-03-12 10:39:56 +01:00
|
|
|
const getSortedField = (fieldName) => {
|
2018-02-26 09:04:27 +01:00
|
|
|
let num = 1;
|
2018-03-12 09:26:44 +01:00
|
|
|
rankingItems.sort((a, b) => b[fieldName] - a[fieldName]);
|
2018-02-26 09:04:27 +01:00
|
|
|
const res = JSON.parse(JSON.stringify(rankingItems));
|
|
|
|
for (const entity of res) {
|
|
|
|
entity.num = num++;
|
|
|
|
}
|
|
|
|
return res;
|
2018-03-12 10:39:56 +01:00
|
|
|
};
|
2017-12-22 07:56:26 +01:00
|
|
|
|
2018-02-26 09:04:27 +01:00
|
|
|
res.locals.items = {
|
|
|
|
kill: getSortedField('kill'),
|
|
|
|
death: getSortedField('death'),
|
|
|
|
friendlyFire: getSortedField('friendlyFire'),
|
2018-03-03 13:49:25 +01:00
|
|
|
vehicle: getSortedField('vehicle'),
|
2018-02-26 09:04:27 +01:00
|
|
|
revive: getSortedField('revive'),
|
|
|
|
respawn: getSortedField('respawn'),
|
2018-03-12 09:26:44 +01:00
|
|
|
flagTouch: getSortedField('flagTouch'),
|
2018-02-26 09:04:27 +01:00
|
|
|
};
|
|
|
|
next();
|
2018-03-12 09:26:44 +01:00
|
|
|
});
|
|
|
|
});
|
2018-02-26 09:04:27 +01:00
|
|
|
})
|
2017-12-22 07:56:26 +01:00
|
|
|
|
2018-02-26 09:04:27 +01:00
|
|
|
.all(
|
|
|
|
routerHandling.httpMethodNotAllowed
|
|
|
|
);
|
2017-12-22 07:56:26 +01:00
|
|
|
|
2018-03-03 19:15:33 +01:00
|
|
|
campaignPlayer.route('/single/:campaignId/:playerId')
|
2018-02-26 09:04:27 +01:00
|
|
|
.get((req, res, next) => {
|
|
|
|
CampaignModel.findById(req.params.campaignId, (err, campaign) => {
|
|
|
|
if (err) return next(err);
|
|
|
|
WarModel.find({campaign: req.params.campaignId}, '_id', (err, wars) => {
|
|
|
|
if (err) return next(err);
|
|
|
|
const warIds = wars.map((obj) => {
|
|
|
|
return obj._id;
|
|
|
|
});
|
2018-03-03 19:15:33 +01:00
|
|
|
|
|
|
|
// find by player name until v1.6.12, afterwards by SteamUUID
|
|
|
|
const playerId = req.params.playerId;
|
|
|
|
const filter = {};
|
|
|
|
filter[isSteamUUID(playerId) ? 'steamUUID' : 'name'] = playerId;
|
2018-03-12 09:26:44 +01:00
|
|
|
filter['warId'] = {'$in': warIds};
|
2018-03-03 19:15:33 +01:00
|
|
|
|
|
|
|
PlayerModel.find(filter)
|
2018-02-26 09:04:27 +01:00
|
|
|
.populate('warId')
|
|
|
|
.exec((err, items) => {
|
|
|
|
if (err) return next(err);
|
|
|
|
if (!items || items.length === 0) {
|
2018-03-03 19:15:33 +01:00
|
|
|
const err = new Error('Unknown player id');
|
2018-02-26 09:04:27 +01:00
|
|
|
err.status = codes.notfound;
|
2018-03-12 09:26:44 +01:00
|
|
|
return next(err);
|
2018-02-26 09:04:27 +01:00
|
|
|
}
|
|
|
|
res.locals.items = {
|
2018-03-03 19:15:33 +01:00
|
|
|
name: items[items.length - 1].name,
|
2018-02-26 09:04:27 +01:00
|
|
|
campaign: campaign,
|
2018-03-12 09:26:44 +01:00
|
|
|
players: items,
|
2018-02-26 09:04:27 +01:00
|
|
|
};
|
|
|
|
next();
|
2018-03-12 09:26:44 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2018-02-26 09:04:27 +01:00
|
|
|
})
|
2017-10-01 13:12:55 +02:00
|
|
|
|
2018-02-26 09:04:27 +01:00
|
|
|
.all(
|
|
|
|
routerHandling.httpMethodNotAllowed
|
|
|
|
);
|
2017-10-01 13:12:55 +02:00
|
|
|
|
|
|
|
|
|
|
|
campaignPlayer.use(routerHandling.emptyResponse);
|
|
|
|
|
|
|
|
module.exports = campaignPlayer;
|