finish basci backend implementation

pull/15/head
Florian Hartwich 2017-10-21 14:54:58 +02:00
parent 35f6ac04b3
commit 1acaba5a60
3 changed files with 34 additions and 18 deletions

View File

@ -63,25 +63,31 @@ wars.route('/')
}) })
.post(apiAuthenticationMiddleware, checkMT, upload.single('log'), (req, res, next) => { .post(apiAuthenticationMiddleware, checkMT, upload.single('log'), (req, res, next) => {
let body = req.body; const body = req.body;
const bodyWar = new WarModel(body); const warBody = 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); const statsResult = parseWarLog(lineArray, warBody);
res.locals.processed = true; statsResult.war.save((err, war) => {
if (err) {
return next(err);
}
PlayerModel.create(statsResult.players, function (err) {
if (err) {
return next(err);
}
res.status(codes.created);
res.locals.items = war;
next(); 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;

View File

@ -18,7 +18,7 @@ const parseWarLog = (lineArray, war) => {
const addPlayersIfNotExists = (inputPlayers) => { const addPlayersIfNotExists = (inputPlayers) => {
inputPlayers.forEach(player => { inputPlayers.forEach(player => {
if (player && !arrayContains(stats.players, player)) { if (player && player.name && player.fraction && !arrayContains(stats.players, player)) {
player['warId'] = war._id; player['warId'] = war._id;
stats.players.push(player); stats.players.push(player);
} }
@ -33,7 +33,6 @@ const parseWarLog = (lineArray, war) => {
stats.clean.push(line); stats.clean.push(line);
const shooterString = line.substring(line.lastIndexOf(' von: ') + 6, line.lastIndexOf('. :OPT LOG END')); const shooterString = line.substring(line.lastIndexOf(' von: ') + 6, line.lastIndexOf('. :OPT LOG END'));
const shooter = getPlayerAndFractionFromString(shooterString); const shooter = getPlayerAndFractionFromString(shooterString);
const targetString = line.substring(line.lastIndexOf(' || ') + 4, line.lastIndexOf(' von:')); const targetString = line.substring(line.lastIndexOf(' || ') + 4, line.lastIndexOf(' von:'));
const target = getPlayerAndFractionFromString(targetString); const target = getPlayerAndFractionFromString(targetString);
@ -58,6 +57,7 @@ const parseWarLog = (lineArray, war) => {
stats.war['endBudgetBlufor'] = transformMoneyString(budg[11]); stats.war['endBudgetBlufor'] = transformMoneyString(budg[11]);
stats.war['endBudgetOpfor'] = transformMoneyString(budg[14]); stats.war['endBudgetOpfor'] = transformMoneyString(budg[14]);
} else if (line.includes('Startbudget')) { } else if (line.includes('Startbudget')) {
stats.war.date = new Date(budg[0].substr(0, budg[0].length - 1).split('/').join('-') + 'T22:00:00.000+02:00');
stats.war['budgetBlufor'] = transformMoneyString(budg[11]); stats.war['budgetBlufor'] = transformMoneyString(budg[11]);
stats.war['budgetOpfor'] = transformMoneyString(budg[14]); stats.war['budgetOpfor'] = transformMoneyString(budg[14]);
} else { } else {
@ -114,7 +114,8 @@ const parseWarLog = (lineArray, war) => {
if (line.includes('Revive')) { if (line.includes('Revive')) {
stats.clean.push(line); stats.clean.push(line);
const stabilized = !!line.includes('stabilisiert'); const stabilized = !!line.includes('stabilisiert');
const medicName = line.substring(line.lastIndexOf('wurde von ') + 10, line.lastIndexOf(stabilized ? ' stabilisiert' : ' wiederbelebt')); const medicName = line.substring(line.lastIndexOf('wurde von ') + 10,
line.lastIndexOf(stabilized ? ' stabilisiert' : ' wiederbelebt'));
const medic = getPlayerAndFractionFromString(medicName); const medic = getPlayerAndFractionFromString(medicName);
const patientName = line.substring(line.lastIndexOf('|| ') + 3, line.lastIndexOf(' wurde von')); const patientName = line.substring(line.lastIndexOf('|| ') + 3, line.lastIndexOf(' wurde von'));
const patient = getPlayerAndFractionFromString(patientName); const patient = getPlayerAndFractionFromString(patientName);
@ -153,11 +154,20 @@ 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;
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.war.playersBlufor = stats.players.filter(player => player.fraction === 'BLUFOR').length; stats.war.playersBlufor = stats.players.filter(player => player.fraction === 'BLUFOR').length;
stats.war.playersOpfor = stats.players.filter(player => player.fraction === 'OPFOR').length; stats.war.playersOpfor = stats.players.filter(player => player.fraction === 'OPFOR').length;
return stats; return stats;
}; };
const getRespawnEntry = (respawn, playerName, warId) => { const getRespawnEntry = (respawn, playerName, warId) => {
@ -189,10 +199,10 @@ const getBudgetEntry = (budg, warId) => {
const getPlayerAndFractionFromString = (nameAndFractionString) => { const getPlayerAndFractionFromString = (nameAndFractionString) => {
const nameArray = nameAndFractionString.split(' '); const nameArray = nameAndFractionString.split(' ');
const fraction = nameArray[nameArray.length - 1] === '(WEST)' ? 'BLUFOR' : 'OPFOR'; 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); const name = nameAndFractionString.substring(0, nameAndFractionString.indexOf(nameArray[nameArray.length - 1]) - 1);
// do not return player for 'Selbstverschulden' // do not return player for 'Selbstverschulden'
if (name || name === 'Error: No unit') { if (name && name !== 'Error: No unit') {
return {name: name, fraction: fraction}; return {name: name, fraction: fraction};
} }
}; };

View File

@ -1,6 +1,6 @@
{ {
"name": "opt-cc", "name": "opt-cc",
"version": "1.5.4", "version": "1.6.0",
"license": "MIT", "license": "MIT",
"private": true, "private": true,
"scripts": { "scripts": {