import { ReferenceCard } 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 CardChangedOnBoardParser implements EventParser { constructor ( private readonly helper , private readonly allCards , private readonly i18n ) { } applies ( gameEvent , state ) { return state && gameEvent . type === GameEvent . CARD_CHANGED_ON_BOARD ; } async parse ( currentState , gameEvent ) { const [ cardId , controllerId , localPlayer , entityId ] = gameEvent . parse ( ) ; const creatorCardId = gameEvent . additionalData ? gameEvent . additionalData . creatorCardId : null ; const isPlayer = controllerId === localPlayer . PlayerId ; const deck = isPlayer ? currentState . playerDeck : currentState . opponentDeck ; const card = this . helper . findCardInZone ( deck . board , null , entityId ) ; if ( ! card ) { console . warn ( '[card-changed-on-board] could not find card change on board' , entityId , deck . board ) ; return currentState ; } const boardWithRemovedCard = this . helper . removeSingleCardFromZone ( deck . board , null , entityId ) [ 0 ] ; const dbCard = this . allCards . getCard ( cardId ) || ( { } as ReferenceCard ) ; const oldCard = card . update ( { zone : 'TRANSFORMED_INTO_OTHER' , } as DeckCard ) ; const updatedCard = card . update ( { cardId : cardId , cardName : this . i18n . getCardName ( dbCard . id ) , manaCost : dbCard . cost , rarity : dbCard . rarity ? dbCard . rarity . toLowerCase ( ) : null , creatorCardId : creatorCardId , } as DeckCard ) ; const newBoard = this . helper . addSingleCardToZone ( boardWithRemovedCard , updatedCard ) ; const newOther = this . helper . addSingleCardToZone ( deck . otherZone , oldCard ) ; const newPlayerDeck = Object . assign ( new DeckState ( ) , deck , { board : newBoard , otherZone : newOther , } ) ; return Object . assign ( new GameState ( ) , currentState , { [ isPlayer ? 'playerDeck' : 'opponentDeck' ] : newPlayerDeck , } ) ; } event ( ) { return GameEvent . CARD_CHANGED_ON_BOARD ; } }