2018-03-12 09:26:44 +01:00
|
|
|
'use strict';
|
2017-07-08 15:34:36 +02:00
|
|
|
|
|
|
|
// modules
|
|
|
|
const fs = require('fs');
|
2018-03-12 09:26:44 +01:00
|
|
|
const mkdirp = require('mkdirp');
|
2017-07-08 15:34:36 +02:00
|
|
|
const express = require('express');
|
|
|
|
const multer = require('multer');
|
|
|
|
const storage = multer.memoryStorage();
|
|
|
|
const upload = multer({storage: storage});
|
|
|
|
|
|
|
|
// HTTP status codes by name
|
|
|
|
const codes = require('./http-codes');
|
|
|
|
|
2018-02-04 16:36:42 +01:00
|
|
|
// access check
|
2017-07-08 15:34:36 +02:00
|
|
|
const apiAuthenticationMiddleware = require('../middleware/auth-middleware');
|
2017-07-08 22:53:06 +02:00
|
|
|
const checkMT = require('../middleware/permission-check').checkMT;
|
2018-02-04 16:36:42 +01:00
|
|
|
|
2017-07-08 15:34:36 +02:00
|
|
|
const routerHandling = require('../middleware/router-handling');
|
2018-02-04 16:36:42 +01:00
|
|
|
const idValidator = require('../middleware/validators').idValidator;
|
2018-03-30 20:21:28 +02:00
|
|
|
const resourceLocation = require('../middleware/resource-location').resourceLocation().concat('/logs/');
|
2017-07-08 15:34:36 +02:00
|
|
|
|
2018-02-04 16:36:42 +01:00
|
|
|
// log paser tool
|
2017-10-20 23:42:41 +02:00
|
|
|
const parseWarLog = require('../tools/log-parse-tool');
|
|
|
|
|
2017-07-08 15:34:36 +02:00
|
|
|
// Mongoose Model using mongoDB
|
|
|
|
const WarModel = require('../models/war');
|
2017-07-08 20:27:56 +02:00
|
|
|
const PlayerModel = require('../models/player');
|
2017-10-21 18:23:04 +02:00
|
|
|
const LogKillModel = require('../models/logs/kill');
|
2018-03-03 12:26:24 +01:00
|
|
|
const LogVehicleKillModel = require('../models/logs/vehicle');
|
2017-10-21 18:23:04 +02:00
|
|
|
const LogRespawnModel = require('../models/logs/respawn');
|
|
|
|
const LogReviveModel = require('../models/logs/revive');
|
|
|
|
const LogTransportModel = require('../models/logs/transport');
|
|
|
|
const LogFlagModel = require('../models/logs/flag');
|
|
|
|
const LogBudgetModel = require('../models/logs/budget');
|
2017-10-21 19:49:24 +02:00
|
|
|
const LogPointsModel = require('../models/logs/points');
|
2019-02-09 11:54:16 +01:00
|
|
|
const LogPlayerCountModel = require('../models/logs/player-count');
|
2019-02-09 20:42:24 +01:00
|
|
|
const LogServerFpsModel = require('../models/logs/server-fps');
|
2017-07-08 15:34:36 +02:00
|
|
|
|
2018-05-04 20:37:35 +02:00
|
|
|
// util
|
|
|
|
const genericPatch = require('./_generic').genericPatch;
|
|
|
|
|
2018-03-12 10:39:56 +01:00
|
|
|
const wars = new express.Router();
|
2017-07-08 15:34:36 +02:00
|
|
|
|
|
|
|
// routes **********************
|
|
|
|
wars.route('/')
|
2018-02-26 09:04:27 +01:00
|
|
|
.get((req, res, next) => {
|
2018-10-09 10:42:28 +02:00
|
|
|
const filter = {};
|
|
|
|
if (req.query.campaignId) {
|
|
|
|
filter.campaign = req.query.campaignId;
|
|
|
|
}
|
|
|
|
WarModel.find(filter, {}, {sort: {date: 'desc'}}, (err, wars) => {
|
2018-02-26 09:04:27 +01:00
|
|
|
if (err) {
|
|
|
|
err.status = codes.servererror;
|
|
|
|
return next(err);
|
|
|
|
}
|
2018-10-09 10:42:28 +02:00
|
|
|
res.locals.items = wars;
|
|
|
|
res.locals.processed = true;
|
|
|
|
next();
|
2018-03-12 09:26:44 +01:00
|
|
|
});
|
2017-08-12 16:28:18 +02:00
|
|
|
})
|
2017-07-08 15:34:36 +02:00
|
|
|
|
2018-02-26 09:04:27 +01:00
|
|
|
.post(apiAuthenticationMiddleware, checkMT, upload.single('log'), (req, res, next) => {
|
2019-02-09 20:42:24 +01:00
|
|
|
const body = req.body;
|
|
|
|
const warBody = new WarModel(body);
|
2017-07-08 20:27:56 +02:00
|
|
|
|
2019-02-09 20:42:24 +01:00
|
|
|
if (req.file) {
|
|
|
|
fs.readFile(req.file.buffer, (file, err) => {
|
2017-10-21 14:54:58 +02:00
|
|
|
if (err) {
|
|
|
|
return next(err);
|
|
|
|
}
|
2019-02-09 20:42:24 +01:00
|
|
|
const lineArray = file.toString().split('\n');
|
|
|
|
const statsResult = parseWarLog(lineArray, warBody);
|
|
|
|
statsResult.war.save((err, war) => {
|
2018-02-26 09:04:27 +01:00
|
|
|
if (err) {
|
|
|
|
return next(err);
|
|
|
|
}
|
2019-02-10 00:54:25 +01:00
|
|
|
LogKillModel.create(statsResult.kills, (err) => {
|
|
|
|
if (err) console.log(err);
|
|
|
|
LogVehicleKillModel.create(statsResult.vehicles, (err) => {
|
|
|
|
if (err) console.log(err);
|
|
|
|
LogRespawnModel.create(statsResult.respawn, (err) => {
|
|
|
|
if (err) console.log(err);
|
|
|
|
LogReviveModel.create(statsResult.revive, (err) => {
|
|
|
|
if (err) console.log(err);
|
|
|
|
LogFlagModel.create(statsResult.flag, (err) => {
|
|
|
|
if (err) console.log(err);
|
|
|
|
LogBudgetModel.create(statsResult.budget, (err) => {
|
|
|
|
if (err) console.log(err);
|
|
|
|
LogTransportModel.create(statsResult.transport, (err) => {
|
|
|
|
if (err) console.log(err);
|
|
|
|
LogPlayerCountModel.create(statsResult.playerCount, (err) => {
|
|
|
|
if (err) console.log(err);
|
|
|
|
LogPointsModel.create(statsResult.points, (err) => {
|
|
|
|
if (err) console.log(err);
|
2019-02-09 20:42:24 +01:00
|
|
|
LogServerFpsModel.create(statsResult.serverFps, (err, serverPerformanceEntries) => {
|
2019-02-10 00:54:25 +01:00
|
|
|
if (err) console.log(err);
|
|
|
|
if (serverPerformanceEntries) {
|
|
|
|
serverPerformanceEntries.forEach((entry) => {
|
|
|
|
const idx = statsResult.players
|
|
|
|
.findIndex((player) => player.name === entry.entityName);
|
|
|
|
if (idx !== -1) {
|
|
|
|
const player = statsResult.players[idx];
|
|
|
|
player['performance'] = entry._id;
|
|
|
|
statsResult.players[idx] = player;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2019-02-09 20:42:24 +01:00
|
|
|
PlayerModel.create(statsResult.players, (err) => {
|
|
|
|
if (err) {
|
|
|
|
return next(err);
|
|
|
|
}
|
|
|
|
const folderName = resourceLocation.concat(war._id);
|
|
|
|
mkdirp(folderName, (err) => {
|
|
|
|
if (err) return next(err);
|
|
|
|
|
|
|
|
// save clean log file
|
|
|
|
const cleanFile = fs.createWriteStream(folderName + '/clean.log');
|
|
|
|
statsResult.clean.forEach((cleanLine) => {
|
|
|
|
cleanFile.write(cleanLine + '\n\n');
|
|
|
|
});
|
|
|
|
cleanFile.end();
|
|
|
|
|
|
|
|
// save raw log file
|
|
|
|
const rawFile = fs.createWriteStream(folderName + '/war.log');
|
|
|
|
lineArray.forEach((rawLine) => {
|
|
|
|
rawFile.write(rawLine + '\n');
|
|
|
|
});
|
|
|
|
rawFile.end();
|
|
|
|
|
|
|
|
res.status(codes.created);
|
|
|
|
res.locals.items = war;
|
|
|
|
next();
|
|
|
|
});
|
2019-02-09 11:54:16 +01:00
|
|
|
});
|
|
|
|
});
|
2018-03-12 09:26:44 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2019-02-09 20:42:24 +01:00
|
|
|
} else {
|
|
|
|
const err = new Error('no Logfile provided');
|
|
|
|
err.status = codes.wrongmediasend;
|
|
|
|
next(err);
|
|
|
|
}
|
2018-02-26 09:04:27 +01:00
|
|
|
}
|
2019-02-09 20:42:24 +01:00
|
|
|
)
|
2017-07-08 15:34:36 +02:00
|
|
|
|
2018-02-26 09:04:27 +01:00
|
|
|
.all(
|
|
|
|
routerHandling.httpMethodNotAllowed
|
|
|
|
);
|
2017-07-08 20:27:56 +02:00
|
|
|
|
|
|
|
wars.route('/:id')
|
2018-02-26 09:04:27 +01:00
|
|
|
.get(idValidator, (req, res, next) => {
|
|
|
|
WarModel.findById(req.params.id, (err, item) => {
|
2017-07-08 20:27:56 +02:00
|
|
|
if (err) {
|
2018-02-26 09:04:27 +01:00
|
|
|
err.status = codes.servererror;
|
2017-07-08 20:27:56 +02:00
|
|
|
return next(err);
|
2018-03-12 09:26:44 +01:00
|
|
|
} else if (!item) {
|
|
|
|
err = new Error('item not found');
|
2018-02-26 09:04:27 +01:00
|
|
|
err.status = codes.notfound;
|
|
|
|
return next(err);
|
|
|
|
}
|
|
|
|
PlayerModel.find({warId: item._id}, {}, {sort: {kill: 'desc'}}, (err, items) => {
|
|
|
|
if (err) {
|
|
|
|
return next(err);
|
|
|
|
}
|
2017-07-08 20:27:56 +02:00
|
|
|
|
2020-04-19 12:47:22 +02:00
|
|
|
// TODO: temp solution for CC-93 - add boat kills to light vehicle kills until FE available
|
|
|
|
items.forEach((player) => {
|
|
|
|
player.vehicleLight = player.vehicleLight + player.vehicleBoat;
|
|
|
|
});
|
|
|
|
|
2018-02-26 09:04:27 +01:00
|
|
|
const responseObj = item.toObject();
|
|
|
|
responseObj.players = items;
|
|
|
|
res.locals.items = responseObj;
|
|
|
|
return next();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
})
|
2017-07-09 11:32:20 +02:00
|
|
|
|
2018-04-29 11:12:09 +02:00
|
|
|
.patch(apiAuthenticationMiddleware, checkMT, (req, res, next) => {
|
2018-05-04 20:37:35 +02:00
|
|
|
return genericPatch(req, res, next, WarModel);
|
2018-04-29 11:12:09 +02:00
|
|
|
})
|
|
|
|
|
2018-02-26 09:04:27 +01:00
|
|
|
.delete(apiAuthenticationMiddleware, checkMT, (req, res, next) => {
|
|
|
|
WarModel.findByIdAndRemove(req.params.id, (err, item) => {
|
|
|
|
if (err) {
|
|
|
|
err.status = codes.wrongrequest;
|
|
|
|
return next(err);
|
2018-03-12 09:26:44 +01:00
|
|
|
} else if (!item) {
|
|
|
|
err = new Error('item not found');
|
2018-02-26 09:04:27 +01:00
|
|
|
err.status = codes.notfound;
|
|
|
|
return next(err);
|
2017-07-15 10:54:35 +02:00
|
|
|
}
|
2018-02-26 09:04:27 +01:00
|
|
|
|
|
|
|
// delete linked appearances
|
|
|
|
PlayerModel.find({warId: item._id}).remove().exec();
|
|
|
|
LogKillModel.find({war: item._id}).remove().exec();
|
2018-06-09 19:33:22 +02:00
|
|
|
LogVehicleKillModel.find({war: item._id}).remove().exec();
|
2018-02-26 09:04:27 +01:00
|
|
|
LogRespawnModel.find({war: item._id}).remove().exec();
|
|
|
|
LogReviveModel.find({war: item._id}).remove().exec();
|
|
|
|
LogFlagModel.find({war: item._id}).remove().exec();
|
|
|
|
LogBudgetModel.find({war: item._id}).remove().exec();
|
|
|
|
LogTransportModel.find({war: item._id}).remove().exec();
|
|
|
|
LogPointsModel.find({war: item._id}).remove().exec();
|
2019-02-09 12:41:02 +01:00
|
|
|
LogPlayerCountModel.find({war: item._id}).remove().exec();
|
2018-02-26 09:04:27 +01:00
|
|
|
|
|
|
|
// check if logfiles exist and delete from fs
|
2018-03-30 20:21:28 +02:00
|
|
|
const warDir = resourceLocation + req.params.id;
|
2018-02-26 09:04:27 +01:00
|
|
|
|
|
|
|
if (fs.existsSync(warDir)) {
|
|
|
|
const cleanLog = warDir + '/clean.log';
|
|
|
|
if (fs.existsSync(cleanLog)) {
|
|
|
|
fs.unlink(cleanLog, (err) => {
|
|
|
|
});
|
|
|
|
}
|
|
|
|
const sourceLog = warDir + '/war.log';
|
|
|
|
if (fs.existsSync(sourceLog)) {
|
|
|
|
fs.unlink(sourceLog, (err) => {
|
|
|
|
});
|
|
|
|
}
|
|
|
|
fs.rmdir(warDir, (err) => {
|
2017-07-15 10:54:35 +02:00
|
|
|
});
|
|
|
|
}
|
2018-02-26 09:04:27 +01:00
|
|
|
// we don't set res.locals.items and thus it will send a 204 (no content) at the end. see last handler
|
|
|
|
// user.use(..)
|
|
|
|
res.locals.processed = true;
|
|
|
|
next();
|
2018-03-12 09:26:44 +01:00
|
|
|
});
|
2017-07-09 11:32:20 +02:00
|
|
|
})
|
|
|
|
|
2018-02-26 09:04:27 +01:00
|
|
|
.all(
|
|
|
|
routerHandling.httpMethodNotAllowed
|
|
|
|
);
|
2017-07-08 15:34:36 +02:00
|
|
|
|
|
|
|
// this middleware function can be used, if you like or remove it
|
|
|
|
// it looks for object(s) in res.locals.items and if they exist, they are send to the client as json
|
|
|
|
wars.use(routerHandling.emptyResponse);
|
|
|
|
|
|
|
|
module.exports = wars;
|