event rework wip

This commit is contained in:
Robin Appelman 2021-08-07 23:25:36 +02:00
commit b2d169601c
10 changed files with 240 additions and 63 deletions

View file

@ -1,15 +1,43 @@
use crate::event::{param_parse_with, quoted, u_int};
use crate::event::{param_parse, param_parse_with, quoted, u_int};
use crate::raw_event::{subject_parser, RawSubject};
use nom::combinator::opt;
use nom::number::complete::float;
use nom::IResult;
#[derive(Debug)]
pub struct HealedEvent<'a> {
pub subject: RawSubject<'a>,
pub target: RawSubject<'a>,
pub amount: u32,
}
pub fn healed_event_parser(input: &str) -> IResult<&str, HealedEvent> {
let (input, subject) = param_parse_with("against", subject_parser)(input)?;
let (input, amount) = param_parse_with("healing", quoted(u_int))(input)?;
Ok((input, HealedEvent { subject, amount }))
Ok((
input,
HealedEvent {
target: subject,
amount,
},
))
}
#[derive(Debug)]
pub struct ChargeDeployedEvent<'a> {
pub medigun: Option<&'a str>,
}
pub fn charge_deployed_event_parser(input: &str) -> IResult<&str, ChargeDeployedEvent> {
let (input, medigun) = opt(param_parse("healing"))(input)?;
Ok((input, ChargeDeployedEvent { medigun }))
}
#[derive(Debug)]
pub struct ChargeEndedEvent {
pub duration: Option<f32>,
}
pub fn charge_ended_event_parser(input: &str) -> IResult<&str, ChargeEndedEvent> {
let (input, duration) = opt(param_parse_with("duration", quoted(float)))(input)?;
Ok((input, ChargeEndedEvent { duration }))
}

View file

@ -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,