Browse Source

enumerate after filtering and add test

pbs
andy 4 years ago
parent
commit
6d4f5ae8f6
  1. 22
      src/ship/items/manager.rs
  2. 4
      src/ship/packet/handler/message.rs
  3. 2
      src/ship/ship.rs
  4. 7
      tests/common.rs
  5. 14
      tests/test_item_equip.rs

22
src/ship/items/manager.rs

@ -986,13 +986,17 @@ impl ItemManager {
item_ids: [u32; 30]) item_ids: [u32; 30])
-> Result<(), ItemManagerError> { -> Result<(), ItemManagerError> {
let inventory = self.character_inventory.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 sorted_inventory_items: Vec<(usize, Option<&InventoryItem>)> = item_ids.iter()
let sorted_inventory_items: Vec<(usize, &InventoryItem)> = item_ids.iter()
.filter(|&client_item_id| *client_item_id < 0xFFFFFFFF)
.enumerate() .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))))
.map(|(slot, &client_item_id)| (slot, inventory.get_item_by_id(ClientItemId(client_item_id)).unwrap()))
.collect(); .collect();
println!("sorted_inventory_items: {:?}", sorted_inventory_items);
// wait how is this different than update_inventory_slots() ???
for (slot, item) in sorted_inventory_items { for (slot, item) in sorted_inventory_items {
match item.unwrap() {
match item {
InventoryItem::Individual(i) => { InventoryItem::Individual(i) => {
entity_gateway.change_item_location(&i.entity_id, ItemLocation::Inventory { entity_gateway.change_item_location(&i.entity_id, ItemLocation::Inventory {
character_id: character.id, character_id: character.id,
@ -1011,6 +1015,16 @@ 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)).cloned().unwrap())
.collect();
let sorted_inventory = CharacterInventory::new(sorted_inventory_items);
update_inventory_slots(entity_gateway, character, &sorted_inventory).await;
*/
Ok(()) Ok(())
} }
} }

4
src/ship/packet/handler/message.rs

@ -360,7 +360,6 @@ where
pub async fn player_sorts_items<EG>(id: ClientId, pub async fn player_sorts_items<EG>(id: ClientId,
pkt: &SortItems, pkt: &SortItems,
entity_gateway: &mut EG, entity_gateway: &mut EG,
client_location: &ClientLocation,
clients: &Clients, clients: &Clients,
item_manager: &mut ItemManager) item_manager: &mut ItemManager)
-> Result<Box<dyn Iterator<Item = (ClientId, SendShipPacket)> + Send>, ShipError> -> Result<Box<dyn Iterator<Item = (ClientId, SendShipPacket)> + Send>, ShipError>
@ -368,9 +367,6 @@ where
EG: EntityGateway EG: EntityGateway
{ {
let client = clients.get(&id).ok_or(ShipError::ClientNotFound(id))?; 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?; 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? Ok(Box::new(None.into_iter())) // Do clients care about the order of other clients items?
} }

2
src/ship/ship.rs

@ -401,7 +401,7 @@ impl<EG: EntityGateway> ShipServerState<EG> {
handler::message::player_unequips_item(id, &player_unequip_item, &mut self.entity_gateway, &mut self.clients, &mut self.item_manager).await 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) => { 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
handler::message::player_sorts_items(id, sort_items, &mut self.entity_gateway, &mut self.clients, &mut self.item_manager).await
}, },
_ => { _ => {
let cmsg = msg.clone(); let cmsg = msg.clone();

7
tests/common.rs

@ -80,3 +80,10 @@ pub async fn join_room<EG: EntityGateway>(ship: &mut ShipServerState<EG>, id: Cl
item: room_id, item: room_id,
})).await.unwrap().for_each(drop); })).await.unwrap().for_each(drop);
} }
pub async fn change_lobby<EG: EntityGateway>(ship: &mut ShipServerState<EG>, id: ClientId, lobby: u32) {
ship.handle(id, &RecvShipPacket::LobbySelect(LobbySelect{
menu: LOBBY_MENU_ID,
lobby: lobby,
})).await.unwrap().for_each(drop);
}

14
tests/test_item_equip.rs

@ -277,6 +277,13 @@ async fn test_sort_items() {
join_lobby(&mut ship, ClientId(1)).await; join_lobby(&mut ship, ClientId(1)).await;
create_room(&mut ship, ClientId(1), "room", "").await; create_room(&mut ship, ClientId(1), "room", "").await;
let old_items = entity_gateway.get_items_by_character(&char1).await;
assert!(old_items[0].location == item::ItemLocation::Inventory{
character_id: char1.id,
slot: 0,
equipped: true,
});
ship.handle(ClientId(1), &RecvShipPacket::Message(Message::new(GameMessage::SortItems(SortItems { ship.handle(ClientId(1), &RecvShipPacket::Message(Message::new(GameMessage::SortItems(SortItems {
client: 255, client: 255,
target: 255, target: 255,
@ -286,5 +293,10 @@ async fn test_sort_items() {
})))).await.unwrap().for_each(drop); })))).await.unwrap().for_each(drop);
let items = entity_gateway.get_items_by_character(&char1).await; let items = entity_gateway.get_items_by_character(&char1).await;
assert!(items[2].item.item_type() == item::ItemType::Armor(item::armor::ArmorType::Frame));
assert!(items[0].item.item_type() == item::ItemType::Armor(item::armor::ArmorType::Frame));
assert!(items[0].location == item::ItemLocation::Inventory{
character_id: char1.id,
slot: 2,
equipped: true,
});
} }
Loading…
Cancel
Save