From 75019a619fc9852a560f64f0fb96c9f0d3e73baf Mon Sep 17 00:00:00 2001 From: HardiReady Date: Sun, 22 Oct 2017 14:13:58 +0200 Subject: [PATCH] Fix first line parse error; Add file save; Add player sum-point-value --- api/models/player.js | 5 + api/routes/wars.js | 44 ++++++--- api/tools/log-parse-tool.js | 17 +++- api/war-parser/clean.sh | 20 ---- api/war-parser/run.sh | 97 ------------------- .../war-detail/war-detail.component.html | 13 +-- 6 files changed, 57 insertions(+), 139 deletions(-) delete mode 100755 api/war-parser/clean.sh delete mode 100755 api/war-parser/run.sh diff --git a/api/models/player.js b/api/models/player.js index fcf4b07..8086056 100644 --- a/api/models/player.js +++ b/api/models/player.js @@ -53,6 +53,11 @@ const PlayerSchema = new Schema({ get: v => Math.round(v), set: v => Math.round(v), required: true + }, + sort: { + type: Number, + get: v => Math.round(v), + set: v => Math.round(v) } }, { collection: 'player', diff --git a/api/routes/wars.js b/api/routes/wars.js index 9424a1c..8f73f41 100644 --- a/api/routes/wars.js +++ b/api/routes/wars.js @@ -3,7 +3,6 @@ // 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(); @@ -88,16 +87,35 @@ wars.route('/') if (err) { return next(err); } - LogKillModel.create(statsResult.kills, function (err) { - LogRespawnModel.create(statsResult.respawn, function (err) { - LogReviveModel.create(statsResult.revive, function (err) { - LogFlagModel.create(statsResult.flag, function (err) { - LogBudgetModel.create(statsResult.budget, function (err) { - LogTransportModel.create(statsResult.transport, function (err) { - LogPointsModel.create(statsResult.points, function (err) { - res.status(codes.created); - res.locals.items = war; - next(); + LogKillModel.create(statsResult.kills, function () { + 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); + + // save clean log file + const cleanFile = fs.createWriteStream(folderName + '/clean.log'); + statsResult.clean.forEach(cleanLine => { + cleanFile.write(cleanLine + '\n\n') + }); + cleanFile.end(); + + // 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(); + }) }) }) }) @@ -105,7 +123,7 @@ wars.route('/') }) }) }) - }); + }) }) }); @@ -132,7 +150,7 @@ wars.route('/:id') err.status = codes.notfound; return next(err); } - PlayerModel.find({warId: item._id}, {}, {sort: {kill: 'desc'}}, (err, items) => { + PlayerModel.find({warId: item._id}, {}, {sort: {sort: 'desc'}}, (err, items) => { if (err) { return next(err); } diff --git a/api/tools/log-parse-tool.js b/api/tools/log-parse-tool.js index 2b76d9b..b7dbbfc 100644 --- a/api/tools/log-parse-tool.js +++ b/api/tools/log-parse-tool.js @@ -3,6 +3,7 @@ const arrayContains = require('./util').arrayContains; const parseWarLog = (lineArray, war) => { + const nameToLongError = 'Error: ENAMETOOLONG: name too long, open \''; const stats = { war: war, clean: [], @@ -25,7 +26,14 @@ const parseWarLog = (lineArray, war) => { }) }; - lineArray.forEach(line => { + lineArray.some(line => { + /** + * sanitize nameToLongError coming up in first line + */ + if (line.includes(nameToLongError)) { + line = line.substring(line.indexOf(nameToLongError) + nameToLongError.length); + } + /** * KILLS */ @@ -94,6 +102,7 @@ const parseWarLog = (lineArray, war) => { if (line.includes('Endpunktestand')) { stats.war['ptBlufor'] = parseInt(pt[11]); stats.war['ptOpfor'] = parseInt(pt[14].slice(0, -1)); + return true; } else { stats.points.push(getPointsEntry(pt, line, war._id)) } @@ -148,7 +157,7 @@ const parseWarLog = (lineArray, war) => { war: war._id, time: getDateTime(line.split(' ')[5]), driver: driver.name, - passenger: passenger.name, + passenger: passenger ? passenger.name : null, fraction: driver.fraction, distance: distance }); @@ -165,6 +174,8 @@ const parseWarLog = (lineArray, war) => { stats.players[i]['death'] = stats.kills.filter(kill => kill.target === playerName).length; stats.players[i]['revive'] = stats.revive.filter(rev => rev.medic === playerName && !rev.stabilized).length; stats.players[i]['flagTouch'] = stats.flag.filter(flag => flag.player === playerName).length; + stats.players[i]['sort'] = stats.players[i]['kill'] + stats.players[i]['revive'] + stats.players[i]['flagTouch'] + - stats.players[i]['friendlyFire'] - stats.players[i]['death'] - stats.players[i]['respawn'] } stats.war.playersBlufor = stats.players.filter(player => player.fraction === 'BLUFOR').length; @@ -187,7 +198,7 @@ const getPointsEntry = (pt, line, warId) => { time: getDateTime(pt[5]), ptBlufor: parseInt(pt[12]), ptOpfor: parseInt(pt[15].slice(0, -1)), - fraction: line.includes('no Domination') ? 'NONE': line.includes('NATO +1') ? 'BLUFOR' : 'OPFOR' + fraction: line.includes('no Domination') ? 'NONE' : line.includes('NATO +1') ? 'BLUFOR' : 'OPFOR' } }; diff --git a/api/war-parser/clean.sh b/api/war-parser/clean.sh deleted file mode 100755 index 5d34276..0000000 --- a/api/war-parser/clean.sh +++ /dev/null @@ -1,20 +0,0 @@ -#!/usr/bin/env bash - -FILE="$1/war.log" - -while IFS='' read -r line || [[ -n "$line" ]]; do - case "$line" in - *"Budget"* | *"Mission"* | *"Abschuss"* | *"Respawn"* | *"Punkte"* | *"Flagge"* | *"Revive"* | *"Transport"*) - echo $line; - echo "" - ;; - esac -done < ${FILE} - -# Add OPT Scoreboard -while IFS= read -r line; do - if [[ $line =~ [^[:space:]] ]]; then - echo "$line" - echo "" - fi -done < <(grep -A 100 Scoreboard ${FILE} ) diff --git a/api/war-parser/run.sh b/api/war-parser/run.sh deleted file mode 100755 index 999fac4..0000000 --- a/api/war-parser/run.sh +++ /dev/null @@ -1,97 +0,0 @@ -#!/usr/bin/env bash - -createScoreboard() { - NAME="$1" - FILE="$2" - WAR_ID="$3" - - KILL=0 - FF=0 - REVIVE=0 - DEATH=0 - RESPAWN=0 - FLAG=0 - FRACTION= - - #escape '[' -> somehow escapes all special chars, hah? - ESC_NAME=$(echo "$NAME" | sed -r 's/[\[]+/\\[/g') - - while IFS= read -r line; do - case "$line" in - *\(WEST\)[" "]von:[" "]${ESC_NAME}[" "]\(EAST\)* | *\(EAST\)[" "]von:[" "]${ESC_NAME}[" "]\(WEST\)*) - ((KILL++)) - ;; - *\(EAST\)[" "]von:[" "]${ESC_NAME}[" "]\(EAST\)* | *\(WEST\)[" "]von:[" "]${ESC_NAME}[" "]\(WEST\)*) - ((FF++)) - ;; - *\(EAST\)[" "]wurde[" "]von[" "]${ESC_NAME}[" "]\(EAST\)[" "]wiederbelebt* | *\(WEST\)[" "]wurde[" "]von[" "]${ESC_NAME}[" "]\(WEST\)[" "]wiederbelebt*) - ((REVIVE++)) - ;; - *${ESC_NAME}[" "]*von:*) - ((DEATH++)) - ;; - *Flagge[" "]erobert[" "]von[" "]${ESC_NAME}* | *Flagge[" "]gesichert[" "]von[" "]${ESC_NAME}*) - ((FLAG++)) - ;; - *Spieler:*${ESC_NAME}*) - ((RESPAWN++)) - ;; - esac - - if [[ -z ${FRACTION} && ( "$line" == *${ESC_NAME}[" "]\(WEST\)* || "$line" == *${ESC_NAME}[" "]\(NATO\)* ) ]]; then - FRACTION="BLUFOR" - fi - if [[ -z ${FRACTION} && ( "$line" == *${ESC_NAME}[" "]\(EAST\)* || "$line" == *${ESC_NAME}[" "]\(CSAT\)* ) ]]; then - FRACTION="OPFOR" - fi - done < <(grep -- "${ESC_NAME}" ${FILE}) - - printf "\t{\"name\":\"$NAME\", \"fraction\":\"$FRACTION\", \"kill\":${KILL}, \"friendlyFire\":${FF}, \"revive\":${REVIVE}, \"death\":${DEATH}, \"respawn\":${RESPAWN}, \"flagTouch\":${FLAG}, \"warId\":\"${WAR_ID}\"} " - if [[ -z ${4} ]]; then - printf ",\n" - else - printf "\n" - fi -} - - -FILE="$1/war.log" -WAR_ID="$2" -PLAYERS=() - -while IFS='' read -r line || [[ -n "$line" ]]; do - if [[ -n $line ]]; then - case "$line" in - *"TFAR_RadioRequestEvent"*) - RES=$(echo "$(grep -oP ':[0-9]+\s\(\K.*?(?=\)\sREMOTE)' <<< "$line")") - ;; - *"Respawn"*) - RES=$(echo "$(grep -oP '\|\|\sSpieler:\s\K.*?(?=\s-\sKosten:)' <<< "$line")") - ;; - *"Abschuss"*) - RES=$(echo "$(grep -oP '\|\|\s\K.*?(?=\s\((EAST|WEST|CIV))' <<< "$line")") - RES1=$(echo "$(grep -oP 'von:\s\K.*?(?=\s\((EAST|WEST|CIV))' <<< "$line")") - ;; - esac - - if [[ $RES != *"Error: No unit"* && $RES1 != *"Error: No unit"* ]]; then - if [[ -n $RES && " ${PLAYERS[*]} " != *" $RES "* ]]; then - PLAYERS+=("$RES") - fi - if [[ -n $RES1 && " ${PLAYERS[*]} " != *" $RES1 "* ]]; then - PLAYERS+=("$RES1") - fi - fi - fi -done < ${FILE} - -echo "[" - -for ((i=0; i<${#PLAYERS[*]}; i++)); -do - if [[ "$((i+1))" -eq "${#PLAYERS[*]}" ]]; then - last="true" - fi - createScoreboard "${PLAYERS[i]}" ${FILE} ${WAR_ID} ${last} -done -echo "]" diff --git a/static/src/app/statistic/war-detail/war-detail.component.html b/static/src/app/statistic/war-detail/war-detail.component.html index 525e7eb..1632a8d 100644 --- a/static/src/app/statistic/war-detail/war-detail.component.html +++ b/static/src/app/statistic/war-detail/war-detail.component.html @@ -1,7 +1,7 @@ -
-
-
-
+
+
+
+

{{war.title}} - vom {{war.date | date: 'dd.MM.yyyy'}}

@@ -52,10 +52,10 @@

+ -
+
+