import { GameType } from '@firestone-hs/reference-data' ; import { RawRequirement } from '../../../../../models/achievement/raw-requirement' ; import { BattlegroundsInfo } from '../../../../../models/battlegrounds-info' ; import { GameEvent } from '../../../../../models/game-event' ; import { MemoryInspectionService } from '../../../../plugins/memory-inspection.service' ; import { Requirement } from '../_requirement' ; export class BattlegroundsRankReq implements Requirement { private isValid : boolean ; private rankAtReset : number ; private isBgs : boolean ; constructor ( private readonly memoryInspection , private readonly targetRank ) { } public static create ( rawReq , memoryInspection ) { if ( ! rawReq . values || rawReq . values . length !== 1 ) { console . error ( '[battlegrounds-rank-req] invalid parameters for BattlegroundsRankReq' , rawReq ) ; } return new BattlegroundsRankReq ( memoryInspection , parseInt ( rawReq . values [ 0 ] ) ) ; } async reset ( ) { this . isValid = undefined ; this . isBgs = undefined ; } afterAchievementCompletionReset ( ) { this . isValid = undefined ; this . isBgs = undefined ; } isCompleted ( ) { return this . isValid ; } test ( gameEvent ) { if ( gameEvent . type === GameEvent . MATCH_METADATA ) { this . handleMetaData ( gameEvent ) ; return ; } if ( gameEvent . type === GameEvent . WINNER ) { this . detectPlayerRank ( ) ; return ; } } private async handleMetaData ( gameEvent ) { this . isBgs = gameEvent . additionalData . metaData . GameType === GameType . GT_BATTLEGROUNDS || gameEvent . additionalData . metaData . GameType === GameType . GT_BATTLEGROUNDS_FRIENDLY ; if ( this . isBgs ) { const battlegroundsInfo = await this . memoryInspection . getBattlegroundsInfo ( ) ; if ( battlegroundsInfo ) { this . rankAtReset = battlegroundsInfo . rating ; } } else { this . rankAtReset = undefined ; } } private async detectPlayerRank ( ) { if ( ! this . isBgs ) { this . isValid = false ; return ; } const battlegroundsInfo = await this . getRank ( ) ; this . isValid = battlegroundsInfo && battlegroundsInfo . rating >= this . targetRank && ( ! this . rankAtReset || battlegroundsInfo . rating !== this . rankAtReset ) ; } private async getRank ( ) { return new Promise < BattlegroundsInfo > ( ( resolve ) => { this . getRankInternal ( ( info ) => resolve ( info ) ) ; } ) ; } private async getRankInternal ( callback ) { const rank = await this . memoryInspection . getBattlegroundsInfo ( ) ; if ( ! rank || this . rankAtReset === rank . rating ) { setTimeout ( ( ) => this . getRankInternal ( callback ) , 1000 ) ; return ; } callback ( rank ) ; } }