diff --git a/src/bancho.rs b/src/bancho.rs index 5ace420..ddac2f1 100644 --- a/src/bancho.rs +++ b/src/bancho.rs @@ -583,17 +583,32 @@ impl Event { self.time } pub fn relates_to_channel(&self, channel: &Channel) -> bool { - self.kind.relates_to_channel(channel) + self.channel() == Some(channel) } pub fn relates_to_match(&self, match_id: MatchId) -> bool { - self.kind.relates_to_match(match_id) + self.relates_to_channel(&Channel::Multiplayer(match_id)) + } + pub fn channel(&self) -> Option<&Channel> { + match &self.kind { + EventKind::Join(c) | EventKind::Part(c) => Some(c), + EventKind::Message(m) => m.channel(), + EventKind::Bot(m) => m.channel(), + EventKind::Multiplayer(e) => Some(e.channel()), + _ => None, + } + } + pub fn user(&self) -> Option<&User> { + match &self.kind { + EventKind::Message(m) => Some(m.user()), + _ => None, + } } } impl From for Event { fn from(message: Message) -> Self { Event { - kind: message.into(), + kind: EventKind::Message(message), instant: Instant::now(), time: chrono::Utc::now(), } @@ -603,7 +618,7 @@ impl From for Event { impl From for Event { fn from(message: bot::Message) -> Self { Event { - kind: message.into(), + kind: EventKind::Bot(message), instant: Instant::now(), time: chrono::Utc::now(), } @@ -613,7 +628,7 @@ impl From for Event { impl From for Event { fn from(event: multiplayer::Event) -> Self { Event { - kind: event.into(), + kind: EventKind::Multiplayer(event), instant: Instant::now(), time: chrono::Utc::now(), } @@ -647,39 +662,6 @@ pub enum EventKind { Closed, } -impl EventKind { - pub fn relates_to_channel(&self, channel: &Channel) -> bool { - match self { - EventKind::Join(c) | EventKind::Part(c) => c == channel, - EventKind::Message(m) => m.channel() == Some(channel), - EventKind::Bot(m) => m.channel() == Some(channel), - EventKind::Multiplayer(m) => &m.channel() == channel, - _ => false, - } - } - pub fn relates_to_match(&self, match_id: MatchId) -> bool { - self.relates_to_channel(&Channel::Multiplayer(match_id)) - } -} - -impl From for EventKind { - fn from(message: Message) -> Self { - EventKind::Message(message) - } -} - -impl From for EventKind { - fn from(message: bot::Message) -> Self { - EventKind::Bot(message) - } -} - -impl From for EventKind { - fn from(event: multiplayer::Event) -> Self { - EventKind::Multiplayer(event) - } -} - struct Matcher {} impl Matcher { // const MULTIPLAYER_CHANNEL_TOPIC: Regex = Regex::new(r"^multiplayer game #(\d+)$").unwrap(); @@ -1232,7 +1214,7 @@ impl ClientActor { } } - if bot_message.is_multiplayer_event() { + if bot_message.is_multiplayer() { if let Some(channel) = bot_message.channel() { match channel { Channel::Multiplayer(match_id) => { @@ -1243,6 +1225,7 @@ impl ClientActor { self.event_tx .send( multiplayer::Event { + channel: channel.clone(), match_id: *match_id, match_internal_id: state.match_internal_id.unwrap(), kind, diff --git a/src/bancho/bot/message.rs b/src/bancho/bot/message.rs index 93c7313..e194859 100644 --- a/src/bancho/bot/message.rs +++ b/src/bancho/bot/message.rs @@ -385,16 +385,69 @@ impl Message { &self.kind } pub fn is_stateful(&self) -> bool { - self.kind.is_stateful() + match self.kind { + MessageKind::StatsStatus { .. } => true, + MessageKind::StatsScore { .. } => true, + MessageKind::StatsPlayCount { .. } => true, + MessageKind::StatsAccuracy { .. } => true, + + MessageKind::MpSettingsRoom { .. } => true, + MessageKind::MpSettingsMap { .. } => true, + MessageKind::MpSettingsTeamScoreModes { .. } => true, + MessageKind::MpSettingsMods { .. } => true, + MessageKind::MpSettingsSize { .. } => true, + MessageKind::MpSettingsSlot { .. } => true, + + MessageKind::MpMapResponseMap { .. } => true, + MessageKind::MpMapResponseGameMode { .. } => true, + + MessageKind::MpListRefsPrompt => true, + MessageKind::MpListRefsLineUser { .. } => true, + + _ => false, + } } - pub fn is_multiplayer_event(&self) -> bool { - self.kind.is_multiplayer_event() + pub fn is_multiplayer(&self) -> bool { + match self.kind { + MessageKind::EventPlayerJoined { .. } => true, + MessageKind::EventPlayerMoved { .. } => true, + MessageKind::EventPlayerTeam { .. } => true, + MessageKind::EventPlayerLeft { .. } => true, + MessageKind::EventHostChanged { .. } => true, + MessageKind::EventHostChangingMap => true, + MessageKind::EventMapChanged { .. } => true, + MessageKind::EventPlayersReady => true, + MessageKind::EventMatchStarted => true, + MessageKind::EventMatchPlayerResult { .. } => true, + + // This is not a an actual event, but a response to `!mp abort`. + // It is included here for convenience. + MessageKind::MpAbortResponse => true, + + MessageKind::EventMatchFinished => true, + MessageKind::EventStartTimer { .. } => true, + MessageKind::EventStartTimerEndLuck => true, + MessageKind::EventTimer { .. } => true, + MessageKind::EventTimerEnd => true, + _ => false, + } } pub fn is_maintenance(&self) -> bool { - self.kind.is_maintenance_event() + match self.kind { + MessageKind::EventMaintenanceAlert => true, + MessageKind::EventMaintenanceTimer { .. } => true, + MessageKind::EventMaintenanceRightBack => true, + _ => false, + } } pub fn maintenance(&self) -> Option { - self.kind.maintenance() + match self.kind { + // The first two events do not contain timer information. + MessageKind::EventMaintenanceAlert => None, + MessageKind::EventMaintenanceTimer { time } => Some(time.clone()), + MessageKind::EventMaintenanceRightBack => Some(Duration::ZERO), + _ => None, + } } } @@ -792,74 +845,6 @@ pub enum MessageKind { EventMaintenanceRightBack, } -impl MessageKind { - pub fn is_stateful(&self) -> bool { - match self { - Self::StatsStatus { .. } => true, - Self::StatsScore { .. } => true, - Self::StatsPlayCount { .. } => true, - Self::StatsAccuracy { .. } => true, - - Self::MpSettingsRoom { .. } => true, - Self::MpSettingsMap { .. } => true, - Self::MpSettingsTeamScoreModes { .. } => true, - Self::MpSettingsMods { .. } => true, - Self::MpSettingsSize { .. } => true, - Self::MpSettingsSlot { .. } => true, - - Self::MpMapResponseMap { .. } => true, - Self::MpMapResponseGameMode { .. } => true, - - Self::MpListRefsPrompt => true, - Self::MpListRefsLineUser { .. } => true, - - _ => false, - } - } - pub fn is_multiplayer_event(&self) -> bool { - match self { - Self::EventPlayerJoined { .. } => true, - Self::EventPlayerMoved { .. } => true, - Self::EventPlayerTeam { .. } => true, - Self::EventPlayerLeft { .. } => true, - Self::EventHostChanged { .. } => true, - Self::EventHostChangingMap => true, - Self::EventMapChanged { .. } => true, - Self::EventPlayersReady => true, - Self::EventMatchStarted => true, - Self::EventMatchPlayerResult { .. } => true, - - // This is not a an actual event, but a response to `!mp abort`. - // It is included here for convenience. - Self::MpAbortResponse => true, - - Self::EventMatchFinished => true, - Self::EventStartTimer { .. } => true, - Self::EventStartTimerEndLuck => true, - Self::EventTimer { .. } => true, - Self::EventTimerEnd => true, - _ => false, - } - } - pub fn is_maintenance_event(&self) -> bool { - match self { - Self::EventMaintenanceAlert => true, - Self::EventMaintenanceTimer { .. } => true, - Self::EventMaintenanceRightBack => true, - _ => false, - } - } - pub fn maintenance(&self) -> Option { - match self { - // The first two events do not contain timer information. - Self::EventMaintenanceAlert => None, - Self::EventMaintenanceTimer { time } => Some(time.clone()), - Self::EventMaintenanceRightBack => Some(Duration::ZERO), - _ => None, - } - } -} - /// This is a general matcher for a collection of regex patterns. /// /// By design [`RegexSet`] is only for matching, not capturing. This matcher is diff --git a/src/bancho/multiplayer.rs b/src/bancho/multiplayer.rs index 8efecd2..25a3664 100644 --- a/src/bancho/multiplayer.rs +++ b/src/bancho/multiplayer.rs @@ -444,6 +444,7 @@ impl FromStr for GameMode { #[derive(Debug, Clone)] pub struct Event { + pub(super) channel: Channel, pub(super) match_id: MatchId, pub(super) match_internal_id: MatchInternalId, pub(super) kind: EventKind, @@ -456,8 +457,8 @@ impl Event { pub fn match_internal_id(&self) -> MatchInternalId { self.match_internal_id } - pub fn channel(&self) -> Channel { - Channel::Multiplayer(self.match_id) + pub fn channel(&self) -> &Channel { + &self.channel } pub fn kind(&self) -> &EventKind { &self.kind