more kill info

This commit is contained in:
Robin Appelman 2026-11-12 19:38:41 +01:00
commit 5d1729110b
Signed by: robin
SSH key fingerprint: SHA256:kifqgrqnut7q1YUIrI0AdMNC23qiKAS/T9uGFmIYvbE
7 changed files with 196 additions and 99 deletions

View file

@ -2,7 +2,7 @@ use main_error::MainError;
use std::env::args; use std::env::args;
use std::fs; use std::fs;
use tf_log_parser::module::Kills; 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> { fn main() -> Result<(), MainError> {
let path = args().nth(1).expect("No path provided"); let path = args().nth(1).expect("No path provided");
@ -11,6 +11,9 @@ fn main() -> Result<(), MainError> {
let kills = parse_with_handler::<Kills>(&content)?.0; let kills = parse_with_handler::<Kills>(&content)?.0;
for kill in kills { 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)?); println!("{}", serde_json::to_string(&kill)?);
} }
Ok(()) Ok(())

View file

@ -4,19 +4,19 @@ use crate::{Error, Event, Result};
use crate::parsing::{skip, take_until}; use crate::parsing::{skip, take_until};
#[derive(Debug, Event)] #[derive(Debug, Event, Clone)]
pub struct RoundWinEvent<'a> { pub struct RoundWinEvent<'a> {
#[event(name = "winner")] #[event(name = "winner")]
pub team: Option<&'a str>, pub team: Option<&'a str>,
} }
#[derive(Debug, Event)] #[derive(Debug, Event, Clone)]
pub struct RoundLengthEvent { pub struct RoundLengthEvent {
#[event(name = "seconds")] #[event(name = "seconds")]
pub length: Option<f32>, pub length: Option<f32>,
} }
#[derive(Debug, Event)] #[derive(Debug, Event, Clone)]
pub struct LogFileStartedEvent<'a> { pub struct LogFileStartedEvent<'a> {
pub file: Option<&'a str>, pub file: Option<&'a str>,
pub game: 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 struct CaptureBlockedEvent<'a> {
pub cp: Option<u8>, pub cp: Option<u8>,
#[event(name = "cpname")] #[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 struct CurrentScoreEvent {
pub score: u8, pub score: u8,
#[event(name = "with")] #[event(name = "with")]
pub players: u8, pub players: u8,
} }
#[derive(Debug, Event)] #[derive(Debug, Event, Clone)]
pub struct GameOverEvent<'a> { pub struct GameOverEvent<'a> {
pub reason: &'a str, pub reason: &'a str,
} }
#[derive(Debug, Event)] #[derive(Debug, Event, Clone)]
pub struct FinalScoreEvent { pub struct FinalScoreEvent {
pub score: u8, pub score: u8,
#[event(name = "with")] #[event(name = "with")]

View file

@ -2,7 +2,7 @@ use crate::event::{parse_field, ParamIter};
use crate::raw_event::RawSubject; use crate::raw_event::RawSubject;
use crate::{Event, Result}; use crate::{Event, Result};
#[derive(Debug, Event)] #[derive(Debug, Event, Clone)]
pub struct HealedEvent<'a> { pub struct HealedEvent<'a> {
#[event(name = "against")] #[event(name = "against")]
pub target: Option<RawSubject<'a>>, pub target: Option<RawSubject<'a>>,
@ -11,33 +11,34 @@ pub struct HealedEvent<'a> {
pub amount: u32, pub amount: u32,
} }
#[derive(Debug, Event)] #[derive(Debug, Event, Clone)]
pub struct ChargeDeployedEvent<'a> { pub struct ChargeDeployedEvent<'a> {
pub medigun: Option<&'a str>, pub medigun: Option<&'a str>,
} }
#[derive(Debug, Event)] #[derive(Debug, Event, Clone)]
pub struct ChargeEndedEvent { pub struct ChargeEndedEvent {
pub duration: Option<f32>, pub duration: Option<f32>,
} }
#[derive(Debug, Event)] #[derive(Debug, Event, Clone)]
pub struct AdvantageLostEvent { pub struct AdvantageLostEvent {
pub time: Option<f32>, pub time: Option<f32>,
} }
#[derive(Debug, Event)] #[derive(Debug, Event, Clone)]
pub struct FirstHealEvent { pub struct FirstHealEvent {
pub time: Option<f32>, pub time: Option<f32>,
} }
#[derive(Debug, Event)] #[derive(Debug, Event, Clone)]
pub struct MedicDeathEvent { pub struct MedicDeathEvent {
#[event(name = "ubercharge")] #[event(name = "ubercharge")]
pub charge: Option<u32>, pub charge: Option<u32>,
} }
#[derive(Debug, Event)] #[derive(Debug, Event, Clone)]
pub struct MedicDeathExEvent { pub struct MedicDeathExEvent {
#[event(name = "uberpct")]
pub charge_percentage: Option<u8>, pub charge_percentage: Option<u8>,
} }

