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
3
src/data/mod.rs
Normal file
3
src/data/mod.rs
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
mod weapons;
|
||||
|
||||
pub use weapons::Weapon;
|
||||
23
src/data/weapons.rs
Normal file
23
src/data/weapons.rs
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
use serde::Serialize;
|
||||
use strum::{AsRefStr, EnumString};
|
||||
|
||||
#[derive(EnumString, AsRefStr, Debug, PartialEq)]
|
||||
#[strum(serialize_all = "lowercase")]
|
||||
pub enum Weapon {
|
||||
// todo, add all weapons...
|
||||
Scattergun,
|
||||
#[strum(default)]
|
||||
Other(String),
|
||||
}
|
||||
|
||||
impl Serialize for Weapon {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: serde::Serializer,
|
||||
{
|
||||
match self {
|
||||
Weapon::Other(other) => other.serialize(serializer),
|
||||
_ => self.as_ref().serialize(serializer),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -29,7 +29,6 @@ pub struct DamageEvent<'a> {
|
|||
pub struct KillEvent<'a> {
|
||||
#[event(unnamed)]
|
||||
#[event(subject)]
|
||||
#[event(skip_after = 1)]
|
||||
pub target: RawSubject<'a>,
|
||||
#[event(name = "with")]
|
||||
pub weapon: &'a str,
|
||||
|
|
@ -41,7 +40,7 @@ pub struct KillEvent<'a> {
|
|||
pub struct KillAssistEvent<'a> {
|
||||
#[event(name = "against")]
|
||||
pub target: RawSubject<'a>,
|
||||
pub attacker_position: Option<(i32, i32, i32)>,
|
||||
pub assister_position: Option<(i32, i32, i32)>,
|
||||
pub victim_position: Option<(i32, i32, i32)>,
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@ mod common;
|
|||
pub mod event;
|
||||
#[macro_use]
|
||||
pub mod module;
|
||||
pub mod data;
|
||||
mod error;
|
||||
pub(crate) mod parsing;
|
||||
pub mod raw_event;
|
||||
|
|
|
|||
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()
|
||||
}
|
||||
}
|
||||
|
|
@ -6,6 +6,7 @@ pub use chat::{ChatMessage, ChatMessages, ChatType};
|
|||
use chrono::NaiveDateTime;
|
||||
pub use classstats::{ClassStats, ClassStatsHandler};
|
||||
pub use healspread::HealSpread;
|
||||
pub use kills::{Kill, Kills};
|
||||
pub use lobbysettings::{
|
||||
LobbySettingsError, LobbySettingsHandler, Location, Settings as LobbySettings,
|
||||
};
|
||||
|
|
@ -17,6 +18,7 @@ use std::marker::PhantomData;
|
|||
mod chat;
|
||||
mod classstats;
|
||||
mod healspread;
|
||||
mod kills;
|
||||
mod lobbysettings;
|
||||
mod medicstats;
|
||||
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ use crate::{SubjectData, SubjectError, SubjectId};
|
|||
use ahash::{AHashMap, RandomState};
|
||||
use std::convert::TryInto;
|
||||
use std::ops::{Index, IndexMut};
|
||||
use steamid_ng::SteamID;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct SubjectMap<T = ()>(AHashMap<SubjectId, (SubjectData, T)>);
|
||||
|
|
@ -50,6 +51,13 @@ impl<T> SubjectMap<T> {
|
|||
&self[id].0
|
||||
}
|
||||
|
||||
pub fn steam_id(&self, id: SubjectId) -> Option<SteamID> {
|
||||
match self.0.get(&id) {
|
||||
Some((SubjectData::Player { steam_id, .. }, _)) => Some(*steam_id),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn to_just_subjects(&self) -> SubjectMap<()> {
|
||||
SubjectMap(
|
||||
self.0
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue