Compare commits

..

3 Commits

Author SHA1 Message Date
Florian Hartwich 35f6ac04b3 Finish parse tool 2017-10-21 12:11:32 +02:00
Florian Hartwich 2d9167b172 create player list & kill log parse 2017-10-21 11:32:25 +02:00
Florian Hartwich db48cd8ef6 Add revive and flag parsing 2017-10-21 10:41:49 +02:00
4 changed files with 193 additions and 99 deletions

View File

@ -10,19 +10,16 @@ const WarSchema = new Schema({
},
date: {
type: Date,
required: true
},
ptBlufor: {
type: Number,
get: v => Math.round(v),
set: v => Math.round(v),
required: true
},
ptOpfor: {
type: Number,
get: v => Math.round(v),
set: v => Math.round(v),
required: true
},
playersBlufor: {
type: Number,
@ -38,8 +35,33 @@ const WarSchema = new Schema({
},
campaign: {
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',
timestamps: {createdAt: 'timestamp'}

View File

@ -64,20 +64,24 @@ wars.route('/')
.post(apiAuthenticationMiddleware, checkMT, upload.single('log'), (req, res, next) => {
let body = req.body;
const war = new WarModel(body);
const bodyWar = new WarModel(body);
if (req.file) {
// bodyWar.save((err, war) => {
// if (err) {
// return next(err);
// }
fs.readFile(req.file.buffer, (file, err) => {
if (err) {
return next(err);
}
const lineArray = file.toString().split("\n");
parseWarLog(lineArray, {});
});
res.locals.items = parseWarLog(lineArray, bodyWar);
res.locals.processed = true;
return next();
next();
});
// })
} else {
const err = new Error('no Logfile provided');
err.status = codes.wrongmediasend;

View File

@ -1,146 +1,214 @@
"use strict";
'use strict';
const arrayContains = require('../middleware/util').arrayContains;
const arrayContains = require('./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 stats = {
war: war,
clean: [],
budget: [],
points: [],
kills: [],
respawn: [],
revive: [],
flag: [],
transport: [],
players: []
};
const addPlayerIfNotExists = (playerName) => {
if (playerName !== 'Error: No unit' && !arrayContains(playerNames, playerName)) {
playerNames.push(playerName);
const addPlayersIfNotExists = (inputPlayers) => {
inputPlayers.forEach(player => {
if (player && !arrayContains(stats.players, player)) {
player['warId'] = war._id;
stats.players.push(player);
}
})
};
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]);
// }
/**
* KILLS
*/
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 targetString = line.substring(line.lastIndexOf(' || ') + 4, line.lastIndexOf(' von:'));
const target = getPlayerAndFractionFromString(targetString);
stats.kills.push({
war: war._id,
time: getDateTime(line.split(' ')[5]),
shooter: shooter ? shooter.name : null,
target: target ? target.name : null,
friendlyFire: target && shooter ? target.fraction === shooter.fraction : false
});
addPlayersIfNotExists([shooter, target]);
}
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]);
/**
* BUDGET
*/
if (line.includes('Budget')) {
stats.clean.push(line);
const budg = line.split(' ');
if (line.includes('Endbudget')) {
stats.war['endBudgetBlufor'] = transformMoneyString(budg[11]);
stats.war['endBudgetOpfor'] = transformMoneyString(budg[14]);
} else if (line.includes('Startbudget')) {
stats.war['budgetBlufor'] = transformMoneyString(budg[11]);
stats.war['budgetOpfor'] = transformMoneyString(budg[14]);
} else {
budget.push(getBudgetEntry(budg));
stats.budget.push(getBudgetEntry(budg, war._id));
}
}
if (line.includes("Fahne")) {
clean.push(line);
/**
* FLAG
*/
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 capture = !!line.includes('Flagge erobert');
stats.flag.push({
war: war._id,
time: getDateTime(line.split(' ')[5]),
player: playerName,
flagFraction: flagFraction,
capture: capture
});
}
if (line.includes("Punkte")) {
clean.push(line);
const pt = line.split(" ");
/**
* POINTS
*/
if (line.includes('Punkte')) {
stats.clean.push(line);
const pt = line.split(' ');
if (line.includes("Endpunktestand")) {
war['ptBlufor'] = parseInt(pt[11]);
war['ptOpfor'] = parseInt(pt[14].slice(0, -1));
if (line.includes('Endpunktestand')) {
stats.war['ptBlufor'] = parseInt(pt[11]);
stats.war['ptOpfor'] = parseInt(pt[14].slice(0, -1));
} else {
points.push(getPointsEntry(pt))
stats.points.push(getPointsEntry(pt, war._id))
}
}
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);
/**
* RESPAWN
*/
if (line.includes('Respawn')) {
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);
//console.log(line);
/**
* REVIVE
*/
if (line.includes('Revive')) {
stats.clean.push(line);
const stabilized = !!line.includes('stabilisiert');
const medicName = line.substring(line.lastIndexOf('wurde von ') + 10, line.lastIndexOf(stabilized ? ' stabilisiert' : ' wiederbelebt'));
const medic = getPlayerAndFractionFromString(medicName);
const patientName = line.substring(line.lastIndexOf('|| ') + 3, line.lastIndexOf(' wurde von'));
const patient = getPlayerAndFractionFromString(patientName);
stats.revive.push({
war: war._id,
time: getDateTime(line.split(' ')[5]),
stabilized: stabilized,
medic: medic.name,
patient: patientName
});
addPlayersIfNotExists([medic, patient]);
}
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);
/**
* TRANSPORT
*/
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 passengerString = line.substring(line.lastIndexOf('|| ') + 3, line.lastIndexOf(' wurde von'));
const passenger = getPlayerAndFractionFromString(passengerString);
const distance = parseInt(line.substring(line.lastIndexOf('eingeflogen (') + 13, line.lastIndexOf('m)') - 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,
stats.transport.push({
war: war._id,
time: getDateTime(line.split(' ')[5]),
driver: driver.name,
passenger: passenger.name,
distance: distance
});
addPlayerIfNotExists(sanitizedDriverName);
addPlayerIfNotExists(sanitizedPassengerName);
addPlayersIfNotExists([driver, passenger]);
}
});
//
playerNames.forEach(budg => console.log(budg));
stats.war.playersBlufor = stats.players.filter(player => player.fraction === 'BLUFOR').length;
stats.war.playersOpfor = stats.players.filter(player => player.fraction === 'OPFOR').length;
return stats;
};
function getRespawnEntry(respawn, playerName) {
const getRespawnEntry = (respawn, playerName, warId) => {
return {
war: "1234567",
war: warId,
time: getDateTime(respawn[5]),
player: playerName
}
}
};
function getPointsEntry(pt) {
const getPointsEntry = (pt, warId) => {
return {
warId: "123-xyz-123",
warId: warId,
time: getDateTime(pt[5]),
ptBlufor: parseInt(pt[12]),
ptOpfor: parseInt(pt[15].slice(0, -1))
}
}
};
function getBudgetEntry(budg) {
const getBudgetEntry = (budg, warId) => {
return {
warId: "123-xyz-123",
warId: warId,
time: getDateTime(budg[5]),
fraction: budg[9] === "NATO" ? "BLUFOR" : "OPFOR",
fraction: budg[9] === 'NATO' ? 'BLUFOR' : 'OPFOR',
oldBudget: transformMoneyString(budg[11]),
newBudget: transformMoneyString(budg[14])
}
};
const getPlayerAndFractionFromString = (nameAndFractionString) => {
const nameArray = nameAndFractionString.split(' ');
const fraction = nameArray[nameArray.length - 1] === '(WEST)' ? 'BLUFOR' : 'OPFOR';
const name = nameAndFractionString.substring(0, nameAndFractionString.indexOf(nameArray[nameArray.length - 1]) - 1);
// do not return player for 'Selbstverschulden'
if (name || name === 'Error: No unit') {
return {name: name, fraction: fraction};
}
};
const transformMoneyString = (budgetString) => {
if (!budgetString.includes("e+")) {
if (!budgetString.includes('e+')) {
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])));
};
function getDateTime(timeString) {
const getDateTime = (timeString) => {
const timeZone = 'Z';
return new Date("1999-01-01T0" + timeString + timeZone);
}
return new Date('1999-01-01T0' + timeString + timeZone);
};
module.exports = parseWarLog;

View File

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