Inventory sorting MR fixes and slight improvements

This commit is contained in:
Matas Minelga 2022-12-31 14:01:03 +02:00
parent 040c9fe6ee
commit 3850d3e86e
No known key found for this signature in database
GPG Key ID: 74923483B70D4E9F
3 changed files with 13 additions and 27 deletions

View File

@ -206,7 +206,7 @@ impl PartialOrd for Protection {
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Armor {
pub kind: ArmorKind,
stats: StatsSource,
pub stats: StatsSource,
}
impl Armor {

View File

@ -306,17 +306,20 @@ impl ItemKind {
)
}
// Used for inventory sorting, what comes before the first colon (:) is used as
// a broader category
pub fn get_itemkind_string(&self) -> String {
let result = match self {
ItemKind::Tool(tool) => format!("Tool: {:?}", tool),
// Using tool and toolkind to sort tools by kind
ItemKind::Tool(tool) => format!("Tool: {:?}", tool.kind),
ItemKind::ModularComponent(modular_component) => {
format!("Modular Component: {:?}", modular_component)
format!("ModularComponent: {:?}", modular_component.toolkind())
},
ItemKind::Lantern(lantern) => format!("Lantern: {:?}", lantern),
ItemKind::Armor(armor) => format!("Armor: {:?}", armor),
ItemKind::Armor(armor) => format!("Armor: {:?}", armor.stats),
ItemKind::Glider => "Glider:".to_string(),
ItemKind::Consumable { kind, effects } => {
format!("Consumable: {:?}, {:?}", kind, effects)
ItemKind::Consumable { kind, .. } => {
format!("Consumable: {:?}", kind)
},
ItemKind::Throwable { kind } => format!("Throwable: {:?}", kind),
ItemKind::Utility { kind } => format!("Utility: {:?}", kind),
@ -327,12 +330,6 @@ impl ItemKind {
}
}
// impl fmt::Display for ItemKind {
// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// write!(f, "{}", self.to_string())
// }
// }
pub type ItemId = AtomicCell<Option<NonZeroU64>>;
/* /// The only way to access an item id outside this module is to mutably, atomically update it using

View File

@ -147,26 +147,14 @@ impl Inventory {
)
}
/// If custom_order is empty, it will always return Ordering::Equal
pub fn order_by_custom(custom_order: &[CustomOrder], a: &Item, b: &Item) -> Ordering {
let mut order = custom_order.iter();
let a_quality = a.quality();
let b_quality = b.quality();
let a_kind = a.kind().get_itemkind_string();
let b_kind = b.kind().get_itemkind_string();
let mut cmp = match order.next() {
Some(CustomOrder::KindFull) => Ord::cmp(&a_kind, &b_kind),
Some(CustomOrder::KindPartial) => Ord::cmp(
&a_kind.split_once(':').unwrap().0,
&b_kind.split_once(':').unwrap().0,
),
Some(CustomOrder::Quality) => Ord::cmp(&b_quality, &a_quality),
Some(CustomOrder::Name) => Ord::cmp(&a.name(), &b.name()),
Some(CustomOrder::Tag) => Ord::cmp(
&a.tags().first().map_or("", |tag| tag.name()),
&b.tags().first().map_or("", |tag| tag.name()),
),
_ => Ordering::Equal,
};
let mut cmp = Ordering::Equal;
while cmp == Ordering::Equal {
match order.next() {
Some(CustomOrder::KindFull) => cmp = Ord::cmp(&a_kind, &b_kind),
@ -200,9 +188,10 @@ impl Inventory {
// Quality is sorted in reverse since we want high quality items first
InventorySortOrder::Quality => Ord::cmp(&b.quality(), &a.quality()),
InventorySortOrder::Category => {
let order = vec![
let order = [
CustomOrder::KindPartial,
CustomOrder::Quality,
CustomOrder::KindFull,
CustomOrder::Name,
];
Self::order_by_custom(&order, a, b)