//! Highlight clips from timeline events, ported from `computeHighlights` in //! `tauri-app/src/types/timeline.ts`. use crate::history::{Timeline, TimelineEvent}; #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum Kind { Kill, Death, Assist, MultiKill, TradeKill, } impl Kind { pub fn label(self) -> &'static str { match self { Kind::Kill => "Kill", Kind::Death => "Death", Kind::Assist => "Assist", Kind::MultiKill => "Multi kill", Kind::TradeKill => "Trade kill", } } fn priority(self) -> u8 { match self { Kind::MultiKill => 0, Kind::TradeKill => 1, Kind::Kill => 2, Kind::Death => 3, Kind::Assist => 4, } } } #[derive(Debug, Clone)] pub struct Highlight { pub start: f64, pub end: f64, pub kind: Kind, pub title: String, pub subtitle: String, /// Timestamps of the events inside the clip, for markers. pub events: Vec, } #[derive(Debug, Clone, Copy)] pub struct Options { pub before: f64, pub after: f64, pub min_duration: f64, pub merge_overlapping: bool, pub merge_gap: f64, } impl Default for Options { fn default() -> Self { Self { before: 10.0, after: 3.0, min_duration: 5.0, merge_overlapping: true, merge_gap: 5.0, } } } fn string_at<'a>(event: &'a TimelineEvent, key: &str) -> Option<&'a str> { event.raw_data.get(key).and_then(|v| v.as_str()) } fn involves(event: &TimelineEvent, player: &str) -> Option { if string_at(event, "KillerName") == Some(player) { return Some(Kind::Kill); } if string_at(event, "VictimName") == Some(player) { return Some(Kind::Death); } let assist = event .raw_data .get("Assisters") .and_then(|v| v.as_array()) .is_some_and(|a| a.iter().any(|n| n.as_str() == Some(player))); if assist { return Some(Kind::Assist); } None } /// One clip per involvement, then multi-kills, then a merge pass. pub fn compute(timeline: &Timeline, player: Option<&str>, options: Options) -> Vec { let Some(player) = player else { return Vec::new(); }; let duration = timeline.duration_secs.max(0) as f64; let kills = timeline.events.iter().filter(|e| e.event_type == "kill"); let mut clips: Vec = Vec::new(); let mut player_kills: Vec = Vec::new(); let mut player_deaths: Vec = Vec::new(); for event in kills { let Some(kind) = involves(event, player) else { continue; }; let at = event.time(); let (title, subtitle) = match kind { Kind::Kill => ( "Kill".to_string(), format!( "Killed {}", string_at(event, "VictimName").unwrap_or("an enemy") ), ), Kind::Death => ( "Death".to_string(), format!( "Killed by {}", string_at(event, "KillerName").unwrap_or("an enemy") ), ), _ => ( "Assist".to_string(), format!( "Assisted {} against {}", string_at(event, "KillerName").unwrap_or("an ally"), string_at(event, "VictimName").unwrap_or("an enemy") ), ), }; match kind { Kind::Kill => player_kills.push(at), Kind::Death => player_deaths.push(at), _ => {} } clips.push(Highlight { start: (at - options.before).max(0.0), end: (at + options.after).min(if duration > 0.0 { duration } else { f64::MAX }), kind, title, subtitle, events: vec![at], }); } // Multi-kills: two or more of the player's kills within 12 s. player_kills.sort_by(|a, b| a.partial_cmp(b).unwrap()); let mut i = 0; while i < player_kills.len() { let mut j = i; while j + 1 < player_kills.len() && player_kills[j + 1] - player_kills[j] <= 12.0 { j += 1; } let count = j - i + 1; if count >= 2 { let first = player_kills[i]; let last = player_kills[j]; let died = player_deaths .iter() .any(|d| *d >= first && *d <= last + 12.0); let (kind, title) = if died { (Kind::TradeKill, "Trade kill".to_string()) } else { (Kind::MultiKill, multi_kill_label(count)) }; let subtitle = if died { format!("{count} kills but died in the trade") } else { format!("{count} kills in quick succession") }; let mut events: Vec = player_kills[i..=j].to_vec(); if died { events.extend( player_deaths .iter() .copied() .filter(|d| *d >= first && *d <= last + 12.0), ); } let end = events.iter().cloned().fold(last, f64::max) + options.after; clips.push(Highlight { start: (first - options.before).max(0.0), end: end.min(if duration > 0.0 { duration } else { f64::MAX }), kind, title, subtitle, events, }); } i = j + 1; } clips.sort_by(|a, b| a.start.partial_cmp(&b.start).unwrap()); let mut merged: Vec = Vec::new(); let mut iter = clips.into_iter(); let Some(mut current) = iter.next() else { return Vec::new(); }; for next in iter { let joins = options.merge_overlapping && next.start <= current.end + options.merge_gap; if joins { current.end = current.end.max(next.end); current.events.extend(next.events.iter().copied()); if next.kind.priority() < current.kind.priority() { current.kind = next.kind; current.title = next.title; current.subtitle = next.subtitle; } } else { merged.push(current); current = next; } } merged.push(current); merged.retain(|c| c.end - c.start >= options.min_duration); merged } fn multi_kill_label(count: usize) -> String { match count { 2 => "Double kill".into(), 3 => "Triple kill".into(), 4 => "Quadra kill".into(), 5 => "Penta kill".into(), n => format!("{n}x kill"), } } #[cfg(test)] mod tests { use super::*; #[test] fn the_sample_game_has_a_clip_for_each_involvement() { let game = &crate::history::sample()[0]; let clips = compute(game, Some("Demo#EUW"), Options::default()); assert_eq!(clips.len(), 3, "{clips:#?}"); assert!(clips.iter().any(|c| c.kind == Kind::Death)); assert!(clips.iter().all(|c| c.end > c.start)); } #[test] fn nobody_named_means_no_clips() { let game = &crate::history::sample()[0]; assert!(compute(game, None, Options::default()).is_empty()); } }