opt-cc/static/src/app/statistic/campaign/overview/stats-overview.component.ts

161 lines
4.4 KiB
TypeScript
Raw Normal View History

2018-03-08 09:44:35 +01:00
import {Component, Input, OnInit} from '@angular/core';
2018-03-07 11:56:50 +01:00
import {ActivatedRoute} from '@angular/router';
import {CampaignService} from '../../../services/logs/campaign.service';
import {ChartUtils} from '../../../utils/chart-utils';
import {Fraction} from '../../../utils/fraction.enum';
2017-07-29 12:59:51 +02:00
@Component({
selector: 'stats-overview',
templateUrl: './stats-overview.component.html',
styleUrls: ['./stats-overview.component.css', '../../../style/list-entry.css', '../../../style/overview.css']
2017-07-29 12:59:51 +02:00
})
2018-03-08 09:44:35 +01:00
export class StatisticOverviewComponent implements OnInit {
2017-07-29 12:59:51 +02:00
2018-03-07 11:56:50 +01:00
@Input() campaigns;
id = '';
title = '';
2017-08-12 23:13:39 +02:00
2017-07-30 08:55:14 +02:00
pointData: any[] = [];
2017-09-02 10:43:23 +02:00
pointSumData: any[] = [];
2017-07-30 08:55:14 +02:00
playerData: any[] = [];
currentData: any[] = [];
2017-10-06 20:52:52 +02:00
activeSlideIndex;
2017-07-29 12:59:51 +02:00
colorScheme = {
domain: [Fraction.COLOR_BLUFOR, Fraction.COLOR_OPFOR]
2017-07-29 12:59:51 +02:00
};
2017-09-24 12:04:48 +02:00
gradient = false;
xAxis = true;
yAxis = true;
roundDomains = true;
legend = true;
legendTitle = '';
showXAxisLabel = true;
showYAxisLabel = true;
2018-03-07 11:56:50 +01:00
yAxisLabel = '';
2017-09-24 12:04:48 +02:00
autoscale = true;
timeline = false;
2017-07-29 12:59:51 +02:00
2017-08-12 21:37:31 +02:00
constructor(private route: ActivatedRoute,
2017-09-12 15:02:35 +02:00
private campaignService: CampaignService) {
2017-07-29 12:59:51 +02:00
}
ngOnInit() {
2017-08-12 21:37:31 +02:00
this.route.params
2018-02-26 09:04:27 +01:00
.map(params => params['id'])
.subscribe((id) => {
this.id = id;
if (this.campaignService.campaigns) {
this.initWars(this.campaignService.campaigns);
} else {
this.campaignService.getAllCampaignsWithWars().subscribe(campaigns => {
2018-02-26 09:04:27 +01:00
this.initWars(campaigns);
2018-03-07 11:56:50 +01:00
});
2018-02-26 09:04:27 +01:00
}
});
2017-08-12 21:37:31 +02:00
}
2017-09-02 13:08:54 +02:00
initWars(campaigns) {
2017-08-12 21:37:31 +02:00
let wars = [];
let itemsProcessed = 0;
2017-09-02 13:08:54 +02:00
campaigns = campaigns.filter(campaign => this.id === 'all' || campaign._id === this.id);
2017-08-12 22:27:23 +02:00
campaigns.forEach(campaign => {
wars = wars.concat(campaign.wars);
itemsProcessed++;
if (itemsProcessed === campaigns.length) {
if (this.id === 'all') {
this.timeline = true;
2018-03-07 11:56:50 +01:00
this.title = 'Gesamtübersicht';
wars.sort((a, b) => {
// sort by dates, because older campaign can contain newer war
2018-03-07 11:56:50 +01:00
if (a['date'] > (b['date'])) {
return -1;
}
if (a['date'] < (b['date'])) {
return 1;
}
return 0;
2018-03-07 11:56:50 +01:00
});
} else {
this.title = campaign.title;
}
2017-08-12 22:27:23 +02:00
this.initChart(wars);
}
2018-03-07 11:56:50 +01:00
});
2017-07-29 18:00:21 +02:00
}
2017-09-02 13:08:54 +02:00
goToSlide(index: number) {
2017-10-06 20:52:52 +02:00
switch (index) {
case 0:
this.currentData = this.pointSumData;
2018-03-07 11:56:50 +01:00
this.yAxisLabel = 'Gesamtpunkte';
break;
case 1:
this.currentData = this.pointData;
2018-03-07 11:56:50 +01:00
this.yAxisLabel = 'Punkte';
break;
case 2:
this.currentData = this.playerData;
2018-03-07 11:56:50 +01:00
this.yAxisLabel = 'Anzahl Spieler';
break;
2017-10-06 20:52:52 +02:00
}
2017-09-02 13:08:54 +02:00
this.activeSlideIndex = index;
}
2017-07-29 18:00:21 +02:00
initChart(wars: any[]) {
const pointsObj = ChartUtils.getMultiDataArray(Fraction.BLUFOR, Fraction.OPFOR);
const pointsSumObj = ChartUtils.getMultiDataArray(Fraction.BLUFOR, Fraction.OPFOR);
const playersObj = ChartUtils.getMultiDataArray(Fraction.BLUFOR, Fraction.OPFOR);
2017-09-24 12:38:57 +02:00
2017-07-29 18:00:21 +02:00
for (let i = wars.length - 1; i >= 0; i--) {
const j = wars.length - i - 1;
const warDate = (this.id === 'all') ? new Date(wars[i].date) : ChartUtils.getShortDateString(wars[i].date);
2017-07-30 08:55:14 +02:00
2017-09-02 10:43:23 +02:00
pointsObj[0].series.push({
name: warDate,
2017-07-29 18:00:21 +02:00
value: wars[i].ptBlufor
2017-09-02 10:43:23 +02:00
});
pointsObj[1].series.push({
name: warDate,
2017-07-29 18:00:21 +02:00
value: wars[i].ptOpfor
2017-09-02 10:43:23 +02:00
});
pointsSumObj[0].series.push({
name: warDate,
2017-09-02 10:43:23 +02:00
value: pointsSumObj[0].series[j - 1] ?
pointsSumObj[0].series[j - 1].value + wars[i].ptBlufor :
wars[i].ptBlufor
});
pointsSumObj[1].series.push({
name: warDate,
2017-09-02 10:43:23 +02:00
value: pointsSumObj[1].series[j - 1]
? pointsSumObj[1].series[j - 1].value + wars[i].ptOpfor :
wars[i].ptOpfor
});
playersObj[0].series.push({
name: warDate,
2017-07-30 08:55:14 +02:00
value: wars[i].playersBlufor
2017-09-02 10:43:23 +02:00
});
playersObj[1].series.push({
name: warDate,
2017-07-30 08:55:14 +02:00
value: wars[i].playersOpfor
2017-09-02 10:43:23 +02:00
});
2017-07-29 18:00:21 +02:00
}
2017-07-30 08:55:14 +02:00
this.pointData = pointsObj;
2017-09-02 10:43:23 +02:00
this.pointSumData = pointsSumObj;
2017-07-30 08:55:14 +02:00
this.playerData = playersObj;
2018-03-07 11:56:50 +01:00
if (this.id !== 'all') {
2017-10-06 20:52:52 +02:00
this.goToSlide(0);
} else {
this.goToSlide(1);
}
Object.assign(this, this.currentData);
2017-07-29 18:00:21 +02:00
}
2017-07-29 12:59:51 +02:00
2017-10-07 08:46:34 +02:00
isActiveSlide(index) {
2018-03-07 11:56:50 +01:00
return this.activeSlideIndex === index ? '#d9edf7' : 'white';
2017-10-07 08:46:34 +02:00
}
2017-07-29 12:59:51 +02:00
}