From 27179f924b828005732098c76864b63dddb2fb8b Mon Sep 17 00:00:00 2001 From: jake Date: Fri, 5 Jun 2020 22:06:42 -0600 Subject: [PATCH] leveltable::get_level_from_exp --- src/common/leveltable.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/common/leveltable.rs b/src/common/leveltable.rs index 997b83e..9d9f4c0 100644 --- a/src/common/leveltable.rs +++ b/src/common/leveltable.rs @@ -76,6 +76,20 @@ impl CharacterLevelTable { } } + pub fn get_level_from_exp(&self, ch_class: CharacterClass, exp: u32) -> u32 { + if let Some(statlist) = self.table.get(&ch_class) { + statlist + .iter() + .filter(|stat| { + stat.exp <= exp + }) + .count() as u32 + } + else { + 0 + } + } + pub fn get_stats_from_exp(&self, ch_class: CharacterClass, exp: u32) -> (u32, CharacterStats) { if let Some(statlist) = self.table.get(&ch_class) { statlist @@ -110,4 +124,12 @@ mod test { assert!(table.get_stats_from_exp(CharacterClass::FOmarl, 0) == (1, CharacterStats { hp: 20, atp: 13, mst: 53, evp: 35, dfp: 10, ata: 15, lck: 10 })); assert!(table.get_stats_from_exp(CharacterClass::FOmarl, 1 << 17) == (36, CharacterStats { hp: 125, atp: 114, mst: 219, evp: 182, dfp: 42, ata: 213, lck: 10 })); } + + #[test] + fn test_levels() { + let table = CharacterLevelTable::new(); + assert!(table.get_level_from_exp(CharacterClass::FOmarl, 0) == 1); + assert!(table.get_level_from_exp(CharacterClass::FOmarl, 3000) == 8); + assert!(table.get_level_from_exp(CharacterClass::FOmarl, 3200) == 9); + } }