opt-cc/api/routes/logs.js

157 lines
6.0 KiB
JavaScript
Raw Normal View History

"use strict";
// modules
const express = require('express');
2017-10-29 17:36:55 +01:00
const async = require('async');
2017-10-22 18:06:37 +02:00
const logger = require('debug')('cc:logs');
const routerHandling = require('../middleware/router-handling');
2017-10-28 22:50:54 +02:00
const decimalToTimeString = require('../tools/util').decimalToTimeString;
// 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');
const LogTransportModel = require('../models/logs/transport');
const LogFlagModel = require('../models/logs/flag');
const LogPointsModel = require('../models/logs/points');
const logsRouter = express.Router();
2017-10-22 17:10:13 +02:00
function processLogRequest(model, filter, res, next) {
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 = require('./http-codes').notfound;
2017-10-22 17:10:13 +02:00
return next(err)
}
2017-10-29 17:36:55 +01:00
res.locals.items = log;
2017-10-22 17:10:13 +02:00
next();
})
}
// 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, {}, sort);
const respawnObjects = LogRespawnModel.find(filter, {}, sort);
const reviveObjects = LogReviveModel.find(filter, {}, sort);
const killObjects = LogKillModel.find(filter, {}, sort);
const transportObjects = LogTransportModel.find(filter, {}, sort);
const flagObjects = LogFlagModel.find(filter, {}, sort);
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),
transport: transportObjects.exec.bind(transportObjects),
flag: flagObjects.exec.bind(flagObjects)
};
async.parallel(resources, function (error, results) {
if (error) {
res.status(500).send(error);
return;
}
res.locals.items = results;
next();
});
})
.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.use(routerHandling.emptyResponse);
module.exports = logsRouter;