Browse Source

fix item pickup tests

pull/112/head
jake 3 years ago
committed by andy
parent
commit
657dabca27
  1. 3
      src/ship/items/actions.rs
  2. 20
      src/ship/items/state.rs
  3. 2
      tests/test_item_pickup.rs

3
src/ship/items/actions.rs

@ -63,6 +63,7 @@ fn add_floor_item_to_inventory(character: &CharacterEntity)
let add_result = inventory.add_floor_item(floor_item)?; let add_result = inventory.add_floor_item(floor_item)?;
transaction.gateway().set_character_inventory(&character.id, &inventory.as_inventory_entity(&character.id)).await?; transaction.gateway().set_character_inventory(&character.id, &inventory.as_inventory_entity(&character.id)).await?;
transaction.gateway().set_character_meseta(&character_id, inventory.meseta).await?;
item_state.set_inventory(inventory); item_state.set_inventory(inventory);
Ok(((item_state, transaction), Ok(((item_state, transaction),
@ -158,7 +159,7 @@ where
entity_gateway.with_transaction(|transaction| async move { entity_gateway.with_transaction(|transaction| async move {
let item_state_proxy = ItemStateProxy::new(item_state); let item_state_proxy = ItemStateProxy::new(item_state);
let ((item_state_proxy, transaction), result) = ItemStateAction::default() let ((item_state_proxy, transaction), result) = ItemStateAction::default()
.act(take_item_from_inventory(character.id, *item_id, 1))
.act(take_item_from_inventory(character.id, *item_id, 0))
.act(add_inventory_item_to_shared_floor(character.id, map_area, drop_position)) .act(add_inventory_item_to_shared_floor(character.id, map_area, drop_position))
.commit((item_state_proxy, transaction)) .commit((item_state_proxy, transaction))
.await?; .await?;

20
src/ship/items/state.rs

@ -358,7 +358,7 @@ impl BankItem {
} }
} }
#[derive(Clone)]
#[derive(Debug, Clone)]
pub enum FloorItemDetail { pub enum FloorItemDetail {
Individual(IndividualItemDetail), Individual(IndividualItemDetail),
Stacked(StackedItemDetail), Stacked(StackedItemDetail),
@ -374,7 +374,7 @@ impl FloorItemDetail {
} }
} }
#[derive(Clone)]
#[derive(Debug, Clone)]
pub struct FloorItem { pub struct FloorItem {
pub item_id: ClientItemId, pub item_id: ClientItemId,
pub item: FloorItemDetail, pub item: FloorItemDetail,
@ -465,12 +465,10 @@ pub enum AddItemResult {
Meseta, Meseta,
} }
#[derive(Clone, Default)]
#[derive(Debug, Clone, Default)]
pub struct LocalFloor(Vec<FloorItem>); pub struct LocalFloor(Vec<FloorItem>);
#[derive(Clone, Default)]
#[derive(Debug, Clone, Default)]
pub struct SharedFloor(Vec<FloorItem>); pub struct SharedFloor(Vec<FloorItem>);
#[derive(Clone)]
pub struct RoomFloorItems(Vec<FloorItem>);
#[derive(Clone)] #[derive(Clone)]
pub struct InventoryState { pub struct InventoryState {
@ -549,7 +547,7 @@ impl InventoryState {
Err(InventoryError::MesetaFull) Err(InventoryError::MesetaFull)
} }
else { else {
self.meseta.0 = std::cmp::min(self.meseta.0 + meseta.0 ,999999);
self.meseta.0 = std::cmp::min(self.meseta.0 + meseta.0, 999999);
Ok(AddItemResult::Meseta) Ok(AddItemResult::Meseta)
} }
}, },
@ -607,7 +605,7 @@ impl InventoryState {
Some(self.inventory.0.remove(idx)) Some(self.inventory.0.remove(idx))
}, },
InventoryItemDetail::Stacked(stacked_item) => { InventoryItemDetail::Stacked(stacked_item) => {
let remove_all = match stacked_item.entity_ids.len().cmp(&(amount as usize)) {
let remove_all = (amount == 0) || match stacked_item.entity_ids.len().cmp(&(amount as usize)) {
Ordering::Equal => true, Ordering::Equal => true,
Ordering::Greater => false, Ordering::Greater => false,
Ordering::Less => return None, Ordering::Less => return None,
@ -708,7 +706,6 @@ impl InventoryState {
match (a_index, b_index) { match (a_index, b_index) {
(Some(a_index), Some(b_index)) => { (Some(a_index), Some(b_index)) => {
dbg!("sort!", a.item_id, a_index, b.item_id, b_index);
a_index.cmp(&b_index) a_index.cmp(&b_index)
}, },
_ => Ordering::Equal _ => Ordering::Equal
@ -1025,6 +1022,7 @@ impl std::cmp::Ord for BankItem {
} }
} }
#[derive(Debug)]
pub struct FloorState { pub struct FloorState {
character_id: CharacterEntityId, character_id: CharacterEntityId,
local: LocalFloor, local: LocalFloor,
@ -1123,7 +1121,7 @@ impl ItemState {
Ok(match item { Ok(match item {
InventoryItemEntity::Individual(item) => { InventoryItemEntity::Individual(item) => {
InventoryItem { InventoryItem {
item_id: self.new_item_id()?,
item_id: ClientItemId(0),
item: InventoryItemDetail::Individual(IndividualItemDetail { item: InventoryItemDetail::Individual(IndividualItemDetail {
entity_id: item.id, entity_id: item.id,
item: item.item, item: item.item,
@ -1132,7 +1130,7 @@ impl ItemState {
}, },
InventoryItemEntity::Stacked(items) => { InventoryItemEntity::Stacked(items) => {
InventoryItem { InventoryItem {
item_id: self.new_item_id()?,
item_id: ClientItemId(0),
item: InventoryItemDetail::Stacked(StackedItemDetail { item: InventoryItemDetail::Stacked(StackedItemDetail {
entity_ids: items.iter().map(|i| i.id).collect(), entity_ids: items.iter().map(|i| i.id).collect(),
tool: items.get(0) tool: items.get(0)

2
tests/test_item_pickup.rs

@ -734,7 +734,7 @@ async fn test_player_drops_partial_stack_and_other_player_picks_it_up() {
ship.handle(ClientId(2), &RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::PickupItem(PickupItem { ship.handle(ClientId(2), &RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::PickupItem(PickupItem {
client: 0, client: 0,
target: 0, target: 0,
item_id: 0x00810001,
item_id: 0x10003,
map_area: 0, map_area: 0,
unknown: [0; 3] unknown: [0; 3]
})))).await.unwrap().for_each(drop); })))).await.unwrap().for_each(drop);

Loading…
Cancel
Save