From 8c9151b351ca6c0ebc99da8161e3c0d7ec003d8c Mon Sep 17 00:00:00 2001 From: Florian Hartwich Date: Sun, 30 Jul 2017 10:57:04 +0200 Subject: [PATCH] Add playercount for log parsing --- api/models/war.js | 4 +-- api/routes/wars.js | 39 +++++++++++++++------ static/src/app/app.component.html | 2 +- static/src/app/wars/war-submit.component.ts | 2 +- 4 files changed, 33 insertions(+), 14 deletions(-) diff --git a/api/models/war.js b/api/models/war.js index 8268982..e0b8de7 100644 --- a/api/models/war.js +++ b/api/models/war.js @@ -28,13 +28,13 @@ const WarSchema = new Schema({ type: Number, get: v => Math.round(v), set: v => Math.round(v), - required: true + default: 0 }, playersOpfor: { type: Number, get: v => Math.round(v), set: v => Math.round(v), - required: true + default: 0 }, bestPlayerId: { type: mongoose.Schema.Types.ObjectId, diff --git a/api/routes/wars.js b/api/routes/wars.js index daa3393..60a9206 100644 --- a/api/routes/wars.js +++ b/api/routes/wars.js @@ -51,11 +51,11 @@ wars.route('/') const war = new WarModel(body); if (req.file) { - war.save((err, item) => { + war.save((err, war) => { if (err) { return next(err); } - const folderName = __dirname + '/../resource/logs/' + item._id; + const folderName = __dirname + '/../resource/logs/' + war._id; mkdirp(folderName, function (err) { if (err) { return next(err); @@ -65,7 +65,7 @@ wars.route('/') return next(err); } //TODO: combine run and clean in one script, so log file gets touched only once - exec(__dirname + '/../war-parser/run.sh ' + folderName + ' ' + item._id, (error, stdout) => { + exec(__dirname + '/../war-parser/run.sh ' + folderName + ' ' + war._id, (error, stdout) => { if (error) { return next(error); } @@ -74,14 +74,33 @@ wars.route('/') return next(error); } let obj = JSON.parse(`${stdout}`); - PlayerModel.create(obj, function (err) { - if (err) { - return next(err); + for (let i = 0; i < obj.length; i++) { + if (obj[i].fraction === 'BLUFOR') { + war.playersBlufor++; + } else { + war.playersOpfor++; } - res.status(codes.created); - res.locals.items = item; - return next(); - }); + } + + WarModel.findByIdAndUpdate(war._id, war, {new: true}, (err, item) => { + if (err) { + 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(); + }); + } + }) }); }); }); diff --git a/static/src/app/app.component.html b/static/src/app/app.component.html index 20123e9..611bdc7 100644 --- a/static/src/app/app.component.html +++ b/static/src/app/app.component.html @@ -24,7 +24,7 @@