Browse Source

enumerate after filtering and add test

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

26
src/ship/items/manager.rs

@ -986,13 +986,17 @@ impl ItemManager {
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();
let sorted_inventory_items: Vec<(usize, &InventoryItem)> = item_ids.iter()
.filter(|&client_item_id| *client_item_id < 0xFFFFFFFF)
.enumerate()
.map(|(slot, &client_item_id)| (slot, inventory.get_item_by_id(ClientItemId(client_item_id)).unwrap()))
.collect();
println!("sorted_inventory_items: {:?}", sorted_inventory_items);
// wait how is this different than update_inventory_slots() ???
for (slot, item) in sorted_inventory_items {
match item.unwrap() {
match item {
InventoryItem::Individual(i) => {
entity_gateway.change_item_location(&i.entity_id, ItemLocation::Inventory {
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(())
}
}

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

@ -360,7 +360,6 @@ where
pub async fn player_sorts_items<EG>(id: ClientId,
pkt: &SortItems,
entity_gateway: &mut EG,
client_location: &ClientLocation,
clients: &Clients,
item_manager: &mut ItemManager)
-> Result<Box<dyn Iterator<Item = (ClientId, SendShipPacket)> + Send>, ShipError>
@ -368,9 +367,6 @@ 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?
}
}

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
},
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();

7
tests/common.rs

@ -80,3 +80,10 @@ pub async fn join_room<EG: EntityGateway>(ship: &mut ShipServerState<EG>, id: Cl
item: room_id,
})).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;
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 {
client: 255,
target: 255,
@ -286,5 +293,10 @@ async fn test_sort_items() {
})))).await.unwrap().for_each(drop);
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