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 { GameEvent } from '../../../models/game-event' ; import { LocalizationFacadeService } from '../../localization-facade.service' ; import { DeckManipulationHelper } from './deck-manipulation-helper' ; import { EventParser } from './event-parser' ; export class CardChangedInDeckParser implements EventParser { constructor ( private readonly helper , private readonly allCards , private readonly i18n ) { } applies ( gameEvent , state ) { return state && gameEvent . type === GameEvent . CARD_CHANGED_IN_DECK ; } async parse ( currentState , gameEvent ) { const [ cardId , controllerId , localPlayer , entityId ] = gameEvent . parse ( ) ; const isPlayer = controllerId === localPlayer . PlayerId ; const deck = isPlayer ? currentState . playerDeck : currentState . opponentDeck ; const previousDeck = deck . deck ; const [ newDeck , theCard ] = this . helper . removeSingleCardFromZone ( previousDeck , cardId , entityId , deck . deckList . length === 0 , true ) ; if ( ! theCard ) { console . warn ( 'could not find card in deck' , cardId , entityId ) ; return currentState ; } const realCardId = ! ! cardId ?. length ? cardId : theCard . cardId ; const cardData = cardId != null ? this . allCards . getCard ( realCardId ) : null ; const newCard = theCard . update ( { cardId : realCardId , entityId : entityId ?? theCard . entityId , cardName : this . i18n . getCardName ( cardData . id ) , manaCost : cardData ? cardData . cost : undefined , rarity : cardData && cardData . rarity ? cardData . rarity . toLowerCase ( ) : undefined , creatorCardId : gameEvent . additionalData ?. creatorCardId , lastAffectedByCardId : gameEvent . additionalData . lastInfluencedByCardId , temporaryCard : false , } as DeckCard ) ; const deckWithNewCard = this . helper . addSingleCardToZone ( newDeck , newCard ) ; const newPlayerDeck = Object . assign ( new DeckState ( ) , deck , { deck : deckWithNewCard , } as DeckState ) ; return Object . assign ( new GameState ( ) , currentState , { [ isPlayer ? 'playerDeck' : 'opponentDeck' ] : newPlayerDeck , } ) ; } event ( ) { return GameEvent . CARD_CHANGED_IN_DECK ; } }