import { RawRequirement } from '../../../../models/achievement/raw-requirement' ; import { GameEvent } from '../../../../models/game-event' ; import { QualifierType } from './_qualifier.type' ; import { Requirement } from './_requirement' ; export class HealthAtEndReq implements Requirement { private hasCorrectHealthAtEnd : boolean ; constructor ( private readonly targetHealth , private readonly qualifier ) { } public static create ( rawReq , qualifier = '' ) { if ( ! rawReq . values || rawReq . values . length === 0 ) { console . error ( 'invalid parameters for HealthAtEndReq' , rawReq ) ; } return new HealthAtEndReq ( parseInt ( rawReq . values [ 0 ] ) , ( rawReq . values . length > 1 ? rawReq . values [ 1 ] : '' ) as QualifierType ) ; } reset ( ) { this . hasCorrectHealthAtEnd = undefined ; } afterAchievementCompletionReset ( ) { this . hasCorrectHealthAtEnd = undefined ; } isCompleted ( ) { return this . hasCorrectHealthAtEnd ; } test ( gameEvent ) { if ( gameEvent . type === GameEvent . GAME_END ) { this . handleEvent ( gameEvent ) ; } } private handleEvent ( gameEvent ) { const localPlayerMaxHealth = gameEvent . additionalData . report ?. LocalPlayer ?. TotalHealth ; const localPlayerDamage = gameEvent . additionalData . report ?. LocalPlayer ?. DamageTaken ; const healthDelta = localPlayerMaxHealth - localPlayerDamage ; if ( this . qualifier === '' && healthDelta === this . targetHealth ) { this . hasCorrectHealthAtEnd = true ; } else if ( this . qualifier === 'AT_MOST' && healthDelta <= this . targetHealth ) { this . hasCorrectHealthAtEnd = true ; } else { this . hasCorrectHealthAtEnd = false ; } } }