From af508646434f4c6316f11c3331ae3e9dbb72e88b Mon Sep 17 00:00:00 2001 From: Sam Date: Sun, 2 Jan 2022 22:35:21 -0500 Subject: [PATCH] Fixed issue in updating item config when loading persisted items. --- common/src/comp/inventory/item/mod.rs | 11 ++-------- common/src/comp/inventory/loadout.rs | 14 ++++++++++++- common/src/comp/inventory/mod.rs | 12 +++++++++++ ...r_weapons.sql => V47__modular_weapons.sql} | 0 .../src/persistence/character/conversions.rs | 20 +++++++++---------- 5 files changed, 37 insertions(+), 20 deletions(-) rename server/src/migrations/{V46__modular_weapons.sql => V47__modular_weapons.sql} (100%) diff --git a/common/src/comp/inventory/item/mod.rs b/common/src/comp/inventory/item/mod.rs index 2b932eed74..90fe87e5ef 100644 --- a/common/src/comp/inventory/item/mod.rs +++ b/common/src/comp/inventory/item/mod.rs @@ -820,22 +820,15 @@ impl Item { } } - pub fn persistence_access_add_component( - &mut self, - component: Item, - ability_map: &AbilityMap, - msm: &MaterialStatManifest, - ) { + pub fn persistence_access_add_component(&mut self, component: Item) { self.components.push(component); - // adding a component changes the stats, so recalculate the ItemConfig - self.update_item_config(ability_map, msm); } pub fn persistence_access_mutable_component(&mut self, index: usize) -> Option<&mut Self> { self.components.get_mut(index) } - fn update_item_config(&mut self, ability_map: &AbilityMap, msm: &MaterialStatManifest) { + pub fn update_item_config(&mut self, ability_map: &AbilityMap, msm: &MaterialStatManifest) { if let Ok(item_config) = ItemConfig::try_from((&*self, ability_map, msm)) { self.item_config = Some(Box::new(item_config)); } diff --git a/common/src/comp/inventory/loadout.rs b/common/src/comp/inventory/loadout.rs index 4a4260b628..4aaef69755 100644 --- a/common/src/comp/inventory/loadout.rs +++ b/common/src/comp/inventory/loadout.rs @@ -1,6 +1,6 @@ use crate::comp::{ inventory::{ - item::{tool::Tool, Hands, ItemKind}, + item::{self, tool::Tool, Hands, ItemKind}, slot::{ArmorSlot, EquipSlot}, InvSlot, }, @@ -402,6 +402,18 @@ impl Loadout { ); } } + + pub fn persistence_update_all_item_configs( + &mut self, + ability_map: &item::tool::AbilityMap, + msm: &item::tool::MaterialStatManifest, + ) { + self.slots.iter_mut().for_each(|slot| { + if let Some(item) = &mut slot.slot { + item.update_item_config(ability_map, msm); + } + }); + } } #[cfg(test)] diff --git a/common/src/comp/inventory/mod.rs b/common/src/comp/inventory/mod.rs index a7e3a2884e..4a62ea1245 100644 --- a/common/src/comp/inventory/mod.rs +++ b/common/src/comp/inventory/mod.rs @@ -783,6 +783,18 @@ impl Inventory { } pub fn swap_equipped_weapons(&mut self) { self.loadout.swap_equipped_weapons() } + + pub fn persistence_update_all_item_configs( + &mut self, + ability_map: &item::tool::AbilityMap, + msm: &item::tool::MaterialStatManifest, + ) { + self.slots_mut().for_each(|mut slot| { + if let Some(item) = &mut slot { + item.update_item_config(ability_map, msm); + } + }); + } } impl Component for Inventory { diff --git a/server/src/migrations/V46__modular_weapons.sql b/server/src/migrations/V47__modular_weapons.sql similarity index 100% rename from server/src/migrations/V46__modular_weapons.sql rename to server/src/migrations/V47__modular_weapons.sql diff --git a/server/src/persistence/character/conversions.rs b/server/src/persistence/character/conversions.rs index c80dc3c428..0aa8ecabac 100644 --- a/server/src/persistence/character/conversions.rs +++ b/server/src/persistence/character/conversions.rs @@ -402,11 +402,7 @@ pub fn convert_inventory_from_database_items( &mut inventory, &|inv, s| inv.slot_mut(slot(s).ok()?).and_then(|a| a.as_mut()), ) { - parent.persistence_access_add_component( - item, - &ABILITY_MAP, - &MATERIAL_STATS_MANIFEST, - ); + parent.persistence_access_add_component(item); } else { return Err(PersistenceError::ConversionError(format!( "Parent slot {} for component {} was empty even though it occurred earlier in \ @@ -422,6 +418,10 @@ pub fn convert_inventory_from_database_items( } } + // Some items may have had components added, so update the item config of each + // item to ensure that it correctly accounts for components that were added + inventory.persistence_update_all_item_configs(&ABILITY_MAP, &MATERIAL_STATS_MANIFEST); + Ok(inventory) } @@ -465,11 +465,7 @@ pub fn convert_loadout_from_database_items( l.get_mut_item_at_slot_using_persistence_key(s).ok() }) { - parent.persistence_access_add_component( - item, - &ABILITY_MAP, - &MATERIAL_STATS_MANIFEST, - ); + parent.persistence_access_add_component(item); } else { return Err(PersistenceError::ConversionError(format!( "Parent slot {} for component {} was empty even though it occurred earlier in \ @@ -485,6 +481,10 @@ pub fn convert_loadout_from_database_items( } } + // Some items may have had components added, so update the item config of each + // item to ensure that it correctly accounts for components that were added + loadout.persistence_update_all_item_configs(&ABILITY_MAP, &MATERIAL_STATS_MANIFEST); + Ok(loadout) }