From 98412ca0ab5fdeebe9077165b058b54b5023ab59 Mon Sep 17 00:00:00 2001 From: HardiReady Date: Fri, 22 Dec 2017 07:56:26 +0100 Subject: [PATCH] finish basic ranking endpoint --- api/routes/players.js | 58 ++++++++++++++++++- package.json | 2 +- static/src/app/app.config.ts | 2 +- .../src/app/services/logs/player.service.ts | 2 +- 4 files changed, 58 insertions(+), 6 deletions(-) diff --git a/api/routes/players.js b/api/routes/players.js index c296f8d..243f4fc 100644 --- a/api/routes/players.js +++ b/api/routes/players.js @@ -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) } diff --git a/package.json b/package.json index 45f5e44..2ee1189 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "opt-cc", - "version": "1.6.6", + "version": "1.6.7", "license": "MIT", "author": "Florian Hartwich ", "private": true, diff --git a/static/src/app/app.config.ts b/static/src/app/app.config.ts index 7187683..69ed7f8 100644 --- a/static/src/app/app.config.ts +++ b/static/src/app/app.config.ts @@ -35,4 +35,4 @@ export const RouteConfig = { requestPromotionPath: 'promotion', confirmAwardPath: 'confirm-award', confirmPromotionPath: 'confirm-promotion' -} +}; diff --git a/static/src/app/services/logs/player.service.ts b/static/src/app/services/logs/player.service.ts index ae231cc..fce391d 100644 --- a/static/src/app/services/logs/player.service.ts +++ b/static/src/app/services/logs/player.service.ts @@ -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()) }