import { Component , OnInit } from '@angular/core' ; import { Router } from '@angular/router' ; import { Store } from '@ngrx/store' ; import { Observable } from 'rxjs/Observable' ; import { PlayerBattleActions } from '../../../core/player/battle/player-battle.actions' ; import { PlayerTargetsActions } from '../../../core/player/targets/player-targets.actions' ; import { PlayerBattleService } from '../../../core/player/battle/player-battle.service' ; import { BattleDetail } from '../../../core/player/battle/battle-detail.model' ; import { AbstractContainerComponent } from '../../shared/components/abstract-container.component' ; import { UnitMap } from '../../shared/models/unit.model' ; @ Component ( { selector : 'e11-battle-history' , templateUrl : './history.component.html' , styleUrls : [ './history.component.scss' ] } ) export class BattleHistoryComponent extends AbstractContainerComponent implements OnInit { battleDetails : BattleDetail [ ] = [ ] ; unitsMap : UnitMap = { } ; battlesLoading : boolean = false ; attackCooldown : number = 0 ; expandedBattleId : string ; constructor ( public store , private router , private playerBattleService ) { super ( store ) ; this . addToSubscriptions ( this . store . select ( s => s . player . battle . status ) . skip ( 1 ) . distinctUntilChanged ( ( a , b ) => a . loading == b . loading ) . subscribe ( status => { this . battlesLoading = status . loading ; } ) , this . setBattles ( ) , this . setUnits ( ) , this . setExpandedBattle ( ) , Observable . combineLatest ( this . store . select ( s => s . player . battle . lastAttackBlock ) . distinctUntilChanged ( ) , this . store . select ( s => s . player . battle . attackCooldown ) . distinctUntilChanged ( ) , this . store . select ( s => s . web3 . lastBlock ) . distinctUntilChanged ( ) ) . subscribe ( data => { const lastAttackBlock = data [ 0 ] ; const attackCooldown = data [ 1 ] ; const lastBlock = data [ 2 ] ; this . attackCooldown = ( lastAttackBlock + attackCooldown > lastBlock ) ? ( lastAttackBlock + attackCooldown ) - lastBlock : 0 ; } ) ) ; } ngOnInit ( ) { if ( ! this . battleDetails || this . battleDetails . length === 0 ) { this . store . dispatch ( new PlayerBattleActions . ClearBattleHistory ( ) ) ; this . store . dispatch ( new PlayerBattleActions . GetBattleHistory ( ) ) ; } } battleDetailClicked ( battleDetail ) { if ( ! battleDetail . village . canAttack && ! battleDetail . village . canTakeRevenge ) return ; this . store . dispatch ( new PlayerTargetsActions . GetTarget ( battleDetail . village . address ) ) ; this . store . dispatch ( new PlayerTargetsActions . SelectTarget ( battleDetail . village . address ) ) ; this . router . navigate ( [ '/battle/target' ] ) ; } expandBattleDetail ( id ) { this . store . dispatch ( new PlayerBattleActions . SelectBattleDetail ( id ) ) ; } setExpandedBattle ( ) { return this . store . select ( s => s . player . battle . expandedBattleDetail ) . distinctUntilChanged ( ) . subscribe ( battleId => { this . expandedBattleId = battleId ; } ) ; } setBattles ( ) { return this . store . select ( s => s . player . battle . battles ) . distinctUntilChanged ( ) . subscribe ( battles => { this . battleDetails = battles ; } ) ; } setUnits ( ) { return this . store . select ( s => s . app . unitsMap ) . subscribe ( unitsMap => { this . unitsMap = unitsMap ; } ) ; } findMoreBattles ( ) { if ( ! this . battlesLoading ) { this . store . dispatch ( new PlayerBattleActions . GetBattleHistory ( ) ) ; } } }