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

82 lines
2.4 KiB
TypeScript
Raw Normal View History

import {Component, OnInit} from '@angular/core';
2018-03-07 11:56:50 +01:00
import {ActivatedRoute} from '@angular/router';
import {PlayerService} from '../../../services/logs/player.service';
2018-03-07 11:56:50 +01:00
import {FormControl} from '@angular/forms';
import {Observable} from 'rxjs/Observable';
import {Player} from '../../../models/model-interfaces';
2018-05-05 08:17:17 +02:00
import {PlayerUtils} from '../../../utils/player-utils';
2019-10-11 20:25:07 +02:00
import {FractionHelpers} from '../../../utils/global.helpers';
2017-12-23 10:59:27 +01:00
@Component({
selector: 'cc-stats-highscore',
2017-12-23 10:59:27 +01:00
templateUrl: './highscore.component.html',
styleUrls: ['./highscore.component.scss']
2017-12-23 10:59:27 +01:00
})
2018-03-08 09:44:35 +01:00
export class StatisticHighScoreComponent implements OnInit {
id = '';
2017-12-23 11:44:38 +01:00
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 = {};
playerAttributeDisplayNames = PlayerUtils.attributeDisplayNames.slice(2, PlayerUtils.attributeDisplayNames.length);
2019-10-11 20:25:07 +02:00
readonly fractionHelpers = FractionHelpers;
2017-12-23 11:44:38 +01:00
2017-12-23 10:59:27 +01:00
constructor(private route: ActivatedRoute,
private playerService: PlayerService) {
2017-12-23 10:59:27 +01:00
}
ngOnInit() {
this.playerAttributeDisplayNames.push({prop: 'warCount', head: 'stats.scoreboard.header.war.count'});
2017-12-23 10:59:27 +01:00
this.route.params
2018-02-26 09:04:27 +01:00
.map(params => params['id'])
.subscribe((id) => {
this.id = id;
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.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 = {};
for (let i = 0; i < this.playerAttributeDisplayNames.length; i++) {
const attributeProperty = this.playerAttributeDisplayNames[i].prop;
this.players[attributeProperty] = this.filterPlayerAttribute(attributeProperty);
}
}
}
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
}