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;
|
|
|
|
});
|
2019-10-11 16:53:26 +02:00
|
|
|
WarModel.findOne({campaign: req.params.campaignId}, {}, {sort: {'date': -1}}, (err, latestWar) => {
|
|
|
|
PlayerModel.find({warId: {'$in': warIds}}, (err, items) => {
|
|
|
|
if (err) return next(err);
|
|
|
|
if (!items || items.length === 0) {
|
|
|
|
const err = new Error('No players for given campaignId');
|
|
|
|
err.status = codes.notfound;
|
|
|
|
return next(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
const rankingItems = [];
|
|
|
|
|
|
|
|
// check only first player to have valid steamUUID - then decide if tracked by name or by ID
|
|
|
|
const usesUUID = isSteamUUID(items[0].steamUUID);
|
|
|
|
|
|
|
|
new Set(items.map(usesUUID ? (x) => x.steamUUID : (x) => x.name))
|
|
|
|
.forEach((player) => {
|
|
|
|
const playerInstances = items.filter(
|
|
|
|
usesUUID ? (p) => p.steamUUID === player : (p) => p.name === player);
|
|
|
|
const resItem = {
|
|
|
|
name: usesUUID ? playerInstances[playerInstances.length - 1].name : player,
|
|
|
|
kill: 0,
|
|
|
|
vehicleLight: 0,
|
|
|
|
vehicleHeavy: 0,
|
|
|
|
vehicleAir: 0,
|
|
|
|
death: 0,
|
|
|
|
friendlyFire: 0,
|
|
|
|
revive: 0,
|
|
|
|
respawn: 0,
|
|
|
|
flagTouch: 0,
|
|
|
|
travelDistance: 0,
|
|
|
|
driverDistance: 0,
|
|
|
|
};
|
|
|
|
for (let i = 0; i < playerInstances.length; i++) {
|
|
|
|
const player = playerInstances[i];
|
|
|
|
resItem.kill += player.kill;
|
|
|
|
resItem.death += player.death;
|
|
|
|
resItem.friendlyFire += player.friendlyFire;
|
|
|
|
resItem.vehicleLight += player.vehicleLight;
|
|
|
|
resItem.vehicleHeavy += player.vehicleHeavy;
|
|
|
|
resItem.vehicleAir += player.vehicleAir;
|
|
|
|
resItem.revive += player.revive;
|
|
|
|
resItem.respawn += player.respawn;
|
|
|
|
resItem.flagTouch += player.flagTouch;
|
|
|
|
if (player.travelDistance) {
|
|
|
|
resItem.travelDistance += Math.round(player.travelDistance / 1000); // meters -> km
|
|
|
|
}
|
|
|
|
if (player.driverDistance) {
|
|
|
|
resItem.driverDistance += Math.round(player.driverDistance / 1000); // meters -> km
|
|
|
|
}
|
2019-02-11 22:57:31 +01:00
|
|
|
}
|
2019-10-11 16:53:26 +02:00
|
|
|
resItem.warCount = playerInstances.length;
|
|
|
|
|
|
|
|
const latestPlayerFraction = playerInstances[playerInstances.length - 1].fraction;
|
|
|
|
resItem.fraction =
|
|
|
|
(latestPlayerFraction === 'OPFOR') ?
|
|
|
|
latestWar.fractionMappingOpfor :
|
|
|
|
latestWar.fractionMappingBlufor;
|
|
|
|
|
|
|
|
rankingItems.push(resItem);
|
|
|
|
});
|
|
|
|
|
|
|
|
const getSortedField = (fieldName) => {
|
|
|
|
let num = 1;
|
|
|
|
const filteredSortResult = rankingItems.map((item) => {
|
|
|
|
return {
|
|
|
|
name: item.name,
|
|
|
|
fraction: item.fraction,
|
|
|
|
[fieldName]: item[fieldName],
|
|
|
|
};
|
|
|
|
})
|
|
|
|
.sort((a, b) => b[fieldName] - a[fieldName]);
|
|
|
|
const res = JSON.parse(JSON.stringify(filteredSortResult));
|
|
|
|
for (const entity of res) {
|
|
|
|
entity.num = num++;
|
2018-03-04 10:55:59 +01:00
|
|
|
}
|
2019-10-11 16:53:26 +02:00
|
|
|
return res;
|
|
|
|
};
|
|
|
|
|
|
|
|
res.locals.items = {
|
|
|
|
kill: getSortedField('kill'),
|
|
|
|
death: getSortedField('death'),
|
|
|
|
friendlyFire: getSortedField('friendlyFire'),
|
|
|
|
vehicleLight: getSortedField('vehicleLight'),
|
|
|
|
vehicleHeavy: getSortedField('vehicleHeavy'),
|
|
|
|
vehicleAir: getSortedField('vehicleAir'),
|
|
|
|
revive: getSortedField('revive'),
|
|
|
|
respawn: getSortedField('respawn'),
|
|
|
|
flagTouch: getSortedField('flagTouch'),
|
|
|
|
travelDistance: getSortedField('travelDistance'),
|
|
|
|
driverDistance: getSortedField('driverDistance'),
|
|
|
|
warCount: getSortedField('warCount'),
|
|
|
|
};
|
|
|
|
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;
|