Cleaned up duplication in bot generation system

Added optional parameter to `cacheHasBotWithKey`

Removed params needed for calling `rollToBePmc`

Created `getMostRecentRaidSettings`
This commit is contained in:
Dev 2024-09-23 23:01:47 +01:00
parent a477b0b584
commit bf39ed957e
4 changed files with 37 additions and 56 deletions

View File

@ -76,7 +76,7 @@ export class BotCallbacks {
* Handle singleplayer/settings/bot/maxCap * Handle singleplayer/settings/bot/maxCap
* @returns string * @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 splitUrl = url.split("/");
const location = splitUrl[splitUrl.length - 1]; const location = splitUrl[splitUrl.length - 1];
return this.httpResponse.noBody(this.botController.getBotCap(location)); return this.httpResponse.noBody(this.botController.getBotCap(location));

View File

@ -173,15 +173,7 @@ export class BotController {
pmcProfile: IPmcData, pmcProfile: IPmcData,
sessionId: string, sessionId: string,
): Promise<IBotBase[]> { ): Promise<IBotBase[]> {
const raidSettings = this.applicationContext const raidSettings = this.getMostRecentRaidSettings();
.getLatestValue(ContextVariableType.RAID_CONFIGURATION)
?.getValue<IGetRaidConfigurationRequestData>();
if (raidSettings === undefined) {
this.logger.warning(this.localisationService.getText("bot-unable_to_load_raid_settings_from_appcontext"));
}
const pmcLevelRangeForMap = this.getPmcLevelRangeForMap(raidSettings?.location);
const allPmcsHaveSameNameAsPlayer = this.randomUtil.getChance100( const allPmcsHaveSameNameAsPlayer = this.randomUtil.getChance100(
this.pmcConfig.allPMCsHavePlayerNameWithRandomPrefixChance, this.pmcConfig.allPMCsHavePlayerNameWithRandomPrefixChance,
@ -193,9 +185,9 @@ export class BotController {
condition, condition,
pmcProfile, pmcProfile,
allPmcsHaveSameNameAsPlayer, allPmcsHaveSameNameAsPlayer,
pmcLevelRangeForMap, raidSettings,
this.botConfig.presetBatch[condition.Role], this.botConfig.presetBatch[condition.Role],
false, this.botHelper.isBotPmc(condition.Role),
); );
conditionPromises.push(this.generateWithBotDetails(condition, botGenerationDetails, sessionId)); conditionPromises.push(this.generateWithBotDetails(condition, botGenerationDetails, sessionId));
@ -209,6 +201,18 @@ export class BotController {
return []; return [];
} }
protected getMostRecentRaidSettings(): IGetRaidConfigurationRequestData {
const raidSettings = this.applicationContext
.getLatestValue(ContextVariableType.RAID_CONFIGURATION)
?.getValue<IGetRaidConfigurationRequestData>();
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 * Get min/max level range values for a specific map
* @param location Map name e.g. factory4_day * @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 condition Client data defining bot type and difficulty
* @param pmcProfile Player who is generating bots * @param pmcProfile Player who is generating bots
* @param allPmcsHaveSameNameAsPlayer Should all PMCs have same name as player * @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 botCountToGenerate How many bots to generate
* @param generateAsPmc Force bot being generated a PMC * @param generateAsPmc Force bot being generated a PMC
* @returns BotGenerationDetails * @returns BotGenerationDetails
@ -236,13 +240,13 @@ export class BotController {
condition: Condition, condition: Condition,
pmcProfile: IPmcData, pmcProfile: IPmcData,
allPmcsHaveSameNameAsPlayer: boolean, allPmcsHaveSameNameAsPlayer: boolean,
pmcLevelRangeForMap: MinMax, raidSettings: IGetRaidConfigurationRequestData,
botCountToGenerate: number, botCountToGenerate: number,
generateAsPmc: boolean, generateAsPmc: boolean,
): BotGenerationDetails { ): BotGenerationDetails {
return { return {
isPmc: generateAsPmc, isPmc: generateAsPmc,
side: SideType.SAVAGE, side: generateAsPmc ? this.botHelper.getPmcSideByRole(condition.Role) : SideType.SAVAGE,
role: condition.Role, role: condition.Role,
playerLevel: this.getPlayerLevelFromProfile(pmcProfile), playerLevel: this.getPlayerLevelFromProfile(pmcProfile),
playerName: pmcProfile.Info.Nickname, playerName: pmcProfile.Info.Nickname,
@ -250,7 +254,7 @@ export class BotController {
botRelativeLevelDeltaMin: this.pmcConfig.botRelativeLevelDeltaMin, botRelativeLevelDeltaMin: this.pmcConfig.botRelativeLevelDeltaMin,
botCountToGenerate: botCountToGenerate, botCountToGenerate: botCountToGenerate,
botDifficulty: condition.Difficulty, botDifficulty: condition.Difficulty,
locationSpecificPmcLevelOverride: pmcLevelRangeForMap, locationSpecificPmcLevelOverride: this.getPmcLevelRangeForMap(raidSettings?.location), // Min/max levels for PMCs to generate within
isPlayerScav: false, isPlayerScav: false,
allPmcsHaveSameNameAsPlayer: allPmcsHaveSameNameAsPlayer, 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 // Create a compound key to store bots in cache against
const cacheKey = this.botGenerationCacheService.createCacheKey( const cacheKey = this.botGenerationCacheService.createCacheKey(
botGenerationDetails.eventRole ?? botGenerationDetails.role, botGenerationDetails.eventRole ?? botGenerationDetails.role,
@ -353,15 +350,7 @@ export class BotController {
const pmcProfile = this.profileHelper.getPmcProfile(sessionId); const pmcProfile = this.profileHelper.getPmcProfile(sessionId);
const requestedBot = request.conditions[0]; const requestedBot = request.conditions[0];
const raidSettings = this.applicationContext const raidSettings = this.getMostRecentRaidSettings();
.getLatestValue(ContextVariableType.RAID_CONFIGURATION)
?.getValue<IGetRaidConfigurationRequestData>();
if (raidSettings === undefined) {
this.logger.warning(this.localisationService.getText("bot-unable_to_load_raid_settings_from_appcontext"));
}
const pmcLevelRangeForMap = this.getPmcLevelRangeForMap(raidSettings?.location);
// Create generation request for when cache is empty // Create generation request for when cache is empty
const condition: Condition = { const condition: Condition = {
@ -373,9 +362,9 @@ export class BotController {
condition, condition,
pmcProfile, pmcProfile,
false, false,
pmcLevelRangeForMap, raidSettings,
this.botConfig.presetBatch[requestedBot.Role], this.botConfig.presetBatch[requestedBot.Role],
false, this.botHelper.isBotPmc(requestedBot.Role),
); );
// Event bots need special actions to occur, set data up for them // 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)) { // Does non pmc bot have a chance of being converted into a pmc
botGenerationDetails.isPmc = true; const convertIntoPmcChanceMinMax = this.getPmcConversionMinMaxForLocation(
botGenerationDetails.side = this.botHelper.getPmcSideByRole(requestedBot.Role); requestedBot.Role,
} raidSettings?.location,
);
// Roll chance to be pmc if type is allowed to be one if (convertIntoPmcChanceMinMax && !botGenerationDetails.isPmc) {
const botConvertRateMinMax = this.getPmcConversionMinMaxForLocation(requestedBot.Role, raidSettings?.location); // Bot has % chance to become pmc and isnt one pmc already
if (botConvertRateMinMax) { const convertToPmc = this.botHelper.rollChanceToBePmc(convertIntoPmcChanceMinMax);
// Should bot become PMC
const convertToPmc = this.botHelper.rollChanceToBePmc(
requestedBot.Role,
botConvertRateMinMax,
raidSettings?.location,
);
if (convertToPmc) { if (convertToPmc) {
// Update requirements
botGenerationDetails.isPmc = true; botGenerationDetails.isPmc = true;
botGenerationDetails.role = this.botHelper.getRandomizedPmcRole(); botGenerationDetails.role = this.botHelper.getRandomizedPmcRole();
botGenerationDetails.side = this.botHelper.getPmcSideByRole(botGenerationDetails.role); botGenerationDetails.side = this.botHelper.getPmcSideByRole(botGenerationDetails.role);
@ -432,7 +416,7 @@ export class BotController {
); );
// Check cache for bot using above key // Check cache for bot using above key
if (!this.botGenerationCacheService.cacheHasBotOfRole(cacheKey)) { if (!this.botGenerationCacheService.cacheHasBotWithKey(cacheKey)) {
const botPromises: Promise<void>[] = []; const botPromises: Promise<void>[] = [];
// No bot in cache, generate new and return one // No bot in cache, generate new and return one
for (let i = 0; i < botGenerationDetails.botCountToGenerate; i++) { for (let i = 0; i < botGenerationDetails.botCountToGenerate; i++) {

View File

@ -92,11 +92,8 @@ export class BotHelper {
} }
} }
public rollChanceToBePmc(role: string, botConvertMinMax: MinMax, location: string): boolean { public rollChanceToBePmc(botConvertMinMax: MinMax): boolean {
return ( return this.randomUtil.getChance100(this.randomUtil.getInt(botConvertMinMax.min, botConvertMinMax.max));
role.toLowerCase() in this.getPmcConversionValuesForLocation(location) &&
this.randomUtil.getChance100(this.randomUtil.getInt(botConvertMinMax.min, botConvertMinMax.max))
);
} }
protected getPmcConversionValuesForLocation(location: string) { protected getPmcConversionValuesForLocation(location: string) {

View File

@ -82,8 +82,8 @@ export class BotGenerationCacheService {
* Does cache have a bot with requested key * Does cache have a bot with requested key
* @returns false if empty * @returns false if empty
*/ */
public cacheHasBotOfRole(key: string): boolean { public cacheHasBotWithKey(key: string, size = 0): boolean {
return this.storedBots.has(key) && this.storedBots.get(key).length > 0; return this.storedBots.has(key) && this.storedBots.get(key).length > size;
} }
public getCachedBotCount(key: string): number { public getCachedBotCount(key: string): number {