mirror of
https://codeberg.org/icewind/tf-log-parser.git
synced 2026-11-30 04:40:47 +01:00
per-player data wip
This commit is contained in:
parent
cba5236496
commit
9da2d2230a
10 changed files with 341 additions and 127 deletions
|
|
@ -1,7 +1,7 @@
|
|||
use crate::common::SubjectId;
|
||||
use crate::event::GameEvent;
|
||||
use crate::raw_event::RawEventType;
|
||||
use crate::SubjectMap;
|
||||
use crate::{SubjectData, SubjectMap};
|
||||
pub use chat::{ChatHandler, ChatMessage, ChatType};
|
||||
pub use classstats::{ClassStat, ClassStatsHandler};
|
||||
pub use healspread::HealSpreadHandler;
|
||||
|
|
@ -17,13 +17,27 @@ mod lobbysettings;
|
|||
mod medicstats;
|
||||
|
||||
pub trait EventHandler: Default {
|
||||
type Output;
|
||||
type GlobalOutput;
|
||||
type PerSubjectData: Default;
|
||||
type PerSubjectOutput;
|
||||
|
||||
fn does_handle(&self, ty: RawEventType) -> bool;
|
||||
|
||||
fn handle(&mut self, time: u32, subject: SubjectId, event: &GameEvent);
|
||||
fn handle(
|
||||
&mut self,
|
||||
time: u32,
|
||||
subject: SubjectId,
|
||||
subject_data: &mut Self::PerSubjectData,
|
||||
event: &GameEvent,
|
||||
);
|
||||
|
||||
fn finish(self, subjects: &SubjectMap) -> Self::Output;
|
||||
fn finish_global(self, subjects: &SubjectMap) -> Self::GlobalOutput;
|
||||
|
||||
fn finish_per_subject(
|
||||
&self,
|
||||
subject: &SubjectData,
|
||||
data: Self::PerSubjectData,
|
||||
) -> Self::PerSubjectOutput;
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
|
|
@ -33,20 +47,52 @@ pub struct HandlerStack<Head, Tail> {
|
|||
}
|
||||
|
||||
impl<Head: EventHandler, Tail: EventHandler> EventHandler for HandlerStack<Head, Tail> {
|
||||
type Output = (Head::Output, Tail::Output);
|
||||
type GlobalOutput = (Head::GlobalOutput, Tail::GlobalOutput);
|
||||
type PerSubjectData = (Head::PerSubjectData, Tail::PerSubjectData);
|
||||
type PerSubjectOutput = (Head::PerSubjectOutput, Tail::PerSubjectOutput);
|
||||
|
||||
fn does_handle(&self, ty: RawEventType) -> bool {
|
||||
self.head.does_handle(ty) || self.tail.does_handle(ty)
|
||||
}
|
||||
|
||||
fn handle(&mut self, time: u32, subject: SubjectId, event: &GameEvent) {
|
||||
self.head.handle(time, subject, event);
|
||||
self.tail.handle(time, subject, event);
|
||||
fn handle(
|
||||
&mut self,
|
||||
time: u32,
|
||||
subject: SubjectId,
|
||||
subject_data: &mut Self::PerSubjectData,
|
||||
event: &GameEvent,
|
||||
) {
|
||||
self.head.handle(time, subject, &mut subject_data.0, event);
|
||||
self.tail.handle(time, subject, &mut subject_data.1, event);
|
||||
}
|
||||
|
||||
fn finish(self, subjects: &SubjectMap) -> Self::Output {
|
||||
(self.head.finish(subjects), self.tail.finish(subjects))
|
||||
fn finish_global(self, subjects: &SubjectMap) -> Self::GlobalOutput {
|
||||
(
|
||||
self.head.finish_global(subjects),
|
||||
self.tail.finish_global(subjects),
|
||||
)
|
||||
}
|
||||
|
||||
fn finish_per_subject(
|
||||
&self,
|
||||
subject: &SubjectData,
|
||||
data: Self::PerSubjectData,
|
||||
) -> Self::PerSubjectOutput {
|
||||
(
|
||||
self.head.finish_per_subject(subject, data.0),
|
||||
self.tail.finish_per_subject(subject, data.1),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! replace_expr {
|
||||
($_t:tt $sub:expr) => {
|
||||
$sub
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! count_tts {
|
||||
($($tts:tt)*) => {0usize $(+ replace_expr!($tts 1usize))*};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
|
|
@ -61,25 +107,55 @@ macro_rules! handler {
|
|||
$($child: $ty),*
|
||||
}
|
||||
|
||||
pub struct [<$name Output>] {
|
||||
$($child: <$ty as $crate::EventHandler>::Output),*
|
||||
pub struct [<$name GlobalOutput>] {
|
||||
$($child: <$ty as $crate::EventHandler>::GlobalOutput),*
|
||||
}
|
||||
|
||||
impl serde::Serialize for [<$name Output>] {
|
||||
impl serde::Serialize for [<$name GlobalOutput>] {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: serde::Serializer,
|
||||
{
|
||||
use serde::ser::SerializeStruct;
|
||||
let mut state = serializer.serialize_struct(concat!("$name", "output"), 3)?;
|
||||
$(state.serialize_field("$child", &self.$child)?;)*
|
||||
let mut state = serializer.serialize_struct(concat!(stringify!($name), "output"), count_tts!($($child)*))?;
|
||||
$(state.serialize_field(stringify!($child), &self.$child)?;)*
|
||||
state.end()
|
||||
}
|
||||
}
|
||||
|
||||
pub struct [<$name PerSubjectData>] {
|
||||
$($child: <$ty as $crate::EventHandler>::PerSubjectData),*
|
||||
}
|
||||
|
||||
impl Default for [<$name PerSubjectData>] {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
$($child: <$ty as $crate::EventHandler>::PerSubjectData::default()),*
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct [<$name PerSubjectOutput>] {
|
||||
$($child: <$ty as $crate::EventHandler>::PerSubjectOutput),*
|
||||
}
|
||||
|
||||
impl serde::Serialize for [<$name PerSubjectOutput>] {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: serde::Serializer,
|
||||
{
|
||||
use serde::ser::SerializeStruct;
|
||||
let mut state = serializer.serialize_struct(concat!(stringify!($name), "output"), count_tts!($($child)*))?;
|
||||
$(state.serialize_field(stringify!($child), &self.$child)?;)*
|
||||
state.end()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl $crate::EventHandler for $name {
|
||||
type Output = [<$name Output>];
|
||||
type GlobalOutput = [<$name GlobalOutput>];
|
||||
type PerSubjectData = [<$name PerSubjectData>];
|
||||
type PerSubjectOutput = [<$name PerSubjectOutput>];
|
||||
|
||||
fn does_handle(&self, ty: $crate::RawEventType) -> bool {
|
||||
#[allow(unused_imports)]
|
||||
|
|
@ -87,17 +163,23 @@ macro_rules! handler {
|
|||
$(self.$child.does_handle(ty))||*
|
||||
}
|
||||
|
||||
fn handle(&mut self, time: u32, subject: $crate::SubjectId, event: &$crate::GameEvent) {
|
||||
fn handle(&mut self, time: u32, subject: $crate::SubjectId,subject_data:&mut Self::PerSubjectData, event: &$crate::GameEvent) {
|
||||
#[allow(unused_imports)]
|
||||
use $crate::EventHandler;
|
||||
$(self.$child.handle(time, subject, event);)*
|
||||
$(self.$child.handle(time, subject, &mut subject_data.$child, event);)*
|
||||
}
|
||||
|
||||
fn finish(self, subjects: &$crate::SubjectMap) -> Self::Output {
|
||||
fn finish_global(self, subjects: &$crate::SubjectMap) -> Self::GlobalOutput {
|
||||
#[allow(unused_imports)]
|
||||
use $crate::EventHandler;
|
||||
Self::Output {
|
||||
$($child: self.$child.finish(subjects),)*
|
||||
Self::GlobalOutput {
|
||||
$($child: self.$child.finish_global(subjects),)*
|
||||
}
|
||||
}
|
||||
|
||||
fn finish_per_subject(&self, subject: &SubjectData, data: Self::PerSubjectData) -> Self::PerSubjectOutput {
|
||||
Self::PerSubjectOutput {
|
||||
$($child: self.$child.finish_per_subject(subject, data.$child),)*
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue