From 5d1729110b5076e7dc780766252b207ec7664cb7 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Thu, 12 Nov 2026 19:38:41 +0100 Subject: [PATCH] more kill info --- examples/kills.rs | 5 +- src/event/game.rs | 14 +-- src/event/medic.rs | 15 ++-- src/event/player.rs | 46 +++++----- src/lib.rs | 2 +- src/module/kills.rs | 215 +++++++++++++++++++++++++++++++------------- src/raw_event.rs | 2 +- 7 files changed, 198 insertions(+), 101 deletions(-) diff --git a/examples/kills.rs b/examples/kills.rs index aac5cea..c45ca51 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; +use tf_log_parser::{parse_with_handler, Class}; fn main() -> Result<(), MainError> { let path = args().nth(1).expect("No path provided"); @@ -11,6 +11,9 @@ 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/event/game.rs b/src/event/game.rs index c110683..19fe0da 100644 --- a/src/event/game.rs +++ b/src/event/game.rs @@ -4,19 +4,19 @@ use crate::{Error, Event, Result}; use crate::parsing::{skip, take_until}; -#[derive(Debug, Event)] +#[derive(Debug, Event, Clone)] pub struct RoundWinEvent<'a> { #[event(name = "winner")] pub team: Option<&'a str>, } -#[derive(Debug, Event)] +#[derive(Debug, Event, Clone)] pub struct RoundLengthEvent { #[event(name = "seconds")] pub length: Option, } -#[derive(Debug, Event)] +#[derive(Debug, Event, Clone)] pub struct LogFileStartedEvent<'a> { pub file: Option<&'a str>, pub game: Option<&'a str>, @@ -39,7 +39,7 @@ impl<'a> Event<'a> for TournamentModeStartedEvent<'a> { } } -#[derive(Debug, Event)] +#[derive(Debug, Event, Clone)] pub struct CaptureBlockedEvent<'a> { pub cp: Option, #[event(name = "cpname")] @@ -103,19 +103,19 @@ impl<'a> Event<'a> for PointCapturedEvent<'a> { } } -#[derive(Debug, Event)] +#[derive(Debug, Event, Clone)] pub struct CurrentScoreEvent { pub score: u8, #[event(name = "with")] pub players: u8, } -#[derive(Debug, Event)] +#[derive(Debug, Event, Clone)] pub struct GameOverEvent<'a> { pub reason: &'a str, } -#[derive(Debug, Event)] +#[derive(Debug, Event, Clone)] pub struct FinalScoreEvent { pub score: u8, #[event(name = "with")] diff --git a/src/event/medic.rs b/src/event/medic.rs index 8360e87..7a0b128 100644 --- a/src/event/medic.rs +++ b/src/event/medic.rs @@ -2,7 +2,7 @@ use crate::event::{parse_field, ParamIter}; use crate::raw_event::RawSubject; use crate::{Event, Result}; -#[derive(Debug, Event)] +#[derive(Debug, Event, Clone)] pub struct HealedEvent<'a> { #[event(name = "against")] pub target: Option>, @@ -11,33 +11,34 @@ pub struct HealedEvent<'a> { pub amount: u32, } -#[derive(Debug, Event)] +#[derive(Debug, Event, Clone)] pub struct ChargeDeployedEvent<'a> { pub medigun: Option<&'a str>, } -#[derive(Debug, Event)] +#[derive(Debug, Event, Clone)] pub struct ChargeEndedEvent { pub duration: Option, } -#[derive(Debug, Event)] +#[derive(Debug, Event, Clone)] pub struct AdvantageLostEvent { pub time: Option, } -#[derive(Debug, Event)] +#[derive(Debug, Event, Clone)] pub struct FirstHealEvent { pub time: Option, } -#[derive(Debug, Event)] +#[derive(Debug, Event, Clone)] pub struct MedicDeathEvent { #[event(name = "ubercharge")] pub charge: Option, } -#[derive(Debug, Event)] +#[derive(Debug, Event, Clone)] pub struct MedicDeathExEvent { + #[event(name = "uberpct")] pub charge_percentage: Option, } diff --git a/src/event/player.rs b/src/event/player.rs index 47938f8..1f9358c 100644 --- a/src/event/player.rs +++ b/src/event/player.rs @@ -4,17 +4,17 @@ use crate::raw_event::{subject_parser, RawSubject}; use crate::{Error, Event, Result}; use std::net::SocketAddr; -#[derive(Debug, Event)] +#[derive(Debug, Event, Clone)] pub struct ShotFiredEvent<'a> { pub weapon: Option<&'a str>, } -#[derive(Debug, Event)] +#[derive(Debug, Event, Clone)] pub struct ShotHitEvent<'a> { pub weapon: Option<&'a str>, } -#[derive(Debug, Event)] +#[derive(Debug, Event, Clone)] pub struct DamageEvent<'a> { #[event(name = "against")] #[event(default)] @@ -25,18 +25,20 @@ pub struct DamageEvent<'a> { pub weapon: Option<&'a str>, } -#[derive(Debug, Event)] +#[derive(Debug, Event, Clone)] pub struct KillEvent<'a> { #[event(unnamed)] #[event(subject)] pub target: RawSubject<'a>, #[event(name = "with")] pub weapon: &'a str, + #[event(name = "customkill")] + pub custom_kill: Option<&'a str>, pub attacker_position: Option<(i32, i32, i32)>, pub victim_position: Option<(i32, i32, i32)>, } -#[derive(Debug, Event)] +#[derive(Debug, Event, Clone)] pub struct KillAssistEvent<'a> { #[event(name = "against")] pub target: RawSubject<'a>, @@ -44,75 +46,75 @@ pub struct KillAssistEvent<'a> { pub victim_position: Option<(i32, i32, i32)>, } -#[derive(Debug, Event)] +#[derive(Debug, Event, Clone)] pub struct SpawnEvent { #[event(name = "as")] pub class: Option, } -#[derive(Debug, Event)] +#[derive(Debug, Event, Clone)] pub struct RoleChangeEvent { #[event(name = "to")] pub class: Option, } -#[derive(Debug, Event)] +#[derive(Debug, Event, Clone)] pub struct ConnectedEvent { #[event(name = "address")] pub address: SocketAddr, } -#[derive(Debug, Event)] +#[derive(Debug, Event, Clone)] pub struct JoinedTeamEvent { pub team: Team, } -#[derive(Debug, Event)] +#[derive(Debug, Event, Clone)] pub struct CommittedSuicideEvent<'a> { #[event(name = "with")] pub weapon: &'a str, pub attacker_position: Option<(i32, i32, i32)>, } -#[derive(Debug, Event)] +#[derive(Debug, Event, Clone)] pub struct PickedUpEvent<'a> { pub item: &'a str, } -#[derive(Debug, Event)] +#[derive(Debug, Event, Clone)] pub struct DominationEvent<'a> { pub against: RawSubject<'a>, } -#[derive(Debug, Event)] +#[derive(Debug, Event, Clone)] pub struct RevengeEvent<'a> { pub against: RawSubject<'a>, } -#[derive(Debug, Event)] +#[derive(Debug, Event, Clone)] pub struct DisconnectEvent<'a> { pub reason: Option<&'a str>, } -#[derive(Debug, Event)] +#[derive(Debug, Event, Clone)] pub struct BuiltObjectEvent<'a> { pub object: Option<&'a str>, pub position: Option<(i32, i32, i32)>, } -#[derive(Debug, Event)] +#[derive(Debug, Event, Clone)] pub struct BuiltCarryEvent<'a> { pub object: Option<&'a str>, pub position: Option<(i32, i32, i32)>, } -#[derive(Debug, Event)] +#[derive(Debug, Event, Clone)] pub struct DropObjectEvent<'a> { pub object: Option<&'a str>, pub position: Option<(i32, i32, i32)>, } -#[derive(Debug, Event)] +#[derive(Debug, Event, Clone)] pub struct KilledObjectEvent<'a> { pub object: Option<&'a str>, pub weapon: Option<&'a str>, @@ -121,14 +123,14 @@ pub struct KilledObjectEvent<'a> { pub attacker_position: Option<(i32, i32, i32)>, } -#[derive(Debug, Event)] +#[derive(Debug, Event, Clone)] pub struct ObjectDetonatedEvent<'a> { pub object: Option<&'a str>, #[event(name = "attacker_position")] pub position: Option<(i32, i32, i32)>, } -#[derive(Debug, Event)] +#[derive(Debug, Event, Clone)] pub struct ExtinguishedEvent<'a> { pub against: RawSubject<'a>, pub with: &'a str, @@ -136,13 +138,13 @@ pub struct ExtinguishedEvent<'a> { pub victim_position: Option<(i32, i32, i32)>, } -#[derive(Debug, Event)] +#[derive(Debug, Event, Clone)] pub struct SayEvent<'a> { #[event(unnamed)] pub text: &'a str, } -#[derive(Debug, Event)] +#[derive(Debug, Event, Clone)] pub struct SayTeamEvent<'a> { #[event(unnamed)] pub text: &'a str, diff --git a/src/lib.rs b/src/lib.rs index eead35a..1fdb7a3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,4 @@ -pub use crate::common::{SteamId3, SubjectData, SubjectError, SubjectId}; +pub use crate::common::{Class, SteamId3, SubjectData, SubjectError, SubjectId}; pub use crate::module::EventHandler; use crate::module::{ ChatMessages, ClassStatsHandler, HealSpread, MedicStatsBuilder, PlayerHandler, diff --git a/src/module/kills.rs b/src/module/kills.rs index 4d80d83..00749dc 100644 --- a/src/module/kills.rs +++ b/src/module/kills.rs @@ -1,86 +1,160 @@ -use crate::common::SubjectId; +use crate::common::{Class, SubjectId}; use crate::data::Weapon; -use crate::event::{GameEvent, KillAssistEvent, KillEvent}; +use crate::event::{GameEvent, KillAssistEvent, KillEvent, MedicDeathExEvent}; use crate::module::GlobalData; use crate::raw_event::RawEventType; use crate::{EventMeta, SubjectMap}; +use ahash::AHashMap; use serde::Serialize; use steamid_ng::SteamID; -#[derive(PartialEq)] -pub struct RawKill { - pub time: u32, - pub killer: SubjectId, - pub assister: Option, - pub victim: SubjectId, - pub weapon: Weapon, - pub attacker_position: Option<(i32, i32, i32)>, - pub assister_position: Option<(i32, i32, i32)>, - pub victim_position: Option<(i32, i32, i32)>, +#[derive(Serialize, PartialEq)] +pub struct Participant { + #[serde(with = "super::serde_steamid")] + pub id: SteamID, + pub class: Class, } -impl RawKill { - fn from_event(time: u32, subject: SubjectId, event: &KillEvent) -> Option { - Some(RawKill { - time, - killer: subject, - assister: None, - victim: event.target.id().ok()?, - weapon: Weapon::from(event.weapon), - attacker_position: event.attacker_position, - assister_position: None, - victim_position: event.victim_position, - }) - } +#[derive(Serialize, PartialEq)] +pub struct Position { + pub x: i32, + pub y: i32, + pub z: i32, +} - fn add_assist(&mut self, subject: SubjectId, event: &KillAssistEvent) { - self.assister = Some(subject); - self.assister_position = event.assister_position; +impl From<(i32, i32, i32)> for Position { + fn from((x, y, z): (i32, i32, i32)) -> Self { + Position { x, y, z } + } +} + +#[derive(Serialize, PartialEq)] +#[serde(rename_all = "kebab-case")] +pub enum KillKind { + Normal, + Backstab, + Headshot, + DeathRinger, +} + +impl KillKind { + pub fn new(event: &KillEvent) -> Self { + match event.custom_kill { + Some("backstab") => Self::Backstab, + Some("headshot") => Self::Headshot, + Some("feign_death") => Self::DeathRinger, + _ => Self::Normal, + } } } #[derive(Serialize, PartialEq)] pub struct Kill { pub time: u32, - #[serde(with = "super::serde_steamid")] - pub killer: SteamID, - #[serde(with = "super::serde_steamid::opt")] - pub assister: Option, - #[serde(with = "super::serde_steamid")] - pub victim: SteamID, + pub killer: Participant, + pub assister: Option, + pub victim: Participant, + pub charge: Option, pub weapon: Weapon, - pub attacker_position: Option<(i32, i32, i32)>, - pub assister_position: Option<(i32, i32, i32)>, - pub victim_position: Option<(i32, i32, i32)>, + pub kind: KillKind, + pub attacker_position: Option, + pub assister_position: Option, + pub victim_position: Option, } impl Kill { - pub fn from_raw(raw: RawKill, subjects: &SubjectMap) -> Option { - let killer = subjects.steam_id(raw.killer)?; - let assister = raw.assister.and_then(|subject| subjects.steam_id(subject)); - let victim = subjects.steam_id(raw.victim)?; - + fn from_event( + time: u32, + subject: SubjectId, + event: &KillEvent, + classes: &AHashMap, + ) -> Option { + let victim = event.target.id().ok()?; Some(Kill { - time: raw.time, - killer, - assister, - victim, - weapon: raw.weapon, - attacker_position: raw.attacker_position, - assister_position: raw.assister_position, - victim_position: raw.victim_position, + time, + killer: Participant { + id: subject.steam_id()?, + class: classes.get(&subject).copied().unwrap_or_default(), + }, + assister: None, + victim: Participant { + id: victim.steam_id()?, + class: classes.get(&victim).copied().unwrap_or_default(), + }, + charge: None, + weapon: Weapon::from(event.weapon), + kind: KillKind::new(event), + attacker_position: event.attacker_position.map(Position::from), + assister_position: None, + victim_position: event.victim_position.map(Position::from), }) } + + fn add_assist( + &mut self, + subject: SubjectId, + event: &KillAssistEvent, + classes: &AHashMap, + ) { + if let Some(steam_id) = subject.steam_id() { + self.assister = Some(Participant { + id: steam_id, + class: classes.get(&subject).copied().unwrap_or_default(), + }); + self.assister_position = event.assister_position.map(Position::from); + } + } + + fn add_medic_info(&mut self, event: &MedicDeathExEvent) { + self.charge = dbg!(event.charge_percentage); + } +} + +struct EarlyData { + time: u32, + victim: SteamID, + medic: Option, } #[derive(Default)] -pub struct Kills(Vec); +pub struct Kills { + classes: AHashMap, + kills: Vec, + early_data: Option, +} + +impl Kills { + fn early_data(&mut self, time: u32, victim: SteamID) -> Option { + self.early_data + .take_if(|data| data.time == time && data.victim == victim) + .and_then(|data| data.medic) + } + + 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); + + self.early_data.get_or_insert(EarlyData { + time, + victim, + medic: None, + }) + } +} impl GlobalData for Kills { type Output = Vec; fn does_handle(ty: RawEventType) -> bool { - matches!(ty, RawEventType::Killed | RawEventType::KillAssist) + matches!( + ty, + RawEventType::Killed + | RawEventType::KillAssist + | RawEventType::Spawned + | RawEventType::MedicDeathEx + ) } fn handle_event(&mut self, meta: &EventMeta, subject: SubjectId, event: &GameEvent) { @@ -89,27 +163,44 @@ impl GlobalData for Kills { return; } match event { + GameEvent::Spawned(event) => { + if let Some(class) = event.class { + self.classes.insert(subject, class); + } + } GameEvent::Killed(event) => { - if let Some(kill) = RawKill::from_event(time, subject, event) { - self.0.push(kill); + if let Some(mut kill) = Kill::from_event(time, subject, event, &self.classes) { + if let Some(medic_info) = self.early_data(time, kill.victim.id) { + kill.add_medic_info(&medic_info); + } + self.kills.push(kill); } } GameEvent::KillAssist(event) => { - if let Some(kill) = self.0.last_mut() { + if let Some(kill) = self.kills.last_mut() { debug_assert_eq!(kill.time, time); - debug_assert_eq!(kill.victim, event.target.id().unwrap()); + debug_assert_eq!( + kill.victim.id, + event.target.id().unwrap().steam_id().unwrap() + ); - kill.add_assist(subject, event); + kill.add_assist(subject, event, &self.classes); + } + } + GameEvent::MedicDeathEx(event) => { + 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); + } else { + self.early_data_mut(time, victim).medic = Some(event.clone()); + } } } _ => {} } } - fn finish(self, subjects: &SubjectMap) -> Self::Output { - self.0 - .into_iter() - .filter_map(|raw| Kill::from_raw(raw, subjects)) - .collect() + fn finish(self, _subjects: &SubjectMap) -> Self::Output { + self.kills } } diff --git a/src/raw_event.rs b/src/raw_event.rs index 4e0a780..7d86a4f 100644 --- a/src/raw_event.rs +++ b/src/raw_event.rs @@ -76,7 +76,7 @@ fn test_parse_date() { ); } -#[derive(Debug, PartialEq)] +#[derive(Debug, PartialEq, Clone)] pub enum RawSubject<'a> { Player(&'a str), Team(Team),