2024-10-18 11:39:51 +01:00
|
|
|
import { BotHelper } from "@spt/helpers/BotHelper";
|
2024-09-07 12:08:37 +01:00
|
|
|
import { ProfileHelper } from "@spt/helpers/ProfileHelper";
|
2024-12-22 16:35:15 +00:00
|
|
|
import { IBotBase } from "@spt/models/eft/common/tables/IBotBase";
|
2024-10-18 11:39:51 +01:00
|
|
|
import { IBotType } from "@spt/models/eft/common/tables/IBotType";
|
2024-09-07 12:08:37 +01:00
|
|
|
import { ConfigTypes } from "@spt/models/enums/ConfigTypes";
|
2024-10-19 12:43:38 +01:00
|
|
|
import { IBotGenerationDetails } from "@spt/models/spt/bots/BotGenerationDetails";
|
2024-09-07 12:08:37 +01:00
|
|
|
import { IBotConfig } from "@spt/models/spt/config/IBotConfig";
|
|
|
|
import { IPmcConfig } from "@spt/models/spt/config/IPmcConfig";
|
|
|
|
import { ILogger } from "@spt/models/spt/utils/ILogger";
|
|
|
|
import { ConfigServer } from "@spt/servers/ConfigServer";
|
|
|
|
import { RandomUtil } from "@spt/utils/RandomUtil";
|
|
|
|
import { ICloner } from "@spt/utils/cloners/ICloner";
|
|
|
|
import { inject, injectable } from "tsyringe";
|
|
|
|
import { DatabaseService } from "./DatabaseService";
|
|
|
|
import { LocalisationService } from "./LocalisationService";
|
|
|
|
|
|
|
|
@injectable()
|
|
|
|
export class BotNameService {
|
|
|
|
protected botConfig: IBotConfig;
|
|
|
|
protected pmcConfig: IPmcConfig;
|
|
|
|
protected usedNameCache: Set<string>;
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
@inject("PrimaryLogger") protected logger: ILogger,
|
|
|
|
@inject("RandomUtil") protected randomUtil: RandomUtil,
|
|
|
|
@inject("ProfileHelper") protected profileHelper: ProfileHelper,
|
2024-10-18 11:39:51 +01:00
|
|
|
@inject("BotHelper") protected botHelper: BotHelper,
|
2024-09-07 12:08:37 +01:00
|
|
|
@inject("DatabaseService") protected databaseService: DatabaseService,
|
|
|
|
@inject("LocalisationService") protected localisationService: LocalisationService,
|
|
|
|
@inject("ConfigServer") protected configServer: ConfigServer,
|
|
|
|
@inject("PrimaryCloner") protected cloner: ICloner,
|
|
|
|
) {
|
|
|
|
this.botConfig = this.configServer.getConfig(ConfigTypes.BOT);
|
|
|
|
this.pmcConfig = this.configServer.getConfig(ConfigTypes.PMC);
|
|
|
|
|
|
|
|
this.usedNameCache = new Set<string>();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear out any entries in Name Set
|
|
|
|
*/
|
|
|
|
public clearNameCache() {
|
|
|
|
this.usedNameCache.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a unique bot nickname
|
2024-10-18 11:39:51 +01:00
|
|
|
* @param botJsonTemplate bot JSON data from db
|
2024-09-07 12:08:37 +01:00
|
|
|
* @param botGenerationDetails
|
|
|
|
* @param botRole role of bot e.g. assault
|
|
|
|
* @param uniqueRoles Lowercase roles to always make unique
|
|
|
|
* @param sessionId OPTIONAL: profile session id
|
|
|
|
* @returns Nickname for bot
|
|
|
|
*/
|
|
|
|
public generateUniqueBotNickname(
|
2024-10-18 11:39:51 +01:00
|
|
|
botJsonTemplate: IBotType,
|
2024-10-19 12:43:38 +01:00
|
|
|
botGenerationDetails: IBotGenerationDetails,
|
2024-09-07 12:08:37 +01:00
|
|
|
botRole: string,
|
|
|
|
uniqueRoles?: string[],
|
|
|
|
): string {
|
2024-10-18 11:39:51 +01:00
|
|
|
const isPmc = botGenerationDetails.isPmc;
|
2024-12-22 16:35:15 +00:00
|
|
|
|
|
|
|
// Never show for players
|
|
|
|
const showTypeInNickname = !botGenerationDetails.isPlayerScav && this.botConfig.showTypeInNickname;
|
2024-12-06 16:49:39 +00:00
|
|
|
const roleShouldBeUnique = uniqueRoles?.includes(botRole.toLowerCase());
|
2024-10-18 11:39:51 +01:00
|
|
|
|
2024-09-07 12:08:37 +01:00
|
|
|
let isUnique = true;
|
|
|
|
let attempts = 0;
|
2024-09-30 16:55:34 +01:00
|
|
|
while (attempts <= 5) {
|
2024-10-18 11:39:51 +01:00
|
|
|
// Get bot name with leading/trailing whitespace removed
|
|
|
|
let name = isPmc // Explicit handling of PMCs, all other bots will get "first_name last_name"
|
|
|
|
? this.botHelper.getPmcNicknameOfMaxLength(this.botConfig.botNameLengthLimit, botGenerationDetails.side)
|
|
|
|
: `${this.randomUtil.getArrayValue(botJsonTemplate.firstName)} ${this.randomUtil.getArrayValue(botJsonTemplate.lastName) || ""}`;
|
2024-09-07 12:08:37 +01:00
|
|
|
name = name.trim();
|
|
|
|
|
|
|
|
// Config is set to add role to end of bot name
|
2024-10-18 11:39:51 +01:00
|
|
|
if (showTypeInNickname) {
|
2024-09-07 12:08:37 +01:00
|
|
|
name += ` ${botRole}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Replace pmc bot names with player name + prefix
|
|
|
|
if (botGenerationDetails.isPmc && botGenerationDetails.allPmcsHaveSameNameAsPlayer) {
|
|
|
|
const prefix = this.localisationService.getRandomTextThatMatchesPartialKey("pmc-name_prefix_");
|
|
|
|
name = `${prefix} ${name}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Is this a role that must be unique
|
2024-10-18 11:39:51 +01:00
|
|
|
if (roleShouldBeUnique) {
|
2024-09-07 12:08:37 +01:00
|
|
|
// Check name in cache
|
|
|
|
isUnique = !this.usedNameCache.has(name);
|
|
|
|
if (!isUnique) {
|
|
|
|
// Not unique
|
|
|
|
if (attempts >= 5) {
|
|
|
|
// 5 attempts to generate a name, pool probably isn't big enough
|
2024-09-30 16:55:34 +01:00
|
|
|
const genericName = `${botGenerationDetails.side} ${this.randomUtil.getInt(100000, 999999)}`;
|
|
|
|
this.logger.debug(
|
|
|
|
`Failed to find unique name for: ${name} after 5 attempts, using: ${genericName}`,
|
|
|
|
);
|
|
|
|
return genericName;
|
2024-09-07 12:08:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
attempts++;
|
|
|
|
|
|
|
|
// Try again
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add bot name to cache to prevent being used again
|
|
|
|
this.usedNameCache.add(name);
|
|
|
|
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-12-22 16:35:15 +00:00
|
|
|
* Add random PMC name to bots MainProfileNickname property
|
|
|
|
* @param bot Bot to update
|
2024-09-07 12:08:37 +01:00
|
|
|
*/
|
2024-12-22 16:35:15 +00:00
|
|
|
public addRandomPmcNameToBotMainProfileNicknameProperty(bot: IBotBase): void {
|
|
|
|
// Simulate bot looking like a player scav with the PMC name in brackets.
|
|
|
|
// E.g. "ScavName (PMC Name)"
|
|
|
|
bot.Info.MainProfileNickname = this.getRandomPMCName();
|
2024-09-07 12:08:37 +01:00
|
|
|
}
|
|
|
|
|
2024-12-22 16:35:15 +00:00
|
|
|
/**
|
|
|
|
* Choose a random PMC name from bear or usec bot jsons
|
|
|
|
* @returns PMC name as string
|
|
|
|
*/
|
|
|
|
protected getRandomPMCName(): string {
|
|
|
|
const bots = this.databaseService.getBots().types;
|
|
|
|
|
|
|
|
const pmcNames = new Set([...bots.usec.firstName, ...bots.bear.firstName]);
|
|
|
|
return this.randomUtil.getArrayValue(Array.from(pmcNames));
|
2024-09-07 12:08:37 +01:00
|
|
|
}
|
|
|
|
}
|