From 7c3acc8b3d73d30b19c70499cb3b781e2df17f65 Mon Sep 17 00:00:00 2001 From: jake Date: Mon, 6 Feb 2023 22:38:13 -0700 Subject: [PATCH] properly order bank items before assigning item ids --- src/ship/items/bank.rs | 51 +++++++++++++++++++--------- src/ship/items/state.rs | 73 ++++++++++++++++++++++------------------- 2 files changed, 75 insertions(+), 49 deletions(-) diff --git a/src/ship/items/bank.rs b/src/ship/items/bank.rs index a4248b5..1513166 100644 --- a/src/ship/items/bank.rs +++ b/src/ship/items/bank.rs @@ -305,12 +305,14 @@ impl BankState { } } -impl std::cmp::PartialEq for BankItem { - fn eq(&self, other: &BankItem) -> bool { + + +impl std::cmp::PartialEq for BankItemDetail { + fn eq(&self, other: &BankItemDetail) -> bool { let mut self_bytes = [0u8; 4]; let mut other_bytes = [0u8; 4]; - self_bytes.copy_from_slice(&self.item.as_client_bytes()[0..4]); - other_bytes.copy_from_slice(&other.item.as_client_bytes()[0..4]); + self_bytes.copy_from_slice(&self.as_client_bytes()[0..4]); + other_bytes.copy_from_slice(&other.as_client_bytes()[0..4]); let self_value = u32::from_be_bytes(self_bytes); let other_value = u32::from_be_bytes(other_bytes); @@ -319,15 +321,14 @@ impl std::cmp::PartialEq for BankItem { } } -impl std::cmp::Eq for BankItem {} +impl std::cmp::Eq for BankItemDetail {} -impl std::cmp::PartialOrd for BankItem { - fn partial_cmp(&self, other: &BankItem) -> Option { +impl std::cmp::PartialOrd for BankItemDetail { + fn partial_cmp(&self, other: &BankItemDetail) -> Option { let mut self_bytes = [0u8; 4]; let mut other_bytes = [0u8; 4]; - self_bytes.copy_from_slice(&self.item.as_client_bytes()[0..4]); - other_bytes.copy_from_slice(&other.item.as_client_bytes()[0..4]); - + self_bytes.copy_from_slice(&self.as_client_bytes()[0..4]); + other_bytes.copy_from_slice(&other.as_client_bytes()[0..4]); let self_value = u32::from_be_bytes(self_bytes); let other_value = u32::from_be_bytes(other_bytes); @@ -336,13 +337,12 @@ impl std::cmp::PartialOrd for BankItem { } } -impl std::cmp::Ord for BankItem { - fn cmp(&self, other: &BankItem) -> std::cmp::Ordering { +impl std::cmp::Ord for BankItemDetail { + fn cmp(&self, other: &BankItemDetail) -> std::cmp::Ordering { let mut self_bytes = [0u8; 4]; let mut other_bytes = [0u8; 4]; - self_bytes.copy_from_slice(&self.item.as_client_bytes()[0..4]); - other_bytes.copy_from_slice(&other.item.as_client_bytes()[0..4]); - + self_bytes.copy_from_slice(&self.as_client_bytes()[0..4]); + other_bytes.copy_from_slice(&other.as_client_bytes()[0..4]); let self_value = u32::from_le_bytes(self_bytes); let other_value = u32::from_le_bytes(other_bytes); @@ -351,3 +351,24 @@ impl std::cmp::Ord for BankItem { } } + +impl std::cmp::PartialEq for BankItem { + fn eq(&self, other: &BankItem) -> bool { + self.item.eq(&other.item) + } +} + +impl std::cmp::Eq for BankItem {} + +impl std::cmp::PartialOrd for BankItem { + fn partial_cmp(&self, other: &BankItem) -> Option { + self.item.partial_cmp(&other.item) + } +} + +impl std::cmp::Ord for BankItem { + fn cmp(&self, other: &BankItem) -> std::cmp::Ordering { + self.item.cmp(&other.item) + } +} + diff --git a/src/ship/items/state.rs b/src/ship/items/state.rs index 80d04a1..906762a 100644 --- a/src/ship/items/state.rs +++ b/src/ship/items/state.rs @@ -1,6 +1,7 @@ -use std::collections::HashMap; +use std::collections::{HashMap, BinaryHeap}; +use std::cmp::Reverse; use async_std::sync::{Arc, RwLock, Mutex}; -use futures::future::join_all; +use futures::stream::{FuturesOrdered, StreamExt}; use anyhow::Context; use crate::entity::gateway::{EntityGateway, GatewayError}; @@ -270,39 +271,43 @@ impl ItemState { pub async fn load_character_bank(&mut self, entity_gateway: &mut EG, character: &CharacterEntity, bank_identifier: BankIdentifier) -> Result<(), anyhow::Error> { let bank = entity_gateway.get_character_bank(&character.id, &bank_identifier).await?; - let bank_items = join_all( - bank.items.into_iter() - .map(|item| { - let mut citem_state = self.clone(); - async move { - Ok(match item { - BankItemEntity::Individual(item) => { - BankItem { - item_id: citem_state.new_item_id().await?, - item: BankItemDetail::Individual(IndividualItemDetail { - entity_id: item.id, - item: item.item, - }) - } - }, - BankItemEntity::Stacked(items) => { - BankItem { - item_id: citem_state.new_item_id().await?, - item: BankItemDetail::Stacked(StackedItemDetail { - entity_ids: items.iter().map(|i| i.id).collect(), - tool: items.get(0) - .ok_or_else(|| ItemStateError::StackedItemError(items.clone()))? - .item - .clone() - .as_tool() - .ok_or_else(|| ItemStateError::StackedItemError(items.clone()))? - }) - } - }, + let bank_items = bank.items + .into_iter() + .map(|item| { + Ok(Reverse(match item { + BankItemEntity::Individual(item) => { + BankItemDetail::Individual(IndividualItemDetail { + entity_id: item.id, + item: item.item, + }) + }, + BankItemEntity::Stacked(items) => { + BankItemDetail::Stacked(StackedItemDetail { + entity_ids: items.iter().map(|i| i.id).collect(), + tool: items.get(0) + .ok_or_else(|| ItemStateError::StackedItemError(items.clone()))? + .item + .clone() + .as_tool() + .ok_or_else(|| ItemStateError::StackedItemError(items.clone()))? }) - }}) - .collect::>()) - .await + } + })) + }) + .collect::, anyhow::Error>>()? + .into_iter() + .map(|item| { + let mut citem_state = self.clone(); + async move { + Ok(BankItem { + item_id: citem_state.new_item_id().await?, + item: item.0, + }) + } + }) + .collect::>() + .collect::>() + .await .into_iter() .collect::, anyhow::Error>>()?;