From eab910d50567b6483e005534a6a2cdc083e56a7b Mon Sep 17 00:00:00 2001 From: Florian Hartwich Date: Sat, 8 Jul 2017 20:27:56 +0200 Subject: [PATCH] Fix empty String username parsing, Finish POST and GET wars --- api/routes/war.js | 180 ++++++++++++++++++++---------------------- api/war-parser/run.sh | 14 ++-- 2 files changed, 92 insertions(+), 102 deletions(-) diff --git a/api/routes/war.js b/api/routes/war.js index 696e366..225c6d6 100644 --- a/api/routes/war.js +++ b/api/routes/war.js @@ -19,77 +19,77 @@ const routerHandling = require('../middleware/router-handling'); // Mongoose Model using mongoDB const WarModel = require('../models/war'); +const PlayerModel = require('../models/player'); const wars = express.Router(); // routes ********************** wars.route('/') -// .get((req, res, next) => { -// const filter = {}; -// WarModel.find(filter, {}, {sort: {date: 'asc'}}, (err, items) => { -// if (err) { -// err.status = codes.servererror; -// return next(err); -// } -// if (items) { -// res.locals.items = items; -// } else { -// res.locals.items = []; -// } -// res.locals.processed = true; -// next(); -// }); -// }) + .get((req, res, next) => { + const filter = {}; + WarModel.find(filter, {}, {sort: {date: 'asc'}}, (err, items) => { + if (err) { + err.status = codes.servererror; + return next(err); + } + if (items) { + res.locals.items = items; + } else { + res.locals.items = []; + } + res.locals.processed = true; + next(); + }); + }) .post(upload.single('log'), (req, res, next) => { - const war = new WarModel(req.body); - // timestamp and default are set automatically by Mongoose Schema Validation + 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); + if (req.file) { - const timestamp = new Date(); - const uploadDate = timestamp.toISOString().slice(0, 10) + '_' + timestamp.toTimeString().slice(0, 8) - const folderName = __dirname + '/../resource/logs/' + uploadDate; - mkdirp(folderName, function (err) { + war.save((err, item) => { if (err) { return next(err); } - fs.appendFile(folderName + '/war.log', new Buffer(req.file.buffer), (err) => { + const folderName = __dirname + '/../resource/logs/' + item._id; + mkdirp(folderName, function (err) { if (err) { - next(err); + return next(err); } - exec(__dirname + '/../war-parser/run.sh ' + folderName + ' | tee resource/logs/' + uploadDate + '/score.log' , (error, stdout, stderr) => { - console.log("log") - if (error) { - return next(error); + fs.appendFile(folderName + '/war.log', new Buffer(req.file.buffer), (err) => { + if (err) { + return next(err); } - console.log(`stdout: ${stdout}`); - console.log(`stderr: ${stderr}`); - res.locals.items={}; - return next(); + //TODO: combine run and clean in one script + exec(__dirname + '/../war-parser/run.sh ' + folderName + ' ' + item._id, (error, stdout) => { + if (error) { + return next(error); + } + let obj = JSON.parse(`${stdout}`); + + 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(); + }); + }); + }); }); - - // 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 { const err = new Error('no Logfile provided'); err.status = codes.wrongmediasend; @@ -100,48 +100,36 @@ wars.route('/') .all( routerHandling.httpMethodNotAllowed ); -// -// 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); -// } -// res.locals.items = item; -// next(); -// }); -// }) -// -// .delete(apiAuthenticationMiddleware, checkHl, (req, res, next) => { -// WarModel.findByIdAndRemove(req.params.id, (err, item) => { -// if (err) { -// err.status = codes.wrongrequest; -// } -// else if (!item) { -// err = new Error("item not found"); -// err.status = codes.notfound; -// } -// -// // delete graphic -// 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 -// ); + +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); + } + PlayerModel.find({warId: item._id}, (err, items) => { + if (err) { + return next(err); + } + + const responseObj = item.toObject(); + responseObj.players = items; + res.locals.items = responseObj; + return next(); + }); + + }); + }) + + .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 diff --git a/api/war-parser/run.sh b/api/war-parser/run.sh index efa488e..332adc0 100755 --- a/api/war-parser/run.sh +++ b/api/war-parser/run.sh @@ -3,6 +3,7 @@ createScoreboard() { NAME="$1" FILE="$2" + WAR_ID="$3" KILL=0 FF=0 @@ -38,8 +39,8 @@ createScoreboard() { fi done < <(grep -- "${ESC_NAME}" ${FILE}) - printf "\t{\"name\":\"$NAME\", \"fraction\":\"$FRACTION\", \"kill\":${KILL}, \"friendlyFire\":${FF}, \"death\":${DEATH}, \"respawn\":${RESPAWN}}, \"flagTouch\":${FLAG}} " - if [[ -z ${3} ]]; then + printf "\t{\"name\":\"$NAME\", \"fraction\":\"$FRACTION\", \"kill\":${KILL}, \"friendlyFire\":${FF}, \"death\":${DEATH}, \"respawn\":${RESPAWN}, \"flagTouch\":${FLAG}, \"warId\":\"${WAR_ID}\"} " + if [[ -z ${4} ]]; then printf ",\n" else printf "\n" @@ -48,10 +49,11 @@ createScoreboard() { FILE="$1/war.log" +WAR_ID="$2" PLAYERS=() while IFS='' read -r line || [[ -n "$line" ]]; do - if [[ $line =~ [^[:space:]] ]]; then + if [[ -n $line ]]; then case "$line" in *"TFAR_RadioRequestEvent"*) RES=$(echo "$(grep -oP ':[0-9]+\s\(\K.*?(?=\)\sREMOTE)' <<< "$line")") @@ -66,10 +68,10 @@ while IFS='' read -r line || [[ -n "$line" ]]; do esac if [[ $RES != *"Error: No unit"* && $RES1 != *"Error: No unit"* ]]; then - if [[ $RES =~ [^[:space:]] && " ${PLAYERS[*]} " != *" $RES "* ]]; then + if [[ -n $RES && " ${PLAYERS[*]} " != *" $RES "* ]]; then PLAYERS+=("$RES") fi - if [[ $RES1 =~ [^[:space:]] && " ${PLAYERS[*]} " != *" $RES1 "* ]]; then + if [[ -n $RES1 && " ${PLAYERS[*]} " != *" $RES1 "* ]]; then PLAYERS+=("$RES1") fi fi @@ -83,6 +85,6 @@ do if [[ "$((i+1))" -eq "${#PLAYERS[*]}" ]]; then last="true" fi - createScoreboard "${PLAYERS[i]}" ${FILE} ${last} + createScoreboard "${PLAYERS[i]}" ${FILE} ${WAR_ID} ${last} done echo "]"