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

158 lines
4.3 KiB
TypeScript
Raw Normal View History

2017-07-29 12:59:51 +02:00
import {Component} from "@angular/core";
2017-08-12 21:37:31 +02:00
import {ActivatedRoute} from "@angular/router";
2017-09-02 13:08:54 +02:00
import {CarouselConfig} from "ngx-bootstrap";
2017-10-22 18:06:37 +02:00
import {CampaignService} from "../../services/logs/campaign.service";
2017-10-02 14:41:17 +02:00
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-09-02 13:08:54 +02:00
inputs: ['campaigns'],
providers: [{provide: CarouselConfig, useValue: {interval: false}}]
2017-07-29 12:59:51 +02:00
})
export class StatisticOverviewComponent {
2017-07-29 12:59:51 +02:00
2017-09-02 13:08:54 +02:00
id = "";
2017-08-12 23:13:39 +02:00
title = "";
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;
2017-10-06 20:52:52 +02: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
.map(params => params['id'])
.subscribe((id) => {
2017-09-02 13:08:54 +02:00
this.id = id;
2017-09-12 15:02:35 +02:00
if (this.campaignService.campaigns) {
this.initWars(this.campaignService.campaigns);
2017-08-12 21:37:31 +02:00
} else {
2017-09-12 15:02:35 +02:00
this.campaignService.getAllCampaigns().subscribe(campaigns => {
2017-09-02 13:08:54 +02:00
this.initWars(campaigns);
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.title = "Gesamtübersicht";
wars.sort((a, b) => {
// sort by dates, because older campaign can contain newer war
if (a['date'] > (b['date'])) return -1;
if (a['date'] < (b['date'])) return 1;
return 0;
})
} else {
this.title = campaign.title;
}
2017-08-12 22:27:23 +02:00
this.initChart(wars);
}
})
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;
this.yAxisLabel = "Gesamtpunkte";
break;
case 1:
this.currentData = this.pointData;
this.yAxisLabel = "Punkte";
break;
case 2:
this.currentData = this.playerData;
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;
2017-10-02 14:41:17 +02:00
const warDateString = 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({
2017-07-29 18:00:21 +02:00
name: warDateString,
value: wars[i].ptBlufor
2017-09-02 10:43:23 +02:00
});
pointsObj[1].series.push({
2017-07-29 18:00:21 +02:00
name: warDateString,
value: wars[i].ptOpfor
2017-09-02 10:43:23 +02:00
});
pointsSumObj[0].series.push({
name: warDateString,
value: pointsSumObj[0].series[j - 1] ?
pointsSumObj[0].series[j - 1].value + wars[i].ptBlufor :
wars[i].ptBlufor
});
pointsSumObj[1].series.push({
name: warDateString,
value: pointsSumObj[1].series[j - 1]
? pointsSumObj[1].series[j - 1].value + wars[i].ptOpfor :
wars[i].ptOpfor
});
playersObj[0].series.push({
2017-07-29 18:00:21 +02:00
name: warDateString,
2017-07-30 08:55:14 +02:00
value: wars[i].playersBlufor
2017-09-02 10:43:23 +02:00
});
playersObj[1].series.push({
2017-07-29 18:00:21 +02:00
name: warDateString,
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;
2017-10-06 20:52:52 +02:00
if (this.id != 'all') {
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) {
return this.activeSlideIndex === index ? '#d9edf7' : 'white'
2017-10-07 08:46:34 +02:00
}
2017-07-29 12:59:51 +02:00
}