Finish parse tool

pull/15/head
Florian Hartwich 2017-10-21 12:11:32 +02:00
parent 2d9167b172
commit 35f6ac04b3
3 changed files with 150 additions and 97 deletions

View File

@ -10,19 +10,16 @@ const WarSchema = new Schema({
}, },
date: { date: {
type: Date, type: Date,
required: true
}, },
ptBlufor: { ptBlufor: {
type: Number, type: Number,
get: v => Math.round(v), get: v => Math.round(v),
set: v => Math.round(v), set: v => Math.round(v),
required: true
}, },
ptOpfor: { ptOpfor: {
type: Number, type: Number,
get: v => Math.round(v), get: v => Math.round(v),
set: v => Math.round(v), set: v => Math.round(v),
required: true
}, },
playersBlufor: { playersBlufor: {
type: Number, type: Number,
@ -38,8 +35,33 @@ const WarSchema = new Schema({
}, },
campaign: { campaign: {
type: mongoose.Schema.Types.ObjectId, type: mongoose.Schema.Types.ObjectId,
ref: 'Campaign' ref: 'Campaign',
} required: true
},
budgetBlufor: {
type: Number,
get: v => Math.round(v),
set: v => Math.round(v),
default: 0
},
budgetOpfor: {
type: Number,
get: v => Math.round(v),
set: v => Math.round(v),
default: 0
},
endBudgetBlufor: {
type: Number,
get: v => Math.round(v),
set: v => Math.round(v),
default: 0
},
endBudgetOpfor: {
type: Number,
get: v => Math.round(v),
set: v => Math.round(v),
default: 0
},
}, { }, {
collection: 'war', collection: 'war',
timestamps: {createdAt: 'timestamp'} timestamps: {createdAt: 'timestamp'}

View File

@ -64,20 +64,24 @@ wars.route('/')
.post(apiAuthenticationMiddleware, checkMT, upload.single('log'), (req, res, next) => { .post(apiAuthenticationMiddleware, checkMT, upload.single('log'), (req, res, next) => {
let body = req.body; let body = req.body;
const war = new WarModel(body); const bodyWar = new WarModel(body);
if (req.file) { if (req.file) {
// bodyWar.save((err, war) => {
// if (err) {
// return next(err);
// }
fs.readFile(req.file.buffer, (file, err) => { fs.readFile(req.file.buffer, (file, err) => {
if (err) { if (err) {
return next(err); return next(err);
} }
const lineArray = file.toString().split("\n"); const lineArray = file.toString().split("\n");
parseWarLog(lineArray, {}); res.locals.items = parseWarLog(lineArray, bodyWar);
res.locals.processed = true;
next();
}); });
res.locals.processed = true; // })
return 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;

View File

@ -1,38 +1,45 @@
"use strict"; 'use strict';
const arrayContains = require('./util').arrayContains; const arrayContains = require('./util').arrayContains;
const parseWarLog = (lineArray, war) => { const parseWarLog = (lineArray, war) => {
const clean = []; const stats = {
const budget = []; war: war,
const points = []; clean: [],
const kills = []; budget: [],
const respawn = []; points: [],
const revive = []; kills: [],
const flag = []; respawn: [],
const transport = []; revive: [],
const players = []; flag: [],
transport: [],
players: []
};
const addPlayersIfNotExists = (inputPlayers) => { const addPlayersIfNotExists = (inputPlayers) => {
inputPlayers.forEach(player => { inputPlayers.forEach(player => {
if (player && player.name !== 'Error: No unit' && !arrayContains(players, player)) { if (player && !arrayContains(stats.players, player)) {
player['warId'] = 'asd78zr3h8szfd7'; player['warId'] = war._id;
players.push(player); stats.players.push(player);
} }
}) })
}; };
lineArray.forEach(line => { lineArray.forEach(line => {
if (line.includes("Abschuss") && !line.includes('Fahrzeug')) { /**
clean.push(line); * KILLS
const shooterString = line.substring(line.lastIndexOf(" von: ") + 6, line.lastIndexOf(". :OPT LOG END")); */
if (line.includes('Abschuss') && !line.includes('Fahrzeug')) {
stats.clean.push(line);
const shooterString = line.substring(line.lastIndexOf(' von: ') + 6, line.lastIndexOf('. :OPT LOG END'));
const shooter = getPlayerAndFractionFromString(shooterString); const shooter = getPlayerAndFractionFromString(shooterString);
const targetString = line.substring(line.lastIndexOf(" || ") + 4, line.lastIndexOf(' von:')); const targetString = line.substring(line.lastIndexOf(' || ') + 4, line.lastIndexOf(' von:'));
const target = getPlayerAndFractionFromString(targetString); const target = getPlayerAndFractionFromString(targetString);
kills.push({ stats.kills.push({
warId: 'asdfaew34tg', war: war._id,
time: getDateTime(line.split(" ")[5]), time: getDateTime(line.split(' ')[5]),
shooter: shooter ? shooter.name : null, shooter: shooter ? shooter.name : null,
target: target ? target.name : null, target: target ? target.name : null,
friendlyFire: target && shooter ? target.fraction === shooter.fraction : false friendlyFire: target && shooter ? target.fraction === shooter.fraction : false
@ -41,65 +48,80 @@ const parseWarLog = (lineArray, war) => {
addPlayersIfNotExists([shooter, target]); addPlayersIfNotExists([shooter, target]);
} }
if (line.includes("Budget")) { /**
clean.push(line); * BUDGET
const budg = line.split(" "); */
if (line.includes("Endbudget")) { if (line.includes('Budget')) {
war["endBudgetBlufor"] = transformMoneyString(budg[11]); stats.clean.push(line);
war['endBudgetOpfor'] = transformMoneyString(budg[14]); const budg = line.split(' ');
} else if (line.includes("Startbudget")) { if (line.includes('Endbudget')) {
war["budgetBlufor"] = transformMoneyString(budg[11]); stats.war['endBudgetBlufor'] = transformMoneyString(budg[11]);
war["budgetOpfor"] = transformMoneyString(budg[14]); stats.war['endBudgetOpfor'] = transformMoneyString(budg[14]);
} else if (line.includes('Startbudget')) {
stats.war['budgetBlufor'] = transformMoneyString(budg[11]);
stats.war['budgetOpfor'] = transformMoneyString(budg[14]);
} else { } else {
budget.push(getBudgetEntry(budg)); stats.budget.push(getBudgetEntry(budg, war._id));
} }
} }
if (line.includes("Fahne")) { /**
clean.push(line); * FLAG
const playerName = line.substring(line.lastIndexOf("t von ") + 6, line.lastIndexOf(" :OPT LOG END")); */
if (line.includes('Fahne')) {
stats.clean.push(line);
const playerName = line.substring(line.lastIndexOf('t von ') + 6, line.lastIndexOf(' :OPT LOG END'));
const flagFraction = line.includes('NATO Flagge') ? 'BLUFOR' : 'OPFOR'; const flagFraction = line.includes('NATO Flagge') ? 'BLUFOR' : 'OPFOR';
const capture = !!line.includes('Flagge erobert'); const capture = !!line.includes('Flagge erobert');
flag.push({ stats.flag.push({
warId: 'sdf89uiz786', war: war._id,
time: getDateTime(line.split(" ")[5]), time: getDateTime(line.split(' ')[5]),
player: playerName, player: playerName,
flagFraction: flagFraction, flagFraction: flagFraction,
capture: capture capture: capture
}); });
} }
if (line.includes("Punkte")) { /**
clean.push(line); * POINTS
const pt = line.split(" "); */
if (line.includes('Punkte')) {
stats.clean.push(line);
const pt = line.split(' ');
if (line.includes("Endpunktestand")) { if (line.includes('Endpunktestand')) {
war['ptBlufor'] = parseInt(pt[11]); stats.war['ptBlufor'] = parseInt(pt[11]);
war['ptOpfor'] = parseInt(pt[14].slice(0, -1)); stats.war['ptOpfor'] = parseInt(pt[14].slice(0, -1));
} else { } else {
points.push(getPointsEntry(pt)) stats.points.push(getPointsEntry(pt, war._id))
} }
} }
if (line.includes("Respawn")) { /**
clean.push(line); * RESPAWN
const resp = line.split(" "); */
const playerName = line.substring(line.lastIndexOf("Spieler:") + 9, line.lastIndexOf("-") - 1); if (line.includes('Respawn')) {
respawn.push(getRespawnEntry(resp, playerName)); stats.clean.push(line);
const resp = line.split(' ');
const playerName = line.substring(line.lastIndexOf('Spieler:') + 9, line.lastIndexOf('-') - 1);
stats.respawn.push(getRespawnEntry(resp, playerName, war._id));
} }
if (line.includes("Revive")) { /**
clean.push(line); * REVIVE
*/
if (line.includes('Revive')) {
stats.clean.push(line);
const stabilized = !!line.includes('stabilisiert'); const stabilized = !!line.includes('stabilisiert');
const medicName = line.substring(line.lastIndexOf("wurde von ") + 10, line.lastIndexOf(stabilized ? ' stabilisiert' : ' wiederbelebt')); const medicName = line.substring(line.lastIndexOf('wurde von ') + 10, line.lastIndexOf(stabilized ? ' stabilisiert' : ' wiederbelebt'));
const medic = getPlayerAndFractionFromString(medicName); const medic = getPlayerAndFractionFromString(medicName);
const patientName = line.substring(line.lastIndexOf("|| ") + 3, line.lastIndexOf(" wurde von")); const patientName = line.substring(line.lastIndexOf('|| ') + 3, line.lastIndexOf(' wurde von'));
const patient = getPlayerAndFractionFromString(patientName); const patient = getPlayerAndFractionFromString(patientName);
revive.push({ stats.revive.push({
warId: 'asfddf', war: war._id,
time: getDateTime(line.split(" ")[5]), time: getDateTime(line.split(' ')[5]),
stabilized: stabilized, stabilized: stabilized,
medic: medic.name, medic: medic.name,
patient: patientName patient: patientName
@ -108,17 +130,20 @@ const parseWarLog = (lineArray, war) => {
addPlayersIfNotExists([medic, patient]); addPlayersIfNotExists([medic, patient]);
} }
if (line.includes("Transport ||")) { /**
clean.push(line); * TRANSPORT
const driverString = line.substring(line.lastIndexOf("wurde von ") + 10, line.lastIndexOf(" eingeflogen")); */
if (line.includes('Transport ||')) {
stats.clean.push(line);
const driverString = line.substring(line.lastIndexOf('wurde von ') + 10, line.lastIndexOf(' eingeflogen'));
const driver = getPlayerAndFractionFromString(driverString); const driver = getPlayerAndFractionFromString(driverString);
const passengerString = line.substring(line.lastIndexOf("|| ") + 3, line.lastIndexOf(" wurde von")); const passengerString = line.substring(line.lastIndexOf('|| ') + 3, line.lastIndexOf(' wurde von'));
const passenger = getPlayerAndFractionFromString(passengerString); const passenger = getPlayerAndFractionFromString(passengerString);
const distance = parseInt(line.substring(line.lastIndexOf("eingeflogen (") + 13, line.lastIndexOf("m)") - 1)); const distance = parseInt(line.substring(line.lastIndexOf('eingeflogen (') + 13, line.lastIndexOf('m)') - 1));
transport.push({ stats.transport.push({
war: "blablub7z8", war: war._id,
time: getDateTime(line.split(" ")[5]), time: getDateTime(line.split(' ')[5]),
driver: driver.name, driver: driver.name,
passenger: passenger.name, passenger: passenger.name,
distance: distance distance: distance
@ -128,60 +153,62 @@ const parseWarLog = (lineArray, war) => {
} }
}); });
players.forEach(player => console.log(player)) stats.war.playersBlufor = stats.players.filter(player => player.fraction === 'BLUFOR').length;
console.log(players.length) stats.war.playersOpfor = stats.players.filter(player => player.fraction === 'OPFOR').length;
return stats;
}; };
function getRespawnEntry(respawn, playerName) { const getRespawnEntry = (respawn, playerName, warId) => {
return { return {
war: "1234567", war: warId,
time: getDateTime(respawn[5]), time: getDateTime(respawn[5]),
player: playerName player: playerName
} }
} };
function getPointsEntry(pt) { const getPointsEntry = (pt, warId) => {
return { return {
warId: "123-xyz-123", warId: warId,
time: getDateTime(pt[5]), time: getDateTime(pt[5]),
ptBlufor: parseInt(pt[12]), ptBlufor: parseInt(pt[12]),
ptOpfor: parseInt(pt[15].slice(0, -1)) ptOpfor: parseInt(pt[15].slice(0, -1))
} }
} };
function getBudgetEntry(budg) { const getBudgetEntry = (budg, warId) => {
return { return {
warId: "123-xyz-123", warId: warId,
time: getDateTime(budg[5]), time: getDateTime(budg[5]),
fraction: budg[9] === "NATO" ? "BLUFOR" : "OPFOR", fraction: budg[9] === 'NATO' ? 'BLUFOR' : 'OPFOR',
oldBudget: transformMoneyString(budg[11]), oldBudget: transformMoneyString(budg[11]),
newBudget: transformMoneyString(budg[14]) newBudget: transformMoneyString(budg[14])
} }
} };
function getPlayerAndFractionFromString(nameAndFractionString) { const getPlayerAndFractionFromString = (nameAndFractionString) => {
const nameArray = nameAndFractionString.split(" "); const nameArray = nameAndFractionString.split(' ');
const fraction = nameArray[nameArray.length - 1] === "(WEST)" ? "BLUFOR" : "OPFOR"; const fraction = nameArray[nameArray.length - 1] === '(WEST)' ? 'BLUFOR' : 'OPFOR';
const name = nameAndFractionString.substring(0, nameAndFractionString.indexOf(nameArray[nameArray.length - 1]) - 1); const name = nameAndFractionString.substring(0, nameAndFractionString.indexOf(nameArray[nameArray.length - 1]) - 1);
// do not return player for "Selbstverschulden" // do not return player for 'Selbstverschulden'
if (name) { if (name || name === 'Error: No unit') {
return {name: name, fraction: fraction}; return {name: name, fraction: fraction};
} }
} };
const transformMoneyString = (budgetString) => { const transformMoneyString = (budgetString) => {
if (!budgetString.includes("e+")) { if (!budgetString.includes('e+')) {
return parseInt(budgetString); return parseInt(budgetString);
} }
const budget = budgetString.split("e+"); const budget = budgetString.split('e+');
return Math.round(parseFloat(budget[0]) * Math.pow(10, parseInt(budget[1]))); return Math.round(parseFloat(budget[0]) * Math.pow(10, parseInt(budget[1])));
}; };
function getDateTime(timeString) { const getDateTime = (timeString) => {
const timeZone = 'Z'; const timeZone = 'Z';
return new Date("1999-01-01T0" + timeString + timeZone); return new Date('1999-01-01T0' + timeString + timeZone);
} };
module.exports = parseWarLog; module.exports = parseWarLog;