use std::collections::HashMap;
use crate::constants::text::{GROWTH_VALUE_TABLE, KANA_TABLE};
use crate::string_utils::name_normalize;
pub fn growth_table_index(c: char) -> Option<u8> {
GROWTH_VALUE_TABLE
.iter()
.find(|&&(ch, _)| ch == c)
.map(|&(_, val)| val)
}
pub fn calculate_name_total(name: &str) -> u16 {
let normalized = name_normalize(name);
normalized
.chars()
.filter_map(growth_table_index)
.map(u16::from)
.sum::<u16>()
}
pub fn calculate_growth_type(name: &str) -> u8 {
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()
.filter_map(|c| char_to_value.get(&c).copied())
.map(|v| v as u32)
.sum();
(sum % 16) as u8
}
#[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,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_ゆうてい() {
assert_eq!(calculate_growth_type("ゆうてい"), 1);
}
#[test]
fn test_みやおう() {
assert_eq!(calculate_growth_type("みやおう"), 0);
}
#[test]
fn test_5文字以上() {
assert_eq!(calculate_growth_type("あいうえお"), 14);
}
#[test]
fn test_無効文字含む() {
assert_eq!(calculate_growth_type("かXなY"), 11);
}
#[test]
fn test_1_moji() {
assert_eq!(calculate_growth_type("う"), 9);
}
#[test]
fn test_2_moji() {
assert_eq!(calculate_growth_type("ゆう"), 8);
}
#[test]
fn test_3_moji() {
assert_eq!(calculate_growth_type("やおう"), 6);
}
}