View file

@ -4,17 +4,17 @@ use crate::raw_event::{subject_parser, RawSubject};
use crate::{Error, Event, Result}; use crate::{Error, Event, Result};
use std::net::SocketAddr; use std::net::SocketAddr;
#[derive(Debug, Event)] #[derive(Debug, Event, Clone)]
pub struct ShotFiredEvent<'a> { pub struct ShotFiredEvent<'a> {
pub weapon: Option<&'a str>, pub weapon: Option<&'a str>,
} }
#[derive(Debug, Event)] #[derive(Debug, Event, Clone)]
pub struct ShotHitEvent<'a> { pub struct ShotHitEvent<'a> {
pub weapon: Option<&'a str>, pub weapon: Option<&'a str>,
} }
#[derive(Debug, Event)] #[derive(Debug, Event, Clone)]
pub struct DamageEvent<'a> { pub struct DamageEvent<'a> {
#[event(name = "against")] #[event(name = "against")]
#[event(default)] #[event(default)]
@ -25,18 +25,20 @@ pub struct DamageEvent<'a> {
pub weapon: Option<&'a str>, pub weapon: Option<&'a str>,
} }
#[derive(Debug, Event)] #[derive(Debug, Event, Clone)]
pub struct KillEvent<'a> { pub struct KillEvent<'a> {
#[event(unnamed)] #[event(unnamed)]
#[event(subject)] #[event(subject)]
pub target: RawSubject<'a>, pub target: RawSubject<'a>,
#[event(name = "with")] #[event(name = "with")]
pub weapon: &'a str, pub weapon: &'a str,
#[event(name = "customkill")]
pub custom_kill: Option<&'a str>,
pub attacker_position: Option<(i32, i32, i32)>, pub attacker_position: Option<(i32, i32, i32)>,
pub victim_position: Option<(i32, i32, i32)>, pub victim_position: Option<(i32, i32, i32)>,
} }
#[derive(Debug, Event)] #[derive(Debug, Event, Clone)]
pub struct KillAssistEvent<'a> { pub struct KillAssistEvent<'a> {
#[event(name = "against")] #[event(name = "against")]
pub target: RawSubject<'a>, pub target: RawSubject<'a>,
@ -44,75 +46,75 @@ pub struct KillAssistEvent<'a> {
pub victim_position: Option<(i32, i32, i32)>, pub victim_position: Option<(i32, i32, i32)>,
} }
#[derive(Debug, Event)] #[derive(Debug, Event, Clone)]
pub struct SpawnEvent { pub struct SpawnEvent {
#[event(name = "as")] #[event(name = "as")]
pub class: Option<Class>, pub class: Option<Class>,
} }
#[derive(Debug, Event)] #[derive(Debug, Event, Clone)]
pub struct RoleChangeEvent { pub struct RoleChangeEvent {
#[event(name = "to")] #[event(name = "to")]
pub class: Option<Class>, pub class: Option<Class>,
} }
#[derive(Debug, Event)] #[derive(Debug, Event, Clone)]
pub struct ConnectedEvent { pub struct ConnectedEvent {
#[event(name = "address")] #[event(name = "address")]
pub address: SocketAddr, pub address: SocketAddr,
} }
#[derive(Debug, Event)] #[derive(Debug, Event, Clone)]
pub struct JoinedTeamEvent { pub struct JoinedTeamEvent {
pub team: Team, pub team: Team,
} }
#[derive(Debug, Event)] #[derive(Debug, Event, Clone)]
pub struct CommittedSuicideEvent<'a> { pub struct CommittedSuicideEvent<'a> {
#[event(name = "with")] #[event(name = "with")]
pub weapon: &'a str, pub weapon: &'a str,
pub attacker_position: Option<(i32, i32, i32)>, pub attacker_position: Option<(i32, i32, i32)>,
} }
#[derive(Debug, Event)] #[derive(Debug, Event, Clone)]
pub struct PickedUpEvent<'a> { pub struct PickedUpEvent<'a> {
pub item: &'a str, pub item: &'a str,
} }
#[derive(Debug, Event)] #[derive(Debug, Event, Clone)]
pub struct DominationEvent<'a> { pub struct DominationEvent<'a> {
pub against: RawSubject<'a>, pub against: RawSubject<'a>,
} }
#[derive(Debug, Event)] #[derive(Debug, Event, Clone)]
pub struct RevengeEvent<'a> { pub struct RevengeEvent<'a> {
pub against: RawSubject<'a>, pub against: RawSubject<'a>,
} }
#[derive(Debug, Event)] #[derive(Debug, Event, Clone)]
pub struct DisconnectEvent<'a> { pub struct DisconnectEvent<'a> {
pub reason: Option<&'a str>, pub reason: Option<&'a str>,
} }
#[derive(Debug, Event)] #[derive(Debug, Event, Clone)]
pub struct BuiltObjectEvent<'a> { pub struct BuiltObjectEvent<'a> {
pub object: Option<&'a str>, pub object: Option<&'a str>,
pub position: Option<(i32, i32, i32)>, pub position: Option<(i32, i32, i32)>,
} }
#[derive(Debug, Event)] #[derive(Debug, Event, Clone)]
pub struct BuiltCarryEvent<'a> { pub struct BuiltCarryEvent<'a> {
pub object: Option<&'a str>, pub object: Option<&'a str>,
pub position: Option<(i32, i32, i32)>, pub position: Option<(i32, i32, i32)>,
} }
#[derive(Debug, Event)] #[derive(Debug, Event, Clone)]
pub struct DropObjectEvent<'a> { pub struct DropObjectEvent<'a> {
pub object: Option<&'a str>, pub object: Option<&'a str>,
pub position: Option<(i32, i32, i32)>, pub position: Option<(i32, i32, i32)>,
} }
#[derive(Debug, Event)] #[derive(Debug, Event, Clone)]
pub struct KilledObjectEvent<'a> { pub struct KilledObjectEvent<'a> {
pub object: Option<&'a str>, pub object: Option<&'a str>,
pub weapon: Option<&'a str>, pub weapon: Option<&'a str>,
@ -121,14 +123,14 @@ pub struct KilledObjectEvent<'a> {
pub attacker_position: Option<(i32, i32, i32)>, pub attacker_position: Option<(i32, i32, i32)>,
} }
#[derive(Debug, Event)] #[derive(Debug, Event, Clone)]
pub struct ObjectDetonatedEvent<'a> { pub struct ObjectDetonatedEvent<'a> {
pub object: Option<&'a str>, pub object: Option<&'a str>,
#[event(name = "attacker_position")] #[event(name = "attacker_position")]
pub position: Option<(i32, i32, i32)>, pub position: Option<(i32, i32, i32)>,
} }
#[derive(Debug, Event)] #[derive(Debug, Event, Clone)]
pub struct ExtinguishedEvent<'a> { pub struct ExtinguishedEvent<'a> {
pub against: RawSubject<'a>, pub against: RawSubject<'a>,
pub with: &'a str, pub with: &'a str,
@ -136,13 +138,13 @@ pub struct ExtinguishedEvent<'a> {
pub victim_position: Option<(i32, i32, i32)>, pub victim_position: Option<(i32, i32, i32)>,
} }
#[derive(Debug, Event)] #[derive(Debug, Event, Clone)]
pub struct SayEvent<'a> { pub struct SayEvent<'a> {
#[event(unnamed)] #[event(unnamed)]
pub text: &'a str, pub text: &'a str,
} }
#[derive(Debug, Event)] #[derive(Debug, Event, Clone)]
pub struct SayTeamEvent<'a> { pub struct SayTeamEvent<'a> {
#[event(unnamed)] #[event(unnamed)]
pub text: &'a str, pub text: &'a str,

View file

@ -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; pub use crate::module::EventHandler;
use crate::module::{ use crate::module::{
ChatMessages, ClassStatsHandler, HealSpread, MedicStatsBuilder, PlayerHandler, ChatMessages, ClassStatsHandler, HealSpread, MedicStatsBuilder, PlayerHandler,

View file

@ -1,86 +1,160 @@
use crate::common::SubjectId; use crate::common::{Class, SubjectId};
use crate::data::Weapon; use crate::data::Weapon;
use crate::event::{GameEvent, KillAssistEvent, KillEvent}; use crate::event::{GameEvent, KillAssistEvent, KillEvent, MedicDeathExEvent};
use crate::module::GlobalData; use crate::module::GlobalData;
use crate::raw_event::RawEventType; use crate::raw_event::RawEventType;
use crate::{EventMeta, SubjectMap}; use crate::{EventMeta, SubjectMap};
use ahash::AHashMap;
use serde::Serialize; use serde::Serialize;
use steamid_ng::SteamID; use steamid_ng::SteamID;
#[derive(PartialEq)] #[derive(Serialize, PartialEq)]
pub struct RawKill { pub struct Participant {
pub time: u32, #[serde(with = "super::serde_steamid")]
pub killer: SubjectId, pub id: SteamID,
pub assister: Option<SubjectId>, pub class: Class,
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)>,
} }
impl RawKill { #[derive(Serialize, PartialEq)]
fn from_event(time: u32, subject: SubjectId, event: &KillEvent) -> Option<Self> { pub struct Position {
Some(RawKill { pub x: i32,
time, pub y: i32,
killer: subject, pub z: i32,
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,
})
}
fn add_assist(&mut self, subject: SubjectId, event: &KillAssistEvent) { impl From<(i32, i32, i32)> for Position {
self.assister = Some(subject); fn from((x, y, z): (i32, i32, i32)) -> Self {
self.assister_position = event.assister_position; 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)] #[derive(Serialize, PartialEq)]
pub struct Kill { pub struct Kill {
pub time: u32, pub time: u32,
#[serde(with = "super::serde_steamid")] pub killer: Participant,
pub killer: SteamID, pub assister: Option<Participant>,
#[serde(with = "super::serde_steamid::opt")] pub victim: Participant,
pub assister: Option<SteamID>, pub charge: Option<u8>,
#[serde(with = "super::serde_steamid")]
pub victim: SteamID,
pub weapon: Weapon, pub weapon: Weapon,
pub attacker_position: Option<(i32, i32, i32)>, pub kind: KillKind,
pub assister_position: Option<(i32, i32, i32)>, pub attacker_position: Option<Position>,
pub victim_position: Option<(i32, i32, i32)>, pub assister_position: Option<Position>,
pub victim_position: Option<Position>,
} }
impl Kill { impl Kill {
pub fn from_raw(raw: RawKill, subjects: &SubjectMap) -> Option<Kill> { fn from_event(
let killer = subjects.steam_id(raw.killer)?; time: u32,
let assister = raw.assister.and_then(|subject| subjects.steam_id(subject)); subject: SubjectId,
let victim = subjects.steam_id(raw.victim)?; event: &KillEvent,
classes: &AHashMap<SubjectId, Class>,
) -> Option<Self> {
let victim = event.target.id().ok()?;
Some(Kill { Some(Kill {
time: raw.time, time,
killer, killer: Participant {
assister, id: subject.steam_id()?,
victim, class: classes.get(&subject).copied().unwrap_or_default(),
weapon: raw.weapon, },
attacker_position: raw.attacker_position, assister: None,
assister_position: raw.assister_position, victim: Participant {
victim_position: raw.victim_position, 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<SubjectId, Class>,
) {
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<MedicDeathExEvent>,
} }
#[derive(Default)] #[derive(Default)]
pub struct Kills(Vec<RawKill>); pub struct Kills {
classes: AHashMap<SubjectId, Class>,
kills: Vec<Kill>,
early_data: Option<EarlyData>,
}
impl Kills {
fn early_data(&mut self, time: u32, victim: SteamID) -> Option<MedicDeathExEvent> {
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 { impl GlobalData for Kills {
type Output = Vec<Kill>; type Output = Vec<Kill>;
fn does_handle(ty: RawEventType) -> bool { 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) { fn handle_event(&mut self, meta: &EventMeta, subject: SubjectId, event: &GameEvent) {
@ -89,27 +163,44 @@ impl GlobalData for Kills {
return; return;
} }
match event { match event {
GameEvent::Spawned(event) => {
if let Some(class) = event.class {
self.classes.insert(subject, class);
}
}
GameEvent::Killed(event) => { GameEvent::Killed(event) => {
if let Some(kill) = RawKill::from_event(time, subject, event) { if let Some(mut kill) = Kill::from_event(time, subject, event, &self.classes) {
self.0.push(kill); 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) => { 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.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 { fn finish(self, _subjects: &SubjectMap) -> Self::Output {
self.0 self.kills
.into_iter()
.filter_map(|raw| Kill::from_raw(raw, subjects))
.collect()
} }
} }

View file

@ -76,7 +76,7 @@ fn test_parse_date() {
); );
} }
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq, Clone)]
pub enum RawSubject<'a> { pub enum RawSubject<'a> {
Player(&'a str), Player(&'a str),
Team(Team), Team(Team),