mirror of
https://codeberg.org/icewind/tf-log-parser.git
synced 2026-11-30 04:40:47 +01:00
event rework wip
This commit is contained in:
parent
e83f7928aa
commit
b2d169601c
10 changed files with 240 additions and 63 deletions
|
|
@ -1,13 +1,75 @@
|
|||
mod medic;
|
||||
mod player;
|
||||
|
||||
use crate::{RawEvent, RawEventType};
|
||||
pub use medic::*;
|
||||
use nom::bytes::complete::{tag, take_while};
|
||||
use nom::character::complete::{alpha1, digit1};
|
||||
use nom::combinator::opt;
|
||||
use nom::error::ErrorKind;
|
||||
use nom::IResult;
|
||||
use nom::{Err, IResult};
|
||||
pub use player::*;
|
||||
use thiserror::Error;
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
pub enum GameEventError {
|
||||
#[error("malformed game event({ty:?}): {err}")]
|
||||
Error {
|
||||
err: nom::error::Error<String>,
|
||||
ty: RawEventType,
|
||||
},
|
||||
#[error("incomplete event body({0:?})")]
|
||||
Incomplete(RawEventType),
|
||||
}
|
||||
|
||||
trait GameEventErrTrait<T> {
|
||||
fn with_type(self, ty: RawEventType) -> Result<T, GameEventError>;
|
||||
}
|
||||
|
||||
impl<'a, T> GameEventErrTrait<T> for IResult<&str, T> {
|
||||
fn with_type(self, ty: RawEventType) -> Result<T, GameEventError> {
|
||||
self.map_err(|err| match err {
|
||||
nom::Err::Error(e) | nom::Err::Failure(e) => GameEventError::Error {
|
||||
err: nom::error::Error {
|
||||
input: e.input.to_string(),
|
||||
code: e.code,
|
||||
},
|
||||
ty,
|
||||
},
|
||||
|
||||
Err::Incomplete(_) => GameEventError::Incomplete(ty),
|
||||
})
|
||||
.map(|(rest, t)| t)
|
||||
}
|
||||
}
|
||||
|
||||
pub enum GameEvent<'a> {
|
||||
ShotFired(ShotFiredEvent<'a>),
|
||||
ShotHit(ShotHitEvent<'a>),
|
||||
Damage(DamageEvent<'a>),
|
||||
Kill(KillEvent<'a>),
|
||||
Say(&'a str),
|
||||
SayTeam(&'a str),
|
||||
Healed(HealedEvent<'a>),
|
||||
}
|
||||
|
||||
impl<'a> GameEvent<'a> {
|
||||
pub fn parse(raw: &RawEvent<'a>) -> Result<GameEvent<'a>, GameEventError> {
|
||||
Ok(match raw.ty {
|
||||
RawEventType::ShotFired => {
|
||||
GameEvent::ShotFired(shot_fired_event_parser(raw.params).with_type(raw.ty)?)
|
||||
}
|
||||
RawEventType::SayTeam => GameEvent::SayTeam(raw.params.trim_matches('"')),
|
||||
RawEventType::Say => GameEvent::Say(raw.params.trim_matches('"')),
|
||||
RawEventType::Healed => {
|
||||
GameEvent::Healed(healed_event_parser(raw.params).with_type(raw.ty)?)
|
||||
}
|
||||
_ => {
|
||||
todo!("{:?} not parsed yet", raw.ty);
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
struct ParamIter<'a> {
|
||||
input: &'a str,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue