import { DeckCard } from '../../../models/decktracker/deck-card' ; import { DeckState } from '../../../models/decktracker/deck-state' ; import { GameState } from '../../../models/decktracker/game-state' ; import { GameEvent } from '../../../models/game-event' ; import { DeckHandlerService } from '../deck-handler.service' ; import { DeckstringOverrideEvent } from '../event/deckstring-override-event' ; import { EventParser } from './event-parser' ; export class DeckstringOverrideParser implements EventParser { constructor ( private readonly deckHandler ) { } applies ( gameEvent , state ) { return state && gameEvent . type === GameEvent . DECKSTRING_OVERRIDE ; } async parse ( currentState , gameEvent , additionalInfo ? , playerOrOpponent = 'opponent' ) { const deckName = gameEvent . deckName ; const deckstring = gameEvent . deckstring ; const initialDeck = playerOrOpponent === 'opponent' ? currentState . opponentDeck : currentState . playerDeck ; if ( ! deckstring ) { console . warn ( '[deckstring-override-parser] no deckstring passed, returning' , gameEvent ) ; return currentState ; } console . log ( '[deckstring-override-parser] overriding with deckstring' , playerOrOpponent , deckstring ) ; const deckInfo = initialDeck . deck ; const cardsNotInInitialDeck = deckInfo . filter ( ( card ) => card . creatorCardId ) ; const cardsMovedOutOfInitialDeck = [ ... initialDeck . board , ... initialDeck . hand , ... initialDeck . otherZone ] . filter ( ( card ) => card . cardId ) . filter ( ( card ) => ! card . creatorCardId ) ; const cardsFromDeckstring = this . deckHandler . buildDeckList ( deckstring ) ; const newDeckContents = [ ... cardsFromDeckstring ] ; for ( const movedOut of cardsMovedOutOfInitialDeck ) { const index = newDeckContents . map ( ( card ) => card . cardId ) . indexOf ( movedOut . cardId ) ; if ( index !== - 1 ) { newDeckContents . splice ( index , 1 ) ; } } const finalDeckContents = [ ... newDeckContents , ... cardsNotInInitialDeck ] ; const newDeck = Object . assign ( new DeckState ( ) , initialDeck , { deckstring : deckstring , name : deckName , deckList : cardsFromDeckstring , deck : finalDeckContents as readonly DeckCard [ ] , } as DeckState ) ; return Object . assign ( new GameState ( ) , currentState , { [ playerOrOpponent === 'opponent' ? 'opponentDeck' : 'playerDeck' ] : newDeck , } ) ; } event ( ) { return GameEvent . DECKSTRING_OVERRIDE ; } }