Fix basic player information parsing

pull/35/head
HardiReady 2018-06-04 21:46:07 +02:00
parent c14b33f9c5
commit f3f5638667
1 changed files with 99 additions and 51 deletions

View File

@ -11,6 +11,14 @@ const VehicleClasses = Object.freeze({
UNKNOWN: 'Unbekannt', UNKNOWN: 'Unbekannt',
}); });
const playerNameRegex = /^(.*?)\s\(/;
const sideRegex = /(side:\s(.*?)\))/;
const magazineRegex = /(magazine:\s(.*?)\))/;
const vehicleRegex = /(vehicle:\s(.*?)\))/;
const parseWarLog = (lineArray, war) => { const parseWarLog = (lineArray, war) => {
const NAME_TOO_LONG_ERROR = 'Error: ENAMETOOLONG: name too long, open \''; const NAME_TOO_LONG_ERROR = 'Error: ENAMETOOLONG: name too long, open \'';
@ -39,6 +47,7 @@ const parseWarLog = (lineArray, war) => {
]; ];
const addPlayerIfNotExists = (inputPlayer, steamUUID) => { const addPlayerIfNotExists = (inputPlayer, steamUUID) => {
console.log(inputPlayer)
const player = getPlayerAndFractionFromString(inputPlayer); const player = getPlayerAndFractionFromString(inputPlayer);
if (player && player.name && player.fraction && !playerArrayContains(stats.players, player)) { if (player && player.name && player.fraction && !playerArrayContains(stats.players, player)) {
player[ 'warId' ] = war._id; player[ 'warId' ] = war._id;
@ -61,7 +70,7 @@ const parseWarLog = (lineArray, war) => {
*/ */
stats.clean.push(line); 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); const shooter = getPlayerAndFractionFromString(shooterString);
if (line.includes('Fahrzeug:')) { if (line.includes('Fahrzeug:')) {
@ -200,7 +209,8 @@ const parseWarLog = (lineArray, war) => {
const playerName = stats.players[ i ].name; const playerName = stats.players[ i ].name;
stats.players[ i ][ 'respawn' ] = stats.respawn.filter((res) => res.player === playerName).length; 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 // TODO: use vehicle class description from enum
stats.players[ i ][ 'vehicleLight' ] = stats.vehicles.filter( stats.players[ i ][ 'vehicleLight' ] = stats.vehicles.filter(
@ -224,7 +234,8 @@ const parseWarLog = (lineArray, war) => {
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 ][ '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 ][ 'friendlyFire' ] - stats.players[ i ][ 'death' ] - stats.players[ i ][ 'respawn' ];
} }
@ -262,16 +273,53 @@ const getBudgetEntry = (budg, warId, warDate) => {
}; };
}; };
const getPlayerAndFractionFromString = (nameAndFractionString) => { const getPlayerAndFractionFromString = (inputString) => {
const nameArray = nameAndFractionString.split(WHITESPACE); const playerNameRegexMatch = playerNameRegex.exec(inputString);
const fraction = nameArray[nameArray.length - 1] !== '(ENEMY)' ? const sideMatch = sideRegex.exec(inputString);
nameArray[nameArray.length - 1] === '(WEST)' ? 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' : 'BLUFOR' : 'OPFOR' :
undefined; undefined;
const name = nameAndFractionString.substring(0, nameAndFractionString.indexOf(nameArray[nameArray.length - 1]) - 1); }
// do not return player for 'Selbstverschulden' or 'Error: No unit'
// do not return player for 'Error: No unit'
if (name && name !== 'Error: No unit') { if (name && name !== 'Error: No unit') {
return {name: name, fraction: fraction}; return {name: name.trim(), fraction: fraction};
} }
}; };