From 3865caba8d1454de9c8c9a61bff3d2ff65d07588 Mon Sep 17 00:00:00 2001 From: jake Date: Fri, 10 Dec 2021 13:19:22 -0700 Subject: [PATCH] refcell room id counter --- src/ship/items/manager.rs | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/ship/items/manager.rs b/src/ship/items/manager.rs index 701a4aa..0f0b1b2 100644 --- a/src/ship/items/manager.rs +++ b/src/ship/items/manager.rs @@ -1,5 +1,6 @@ use crate::ship::items::ClientItemId; use std::collections::HashMap; +use std::cell::RefCell; use thiserror::Error; use crate::entity::gateway::{EntityGateway, GatewayError}; use crate::entity::character::{CharacterEntity, CharacterEntityId, TechLevel}; @@ -95,7 +96,7 @@ pub struct ItemManager { pub(self) character_room: HashMap, pub(self) room_floor: HashMap, - pub(self) room_item_id_counter: HashMap ClientItemId + Send>>, + pub(self) room_item_id_counter: RefCell ClientItemId + Send>>>, } impl Default for ItemManager { @@ -107,7 +108,7 @@ impl Default for ItemManager { character_floor: HashMap::new(), character_room: HashMap::new(), room_floor: HashMap::new(), - room_item_id_counter: HashMap::new(), + room_item_id_counter: RefCell::new(HashMap::new()), } } } @@ -196,7 +197,7 @@ impl ItemManager { self.room_floor.entry(room_id).or_insert_with(RoomFloorItems::default); let mut inc = 0x00810000; - self.room_item_id_counter.entry(room_id).or_insert_with(|| Box::new(move || { + self.room_item_id_counter.borrow_mut().entry(room_id).or_insert_with(|| Box::new(move || { inc += 1; ClientItemId(inc) })); @@ -361,7 +362,7 @@ impl ItemManager { _ => unreachable!() // rust isnt smart enough to see that the conditional on tool catches everything }; - let item_id = self.room_item_id_counter.get_mut(room_id).ok_or(ItemManagerError::NoCharacter(character.id))?(); + let item_id = self.room_item_id_counter.borrow_mut().get_mut(room_id).ok_or(ItemManagerError::NoCharacter(character.id))?(); let floor_item = match item { ItemOrMeseta::Individual(item_detail) => { let entity = entity_gateway.create_item(NewItemEntity { @@ -482,7 +483,7 @@ impl ItemManager { character.meseta -= amount; entity_gateway.save_character(character).await?; - let item_id = self.room_item_id_counter.get_mut(room_id).ok_or(ItemManagerError::NoCharacter(character.id))?(); + let item_id = self.room_item_id_counter.borrow_mut().get_mut(room_id).ok_or(ItemManagerError::NoCharacter(character.id))?(); let floor_item = FloorItem::Meseta(MesetaFloorItem { item_id, meseta: Meseta(amount), @@ -510,7 +511,7 @@ impl ItemManager { let item_to_split = inventory.get_item_handle_by_id(item_id).ok_or(ItemManagerError::NoSuchItemId(item_id))?; - let new_item_id = self.room_item_id_counter.get_mut(room_id).ok_or(ItemManagerError::NoCharacter(character.id))?(); + let new_item_id = self.room_item_id_counter.borrow_mut().get_mut(room_id).ok_or(ItemManagerError::NoCharacter(character.id))?(); let stacked_floor_item = shared_floor.drop_partial_stacked_inventory_item(item_to_split, amount, new_item_id, (drop_location.map_area, drop_location.x, 0.0, drop_location.z)) .ok_or(ItemManagerError::CouldNotSplitItem(item_id))?; @@ -911,7 +912,7 @@ impl ItemManager { entity_id, item_id, item: ItemDetail::Weapon(weapon.clone()), - }))?; + })); entity_gateway.set_character_inventory(&character.id, &inventory.as_inventory_entity(&character.id)).await?;