opt-cc/server/routes/logs.js

182 lines
7.0 KiB
JavaScript
Raw Permalink Normal View History

2018-03-12 09:26:44 +01:00
'use strict';
// modules
const express = require('express');
2017-10-29 17:36:55 +01:00
const async = require('async');
// HTTP status codes by name
const codes = require('./http-codes');
const routerHandling = require('../middleware/router-handling');
// Mongoose Model using mongoDB
const LogBudgetModel = require('../models/logs/budget');
const LogRespawnModel = require('../models/logs/respawn');
const LogReviveModel = require('../models/logs/revive');
const LogKillModel = require('../models/logs/kill');
2018-03-03 13:17:36 +01:00
const LogVehicleModel = require('../models/logs/vehicle');
const LogTransportModel = require('../models/logs/transport');
const LogFlagModel = require('../models/logs/flag');
const LogPointsModel = require('../models/logs/points');
2019-02-09 11:54:16 +01:00
const LogPlayerCountModel = require('../models/logs/player-count');
const LogServerFpsModel = require('../models/logs/server-fps');
const logsRouter = new express.Router();
const processLogRequest = (model, filter, res, next) => {
2017-10-22 17:10:13 +02:00
model.find(filter, {}, {sort: {time: 1}}, (err, log) => {
if (err) return next(err);
if (!log || log.length === 0) {
2017-10-22 18:06:37 +02:00
const err = new Error('No logs found');
err.status = codes.notfound;
2018-03-12 09:26:44 +01:00
return next(err);
2017-10-22 17:10:13 +02:00
}
2017-10-29 17:36:55 +01:00
res.locals.items = log;
2017-10-22 17:10:13 +02:00
next();
2018-03-12 09:26:44 +01:00
});
};
2017-10-22 17:10:13 +02:00
// routes **********************
2017-10-29 17:36:55 +01:00
logsRouter.route('/:warId')
2018-02-26 09:04:27 +01:00
.get((req, res, next) => {
const filter = {war: req.params.warId};
const sort = {sort: {time: 1}};
const pointsObjects = LogPointsModel.find(filter, {}, sort);
const budgetObjects = LogBudgetModel.find(filter, {player: 0, item: 0, buy: 0}, sort);
2018-02-26 09:04:27 +01:00
const respawnObjects = LogRespawnModel.find(filter, {}, sort);
const reviveObjects = LogReviveModel.find(filter, {}, sort);
const killObjects = LogKillModel.find(filter, {}, sort);
2018-03-03 13:17:36 +01:00
const vehicleObjects = LogVehicleModel.find(filter, {}, sort);
2018-02-26 09:04:27 +01:00
const transportObjects = LogTransportModel.find(filter, {}, sort);
const flagObjects = LogFlagModel.find(filter, {}, sort);
2019-02-09 11:54:16 +01:00
const playerCountObjects = LogPlayerCountModel.find(filter, {}, sort);
2018-02-26 09:04:27 +01:00
const resources = {
points: pointsObjects.exec.bind(pointsObjects),
budget: budgetObjects.exec.bind(budgetObjects),
respawn: respawnObjects.exec.bind(respawnObjects),
revive: reviveObjects.exec.bind(reviveObjects),
kill: killObjects.exec.bind(killObjects),
2018-03-03 13:17:36 +01:00
vehicle: killObjects.exec.bind(vehicleObjects),
2018-02-26 09:04:27 +01:00
transport: transportObjects.exec.bind(transportObjects),
2018-03-12 09:26:44 +01:00
flag: flagObjects.exec.bind(flagObjects),
2019-02-09 11:54:16 +01:00
playerCount: playerCountObjects.exec.bind(playerCountObjects),
2018-02-26 09:04:27 +01:00
};
async.parallel(resources, (error, results) => {
2018-02-26 09:04:27 +01:00
if (error) {
2018-04-01 10:59:09 +02:00
res.status(500);
2018-02-26 09:04:27 +01:00
}
res.locals.items = results;
2018-04-01 10:59:09 +02:00
next(error);
2018-02-26 09:04:27 +01:00
});
})
.all(
routerHandling.httpMethodNotAllowed
);
2017-10-29 17:36:55 +01:00
logsRouter.route('/:warId/budget')
2018-02-26 09:04:27 +01:00
.get((req, res, next) => {
const filter = {war: req.params.warId};
if (req.query.fraction) filter['fraction'] = req.query.fraction;
processLogRequest(LogBudgetModel, filter, res, next);
})
.all(
routerHandling.httpMethodNotAllowed
);
logsRouter.route('/:warId/respawn')
2018-02-26 09:04:27 +01:00
.get((req, res, next) => {
const filter = {war: req.params.warId};
if (req.query.player) filter['player'] = req.query.player;
processLogRequest(LogRespawnModel, filter, res, next);
})
.all(
routerHandling.httpMethodNotAllowed
);
logsRouter.route('/:warId/revive')
2018-02-26 09:04:27 +01:00
.get((req, res, next) => {
const filter = {war: req.params.warId};
if (req.query.medic) filter['medic'] = req.query.medic;
if (req.query.patient) filter['patient'] = req.query.patient;
if (req.query.stabilized) filter['stabilized'] = true;
if (req.query.revive) filter['stabilized'] = false;
if (req.query.fraction) filter['fraction'] = req.query.fraction;
processLogRequest(LogReviveModel, filter, res, next);
})
.all(
routerHandling.httpMethodNotAllowed
);
logsRouter.route('/:warId/kills')
2018-02-26 09:04:27 +01:00
.get((req, res, next) => {
const filter = {war: req.params.warId};
if (req.query.shooter) filter['shooter'] = req.query.shooter;
if (req.query.target) filter['target'] = req.query.target;
if (req.query.friendlyFire) filter['friendlyFire'] = true;
if (req.query.noFriendlyFire) filter['friendlyFire'] = false;
if (req.query.fraction) filter['fraction'] = req.query.fraction;
processLogRequest(LogKillModel, filter, res, next);
})
.all(
routerHandling.httpMethodNotAllowed
);
logsRouter.route('/:warId/transport')
2018-02-26 09:04:27 +01:00
.get((req, res, next) => {
const filter = {war: req.params.warId};
if (req.query.driver) filter['driver'] = req.query.driver;
if (req.query.passenger) filter['passenger'] = req.query.passenger;
if (req.query.fraction) filter['fraction'] = req.query.fraction;
processLogRequest(LogTransportModel, filter, res, next);
})
.all(
routerHandling.httpMethodNotAllowed
);
logsRouter.route('/:warId/flag')
2018-02-26 09:04:27 +01:00
.get((req, res, next) => {
const filter = {war: req.params.warId};
if (req.query.player) filter['player'] = req.query.player;
if (req.query.capture) filter['capture'] = true;
if (req.query.defend) filter['capture'] = false;
if (req.query.fraction) filter['fraction'] = req.query.fraction;
processLogRequest(LogFlagModel, filter, res, next);
})
.all(
routerHandling.httpMethodNotAllowed
);
logsRouter.route('/:warId/points')
2018-02-26 09:04:27 +01:00
.get((req, res, next) => {
const filter = {war: req.params.warId};
if (req.query.fraction) filter['fraction'] = req.query.fraction;
processLogRequest(LogPointsModel, filter, res, next);
})
.all(
routerHandling.httpMethodNotAllowed
);
logsRouter.route('/:warId/performance')
.get((req, res, next) => {
const filter = {war: req.params.warId};
LogServerFpsModel.find(filter, (err, items) => {
if (err) return next(err);
if (!items) {
const err = new Error('No logs found');
err.status = codes.notfound;
return next(err);
}
res.locals.items = items;
next();
});
})
.all(
routerHandling.httpMethodNotAllowed
);
logsRouter.use(routerHandling.emptyResponse);
module.exports = logsRouter;