mirror of
https://codeberg.org/icewind/tf-log-parser.git
synced 2026-11-30 04:40:47 +01:00
198 lines
5.8 KiB
Rust
198 lines
5.8 KiB
Rust
use crate::common::{Class, SubjectId};
|
|
use crate::data::{Position, Weapon};
|
|
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(Serialize, PartialEq)]
|
|
pub struct Participant {
|
|
#[serde(with = "super::serde_steamid")]
|
|
pub id: SteamID,
|
|
pub class: Class,
|
|
}
|
|
|
|
#[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,
|
|
pub killer: Participant,
|
|
pub assister: Option<Participant>,
|
|
pub victim: Participant,
|
|
pub charge: Option<u8>,
|
|
pub weapon: Weapon,
|
|
pub kind: KillKind,
|
|
pub attacker_position: Option<Position>,
|
|
pub assister_position: Option<Position>,
|
|
pub victim_position: Option<Position>,
|
|
}
|
|
|
|
impl Kill {
|
|
fn from_event(
|
|
time: u32,
|
|
subject: SubjectId,
|
|
event: &KillEvent,
|
|
classes: &AHashMap<SubjectId, Class>,
|
|
) -> Option<Self> {
|
|
let victim = event.target.id().ok()?;
|
|
// mallformed log line
|
|
if event.weapon.contains(' ') || event.weapon.chars().any(|c| !c.is_ascii_lowercase()) {
|
|
return None;
|
|
}
|
|
|
|
Some(Kill {
|
|
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<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)]
|
|
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 {
|
|
type Output = Vec<Kill>;
|
|
|
|
fn does_handle(ty: RawEventType) -> bool {
|
|
matches!(
|
|
ty,
|
|
RawEventType::Killed
|
|
| RawEventType::KillAssist
|
|
| RawEventType::Spawned
|
|
| RawEventType::MedicDeathEx
|
|
)
|
|
}
|
|
|
|
fn handle_event(&mut self, meta: &EventMeta, subject: SubjectId, event: &GameEvent) {
|
|
let time = meta.time;
|
|
if !matches!(subject, SubjectId::Player(_)) {
|
|
return;
|
|
}
|
|
match event {
|
|
GameEvent::Spawned(event) => {
|
|
if let Some(class) = event.class {
|
|
self.classes.insert(subject, class);
|
|
}
|
|
}
|
|
GameEvent::Killed(event) => {
|
|
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.kills.last_mut() {
|
|
debug_assert_eq!(kill.time, time);
|
|
debug_assert_eq!(
|
|
kill.victim.id,
|
|
event.target.id().unwrap().steam_id().unwrap()
|
|
);
|
|
|
|
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.kills
|
|
}
|
|
}
|