import { Component , OnInit , OnDestroy } from '@angular/core' ; import { ModalController } from '@ionic/angular' ; import { GameService } from '../game.service' ; import { SocketClusterService } from '../socket-cluster.service' ; import { AdventureNames , AdventureDescriptions , IAdventure , ServerEventName , IPet } from '../../../shared/interfaces' ; import { ChoosePetsModal } from './choosepets.modal' ; @ Component ( { selector : 'app-adventures' , templateUrl : './adventures.page.html' , styleUrls : [ './adventures.page.scss' ] } ) export class AdventuresPage implements OnInit , OnDestroy { public get adventureNames ( ) { return AdventureNames ; } public get adventureDescs ( ) { return AdventureDescriptions ; } private rewardsCb : Function ; constructor ( private modalCtrl , private socketService , public gameService ) { } ngOnInit ( ) { this . rewardsCb = ( { rewards , adventure } ) => this . gameService . showRewards ( this . adventureNames [ adventure . type ] , rewards ) ; this . socketService . register ( ServerEventName . PetAdventureRewards , this . rewardsCb ) ; } ngOnDestroy ( ) { this . socketService . unregister ( ServerEventName . PetAdventureRewards , this . rewardsCb ) ; } public isAdventureDone ( adventure ) { return adventure . finishAt < Date . now ( ) ; } public petsOnAdventure ( adventure ) { const player = this . gameService . playerRef ; if ( ! player ) return '' ; const pets = player . $petsData . allPets ; return Object . values ( pets ) . filter ( ( x ) => x . currentAdventureId === adventure . id ) . map ( ( x ) => `${ x . name } (${ x . typeName })` ) . join ( ', ' ) ; } public async embark ( adventure ) { const modal = await this . modalCtrl . create ( { component : ChoosePetsModal } ) ; await modal . present ( ) ; const res = await modal . onDidDismiss ( ) ; if ( res . data && res . data . length > 0 ) { this . socketService . emit ( ServerEventName . PetAdventureEmbark , { adventureId : adventure . id , petIds : res . data } ) ; } } public collect ( adventure ) { this . socketService . emit ( ServerEventName . PetAdventureFinish , { adventureId : adventure . id } ) ; } }