import { 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' ; import { buildCardArraysFromDeck } from './deckbuilding-helper' ; export class DeckbuildingTextNumberOfWordsReq implements Requirement { private doesDeckMeetSpec : boolean ; constructor ( private readonly targetCardQuantity , private readonly qualifier , private readonly targetNumberOfWords , private readonly numberOfWordsQualifier , private readonly cards ) { } public static create ( rawReq , cards ) { return new DeckbuildingTextNumberOfWordsReq ( parseInt ( rawReq . values [ 0 ] ) , rawReq . values [ 1 ] as QualifierType , parseInt ( rawReq . values [ 2 ] ) , rawReq . values [ 3 ] as QualifierType , cards ) ; } reset ( ) { this . doesDeckMeetSpec = undefined ; } afterAchievementCompletionReset ( ) { this . doesDeckMeetSpec = undefined ; } isCompleted ( ) { return this . doesDeckMeetSpec ; } test ( gameEvent ) { if ( gameEvent . type === GameEvent . PLAYERS_INFO ) { this . handleEvent ( gameEvent ) ; } } private handleEvent ( gameEvent ) { const deck = gameEvent . localPlayer ?. deck ? gameEvent . localPlayer . deck . deck : null ; if ( deck && deck . cards && deck . cards . length > 0 ) { const cards = buildCardArraysFromDeck ( deck , this . cards ) ; const numberOfMatchingCards = cards . filter ( ( card ) => this . cardMatches ( card ) ) . length ; if ( this . qualifier === 'AT_LEAST' ) { this . doesDeckMeetSpec = numberOfMatchingCards >= this . targetCardQuantity ; } else if ( this . qualifier === 'AT_MOST' ) { this . doesDeckMeetSpec = numberOfMatchingCards <= this . targetCardQuantity ; } else { this . doesDeckMeetSpec = numberOfMatchingCards === this . targetCardQuantity ; } } else { this . doesDeckMeetSpec = false ; } } private cardMatches ( card ) { const numberOfWords = ( card . text || '' ) . replace ( '\n' , ' ' ) . split ( ' ' ) . length ; if ( this . numberOfWordsQualifier === 'AT_LEAST' ) { return numberOfWords >= this . targetNumberOfWords ; } else if ( this . numberOfWordsQualifier === 'AT_MOST' ) { return numberOfWords <= this . targetNumberOfWords ; } else { return numberOfWords === this . targetNumberOfWords ; } } }