import { BoardSecret } from '../../../models/decktracker/board-secret' ; 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 { SecretConfigService } from '../secret-config.service' ; import { DeckManipulationHelper } from './deck-manipulation-helper' ; import { EventParser } from './event-parser' ; export class SecretPlayedFromDeckParser implements EventParser { constructor ( private readonly helper , private readonly secretConfig ) { } applies ( gameEvent , state ) { return state && gameEvent . type === GameEvent . SECRET_PLAYED_FROM_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 secretClass = gameEvent . additionalData . playerClass ; const creatorCardId = gameEvent . additionalData . creatorCardId ; const card = this . helper . findCardInZone ( deck . deck , cardId , entityId ) ; const previousDeck = deck . deck ; const newDeck = this . helper . removeSingleCardFromZone ( previousDeck , cardId , entityId , deck . deckList . length === 0 ) [ 0 ] ; const cardWithZone = card . update ( { zone : 'SECRET' , } as DeckCard ) ; const previousOtherZone = deck . otherZone ; const newOtherZone = this . helper . addSingleCardToZone ( previousOtherZone , cardWithZone ) ; const newPlayerDeck = Object . assign ( new DeckState ( ) , deck , { deck : newDeck , otherZone : newOtherZone , secrets : [ ... deck . secrets , BoardSecret . create ( entityId , cardId , this . secretConfig . getValidSecrets ( currentState . metadata , secretClass ) ) , ] as readonly BoardSecret [ ] , } as DeckState ) ; return Object . assign ( new GameState ( ) , currentState , { [ isPlayer ? 'playerDeck' : 'opponentDeck' ] : newPlayerDeck , } ) ; } event ( ) { return GameEvent . SECRET_PLAYED_FROM_DECK ; } }