const COPPER: &str = "copper_coin";
const SILVER: &str = "silver_coin";
const GOLD: &str = "gold_coin";
const PLATINUM: &str = "platinum_coin";
const COPPER_PER_SILVER: u64 = 100;
const COPPER_PER_GOLD: u64 = 10_000;
const COPPER_PER_PLATINUM: u64 = 1_000_000;
pub fn copper_from_counts(counts: &std::collections::HashMap<String, u32>) -> u64 {
counts
.iter()
.map(|(id, qty)| copper_value(id).saturating_mul(*qty as u64))
.sum()
}
fn copper_value(template_id: &str) -> u64 {
match template_id {
PLATINUM => COPPER_PER_PLATINUM,
GOLD => COPPER_PER_GOLD,
SILVER => COPPER_PER_SILVER,
COPPER => 1,
_ => 0,
}
}
pub fn format_copper(amount: u64) -> String {
let denoms = [
(COPPER_PER_PLATINUM, "pp"),
(COPPER_PER_GOLD, "gp"),
(COPPER_PER_SILVER, "sp"),
(1, "cp"),
];
let mut rest = amount;
let mut parts = Vec::new();
for (unit, label) in denoms {
if rest >= unit {
let count = rest / unit;
rest %= unit;
parts.push(format!("{count}{label}"));
}
}
if parts.is_empty() {
"0cp".into()
} else {
parts.join(" ")
}
}
pub fn currency_line(counts: &std::collections::HashMap<String, u32>) -> String {
format_copper(copper_from_counts(counts))
}