//! Text formatting shared by the screens, matching `tauri-app/src/types/timeline.ts`. use chrono::{DateTime, Local, Utc}; pub fn duration(secs: f64) -> String { let total = secs.max(0.0).round() as u64; format!("{}:{:02}", total / 60, total % 60) } pub fn clock(secs: f64) -> String { let total = secs.max(0.0) as u64; format!("{}:{:02}", total / 60, total % 60) } pub fn relative_time(when: DateTime) -> String { let now = Utc::now(); let delta = now.signed_duration_since(when); let days = delta.num_days(); let local = when.with_timezone(&Local); if days > 7 { return local.format("%b %e, %Y, %H:%M").to_string(); } if days > 0 { return format!("{days} day{} ago", plural(days)); } let hours = delta.num_hours(); if hours > 0 { return format!("{hours} hour{} ago", plural(hours)); } let minutes = delta.num_minutes(); if minutes > 0 { return format!("{minutes} minute{} ago", plural(minutes)); } "Just now".into() } pub fn game_start(when: DateTime) -> String { let local = when.with_timezone(&Local); let now = Local::now(); let day = local.date_naive(); let time = local.format("%H:%M").to_string(); if day == now.date_naive() { format!("Today at {time}") } else if day == now.date_naive() - chrono::Days::new(1) { format!("Yesterday at {time}") } else { format!("{} at {time}", local.format("%b %e")) } } fn plural(n: i64) -> &'static str { if n == 1 { "" } else { "s" } } pub fn kda_ratio(kills: u32, deaths: u32, assists: u32) -> String { let value = if deaths == 0 { (kills + assists) as f64 } else { (kills + assists) as f64 / deaths as f64 }; format!("{value:.1}") } pub fn cs_per_min(stats: &crate::history::Stats) -> String { if stats.game_duration == 0 { return "0.0".into(); } format!( "{:.1}", stats.creep_score as f64 / (stats.game_duration as f64 / 60.0) ) } pub fn gold_per_min(stats: &crate::history::Stats) -> String { if stats.game_duration == 0 { return "0".into(); } let value = stats.gold_earned as f64 / (stats.game_duration as f64 / 60.0); number(value.round() as u64) } pub fn number(n: u64) -> String { if n >= 1_000_000 { format!("{:.1}M", n as f64 / 1_000_000.0) } else if n >= 1_000 { format!("{:.1}K", n as f64 / 1_000.0) } else { n.to_string() } } pub fn rank(tier: &str, division: &str) -> String { if tier.is_empty() || tier == "UNRANKED" { return "Unranked".into(); } let mut chars = tier.chars(); let tidy = match chars.next() { Some(first) => first.to_uppercase().collect::() + &chars.as_str().to_lowercase(), None => String::new(), }; if division.is_empty() { tidy } else { format!("{tidy} {division}") } } pub fn lp_delta(delta: i64) -> String { if delta > 0 { format!("+{delta}") } else { delta.to_string() } } #[cfg(test)] mod tests { use super::*; use crate::history::Stats; #[test] fn durations_read_as_minutes_and_seconds() { assert_eq!(duration(1830.0), "30:30"); assert_eq!(duration(65.4), "1:05"); assert_eq!(duration(-3.0), "0:00"); } #[test] fn kda_ratio_survives_zero_deaths() { assert_eq!(kda_ratio(9, 3, 7), "5.3"); assert_eq!(kda_ratio(9, 0, 7), "16.0"); } #[test] fn ranks_are_tidied_for_people() { assert_eq!(rank("EMERALD", "II"), "Emerald II"); assert_eq!(rank("UNRANKED", ""), "Unranked"); assert_eq!(rank("GOLD", ""), "Gold"); } #[test] fn per_minute_stats_use_the_game_length() { let stats = Stats { creep_score: 200, gold_earned: 12_000, game_duration: 1200, ..Default::default() }; assert_eq!(cs_per_min(&stats), "10.0"); assert_eq!(gold_per_min(&stats), "600"); } }