mirror of
https://codeberg.org/icewind/tf-log-parser.git
synced 2026-11-30 04:40:47 +01:00
kills module
This commit is contained in:
parent
b03d328ab9
commit
c8664f9e47
13 changed files with 233 additions and 36 deletions
115
src/module/kills.rs
Normal file
115
src/module/kills.rs
Normal file
|
|
@ -0,0 +1,115 @@
|
|||
use crate::common::SubjectId;
|
||||
use crate::data::Weapon;
|
||||
use crate::event::{GameEvent, KillAssistEvent, KillEvent};
|
||||
use crate::module::GlobalData;
|
||||
use crate::raw_event::RawEventType;
|
||||
use crate::{EventMeta, SubjectMap};
|
||||
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)>,
|
||||
}
|
||||
|
||||
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,
|
||||
})
|
||||
}
|
||||
|
||||
fn add_assist(&mut self, subject: SubjectId, event: &KillAssistEvent) {
|
||||
self.assister = Some(subject);
|
||||
self.assister_position = event.assister_position;
|
||||
}
|
||||
}
|
||||
|
||||
#[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 weapon: Weapon,
|
||||
pub attacker_position: Option<(i32, i32, i32)>,
|
||||
pub assister_position: Option<(i32, i32, i32)>,
|
||||
pub victim_position: Option<(i32, i32, i32)>,
|
||||
}
|
||||
|
||||
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)?;
|
||||
|
||||
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,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Kills(Vec<RawKill>);
|
||||
|
||||
impl GlobalData for Kills {
|
||||
type Output = Vec<Kill>;
|
||||
|
||||
fn does_handle(ty: RawEventType) -> bool {
|
||||
matches!(ty, RawEventType::Killed | RawEventType::KillAssist)
|
||||
}
|
||||
|
||||
fn handle_event(&mut self, meta: &EventMeta, subject: SubjectId, event: &GameEvent) {
|
||||
let time = meta.time;
|
||||
if !matches!(subject, SubjectId::Player(_)) {
|
||||
return;
|
||||
}
|
||||
match event {
|
||||
GameEvent::Killed(event) => {
|
||||
if let Some(kill) = RawKill::from_event(time, subject, event) {
|
||||
self.0.push(kill);
|
||||
}
|
||||
}
|
||||
GameEvent::KillAssist(event) => {
|
||||
if let Some(kill) = self.0.last_mut() {
|
||||
debug_assert_eq!(kill.time, time);
|
||||
debug_assert_eq!(kill.victim, event.target.id().unwrap());
|
||||
|
||||
kill.add_assist(subject, event);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
fn finish(self, subjects: &SubjectMap) -> Self::Output {
|
||||
self.0
|
||||
.into_iter()
|
||||
.filter_map(|raw| Kill::from_raw(raw, subjects))
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue