finish basic ranking endpoint

pull/22/head
HardiReady 2017-12-22 07:56:26 +01:00
parent 43ff1e86dc
commit 98412ca0ab
4 changed files with 58 additions and 6 deletions

View File

@ -2,7 +2,7 @@
// modules
const express = require('express');
const logger = require('debug')('cc:wars');
const logger = require('debug')('cc:players');
// HTTP status codes by name
const codes = require('./http-codes');
@ -17,7 +17,59 @@ const WarModel = require('../models/war');
const campaignPlayer = express.Router();
// routes **********************
campaignPlayer.route('/:campaignId/:playerName')
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')
.get((req, res, next) => {
CampaignModel.findById(req.params.campaignId, (err, campaign) => {
if (err) return next(err);
@ -31,7 +83,7 @@ campaignPlayer.route('/:campaignId/:playerName')
.exec((err, items) => {
if (err) return next(err);
if (!items || items.length === 0) {
const err = new Error('unknown player name');
const err = new Error('Unknown player name');
err.status = codes.notfound;
return next(err)
}

View File

@ -1,6 +1,6 @@
{
"name": "opt-cc",
"version": "1.6.6",
"version": "1.6.7",
"license": "MIT",
"author": "Florian Hartwich <hardi@noarch.de>",
"private": true,

View File

@ -35,4 +35,4 @@ export const RouteConfig = {
requestPromotionPath: 'promotion',
confirmAwardPath: 'confirm-award',
confirmPromotionPath: 'confirm-promotion'
}
};

View File

@ -10,7 +10,7 @@ export class PlayerService {
}
getCampaignPlayer(campaignId: string, playerName: string) {
return this.http.get(this.config.apiPlayersPath + '/' + campaignId + '/' + playerName)
return this.http.get(this.config.apiPlayersPath + '/single/' + campaignId + '/' + playerName)
.map(res => res.json())
}