Add endpoints for log entries & add additional fraction fields for DOs

pull/15/head
Florian Hartwich 2017-10-21 19:49:24 +02:00
parent 443184027e
commit cd77c4b7f2
9 changed files with 218 additions and 14 deletions

View File

@ -8,6 +8,7 @@ module.exports = {
cmdCreateSig: rootRoute + '/cmd/createSignature', cmdCreateSig: rootRoute + '/cmd/createSignature',
command: rootRoute + '/cmd', command: rootRoute + '/cmd',
decorations: rootRoute + '/decorations', decorations: rootRoute + '/decorations',
logs: rootRoute + '/logs',
overview: rootRoute + '/overview', overview: rootRoute + '/overview',
players: rootRoute + '/players', players: rootRoute + '/players',
ranks: rootRoute + '/ranks', ranks: rootRoute + '/ranks',

View File

@ -14,8 +14,7 @@ const LogKillSchema = new Schema({
required: true required: true
}, },
shooter: { shooter: {
type: String, type: String
required: true
}, },
target: { target: {
type: String, type: String,
@ -24,6 +23,11 @@ const LogKillSchema = new Schema({
friendlyFire: { friendlyFire: {
type: Boolean, type: Boolean,
required: true required: true
},
fraction: {
type: String,
enum: ['BLUFOR', 'OPFOR', 'NONE'],
required: true
} }
}, { }, {
collection: 'logKill' collection: 'logKill'

View File

@ -24,11 +24,16 @@ const LogKillSchema = new Schema({
get: v => Math.round(v), get: v => Math.round(v),
set: v => Math.round(v), set: v => Math.round(v),
required: true required: true
},
fraction: {
type: String,
enum: ['BLUFOR', 'OPFOR', 'NONE'],
required: true
} }
}, { }, {
collection: 'logKill' collection: 'logPoints'
}); });
// optional more indices // optional more indices
LogKillSchema.index({war: 1, shooter: 1, target: 1}); LogKillSchema.index({war: 1, shooter: 1, target: 1});
module.exports = mongoose.model('LogKill', LogKillSchema); module.exports = mongoose.model('LogPoints', LogKillSchema);

View File

@ -24,6 +24,11 @@ const LogReviveSchema = new Schema({
stabilized: { stabilized: {
type: Boolean, type: Boolean,
required: true required: true
},
fraction: {
type: String,
enum: ['BLUFOR', 'OPFOR'],
required: true
} }
}, { }, {
collection: 'logRevive' collection: 'logRevive'

View File

@ -26,6 +26,11 @@ const LogTransportSchema = new Schema({
get: v => Math.round(v), get: v => Math.round(v),
set: v => Math.round(v), set: v => Math.round(v),
required: true required: true
},
fraction: {
type: String,
enum: ['BLUFOR', 'OPFOR'],
required: true
} }
}, { }, {
collection: 'logTransport' collection: 'logTransport'

175
api/routes/logs.js Normal file
View File

@ -0,0 +1,175 @@
"use strict";
// modules
const express = require('express');
const logger = require('debug')('cc:wars');
// 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');
const LogTransportModel = require('../models/logs/transport');
const LogFlagModel = require('../models/logs/flag');
const LogPointsModel = require('../models/logs/points');
const notFoundError = new Error('No logs found');
const logsRouter = express.Router();
// routes **********************
logsRouter.route('/:warId/budget')
.get((req, res, next) => {
const filter = {war: req.params.warId};
if (req.query.fraction) filter['fraction'] = req.query.fraction;
LogBudgetModel.find(filter, {}, {sort: {time: 1}}, (err, logBudget) => {
if (err) return next(err);
if (!logBudget || logBudget.length === 0) {
const err = notFoundError;
err.status = codes.notfound;
return next(err)
}
res.locals.items = logBudget;
next();
})
})
.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;
LogRespawnModel.find(filter, {}, {sort: {time: 1}}, (err, logRespawn) => {
if (err) return next(err);
if (!logRespawn || logRespawn.length === 0) {
const err = notFoundError;
err.status = codes.notfound;
return next(err)
}
res.locals.items = logRespawn;
next();
})
})
.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;
LogReviveModel.find(filter, {}, {sort: {time: 1}}, (err, logRevive) => {
if (err) return next(err);
if (!logRevive || logRevive.length === 0) {
const err = notFoundError;
err.status = codes.notfound;
return next(err)
}
res.locals.items = logRevive;
next();
})
})
.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;
LogKillModel.find(filter, {}, {sort: {time: 1}}, (err, logKill) => {
if (err) return next(err);
if (!logKill || logKill.length === 0) {
const err = notFoundError;
err.status = codes.notfound;
return next(err)
}
res.locals.items = logKill;
next();
})
})
.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;
LogTransportModel.find(filter, {}, {sort: {time: 1}}, (err, logTransport) => {
if (err) return next(err);
if (!logTransport || logTransport.length === 0) {
const err = notFoundError;
err.status = codes.notfound;
return next(err)
}
res.locals.items = logTransport;
next();
})
})
.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;
LogFlagModel.find(filter, {}, {sort: {time: 1}}, (err, logFlag) => {
if (err) return next(err);
if (!logFlag || logFlag.length === 0) {
const err = notFoundError;
err.status = codes.notfound;
return next(err)
}
res.locals.items = logFlag;
next();
})
})
.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;
LogPointsModel.find(filter, {}, {sort: {time: 1}}, (err, logPoints) => {
if (err) return next(err);
if (!logPoints || logPoints.length === 0) {
const err = notFoundError;
err.status = codes.notfound;
return next(err)
}
res.locals.items = logPoints;
next();
})
})
.all(
routerHandling.httpMethodNotAllowed
);
logsRouter.use(routerHandling.emptyResponse);
module.exports = logsRouter;

