Improved logic inside isModValidForSlot() to better handle edge cases

This commit is contained in:
Dev 2024-04-22 09:39:32 +01:00
parent e3bb1932ff
commit fe7d09e075

View File

@ -1106,12 +1106,12 @@ export class BotEquipmentModGenerator
} }
/** /**
* Log errors if mod is not compatible with slot * Check if mod exists in db + is for a required slot
* @param modToAdd template of mod to check * @param modToAdd Db template of mod to check
* @param slotAddedToTemplate slot the item will be placed in * @param slotAddedToTemplate Slot object the item will be placed as child into
* @param modSlot slot the mod will fill * @param modSlot Slot the mod will fill
* @param parentTemplate template of the mods being added * @param parentTemplate Db template of the mods being added
* @param botRole * @param botRole Bots wildspawntype (assault/pmcBot etc)
* @returns true if valid * @returns true if valid
*/ */
protected isModValidForSlot( protected isModValidForSlot(
@ -1122,31 +1122,31 @@ export class BotEquipmentModGenerator
botRole: string, botRole: string,
): boolean ): boolean
{ {
const modBeingAddedTemplate = modToAdd[1]; const modBeingAddedDbTemplate = modToAdd[1];
// Mod lacks template item // Mod lacks db template object
if (!modBeingAddedTemplate) if (!modBeingAddedDbTemplate)
{ {
this.logger.error( this.logger.error(
this.localisationService.getText("bot-no_item_template_found_when_adding_mod", { this.localisationService.getText("bot-no_item_template_found_when_adding_mod", {
modId: modBeingAddedTemplate._id, modId: modBeingAddedDbTemplate?._id ?? "UNKNOWN",
modSlot: modSlot, modSlot: modSlot,
}), }),
); );
this.logger.debug(`Item -> ${parentTemplate._id}; Slot -> ${modSlot}`); this.logger.debug(`Item -> ${parentTemplate?._id}; Slot -> ${modSlot}`);
return false; return false;
} }
// Mod isn't a valid item // Mod has invalid db item
if (!modToAdd[0]) if (!modToAdd[0])
{ {
// Slot must be filled, show warning // Parent slot must be filled but db object is invalid, show warning and return false
if (slotAddedToTemplate._required) if (slotAddedToTemplate._required)
{ {
this.logger.warning( this.logger.warning(
this.localisationService.getText("bot-unable_to_add_mod_item_invalid", { this.localisationService.getText("bot-unable_to_add_mod_item_invalid", {
itemName: modBeingAddedTemplate._name, itemName: modBeingAddedDbTemplate?._name ?? "UNKNOWN",
modSlot: modSlot, modSlot: modSlot,
parentItemName: parentTemplate._name, parentItemName: parentTemplate._name,
botRole: botRole, botRole: botRole,
@ -1157,6 +1157,7 @@ export class BotEquipmentModGenerator
return false; return false;
} }
// Mod was found in db
return true; return true;
} }