import { Injectable } from '@angular/core' ; import { PlayerInfo } from '../models/player-info' ; import { Events } from './events.service' ; import { MemoryInspectionService } from './plugins/memory-inspection.service' ; @ Injectable ( ) export class PlayersInfoService { private cachedInfo : { localPlayer : PlayerInfo ; opponent : PlayerInfo ; } ; private cacheInvalidationTimeout ; constructor ( private events , private memoryService ) { } public async getPlayerInfo ( ) { const infoFromGep = this . cachedInfo ?. localPlayer ? this . cachedInfo : await this . memoryService . getMatchInfo ( ) ; if ( ! infoFromGep ) { console . warn ( '[players-info] No player info returned by mindvision' , this . cachedInfo , infoFromGep ) ; return null ; } this . shortCache ( infoFromGep ) ; console . log ( '[players-info] retrieved player info from memory' , infoFromGep . localPlayer ) ; return infoFromGep . localPlayer ; } public async getOpponentInfo ( ) { const infoFromGep = this . cachedInfo ?. opponent ? this . cachedInfo : await this . memoryService . getMatchInfo ( ) ; if ( ! infoFromGep ) { console . warn ( '[players-info] No player info returned by mindvision' ) ; return null ; } this . shortCache ( infoFromGep ) ; console . log ( '[players-info] retrieved opponent info from memory' , infoFromGep . opponent ) ; return infoFromGep . opponent ; } private shortCache ( memoryInfo ) { this . cachedInfo = memoryInfo ; if ( this . cacheInvalidationTimeout ) { clearTimeout ( this . cacheInvalidationTimeout ) ; } this . cacheInvalidationTimeout = setTimeout ( ( ) => { this . cachedInfo = null ; this . cacheInvalidationTimeout = null ; } , 10000 ) ; } }