import { CardsFacadeService } from '@services/cards-facade.service' ; import { DeckCard } from '../../../models/decktracker/deck-card' ; import { DeckState } from '../../../models/decktracker/deck-state' ; import { GameState } from '../../../models/decktracker/game-state' ; import { HeroCard } from '../../../models/decktracker/hero-card' ; import { GameEvent } from '../../../models/game-event' ; import { PreferencesService } from '../../preferences.service' ; import { AiDeckService } from '../ai-deck-service.service' ; import { DeckHandlerService } from '../deck-handler.service' ; import { DeckManipulationHelper } from './deck-manipulation-helper' ; import { EventParser } from './event-parser' ; export class OpponentPlayerParser implements EventParser { constructor ( private readonly aiDecks , private readonly handler , private readonly helper , private readonly allCards , private readonly prefs ) { } applies ( gameEvent , state ) { return state && state . opponentDeck && gameEvent . type === GameEvent . OPPONENT ; } async parse ( currentState , gameEvent ) { console . log ( 'will process OPPONENT event' ) ; const battleTag = gameEvent . opponentPlayer && gameEvent . opponentPlayer . Name ; const playerName = this . extractNameFromBTag ( battleTag ) || this . getNameFromCard ( gameEvent . opponentPlayer . CardID ) ; const playerClass = this . allCards . getCard ( gameEvent . opponentPlayer . CardID ) . playerClass ; const newHero = Object . assign ( new HeroCard ( ) , currentState . opponentDeck . hero , { playerName : playerName , playerClass : playerClass ? playerClass . toLowerCase ( ) : null , } as HeroCard ) ; const cardsInDeck = currentState . opponentDeck . hand . length + currentState . opponentDeck . deck . length ; const shouldLoadDecklist = ( await this . prefs . getPreferences ( ) ) . opponentLoadAiDecklist ; const aiDeck = this . aiDecks . getAiDeck ( gameEvent . opponentPlayer . CardID , currentState . metadata . scenarioId ) ; const aiDeckString = shouldLoadDecklist && aiDeck ? aiDeck . deckstring : null ; if ( ! aiDeckString ) { const newPlayerDeck = currentState . opponentDeck . update ( { hero : newHero , } as DeckState ) ; return currentState . update ( { opponentDeck : newPlayerDeck , } as GameState ) ; } console . log ( '[opponent-player] got AI deckstring' , aiDeckString , currentState . metadata ) ; const decklist = await this . handler . postProcessDeck ( this . handler . buildDeckList ( aiDeckString ) ) ; let newDeck = decklist ; if ( aiDeckString ) { for ( const card of [ ... currentState . opponentDeck . hand , ... currentState . opponentDeck . otherZone , ... currentState . opponentDeck . board , ] ) { newDeck = this . helper . removeSingleCardFromZone ( newDeck , card . cardId , card . entityId ) [ 0 ] ; } } const hand = aiDeckString ? this . flagCards ( currentState . opponentDeck . hand ) : currentState . opponentDeck . hand ; const deck = aiDeckString ? this . flagCards ( newDeck ) : newDeck ; const newPlayerDeck = currentState . opponentDeck . update ( { hero : newHero , deckstring : aiDeckString , deckList : decklist , deck : deck , hand : hand , otherZone : aiDeckString ? this . flagCards ( currentState . opponentDeck . otherZone ) : currentState . opponentDeck . otherZone , showDecklistWarning : cardsInDeck < decklist . length , } as DeckState ) ; return currentState . update ( { opponentDeck : newPlayerDeck , } as GameState ) ; } event ( ) { return GameEvent . OPPONENT ; } private extractNameFromBTag ( battleTag ) { return battleTag && battleTag . indexOf ( '#' ) !== - 1 ? battleTag . split ( '#' ) [ 0 ] : battleTag ; } private getNameFromCard ( cardId ) { const card = cardId ? this . allCards . getCard ( cardId ) : null ; return card ? card . name : 'Unnamed player' ; } private flagCards ( cards ) { return cards . map ( ( card ) => card . update ( { inInitialDeck : true , } as DeckCard ) ) ; } }