some event parsing

This commit is contained in:
Robin Appelman 2021-08-07 22:07:59 +02:00
commit e83f7928aa
7 changed files with 282 additions and 61 deletions

103
src/event/player.rs Normal file
View file

@ -0,0 +1,103 @@
use crate::event::{param_parse, param_parse_with, position, quoted, u_int, ParamIter};
use crate::raw_event::{subject_parser, RawSubject};
use nom::combinator::opt;
use nom::IResult;
use std::num::NonZeroU32;
#[derive(Debug)]
pub struct ShotFiredEvent<'a> {
pub weapon: Option<&'a str>,
}
pub fn shot_fired_event_parser(input: &str) -> IResult<&str, ShotFiredEvent> {
let (input, weapon) = opt(param_parse("weapon"))(input)?;
Ok((input, ShotFiredEvent { weapon }))
}
#[derive(Debug)]
pub struct ShotHitEvent<'a> {
pub weapon: Option<&'a str>,
}
pub fn shot_hit_event_parser(input: &str) -> IResult<&str, ShotFiredEvent> {
let (input, weapon) = opt(param_parse("weapon"))(input)?;
Ok((input, ShotFiredEvent { weapon }))
}
#[derive(Debug)]
pub struct DamageEvent<'a> {
pub target: RawSubject<'a>,
pub damage: Option<NonZeroU32>,
pub real_damage: Option<NonZeroU32>,
pub weapon: Option<&'a str>,
}
pub fn damage_event_parser(input: &str) -> IResult<&str, DamageEvent> {
let (input, target) = param_parse_with("against", subject_parser)(input)?;
let mut event = DamageEvent {
target,
damage: None,
real_damage: None,
weapon: None,
};
for (key, value) in ParamIter::new(input) {
match key {
"damage" => event.damage = NonZeroU32::new(quoted(u_int)(value)?.1),
"realdamage" => event.real_damage = NonZeroU32::new(quoted(u_int)(value)?.1),
"weapon" => event.weapon = Some(value.trim_matches('"')),
_ => {}
}
}
Ok(("", event))
}
#[derive(Debug)]
pub struct KillEvent<'a> {
pub target: RawSubject<'a>,
pub weapon: &'a str,
pub attacker_position: Option<(i32, i32, i32)>,
pub victim_position: Option<(i32, i32, i32)>,
}
pub fn kill_event_parser(input: &str) -> IResult<&str, KillEvent> {
let (input, target) = param_parse_with("against", subject_parser)(input)?;
let (input, weapon) = param_parse("with")(input)?;
let mut event = KillEvent {
target,
weapon,
attacker_position: None,
victim_position: None,
};
for (key, value) in ParamIter::new(input) {
match key {
"attacker_position" => event.attacker_position = Some(position(value)?.1),
"victim_position" => event.victim_position = Some(position(value)?.1),
_ => {}
}
}
Ok(("", event))
}
#[derive(Debug)]
pub struct KillAssistEvent<'a> {
pub target: RawSubject<'a>,
pub attacker_position: Option<(i32, i32, i32)>,
pub victim_position: Option<(i32, i32, i32)>,
}
pub fn kill_assist_event_parser(input: &str) -> IResult<&str, KillAssistEvent> {
let (input, target) = param_parse_with("against", subject_parser)(input)?;
let mut event = KillAssistEvent {
target,
attacker_position: None,
victim_position: None,
};
for (key, value) in ParamIter::new(input) {
match key {
"attacker_position" => event.attacker_position = Some(position(value)?.1),
"victim_position" => event.victim_position = Some(position(value)?.1),
_ => {}
}
}
Ok(("", event))
}