mirror of
https://gitlab.com/veloren/veloren.git
synced 2024-08-30 18:12:32 +00:00
Inventory sorting MR fixes and slight improvements
This commit is contained in:
parent
040c9fe6ee
commit
3850d3e86e
@ -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 {
|
||||
|
@ -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
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user