use crate::constants::status::{DEFAULT_STATUS, STATUS_TABLE, Status};
use crate::constants::text::KANA_TABLE;
use crate::utility::status_utils::get_status_by_level;
use crate::utility::string_utils::name_normalize;
use std::collections::HashMap;
pub fn calculate_growth_name_total(name: &str) -> u16 {
let mut char_to_value = HashMap::new();
for (i, &c) in KANA_TABLE.iter().enumerate() {
char_to_value.insert(c, (i % 16) as u8);
}
let normalized = name_normalize(name);
let sum: u32 = normalized
.chars()
.map(|c| match c {
'゛' => Some(3),
'゜' => Some(4),
_ => char_to_value.get(&c).copied(),
})
.flatten()
.map(|v| v as u32)
.sum();
(sum % 16) as u16
}
#[derive(Debug)]
pub struct GrowthModifiers {
pub a: u16, pub b: u16, pub c: u16, }
pub fn calculate_abc(total: u16) -> GrowthModifiers {
GrowthModifiers {
a: (total / 4) % 4,
b: (total / 2) % 2,
c: total % 2,
}
}
pub fn get_adjusted_status_list(name: &str) -> Vec<Status> {
let abc = calculate_abc(calculate_growth_name_total(&name));
STATUS_TABLE
.iter()
.map(|base| base.apply_abc_modifiers(&abc))
.collect()
}
pub fn get_adjusted_status_by_name_lv(name: &str, lv: u8) -> Status {
let abc = calculate_abc(calculate_growth_name_total(&name));
let base = get_status_by_level(lv).unwrap_or(DEFAULT_STATUS.clone());
base.apply_abc_modifiers(&abc)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_normal_name() {
assert_eq!(calculate_growth_name_total("ゆうてい"), 1);
assert_eq!(calculate_growth_name_total("みやおう"), 0);
}
#[test]
fn test_dakuten_handakuten() {
assert_eq!(calculate_growth_name_total("だい"), 6);
assert_eq!(calculate_growth_name_total("ぴぴ"), 0);
assert_eq!(calculate_growth_name_total("ばば"), 12);
}
#[test]
fn test_zero_one() {
assert_eq!(calculate_growth_name_total("0きぬら"), 0);
assert_eq!(calculate_growth_name_total("きくぬら"), 1);
}
#[test]
fn test_more_than_5_characters() {
assert_eq!(calculate_growth_name_total("あいうえお"), 14);
}
#[test]
fn test_contains_invalid_characters() {
assert_eq!(calculate_growth_name_total("かXなY"), 11);
}
#[test]
fn test_moji_lenght() {
assert_eq!(calculate_growth_name_total("う"), 9);
assert_eq!(calculate_growth_name_total("ゆう"), 8);
assert_eq!(calculate_growth_name_total("やおう"), 6);
}
}