From b09ae94c67af9bf0a3d46d305419912aff817243 Mon Sep 17 00:00:00 2001 From: Chomp Date: Thu, 6 Feb 2025 09:56:31 +0000 Subject: [PATCH] Reduce cognitive load of `PopulateParentAttachmentsMap ` --- .../Core/Controllers/InsuranceController.cs | 90 ++++++++++--------- 1 file changed, 46 insertions(+), 44 deletions(-) diff --git a/Libraries/Core/Controllers/InsuranceController.cs b/Libraries/Core/Controllers/InsuranceController.cs index d877f04e..56872242 100644 --- a/Libraries/Core/Controllers/InsuranceController.cs +++ b/Libraries/Core/Controllers/InsuranceController.cs @@ -245,55 +245,57 @@ public class InsuranceController( continue; } - // Check if this is an attachment currently attached to its parent. - if (_itemHelper.IsAttachmentAttached(insuredItem)) + // Not attached to parent, skip + if (!_itemHelper.IsAttachmentAttached(insuredItem)) { - // Make sure the template for the item exists. - if (!_itemHelper.GetItem(insuredItem.Template).Key) - { - _logger.Warning( - _localisationService.GetText( - "insurance-unable_to_find_attachment_in_db", - new - { - insuredItemId = insuredItem.Id, - insuredItemTpl = insuredItem.Template - } - ) - ); + continue; + } - continue; - } + // Make sure the template for the item exists. + if (!_itemHelper.GetItem(insuredItem.Template).Key) + { + _logger.Warning( + _localisationService.GetText( + "insurance-unable_to_find_attachment_in_db", + new + { + insuredItemId = insuredItem.Id, + insuredItemTpl = insuredItem.Template + } + ) + ); - // Get the main parent of this attachment. (e.g., The gun that this suppressor is attached to.) - var mainParent = _itemHelper.GetAttachmentMainParent(insuredItem.Id, itemsMap); - if (mainParent is null) - { - // Odd. The parent couldn't be found. Skip this attachment and warn. - _logger.Warning( - _localisationService.GetText( - "insurance-unable_to_find_main_parent_for_attachment", - new - { - insuredItemId = insuredItem.Id, - insuredItemTpl = insuredItem.Template, - parentId = insuredItem.ParentId - } - ) - ); + continue; + } - continue; - } + // Get the main parent of this attachment. (e.g., The gun that this suppressor is attached to.) + var mainParent = _itemHelper.GetAttachmentMainParent(insuredItem.Id, itemsMap); + if (mainParent is null) + { + // Odd. The parent couldn't be found. Skip this attachment and warn. + _logger.Warning( + _localisationService.GetText( + "insurance-unable_to_find_main_parent_for_attachment", + new + { + insuredItemId = insuredItem.Id, + insuredItemTpl = insuredItem.Template, + parentId = insuredItem.ParentId + } + ) + ); - // Update (or add to) the main-parent to attachments map. - if (mainParentToAttachmentsMap.ContainsKey(mainParent.Id)) - { - if (mainParentToAttachmentsMap.TryGetValue(mainParent.Id, out var parent)) parent.Add(insuredItem); - } - else - { - mainParentToAttachmentsMap.TryAdd(mainParent.Id, [insuredItem]); - } + continue; + } + + // Update (or add to) the main-parent to attachments map. + if (mainParentToAttachmentsMap.ContainsKey(mainParent.Id)) + { + if (mainParentToAttachmentsMap.TryGetValue(mainParent.Id, out var parent)) parent.Add(insuredItem); + } + else + { + mainParentToAttachmentsMap.TryAdd(mainParent.Id, [insuredItem]); } }