import { CardType , ReferenceCard } from '@firestone-hs/reference-data' ; import { CardsFacadeService } from '@services/cards-facade.service' ; import { RawRequirement } from '../../../../models/achievement/raw-requirement' ; import { GameEvent } from '../../../../models/game-event' ; import { QualifierType } from './_qualifier.type' ; import { Requirement } from './_requirement' ; export class CardWithSameAttributePlayedReq implements Requirement { private isCardPlayed : boolean ; private playCounts : { [ cardAttribute ] : number ; } ; private readonly cardType : CardType ; private readonly attribute : string ; constructor ( private readonly targetQuantity , private readonly qualifier , attribute , cardType , private readonly cards ) { this . cardType = CardType [ cardType ] ; this . attribute = attribute . toLowerCase ( ) ; this . playCounts = { } ; } public static create ( rawReq , cards ) { if ( ! rawReq . values || rawReq . values . length !== 4 ) { console . error ( 'invalid parameters for CardWithSameAttributePlayedReq' , rawReq ) ; } return new CardWithSameAttributePlayedReq ( parseInt ( rawReq . values [ 0 ] ) , rawReq . values [ 1 ] as QualifierType , rawReq . values [ 2 ] . toLowerCase ( ) , rawReq . values [ 3 ] , cards ) ; } reset ( ) { this . playCounts = { } ; this . isCardPlayed = undefined ; } afterAchievementCompletionReset ( ) { this . playCounts = { } ; this . isCardPlayed = undefined ; } isCompleted ( ) { return this . isCardPlayed ; } test ( gameEvent ) { if ( gameEvent . type === GameEvent . CARD_PLAYED ) { this . detectCardPlayedEvent ( gameEvent ) ; return ; } } private detectCardPlayedEvent ( gameEvent ) { const cardId = gameEvent . cardId ; const controllerId = gameEvent . controllerId ; const localPlayer = gameEvent . localPlayer ; const card = this . cards . getCard ( cardId ) ; if ( ! card ?. type ) { return ; } if ( controllerId === localPlayer ?. PlayerId && CardType [ card . type . toUpperCase ( ) ] === this . cardType && ! this . hasEcho ( card ) ) { const attributeValue = card [ this . attribute ] ; this . playCounts [ attributeValue ] = ( this . playCounts [ attributeValue ] || 0 ) + 1 ; this . isCardPlayed = Object . keys ( this . playCounts ) . some ( ( cardAttribute ) => this . playCounts [ cardAttribute ] >= this . targetQuantity ) ; } } private hasEcho ( card ) { return card . mechanics && card . mechanics . includes ( 'ECHO' ) ; } }