import { Component , OnInit } from '@angular/core' ; import { HttpClient } from '@angular/common/http' ; import { sortBy } from 'lodash' ; import { GameService } from '../game.service' ; import { map } from 'rxjs/operators' ; import { IFestival } from '../../../shared/interfaces' ; @ Component ( { selector : 'app-festivals' , templateUrl : './festivals.page.html' , styleUrls : [ './festivals.page.scss' ] , } ) export class FestivalsPage implements OnInit { public isLoaded : boolean ; public festivals : IFestival [ ] = [ ] ; public aggregate : any ; constructor ( private http , private gameService ) { } ngOnInit ( ) { this . http . get ( `${ this . gameService . apiUrl }/festivals` ) . pipe ( map ( ( x ) => x . festivals ) ) . subscribe ( festivals => { this . festivals = sortBy ( festivals . filter ( ( x ) => this . isFestivalValid ( x ) ) , fest => ! fest . startedBy . includes ( this . gameService . playerRef . name ) ) ; this . aggregate = this . festivalAggregate ( this . festivals ) ; this . isLoaded = true ; } ) ; } public festivalAggregate ( festivals ) { const aggStats = { stats : { } } ; for ( const o of festivals ) { for ( const [ k , v ] of Object . entries ( o . stats ) ) { if ( ! aggStats . stats [ k ] ) { aggStats . stats [ k ] = v ; } else { aggStats . stats [ k ] += v ; } } } return aggStats ; } public isFestivalValid ( festival ) { return festival . endTime > Date . now ( ) ; } }