opt-cc/static/src/app/statistic/highscore/highscore.component.ts

113 lines
3.1 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 {PlayerService} from '../../services/logs/player.service';
import {CampaignService} from '../../services/logs/campaign.service';
import {Fraction} from '../../utils/fraction.enum';
import {FormControl} from '@angular/forms';
import {Observable} from 'rxjs/Observable';
import {Player} from '../../models/model-interfaces';
2017-12-23 10:59:27 +01:00
@Component({
selector: 'stats-highscore',
templateUrl: './highscore.component.html',
2018-03-08 09:44:35 +01:00
styleUrls: ['./highscore.component.css', '../../style/list-entry.css', '../../style/overview.css']
2017-12-23 10:59:27 +01:00
})
2018-03-08 09:44:35 +01:00
export class StatisticHighScoreComponent implements OnInit {
@Input() campaigns;
2017-12-23 10:59:27 +01:00
id = '';
2017-12-23 11:44:38 +01:00
title = '';
searchTerm = new FormControl();
2017-12-23 10:59:27 +01:00
2018-02-26 09:04:27 +01:00
players: Player = {};
2017-12-23 11:44:38 +01:00
playersStored = {};
2017-12-23 11:44:38 +01:00
cellHeight = 40;
numberColWidth = 60;
nameColWidth = 210;
valueColWidth = 110;
emptyMessage = {emptyMessage: 'Keine Einträge'};
2017-12-23 11:44:38 +01:00
customClasses = {
sortAscending: 'glyphicon glyphicon-triangle-top',
sortDescending: 'glyphicon glyphicon-triangle-bottom',
};
readonly fraction = Fraction;
2017-12-23 10:59:27 +01:00
constructor(private route: ActivatedRoute,
2017-12-23 11:19:08 +01:00
private playerService: PlayerService,
2017-12-23 10:59:27 +01:00
private campaignService: CampaignService) {
}
ngOnInit() {
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.initData();
} else {
this.campaignService.getAllCampaigns().subscribe(campaigns => {
2018-03-07 11:56:50 +01:00
this.initData();
});
2018-02-26 09:04:27 +01:00
}
});
2017-12-27 12:21:41 +01:00
const searchTermStream = this.searchTerm.valueChanges.debounceTime(400);
Observable.merge(searchTermStream)
2018-02-26 09:04:27 +01:00
.distinctUntilChanged().map(query => this.filterPlayers())
.subscribe();
2017-12-23 10:59:27 +01:00
}
2017-12-23 11:19:08 +01:00
initData() {
this.title = this.campaignService.campaigns
2018-02-26 09:04:27 +01:00
.filter(camp => camp._id === this.id).pop().title;
2017-12-23 10:59:27 +01:00
2017-12-23 11:19:08 +01:00
this.playerService.getCampaignHighscore(this.id).subscribe(players => {
2017-12-23 11:44:38 +01:00
this.players = players;
this.playersStored = players;
2018-03-07 11:56:50 +01:00
});
2017-12-23 10:59:27 +01:00
}
filterPlayers() {
if (!this.searchTerm.value || this.searchTerm.value === '') {
this.players = this.playersStored;
} else {
this.players = {
kill: this.filterPlayerAttribute('kill'),
friendlyFire: this.filterPlayerAttribute('friendlyFire'),
vehicle: this.filterPlayerAttribute('vehicle'),
death: this.filterPlayerAttribute('death'),
respawn: this.filterPlayerAttribute('respawn'),
revive: this.filterPlayerAttribute('revive'),
flagTouch: this.filterPlayerAttribute('flagTouch')
2018-03-07 11:56:50 +01:00
};
}
}
private filterPlayerAttribute(attribute) {
2017-12-27 12:50:22 +01:00
const query = this.searchTerm.value.toLowerCase().split('&');
return this.playersStored[attribute].filter(player => {
for (let i = 0; i < query.length; i++) {
2018-03-07 11:56:50 +01:00
if (query[i].trim() !== '' && player.name.toLowerCase().includes(query[i].trim())) {
2017-12-27 12:50:22 +01:00
return true;
}
}
return false;
2018-03-07 11:56:50 +01:00
});
}
2017-12-23 10:59:27 +01:00
}