import {Component, ElementRef, EventEmitter, Input, OnChanges, Output, SimpleChanges} from '@angular/core'; import {War} from '../../../models/model-interfaces'; import {Fraction} from '../../../utils/fraction.enum'; import {PlayerUtils} from '../../../utils/player-utils'; import {saveAs} from 'file-saver/FileSaver'; import {MatSort} from '@angular/material'; import {SortUtils} from '../../../utils/sort-utils'; @Component({ selector: 'cc-scoreboard', templateUrl: './scoreboard.component.html', styleUrls: ['./scoreboard.component.css', '../../../style/list-entry.css', '../../../style/hide-scrollbar.css'] }) export class ScoreboardComponent implements OnChanges { readonly fraction = Fraction; @Input() war: War; @Input() fractionFilterSelected: string; @Output() playerTabSwitch = new EventEmitter(); tableHead = PlayerUtils.attributeDisplayNames; isSteamUUID = PlayerUtils.isSteamUUID; cellHeight = 40; columnWidth = 65; rows = []; sortedRows = []; currentSort = new MatSort(); displayedColumns = this.tableHead.map(head => head.prop); constructor(private elRef: ElementRef) { this.displayedColumns.push('interact'); } selectPlayerDetail(view: number, player) { this.playerTabSwitch.emit({ view: view, player: player }); } ngOnChanges(changes: SimpleChanges) { if (changes.war) { this.rows = changes.war.currentValue.players; this.currentSort.active = 'kill'; this.sortScoreboardData(this.currentSort); // this.elRef.nativeElement // .querySelector('.datatable-body') // .scrollTo(0, 0); } if (changes.fractionFilterSelected) { this.filterPlayersByFraction(this.fractionFilterSelected); } } filterPlayersByFraction(fraction?: string) { if (fraction) { this.rows = this.war.players.filter((player) => { return player.fraction === fraction; }); } else { this.rows = this.war.players; } this.sortScoreboardData(this.currentSort); } sortScoreboardData(sort: MatSort) { if (sort) { this.currentSort = sort; } const data = this.rows.slice(); if (!sort.active || sort.direction === '') { this.sortedRows = data; return; } this.sortedRows = data.sort((a, b) => { const isAsc = sort.direction === 'desc'; const sortProperty = sort.active; return SortUtils.compare(a[sortProperty], b[sortProperty], isAsc); }); } exportCSV() { let csvOut = ''; for (let i = 0; i < this.tableHead.length; i++) { csvOut += this.tableHead[i].head; if (i !== this.tableHead.length - 1) { csvOut += ','; } } for (let j = 0; j < this.war.players.length; j++) { const player = this.war.players[j]; csvOut += '\r\n'; csvOut += '"' + player.name + '",'; csvOut += player.fraction + ','; csvOut += player.kill + ','; csvOut += player.friendlyFire + ','; csvOut += player.revive + ','; csvOut += player.flagTouch + ','; csvOut += player.vehicleLight + ','; csvOut += player.vehicleHeavy + ','; csvOut += player.vehicleAir + ','; csvOut += player.death + ','; csvOut += player.respawn; } const blob = new Blob([csvOut], {type: 'text/plain'}); saveAs(blob, this.war.title.toLowerCase().replace(' ', '_').concat('.csv')); } }