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

110 lines
2.7 KiB
TypeScript

import {Component} from "@angular/core";
import {WarService} from "../../services/war-service/war.service";
import {Campaign, War} from "../../models/model-interfaces";
import {WarListComponent} from "../war-list/war-list.component";
import {ActivatedRoute} from "@angular/router";
@Component({
selector: 'stats-overview',
templateUrl: './stats-overview.component.html',
styleUrls: ['./stats-overview.component.css'],
inputs: ['campaigns']
})
export class StatisticOverviewComponent {
pointData: any[] = [];
playerData: any[] = [];
colorScheme = {
domain: ['#0000FF', '#B22222']
};
constructor(private route: ActivatedRoute,
private warService: WarService) {
}
ngOnInit() {
this.route.params
.map(params => params['id'])
.subscribe((id) => {
if (this.warService.campaigns) {
this.initWars(this.warService.campaigns, id);
} else {
this.warService.getAllCampaigns().subscribe(campaigns => {
this.initWars(campaigns, id);
})
}
});
}
initWars(campaigns, id) {
let wars = [];
let itemsProcessed = 0;
campaigns = campaigns.filter(campaign => id === 'all' || campaign._id === id);
campaigns.forEach(campaign => {
wars = wars.concat(campaign.wars);
itemsProcessed++;
if (itemsProcessed === campaigns.length) {
this.initChart(wars);
}
})
}
initChart(wars: any[]) {
let pointsObj = [
{
"name": "NATO",
"series": []
},
{
"name": "CSAT",
"series": []
}];
let playersObj = [
{
"name": "NATO",
"series": []
},
{
"name": "CSAT",
"series": []
}
];
for (let i = wars.length - 1; i >= 0; i--) {
const isoDate = wars[i].date.slice(0, 10);
const dayDate = parseInt(isoDate.slice(8, 10)) + 1;
const warDateString = (dayDate < 10 ? "0" + dayDate : dayDate) + '.'
+ isoDate.slice(5, 7) + '.' + isoDate.slice(0, 4);
const bluforData = {
name: warDateString,
value: wars[i].ptBlufor
};
const opforData = {
name: warDateString,
value: wars[i].ptOpfor
};
const bluforPlayers = {
name: warDateString,
value: wars[i].playersBlufor
};
const opforPlayers = {
name: warDateString,
value: wars[i].playersOpfor
};
playersObj[0].series.push(bluforData);
playersObj[1].series.push(opforData);
pointsObj[0].series.push(bluforPlayers);
pointsObj[1].series.push(opforPlayers);
}
this.pointData = pointsObj;
this.playerData = playersObj;
Object.assign(this, [this.playerData, this.pointData])
}
}