2017-10-01 13:12:55 +02:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
// modules
|
|
|
|
const express = require('express');
|
2017-12-22 07:56:26 +01:00
|
|
|
const logger = require('debug')('cc:players');
|
2017-10-01 13:12:55 +02:00
|
|
|
|
|
|
|
// 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');
|
|
|
|
|
|
|
|
const campaignPlayer = express.Router();
|
|
|
|
|
|
|
|
// routes **********************
|
2017-12-22 07:56:26 +01:00
|
|
|
campaignPlayer.route('/ranking/:campaignId')
|
|
|
|
.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;
|
|
|
|
});
|
|
|
|
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 = [];
|
|
|
|
new Set(items.map(x => x.name)).forEach(playerName => {
|
|
|
|
const playerInstances = items.filter(p => p.name === playerName)
|
|
|
|
const resItem = {name: playerName, kill: 0, death: 0, friendlyFire: 0, revive: 0, respawn: 0, flagTouch: 0};
|
|
|
|
for (let i = 0; i < playerInstances.length; i++) {
|
|
|
|
resItem.kill += playerInstances[i].kill;
|
|
|
|
resItem.death += playerInstances[i].death;
|
|
|
|
resItem.friendlyFire += playerInstances[i].friendlyFire;
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
|
|
|
|
function getTopFive(fieldName) {
|
|
|
|
return rankingItems.sort((a, b) => b[fieldName] - a[fieldName]).slice(0, 5)
|
|
|
|
}
|
|
|
|
|
|
|
|
res.locals.items = {
|
|
|
|
kills: getTopFive('kill'),
|
|
|
|
death: getTopFive('death'),
|
|
|
|
friendlyFire: getTopFive('friendlyFire'),
|
|
|
|
revive: getTopFive('revive'),
|
|
|
|
respawn: getTopFive('respawn'),
|
|
|
|
flagTouch: getTopFive('flagTouch')
|
|
|
|
};
|
|
|
|
next();
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
.all(
|
|
|
|
routerHandling.httpMethodNotAllowed
|
|
|
|
);
|
|
|
|
|
|
|
|
campaignPlayer.route('/single/:campaignId/:playerName')
|
2017-10-01 13:12:55 +02: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;
|
|
|
|
});
|
|
|
|
PlayerModel.find({name: req.params.playerName, warId: {"$in": warIds}})
|
|
|
|
.populate('warId')
|
|
|
|
.exec((err, items) => {
|
|
|
|
if (err) return next(err);
|
|
|
|
if (!items || items.length === 0) {
|
2017-12-22 07:56:26 +01:00
|
|
|
const err = new Error('Unknown player name');
|
2017-10-01 13:12:55 +02:00
|
|
|
err.status = codes.notfound;
|
|
|
|
return next(err)
|
|
|
|
}
|
|
|
|
res.locals.items = {
|
|
|
|
name: req.params.playerName,
|
|
|
|
campaign: campaign,
|
|
|
|
players: items
|
|
|
|
};
|
|
|
|
next();
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
.all(
|
|
|
|
routerHandling.httpMethodNotAllowed
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
campaignPlayer.use(routerHandling.emptyResponse);
|
|
|
|
|
|
|
|
module.exports = campaignPlayer;
|