Browse Source

properly set item_id after a trade serverside

pull/80/head
jake 3 years ago
parent
commit
22f630f562
  1. 13
      src/ship/items/inventory.rs
  2. 4
      src/ship/items/manager.rs
  3. 113
      tests/test_trade.rs

13
src/ship/items/inventory.rs

@ -654,6 +654,19 @@ impl CharacterInventory {
}
}
pub fn add_item_with_new_item_id(&mut self, item: InventoryItem, item_id: ClientItemId) {
match item {
InventoryItem::Individual(mut individual_inventory_item) => {
individual_inventory_item.item_id = item_id;
self.add_item(InventoryItem::Individual(individual_inventory_item));
},
InventoryItem::Stacked(mut stacked_inventory_item) => {
stacked_inventory_item.item_id = item_id;
self.add_stacked_item(stacked_inventory_item)
}
}
}
pub fn add_individual_floor_item(&mut self, floor_item: &IndividualFloorItem) -> &InventoryItem {
self.items.push(InventoryItem::Individual(IndividualInventoryItem {
entity_id: floor_item.entity_id,

4
src/ship/items/manager.rs

@ -1277,7 +1277,7 @@ impl<EG: EntityGateway> ItemAction<EG> for TradeIndividualItem {
entity_gateway.set_character_inventory(&self.src_character_id, &src_inventory.as_inventory_entity(&self.src_character_id)).await?;
let dest_inventory = item_manager.character_inventory.get_mut(&self.dest_character_id).ok_or(ItemManagerError::NoCharacter(self.dest_character_id))?;
dest_inventory.add_item(inventory_item);
dest_inventory.add_item_with_new_item_id(inventory_item, self.new_item_id);
entity_gateway.set_character_inventory(&self.dest_character_id, &dest_inventory.as_inventory_entity(&self.dest_character_id)).await?;
Ok(())
@ -1301,7 +1301,7 @@ impl<EG: EntityGateway> ItemAction<EG> for TradeStackedItem {
entity_gateway.set_character_inventory(&self.src_character_id, &src_inventory.as_inventory_entity(&self.src_character_id)).await?;
let dest_inventory = item_manager.character_inventory.get_mut(&self.dest_character_id).ok_or(ItemManagerError::NoCharacter(self.dest_character_id))?;
dest_inventory.add_stacked_item(inventory_item);
dest_inventory.add_item_with_new_item_id(InventoryItem::Stacked(inventory_item), self.new_item_id);
entity_gateway.set_character_inventory(&self.dest_character_id, &dest_inventory.as_inventory_entity(&self.dest_character_id)).await?;
Ok(())

113
tests/test_trade.rs

@ -3726,7 +3726,6 @@ async fn test_items_to_trade_data_does_not_match() {
count: 1,
items: titems,
})).await.unwrap().collect::<Vec<_>>();
dbg!(&ack);
assert_eq!(ack.len(), 2);
assert!(matches!(ack[0], (ClientId(2), SendShipPacket::CancelTrade(..))));
assert!(matches!(ack[1], (ClientId(1), SendShipPacket::CancelTrade(..))));
@ -4269,3 +4268,115 @@ async fn test_items_to_trade_count_mismatch_with_meseta() {
let p2_items = entity_gateway.get_character_inventory(&char2.id).await.unwrap();
assert_eq!(p2_items.items.len(), 0);
}
#[async_std::test]
async fn test_dropping_item_after_trade() {
let mut entity_gateway = InMemoryGateway::default();
let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a").await;
let (_user2, char2) = new_user_character(&mut entity_gateway, "a2", "a").await;
let mut p1_inv = Vec::new();
p1_inv.push(entity_gateway.create_item(
item::NewItemEntity {
item: item::ItemDetail::Weapon(
item::weapon::Weapon {
weapon: item::weapon::WeaponType::Handgun,
grind: 0,
special: None,
attrs: [None, None, None],
tekked: true,
}
),
}).await.unwrap());
entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(p1_inv)).await.unwrap();
entity_gateway.set_character_inventory(&char2.id, &item::InventoryEntity::new(Vec::<item::InventoryItemEntity>::new())).await.unwrap();
let mut ship = Box::new(ShipServerState::builder()
.gateway(entity_gateway.clone())
.build());
log_in_char(&mut ship, ClientId(1), "a1", "a").await;
log_in_char(&mut ship, ClientId(2), "a2", "a").await;
join_lobby(&mut ship, ClientId(1)).await;
join_lobby(&mut ship, ClientId(2)).await;
create_room(&mut ship, ClientId(1), "room", "").await;
join_room(&mut ship, ClientId(2), 0).await;
let p1_items = entity_gateway.get_character_inventory(&char1.id).await.unwrap();
assert_eq!(p1_items.items.len(), 1);
let p2_items = entity_gateway.get_character_inventory(&char2.id).await.unwrap();
assert_eq!(p2_items.items.len(), 0);
initialize_trade(&mut ship, ClientId(1), ClientId(2)).await;
ship.handle(ClientId(1), &RecvShipPacket::DirectMessage(DirectMessage::new(1, GameMessage::TradeRequest(TradeRequest {
client: 1,
target: 0,
trade: TradeRequestCommand::AddItem(0x10000, 1)
})))).await.unwrap().for_each(drop);
confirm_trade(&mut ship, ClientId(1), ClientId(2)).await;
finalconfirm_trade(&mut ship, ClientId(1), ClientId(2)).await;
let titems = TradeItemBuilder::default()
.individual(&p1_items.items[0], 0x10000)
.build();
let ack = ship.handle(ClientId(1), &RecvShipPacket::ItemsToTrade(ItemsToTrade {
trade_target: 1,
unknown2: 0,
count: 1,
items: titems,
})).await.unwrap().collect::<Vec<_>>();
assert_eq!(ack.len(), 0);
let ack = ship.handle(ClientId(2), &RecvShipPacket::ItemsToTrade(ItemsToTrade {
trade_target: 0,
unknown2: 0,
count: 0,
items: Default::default(),
})).await.unwrap().collect::<Vec<_>>();
assert_eq!(ack.len(), 2);
assert!(matches!(ack[0], (ClientId(2), SendShipPacket::AcknowledgeTrade(AcknowledgeTrade {}))));
assert!(matches!(ack[1], (ClientId(1), SendShipPacket::AcknowledgeTrade(AcknowledgeTrade {}))));
let ack = ship.handle(ClientId(1), &RecvShipPacket::TradeConfirmed(TradeConfirmed {
})).await.unwrap().collect::<Vec<_>>();
assert_eq!(ack.len(), 0);
let ack = ship.handle(ClientId(2), &RecvShipPacket::TradeConfirmed(TradeConfirmed {
})).await.unwrap().collect::<Vec<_>>();
assert_eq!(ack.len(), 5);
assert!(matches!(ack[0], (ClientId(1), SendShipPacket::Message(Message {
msg: GameMessage::CreateItem(CreateItem {..}),
..
}))));
assert!(matches!(ack[1], (ClientId(2), SendShipPacket::Message(Message {
msg: GameMessage::CreateItem(CreateItem {..}),
..
}))));
assert!(matches!(ack[2], (ClientId(2), SendShipPacket::Message(Message {
msg: GameMessage::PlayerNoLongerHasItem(PlayerNoLongerHasItem {..}),
..
}))));
assert!(matches!(ack[3], (ClientId(2), SendShipPacket::TradeSuccessful {..})));
assert!(matches!(ack[4], (ClientId(1), SendShipPacket::TradeSuccessful {..})));
let ack = ship.handle(ClientId(2), &RecvShipPacket::Message(Message::new(GameMessage::PlayerDropItem(PlayerDropItem {
client: 0,
target: 0,
unknown1: 0,
map_area: 0,
item_id: 0x810001,
x: 0.0,
y: 0.0,
z: 0.0,
})))).await.unwrap().for_each(drop);
let p1_items = entity_gateway.get_character_inventory(&char1.id).await.unwrap();
assert_eq!(p1_items.items.len(), 0);
let p2_items = entity_gateway.get_character_inventory(&char2.id).await.unwrap();
assert_eq!(p2_items.items.len(), 0);
}
Loading…
Cancel
Save