Addressed review comments

This commit is contained in:
Sam 2024-01-21 21:33:39 -05:00
parent b87d1b649d
commit 394e798d04
2 changed files with 34 additions and 48 deletions

View File

@ -1175,6 +1175,7 @@ impl Client {
pub fn split_swap_slots(&mut self, a: Slot, b: Slot) { pub fn split_swap_slots(&mut self, a: Slot, b: Slot) {
match (a, b) { match (a, b) {
(Slot::Overflow(_), _) | (_, Slot::Overflow(_)) => {},
(Slot::Equip(equip), slot) | (slot, Slot::Equip(equip)) => self.control_action( (Slot::Equip(equip), slot) | (slot, Slot::Equip(equip)) => self.control_action(
ControlAction::InventoryAction(InventoryAction::Swap(equip, slot)), ControlAction::InventoryAction(InventoryAction::Swap(equip, slot)),
), ),
@ -1183,7 +1184,6 @@ impl Client {
InventoryEvent::SplitSwap(inv1, inv2), InventoryEvent::SplitSwap(inv1, inv2),
))) )))
}, },
(Slot::Overflow(_), _) | (_, Slot::Overflow(_)) => {},
} }
} }

View File

@ -372,23 +372,7 @@ pub fn convert_inventory_from_database_items(
let mut inventory = Inventory::with_loadout_humanoid(loadout); let mut inventory = Inventory::with_loadout_humanoid(loadout);
let mut item_indices = HashMap::new(); let mut item_indices = HashMap::new();
struct FailedInserts { let mut failed_inserts = HashMap::new();
items: Vec<VelorenItem>,
map: HashMap<String, usize>,
}
impl FailedInserts {
fn insert(&mut self, db_pos: String, item: VelorenItem) {
let i = self.items.len();
self.items.push(item);
self.map.insert(db_pos, i);
}
}
let mut failed_inserts = FailedInserts {
items: Vec::new(),
map: HashMap::new(),
};
// In order to items with components to properly load, it is important that this // In order to items with components to properly load, it is important that this
// item iteration occurs in order so that any modular items are loaded before // item iteration occurs in order so that any modular items are loaded before
@ -439,30 +423,38 @@ pub fn convert_inventory_from_database_items(
}; };
if db_item.parent_container_item_id == inventory_container_id { if db_item.parent_container_item_id == inventory_container_id {
if let Ok(slot) = slot(&db_item.position) { if db_item.position.contains("overflow_item") {
let insert_res = inventory.insert_at(slot, item); failed_inserts.insert(db_item.position.clone(), item);
} else {
match slot(&db_item.position) {
Ok(slot) => {
let insert_res = inventory.insert_at(slot, item);
match insert_res { match insert_res {
Ok(None) => { Ok(None) => {
// Insert successful // Insert successful
},
Ok(Some(_item)) => {
// If inventory.insert returns an item, it means it was swapped for
// an item that already occupied the
// slot. Multiple items being stored
// in the database for the same slot is
// an error.
return Err(PersistenceError::ConversionError(
"Inserted an item into the same slot twice".to_string(),
));
},
Err(item) => {
// If this happens there were too many items in the database for the
// current inventory size
failed_inserts.insert(db_item.position.clone(), item);
},
}
}, },
Ok(Some(_item)) => { Err(err) => {
// If inventory.insert returns an item, it means it was swapped for an item return Err(err);
// that already occupied the slot. Multiple items
// being stored in the database for the same slot is
// an error.
return Err(PersistenceError::ConversionError(
"Inserted an item into the same slot twice".to_string(),
));
},
Err(item) => {
// If this happens there were too many items in the database for the current
// inventory size
failed_inserts.insert(db_item.position.clone(), item);
}, },
} }
} else {
failed_inserts.insert(db_item.position.clone(), item);
} }
} else if let Some(&j) = item_indices.get(&db_item.parent_container_item_id) { } else if let Some(&j) = item_indices.get(&db_item.parent_container_item_id) {
get_mutable_item( get_mutable_item(
@ -470,21 +462,15 @@ pub fn convert_inventory_from_database_items(
inventory_items, inventory_items,
&item_indices, &item_indices,
&mut (&mut inventory, &mut failed_inserts), &mut (&mut inventory, &mut failed_inserts),
&|(inv, f_i): &mut (&mut Inventory, &mut FailedInserts), s| { &|(inv, f_i): &mut (&mut Inventory, &mut HashMap<String, VelorenItem>), s| {
// Attempts first to access inventory if that slot exists there. If it does not // Attempts first to access inventory if that slot exists there. If it does not
// it instead attempts to access failed inserts list. // it instead attempts to access failed inserts list.
slot(s) slot(s)
.ok() .ok()
.and_then(|slot| inv.slot_mut(slot)) .and_then(|slot| inv.slot_mut(slot))
.and_then(|a| a.as_mut()) .and_then(|a| a.as_mut())
.or(f_i.map.get(s).and_then(|i| f_i.items.get_mut(*i))) // .or_else(f_i.map.get(s).and_then(|i| f_i.items.get_mut(*i)))
// if let Ok(slot) = slot(s) { .or_else(|| f_i.get_mut(s))
// dbg!(0);
// inv.slot_mut(slot).and_then(|a| a.as_mut())
// } else {
// dbg!(1);
// f_i.map.get(s).and_then(|i| f_i.items.get_mut(*i))
// }
}, },
)? )?
.persistence_access_add_component(item); .persistence_access_add_component(item);
@ -498,7 +484,7 @@ pub fn convert_inventory_from_database_items(
// For failed inserts, attempt to push to inventory. If push fails, move to // For failed inserts, attempt to push to inventory. If push fails, move to
// overflow slots. // overflow slots.
if let Err(inv_error) = inventory.push_all(failed_inserts.items.into_iter()) { if let Err(inv_error) = inventory.push_all(failed_inserts.into_values()) {
inventory.persistence_push_overflow_items(inv_error.returned_items()); inventory.persistence_push_overflow_items(inv_error.returned_items());
} }