From fe7d09e075662b53715409c96f9dd5b265bdcc04 Mon Sep 17 00:00:00 2001 From: Dev Date: Mon, 22 Apr 2024 09:39:32 +0100 Subject: [PATCH] Improved logic inside `isModValidForSlot()` to better handle edge cases --- .../generators/BotEquipmentModGenerator.ts | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/project/src/generators/BotEquipmentModGenerator.ts b/project/src/generators/BotEquipmentModGenerator.ts index 56bbcd2f..dc0612d5 100644 --- a/project/src/generators/BotEquipmentModGenerator.ts +++ b/project/src/generators/BotEquipmentModGenerator.ts @@ -1106,12 +1106,12 @@ export class BotEquipmentModGenerator } /** - * Log errors if mod is not compatible with slot - * @param modToAdd template of mod to check - * @param slotAddedToTemplate slot the item will be placed in - * @param modSlot slot the mod will fill - * @param parentTemplate template of the mods being added - * @param botRole + * Check if mod exists in db + is for a required slot + * @param modToAdd Db template of mod to check + * @param slotAddedToTemplate Slot object the item will be placed as child into + * @param modSlot Slot the mod will fill + * @param parentTemplate Db template of the mods being added + * @param botRole Bots wildspawntype (assault/pmcBot etc) * @returns true if valid */ protected isModValidForSlot( @@ -1122,31 +1122,31 @@ export class BotEquipmentModGenerator botRole: string, ): boolean { - const modBeingAddedTemplate = modToAdd[1]; + const modBeingAddedDbTemplate = modToAdd[1]; - // Mod lacks template item - if (!modBeingAddedTemplate) + // Mod lacks db template object + if (!modBeingAddedDbTemplate) { this.logger.error( this.localisationService.getText("bot-no_item_template_found_when_adding_mod", { - modId: modBeingAddedTemplate._id, + modId: modBeingAddedDbTemplate?._id ?? "UNKNOWN", modSlot: modSlot, }), ); - this.logger.debug(`Item -> ${parentTemplate._id}; Slot -> ${modSlot}`); + this.logger.debug(`Item -> ${parentTemplate?._id}; Slot -> ${modSlot}`); return false; } - // Mod isn't a valid item + // Mod has invalid db item 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) { this.logger.warning( this.localisationService.getText("bot-unable_to_add_mod_item_invalid", { - itemName: modBeingAddedTemplate._name, + itemName: modBeingAddedDbTemplate?._name ?? "UNKNOWN", modSlot: modSlot, parentItemName: parentTemplate._name, botRole: botRole, @@ -1157,6 +1157,7 @@ export class BotEquipmentModGenerator return false; } + // Mod was found in db return true; }