Browse Source

properly order bank items before assigning item ids

pull/130/head
jake 1 year ago
parent
commit
7c3acc8b3d
  1. 51
      src/ship/items/bank.rs
  2. 73
      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)
}
}

73
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<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()
.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::<Vec<_>>())
.await
}
}))
})
.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::<FuturesOrdered<_>>()
.collect::<Vec<_>>()
.await
.into_iter()
.collect::<Result<Vec<_>, anyhow::Error>>()?;

Loading…
Cancel
Save