2017-07-08 15:34:36 +02:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
// modules
|
|
|
|
const fs = require('fs');
|
|
|
|
const mkdirp = require("mkdirp");
|
|
|
|
const {exec} = require('child_process');
|
|
|
|
const express = require('express');
|
|
|
|
const multer = require('multer');
|
|
|
|
const storage = multer.memoryStorage();
|
|
|
|
const upload = multer({storage: storage});
|
2017-07-15 12:03:38 +02:00
|
|
|
const logger = require('debug')('cc:wars');
|
2017-07-08 15:34:36 +02:00
|
|
|
|
|
|
|
// HTTP status codes by name
|
|
|
|
const codes = require('./http-codes');
|
|
|
|
|
|
|
|
const apiAuthenticationMiddleware = require('../middleware/auth-middleware');
|
2017-07-08 22:53:06 +02:00
|
|
|
const checkMT = require('../middleware/permission-check').checkMT;
|
2017-07-08 15:34:36 +02:00
|
|
|
const routerHandling = require('../middleware/router-handling');
|
|
|
|
|
|
|
|
// Mongoose Model using mongoDB
|
2017-08-12 16:28:18 +02:00
|
|
|
const CampaignModel = require('../models/campaign');
|
2017-07-08 15:34:36 +02:00
|
|
|
const WarModel = require('../models/war');
|
2017-07-08 20:27:56 +02:00
|
|
|
const PlayerModel = require('../models/player');
|
2017-07-08 15:34:36 +02:00
|
|
|
|
|
|
|
const wars = express.Router();
|
|
|
|
|
|
|
|
// routes **********************
|
|
|
|
wars.route('/')
|
2017-07-08 20:27:56 +02:00
|
|
|
.get((req, res, next) => {
|
2017-08-12 16:28:18 +02:00
|
|
|
let result = [];
|
|
|
|
CampaignModel.find({}, {}, {sort: {timestamp: 'desc'}}, (err, campaigns) => {
|
2017-07-08 20:27:56 +02:00
|
|
|
if (err) {
|
|
|
|
err.status = codes.servererror;
|
|
|
|
return next(err);
|
|
|
|
}
|
2017-08-12 16:28:18 +02:00
|
|
|
if (campaigns) {
|
|
|
|
WarModel.find({}, {}, {sort: {date: 'desc'}}, (err, wars) => {
|
|
|
|
if (err) {
|
|
|
|
err.status = codes.servererror;
|
|
|
|
return next(err);
|
|
|
|
}
|
|
|
|
if (wars) {
|
|
|
|
campaigns.forEach(campaign => {
|
2017-08-12 22:27:23 +02:00
|
|
|
let entry = {_id: campaign._id, title: campaign.title, wars: []};
|
2017-08-12 16:28:18 +02:00
|
|
|
wars.forEach((war) => {
|
|
|
|
if (String(campaign._id) === String(war.campaign)) {
|
|
|
|
entry.wars.push(war);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
result.push(entry);
|
|
|
|
});
|
|
|
|
res.locals.items = result;
|
|
|
|
}
|
|
|
|
res.locals.processed = true;
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
)
|
|
|
|
;
|
2017-07-08 20:27:56 +02:00
|
|
|
}
|
2017-08-12 16:28:18 +02:00
|
|
|
})
|
2017-07-08 20:27:56 +02:00
|
|
|
})
|
2017-07-08 15:34:36 +02:00
|
|
|
|
2017-07-09 18:45:07 +02:00
|
|
|
.post(apiAuthenticationMiddleware, checkMT, upload.single('log'), (req, res, next) => {
|
2017-07-08 20:27:56 +02:00
|
|
|
let body = req.body;
|
|
|
|
let parts = body.date.split("-");
|
|
|
|
body.date = new Date(parseInt(parts[0], 10),
|
|
|
|
parseInt(parts[1], 10) - 1,
|
|
|
|
parseInt(parts[2], 10));
|
|
|
|
const war = new WarModel(body);
|
|
|
|
|
2017-07-08 15:34:36 +02:00
|
|
|
if (req.file) {
|
2017-07-30 10:57:04 +02:00
|
|
|
war.save((err, war) => {
|
2017-07-08 15:34:36 +02:00
|
|
|
if (err) {
|
|
|
|
return next(err);
|
|
|
|
}
|
2017-07-30 10:57:04 +02:00
|
|
|
const folderName = __dirname + '/../resource/logs/' + war._id;
|
2017-07-08 20:27:56 +02:00
|
|
|
mkdirp(folderName, function (err) {
|
2017-07-08 15:34:36 +02:00
|
|
|
if (err) {
|
2017-07-08 20:27:56 +02:00
|
|
|
return next(err);
|
2017-07-08 15:34:36 +02:00
|
|
|
}
|
2017-07-08 20:27:56 +02:00
|
|
|
fs.appendFile(folderName + '/war.log', new Buffer(req.file.buffer), (err) => {
|
|
|
|
if (err) {
|
|
|
|
return next(err);
|
2017-07-08 15:34:36 +02:00
|
|
|
}
|
2017-07-30 10:57:04 +02:00
|
|
|
exec(__dirname + '/../war-parser/run.sh ' + folderName + ' ' + war._id, (error, stdout) => {
|
2017-07-08 20:27:56 +02:00
|
|
|
if (error) {
|
|
|
|
return next(error);
|
|
|
|
}
|
2017-07-14 23:33:17 +02:00
|
|
|
exec(__dirname + '/../war-parser/clean.sh /../' + folderName + ' | tee ' + folderName + '/clean.log', (error) => {
|
|
|
|
if (error) {
|
|
|
|
return next(error);
|
2017-07-08 20:27:56 +02:00
|
|
|
}
|
2017-07-14 23:33:17 +02:00
|
|
|
let obj = JSON.parse(`${stdout}`);
|
2017-07-30 10:57:04 +02:00
|
|
|
for (let i = 0; i < obj.length; i++) {
|
2017-08-13 09:16:03 +02:00
|
|
|
if (!obj[i].fraction) {
|
|
|
|
obj.splice(i, 1);
|
|
|
|
} else if (obj[i].fraction === 'BLUFOR') {
|
2017-07-30 10:57:04 +02:00
|
|
|
war.playersBlufor++;
|
|
|
|
} else {
|
|
|
|
war.playersOpfor++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
WarModel.findByIdAndUpdate(war._id, war, {new: true}, (err, item) => {
|
2017-07-14 23:33:17 +02:00
|
|
|
if (err) {
|
2017-07-30 10:57:04 +02:00
|
|
|
err.status = codes.wrongrequest;
|
|
|
|
}
|
|
|
|
else if (!item) {
|
|
|
|
err = new Error("item not found");
|
|
|
|
err.status = codes.notfound;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PlayerModel.create(obj, function (err) {
|
|
|
|
if (err) {
|
|
|
|
return next(err);
|
|
|
|
}
|
|
|
|
res.status(codes.created);
|
|
|
|
res.locals.items = war;
|
|
|
|
return next();
|
|
|
|
});
|
2017-07-08 20:27:56 +02:00
|
|
|
}
|
2017-07-30 10:57:04 +02:00
|
|
|
})
|
2017-07-08 20:27:56 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2017-07-08 15:34:36 +02:00
|
|
|
});
|
2017-07-08 20:27:56 +02:00
|
|
|
})
|
2017-07-08 15:34:36 +02:00
|
|
|
|
|
|
|
} else {
|
|
|
|
const err = new Error('no Logfile provided');
|
|
|
|
err.status = codes.wrongmediasend;
|
|
|
|
next(err);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
.all(
|
|
|
|
routerHandling.httpMethodNotAllowed
|
|
|
|
);
|
2017-07-08 20:27:56 +02:00
|
|
|
|
|
|
|
wars.route('/:id')
|
|
|
|
.get((req, res, next) => {
|
|
|
|
WarModel.findById(req.params.id, (err, item) => {
|
|
|
|
if (err) {
|
|
|
|
err.status = codes.servererror;
|
|
|
|
return next(err);
|
|
|
|
}
|
|
|
|
else if (!item) {
|
|
|
|
err = new Error("item not found");
|
|
|
|
err.status = codes.notfound;
|
|
|
|
return next(err);
|
|
|
|
}
|
2017-09-13 11:49:34 +02:00
|
|
|
PlayerModel.find({warId: item._id}, {}, {sort: {kill: 'desc'}}, (err, items) => {
|
2017-07-08 20:27:56 +02:00
|
|
|
if (err) {
|
|
|
|
return next(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
const responseObj = item.toObject();
|
|
|
|
responseObj.players = items;
|
|
|
|
res.locals.items = responseObj;
|
|
|
|
return next();
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
})
|
|
|
|
|
2017-07-09 17:08:32 +02:00
|
|
|
.delete(apiAuthenticationMiddleware, checkMT, (req, res, next) => {
|
2017-07-09 11:32:20 +02:00
|
|
|
WarModel.findByIdAndRemove(req.params.id, (err, item) => {
|
|
|
|
if (err) {
|
|
|
|
err.status = codes.wrongrequest;
|
2017-07-14 23:50:58 +02:00
|
|
|
return next(err);
|
2017-07-09 11:32:20 +02:00
|
|
|
}
|
|
|
|
else if (!item) {
|
|
|
|
err = new Error("item not found");
|
|
|
|
err.status = codes.notfound;
|
2017-07-14 23:50:58 +02:00
|
|
|
return next(err);
|
2017-07-09 11:32:20 +02:00
|
|
|
}
|
|
|
|
|
2017-07-15 10:54:35 +02:00
|
|
|
// delete players having this war ID as foreign key
|
2017-07-09 11:32:20 +02:00
|
|
|
PlayerModel.find({warId: item._id}).remove().exec();
|
|
|
|
|
2017-07-15 10:54:35 +02:00
|
|
|
// check if logfiles exist and delete from fs
|
|
|
|
const warDir = __dirname + '/../resource/logs/' + req.params.id;
|
|
|
|
|
|
|
|
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-09 11:32:20 +02: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;
|
2017-07-14 23:50:58 +02:00
|
|
|
next();
|
2017-07-09 11:32:20 +02:00
|
|
|
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-07-08 20:27:56 +02: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;
|