opt-cc/api/tools/log-parse-tool.js

246 lines
8.7 KiB
JavaScript

'use strict';
const playerArrayContains = require('./util').playerArrayContains;
const parseWarLog = (lineArray, war) => {
const nameToLongError = 'Error: ENAMETOOLONG: name too long, open \'';
const stats = {
war: war,
clean: [],
budget: [],
points: [],
kills: [],
respawn: [],
revive: [],
flag: [],
transport: [],
players: []
};
const addPlayerIfNotExists = (inputPlayer) => {
const player = getPlayerAndFractionFromString(inputPlayer);
if (player && player.name && player.fraction && !playerArrayContains(stats.players, player)) {
player['warId'] = war._id;
stats.players.push(player);
}
};
lineArray.some(line => {
/**
* sanitize nameTooLongError coming up in first line
*/
if (line.includes(nameToLongError)) {
line = line.substring(line.indexOf(nameToLongError) + nameToLongError.length);
}
/**
* KILLS
*/
if (line.includes('(Abschuss)') && !line.includes('Fahrzeug')) {
stats.clean.push(line);
const shooterString = line.substring(line.lastIndexOf(' von: ') + 6, line.lastIndexOf('."'));
const shooter = getPlayerAndFractionFromString(shooterString);
const targetString = line.substring(line.lastIndexOf(' --- ') + 5, line.lastIndexOf(' von:'));
const target = getPlayerAndFractionFromString(targetString);
stats.kills.push({
war: war._id,
time: getFullTimeDate(war.date, line.split(' ')[5]),
shooter: shooter ? shooter.name : null,
target: target ? target.name : null,
friendlyFire: shooter ? target.fraction === shooter.fraction : false,
fraction: shooter ? shooter.fraction : 'NONE'
});
}
/**
* BUDGET
*/
else if (line.includes('(Budget)')) {
stats.clean.push(line);
const budg = line.split(' ');
if (line.includes('Startbudget')) {
stats.war['budgetBlufor'] = transformMoneyString(budg[9]);
stats.war['budgetOpfor'] = transformMoneyString(budg[12].slice(0,-1));
// this date needs to be assigned in first place !important
stats.war.date = new Date(budg[0].slice(0,-1).split('/').join('-'));
} else if (line.includes('Endbudget')) {
stats.war['endBudgetBlufor'] = transformMoneyString(budg[9].substr(1));
stats.war['endBudgetOpfor'] = transformMoneyString(budg[12].slice(0,-1));
stats.war.endDate = getFullTimeDate(war.date, budg[5]);
} else {
stats.budget.push(getBudgetEntry(budg, war._id, war.date));
}
}
/**
* FLAG
*/
else if (line.includes('(Fahne)') && !line.includes('Dominator')) {
stats.clean.push(line);
const playerName = line.substring(line.lastIndexOf('rt von ') + 7).slice(0, -2);
const flagFraction = line.includes('NATO Flagge') ? 'BLUFOR' : 'OPFOR';
const capture = !!line.includes('Flagge erobert');
stats.flag.push({
war: war._id,
time: getFullTimeDate(war.date, line.split(' ')[5]),
player: playerName,
flagFraction: flagFraction,
capture: capture
});
}
/**
* POINTS
*/
else if (line.includes('(Punkte)')) {
stats.clean.push(line);
const pt = line.split(' ');
if (line.includes('Endpunktestand')) {
stats.war['ptBlufor'] = parseInt(pt[9]);
stats.war['ptOpfor'] = parseInt(pt[12].slice(0, -1));
// EXIT LOOP
return true;
} else {
stats.points.push(getPointsEntry(pt, line, war._id, war.date))
}
}
//
// /**
// * RESPAWN
// */
// else 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, war.date));
// }
/**
* REVIVE
*/
else 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('--- ') + 4, line.lastIndexOf(' wurde von'));
const patient = getPlayerAndFractionFromString(patientName);
stats.revive.push({
war: war._id,
time: getFullTimeDate(war.date, line.split(' ')[5]),
stabilized: stabilized,
medic: medic.name,
patient: patient.name,
fraction: medic.fraction
});
}
// /**
// * TRANSPORT
// */
// else 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));
//
// stats.transport.push({
// war: war._id,
// time: getFullTimeDate(war.date, line.split(' ')[5]),
// driver: driver.name,
// passenger: passenger ? passenger.name : null,
// fraction: driver.fraction,
// distance: distance
// });
// }
/**
* PLAYERS
*/
else if (line.includes('(Fraktionsuebersicht)')) {
const playerString = line.substring(line.lastIndexOf('--- ') + 4, line.lastIndexOf(', PUID'));
addPlayerIfNotExists(playerString)
}
});
for (let i = 0; i < stats.players.length; i++) {
const playerName = stats.players[i].name;
stats.players[i]['respawn'] = stats.respawn.filter(res => res.player === playerName).length;
stats.players[i]['kill'] = stats.kills.filter(kill => kill.shooter === playerName && !kill.friendlyFire).length;
stats.players[i]['friendlyFire'] = stats.kills.filter(kill => kill.shooter === playerName && kill.friendlyFire).length;
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;
stats.war.playersOpfor = stats.players.filter(player => player.fraction === 'OPFOR').length;
return stats;
};
const getRespawnEntry = (respawn, playerName, warId, warDate) => {
return {
war: warId,
time: getFullTimeDate(warDate, respawn[5]),
player: playerName
}
};
const getPointsEntry = (pt, line, warId, warDate) => {
return {
war: warId,
time: getFullTimeDate(warDate, pt[5]),
ptBlufor: parseInt(pt[10]),
ptOpfor: parseInt(pt[13].slice(0, -3)),
fraction: line.includes('Kein Dominator') ? 'NONE' : line.includes('NATO +1') ? 'BLUFOR' : 'OPFOR'
}
};
const getBudgetEntry = (budg, warId, warDate) => {
return {
war: warId,
time: getFullTimeDate(warDate, budg[5]),
fraction: budg[7] === 'NATO' ? 'BLUFOR' : 'OPFOR',
oldBudget: transformMoneyString(budg[9]),
newBudget: transformMoneyString(budg[12])
}
};
const getPlayerAndFractionFromString = (nameAndFractionString) => {
const nameArray = nameAndFractionString.split(' ');
const fraction = nameArray[nameArray.length - 1] !== '(ENEMY)' ? nameArray[nameArray.length - 1] === '(WEST)' ? 'BLUFOR' : 'OPFOR' : undefined;
const name = nameAndFractionString.substring(0, nameAndFractionString.indexOf(nameArray[nameArray.length - 1]) - 1);
// do not return player for 'Selbstverschulden' or 'Error: No unit'
if (name && name !== 'Error: No unit') {
return {name: name, fraction: fraction};
}
};
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])));
};
const getFullTimeDate = (date, timeString) => {
const returnDate = new Date(date);
const time = timeString.split(':');
returnDate.setHours(time[0]);
returnDate.setMinutes(time[1]);
return returnDate;
};
module.exports = parseWarLog;