import { Injectable } from '@angular/core' ; import { TranslateService } from '@ngx-translate/core' ; import * as async from 'async' ; import { EventEmitter } from 'eventemitter3' ; import { GameService } from "@services/game.service" ; import { TabService } from "@services/tab.service" ; import { PromptService } from "@services/prompt.service" ; import { ApplicationService } from "@services/electron/application.service" ; import { CryptService } from "@services/crypt.service" ; import { Tab } from "@helpers/tab" ; import { Game } from "@helpers/game" ; @ Injectable ( ) export class TabGameService extends EventEmitter { private lastTabGameIndex : number = 1 ; constructor ( private gameService , private tabService , private promptService , private translate , private applicationService , private crypt ) { super ( ) ; } param credentials param cb public addTabGame ( credentials = undefined , cb = undefined ) { const add = ( ) => { const tab = new Tab ( this . lastTabGameIndex ++ ) ; this . tabService . addTab ( tab ) ; const game = new Game ( tab . id , credentials ) ; game . on ( 'character' , ( name ) => { tab . character = name ; } ) ; game . on ( 'icon' , ( icon ) => { tab . icon = icon ; this . emit ( 'icon-change' , tab ) ; } ) ; game . on ( 'logged' , ( logged ) => { tab . isLogged = logged ; } ) ; game . on ( 'notification' , ( ) => { if ( this . tabService . active . id !== tab . id ) { tab . notification = true ; } } ) ; this . gameService . addGame ( game ) ; this . selectTabGame ( tab . id ) ; if ( cb ) cb ( ) ; } ; if ( this . gameService . games . length > 4 ) { void this . promptService . confirm ( { title : this . translate . instant ( "app.prompt.title.confirm" ) , html : this . translate . instant ( "app.main.prompt.tabs-overflow.text" ) , icon : "warning" } ) . then ( ( result ) => { if ( result . isConfirmed ) add ( ) ; } ) ; } else { add ( ) ; } } param id public selectTabGame ( id ) { if ( this . tabService . active !== null ) { this . tabService . active . isFocus = false ; this . gameService . getGame ( this . tabService . active . id ) . isFocus = false ; } const tab = this . tabService . getTab ( id ) ; const game = this . gameService . getGame ( id ) ; tab . isFocus = true ; tab . notification = false ; game . isFocus = true ; if ( tab . isLogged ) { game . window . focus ( ) ; } this . tabService . active = tab ; } param id public removeTabGame ( id ) { const tab = this . tabService . getTab ( id ) ; const game = this . gameService . getGame ( id ) ; if ( this . tabService . active !== null && this . tabService . active . id === id ) { this . tabService . active = null ; } const currentTabIndex = this . tabService . tabs . findIndex ( ( element ) => { return element . id == tab . id ; } ) ; this . tabService . removeTab ( tab ) ; this . gameService . removeGame ( game ) ; if ( typeof ( this . tabService . tabs [ currentTabIndex ] ) != "undefined" ) { this . selectTabGame ( this . tabService . tabs [ currentTabIndex ] . id ) ; } else { const lastGame = this . tabService . tabs . slice ( - 1 ) . pop ( ) ; if ( ( lastGame !== undefined ) ) { this . selectTabGame ( lastGame . id ) ; } } } addMultiAccountGames ( accounts ? ) { if ( ! accounts || accounts . length == 0 ) { this . addTabGame ( ) ; return ; } async . eachSeries ( accounts , ( account , cb ) => { const credentials = { account_name : this . crypt . decrypt ( account . account_name_encrypted , Application . masterPassword ) , password : this . crypt . decrypt ( account . password_encrypted , Application . masterPassword ) , } ; this . addTabGame ( credentials , ( ) => { setTimeout ( cb , 1500 ) ; } ) ; } , ( ) => { this . selectTabGame ( this . lastTabGameIndex - 1 ) ; } ) ; } }