From 45d9b82598e4ca0c99807cc44609892ba5efde73 Mon Sep 17 00:00:00 2001 From: andy Date: Sat, 31 Oct 2020 01:25:18 -0300 Subject: [PATCH] sort items packets and dont equip multiple items in main it's confusing --- src/bin/main.rs | 4 ++-- src/ship/items/manager.rs | 34 ++++++++++++++++++++++++++++++ src/ship/packet/handler/message.rs | 20 +++++++++++++++++- src/ship/ship.rs | 4 +++- 4 files changed, 58 insertions(+), 4 deletions(-) diff --git a/src/bin/main.rs b/src/bin/main.rs index fcd99af..529fdc2 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -185,7 +185,7 @@ fn main() { location: ItemLocation::Inventory { character_id: character.id, slot: 3, - equipped: true, + equipped: false, } }).await.unwrap(); entity_gateway.create_item( @@ -205,7 +205,7 @@ fn main() { location: ItemLocation::Inventory { character_id: character.id, slot: 4, - equipped: true, + equipped: false, } }).await.unwrap(); diff --git a/src/ship/items/manager.rs b/src/ship/items/manager.rs index b037f59..5e5bd0c 100644 --- a/src/ship/items/manager.rs +++ b/src/ship/items/manager.rs @@ -979,4 +979,38 @@ impl ItemManager { }).await; Ok(()) } + + pub async fn player_sorts_items(&mut self, + entity_gateway: &mut EG, + character: &CharacterEntity, + item_ids: [u32; 30]) + -> Result<(), ItemManagerError> { + let inventory = self.character_inventory.get_mut(&character.id).ok_or(ItemManagerError::NoCharacter(character.id))?; + let sorted_inventory_items: Vec<(usize, Option<&InventoryItem>)> = item_ids.iter() + .enumerate() + .filter(|(slot, &client_item_id)| client_item_id < 0xFFFFFFFF) + .map(|(slot, &client_item_id)| (slot, inventory.get_item_by_id(ClientItemId(client_item_id)))) + .collect(); + for (slot, item) in sorted_inventory_items { + match item.unwrap() { + InventoryItem::Individual(i) => { + entity_gateway.change_item_location(&i.entity_id, ItemLocation::Inventory { + character_id: character.id, + slot: slot, + equipped: i.equipped, + }).await + }, + InventoryItem::Stacked(s) => { + for entity_id in s.entity_ids.iter() { + entity_gateway.change_item_location(&entity_id, ItemLocation::Inventory { + character_id: character.id, + slot: slot, + equipped: false, + }).await + } + }, + }; + } + Ok(()) + } } diff --git a/src/ship/packet/handler/message.rs b/src/ship/packet/handler/message.rs index 8934b19..489c3a5 100644 --- a/src/ship/packet/handler/message.rs +++ b/src/ship/packet/handler/message.rs @@ -6,7 +6,7 @@ use crate::common::serverstate::ClientId; use crate::common::leveltable::CharacterLevelTable; use crate::ship::ship::{SendShipPacket, ShipError, Rooms, Clients, ItemDropLocation}; use crate::ship::location::{ClientLocation, ClientLocationError}; -use crate::ship::items::{ItemManager, ClientItemId}; +use crate::ship::items::{ItemManager, ClientItemId, InventoryItem}; use crate::ship::packet::builder; pub async fn request_exp(id: ClientId, @@ -356,3 +356,21 @@ where Ok(Box::new(None.into_iter())) } + +pub async fn player_sorts_items(id: ClientId, + pkt: &SortItems, + entity_gateway: &mut EG, + client_location: &ClientLocation, + clients: &Clients, + item_manager: &mut ItemManager) + -> Result + Send>, ShipError> +where + EG: EntityGateway +{ + let client = clients.get(&id).ok_or(ShipError::ClientNotFound(id))?; + let room_id = client_location.get_room(id).map_err(|err| -> ClientLocationError { err.into() })?; + let clients_in_area = client_location.get_clients_in_room(room_id).map_err(|err| -> ClientLocationError { err.into() })?; + item_manager.player_sorts_items(entity_gateway, &client.character, pkt.item_ids).await?; + let sort_packet = pkt.clone(); + Ok(Box::new(None.into_iter())) // Do clients care about the order of other clients items? +} diff --git a/src/ship/ship.rs b/src/ship/ship.rs index d27c10f..53357a1 100644 --- a/src/ship/ship.rs +++ b/src/ship/ship.rs @@ -400,7 +400,9 @@ impl ShipServerState { 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 }, - + GameMessage::SortItems(sort_items) => { + handler::message::player_sorts_items(id, sort_items, &mut self.entity_gateway, &mut self.client_location, &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()