From 216c19dccde2381541911d47739e1096dd0352da Mon Sep 17 00:00:00 2001 From: HardiReady Date: Fri, 27 Apr 2018 09:23:43 +0200 Subject: [PATCH] Simplify logic for player campaign detail page init --- .../campaign-player-detail.component.ts | 179 ++++-------------- 1 file changed, 37 insertions(+), 142 deletions(-) diff --git a/static/src/app/statistic/campaign-player-detail/campaign-player-detail.component.ts b/static/src/app/statistic/campaign-player-detail/campaign-player-detail.component.ts index 938579b..9990006 100644 --- a/static/src/app/statistic/campaign-player-detail/campaign-player-detail.component.ts +++ b/static/src/app/statistic/campaign-player-detail/campaign-player-detail.component.ts @@ -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() {