From 48abf4533fc1fa5693973760043824bce97069d0 Mon Sep 17 00:00:00 2001 From: andy Date: Tue, 6 Oct 2020 21:35:20 -0300 Subject: [PATCH] player can (un)equip things now --- src/bin/main.rs | 76 ++++++++++++++++++++++++++++++ src/ship/items/manager.rs | 42 ++++++++++++++++- src/ship/packet/handler/message.rs | 29 ++++++++++++ src/ship/ship.rs | 7 +++ 4 files changed, 153 insertions(+), 1 deletion(-) diff --git a/src/bin/main.rs b/src/bin/main.rs index fe379e0..0ff44be 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -194,6 +194,82 @@ fn main() { name: item::BankName("".to_string()), } }).await; + entity_gateway.create_item( + NewItemEntity { + item: ItemDetail::Armor( + item::armor::Armor { + armor: item::armor::ArmorType::Frame, + dfp: 0, + evp: 0, + slots: 4, + modifiers: Vec::new(), + } + ), + location: ItemLocation::Inventory { + character_id: character.id, + slot: 5, + equipped: true, + } + } + ).await; + entity_gateway.create_item( + NewItemEntity { + item: ItemDetail::Shield( + item::shield::Shield { + shield: item::shield::ShieldType::Barrier, + dfp: 0, + evp: 0, + } + ), + location: ItemLocation::Inventory { + character_id: character.id, + slot: 6, + equipped: true, + } + } + ).await; + entity_gateway.create_item( + NewItemEntity { + item: ItemDetail::Unit( + item::unit::Unit { + unit: item::unit::UnitType::KnightPower, + modifier: Some(item::unit::UnitModifier::Plus), + } + ), + location: ItemLocation::Inventory { + character_id: character.id, + slot: 7, + equipped: true, + } + } + ).await; + entity_gateway.create_item( + NewItemEntity { + item: ItemDetail::Unit( + item::unit::Unit { + unit: item::unit::UnitType::KnightPower, + modifier: Some(item::unit::UnitModifier::PlusPlus), + } + ), + location: ItemLocation::Inventory { + character_id: character.id, + slot: 8, + equipped: true, + } + } + ).await; + entity_gateway.create_item( + NewItemEntity { + item: ItemDetail::Mag( + item::mag::Mag::baby_mag(5) + ), + location: ItemLocation::Inventory { + character_id: character.id, + slot: 9, + equipped: true, + } + } + ).await; } info!("[patch] starting server"); diff --git a/src/ship/items/manager.rs b/src/ship/items/manager.rs index 59de10f..e47be8b 100644 --- a/src/ship/items/manager.rs +++ b/src/ship/items/manager.rs @@ -42,7 +42,9 @@ pub enum ItemManagerError { UseItemError(#[from] use_tool::UseItemError), CouldNotBuyItem, CouldNotAddBoughtItemToInventory, - ItemIdNotInInventory(ClientItemId) + ItemIdNotInInventory(ClientItemId), + CannotGetMutItem, + CannotGetIndividualItem, } @@ -893,4 +895,42 @@ impl ItemManager { }; Ok(inventory_item) } + + pub async fn player_equips_item(&mut self, + entity_gateway: &mut EG, + character: &CharacterEntity, + item_id: ClientItemId) + -> Result<(), ItemManagerError> { + let inventory = self.character_inventory.get_mut(&character.id).ok_or(ItemManagerError::NoCharacter(character.id))?; + let mut inventory_item_handle = inventory.get_item_handle_by_id(item_id).ok_or(ItemManagerError::NoSuchItemId(item_id))?; + let inventory_item = inventory_item_handle.item_mut().ok_or(ItemManagerError::CannotGetMutItem)?.individual().ok_or(ItemManagerError::CannotGetIndividualItem)?; + inventory_item.equipped = true; + entity_gateway.change_item_location(&inventory_item.entity_id, ItemLocation::Inventory{ + character_id: character.id, + slot: character.slot as usize, + equipped: true, + }).await; + + entity_gateway.save_character(character).await; + Ok(()) + } + + pub async fn player_unequips_item(&mut self, + entity_gateway: &mut EG, + character: &CharacterEntity, + item_id: ClientItemId) + -> Result<(), ItemManagerError> { + let inventory = self.character_inventory.get_mut(&character.id).ok_or(ItemManagerError::NoCharacter(character.id))?; + let mut inventory_item_handle = inventory.get_item_handle_by_id(item_id).ok_or(ItemManagerError::NoSuchItemId(item_id))?; + let inventory_item = inventory_item_handle.item_mut().ok_or(ItemManagerError::CannotGetMutItem)?.individual().ok_or(ItemManagerError::CannotGetIndividualItem)?; + inventory_item.equipped = false; + entity_gateway.change_item_location(&inventory_item.entity_id, ItemLocation::Inventory{ + character_id: character.id, + slot: character.slot as usize, + equipped: false, + }).await; + + entity_gateway.save_character(character).await; + Ok(()) + } } diff --git a/src/ship/packet/handler/message.rs b/src/ship/packet/handler/message.rs index 0307993..96a4d36 100644 --- a/src/ship/packet/handler/message.rs +++ b/src/ship/packet/handler/message.rs @@ -307,3 +307,32 @@ where (client.client, SendShipPacket::Message(Message::new(GameMessage::PlayerFeedMag(mag_feed.clone())))) }))) } + +pub async fn player_equips_item(id: ClientId, + pkt: &PlayerEquipItem, + entity_gateway: &mut EG, + clients: &Clients, + item_manager: &mut ItemManager) + -> Result + Send>, ShipError> +where + EG: EntityGateway +{ + let client = clients.get(&id).ok_or(ShipError::ClientNotFound(id))?; + item_manager.player_equips_item(entity_gateway, &client.character, ClientItemId(pkt.item_id)).await?; + Ok(Box::new(None.into_iter())) +} + +pub async fn player_unequips_item(id: ClientId, + pkt: &PlayerUnequipItem, + entity_gateway: &mut EG, + clients: &Clients, + item_manager: &mut ItemManager) + -> Result + Send>, ShipError> +where + EG: EntityGateway +{ + let client = clients.get(&id).ok_or(ShipError::ClientNotFound(id))?; + item_manager.player_unequips_item(entity_gateway, &client.character, ClientItemId(pkt.item_id)).await?; + Ok(Box::new(None.into_iter())) +} + diff --git a/src/ship/ship.rs b/src/ship/ship.rs index 5d314d5..dc6b6b2 100644 --- a/src/ship/ship.rs +++ b/src/ship/ship.rs @@ -393,6 +393,13 @@ impl ShipServerState { GameMessage::PlayerFeedMag(player_feed_mag) => { handler::message::player_feed_mag(id, &player_feed_mag, &mut self.entity_gateway, &mut self.client_location, &mut self.clients, &mut self.item_manager).await }, + GameMessage::PlayerEquipItem(player_equip_item) => { + handler::message::player_equips_item(id, &player_equip_item, &mut self.entity_gateway, &mut self.clients, &mut self.item_manager).await + }, + GameMessage::PlayerUnequipItem(player_unequip_item) => { + handler::message::player_unequips_item(id, &player_unequip_item, &mut self.entity_gateway, &mut self.clients, &mut self.item_manager).await + }, + _ => { let cmsg = msg.clone(); Ok(Box::new(self.client_location.get_client_neighbors(id).unwrap().into_iter()