From bf39ed957ead15c96321201a525fd53cbedbb867 Mon Sep 17 00:00:00 2001 From: Dev Date: Mon, 23 Sep 2024 23:01:47 +0100 Subject: [PATCH] Cleaned up duplication in bot generation system Added optional parameter to `cacheHasBotWithKey` Removed params needed for calling `rollToBePmc` Created `getMostRecentRaidSettings` --- project/src/callbacks/BotCallbacks.ts | 2 +- project/src/controllers/BotController.ts | 80 ++++++++----------- project/src/helpers/BotHelper.ts | 7 +- .../src/services/BotGenerationCacheService.ts | 4 +- 4 files changed, 37 insertions(+), 56 deletions(-) diff --git a/project/src/callbacks/BotCallbacks.ts b/project/src/callbacks/BotCallbacks.ts index d5aaeff1..558d9f2c 100644 --- a/project/src/callbacks/BotCallbacks.ts +++ b/project/src/callbacks/BotCallbacks.ts @@ -76,7 +76,7 @@ export class BotCallbacks { * Handle singleplayer/settings/bot/maxCap * @returns string */ - public getBotCap(url: string, info: any, sessionID: string): string { + public getBotCap(url: string, info: IEmptyRequestData, sessionID: string): string { const splitUrl = url.split("/"); const location = splitUrl[splitUrl.length - 1]; return this.httpResponse.noBody(this.botController.getBotCap(location)); diff --git a/project/src/controllers/BotController.ts b/project/src/controllers/BotController.ts index a3f6baf5..eaa6eb3d 100644 --- a/project/src/controllers/BotController.ts +++ b/project/src/controllers/BotController.ts @@ -173,15 +173,7 @@ export class BotController { pmcProfile: IPmcData, sessionId: string, ): Promise { - const raidSettings = this.applicationContext - .getLatestValue(ContextVariableType.RAID_CONFIGURATION) - ?.getValue(); - - if (raidSettings === undefined) { - this.logger.warning(this.localisationService.getText("bot-unable_to_load_raid_settings_from_appcontext")); - } - - const pmcLevelRangeForMap = this.getPmcLevelRangeForMap(raidSettings?.location); + const raidSettings = this.getMostRecentRaidSettings(); const allPmcsHaveSameNameAsPlayer = this.randomUtil.getChance100( this.pmcConfig.allPMCsHavePlayerNameWithRandomPrefixChance, @@ -193,9 +185,9 @@ export class BotController { condition, pmcProfile, allPmcsHaveSameNameAsPlayer, - pmcLevelRangeForMap, + raidSettings, this.botConfig.presetBatch[condition.Role], - false, + this.botHelper.isBotPmc(condition.Role), ); conditionPromises.push(this.generateWithBotDetails(condition, botGenerationDetails, sessionId)); @@ -209,6 +201,18 @@ export class BotController { return []; } + protected getMostRecentRaidSettings(): IGetRaidConfigurationRequestData { + const raidSettings = this.applicationContext + .getLatestValue(ContextVariableType.RAID_CONFIGURATION) + ?.getValue(); + + if (raidSettings === undefined) { + this.logger.warning(this.localisationService.getText("bot-unable_to_load_raid_settings_from_appcontext")); + } + + return raidSettings; + } + /** * Get min/max level range values for a specific map * @param location Map name e.g. factory4_day @@ -227,7 +231,7 @@ export class BotController { * @param condition Client data defining bot type and difficulty * @param pmcProfile Player who is generating bots * @param allPmcsHaveSameNameAsPlayer Should all PMCs have same name as player - * @param pmcLevelRangeForMap Min/max levels for PMCs to generate within + * @param raidSettings Settings chosen pre-raid by player * @param botCountToGenerate How many bots to generate * @param generateAsPmc Force bot being generated a PMC * @returns BotGenerationDetails @@ -236,13 +240,13 @@ export class BotController { condition: Condition, pmcProfile: IPmcData, allPmcsHaveSameNameAsPlayer: boolean, - pmcLevelRangeForMap: MinMax, + raidSettings: IGetRaidConfigurationRequestData, botCountToGenerate: number, generateAsPmc: boolean, ): BotGenerationDetails { return { isPmc: generateAsPmc, - side: SideType.SAVAGE, + side: generateAsPmc ? this.botHelper.getPmcSideByRole(condition.Role) : SideType.SAVAGE, role: condition.Role, playerLevel: this.getPlayerLevelFromProfile(pmcProfile), playerName: pmcProfile.Info.Nickname, @@ -250,7 +254,7 @@ export class BotController { botRelativeLevelDeltaMin: this.pmcConfig.botRelativeLevelDeltaMin, botCountToGenerate: botCountToGenerate, botDifficulty: condition.Difficulty, - locationSpecificPmcLevelOverride: pmcLevelRangeForMap, + locationSpecificPmcLevelOverride: this.getPmcLevelRangeForMap(raidSettings?.location), // Min/max levels for PMCs to generate within isPlayerScav: false, allPmcsHaveSameNameAsPlayer: allPmcsHaveSameNameAsPlayer, }; @@ -286,13 +290,6 @@ export class BotController { ); } - // Custom map waves can have spt roles in them - // Is bot type pmcUSEC/pmcBEAR, set is pmc true and set side - if (this.botHelper.botRoleIsPmc(condition.Role)) { - botGenerationDetails.isPmc = true; - botGenerationDetails.side = this.botHelper.getPmcSideByRole(condition.Role); - } - // Create a compound key to store bots in cache against const cacheKey = this.botGenerationCacheService.createCacheKey( botGenerationDetails.eventRole ?? botGenerationDetails.role, @@ -353,15 +350,7 @@ export class BotController { const pmcProfile = this.profileHelper.getPmcProfile(sessionId); const requestedBot = request.conditions[0]; - const raidSettings = this.applicationContext - .getLatestValue(ContextVariableType.RAID_CONFIGURATION) - ?.getValue(); - - if (raidSettings === undefined) { - this.logger.warning(this.localisationService.getText("bot-unable_to_load_raid_settings_from_appcontext")); - } - - const pmcLevelRangeForMap = this.getPmcLevelRangeForMap(raidSettings?.location); + const raidSettings = this.getMostRecentRaidSettings(); // Create generation request for when cache is empty const condition: Condition = { @@ -373,9 +362,9 @@ export class BotController { condition, pmcProfile, false, - pmcLevelRangeForMap, + raidSettings, this.botConfig.presetBatch[requestedBot.Role], - false, + this.botHelper.isBotPmc(requestedBot.Role), ); // Event bots need special actions to occur, set data up for them @@ -388,21 +377,16 @@ export class BotController { ); } - if (this.botHelper.isBotPmc(botGenerationDetails.role)) { - botGenerationDetails.isPmc = true; - botGenerationDetails.side = this.botHelper.getPmcSideByRole(requestedBot.Role); - } - - // Roll chance to be pmc if type is allowed to be one - const botConvertRateMinMax = this.getPmcConversionMinMaxForLocation(requestedBot.Role, raidSettings?.location); - if (botConvertRateMinMax) { - // Should bot become PMC - const convertToPmc = this.botHelper.rollChanceToBePmc( - requestedBot.Role, - botConvertRateMinMax, - raidSettings?.location, - ); + // Does non pmc bot have a chance of being converted into a pmc + const convertIntoPmcChanceMinMax = this.getPmcConversionMinMaxForLocation( + requestedBot.Role, + raidSettings?.location, + ); + if (convertIntoPmcChanceMinMax && !botGenerationDetails.isPmc) { + // Bot has % chance to become pmc and isnt one pmc already + const convertToPmc = this.botHelper.rollChanceToBePmc(convertIntoPmcChanceMinMax); if (convertToPmc) { + // Update requirements botGenerationDetails.isPmc = true; botGenerationDetails.role = this.botHelper.getRandomizedPmcRole(); botGenerationDetails.side = this.botHelper.getPmcSideByRole(botGenerationDetails.role); @@ -432,7 +416,7 @@ export class BotController { ); // Check cache for bot using above key - if (!this.botGenerationCacheService.cacheHasBotOfRole(cacheKey)) { + if (!this.botGenerationCacheService.cacheHasBotWithKey(cacheKey)) { const botPromises: Promise[] = []; // No bot in cache, generate new and return one for (let i = 0; i < botGenerationDetails.botCountToGenerate; i++) { diff --git a/project/src/helpers/BotHelper.ts b/project/src/helpers/BotHelper.ts index 5138cb5f..05368805 100644 --- a/project/src/helpers/BotHelper.ts +++ b/project/src/helpers/BotHelper.ts @@ -92,11 +92,8 @@ export class BotHelper { } } - public rollChanceToBePmc(role: string, botConvertMinMax: MinMax, location: string): boolean { - return ( - role.toLowerCase() in this.getPmcConversionValuesForLocation(location) && - this.randomUtil.getChance100(this.randomUtil.getInt(botConvertMinMax.min, botConvertMinMax.max)) - ); + public rollChanceToBePmc(botConvertMinMax: MinMax): boolean { + return this.randomUtil.getChance100(this.randomUtil.getInt(botConvertMinMax.min, botConvertMinMax.max)); } protected getPmcConversionValuesForLocation(location: string) { diff --git a/project/src/services/BotGenerationCacheService.ts b/project/src/services/BotGenerationCacheService.ts index 49ac5a8a..6f32b8da 100644 --- a/project/src/services/BotGenerationCacheService.ts +++ b/project/src/services/BotGenerationCacheService.ts @@ -82,8 +82,8 @@ export class BotGenerationCacheService { * Does cache have a bot with requested key * @returns false if empty */ - public cacheHasBotOfRole(key: string): boolean { - return this.storedBots.has(key) && this.storedBots.get(key).length > 0; + public cacheHasBotWithKey(key: string, size = 0): boolean { + return this.storedBots.has(key) && this.storedBots.get(key).length > size; } public getCachedBotCount(key: string): number {