2017-10-21 19:49:24 +02:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
// modules
|
|
|
|
const express = require('express');
|
2017-10-22 18:06:37 +02:00
|
|
|
const logger = require('debug')('cc:logs');
|
2017-10-21 19:49:24 +02:00
|
|
|
|
|
|
|
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');
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
res.locals.items = log;
|
|
|
|
next();
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-10-21 19:49:24 +02:00
|
|
|
// routes **********************
|
|
|
|
logsRouter.route('/:warId/budget')
|
|
|
|
.get((req, res, next) => {
|
|
|
|
const filter = {war: req.params.warId};
|
|
|
|
if (req.query.fraction) filter['fraction'] = req.query.fraction;
|
2017-10-22 17:10:13 +02:00
|
|
|
processLogRequest(LogBudgetModel, filter, res, next);
|
2017-10-21 19:49:24 +02:00
|
|
|
})
|
|
|
|
.all(
|
|
|
|
routerHandling.httpMethodNotAllowed
|
|
|
|
);
|
|
|
|
|
|
|
|
logsRouter.route('/:warId/respawn')
|
|
|
|
.get((req, res, next) => {
|
|
|
|
const filter = {war: req.params.warId};
|
|
|
|
if (req.query.player) filter['player'] = req.query.player;
|
2017-10-22 17:10:13 +02:00
|
|
|
processLogRequest(LogRespawnModel, filter, res, next);
|
2017-10-21 19:49:24 +02:00
|
|
|
})
|
|
|
|
.all(
|
|
|
|
routerHandling.httpMethodNotAllowed
|
|
|
|
);
|
|
|
|
|
|
|
|
logsRouter.route('/:warId/revive')
|
|
|
|
.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;
|
2017-10-22 17:10:13 +02:00
|
|
|
processLogRequest(LogReviveModel, filter, res, next);
|
2017-10-21 19:49:24 +02:00
|
|
|
})
|
|
|
|
.all(
|
|
|
|
routerHandling.httpMethodNotAllowed
|
|
|
|
);
|
|
|
|
|
|
|
|
logsRouter.route('/:warId/kills')
|
|
|
|
.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;
|
2017-10-22 17:10:13 +02:00
|
|
|
processLogRequest(LogKillModel, filter, res, next);
|
2017-10-21 19:49:24 +02:00
|
|
|
})
|
|
|
|
.all(
|
|
|
|
routerHandling.httpMethodNotAllowed
|
|
|
|
);
|
|
|
|
|
|
|
|
logsRouter.route('/:warId/transport')
|
|
|
|
.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;
|
2017-10-22 17:10:13 +02:00
|
|
|
processLogRequest(LogTransportModel, filter, res, next);
|
2017-10-21 19:49:24 +02:00
|
|
|
})
|
|
|
|
.all(
|
|
|
|
routerHandling.httpMethodNotAllowed
|
|
|
|
);
|
|
|
|
|
|
|
|
logsRouter.route('/:warId/flag')
|
|
|
|
.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;
|
2017-10-22 17:10:13 +02:00
|
|
|
processLogRequest(LogFlagModel, filter, res, next);
|
2017-10-21 19:49:24 +02:00
|
|
|
})
|
|
|
|
.all(
|
|
|
|
routerHandling.httpMethodNotAllowed
|
|
|
|
);
|
|
|
|
|
|
|
|
logsRouter.route('/:warId/points')
|
|
|
|
.get((req, res, next) => {
|
|
|
|
const filter = {war: req.params.warId};
|
|
|
|
if (req.query.fraction) filter['fraction'] = req.query.fraction;
|
2017-10-22 17:10:13 +02:00
|
|
|
processLogRequest(LogPointsModel, filter, res, next);
|
2017-10-21 19:49:24 +02:00
|
|
|
})
|
|
|
|
.all(
|
|
|
|
routerHandling.httpMethodNotAllowed
|
|
|
|
);
|
|
|
|
|
|
|
|
logsRouter.use(routerHandling.emptyResponse);
|
|
|
|
|
|
|
|
module.exports = logsRouter;
|