From 8aa8ae6e3f491a6f69622eeea19ba3a2e5e1c4d5 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Fri, 13 Nov 2026 21:20:43 +0100 Subject: [PATCH] fix medic data --- examples/kills.rs | 5 +---- src/module/kills.rs | 55 ++++++++++++++++++++++++++++++++++++--------- 2 files changed, 46 insertions(+), 14 deletions(-) diff --git a/examples/kills.rs b/examples/kills.rs index c45ca51..aac5cea 100644 --- a/examples/kills.rs +++ b/examples/kills.rs @@ -2,7 +2,7 @@ use main_error::MainError; use std::env::args; use std::fs; use tf_log_parser::module::Kills; -use tf_log_parser::{parse_with_handler, Class}; +use tf_log_parser::parse_with_handler; fn main() -> Result<(), MainError> { let path = args().nth(1).expect("No path provided"); @@ -11,9 +11,6 @@ fn main() -> Result<(), MainError> { let kills = parse_with_handler::(&content)?.0; for kill in kills { - if kill.victim.class == Class::Medic && kill.charge.is_none() { - panic!("medic death without charge"); - } println!("{}", serde_json::to_string(&kill)?); } Ok(()) diff --git a/src/module/kills.rs b/src/module/kills.rs index 39d5520..6a066e6 100644 --- a/src/module/kills.rs +++ b/src/module/kills.rs @@ -1,6 +1,6 @@ use crate::common::{Class, SubjectId}; use crate::data::{Position, Weapon}; -use crate::event::{GameEvent, KillAssistEvent, KillEvent, MedicDeathExEvent}; +use crate::event::{GameEvent, KillAssistEvent, KillEvent, MedicDeathEvent, MedicDeathExEvent}; use crate::module::GlobalData; use crate::raw_event::RawEventType; use crate::{EventMeta, SubjectMap}; @@ -108,7 +108,13 @@ impl Kill { } } - fn add_medic_info(&mut self, event: &MedicDeathExEvent) { + fn add_medic_info(&mut self, event: &MedicDeathEvent) { + if event.charge == Some(1) { + self.charge = Some(100); + } + } + + fn add_medic_info_ex(&mut self, event: &MedicDeathExEvent) { self.charge = event.charge_percentage; } } @@ -116,7 +122,8 @@ impl Kill { struct EarlyData { time: u32, victim: SteamID, - medic: Option, + medic: Option, + medic_ex: Option, } #[derive(Default)] @@ -130,22 +137,28 @@ pub struct Kills { } impl Kills { - fn early_data(&mut self, time: u32, victim: SteamID) -> Option { + fn early_data( + &mut self, + time: u32, + victim: SteamID, + ) -> Option<(Option, Option)> { self.early_data - .take_if(|data| data.time == time && data.victim == victim) - .and_then(|data| data.medic) + .take() + .filter(|data| data.time == time && data.victim == victim) + .map(|data| (data.medic, data.medic_ex)) } fn early_data_mut(&mut self, time: u32, victim: SteamID) -> &mut EarlyData { // remove state early data let _ = self .early_data - .take_if(|data| data.time == time && data.victim == victim); + .take_if(|data| !(data.time == time && data.victim == victim)); self.early_data.get_or_insert(EarlyData { time, victim, medic: None, + medic_ex: None, }) } } @@ -159,6 +172,9 @@ impl GlobalData for Kills { RawEventType::Killed | RawEventType::KillAssist | RawEventType::Spawned + | RawEventType::ChargeDeployed + | RawEventType::ChargeReady + | RawEventType::MedicDeath | RawEventType::MedicDeathEx | RawEventType::RoundStart | RawEventType::RoundWin @@ -210,9 +226,15 @@ impl GlobalData for Kills { self.classes.insert(subject, class); } - if let Some(medic_info) = self.early_data(time, kill.victim.id) { - kill.add_medic_info(&medic_info); + if let Some((medic, medic_ex)) = self.early_data(time, kill.victim.id) { + if let Some(medic) = medic { + kill.add_medic_info(&medic); + } + if let Some(medic_ex) = medic_ex { + kill.add_medic_info_ex(&medic_ex); + } } + self.kills.push(kill); } } @@ -226,7 +248,11 @@ impl GlobalData for Kills { } } } - GameEvent::MedicDeathEx(event) if self.active => { + GameEvent::ChargeReady | GameEvent::ChargeDeployed(_) => { + self.classes.insert(subject, Class::Medic); + } + GameEvent::MedicDeath(event) if self.active => { + self.classes.insert(subject, Class::Medic); if let (Some(kill), Some(victim)) = (self.kills.last_mut(), subject.steam_id()) { if kill.time == time && kill.victim.id == victim { kill.add_medic_info(event); @@ -235,6 +261,15 @@ impl GlobalData for Kills { } } } + GameEvent::MedicDeathEx(event) if self.active => { + if let (Some(kill), Some(victim)) = (self.kills.last_mut(), subject.steam_id()) { + if kill.time == time && kill.victim.id == victim { + kill.add_medic_info_ex(event); + } else { + self.early_data_mut(time, victim).medic_ex = Some(event.clone()); + } + } + } _ => {} } }