From a3cc29b68f17f385e833015710f36fe695af37ef Mon Sep 17 00:00:00 2001 From: jake Date: Mon, 16 Nov 2020 21:27:03 -0700 Subject: [PATCH] use anyhow for itemmanager --- src/ship/items/manager.rs | 38 +++++++++++------------ src/ship/packet/handler/auth.rs | 2 +- src/ship/packet/handler/direct_message.rs | 24 +++++++------- src/ship/packet/handler/lobby.rs | 6 ++-- src/ship/packet/handler/message.rs | 30 +++++++++--------- 5 files changed, 50 insertions(+), 50 deletions(-) diff --git a/src/ship/items/manager.rs b/src/ship/items/manager.rs index b0d4a12..d102a4b 100644 --- a/src/ship/items/manager.rs +++ b/src/ship/items/manager.rs @@ -89,7 +89,7 @@ impl ItemManager { } // TODO: Result - pub async fn load_character(&mut self, entity_gateway: &mut EG, character: &CharacterEntity) -> Result<(), ItemManagerError> { + pub async fn load_character(&mut self, entity_gateway: &mut EG, character: &CharacterEntity) -> Result<(), anyhow::Error> { let inventory = entity_gateway.get_character_inventory(&character.id).await?; let bank = entity_gateway.get_character_bank(&character.id, BankName("".into())).await?; let equipped = entity_gateway.get_character_equips(&character.id).await?; @@ -176,12 +176,12 @@ impl ItemManager { })); } - pub fn get_character_inventory(&self, character: &CharacterEntity) -> Result<&CharacterInventory, ItemManagerError> { + pub fn get_character_inventory(&self, character: &CharacterEntity) -> Result<&CharacterInventory, anyhow::Error> { Ok(self.character_inventory.get(&character.id) .ok_or(ItemManagerError::NoCharacter(character.id))?) } - pub fn get_character_bank(&self, character: &CharacterEntity) -> Result<&CharacterBank, ItemManagerError> { + pub fn get_character_bank(&self, character: &CharacterEntity) -> Result<&CharacterBank, anyhow::Error> { Ok(self.character_bank .get(&character.id) .ok_or(ItemManagerError::NoCharacter(character.id))?) @@ -210,7 +210,7 @@ impl ItemManager { }); } - pub fn get_floor_item_by_id(&self, character: &CharacterEntity, item_id: ClientItemId) -> Result<(&FloorItem, FloorType), ItemManagerError> { + pub fn get_floor_item_by_id(&self, character: &CharacterEntity, item_id: ClientItemId) -> Result<(&FloorItem, FloorType), anyhow::Error> { let local_floor = self.character_floor.get(&character.id).ok_or(ItemManagerError::NoCharacter(character.id))?; let room = self.character_room.get(&character.id).ok_or(ItemManagerError::NoCharacter(character.id))?; let shared_floor = self.room_floor.get(room).ok_or(ItemManagerError::NoCharacter(character.id))?; @@ -219,7 +219,7 @@ impl ItemManager { .or_else(|| { shared_floor.get_item_by_id(item_id).map(|item| (item, FloorType::Shared)) }) - .ok_or(ItemManagerError::NoSuchItemId(item_id)) + .ok_or(ItemManagerError::NoSuchItemId(item_id).into()) } pub async fn character_picks_up_item(&mut self, entity_gateway: &mut EG, character: &mut CharacterEntity, item_id: ClientItemId) @@ -300,7 +300,7 @@ impl ItemManager { Ok(trigger_create_item) } - pub async fn enemy_drop_item_on_local_floor(&mut self, entity_gateway: &mut EG, character: &CharacterEntity, item_drop: ItemDrop) -> Result<&FloorItem, ItemManagerError> { + pub async fn enemy_drop_item_on_local_floor(&mut self, entity_gateway: &mut EG, character: &CharacterEntity, item_drop: ItemDrop) -> Result<&FloorItem, anyhow::Error> { let room_id = self.character_room.get(&character.id).ok_or(ItemManagerError::NoCharacter(character.id))?; enum ItemOrMeseta { @@ -382,7 +382,7 @@ impl ItemManager { self.character_floor.entry(character.id).or_insert(RoomFloorItems::new()).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) + self.character_floor.get(&character.id).ok_or(ItemManagerError::Idunnoman)?.get_item_by_id(item_id).ok_or(ItemManagerError::Idunnoman.into()) } pub async fn player_drop_item_on_shared_floor(&mut self, @@ -466,7 +466,7 @@ impl ItemManager { item_id: ClientItemId, drop_location: ItemDropLocation, amount: usize) - -> Result<&StackedFloorItem, ItemManagerError> { + -> 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))?; @@ -498,7 +498,7 @@ impl ItemManager { character: &mut CharacterEntity, item_id: ClientItemId, amount: usize) - -> Result { + -> Result { let inventory = self.character_inventory.get_mut(&character.id).ok_or(ItemManagerError::NoCharacter(character.id))?; let used_item = inventory.get_item_handle_by_id(item_id).ok_or(ItemManagerError::NoSuchItemId(item_id))?; let consumed_item = used_item.consume(amount)?; @@ -523,7 +523,7 @@ impl ItemManager { character: &CharacterEntity, item_id: ClientItemId, amount: usize) - -> Result<(), ItemManagerError> { + -> Result<(), anyhow::Error> { let inventory = self.character_inventory.get_mut(&character.id).ok_or(ItemManagerError::NoCharacter(character.id))?; let bank = self.character_bank .get_mut(&character.id) @@ -561,7 +561,7 @@ impl ItemManager { character: &CharacterEntity, item_id: ClientItemId, amount: usize) - -> Result<&InventoryItem, ItemManagerError> { + -> Result<&InventoryItem, anyhow::Error> { let inventory = self.character_inventory.get_mut(&character.id).ok_or(ItemManagerError::NoCharacter(character.id))?; let bank = self.character_bank @@ -593,7 +593,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) + inventory.slot(inventory_item_slot).ok_or(ItemManagerError::Idunnoman.into()) } pub async fn player_feeds_mag_item(&mut self, @@ -601,7 +601,7 @@ impl ItemManager { character: &CharacterEntity, mag_id: ClientItemId, tool_id: ClientItemId) - -> Result<(), ItemManagerError> { + -> Result<(), anyhow::Error> { let inventory = self.character_inventory.get_mut(&character.id).ok_or(ItemManagerError::NoCharacter(character.id))?; let consumed_tool = { let item_to_feed = inventory.get_item_handle_by_id(tool_id).ok_or(ItemManagerError::NoSuchItemId(tool_id))?; @@ -619,7 +619,7 @@ impl ItemManager { let consumed_tool_type = match &consumed_tool { ConsumedItem::Stacked(stacked_consumed_item) => stacked_consumed_item.tool.tool, - _ => return Err(ItemManagerError::WrongItemType(tool_id)) + _ => return Err(ItemManagerError::WrongItemType(tool_id).into()) }; mag.feed(consumed_tool_type); @@ -637,7 +637,7 @@ impl ItemManager { pub async fn use_item(&mut self, used_item: ConsumedItem, entity_gateway: &mut EG, - character: &mut CharacterEntity) -> Result<(), ItemManagerError> { + character: &mut CharacterEntity) -> Result<(), anyhow::Error> { let inventory = self.character_inventory.get_mut(&character.id).ok_or(ItemManagerError::NoCharacter(character.id))?; match &used_item.item() { ItemDetail::Weapon(_w) => { @@ -754,7 +754,7 @@ impl ItemManager { shop_item: &(dyn ShopItem + Send + Sync), item_id: ClientItemId, amount: usize) - -> Result<&InventoryItem, ItemManagerError> { + -> Result<&InventoryItem, anyhow::Error> { let inventory = self.character_inventory.get_mut(&character.id).ok_or(ItemManagerError::NoCharacter(character.id))?; let item_detail = shop_item.as_item(); @@ -853,7 +853,7 @@ impl ItemManager { character: &CharacterEntity, item_id: ClientItemId, equip_slot: u8) - -> Result<(), ItemManagerError> { + -> Result<(), anyhow::Error> { let inventory = self.character_inventory.get_mut(&character.id).ok_or(ItemManagerError::NoCharacter(character.id))?; inventory.equip(&item_id, equip_slot); entity_gateway.set_character_equips(&character.id, &inventory.as_equipped_entity()).await?; @@ -864,7 +864,7 @@ impl ItemManager { entity_gateway: &mut EG, character: &CharacterEntity, item_id: ClientItemId) - -> Result<(), ItemManagerError> { + -> Result<(), anyhow::Error> { let inventory = self.character_inventory.get_mut(&character.id).ok_or(ItemManagerError::NoCharacter(character.id))?; inventory.unequip(&item_id); entity_gateway.set_character_equips(&character.id, &inventory.as_equipped_entity()).await?; @@ -875,7 +875,7 @@ impl ItemManager { entity_gateway: &mut EG, character: &CharacterEntity, item_ids: [u32; 30]) - -> Result<(), ItemManagerError> { + -> Result<(), anyhow::Error> { let inventory = self.character_inventory.get_mut(&character.id).ok_or(ItemManagerError::NoCharacter(character.id))?; let sorted_inventory_items: Vec = item_ids.iter() .filter(|&client_item_id| *client_item_id < 0xFFFFFFFF) diff --git a/src/ship/packet/handler/auth.rs b/src/ship/packet/handler/auth.rs index c389c6c..16228c5 100644 --- a/src/ship/packet/handler/auth.rs +++ b/src/ship/packet/handler/auth.rs @@ -12,7 +12,7 @@ pub async fn validate_login(id: ClientId, clients: &mut Clients, item_manager: &mut ItemManager, ship_name: &String) - -> Result, ShipError> { + -> Result, anyhow::Error> { Ok(match get_login_status(entity_gateway, pkt).await.and_then(check_if_already_online) { Ok(mut user) => { user.at_ship= true; diff --git a/src/ship/packet/handler/direct_message.rs b/src/ship/packet/handler/direct_message.rs index f5ec259..4e93be7 100644 --- a/src/ship/packet/handler/direct_message.rs +++ b/src/ship/packet/handler/direct_message.rs @@ -65,7 +65,7 @@ pub async fn request_item(id: ClientId, clients: &mut Clients, rooms: &mut Rooms, item_manager: &mut ItemManager) - -> Result + Send>, ShipError> + -> Result + Send>, anyhow::Error> where EG: EntityGateway { @@ -77,7 +77,7 @@ where let monster = room.maps.enemy_by_id(request_item.enemy_id as usize)?; if monster.dropped_item { - return Err(ShipError::MonsterAlreadyDroppedItem(id, request_item.enemy_id)) + return Err(ShipError::MonsterAlreadyDroppedItem(id, request_item.enemy_id).into()) } let clients_in_area = client_location.get_clients_in_room(room_id).map_err(|err| -> ClientLocationError { err.into() })?; @@ -114,7 +114,7 @@ pub async fn pickup_item(id: ClientId, client_location: &ClientLocation, clients: &mut Clients, item_manager: &mut ItemManager) - -> Result + Send>, ShipError> + -> Result + Send>, anyhow::Error> where EG: EntityGateway { @@ -169,7 +169,7 @@ pub async fn request_box_item(id: ClientId, clients: &mut Clients, rooms: &mut Rooms, item_manager: &mut ItemManager) - -> Result + Send>, ShipError> + -> Result + Send>, anyhow::Error> where EG: EntityGateway { @@ -181,7 +181,7 @@ EG: EntityGateway let box_object = room.maps.object_by_id(box_drop_request.object_id as usize)?; if box_object.dropped_item { - return Err(ShipError::BoxAlreadyDroppedItem(id, box_drop_request.object_id)) + return Err(ShipError::BoxAlreadyDroppedItem(id, box_drop_request.object_id).into()) } let clients_in_area = client_location.get_clients_in_room(room_id).map_err(|err| -> ClientLocationError { err.into() })?; @@ -216,7 +216,7 @@ EG: EntityGateway pub async fn send_bank_list(id: ClientId, clients: &Clients, item_manager: &mut ItemManager) - -> Result + Send>, ShipError> + -> Result + Send>, anyhow::Error> { let client = clients.get(&id).ok_or(ShipError::ClientNotFound(id))?; let bank_items = item_manager.get_character_bank(&client.character)?; @@ -231,7 +231,7 @@ pub async fn bank_interaction(id: ClientId, client_location: &ClientLocation, clients: &mut Clients, item_manager: &mut ItemManager) - -> Result + Send>, ShipError> + -> Result + Send>, anyhow::Error> where EG: EntityGateway { @@ -292,7 +292,7 @@ pub async fn shop_request(id: ClientId, rooms: &Rooms, level_table: &CharacterLevelTable, shops: &mut ItemShops) - -> Result + Send>, ShipError> + -> Result + Send>, anyhow::Error> { let client = clients.get_mut(&id).ok_or(ShipError::ClientNotFound(id))?; let room_id = client_location.get_room(id).map_err(|err| -> ClientLocationError { err.into() })?; @@ -317,7 +317,7 @@ pub async fn shop_request(id: ClientId, builder::message::shop_list(shop_request.shop_type, &client.armor_shop) }, _ => { - return Err(ShipError::ShopError) + return Err(ShipError::ShopError.into()) } }; @@ -331,7 +331,7 @@ pub async fn buy_item(id: ClientId, client_location: &ClientLocation, clients: &mut Clients, item_manager: &mut ItemManager) - -> Result + Send>, ShipError> + -> Result + Send>, anyhow::Error> where EG: EntityGateway { @@ -360,12 +360,12 @@ where (item, remove) }, _ => { - return Err(ShipError::ShopError) + return Err(ShipError::ShopError.into()) } }; if client.character.meseta < item.price() as u32 { - return Err(ShipError::ShopError) + return Err(ShipError::ShopError.into()) } client.character.meseta -= item.price() as u32; diff --git a/src/ship/packet/handler/lobby.rs b/src/ship/packet/handler/lobby.rs index cf4f024..4954a27 100644 --- a/src/ship/packet/handler/lobby.rs +++ b/src/ship/packet/handler/lobby.rs @@ -15,7 +15,7 @@ pub fn block_selected(id: ClientId, clients: &mut Clients, item_manager: &ItemManager, level_table: &CharacterLevelTable) - -> Result, ShipError> { + -> Result, anyhow::Error> { let client = clients.get_mut(&id).ok_or(ShipError::ClientNotFound(id))?; client.block = pkt.item as u32; @@ -52,7 +52,7 @@ pub fn send_player_to_lobby(id: ClientId, clients: &Clients, item_manager: &ItemManager, level_table: &CharacterLevelTable) - -> Result, ShipError> { + -> Result, anyhow::Error> { let lobby = client_location.add_client_to_next_available_lobby(id, LobbyId(0)).map_err(|_| ShipError::TooManyClients)?; let join_lobby = packet::builder::lobby::join_lobby(id, lobby, client_location, clients, item_manager, level_table)?; let addto = packet::builder::lobby::add_to_lobby(id, lobby, client_location, clients, item_manager, level_table)?; @@ -71,7 +71,7 @@ pub async fn change_lobby(id: ClientId, level_table: &CharacterLevelTable, ship_rooms: &mut Rooms, entity_gateway: &mut EG) - -> Result, ShipError> { + -> Result, anyhow::Error> { let client = clients.get(&id).ok_or(ShipError::ClientNotFound(id))?; let prev_area = client_location.get_area(id).map_err(|err| -> ClientLocationError {err.into()})?; match prev_area { diff --git a/src/ship/packet/handler/message.rs b/src/ship/packet/handler/message.rs index f8a9f82..39ab2f4 100644 --- a/src/ship/packet/handler/message.rs +++ b/src/ship/packet/handler/message.rs @@ -16,7 +16,7 @@ pub async fn request_exp(id: ClientId, clients: &mut Clients, rooms: &mut Rooms, level_table: &CharacterLevelTable) - -> Result + Send>, ShipError> { + -> Result + Send>, anyhow::Error> { let client = clients.get_mut(&id).ok_or(ShipError::ClientNotFound(id))?; let area_client = client_location.get_local_client(id).map_err(|err| -> ClientLocationError { err.into() })?; let room_id = client_location.get_room(id).map_err(|err| -> ClientLocationError { err.into() })?; @@ -70,7 +70,7 @@ pub async fn player_drop_item(id: ClientId, clients: &mut Clients, rooms: &mut Rooms, item_manager: &mut ItemManager) - -> Result + Send>, ShipError> + -> Result + Send>, anyhow::Error> where EG: EntityGateway { @@ -95,7 +95,7 @@ pub fn drop_coordinates(id: ClientId, client_location: &ClientLocation, clients: &mut Clients, rooms: &Rooms) - -> Result + Send>, ShipError> + -> Result + Send>, anyhow::Error> { let client = clients.get_mut(&id).ok_or(ShipError::ClientNotFound(id))?; let room_id = client_location.get_room(id).map_err(|err| -> ClientLocationError { err.into() })?; @@ -120,7 +120,7 @@ pub async fn split_item_stack(id: ClientId, client_location: &ClientLocation, clients: &mut Clients, item_manager: &mut ItemManager) - -> Result + Send>, ShipError> + -> Result + Send>, anyhow::Error> where EG: EntityGateway { @@ -130,7 +130,7 @@ where let drop_location = client.item_drop_location.ok_or(ShipError::ItemDropLocationNotSet)?; if drop_location.item_id.0 != no_longer_has_item.item_id { - return Err(ShipError::DropInvalidItemId(no_longer_has_item.item_id)); + return Err(ShipError::DropInvalidItemId(no_longer_has_item.item_id).into()); } if no_longer_has_item.item_id == 0xFFFFFFFF { @@ -164,7 +164,7 @@ pub fn update_player_position(id: ClientId, clients: &mut Clients, client_location: &ClientLocation, rooms: &Rooms) - -> Result + Send>, ShipError> { + -> Result + Send>, anyhow::Error> { let client = clients.get_mut(&id).ok_or(ShipError::ClientNotFound(id))?; if let Ok(room_id) = client_location.get_room(id).map_err(|err| -> ClientLocationError { err.into() }) { let room = rooms.get(room_id.0) @@ -236,7 +236,7 @@ pub async fn charge_attack(id: ClientId, charge: &ChargeAttack, clients: &mut Clients, entity_gateway: &mut EG) - -> Result + Send>, ShipError> + -> Result + Send>, anyhow::Error> where EG: EntityGateway { @@ -246,7 +246,7 @@ where entity_gateway.save_character(&client.character).await?; Ok(Box::new(None.into_iter())) } else { - Err(ShipError::NotEnoughMeseta(id, client.character.meseta)) + Err(ShipError::NotEnoughMeseta(id, client.character.meseta).into()) } } @@ -256,7 +256,7 @@ pub async fn use_item(id: ClientId, _client_location: &ClientLocation, clients: &mut Clients, item_manager: &mut ItemManager) - -> Result + Send>, ShipError> + -> Result + Send>, anyhow::Error> where EG: EntityGateway { @@ -271,7 +271,7 @@ pub async fn player_used_medical_center(id: ClientId, _pumc: &PlayerUsedMedicalCenter, // not needed? entity_gateway: &mut EG, clients: &mut Clients) - -> Result + Send>, ShipError> + -> Result + Send>, anyhow::Error> where EG: EntityGateway { @@ -281,7 +281,7 @@ where entity_gateway.save_character(&client.character).await?; Ok(Box::new(None.into_iter())) } else { - Err(ShipError::NotEnoughMeseta(id, client.character.meseta)) + Err(ShipError::NotEnoughMeseta(id, client.character.meseta).into()) } } @@ -292,7 +292,7 @@ pub async fn player_feed_mag(id: ClientId, client_location: &ClientLocation, clients: &Clients, item_manager: &mut ItemManager) - -> Result + Send>, ShipError> + -> Result + Send>, anyhow::Error> where EG: EntityGateway { @@ -311,7 +311,7 @@ pub async fn player_equips_item(id: ClientId, entity_gateway: &mut EG, clients: &Clients, item_manager: &mut ItemManager) - -> Result + Send>, ShipError> + -> Result + Send>, anyhow::Error> where EG: EntityGateway { @@ -331,7 +331,7 @@ pub async fn player_unequips_item(id: ClientId, entity_gateway: &mut EG, clients: &Clients, item_manager: &mut ItemManager) - -> Result + Send>, ShipError> + -> Result + Send>, anyhow::Error> where EG: EntityGateway { @@ -345,7 +345,7 @@ pub async fn player_sorts_items(id: ClientId, entity_gateway: &mut EG, clients: &Clients, item_manager: &mut ItemManager) - -> Result + Send>, ShipError> + -> Result + Send>, anyhow::Error> where EG: EntityGateway {