Browse Source

lint src/ship/items/*

pull/42/head
jake 3 years ago
parent
commit
3457996afd
  1. 4
      src/ship/item_stats.rs
  2. 7
      src/ship/items/bank.rs
  3. 8
      src/ship/items/floor.rs
  4. 48
      src/ship/items/inventory.rs
  5. 69
      src/ship/items/manager.rs
  6. 2
      src/ship/ship.rs

4
src/ship/item_stats.rs

@ -172,9 +172,9 @@ pub fn mag_stats() -> HashMap<MagType, MagStats> {
pub fn mag_feed_tables() -> MagFeedTables {
let mag_feed_tables: BTreeMap<String, Vec<BTreeMap<String, MagFeedStats>>> = load_data_file("data/item_stats/mag_feed_table.toml");
MagFeedTables(mag_feed_tables.get("feedtable").unwrap().into_iter()
MagFeedTables(mag_feed_tables.get("feedtable").unwrap().iter()
.map(|feed_table| {
MagFeedTable(feed_table.into_iter()
MagFeedTable(feed_table.iter()
.map(|(tool, feed_stats)| {
(tool.parse().unwrap(), *feed_stats)
}).collect())

7
src/ship/items/bank.rs

@ -178,13 +178,12 @@ impl CharacterBank {
self.item_id_counter = base_item_id + self.items.len() as u32 + 1;
}
pub fn get_item_handle_by_id<'a>(&'a mut self, item_id: ClientItemId) -> Option<BankItemHandle<'a>> {
pub fn get_item_handle_by_id(&mut self, item_id: ClientItemId) -> Option<BankItemHandle> {
let (index, _) = self.items.iter()
.enumerate()
.filter(|(_, item)| {
.find(|(_, item)| {
item.item_id() == item_id
})
.nth(0)?;
})?;
Some(BankItemHandle {
bank: self,
index: index,

8
src/ship/items/floor.rs

@ -162,11 +162,13 @@ impl<'a> FloorItemHandle<'a> {
#[derive(Debug)]
pub struct RoomFloorItems(Vec<FloorItem>);
impl RoomFloorItems {
pub fn new() -> RoomFloorItems {
impl Default for RoomFloorItems {
fn default() -> RoomFloorItems {
RoomFloorItems(Vec::new())
}
}
impl RoomFloorItems {
pub fn add_item(&mut self, item: FloorItem) {
self.0.push(item);
}
@ -186,7 +188,7 @@ impl RoomFloorItems {
pub fn take_item_by_id(&mut self, item_id: ClientItemId) -> Option<FloorItem> {
self.0
.drain_filter(|i| i.item_id() == item_id)
.nth(0)
.next()
}
pub fn drop_individual_inventory_item(&mut self, individual_inventory_item: IndividualInventoryItem, item_drop_location: (MapArea, f32, f32, f32)) -> &IndividualFloorItem {

48
src/ship/items/inventory.rs

@ -158,12 +158,8 @@ impl InventoryItem {
// TODO: result
// TOOD: delete?
pub fn combine_stacks(&mut self, other_stacked_item: &mut StackedFloorItem) {
match self {
InventoryItem::Stacked(self_stacked_item) => {
self_stacked_item.entity_ids.append(&mut other_stacked_item.entity_ids);
},
_ => {
}
if let InventoryItem::Stacked(self_stacked_item) = self {
self_stacked_item.entity_ids.append(&mut other_stacked_item.entity_ids);
}
}
@ -417,85 +413,80 @@ impl CharacterInventory {
self.items.len()
}
pub fn get_item_handle_by_id<'a>(&'a mut self, item_id: ClientItemId) -> Option<InventoryItemHandle<'a>> {
pub fn get_item_handle_by_id(&mut self, item_id: ClientItemId) -> Option<InventoryItemHandle> {
let (slot, _) = self.items.iter()
.enumerate()
.filter(|(_, item)| {
.find(|(_, item)| {
item.item_id() == item_id
})
.nth(0)?;
})?;
Some(InventoryItemHandle {
inventory: self,
slot: slot,
})
}
pub fn get_equipped_mag_handle<'a>(&'a mut self) -> Option<InventoryItemHandle<'a>> {
pub fn get_equipped_mag_handle(&mut self) -> Option<InventoryItemHandle> {
let (slot, _) = self.items.iter()
.enumerate()
.filter(|(_, item)| {
.find(|(_, item)| {
if let InventoryItem::Individual(individual_inventory_item) = item {
if let ItemDetail::Mag(_) = &individual_inventory_item.item {
return self.equipped.is_equipped(&individual_inventory_item.entity_id)
}
}
false
})
.nth(0)?;
})?;
Some(InventoryItemHandle {
inventory: self,
slot: slot,
})
}
pub fn get_equipped_armor_handle<'a>(&'a mut self) -> Option<InventoryItemHandle<'a>> {
pub fn get_equipped_armor_handle(&mut self) -> Option<InventoryItemHandle> {
let (slot, _) = self.items.iter()
.enumerate()
.filter(|(_, item)| {
.find(|(_, item)| {
if let InventoryItem::Individual(individual_inventory_item) = item {
if let ItemDetail::Armor(_) = &individual_inventory_item.item {
return self.equipped.is_equipped(&individual_inventory_item.entity_id)
}
}
false
})
.nth(0)?;
})?;
Some(InventoryItemHandle {
inventory: self,
slot: slot,
})
}
pub fn get_equipped_shield_handle<'a>(&'a mut self) -> Option<InventoryItemHandle<'a>> {
pub fn get_equipped_shield_handle(&mut self) -> Option<InventoryItemHandle> {
let (slot, _) = self.items.iter()
.enumerate()
.filter(|(_, item)| {
.find(|(_, item)| {
if let InventoryItem::Individual(individual_inventory_item) = item {
if let ItemDetail::Shield(_) = &individual_inventory_item.item {
return self.equipped.is_equipped(&individual_inventory_item.entity_id)
}
}
false
})
.nth(0)?;
})?;
Some(InventoryItemHandle {
inventory: self,
slot: slot,
})
}
pub fn get_equipped_weapon_handle<'a>(&'a mut self) -> Option<InventoryItemHandle<'a>> {
pub fn get_equipped_weapon_handle(&mut self) -> Option<InventoryItemHandle> {
let (slot, _) = self.items.iter()
.enumerate()
.filter(|(_, item)| {
.find(|(_, item)| {
if let InventoryItem::Individual(individual_inventory_item) = item {
if let ItemDetail::Weapon(_) = &individual_inventory_item.item {
return self.equipped.is_equipped(&individual_inventory_item.entity_id)
}
}
false
})
.nth(0)?;
})?;
Some(InventoryItemHandle {
inventory: self,
slot: slot,
@ -504,16 +495,15 @@ impl CharacterInventory {
pub fn get_item_by_id(&self, item_id: ClientItemId) -> Option<&InventoryItem> {
self.items.iter()
.filter(|item| {
.find(|item| {
item.item_id() == item_id
})
.nth(0)
}
pub fn take_item_by_id(&mut self, item_id: ClientItemId) -> Option<InventoryItem> {
self.items
.drain_filter(|i| i.item_id() == item_id)
.nth(0)
.next()
}
pub fn add_item(&mut self, item: InventoryItem) -> Result<(), InventoryAddError> { // TODO: errors

69
src/ship/items/manager.rs

@ -71,8 +71,8 @@ pub struct ItemManager {
room_item_id_counter: HashMap<RoomId, Box<dyn FnMut() -> ClientItemId + Send>>,
}
impl ItemManager {
pub fn new() -> ItemManager {
impl Default for ItemManager {
fn default() -> ItemManager {
ItemManager {
id_counter: 0,
character_inventory: HashMap::new(),
@ -83,7 +83,9 @@ impl ItemManager {
room_item_id_counter: HashMap::new(),
}
}
}
impl ItemManager {
pub fn next_global_item_id(&mut self) -> ClientItemId {
self.id_counter += 1;
ClientItemId(self.id_counter)
@ -110,11 +112,11 @@ impl ItemManager {
entity_ids: items.iter().map(|i| i.id).collect(),
item_id: self.next_global_item_id(),
tool: items.get(0)
.ok_or(ItemManagerError::StackedItemError(items.clone()))?
.ok_or_else(|| ItemManagerError::StackedItemError(items.clone()))?
.item
.clone()
.as_tool()
.ok_or(ItemManagerError::StackedItemError(items.clone()))?
.ok_or_else(|| ItemManagerError::StackedItemError(items.clone()))?
})
},
})
@ -137,11 +139,11 @@ impl ItemManager {
entity_ids: items.iter().map(|i| i.id).collect(),
item_id: self.next_global_item_id(),
tool: items.get(0)
.ok_or(ItemManagerError::StackedItemError(items.clone()))?
.ok_or_else(|| ItemManagerError::StackedItemError(items.clone()))?
.item
.clone()
.as_tool()
.ok_or(ItemManagerError::StackedItemError(items.clone()))?
.ok_or_else(|| ItemManagerError::StackedItemError(items.clone()))?
})
},
})
@ -160,18 +162,15 @@ impl ItemManager {
inventory.initialize_item_ids(base_inventory_id);
let base_bank_id = ((area_client.local_client.id() as u32) << 21) | 0x20000;
let default_bank = self.character_bank.get_mut(&character.id);
match default_bank {
Some(default_bank) => {
default_bank.initialize_item_ids(base_bank_id);
},
None => {},
if let Some(default_bank ) = default_bank {
default_bank.initialize_item_ids(base_bank_id);
}
self.character_room.insert(character.id, room_id);
self.character_floor.insert(character.id, RoomFloorItems::new());
self.room_floor.entry(room_id).or_insert(RoomFloorItems::new());
self.character_floor.insert(character.id, RoomFloorItems::default());
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(Box::new(move || {
self.room_item_id_counter.entry(room_id).or_insert_with(|| Box::new(move || {
inc += 1;
ClientItemId(inc)
}));
@ -202,13 +201,11 @@ impl ItemManager {
pub fn remove_character_from_room(&mut self, character: &CharacterEntity) {
self.character_inventory.remove(&character.id);
self.character_floor.remove(&character.id);
self.character_room.remove(&character.id)
.as_ref()
.map(|room| {
if self.character_room.iter().find(|(_, r)| *r == room).is_none() {
self.room_floor.remove(room);
}
});
if let Some(room) = self.character_room.remove(&character.id).as_ref() {
if self.character_room.iter().any(|(_, r)| r == room) {
self.room_floor.remove(room);
}
}
}
pub fn get_floor_item_by_id(&self, character: &CharacterEntity, item_id: ClientItemId) -> Result<(&FloorItem, FloorType), anyhow::Error> {
@ -220,7 +217,7 @@ impl ItemManager {
.or_else(|| {
shared_floor.get_item_by_id(item_id).map(|item| (item, FloorType::Shared))
})
.ok_or(ItemManagerError::NoSuchItemId(item_id).into())
.ok_or_else(|| ItemManagerError::NoSuchItemId(item_id).into())
}
pub async fn character_picks_up_item<EG: EntityGateway>(&mut self, entity_gateway: &mut EG, character: &mut CharacterEntity, item_id: ClientItemId)
@ -228,7 +225,7 @@ impl ItemManager {
let local_floor = self.character_floor.get_mut(&character.id).ok_or(ItemManagerError::NoCharacter(character.id))?;
let inventory = self.character_inventory.get_mut(&character.id).ok_or(ItemManagerError::NoCharacter(character.id))?;
let room_id = self.character_room.get(&character.id).ok_or(ItemManagerError::NoCharacter(character.id))?;
let shared_floor = self.room_floor.get_mut(&room_id).ok_or(ItemManagerError::NoCharacter(character.id))?;
let shared_floor = self.room_floor.get_mut(room_id).ok_or(ItemManagerError::NoCharacter(character.id))?;
let floor_item = local_floor.get_item_handle_by_id(item_id)
.or_else(|| {
@ -238,7 +235,7 @@ impl ItemManager {
let trigger_create_item = match floor_item.item() {
Some(FloorItem::Individual(individual_floor_item)) => {
let new_inventory_item = inventory.pick_up_individual_floor_item(&individual_floor_item);
let new_inventory_item = inventory.pick_up_individual_floor_item(individual_floor_item);
match new_inventory_item {
Some((new_inventory_item, slot)) => {
entity_gateway.change_item_location(
@ -247,7 +244,7 @@ impl ItemManager {
character_id: character.id,
}
).await?;
if let Some(_) = new_inventory_item.mag() {
if new_inventory_item.mag().is_some() {
entity_gateway.change_mag_owner(&new_inventory_item.entity_id, character).await?;
}
},
@ -258,13 +255,13 @@ impl ItemManager {
TriggerCreateItem::Yes
},
Some(FloorItem::Stacked(stacked_floor_item)) => {
let new_inventory_item = inventory.pick_up_stacked_floor_item(&stacked_floor_item);
let new_inventory_item = inventory.pick_up_stacked_floor_item(stacked_floor_item);
match new_inventory_item {
Some((new_inventory_item, slot)) => {
for entity_id in &new_inventory_item.entity_ids {
entity_gateway.change_item_location(
&entity_id,
entity_id,
ItemLocation::Inventory {
character_id: character.id,
}
@ -288,7 +285,7 @@ impl ItemManager {
return Err(ItemManagerError::CouldNotAddToInventory(item_id).into());
}
character.meseta = std::cmp::min(character.meseta + meseta_floor_item.meseta.0, 999999);
entity_gateway.save_character(&character).await?;
entity_gateway.save_character(character).await?;
TriggerCreateItem::No
},
None => {
@ -381,9 +378,9 @@ impl ItemManager {
},
};
self.character_floor.entry(character.id).or_insert(RoomFloorItems::new()).add_item(floor_item);
self.character_floor.entry(character.id).or_insert_with(RoomFloorItems::default).add_item(floor_item);
// TODO: make these real errors
self.character_floor.get(&character.id).ok_or(ItemManagerError::Idunnoman)?.get_item_by_id(item_id).ok_or(ItemManagerError::Idunnoman.into())
self.character_floor.get(&character.id).ok_or(ItemManagerError::Idunnoman)?.get_item_by_id(item_id).ok_or_else(|| ItemManagerError::Idunnoman.into())
}
pub async fn player_drop_item_on_shared_floor<EG: EntityGateway>(&mut self,
@ -395,7 +392,7 @@ impl ItemManager {
-> Result<(), anyhow::Error> {
let inventory = self.character_inventory.get_mut(&character.id).ok_or(ItemManagerError::NoCharacter(character.id))?;
let room_id = self.character_room.get(&character.id).ok_or(ItemManagerError::NoCharacter(character.id))?;
let shared_floor = self.room_floor.get_mut(&room_id).ok_or(ItemManagerError::NoCharacter(character.id))?;
let shared_floor = self.room_floor.get_mut(room_id).ok_or(ItemManagerError::NoCharacter(character.id))?;
let dropped_inventory_item = inventory.take_item_by_id(item_id).ok_or(ItemManagerError::NoSuchItemId(item_id))?;
@ -439,12 +436,12 @@ impl ItemManager {
amount: u32)
-> Result<FloorItem, anyhow::Error> {
let room_id = self.character_room.get(&character.id).ok_or(ItemManagerError::NoCharacter(character.id))?;
let shared_floor = self.room_floor.get_mut(&room_id).ok_or(ItemManagerError::NoCharacter(character.id))?;
let shared_floor = self.room_floor.get_mut(room_id).ok_or(ItemManagerError::NoCharacter(character.id))?;
if character.meseta < amount {
return Err(ItemManagerError::CouldNotDropMeseta.into())
}
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 floor_item = FloorItem::Meseta(MesetaFloorItem {
@ -470,7 +467,7 @@ impl ItemManager {
-> Result<&StackedFloorItem, anyhow::Error> {
let inventory = self.character_inventory.get_mut(&character.id).ok_or(ItemManagerError::NoCharacter(character.id))?;
let room_id = self.character_room.get(&character.id).ok_or(ItemManagerError::NoCharacter(character.id))?;
let shared_floor = self.room_floor.get_mut(&room_id).ok_or(ItemManagerError::NoCharacter(character.id))?;
let shared_floor = self.room_floor.get_mut(room_id).ok_or(ItemManagerError::NoCharacter(character.id))?;
let item_to_split = inventory.get_item_handle_by_id(item_id).ok_or(ItemManagerError::NoSuchItemId(item_id))?;
@ -594,7 +591,7 @@ impl ItemManager {
entity_gateway.set_character_inventory(&character.id, &inventory.as_inventory_entity(&character.id)).await?;
entity_gateway.set_character_bank(&character.id, &bank.as_bank_entity(&character.id, &BankName("".into())), BankName("".into())).await?;
inventory.slot(inventory_item_slot).ok_or(ItemManagerError::Idunnoman.into())
inventory.slot(inventory_item_slot).ok_or_else(|| ItemManagerError::Idunnoman.into())
}
pub async fn player_feeds_mag_item<EG: EntityGateway>(&mut self,
@ -881,7 +878,7 @@ impl ItemManager {
let sorted_inventory_items: Vec<InventoryItem> = item_ids.iter()
.filter(|&client_item_id| *client_item_id < 0xFFFFFFFF)
.map(|&client_item_id| inventory.get_item_by_id(ClientItemId(client_item_id)))
.filter(|&x| x.is_some() == true)
.filter(|&x| x.is_some())
.map(|x| x.cloned().unwrap())
.collect();

2
src/ship/ship.rs

@ -367,7 +367,7 @@ impl<EG: EntityGateway> ShipServerStateBuilder<EG> {
clients: HashMap::new(),
level_table: CharacterLevelTable::new(),
name: self.name.unwrap_or("NAMENOTSET".into()),
item_manager: items::ItemManager::new(),
item_manager: items::ItemManager::default(),
quests: quests::load_quests("data/quests.toml".into()).unwrap(),
ip: self.ip.unwrap_or(Ipv4Addr::new(127,0,0,1)),
port: self.port.unwrap_or(SHIP_PORT),

Loading…
Cancel
Save