View File

@ -29,6 +29,7 @@ const LogReviveModel = require('../models/logs/revive');
const LogTransportModel = require('../models/logs/transport'); const LogTransportModel = require('../models/logs/transport');
const LogFlagModel = require('../models/logs/flag'); const LogFlagModel = require('../models/logs/flag');
const LogBudgetModel = require('../models/logs/budget'); const LogBudgetModel = require('../models/logs/budget');
const LogPointsModel = require('../models/logs/points');
const wars = express.Router(); const wars = express.Router();
@ -93,6 +94,7 @@ wars.route('/')
LogFlagModel.create(statsResult.flag, function (err) { LogFlagModel.create(statsResult.flag, function (err) {
LogBudgetModel.create(statsResult.budget, function (err) { LogBudgetModel.create(statsResult.budget, function (err) {
LogTransportModel.create(statsResult.transport, function (err) { LogTransportModel.create(statsResult.transport, function (err) {
LogPointsModel.create(statsResult.points, function (err) {
res.status(codes.created); res.status(codes.created);
res.locals.items = war; res.locals.items = war;
next(); next();
@ -102,6 +104,7 @@ wars.route('/')
}) })
}) })
}) })
})
}); });
}) })
}); });

View File

@ -35,6 +35,7 @@ const signatureRouter = require('./routes/signatures');
const commandRouter = require('./routes/command'); const commandRouter = require('./routes/command');
const campaignRouter = require('./routes/campaigns'); const campaignRouter = require('./routes/campaigns');
const warRouter = require('./routes/wars'); const warRouter = require('./routes/wars');
const logRouter = require('./routes/logs');
// Configuration *********************************** // Configuration ***********************************
// mongoose promise setup // mongoose promise setup
@ -83,6 +84,7 @@ app.use(urls.awards, awardingRouter);
app.use(urls.wars, warRouter); app.use(urls.wars, warRouter);
app.use(urls.players, playerRouter); app.use(urls.players, playerRouter);
app.use(urls.campaigns,campaignRouter); app.use(urls.campaigns,campaignRouter);
app.use(urls.logs,logRouter);
app.use(urls.command, apiAuthenticationMiddleware, checkAdmin, commandRouter); app.use(urls.command, apiAuthenticationMiddleware, checkAdmin, commandRouter);
app.use(urls.account, apiAuthenticationMiddleware, checkAdmin, accountRouter); app.use(urls.account, apiAuthenticationMiddleware, checkAdmin, accountRouter);

