Browse Source

properly order bank items before assigning item ids

pull/130/head
jake 2 years ago
parent
commit
7c3acc8b3d
  1. 51
      src/ship/items/bank.rs
  2. 39
      src/ship/items/state.rs

51
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<std::cmp::Ordering> {
impl std::cmp::PartialOrd for BankItemDetail {
fn partial_cmp(&self, other: &BankItemDetail) -> Option<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_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<std::cmp::Ordering> {
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)
}
}

39
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,25 +271,18 @@ impl ItemState {
pub async fn load_character_bank<EG: EntityGateway>(&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()
let bank_items = bank.items
.into_iter()
.map(|item| {
let mut citem_state = self.clone();
async move {
Ok(match item {
Ok(Reverse(match item {
BankItemEntity::Individual(item) => {
BankItem {
item_id: citem_state.new_item_id().await?,
item: BankItemDetail::Individual(IndividualItemDetail {
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 {
BankItemDetail::Stacked(StackedItemDetail {
entity_ids: items.iter().map(|i| i.id).collect(),
tool: items.get(0)
.ok_or_else(|| ItemStateError::StackedItemError(items.clone()))?
@ -298,10 +292,21 @@ impl ItemState {
.ok_or_else(|| ItemStateError::StackedItemError(items.clone()))?
})
}
},
}))
})
.collect::<Result<BinaryHeap<_>, 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::<Vec<_>>())
.collect::<FuturesOrdered<_>>()
.collect::<Vec<_>>()
.await
.into_iter()
.collect::<Result<Vec<_>, anyhow::Error>>()?;

Loading…
Cancel
Save