import { DeckState } from '../../../models/decktracker/deck-state' ; import { GameState } from '../../../models/decktracker/game-state' ; 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 { EventParser } from './event-parser' ; export class DecklistUpdateParser implements EventParser { constructor ( private readonly aiDecks , private readonly handler , private readonly prefs ) { } applies ( gameEvent , state ) { return state && state . opponentDeck && gameEvent . type === GameEvent . DECKLIST_UPDATE ; } async parse ( currentState , gameEvent ) { const [ , controllerId , localPlayer ] = gameEvent . parse ( ) ; const isPlayer = controllerId === localPlayer . PlayerId ; if ( isPlayer ) { return currentState ; } const shouldLoadDecklist = ( await this . prefs . getPreferences ( ) ) . opponentLoadAiDecklist ; if ( ! shouldLoadDecklist ) { return currentState ; } const deckId = gameEvent . additionalData . deckId ; const aiDeck = shouldLoadDecklist ? this . aiDecks . getAiDeck ( gameEvent . opponentPlayer . CardID , currentState . metadata . scenarioId ) : null ; const newDeckstring = aiDeck && aiDeck . decks && aiDeck . decks [ deckId ] ; console . log ( '[decklist-update] loading AI deck' , gameEvent . opponentPlayer . CardID , currentState . metadata . scenarioId , deckId , newDeckstring ) ; if ( ! newDeckstring ) { console . log ( '[decklist-update] could not find new deck' ) ; return currentState ; } const decklist = await this . handler . postProcessDeck ( this . handler . buildDeckList ( newDeckstring ) ) ; const newPlayerDeck = currentState . opponentDeck . update ( { deckList : shouldLoadDecklist ? decklist : currentState . opponentDeck . deckList , deck : decklist , } as DeckState ) ; return currentState . update ( { opponentDeck : newPlayerDeck , } as GameState ) ; } event ( ) { return GameEvent . DECKLIST_UPDATE ; } }