opt-cc/api/routes/wars.js

231 lines
7.9 KiB
JavaScript
Raw Normal View History

"use strict";
// modules
const fs = require('fs');
const mkdirp = require("mkdirp");
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');
// HTTP status codes by name
const codes = require('./http-codes');
// access check
const apiAuthenticationMiddleware = require('../middleware/auth-middleware');
const checkMT = require('../middleware/permission-check').checkMT;
const routerHandling = require('../middleware/router-handling');
const idValidator = require('../middleware/validators').idValidator;
// log paser tool
2017-10-20 23:42:41 +02:00
const parseWarLog = require('../tools/log-parse-tool');
// Mongoose Model using mongoDB
2017-08-12 16:28:18 +02:00
const CampaignModel = require('../models/campaign');
const WarModel = require('../models/war');
const PlayerModel = require('../models/player');
2017-10-21 18:23:04 +02:00
const LogKillModel = require('../models/logs/kill');
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');
const LogPointsModel = require('../models/logs/points');
const wars = express.Router();
// routes **********************
wars.route('/')
2018-02-26 09:04:27 +01:00
.get((req, res, next) => {
let result = [];
CampaignModel.find({}, {}, {sort: {timestamp: 'desc'}}, (err, campaigns) => {
if (err) {
err.status = codes.servererror;
return next(err);
}
if (campaigns) {
WarModel.find({}, {}, {sort: {date: 'desc'}}, (err, wars) => {
if (err) {
err.status = codes.servererror;
return next(err);
}
if (wars) {
campaigns.forEach(campaign => {
let entry = {_id: campaign._id, title: campaign.title, wars: []};
wars.forEach((war) => {
if (String(campaign._id) === String(war.campaign)) {
entry.wars.push(war);
}
});
result.push(entry);
2017-08-12 16:28:18 +02:00
});
2018-02-26 09:04:27 +01:00
res.locals.items = result;
}
res.locals.processed = true;
next();
2017-08-12 16:28:18 +02:00
}
2018-02-26 09:04:27 +01:00
)
;
}
})
2017-08-12 16:28:18 +02:00
})
2018-02-26 09:04:27 +01:00
.post(apiAuthenticationMiddleware, checkMT, upload.single('log'), (req, res, next) => {
const body = req.body;
const warBody = new WarModel(body);
2018-02-26 09:04:27 +01:00
if (req.file) {
fs.readFile(req.file.buffer, (file, err) => {
if (err) {
return next(err);
}
2018-02-26 09:04:27 +01:00
const lineArray = file.toString().split("\n");
const statsResult = parseWarLog(lineArray, warBody);
statsResult.war.save((err, war) => {
2017-10-21 14:54:58 +02:00
if (err) {
return next(err);
}
2018-02-26 09:04:27 +01:00
PlayerModel.create(statsResult.players, function (err) {
if (err) {
return next(err);
}
LogKillModel.create(statsResult.kills, function () {
LogVehicleKillModel.create(statsResult.vehicles, function () {
2018-02-26 09:04:27 +01:00
LogRespawnModel.create(statsResult.respawn, function () {
LogReviveModel.create(statsResult.revive, function () {
LogFlagModel.create(statsResult.flag, function () {
LogBudgetModel.create(statsResult.budget, function () {
LogTransportModel.create(statsResult.transport, function () {
LogPointsModel.create(statsResult.points, function () {
const folderName = __dirname + '/../resource/logs/' + war._id;
mkdirp(folderName, function (err) {
if (err) return next(err);
2018-02-26 09:04:27 +01:00
// save clean log file
const cleanFile = fs.createWriteStream(folderName + '/clean.log');
statsResult.clean.forEach(cleanLine => {
cleanFile.write(cleanLine + '\n\n')
});
cleanFile.end();
2018-02-26 09:04:27 +01:00
// 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();
})
2018-02-26 09:04:27 +01:00
})
})
})
2017-10-21 18:23:04 +02:00
})
})
})
})
})
})
})
2018-02-26 09:04:27 +01:00
});
2017-10-21 14:54:58 +02:00
2018-02-26 09:04:27 +01:00
} else {
const err = new Error('no Logfile provided');
err.status = codes.wrongmediasend;
next(err);
}
})
2018-02-26 09:04:27 +01:00
.all(
routerHandling.httpMethodNotAllowed
);
wars.route('/:id')
2018-02-26 09:04:27 +01:00
.get(idValidator, (req, res, next) => {
WarModel.findById(req.params.id, (err, item) => {
if (err) {
2018-02-26 09:04:27 +01:00
err.status = codes.servererror;
return next(err);
}
2018-02-26 09:04:27 +01:00
else if (!item) {
err = new Error("item not found");
err.status = codes.notfound;
return next(err);
}
PlayerModel.find({warId: item._id}, {}, {sort: {kill: 'desc'}}, (err, items) => {
if (err) {
return next(err);
}
2018-02-26 09:04:27 +01:00
const responseObj = item.toObject();
responseObj.players = items;
res.locals.items = responseObj;
return next();
});
2018-02-26 09:04:27 +01:00
});
})
2017-07-09 11:32:20 +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);
}
else if (!item) {
err = new Error("item not found");
err.status = codes.notfound;
return next(err);
}
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();
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();
// 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) => {
});
}
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();
2017-07-09 11:32:20 +02:00
2018-02-26 09:04:27 +01:00
})
2017-07-09 11:32:20 +02:00
})
2018-02-26 09:04:27 +01:00
.all(
routerHandling.httpMethodNotAllowed
);
// 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;