From c1642d0408a3cd21935b6da1cb4997998d7f2299 Mon Sep 17 00:00:00 2001 From: Lippy13 Date: Tue, 2 Mar 2021 23:31:47 +0000 Subject: [PATCH] Account for no duration being possible --- voxygen/src/hud/util.rs | 62 ++++++++++++++++++++++++++++++----------- 1 file changed, 46 insertions(+), 16 deletions(-) diff --git a/voxygen/src/hud/util.rs b/voxygen/src/hud/util.rs index 877b863f8e..e3b57ae12f 100644 --- a/voxygen/src/hud/util.rs +++ b/voxygen/src/hud/util.rs @@ -94,33 +94,63 @@ fn consumable_desc(maybe_effects: Option<&Vec>, desc: &str) -> String { for effect in effects { if let Effect::Buff(buff) = effect { let strength = buff.data.strength * 0.1; - let dur_secs = buff.data.duration.unwrap().as_secs_f32(); - let str_total = strength * dur_secs; + let dur_secs = match buff.data.duration { + Some(dur_secs) => dur_secs.as_secs_f32(), + None => 0.0, + }; + + let str_total = { + if dur_secs > 0.0 { + strength * dur_secs + } else { + strength + } + }; let buff_desc = match buff.kind { - BuffKind::Saturation { .. } | BuffKind::Regeneration { .. } => { - format!("Restores {} Health over {} seconds", str_total, dur_secs) - }, - BuffKind::Potion { .. } => { + BuffKind::Saturation { .. } + | BuffKind::Regeneration { .. } + | BuffKind::Potion { .. } => { format!("Restores {} Health", str_total) }, BuffKind::IncreaseMaxEnergy { .. } => { - format!( - "Raises Maximum Stamina by {} for {} seconds", - strength, dur_secs - ) + format!("Raises Maximum Stamina by {}", strength) }, BuffKind::IncreaseMaxHealth { .. } => { - format!( - "Raises Maximum Health by {} for {} seconds", - strength, dur_secs - ) + format!("Raises Maximum Health by {}", strength) }, _ => String::new(), }; - if !buff_desc.is_empty() { - write!(&mut description, "\n\n{}", buff_desc).unwrap(); + if buff_desc.is_empty() { + continue; + } + + write!(&mut description, "\n\n{}", buff_desc).unwrap(); + + // The Potion buff has no real duration + if let BuffKind::Potion { .. } = buff.kind { + continue; + } + + let dur_desc = { + if dur_secs > 0.0 { + match buff.kind { + BuffKind::Saturation { .. } | BuffKind::Regeneration { .. } => { + format!("over {} seconds", dur_secs) + }, + BuffKind::IncreaseMaxEnergy | BuffKind::IncreaseMaxHealth { .. } => { + format!("for {} seconds", dur_secs) + }, + _ => String::new(), + } + } else { + "every second".to_string() + } + }; + + if !dur_desc.is_empty() { + write!(&mut description, " {}", dur_desc).unwrap(); } } }