Simplify logic for player campaign detail page init

pull/34/head
HardiReady 2018-04-27 09:23:43 +02:00
parent dd7bce59e3
commit 216c19dccd
1 changed files with 37 additions and 142 deletions

View File

@ -23,15 +23,6 @@ export class CampaignPlayerDetailComponent implements OnInit {
graphData: any[] = [];
sumData: any[] = [];
yAxisKill = 'Kills';
yAxisFriendlyFire = 'FriendlyFire';
yAxisVehicleLight = 'Fahrzeug (Light)';
yAxisVehicleHeavy = 'Fahrzeug (Heavy)';
yAxisVehicleAir = 'Fahrzeug (Air)';
yAxisDeath = 'Tode';
yAxisRespawn = 'Respawn';
yAxisRevive = 'Revive';
yAxisCapture = 'Eroberungen';
avgLabel = 'Durchschnitt';
colorScheme = {
@ -44,16 +35,6 @@ export class CampaignPlayerDetailComponent implements OnInit {
};
showRefLines = true;
showRefLabels = true;
killRefLines = [];
vehicleLightRefLines = [];
vehicleHeavyRefLines = [];
vehicleAirRefLines = [];
deathRefLines = [];
captureRefLines = [];
friendlyFireRefLines = [];
reviveRefLines = [];
respawnRefLines = [];
gradient = false;
xAxis = true;
yAxis = true;
@ -64,23 +45,12 @@ export class CampaignPlayerDetailComponent implements OnInit {
timeline = false;
roundDomains = true;
totalKills;
totalFriendlyFire;
totalVehicleLight;
totalVehicleHeavy;
totalVehicleAir;
totalDeath;
totalRespawn;
totalRevive;
totalCapture;
kdRatio = 0;
maxKd = 1.7;
respawnDeathRatio = 0;
maxRespawnDeathRatio = 1;
constructor(private playerService: PlayerService) {
}
@ -90,69 +60,26 @@ export class CampaignPlayerDetailComponent implements OnInit {
this.campaignPlayer = campaignPlayer;
this.graphData = [
{
data: this.assignData(this.yAxisKill, 'kill'),
refLine: this.killRefLines,
label: this.yAxisKill,
total: this.totalKills
},
{
data: this.assignData(this.yAxisFriendlyFire, 'friendlyFire'),
refLine: this.friendlyFireRefLines,
label: this.yAxisFriendlyFire,
total: this.totalFriendlyFire
},
{
data: this.assignData(this.yAxisDeath, 'death'),
refLine: this.deathRefLines,
label: this.yAxisDeath,
total: this.totalDeath
},
{
data: this.assignData(this.yAxisRespawn, 'respawn'),
refLine: this.respawnRefLines,
label: this.yAxisRespawn,
total: this.totalRespawn
},
{
data: this.assignData(this.yAxisRevive, 'revive'),
refLine: this.reviveRefLines,
label: this.yAxisRevive,
total: this.totalRevive
},
{
data: this.assignData(this.yAxisCapture, 'flagTouch'),
refLine: this.captureRefLines,
label: this.yAxisCapture,
total: this.totalCapture
},
{
data: this.assignData(this.yAxisVehicleLight, 'vehicleLight'),
refLine: this.vehicleLightRefLines,
label: this.yAxisVehicleLight,
total: this.totalVehicleLight
},
{
data: this.assignData(this.yAxisVehicleHeavy, 'vehicleHeavy'),
refLine: this.vehicleHeavyRefLines,
label: this.yAxisVehicleHeavy,
total: this.totalVehicleHeavy
},
{
data: this.assignData(this.yAxisVehicleAir, 'vehicleAir'),
refLine: this.vehicleAirRefLines,
label: this.yAxisVehicleAir,
total: this.totalVehicleAir
},
{key: 'kill', label: 'Kills',},
{key: 'friendlyFire', label: 'Friendly Fire',},
{key: 'death', label: 'Tode',},
{key: 'respawn', label: 'Respawn',},
{key: 'revive', label: 'Revive',},
{key: 'flagTouch', label: 'Eroberungen',},
{key: 'vehicleLight', label: 'Fahrzeug (Leicht)',},
{key: 'vehicleHeavy', label: 'Fahrzeug (Schwer)',},
{key: 'vehicleAir', label: 'Fahrzeug (Luft)',},
];
const totalDeathDiv = this.totalDeath === 0 ? 1 : this.totalDeath;
this.kdRatio = parseFloat((this.totalKills / totalDeathDiv).toFixed(2));
this.initDataArray();
const totalDeathDiv = this.graphData[2].total === 0 ? 1 : this.graphData[2].total;
this.kdRatio = parseFloat((this.graphData[0].total / totalDeathDiv).toFixed(2));
if (this.kdRatio > 1) {
this.maxKd = this.kdRatio * 1.7;
}
this.respawnDeathRatio = parseFloat((this.totalRespawn / totalDeathDiv).toFixed(2));
this.respawnDeathRatio = parseFloat((this.graphData[3].total / totalDeathDiv).toFixed(2));
// we can not directly push to target array, since only full reference changes trigger the refresh of data
const tmpSumData = [];
@ -163,61 +90,29 @@ export class CampaignPlayerDetailComponent implements OnInit {
});
}
private assignData(label, field) {
const killObj = {
name: label,
series: []
};
const playerLength = this.campaignPlayer.players.length;
let total = 0;
for (let i = 0; i < playerLength; i++) {
const warDateString = ChartUtils.getShortDateString(this.campaignPlayer.players[i].warId.date);
const value = this.campaignPlayer.players[i][field];
killObj.series.push({
name: warDateString,
value: value
});
total += value;
}
switch (field) {
case 'kill':
this.killRefLines.push({value: total / playerLength, name: this.avgLabel});
this.totalKills = total;
break;
case 'friendlyFire':
this.friendlyFireRefLines.push({value: total / playerLength, name: this.avgLabel});
this.totalFriendlyFire = total;
break;
case 'death':
this.deathRefLines.push({value: total / playerLength, name: this.avgLabel});
this.totalDeath = total;
break;
case 'respawn':
this.respawnRefLines.push({value: total / playerLength, name: this.avgLabel});
this.totalRespawn = total;
break;
case 'revive':
this.reviveRefLines.push({value: total / playerLength, name: this.avgLabel});
this.totalRevive = total;
break;
case 'flagTouch':
this.captureRefLines.push({value: total / playerLength, name: this.avgLabel});
this.totalCapture = total;
break;
case 'vehicleLight':
this.vehicleLightRefLines.push({value: total / playerLength, name: this.avgLabel});
this.totalVehicleLight = total;
break;
case 'vehicleHeavy':
this.vehicleHeavyRefLines.push({value: total / playerLength, name: this.avgLabel});
this.totalVehicleHeavy = total;
break;
case 'vehicleAir':
this.vehicleAirRefLines.push({value: total / playerLength, name: this.avgLabel});
this.totalVehicleAir = total;
break;
}
return [killObj];
private initDataArray() {
this.graphData.forEach(dataSet => {
const killObj = {
name: dataSet.label,
series: []
};
const playerLength = this.campaignPlayer.players.length;
let total = 0;
for (let i = 0; i < playerLength; i++) {
const warDateString = ChartUtils.getShortDateString(this.campaignPlayer.players[i].warId.date);
const value = this.campaignPlayer.players[i][dataSet.key];
killObj.series.push({
name: warDateString,
value: value
});
total += value;
}
dataSet.data = [killObj];
dataSet.refLine = [{value: total / playerLength, name: this.avgLabel}]
dataSet.total = total;
})
}
navigateBack() {