Added bot cache count when generating. Will only add more bots to cache when below desired amount

This commit is contained in:
Dev 2024-07-06 16:42:47 +01:00
parent 47edbbcd98
commit 4d167568fb
2 changed files with 37 additions and 19 deletions

View File

@ -185,13 +185,12 @@ export class BotController
{ {
const pmcProfile = this.profileHelper.getPmcProfile(sessionId); const pmcProfile = this.profileHelper.getPmcProfile(sessionId);
// If there's more than 1 condition, this is the first time client has requested bots
// Client sends every bot type it will need in raid
// Use this opportunity to create and cache bots for later retreval // Use this opportunity to create and cache bots for later retreval
const isFirstGen = info.conditions.length > 1; const multipleBotTypesRequested = info.conditions.length > 1;
if (isFirstGen) if (multipleBotTypesRequested)
{ {
return this.generateBotsFirstTime(info, pmcProfile, sessionId); this.logger.warning(`${JSON.stringify(info)}`);
return this.generateMultipleBotsAndCache(info, pmcProfile, sessionId);
} }
return this.returnSingleBotFromCache(sessionId, info); return this.returnSingleBotFromCache(sessionId, info);
@ -204,7 +203,7 @@ export class BotController
* @param sessionId Session id * @param sessionId Session id
* @returns * @returns
*/ */
protected async generateBotsFirstTime( protected async generateMultipleBotsAndCache(
request: IGenerateBotsRequestData, request: IGenerateBotsRequestData,
pmcProfile: IPmcData, pmcProfile: IPmcData,
sessionId: string, sessionId: string,
@ -216,10 +215,10 @@ export class BotController
if (raidSettings === undefined) if (raidSettings === undefined)
{ {
throw new Error(this.localisationService.getText("bot-unable_to_load_raid_settings_from_appcontext")); // throw new Error(this.localisationService.getText("bot-unable_to_load_raid_settings_from_appcontext"));
} }
const pmcLevelRangeForMap const pmcLevelRangeForMap
= this.pmcConfig.locationSpecificPmcLevelOverride[raidSettings.location.toLowerCase()]; = this.pmcConfig.locationSpecificPmcLevelOverride[raidSettings?.location.toLowerCase()];
const allPmcsHaveSameNameAsPlayer = this.randomUtil.getChance100( const allPmcsHaveSameNameAsPlayer = this.randomUtil.getChance100(
this.pmcConfig.allPMCsHavePlayerNameWithRandomPrefixChance, this.pmcConfig.allPMCsHavePlayerNameWithRandomPrefixChance,
@ -321,16 +320,25 @@ export class BotController
botGenerationDetails.side = this.botHelper.getPmcSideByRole(condition.Role); botGenerationDetails.side = this.botHelper.getPmcSideByRole(condition.Role);
} }
// Loop over and make x bots for this bot wave // Create a compound key to store bots in cache against
const cacheKey = `${ const cacheKey = this.botGenerationCacheService.createCacheKey(
botGenerationDetails.eventRole ?? botGenerationDetails.role botGenerationDetails.eventRole ?? botGenerationDetails.role,
}${botGenerationDetails.botDifficulty}`; botGenerationDetails.botDifficulty,
);
// Get number of bots we have in cache
const botCacheCount = this.botGenerationCacheService.getCachedBotCount(cacheKey);
const botPromises: Promise<void>[] = []; const botPromises: Promise<void>[] = [];
for (let i = 0; i < botGenerationDetails.botCountToGenerate; i++) if (botCacheCount < botGenerationDetails.botCountToGenerate)
{ {
const detailsClone = this.cloner.clone(botGenerationDetails); // We're below desired count, add bots to cache
botPromises.push(this.generateSingleBotAndStoreInCache(detailsClone, sessionId, cacheKey)); for (let i = 0; i < botGenerationDetails.botCountToGenerate; i++)
{
const detailsClone = this.cloner.clone(botGenerationDetails);
botPromises.push(this.generateSingleBotAndStoreInCache(detailsClone, sessionId, cacheKey));
}
} }
return Promise.all(botPromises).then(() => return Promise.all(botPromises).then(() =>
{ {
this.logger.debug( this.logger.debug(
@ -454,10 +462,10 @@ export class BotController
} }
} }
// Construct cache key // Create a compound key to store bots in cache against
const cacheKey = `${ const cacheKey = this.botGenerationCacheService.createCacheKey(
botGenerationDetails.eventRole ?? botGenerationDetails.role botGenerationDetails.eventRole ?? botGenerationDetails.role,
}${botGenerationDetails.botDifficulty}`; botGenerationDetails.botDifficulty);
// Check cache for bot using above key // Check cache for bot using above key
if (!this.botGenerationCacheService.cacheHasBotOfRole(cacheKey)) if (!this.botGenerationCacheService.cacheHasBotOfRole(cacheKey))

View File

@ -99,4 +99,14 @@ export class BotGenerationCacheService
{ {
return this.storedBots.has(key) && this.storedBots.get(key).length > 0; return this.storedBots.has(key) && this.storedBots.get(key).length > 0;
} }
public getCachedBotCount(key: string): number
{
return this.storedBots.get(key)?.length ?? 0;
}
public createCacheKey(role: string, difficulty: string): string
{
return `${role.toLowerCase()}${difficulty.toLowerCase()}`;
}
} }