mirror of
https://codeberg.org/icewind/tf-log-parser.git
synced 2026-11-30 04:40:47 +01:00
more kill info
This commit is contained in:
parent
c8664f9e47
commit
5d1729110b
7 changed files with 196 additions and 99 deletions
|
|
@ -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<SubjectId>,
|
||||
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<Self> {
|
||||
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<SteamID>,
|
||||
#[serde(with = "super::serde_steamid")]
|
||||
pub victim: SteamID,
|
||||
pub killer: Participant,
|
||||
pub assister: Option<Participant>,
|
||||
pub victim: Participant,
|
||||
pub charge: Option<u8>,
|
||||
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<Position>,
|
||||
pub assister_position: Option<Position>,
|
||||
pub victim_position: Option<Position>,
|
||||
}
|
||||
|
||||
impl Kill {
|
||||
pub fn from_raw(raw: RawKill, subjects: &SubjectMap) -> Option<Kill> {
|
||||
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<SubjectId, Class>,
|
||||
) -> Option<Self> {
|
||||
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<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(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 {
|
||||
type Output = Vec<Kill>;
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue