Add parser for basic events

pull/15/head
Florian Hartwich 2017-10-20 23:42:41 +02:00
parent 371b62a0cc
commit f1449e5047
13 changed files with 171 additions and 59 deletions

View File

@ -4,7 +4,7 @@ const cron = require('cron');
const async = require('async');
const {exec} = require('child_process');
const UserModel = require('../models/user');
const signatureTool = require('../signature-tool/signature-tool');
const signatureTool = require('../tools/signature-tool');
const createAllSignatures = () => {
console.log('\x1b[35m%s\x1b[0m', new Date().toLocaleString()

View File

@ -11,4 +11,18 @@ const sortCollectionBy = (collection, key) => {
return collection;
};
const arrayContains = (arr, user) => {
let i = 0, count = arr.length, matchFound = false;
for(; i < count; i++) {
if (arr[i] === user) {
matchFound = true;
break;
}
}
return matchFound;
};
exports.sortCollection = sortCollectionBy;
exports.arrayContains = arrayContains;

View File

@ -9,7 +9,7 @@ const codes = require('./http-codes');
const routerHandling = require('../middleware/router-handling');
const createAllSignatures = require('../cron-job/cron').createAllSignatures;
const createSignature = require('../signature-tool/signature-tool');
const createSignature = require('../tools/signature-tool');
const command = express.Router();

View File

@ -17,6 +17,8 @@ const apiAuthenticationMiddleware = require('../middleware/auth-middleware');
const checkMT = require('../middleware/permission-check').checkMT;
const routerHandling = require('../middleware/router-handling');
const parseWarLog = require('../tools/log-parse-tool');
// Mongoose Model using mongoDB
const CampaignModel = require('../models/campaign');
const WarModel = require('../models/war');
@ -62,69 +64,19 @@ wars.route('/')
.post(apiAuthenticationMiddleware, checkMT, upload.single('log'), (req, res, next) => {
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) {
war.save((err, war) => {
if (err) {
return next(err);
}
const folderName = __dirname + '/../resource/logs/' + war._id;
mkdirp(folderName, function (err) {
fs.readFile(req.file.buffer, (file, err) => {
if (err) {
return next(err);
}
fs.appendFile(folderName + '/war.log', new Buffer(req.file.buffer), (err) => {
if (err) {
return next(err);
}
exec(__dirname + '/../war-parser/run.sh ' + folderName + ' ' + war._id, (error, stdout) => {
if (error) {
return next(error);
}
exec(__dirname + '/../war-parser/clean.sh /../' + folderName + ' | tee ' + folderName + '/clean.log', (error) => {
if (error) {
return next(error);
}
let obj = JSON.parse(`${stdout}`);
for (let i = 0; i < obj.length; i++) {
if (!obj[i].fraction) {
obj.splice(i, 1);
} else if (obj[i].fraction === 'BLUFOR') {
war.playersBlufor++;
} else {
war.playersOpfor++;
}
}
const lineArray = file.toString().split("\n");
parseWarLog(lineArray, {});
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();
});
}
})
});
});
});
});
})
res.locals.processed = true;
return next();
} else {
const err = new Error('no Logfile provided');

View File

Before

Width:  |  Height:  |  Size: 53 KiB

After

Width:  |  Height:  |  Size: 53 KiB

View File

Before

Width:  |  Height:  |  Size: 61 KiB

After

Width:  |  Height:  |  Size: 61 KiB

View File

Before

Width:  |  Height:  |  Size: 10 KiB

After

Width:  |  Height:  |  Size: 10 KiB

View File

Before

Width:  |  Height:  |  Size: 18 KiB

After

Width:  |  Height:  |  Size: 18 KiB

146
api/tools/log-parse-tool.js Normal file
View File

@ -0,0 +1,146 @@
"use strict";
const arrayContains = require('../middleware/util').arrayContains;
const parseWarLog = (lineArray, war) => {
const clean = [];
const budget = [];
const points = [];
const kills = [];
const respawn = [];
const revive = [];
const flag = [];
const transport = [];
const playerNames = [];
const addPlayerIfNotExists = (playerName) => {
if (playerName !== 'Error: No unit' && !arrayContains(playerNames, playerName)) {
playerNames.push(playerName);
}
};
lineArray.forEach(line => {
if (line.includes("Abschuss")) {
clean.push(line);
// const kill = line.split(" ");
// for (let i=0; i< kill.length; i++ ) {
// console.log(i + " +++ " + kill[i]);
// }
}
if (line.includes("Budget")) {
clean.push(line);
const budg = line.split(" ");
if (line.includes("Endbudget")) {
war["endBudgetBlufor"] = transformMoneyString(budg[11]);
war['endBudgetOpfor'] = transformMoneyString(budg[14]);
} else if (line.includes("Startbudget")) {
war["budgetBlufor"] = transformMoneyString(budg[11]);
war["budgetOpfor"] = transformMoneyString(budg[14]);
} else {
budget.push(getBudgetEntry(budg));
}
}
if (line.includes("Fahne")) {
clean.push(line);
}
if (line.includes("Punkte")) {
clean.push(line);
const pt = line.split(" ");
if (line.includes("Endpunktestand")) {
war['ptBlufor'] = parseInt(pt[11]);
war['ptOpfor'] = parseInt(pt[14].slice(0, -1));
} else {
points.push(getPointsEntry(pt))
}
}
if (line.includes("Respawn")) {
clean.push(line);
const resp = line.split(" ");
const playerName = line.substring(line.lastIndexOf("Spieler:") + 9, line.lastIndexOf("-") - 1);
respawn.push(getRespawnEntry(resp, playerName));
addPlayerIfNotExists(playerName);
}
if (line.includes("Revive")) {
clean.push(line);
//console.log(line);
}
if (line.includes("Transport ||")) {
clean.push(line);
const driverName = line.substring(line.lastIndexOf("wurde von ") + 10, line.lastIndexOf(" eingeflogen"));
const driverNameArray = driverName.split(" ");
const driverFraction = driverNameArray[driverNameArray.length-1] === "(WEST)" ? "BLUFOR" : "OPFOR";
const sanitizedDriverName = driverName.substring(0, driverName.indexOf(driverNameArray[driverNameArray.length-1])-1);
const passengerName = line.substring(line.lastIndexOf("|| ") + 3, line.lastIndexOf(" wurde von"));
const passengerNameArray = passengerName.split(" ");
const passengerFraction = passengerNameArray[passengerNameArray.length-1] === "(WEST)" ? "BLUFOR" : "OPFOR";
const sanitizedPassengerName = passengerName.substring(0, passengerName.indexOf(passengerNameArray[passengerNameArray.length-1])-1);
const distance = parseInt(line.substring(line.lastIndexOf("eingeflogen (") + 13, line.lastIndexOf("m)") - 1));
transport.push({
war: "blablub7z8",
driver: sanitizedDriverName,
passenger: sanitizedPassengerName,
distance: distance
});
addPlayerIfNotExists(sanitizedDriverName);
addPlayerIfNotExists(sanitizedPassengerName);
}
});
//
playerNames.forEach(budg => console.log(budg));
};
function getRespawnEntry(respawn, playerName) {
return {
war: "1234567",
time: getDateTime(respawn[5]),
player: playerName
}
}
function getPointsEntry(pt) {
return {
warId: "123-xyz-123",
time: getDateTime(pt[5]),
ptBlufor: parseInt(pt[12]),
ptOpfor: parseInt(pt[15].slice(0, -1))
}
}
function getBudgetEntry(budg) {
return {
warId: "123-xyz-123",
time: getDateTime(budg[5]),
fraction: budg[9] === "NATO" ? "BLUFOR" : "OPFOR",
oldBudget: transformMoneyString(budg[11]),
newBudget: transformMoneyString(budg[14])
}
}
const transformMoneyString = (budgetString) => {
if (!budgetString.includes("e+")) {
return parseInt(budgetString);
}
const budget = budgetString.split("e+");
return Math.round(parseFloat(budget[0]) * Math.pow(10, parseInt(budget[1])));
};
function getDateTime(timeString) {
const timeZone = 'Z';
return new Date("1999-01-01T0" + timeString + timeZone);
}
module.exports = parseWarLog;

View File

@ -1,4 +1,4 @@
import {Component, HostListener} from '@angular/core';
import {Component} from '@angular/core';
import {NavigationEnd, NavigationStart, Router} from '@angular/router';
import {LoginService} from './services/login-service/login-service';
import {PromotionService} from "./services/promotion-service/promotion.service";
@ -30,7 +30,7 @@ export class AppComponent {
}
if (event instanceof NavigationEnd) {
this.loading = false;
if (router.url.includes('stats') || router.url.includes('overview')) {
if (router.url.includes('overview')) {
window.scrollTo({left: 0, top: 0, behavior: 'smooth'});
}
}