Fix empty String username parsing, Finish POST and GET wars

pull/1/head
Florian Hartwich 2017-07-08 20:27:56 +02:00
parent 5788f65ce5
commit eab910d505
2 changed files with 92 additions and 102 deletions

View File

@ -19,77 +19,77 @@ const routerHandling = require('../middleware/router-handling');
// Mongoose Model using mongoDB // Mongoose Model using mongoDB
const WarModel = require('../models/war'); const WarModel = require('../models/war');
const PlayerModel = require('../models/player');
const wars = express.Router(); const wars = express.Router();
// routes ********************** // routes **********************
wars.route('/') wars.route('/')
// .get((req, res, next) => { .get((req, res, next) => {
// const filter = {}; const filter = {};
// WarModel.find(filter, {}, {sort: {date: 'asc'}}, (err, items) => { WarModel.find(filter, {}, {sort: {date: 'asc'}}, (err, items) => {
// if (err) { if (err) {
// err.status = codes.servererror; err.status = codes.servererror;
// return next(err); return next(err);
// } }
// if (items) { if (items) {
// res.locals.items = items; res.locals.items = items;
// } else { } else {
// res.locals.items = []; res.locals.items = [];
// } }
// res.locals.processed = true; res.locals.processed = true;
// next(); next();
// }); });
// }) })
.post(upload.single('log'), (req, res, next) => { .post(upload.single('log'), (req, res, next) => {
const war = new WarModel(req.body); let body = req.body;
// timestamp and default are set automatically by Mongoose Schema Validation 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);
if (req.file) { if (req.file) {
const timestamp = new Date(); war.save((err, item) => {
const uploadDate = timestamp.toISOString().slice(0, 10) + '_' + timestamp.toTimeString().slice(0, 8) if (err) {
const folderName = __dirname + '/../resource/logs/' + uploadDate; return next(err);
}
const folderName = __dirname + '/../resource/logs/' + item._id;
mkdirp(folderName, function (err) { mkdirp(folderName, function (err) {
if (err) { if (err) {
return next(err); return next(err);
} }
fs.appendFile(folderName + '/war.log', new Buffer(req.file.buffer), (err) => { fs.appendFile(folderName + '/war.log', new Buffer(req.file.buffer), (err) => {
if (err) { if (err) {
next(err); return next(err);
} }
exec(__dirname + '/../war-parser/run.sh ' + folderName + ' | tee resource/logs/' + uploadDate + '/score.log' , (error, stdout, stderr) => { //TODO: combine run and clean in one script
console.log("log") exec(__dirname + '/../war-parser/run.sh ' + folderName + ' ' + item._id, (error, stdout) => {
if (error) { if (error) {
return next(error); return next(error);
} }
console.log(`stdout: ${stdout}`); let obj = JSON.parse(`${stdout}`);
console.log(`stderr: ${stderr}`);
res.locals.items={}; PlayerModel.create(obj, function (err) {
if (err) {
return next(err);
}
exec(__dirname + '/../war-parser/clean.sh /../' + folderName + ' | tee ' + folderName + '/clean.log', (error) => {
if (error) {
return next(error);
}
res.status(codes.created);
res.locals.items = item;
return next(); return next();
}); });
// exec(__dirname + '/../war-parser/clean.sh ' + folderName + ' | tee resource/logs/' + uploadDate + '/clean.log' , (error) => {
// if (error) {
// return next(error);
// }
//
// });
}); });
}); });
});
});
})
// war.save((err) => {
// if (err) {
// err.status = codes.wrongrequest;
// err.message += ' in fields: ' + Object.getOwnPropertyNames(err.errors);
// return next(err);
// }
// res.status(codes.created);
// res.locals.items = war;
// fs.appendFile(__dirname + '/../resource/squad/' + war.id + '.png', new Buffer(req.file.buffer), (err) => {
// if (err) next(err);
// });
// next();
// })
} else { } else {
const err = new Error('no Logfile provided'); const err = new Error('no Logfile provided');
err.status = codes.wrongmediasend; err.status = codes.wrongmediasend;
@ -100,48 +100,36 @@ wars.route('/')
.all( .all(
routerHandling.httpMethodNotAllowed routerHandling.httpMethodNotAllowed
); );
//
// wars.route('/:id') wars.route('/:id')
// .get((req, res, next) => { .get((req, res, next) => {
// WarModel.findById(req.params.id, (err, item) => { WarModel.findById(req.params.id, (err, item) => {
// if (err) { if (err) {
// err.status = codes.servererror; err.status = codes.servererror;
// return next(err); return next(err);
// } }
// else if (!item) { else if (!item) {
// err = new Error("item not found"); err = new Error("item not found");
// err.status = codes.notfound; err.status = codes.notfound;
// return next(err); return next(err);
// } }
// res.locals.items = item; PlayerModel.find({warId: item._id}, (err, items) => {
// next(); if (err) {
// }); return next(err);
// }) }
//
// .delete(apiAuthenticationMiddleware, checkHl, (req, res, next) => { const responseObj = item.toObject();
// WarModel.findByIdAndRemove(req.params.id, (err, item) => { responseObj.players = items;
// if (err) { res.locals.items = responseObj;
// err.status = codes.wrongrequest; return next();
// } });
// else if (!item) {
// err = new Error("item not found"); });
// err.status = codes.notfound; })
// }
// .all(
// // delete graphic routerHandling.httpMethodNotAllowed
// fs.unlink(__dirname + '/../resource/squad/' + req.params.id + '.png', (err) => { );
// if (err) next(err);
// });
//
// // 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(err); // this works because err is in normal case undefined and that is the same as no parameter
// });
// })
//
// .all(
// routerHandling.httpMethodNotAllowed
// );
// this middleware function can be used, if you like or remove it // 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 // it looks for object(s) in res.locals.items and if they exist, they are send to the client as json

View File

@ -3,6 +3,7 @@
createScoreboard() { createScoreboard() {
NAME="$1" NAME="$1"
FILE="$2" FILE="$2"
WAR_ID="$3"
KILL=0 KILL=0
FF=0 FF=0
@ -38,8 +39,8 @@ createScoreboard() {
fi fi
done < <(grep -- "${ESC_NAME}" ${FILE}) done < <(grep -- "${ESC_NAME}" ${FILE})
printf "\t{\"name\":\"$NAME\", \"fraction\":\"$FRACTION\", \"kill\":${KILL}, \"friendlyFire\":${FF}, \"death\":${DEATH}, \"respawn\":${RESPAWN}}, \"flagTouch\":${FLAG}} " printf "\t{\"name\":\"$NAME\", \"fraction\":\"$FRACTION\", \"kill\":${KILL}, \"friendlyFire\":${FF}, \"death\":${DEATH}, \"respawn\":${RESPAWN}, \"flagTouch\":${FLAG}, \"warId\":\"${WAR_ID}\"} "
if [[ -z ${3} ]]; then if [[ -z ${4} ]]; then
printf ",\n" printf ",\n"
else else
printf "\n" printf "\n"
@ -48,10 +49,11 @@ createScoreboard() {
FILE="$1/war.log" FILE="$1/war.log"
WAR_ID="$2"
PLAYERS=() PLAYERS=()
while IFS='' read -r line || [[ -n "$line" ]]; do while IFS='' read -r line || [[ -n "$line" ]]; do
if [[ $line =~ [^[:space:]] ]]; then if [[ -n $line ]]; then
case "$line" in case "$line" in
*"TFAR_RadioRequestEvent"*) *"TFAR_RadioRequestEvent"*)
RES=$(echo "$(grep -oP ':[0-9]+\s\(\K.*?(?=\)\sREMOTE)' <<< "$line")") RES=$(echo "$(grep -oP ':[0-9]+\s\(\K.*?(?=\)\sREMOTE)' <<< "$line")")
@ -66,10 +68,10 @@ while IFS='' read -r line || [[ -n "$line" ]]; do
esac esac
if [[ $RES != *"Error: No unit"* && $RES1 != *"Error: No unit"* ]]; then if [[ $RES != *"Error: No unit"* && $RES1 != *"Error: No unit"* ]]; then
if [[ $RES =~ [^[:space:]] && " ${PLAYERS[*]} " != *" $RES "* ]]; then if [[ -n $RES && " ${PLAYERS[*]} " != *" $RES "* ]]; then
PLAYERS+=("$RES") PLAYERS+=("$RES")
fi fi
if [[ $RES1 =~ [^[:space:]] && " ${PLAYERS[*]} " != *" $RES1 "* ]]; then if [[ -n $RES1 && " ${PLAYERS[*]} " != *" $RES1 "* ]]; then
PLAYERS+=("$RES1") PLAYERS+=("$RES1")
fi fi
fi fi
@ -83,6 +85,6 @@ do
if [[ "$((i+1))" -eq "${#PLAYERS[*]}" ]]; then if [[ "$((i+1))" -eq "${#PLAYERS[*]}" ]]; then
last="true" last="true"
fi fi
createScoreboard "${PLAYERS[i]}" ${FILE} ${last} createScoreboard "${PLAYERS[i]}" ${FILE} ${WAR_ID} ${last}
done done
echo "]" echo "]"