From f3f563866798c831cd6795297def287477cba298 Mon Sep 17 00:00:00 2001 From: HardiReady Date: Mon, 4 Jun 2018 21:46:07 +0200 Subject: [PATCH] Fix basic player information parsing --- api/tools/log-parse-tool.js | 150 ++++++++++++++++++++++++------------ 1 file changed, 99 insertions(+), 51 deletions(-) diff --git a/api/tools/log-parse-tool.js b/api/tools/log-parse-tool.js index 17309c3..9213c7b 100644 --- a/api/tools/log-parse-tool.js +++ b/api/tools/log-parse-tool.js @@ -11,6 +11,14 @@ const VehicleClasses = Object.freeze({ UNKNOWN: 'Unbekannt', }); +const playerNameRegex = /^(.*?)\s\(/; + +const sideRegex = /(side:\s(.*?)\))/; + +const magazineRegex = /(magazine:\s(.*?)\))/; + +const vehicleRegex = /(vehicle:\s(.*?)\))/; + const parseWarLog = (lineArray, war) => { const NAME_TOO_LONG_ERROR = 'Error: ENAMETOOLONG: name too long, open \''; @@ -39,10 +47,11 @@ const parseWarLog = (lineArray, war) => { ]; const addPlayerIfNotExists = (inputPlayer, steamUUID) => { + console.log(inputPlayer) const player = getPlayerAndFractionFromString(inputPlayer); if (player && player.name && player.fraction && !playerArrayContains(stats.players, player)) { - player['warId'] = war._id; - player['steamUUID'] = steamUUID; + player[ 'warId' ] = war._id; + player[ 'steamUUID' ] = steamUUID; stats.players.push(player); } }; @@ -61,7 +70,7 @@ const parseWarLog = (lineArray, war) => { */ stats.clean.push(line); - const shooterString = line.substring(line.lastIndexOf(' von: ') + 6, line.lastIndexOf('."')); + const shooterString = line.substring(line.lastIndexOf(' von: ') + 6, line.search(/$/) - 1); const shooter = getPlayerAndFractionFromString(shooterString); if (line.includes('Fahrzeug:')) { @@ -70,7 +79,7 @@ const parseWarLog = (lineArray, war) => { if (target && shooter && target.fraction !== shooter.fraction) { stats.vehicles.push({ war: war._id, - time: getFullTimeDate(war.date, line.split(WHITESPACE)[5]), + time: getFullTimeDate(war.date, line.split(WHITESPACE)[ 5 ]), shooter: shooter ? shooter.name : null, target: target ? target.name : null, fraction: shooter ? shooter.fraction : 'NONE', @@ -82,7 +91,7 @@ const parseWarLog = (lineArray, war) => { const target = getPlayerAndFractionFromString(targetString); stats.kills.push({ war: war._id, - time: getFullTimeDate(war.date, line.split(WHITESPACE)[5]), + time: getFullTimeDate(war.date, line.split(WHITESPACE)[ 5 ]), shooter: shooter ? shooter.name : null, target: target ? target.name : null, friendlyFire: shooter ? target.fraction === shooter.fraction : false, @@ -96,16 +105,16 @@ const parseWarLog = (lineArray, war) => { 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)); + 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]); + 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]); + 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 ')) { /** @@ -127,7 +136,7 @@ const parseWarLog = (lineArray, war) => { stats.flag.push({ war: war._id, - time: getFullTimeDate(war.date, line.split(WHITESPACE)[5]), + time: getFullTimeDate(war.date, line.split(WHITESPACE)[ 5 ]), player: playerName, flagFraction: flagFraction, capture: capture, @@ -140,8 +149,8 @@ const parseWarLog = (lineArray, war) => { const pt = line.split(WHITESPACE); if (line.includes('Endpunktestand')) { - stats.war['ptBlufor'] = parseInt(pt[9]); - stats.war['ptOpfor'] = parseInt(pt[12].slice(0, -1)); + stats.war[ 'ptBlufor' ] = parseInt(pt[ 9 ]); + stats.war[ 'ptOpfor' ] = parseInt(pt[ 12 ].slice(0, -1)); // EXIT LOOP return true; } else { @@ -161,7 +170,7 @@ const parseWarLog = (lineArray, war) => { stats.revive.push({ war: war._id, - time: getFullTimeDate(war.date, line.split(WHITESPACE)[5]), + time: getFullTimeDate(war.date, line.split(WHITESPACE)[ 5 ]), stabilized: stabilized, medic: medic.name, patient: patient.name, @@ -180,7 +189,7 @@ const parseWarLog = (lineArray, war) => { stats.transport.push({ war: war._id, - time: getFullTimeDate(war.date, line.split(WHITESPACE)[5]), + time: getFullTimeDate(war.date, line.split(WHITESPACE)[ 5 ]), driver: driver ? driver.name : null, passenger: passenger ? passenger.name : null, fraction: driver ? driver.fraction : 'NONE', @@ -197,35 +206,37 @@ const parseWarLog = (lineArray, war) => { }); 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; + 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 ][ '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( + stats.players[ i ][ 'vehicleLight' ] = stats.vehicles.filter( (vehicle) => vehicle.shooter === playerName && vehicle.vehicleClass === 'LIGHT' && VEHICLE_BLACKLIST.indexOf(vehicle.target) < 0).length; - stats.players[i]['vehicleHeavy'] = stats.vehicles.filter( + stats.players[ i ][ 'vehicleHeavy' ] = stats.vehicles.filter( (vehicle) => vehicle.shooter === playerName && vehicle.vehicleClass === 'HEAVY' && VEHICLE_BLACKLIST.indexOf(vehicle.target) < 0).length; - stats.players[i]['vehicleAir'] = stats.vehicles.filter( + stats.players[ i ][ 'vehicleAir' ] = stats.vehicles.filter( (vehicle) => vehicle.shooter === playerName && vehicle.vehicleClass === 'AIR' && VEHICLE_BLACKLIST.indexOf(vehicle.target) < 0).length; - stats.players[i]['friendlyFire'] = stats.kills.filter( + 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 ][ '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 ][ '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 ][ '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.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; @@ -237,7 +248,7 @@ const parseWarLog = (lineArray, war) => { const getRespawnEntry = (respawn, playerName, warId, warDate) => { return { war: warId, - time: getFullTimeDate(warDate, respawn[5]), + time: getFullTimeDate(warDate, respawn[ 5 ]), player: playerName, }; }; @@ -245,9 +256,9 @@ const getRespawnEntry = (respawn, playerName, warId, warDate) => { 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)), + 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', }; }; @@ -255,32 +266,69 @@ const getPointsEntry = (pt, line, warId, warDate) => { 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]), + 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(WHITESPACE); - const fraction = nameArray[nameArray.length - 1] !== '(ENEMY)' ? - nameArray[nameArray.length - 1] === '(WEST)' ? +const getPlayerAndFractionFromString = (inputString) => { + const playerNameRegexMatch = playerNameRegex.exec(inputString); + const sideMatch = sideRegex.exec(inputString); + const magazineMatch = magazineRegex.exec(inputString); + const vehicleMatch = vehicleRegex.exec(inputString); + + let name; + if (playerNameRegexMatch && playerNameRegexMatch.length >= 2) { + // NAME + name = playerNameRegexMatch[ 1 ]; + } + + let side + if (sideMatch && sideMatch.length >= 3) { + // SIDE + side = sideMatch[ 2 ]; + } else { + const inputArray = inputString.split(WHITESPACE) + if (inputArray && inputArray.length === 2) { + side = inputArray[ 1 ].replace('(', '').replace(')', ''); + } + } + + if (magazineMatch && magazineMatch.length >= 3) { + // MAGAZINE + // console.log(magazineMatch[2]) + } + + if (vehicleMatch && vehicleMatch.length >= 3 && vehicleMatch[ 2 ]) { + let vehicle = vehicleMatch[ 2 ]; + if (new RegExp("\\(.*$").test(vehicle)) { + vehicle = vehicle.concat(")"); + } + // VEHICLE + // console.log(vehicle) + } + + let fraction; + if (side) { + fraction = side !== 'ENEMY' ? side === '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' + undefined; + } + + // do not return player for 'Error: No unit' if (name && name !== 'Error: No unit') { - return {name: name, fraction: fraction}; + return {name: name.trim(), fraction: fraction}; } }; const getVehicleAndFractionFromString = (nameClassFractionString) => { const nameArray = nameClassFractionString.trim().split(WHITESPACE); - const vehicleFraction = nameArray[nameArray.length - 1].slice(0,-1); + const vehicleFraction = nameArray[ nameArray.length - 1 ].slice(0, -1); nameArray.pop(); nameArray.pop(); - const vehicleClassString = nameArray[nameArray.length - 1].replace('(', '').replace(')', ''); + const vehicleClassString = nameArray[ nameArray.length - 1 ].replace('(', '').replace(')', ''); nameArray.pop(); nameArray.pop(); const vehicleName = nameArray.join(WHITESPACE); @@ -294,7 +342,7 @@ const getVehicleAndFractionFromString = (nameClassFractionString) => { let vehicleClass; for (const key in VehicleClasses) { - if (VehicleClasses.hasOwnProperty(key) && VehicleClasses[key] === vehicleClassString) { + if (VehicleClasses.hasOwnProperty(key) && VehicleClasses[ key ] === vehicleClassString) { vehicleClass = key; } } @@ -311,14 +359,14 @@ const transformMoneyString = (budgetString) => { return parseInt(budgetString); } 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 ]))); }; const getFullTimeDate = (date, timeString) => { const returnDate = new Date(date); const time = timeString.split(':'); - returnDate.setHours(time[0]); - returnDate.setMinutes(time[1]); + returnDate.setHours(time[ 0 ]); + returnDate.setMinutes(time[ 1 ]); return returnDate; };