Compare commits
No commits in common. "35f6ac04b3455fa82aa8a2d149d6ccf02294e6f1" and "f1449e50477b39cf050c9a27824feca75c1d7acc" have entirely different histories.
35f6ac04b3
...
f1449e5047
|
@ -11,11 +11,11 @@ const sortCollectionBy = (collection, key) => {
|
||||||
return collection;
|
return collection;
|
||||||
};
|
};
|
||||||
|
|
||||||
const playerArrayContains = (arr, item) => {
|
const arrayContains = (arr, user) => {
|
||||||
let i = 0, count = arr.length, matchFound = false;
|
let i = 0, count = arr.length, matchFound = false;
|
||||||
|
|
||||||
for(; i < count; i++) {
|
for(; i < count; i++) {
|
||||||
if (arr[i].name === item.name && arr[i].fraction === item.fraction) {
|
if (arr[i] === user) {
|
||||||
matchFound = true;
|
matchFound = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -25,4 +25,4 @@ const playerArrayContains = (arr, item) => {
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.sortCollection = sortCollectionBy;
|
exports.sortCollection = sortCollectionBy;
|
||||||
exports.arrayContains = playerArrayContains;
|
exports.arrayContains = arrayContains;
|
|
@ -10,16 +10,19 @@ const WarSchema = new Schema({
|
||||||
},
|
},
|
||||||
date: {
|
date: {
|
||||||
type: Date,
|
type: Date,
|
||||||
|
required: true
|
||||||
},
|
},
|
||||||
ptBlufor: {
|
ptBlufor: {
|
||||||
type: Number,
|
type: Number,
|
||||||
get: v => Math.round(v),
|
get: v => Math.round(v),
|
||||||
set: v => Math.round(v),
|
set: v => Math.round(v),
|
||||||
|
required: true
|
||||||
},
|
},
|
||||||
ptOpfor: {
|
ptOpfor: {
|
||||||
type: Number,
|
type: Number,
|
||||||
get: v => Math.round(v),
|
get: v => Math.round(v),
|
||||||
set: v => Math.round(v),
|
set: v => Math.round(v),
|
||||||
|
required: true
|
||||||
},
|
},
|
||||||
playersBlufor: {
|
playersBlufor: {
|
||||||
type: Number,
|
type: Number,
|
||||||
|
@ -35,33 +38,8 @@ const WarSchema = new Schema({
|
||||||
},
|
},
|
||||||
campaign: {
|
campaign: {
|
||||||
type: mongoose.Schema.Types.ObjectId,
|
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',
|
collection: 'war',
|
||||||
timestamps: {createdAt: 'timestamp'}
|
timestamps: {createdAt: 'timestamp'}
|
||||||
|
|
|
@ -64,24 +64,20 @@ wars.route('/')
|
||||||
|
|
||||||
.post(apiAuthenticationMiddleware, checkMT, upload.single('log'), (req, res, next) => {
|
.post(apiAuthenticationMiddleware, checkMT, upload.single('log'), (req, res, next) => {
|
||||||
let body = req.body;
|
let body = req.body;
|
||||||
const bodyWar = new WarModel(body);
|
const war = new WarModel(body);
|
||||||
|
|
||||||
if (req.file) {
|
if (req.file) {
|
||||||
// bodyWar.save((err, war) => {
|
|
||||||
// if (err) {
|
|
||||||
// return next(err);
|
|
||||||
// }
|
|
||||||
fs.readFile(req.file.buffer, (file, err) => {
|
fs.readFile(req.file.buffer, (file, err) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
return next(err);
|
return next(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
const lineArray = file.toString().split("\n");
|
const lineArray = file.toString().split("\n");
|
||||||
res.locals.items = parseWarLog(lineArray, bodyWar);
|
parseWarLog(lineArray, {});
|
||||||
res.locals.processed = true;
|
|
||||||
next();
|
|
||||||
});
|
});
|
||||||
// })
|
res.locals.processed = true;
|
||||||
|
return next();
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
const err = new Error('no Logfile provided');
|
const err = new Error('no Logfile provided');
|
||||||
err.status = codes.wrongmediasend;
|
err.status = codes.wrongmediasend;
|
||||||
|
|
|
@ -1,214 +1,146 @@
|
||||||
'use strict';
|
"use strict";
|
||||||
|
|
||||||
const arrayContains = require('./util').arrayContains;
|
const arrayContains = require('../middleware/util').arrayContains;
|
||||||
|
|
||||||
const parseWarLog = (lineArray, war) => {
|
const parseWarLog = (lineArray, war) => {
|
||||||
const stats = {
|
const clean = [];
|
||||||
war: war,
|
const budget = [];
|
||||||
clean: [],
|
const points = [];
|
||||||
budget: [],
|
const kills = [];
|
||||||
points: [],
|
const respawn = [];
|
||||||
kills: [],
|
const revive = [];
|
||||||
respawn: [],
|
const flag = [];
|
||||||
revive: [],
|
const transport = [];
|
||||||
flag: [],
|
const playerNames = [];
|
||||||
transport: [],
|
|
||||||
players: []
|
|
||||||
};
|
|
||||||
|
|
||||||
const addPlayersIfNotExists = (inputPlayers) => {
|
const addPlayerIfNotExists = (playerName) => {
|
||||||
inputPlayers.forEach(player => {
|
if (playerName !== 'Error: No unit' && !arrayContains(playerNames, playerName)) {
|
||||||
if (player && !arrayContains(stats.players, player)) {
|
playerNames.push(playerName);
|
||||||
player['warId'] = war._id;
|
|
||||||
stats.players.push(player);
|
|
||||||
}
|
}
|
||||||
})
|
|
||||||
};
|
};
|
||||||
|
|
||||||
lineArray.forEach(line => {
|
lineArray.forEach(line => {
|
||||||
/**
|
if (line.includes("Abschuss")) {
|
||||||
* KILLS
|
clean.push(line);
|
||||||
*/
|
// const kill = line.split(" ");
|
||||||
if (line.includes('Abschuss') && !line.includes('Fahrzeug')) {
|
// for (let i=0; i< kill.length; i++ ) {
|
||||||
stats.clean.push(line);
|
// console.log(i + " +++ " + kill[i]);
|
||||||
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")) {
|
||||||
* BUDGET
|
clean.push(line);
|
||||||
*/
|
const budg = line.split(" ");
|
||||||
if (line.includes('Budget')) {
|
if (line.includes("Endbudget")) {
|
||||||
stats.clean.push(line);
|
war["endBudgetBlufor"] = transformMoneyString(budg[11]);
|
||||||
const budg = line.split(' ');
|
war['endBudgetOpfor'] = transformMoneyString(budg[14]);
|
||||||
if (line.includes('Endbudget')) {
|
} else if (line.includes("Startbudget")) {
|
||||||
stats.war['endBudgetBlufor'] = transformMoneyString(budg[11]);
|
war["budgetBlufor"] = transformMoneyString(budg[11]);
|
||||||
stats.war['endBudgetOpfor'] = transformMoneyString(budg[14]);
|
war["budgetOpfor"] = transformMoneyString(budg[14]);
|
||||||
} else if (line.includes('Startbudget')) {
|
|
||||||
stats.war['budgetBlufor'] = transformMoneyString(budg[11]);
|
|
||||||
stats.war['budgetOpfor'] = transformMoneyString(budg[14]);
|
|
||||||
} else {
|
} else {
|
||||||
stats.budget.push(getBudgetEntry(budg, war._id));
|
budget.push(getBudgetEntry(budg));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
if (line.includes("Fahne")) {
|
||||||
* FLAG
|
clean.push(line);
|
||||||
*/
|
|
||||||
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")) {
|
||||||
* POINTS
|
clean.push(line);
|
||||||
*/
|
const pt = line.split(" ");
|
||||||
if (line.includes('Punkte')) {
|
|
||||||
stats.clean.push(line);
|
|
||||||
const pt = line.split(' ');
|
|
||||||
|
|
||||||
if (line.includes('Endpunktestand')) {
|
if (line.includes("Endpunktestand")) {
|
||||||
stats.war['ptBlufor'] = parseInt(pt[11]);
|
war['ptBlufor'] = parseInt(pt[11]);
|
||||||
stats.war['ptOpfor'] = parseInt(pt[14].slice(0, -1));
|
war['ptOpfor'] = parseInt(pt[14].slice(0, -1));
|
||||||
} else {
|
} else {
|
||||||
stats.points.push(getPointsEntry(pt, war._id))
|
points.push(getPointsEntry(pt))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
if (line.includes("Respawn")) {
|
||||||
* RESPAWN
|
clean.push(line);
|
||||||
*/
|
const resp = line.split(" ");
|
||||||
if (line.includes('Respawn')) {
|
const playerName = line.substring(line.lastIndexOf("Spieler:") + 9, line.lastIndexOf("-") - 1);
|
||||||
stats.clean.push(line);
|
|
||||||
const resp = line.split(' ');
|
respawn.push(getRespawnEntry(resp, playerName));
|
||||||
const playerName = line.substring(line.lastIndexOf('Spieler:') + 9, line.lastIndexOf('-') - 1);
|
addPlayerIfNotExists(playerName);
|
||||||
stats.respawn.push(getRespawnEntry(resp, playerName, war._id));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
if (line.includes("Revive")) {
|
||||||
* REVIVE
|
clean.push(line);
|
||||||
*/
|
//console.log(line);
|
||||||
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 ||")) {
|
||||||
* TRANSPORT
|
clean.push(line);
|
||||||
*/
|
const driverName = line.substring(line.lastIndexOf("wurde von ") + 10, line.lastIndexOf(" eingeflogen"));
|
||||||
if (line.includes('Transport ||')) {
|
const driverNameArray = driverName.split(" ");
|
||||||
stats.clean.push(line);
|
const driverFraction = driverNameArray[driverNameArray.length-1] === "(WEST)" ? "BLUFOR" : "OPFOR";
|
||||||
const driverString = line.substring(line.lastIndexOf('wurde von ') + 10, line.lastIndexOf(' eingeflogen'));
|
const sanitizedDriverName = driverName.substring(0, driverName.indexOf(driverNameArray[driverNameArray.length-1])-1);
|
||||||
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({
|
const passengerName = line.substring(line.lastIndexOf("|| ") + 3, line.lastIndexOf(" wurde von"));
|
||||||
war: war._id,
|
const passengerNameArray = passengerName.split(" ");
|
||||||
time: getDateTime(line.split(' ')[5]),
|
const passengerFraction = passengerNameArray[passengerNameArray.length-1] === "(WEST)" ? "BLUFOR" : "OPFOR";
|
||||||
driver: driver.name,
|
const sanitizedPassengerName = passengerName.substring(0, passengerName.indexOf(passengerNameArray[passengerNameArray.length-1])-1);
|
||||||
passenger: passenger.name,
|
const distance = parseInt(line.substring(line.lastIndexOf("eingeflogen (") + 13, line.lastIndexOf("m)") - 1));
|
||||||
|
|
||||||
|
transport.push({
|
||||||
|
war: "blablub7z8",
|
||||||
|
driver: sanitizedDriverName,
|
||||||
|
passenger: sanitizedPassengerName,
|
||||||
distance: distance
|
distance: distance
|
||||||
});
|
});
|
||||||
|
|
||||||
addPlayersIfNotExists([driver, passenger]);
|
addPlayerIfNotExists(sanitizedDriverName);
|
||||||
|
addPlayerIfNotExists(sanitizedPassengerName);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
//
|
||||||
stats.war.playersBlufor = stats.players.filter(player => player.fraction === 'BLUFOR').length;
|
playerNames.forEach(budg => console.log(budg));
|
||||||
stats.war.playersOpfor = stats.players.filter(player => player.fraction === 'OPFOR').length;
|
|
||||||
|
|
||||||
return stats;
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const getRespawnEntry = (respawn, playerName, warId) => {
|
function getRespawnEntry(respawn, playerName) {
|
||||||
return {
|
return {
|
||||||
war: warId,
|
war: "1234567",
|
||||||
time: getDateTime(respawn[5]),
|
time: getDateTime(respawn[5]),
|
||||||
player: playerName
|
player: playerName
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
const getPointsEntry = (pt, warId) => {
|
function getPointsEntry(pt) {
|
||||||
return {
|
return {
|
||||||
warId: warId,
|
warId: "123-xyz-123",
|
||||||
time: getDateTime(pt[5]),
|
time: getDateTime(pt[5]),
|
||||||
ptBlufor: parseInt(pt[12]),
|
ptBlufor: parseInt(pt[12]),
|
||||||
ptOpfor: parseInt(pt[15].slice(0, -1))
|
ptOpfor: parseInt(pt[15].slice(0, -1))
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
const getBudgetEntry = (budg, warId) => {
|
function getBudgetEntry(budg) {
|
||||||
return {
|
return {
|
||||||
warId: warId,
|
warId: "123-xyz-123",
|
||||||
time: getDateTime(budg[5]),
|
time: getDateTime(budg[5]),
|
||||||
fraction: budg[9] === 'NATO' ? 'BLUFOR' : 'OPFOR',
|
fraction: budg[9] === "NATO" ? "BLUFOR" : "OPFOR",
|
||||||
oldBudget: transformMoneyString(budg[11]),
|
oldBudget: transformMoneyString(budg[11]),
|
||||||
newBudget: transformMoneyString(budg[14])
|
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) => {
|
const transformMoneyString = (budgetString) => {
|
||||||
if (!budgetString.includes('e+')) {
|
if (!budgetString.includes("e+")) {
|
||||||
return parseInt(budgetString);
|
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])));
|
return Math.round(parseFloat(budget[0]) * Math.pow(10, parseInt(budget[1])));
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const getDateTime = (timeString) => {
|
function getDateTime(timeString) {
|
||||||
const timeZone = 'Z';
|
const timeZone = 'Z';
|
||||||
return new Date('1999-01-01T0' + timeString + timeZone);
|
return new Date("1999-01-01T0" + timeString + timeZone);
|
||||||
};
|
}
|
||||||
|
|
||||||
module.exports = parseWarLog;
|
module.exports = parseWarLog;
|
||||||
|
|
Loading…
Reference in New Issue