subject postprocessing

This commit is contained in:
Robin Appelman 2021-08-07 19:28:58 +02:00
commit 747ee7d1a9
10 changed files with 2970 additions and 22 deletions

View file

@ -1,22 +1,50 @@
use crate::common::SubjectId;
use crate::common::{SubjectData, SubjectId};
use crate::module::EventHandler;
use crate::raw_event::{RawEvent, RawEventType};
use crate::SubjectMap;
use std::convert::Infallible;
use steamid_ng::SteamID;
pub struct ChatMessage {
struct BareChatMessage {
pub time: u32,
pub subject: SubjectId,
pub message: String,
pub chat_type: ChatType,
}
pub struct ChatMessage {
pub time: u32,
pub name: String,
pub steam_id: SteamID,
pub message: String,
pub chat_type: ChatType,
}
impl ChatMessage {
fn from_bare(bare: BareChatMessage, subjects: &SubjectMap) -> Self {
let (name, steam_id) = match &subjects[bare.subject] {
SubjectData::Player { name, steam_id, .. } => (name.clone(), steam_id.clone()),
_ => {
unreachable!("only player messages are added");
}
};
ChatMessage {
time: bare.time,
name,
steam_id,
message: bare.message,
chat_type: bare.chat_type,
}
}
}
pub enum ChatType {
All,
Team,
}
#[derive(Default)]
pub struct ChatHandler(Vec<ChatMessage>);
pub struct ChatHandler(Vec<BareChatMessage>);
impl EventHandler for ChatHandler {
type Output = Vec<ChatMessage>;
@ -36,13 +64,13 @@ impl EventHandler for ChatHandler {
return Ok(());
}
match event.ty {
RawEventType::SayTeam => self.0.push(ChatMessage {
RawEventType::SayTeam => self.0.push(BareChatMessage {
time,
subject,
message: event.params.trim_matches('"').into(),
chat_type: ChatType::Team,
}),
RawEventType::Say => self.0.push(ChatMessage {
RawEventType::Say => self.0.push(BareChatMessage {
time,
subject,
message: event.params.trim_matches('"').into(),
@ -53,7 +81,10 @@ impl EventHandler for ChatHandler {
Ok(())
}
fn finish(self) -> Self::Output {
fn finish(self, subjects: &SubjectMap) -> Self::Output {
self.0
.into_iter()
.map(|bare| ChatMessage::from_bare(bare, subjects))
.collect()
}
}

View file

@ -1,6 +1,7 @@
use crate::common::SubjectId;
use crate::module::EventHandler;
use crate::raw_event::{RawEvent, RawEventType};
use crate::SubjectMap;
use chrono::{DateTime, FixedOffset, NaiveDateTime, TimeZone, Utc};
use std::str::{FromStr, ParseBoolError};
use steamid_ng::SteamID;
@ -182,7 +183,7 @@ impl EventHandler for LobbySettingsHandler {
Ok(())
}
fn finish(self) -> Self::Output {
fn finish(self, _subjects: &SubjectMap) -> Self::Output {
if self.0.id > 0 {
Some(self.0)
} else {

View file

@ -1,5 +1,6 @@
use crate::common::SubjectId;
use crate::raw_event::{RawEvent, RawEventType};
use crate::SubjectMap;
pub use chat::{ChatHandler, ChatMessage, ChatType};
pub use lobbysettings::{
LobbySettingsError, LobbySettingsHandler, Location, Settings as LobbySettings,
@ -25,7 +26,7 @@ pub trait EventHandler: Default {
event: &RawEvent,
) -> Result<(), Self::Error>;
fn finish(self) -> Self::Output;
fn finish(self, subjects: &SubjectMap) -> Self::Output;
}
#[derive(Default)]
@ -57,8 +58,8 @@ impl<Head: EventHandler, Tail: EventHandler> EventHandler for HandlerStack<Head,
Ok(())
}
fn finish(self) -> Self::Output {
(self.head.finish(), self.tail.finish())
fn finish(self, subjects: &SubjectMap) -> Self::Output {
(self.head.finish(subjects), self.tail.finish(subjects))
}
}
@ -112,9 +113,9 @@ impl<Handler: EventHandler> EventHandler for OptionalHandler<Handler> {
Ok(())
}
fn finish(self) -> Self::Output {
fn finish(self, subjects: &SubjectMap) -> Self::Output {
match self {
OptionalHandler::Active(handler) => Ok(handler.finish()),
OptionalHandler::Active(handler) => Ok(handler.finish(subjects)),
OptionalHandler::Failed(e) => Err(e),
}
}