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

522 lines
18 KiB
JavaScript

'use strict';
const playerArrayContains = require('./util').playerArrayContains;
const WHITESPACE = ' ';
const VehicleClasses = Object.freeze({
LIGHT: 'Leicht',
HEAVY: 'Schwer',
AIR: 'Flug',
UNKNOWN: 'Unbekannt',
});
const playerNameRegex = /(^|,\s)(.*?)\s\(/g;
const vehicleNameEndRegex = /\s\(\w+:/;
const sideRegex = /(side:\s(.*?)\))/;
const magazineRegex = /(magazine:\s(.*?)\))/;
const vehicleRegex = /(vehicle:\s(.*?)\))/;
const categoryRegex = /(category:\s(.*?)\))/;
const bluforPlayerCountRegex = /NATO\s(\d*)/;
const opforPlayerCountRegex = /CSAT\s(\d*)/;
const timestampRegex = /LOG:\s(\d*:\d*:\d*)\s---/;
const singleMinFpsRegex = /Single min\. FPS for (.*):\s(\d*.\d*)"/; // group1 = entity name, group2 = value
const singleAvgFpsRegex = /Single avg\. FPS for (.*):\s(\d*.\d*)"/; // group1 = entity name, group2 = value
const minFpsRegex = /Min\. FPS for (.*):\s\[(.*)\]"/; // group1 = entity name, group2 = comma separated values
const avgFpsRegex = /Avg\. FPS for (.*):\s\[(.*)\]"/; // group1 = entity name, group2 = comma separated values
const parseWarLog = (lineArray, war) => {
let flagBlufor = true;
let flagOpfor = true;
const NAME_TOO_LONG_ERROR = 'Error: ENAMETOOLONG: name too long, open \'';
const stats = {
war: war,
clean: [],
budget: [],
points: [],
kills: [],
vehicles: [],
respawn: [],
revive: [],
flag: [],
transport: [],
players: [],
playerCount: [],
serverFps: [],
};
const VEHICLE_BLACKLIST = [
'Prowler (Unbewaffnet)', 'Prowler (Bewaffnet)', 'Hunter',
'HEMTT Transporter', 'HEMTT Transporter (abgedeckt)', 'HEMTT Sanitätsfahrzeug',
'Remote Designator [NATO]', 'UGV Stomper',
'Qilin (Unbewaffnet)', 'Qilin (Bewaffnet)', 'Ifrit',
'Tempest-Transporter', 'Tempest-Transporter (abgedeckt)', 'Tempest Sanitätsfahrzeug',
'Remote Designator [CSAT]', 'UBF Saif',
'Quad Bike', 'HuntIR',
];
const addPlayerIfNotExists = (inputPlayer, steamUUID) => {
const player = getPlayerInfoFromString(inputPlayer);
if (player && player.name && player.fraction && !playerArrayContains(stats.players, player)) {
player['warId'] = war._id;
player['steamUUID'] = steamUUID;
stats.players.push(player);
}
};
lineArray.some((line) => {
/**
* sanitize nameTooLongError coming up in first line
*/
if (line.includes(NAME_TOO_LONG_ERROR)) {
line = line.substring(line.indexOf(NAME_TOO_LONG_ERROR) + NAME_TOO_LONG_ERROR.length);
}
if (line.includes('(Abschuss)')) {
/**
* KILLS & VEHICLE KILLS
*/
stats.clean.push(line);
const shooterString = line.substring(line.lastIndexOf(' von: ') + 6, line.search(/$/) - 1);
const shooter = getPlayerInfoFromString(shooterString);
if (line.includes('Fahrzeug:')) {
const targetString = line.substring(line.lastIndexOf(' --- Fahrzeug: ') + 15, line.lastIndexOf(' von:'));
const target = getVehicleAndFractionFromString(targetString);
if (target && shooter && target.fraction !== shooter.fraction) {
const vehicleKill = {
war: war._id,
time: getFullTimeDate(war.date, line.split(WHITESPACE)[5]),
target: target ? target.name : null,
fraction: shooter ? shooter.fraction : 'NONE',
vehicleClass: target.vehicleClass,
};
if (shooter && shooter.name instanceof Array) {
vehicleKill.shooter = shooter.name[0];
vehicleKill.additionalShooter = shooter.name.slice(1, shooter.name.length);
} else {
vehicleKill.shooter = shooter ? shooter.name : null;
}
if (shooter.magazine) {
vehicleKill.magazine = shooter.magazine;
}
if (shooter.vehicle) {
vehicleKill.shooterVehicle = shooter.vehicle;
}
stats.vehicles.push(vehicleKill);
}
} else {
const targetString = line.substring(line.lastIndexOf(' --- Einheit: ') + 14, line.lastIndexOf(' von:'));
const target = getPlayerInfoFromString(targetString);
const kill = {
war: war._id,
time: getFullTimeDate(war.date, line.split(WHITESPACE)[5]),
};
if (shooter) {
kill.shooter = shooter.name;
kill.fraction = shooter.fraction;
if (target) {
kill.friendlyFire = (target.fraction === shooter.fraction);
}
if (shooter.magazine) {
kill.magazine = shooter.magazine;
}
if (shooter.vehicle) {
kill.shooterVehicle = shooter.vehicle;
}
} else {
kill.fraction = 'NONE';
}
if (target) {
kill.target = target.name;
if (target.vehicle) {
kill.targetVehicle = target.vehicle;
}
}
stats.kills.push(kill);
}
} else if (line.includes('(Budget)')) {
/**
* BUDGET
*/
stats.clean.push(line);
const budg = line.split(WHITESPACE);
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
const dateString = budg[0].slice(0, -1).split('/').map((s) => parseInt(s));
stats.war.date = new Date(dateString[0], dateString[1] - 1, dateString[2]);
} else if (line.includes('Endbudget')) {
stats.war['endBudgetBlufor'] = transformMoneyString(budg[9]);
stats.war['endBudgetOpfor'] = transformMoneyString(budg[12].slice(0, -2));
stats.war.endDate = getFullTimeDate(war.date, budg[5]);
} else {
if (line.includes('Respawn von ')) {
/**
* RESPAWN
*/
const playerName = line.substring(line.lastIndexOf('Respawn von ') + 12, line.lastIndexOf('"'));
stats.respawn.push(getRespawnEntry(budg, playerName, war._id, war.date));
}
stats.budget.push(getBudgetEntry(budg, war._id, war.date));
}
} else if (line.includes('(Fahne)') && !line.includes('Dominator')) {
/**
* FLAG
*/
const flagFraction = line.includes('NATO Flagge') ? 'BLUFOR' : 'OPFOR';
const capture = !!line.includes('Flagge erobert');
if ((flagFraction === 'BLUFOR' && ((capture && flagBlufor) || (!capture && !flagBlufor))) ||
(flagFraction === 'OPFOR' && ((capture && flagOpfor) || (!capture && !flagOpfor)))) {
stats.clean.push(line);
const playerName = line.substring(line.lastIndexOf('rt von ') + 7).slice(0, -1);
if (flagFraction === 'BLUFOR') {
flagBlufor = !capture;
} else if (flagFraction === 'OPFOR') {
flagOpfor = !capture;
}
stats.flag.push({
war: war._id,
time: getFullTimeDate(war.date, line.split(WHITESPACE)[5]),
player: playerName,
flagFraction: flagFraction,
capture: capture,
});
}
} else if (line.includes('(Punkte)')) {
/**
* POINTS
*/
stats.clean.push(line);
const pt = line.split(WHITESPACE);
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));
}
} else if (line.includes('(Revive)')) {
/**
* 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 = getPlayerInfoFromString(medicName);
const patientName = line.substring(line.lastIndexOf('--- ') + 4, line.lastIndexOf(' wurde von'));
const patient = getPlayerInfoFromString(patientName);
stats.revive.push({
war: war._id,
time: getFullTimeDate(war.date, line.split(WHITESPACE)[5]),
stabilized: stabilized,
medic: medic.name,
patient: patient.name,
fraction: medic.fraction,
});
} else if (line.includes('(Transport)')) {
/**
* TRANSPORT
*/
stats.clean.push(line);
const driverString = line.substring(line.lastIndexOf('wurde von ') + 10, line.lastIndexOf(' eingeflogen'));
const driver = getPlayerInfoFromString(driverString);
const passengerString = line.substring(line.lastIndexOf('--- ') + 3, line.lastIndexOf(' wurde von'));
const passenger = getPlayerInfoFromString(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(WHITESPACE)[5]),
driver: driver ? driver.name : null,
passenger: passenger ? passenger.name : null,
fraction: driver ? driver.fraction : 'NONE',
distance: distance,
});
} else if (line.includes('(Spieleranzahl)')) {
stats.clean.push(line);
const timestamp = (timestampRegex.exec(line))[1];
const countBlufor = (bluforPlayerCountRegex.exec(line))[1];
const countOpfor = (opforPlayerCountRegex.exec(line))[1];
stats.playerCount.push({
war: war.id,
time: getFullTimeDate(war.date, timestamp),
countBlufor: countBlufor,
countOpfor: countOpfor,
});
} else if (line.includes('(FPS)')) {
stats.clean.push(line);
const updateServerFpsLogEntry = (entityName, addKey, addValue) => {
const idx = stats.serverFps.findIndex((entry) => entry.entityName === entityName);
if (idx === -1) {
stats.serverFps.push({
war: war.id,
entityName: entityName,
[addKey]: addValue,
});
return;
}
const entity = stats.serverFps[idx];
entity[addKey] = addValue;
stats.serverFps[idx] = entity;
};
const singleMinMatch = singleMinFpsRegex.exec(line);
if (singleMinMatch) {
updateServerFpsLogEntry(singleMinMatch[1], 'singleMinFps', singleMinMatch[2]);
} else {
const singleAvgMatch = singleAvgFpsRegex.exec(line);
if (singleAvgMatch) {
updateServerFpsLogEntry(singleAvgMatch[1], 'singleAvgFps', singleAvgMatch[2]);
} else {
const minCollectionMatch = minFpsRegex.exec(line);
if (minCollectionMatch) {
updateServerFpsLogEntry(minCollectionMatch[1], 'minFps', minCollectionMatch[2].split(','));
} else {
const avgCollectionMatch = avgFpsRegex.exec(line);
if (avgCollectionMatch) {
updateServerFpsLogEntry(avgCollectionMatch[1], 'avgFps', avgCollectionMatch[2].split(','));
}
}
}
}
} else if (line.includes('(Fraktionsuebersicht)') || line.includes('Fraktionsübersicht')) {
/**
* PLAYERS
*/
const playerString = line.substring(line.lastIndexOf('--- ') + 4, line.lastIndexOf(', PUID'));
const playerUUID = line.substring(line.lastIndexOf('PUID ') + 5, line.lastIndexOf('"'));
addPlayerIfNotExists(playerString, playerUUID);
}
});
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;
// TODO: use vehicle class description from enum
stats.players[i]['vehicleLight'] = stats.vehicles.filter(
(vehicle) => (vehicle.shooter === playerName ||
(vehicle.additionalShooter && vehicle.additionalShooter.indexOf(playerName)) > -1) && vehicle.vehicleClass ===
'LIGHT' && VEHICLE_BLACKLIST.indexOf(vehicle.target) < 0).length;
stats.players[i]['vehicleHeavy'] = stats.vehicles.filter(
(vehicle) => (vehicle.shooter === playerName ||
(vehicle.additionalShooter && vehicle.additionalShooter.indexOf(playerName)) > -1) && vehicle.vehicleClass ===
'HEAVY' && VEHICLE_BLACKLIST.indexOf(vehicle.target) < 0).length;
stats.players[i]['vehicleAir'] = stats.vehicles.filter(
(vehicle) => (vehicle.shooter === playerName ||
(vehicle.additionalShooter && vehicle.additionalShooter.indexOf(playerName)) > -1) && vehicle.vehicleClass ===
'AIR' && VEHICLE_BLACKLIST.indexOf(vehicle.target) < 0).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, -2)),
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 getPlayerInfoFromString = (inputString) => {
const resPlayer = {};
const playerNameRegexMatch = playerNameRegex.exec(inputString);
const sideMatch = sideRegex.exec(inputString);
// SINGLE PLAYER NAME
let name;
if (playerNameRegexMatch && playerNameRegexMatch.length >= 2) {
name = playerNameRegexMatch[2].trim();
// do not return player for 'unbekannt' or 'Error: No unit'
if (!name || name === 'unbekannt' || name === 'Error: No unit' || name === 'Selbstverschulden.') {
return;
}
resPlayer.name = name;
} else {
return;
}
// ADDITIONAL PLAYER NAMES
let additionalPlayerMatch;
while ((additionalPlayerMatch = playerNameRegex.exec(inputString)) !== null) {
const addPlayer = additionalPlayerMatch[0].replace(/^,\s/, '').replace(/\s\($/, '').trim();
if (name instanceof Array) {
name.push(addPlayer);
} else {
name = [name, addPlayer];
}
}
// PLAYER FRACTION
let side;
if (sideMatch && sideMatch.length >= 3) {
side = sideMatch[2];
} else {
const inputArray = inputString.split(WHITESPACE);
if (inputArray) {
side = inputArray[inputArray.length - 1].replace('(', '').replace(')', '');
}
}
if (side && side !== 'ENEMY') {
resPlayer.fraction = (side === 'WEST') ? 'BLUFOR' : 'OPFOR';
}
// MAGAZINE
const magazineMatch = magazineRegex.exec(inputString);
if (magazineMatch && magazineMatch.length >= 3) {
let magazine = magazineMatch[2];
if (new RegExp('\\(.*$').test(magazine)) {
magazine = magazine.concat(')');
}
resPlayer.magazine = magazine;
}
// VEHICLE
const vehicleMatch = vehicleRegex.exec(inputString);
if (vehicleMatch && vehicleMatch.length >= 3 && vehicleMatch[2]) {
let vehicle = vehicleMatch[2];
if (new RegExp('\\(.*$').test(vehicle)) {
vehicle = vehicle.concat(')');
}
resPlayer.vehicle = vehicle;
}
return resPlayer;
};
const getVehicleAndFractionFromString = (nameClassFractionString) => {
const sideMatch = sideRegex.exec(nameClassFractionString);
const categoryMatch = categoryRegex.exec(nameClassFractionString);
// NAME
const vehicleName = nameClassFractionString.substring(0,
nameClassFractionString.indexOf(vehicleNameEndRegex.exec(nameClassFractionString)));
let fraction;
if (sideMatch && sideMatch.length >= 3) {
// SIDE
const side = sideMatch[2];
fraction = side === 'OPT_NATO' || side === 'OPT_NATO_T' || side === 'WEST' ? 'BLUFOR' : 'OPFOR';
}
let vehicleClassString;
if (categoryMatch && categoryMatch.length >= 3) {
// CATEGORY
vehicleClassString = categoryMatch[2];
}
// skip logging here - this is some basic equipment identifier (i.e. parachute)
if (fraction === 'OPF_F' || fraction === 'BLU_F' || vehicleClassString === VehicleClasses.UNKNOWN) {
return;
}
let vehicleClass;
for (const key in VehicleClasses) {
if (VehicleClasses.hasOwnProperty(key) && VehicleClasses[key] === vehicleClassString) {
vehicleClass = key;
}
}
if (vehicleName && fraction && vehicleClass) {
return {
name: vehicleName,
fraction: fraction,
vehicleClass: vehicleClass,
};
}
};
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]);
returnDate.setSeconds(time[2]);
return returnDate;
};
module.exports = parseWarLog;