Browse Source

refcell room id counter

pull/80/head
jake 3 years ago
parent
commit
3865caba8d
  1. 15
      src/ship/items/manager.rs

15
src/ship/items/manager.rs

@ -1,5 +1,6 @@
use crate::ship::items::ClientItemId; use crate::ship::items::ClientItemId;
use std::collections::HashMap; use std::collections::HashMap;
use std::cell::RefCell;
use thiserror::Error; use thiserror::Error;
use crate::entity::gateway::{EntityGateway, GatewayError}; use crate::entity::gateway::{EntityGateway, GatewayError};
use crate::entity::character::{CharacterEntity, CharacterEntityId, TechLevel}; use crate::entity::character::{CharacterEntity, CharacterEntityId, TechLevel};
@ -95,7 +96,7 @@ pub struct ItemManager {
pub(self) character_room: HashMap<CharacterEntityId, RoomId>, pub(self) character_room: HashMap<CharacterEntityId, RoomId>,
pub(self) room_floor: HashMap<RoomId, RoomFloorItems>, pub(self) room_floor: HashMap<RoomId, RoomFloorItems>,
pub(self) room_item_id_counter: HashMap<RoomId, Box<dyn FnMut() -> ClientItemId + Send>>,
pub(self) room_item_id_counter: RefCell<HashMap<RoomId, Box<dyn FnMut() -> ClientItemId + Send>>>,
} }
impl Default for ItemManager { impl Default for ItemManager {
@ -107,7 +108,7 @@ impl Default for ItemManager {
character_floor: HashMap::new(), character_floor: HashMap::new(),
character_room: HashMap::new(), character_room: HashMap::new(),
room_floor: 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); self.room_floor.entry(room_id).or_insert_with(RoomFloorItems::default);
let mut inc = 0x00810000; 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; inc += 1;
ClientItemId(inc) ClientItemId(inc)
})); }));
@ -361,7 +362,7 @@ impl ItemManager {
_ => unreachable!() // rust isnt smart enough to see that the conditional on tool catches everything _ => 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 { let floor_item = match item {
ItemOrMeseta::Individual(item_detail) => { ItemOrMeseta::Individual(item_detail) => {
let entity = entity_gateway.create_item(NewItemEntity { let entity = entity_gateway.create_item(NewItemEntity {
@ -482,7 +483,7 @@ impl ItemManager {
character.meseta -= amount; character.meseta -= amount;
entity_gateway.save_character(character).await?; 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 { let floor_item = FloorItem::Meseta(MesetaFloorItem {
item_id, item_id,
meseta: Meseta(amount), 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 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)) 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))?; .ok_or(ItemManagerError::CouldNotSplitItem(item_id))?;
@ -911,7 +912,7 @@ impl ItemManager {
entity_id, entity_id,
item_id, item_id,
item: ItemDetail::Weapon(weapon.clone()), item: ItemDetail::Weapon(weapon.clone()),
}))?;
}));
entity_gateway.set_character_inventory(&character.id, &inventory.as_inventory_entity(&character.id)).await?; entity_gateway.set_character_inventory(&character.id, &inventory.as_inventory_entity(&character.id)).await?;

Loading…
Cancel
Save