import { CardIds } from '@firestone-hs/reference-data' ; 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 CreateCardInDeckParser implements EventParser { constructor ( private readonly helper , private readonly allCards , private readonly i18n ) { } applies ( gameEvent , state ) { return state && gameEvent . type === GameEvent . CREATE_CARD_IN_DECK ; } async parse ( currentState , gameEvent ) { const [ cardId , controllerId , localPlayer , entityId ] = gameEvent . parse ( ) ; const isPlayer = controllerId === localPlayer . PlayerId ; if ( gameEvent . additionalData . creatorCardId === CardIds . CthunTheShattered ) { return currentState ; } const deck = isPlayer ? currentState . playerDeck : currentState . opponentDeck ; const cardData = cardId ?. length ? this . allCards . getCard ( cardId ) : null ; const card = DeckCard . create ( { cardId : cardId , entityId : entityId , cardName : this . buildCardName ( cardData , gameEvent . additionalData . creatorCardId ) , manaCost : cardData ? cardData . cost : undefined , rarity : cardData && cardData . rarity ? cardData . rarity . toLowerCase ( ) : undefined , creatorCardId : gameEvent . additionalData . creatorCardId , bonusDamage : gameEvent . additionalData . creatorEntityId ? buildBonusDamage ( deck . findCard ( gameEvent . additionalData . creatorEntityId ) ) : null , } as DeckCard ) ; const previousDeck = deck . deck ; const newDeck = this . helper . addSingleCardToZone ( previousDeck , card ) ; const newPlayerDeck = Object . assign ( new DeckState ( ) , deck , { deck : newDeck , } ) ; if ( ! card . cardId && ! card . entityId ) { console . warn ( 'Adding unidentified card in deck' , card , gameEvent ) ; } return Object . assign ( new GameState ( ) , currentState , { [ isPlayer ? 'playerDeck' : 'opponentDeck' ] : newPlayerDeck , } ) ; } event ( ) { return GameEvent . CREATE_CARD_IN_DECK ; } private buildCardName ( card , creatorCardId ) { if ( card ) { return this . i18n . getCardName ( card . id ) ; } if ( creatorCardId ) { return this . i18n . getCreatedByCardName ( creatorCardId ) ; } return this . i18n . getUnknownCardName ( ) ; } } const buildBonusDamage = ( card ) : number => { if ( card ?. cardId === CardIds . Ignite ) { return 1 + ( card . bonusDamage ?? 0 ) ; } return null ; } ;