Fixed issue in updating item config when loading persisted items.

This commit is contained in:
Sam 2022-01-02 22:35:21 -05:00
parent 3d5e29b44d
commit af50864643
5 changed files with 37 additions and 20 deletions

View File

@ -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));
}

View File

@ -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)]

View File

@ -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 {

View File

@ -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)
}