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 { COUNTERSPELLS , globalEffectCards } from '../../hs-utils' ; import { LocalizationFacadeService } from '../../localization-facade.service' ; import { modifyDeckForSpecialCards } from './deck-contents-utils' ; import { DeckManipulationHelper } from './deck-manipulation-helper' ; import { EventParser } from './event-parser' ; export class CardPlayedByEffectParser implements EventParser { constructor ( private readonly helper , private readonly allCards , private readonly i18n ) { } applies ( gameEvent , state ) { return state && gameEvent . type === GameEvent . CARD_PLAYED_BY_EFFECT ; } async parse ( currentState , gameEvent , additionalInfo ? ) { const [ cardId , controllerId , localPlayer , entityId ] = gameEvent . parse ( ) ; const isPlayer = controllerId === localPlayer . PlayerId ; const deck = isPlayer ? currentState . playerDeck : currentState . opponentDeck ; const refCard = this . allCards . getCard ( cardId ) ; const isOnBoard = refCard && refCard . type === 'Minion' ; const cardWithZone = DeckCard . create ( { entityId : entityId , cardId : cardId , cardName : this . i18n . getCardName ( refCard ?. id ) , manaCost : refCard ?. cost , rarity : refCard ?. rarity ?. toLowerCase ( ) , zone : isOnBoard ? 'PLAY' : null , temporaryCard : false , } as DeckCard ) ; const newBoard = isOnBoard ? this . helper . addSingleCardToZone ( deck . board , cardWithZone ) : deck . board ; const newOtherZone = isOnBoard ? deck . otherZone : this . helper . addSingleCardToZone ( deck . otherZone , cardWithZone ) ; const isCardCountered = ( ( additionalInfo ?. secretWillTrigger ?. reactingToEntityId && additionalInfo ?. secretWillTrigger ?. reactingToEntityId === entityId ) || ( additionalInfo ?. secretWillTrigger ?. reactingToCardId && additionalInfo ?. secretWillTrigger ?. reactingToCardId === cardId ) ) && COUNTERSPELLS . includes ( additionalInfo ?. secretWillTrigger ?. cardId as CardIds ) ; let newGlobalEffects = deck . globalEffects ; if ( ! isCardCountered && globalEffectCards . includes ( cardId as CardIds ) ) { newGlobalEffects = this . helper . addSingleCardToZone ( deck . globalEffects , cardWithZone ?. update ( { entityId : null , } as DeckCard ) ) ; } const newPlayerDeck = Object . assign ( new DeckState ( ) , deck , { board : newBoard , otherZone : newOtherZone , globalEffects : newGlobalEffects , } as DeckState ) ; const deckAfterSpecialCaseUpdate = isCardCountered ? newPlayerDeck : modifyDeckForSpecialCards ( cardId , newPlayerDeck , this . allCards , this . i18n ) ; return Object . assign ( new GameState ( ) , currentState , { [ isPlayer ? 'playerDeck' : 'opponentDeck' ] : deckAfterSpecialCaseUpdate , } ) ; } event ( ) { return GameEvent . CARD_PLAYED_BY_EFFECT ; } }