View File

@ -40,8 +40,9 @@ const parseWarLog = (lineArray, war) => {
war: war._id, war: war._id,
time: getDateTime(line.split(' ')[5]), time: getDateTime(line.split(' ')[5]),
shooter: shooter ? shooter.name : null, shooter: shooter ? shooter.name : null,
target: target ? target.name : null, target: target.name,
friendlyFire: target && shooter ? target.fraction === shooter.fraction : false friendlyFire: shooter ? target.fraction === shooter.fraction : false,
fraction: shooter ? shooter.fraction : 'NONE'
}); });
addPlayersIfNotExists([shooter, target]); addPlayersIfNotExists([shooter, target]);
@ -94,7 +95,7 @@ const parseWarLog = (lineArray, war) => {
stats.war['ptBlufor'] = parseInt(pt[11]); stats.war['ptBlufor'] = parseInt(pt[11]);
stats.war['ptOpfor'] = parseInt(pt[14].slice(0, -1)); stats.war['ptOpfor'] = parseInt(pt[14].slice(0, -1));
} else { } else {
stats.points.push(getPointsEntry(pt, war._id)) stats.points.push(getPointsEntry(pt, line, war._id))
} }
} }
@ -125,7 +126,8 @@ const parseWarLog = (lineArray, war) => {
time: getDateTime(line.split(' ')[5]), time: getDateTime(line.split(' ')[5]),
stabilized: stabilized, stabilized: stabilized,
medic: medic.name, medic: medic.name,
patient: patientName patient: patient.name,
fraction: medic.fraction
}); });
addPlayersIfNotExists([medic, patient]); addPlayersIfNotExists([medic, patient]);
@ -147,6 +149,7 @@ const parseWarLog = (lineArray, war) => {
time: getDateTime(line.split(' ')[5]), time: getDateTime(line.split(' ')[5]),
driver: driver.name, driver: driver.name,
passenger: passenger.name, passenger: passenger.name,
fraction: driver.fraction,
distance: distance distance: distance
}); });
@ -178,12 +181,13 @@ const getRespawnEntry = (respawn, playerName, warId) => {
} }
}; };
const getPointsEntry = (pt, warId) => { const getPointsEntry = (pt, line, warId) => {
return { return {
war: warId, war: warId,
time: getDateTime(pt[5]), time: getDateTime(pt[5]),
ptBlufor: parseInt(pt[12]), ptBlufor: parseInt(pt[12]),
ptOpfor: parseInt(pt[15].slice(0, -1)) ptOpfor: parseInt(pt[15].slice(0, -1)),
fraction: line.includes('no Domination') ? 'NONE': line.includes('NATO +1') ? 'BLUFOR' : 'OPFOR'
} }
}; };
@ -201,7 +205,7 @@ const getPlayerAndFractionFromString = (nameAndFractionString) => {
const nameArray = nameAndFractionString.split(' '); const nameArray = nameAndFractionString.split(' ');
const fraction = nameArray[nameArray.length - 1] !== '(ENEMY)' ? nameArray[nameArray.length - 1] === '(WEST)' ? 'BLUFOR' : 'OPFOR' : undefined; const fraction = nameArray[nameArray.length - 1] !== '(ENEMY)' ? nameArray[nameArray.length - 1] === '(WEST)' ? 'BLUFOR' : 'OPFOR' : undefined;
const name = nameAndFractionString.substring(0, nameAndFractionString.indexOf(nameArray[nameArray.length - 1]) - 1); const name = nameAndFractionString.substring(0, nameAndFractionString.indexOf(nameArray[nameArray.length - 1]) - 1);
// do not return player for 'Selbstverschulden' // do not return player for 'Selbstverschulden' or 'Error: No unit'
if (name && name !== 'Error: No unit') { if (name && name !== 'Error: No unit') {
return {name: name, fraction: fraction}; return {name: name, fraction: fraction};
} }