Skip to content

Commit

Permalink
chore: removing all sub-event kind public methods
Browse files Browse the repository at this point in the history
  • Loading branch information
y5c4l3 committed Oct 19, 2023
1 parent 3407fbf commit 4637c76
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 114 deletions.
61 changes: 22 additions & 39 deletions src/bancho.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Message> for Event {
fn from(message: Message) -> Self {
Event {
kind: message.into(),
kind: EventKind::Message(message),
instant: Instant::now(),
time: chrono::Utc::now(),
}
Expand All @@ -603,7 +618,7 @@ impl From<Message> for Event {
impl From<bot::Message> for Event {
fn from(message: bot::Message) -> Self {
Event {
kind: message.into(),
kind: EventKind::Bot(message),
instant: Instant::now(),
time: chrono::Utc::now(),
}
Expand All @@ -613,7 +628,7 @@ impl From<bot::Message> for Event {
impl From<multiplayer::Event> for Event {
fn from(event: multiplayer::Event) -> Self {
Event {
kind: event.into(),
kind: EventKind::Multiplayer(event),
instant: Instant::now(),
time: chrono::Utc::now(),
}
Expand Down Expand Up @@ -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<Message> for EventKind {
fn from(message: Message) -> Self {
EventKind::Message(message)
}
}

impl From<bot::Message> for EventKind {
fn from(message: bot::Message) -> Self {
EventKind::Bot(message)
}
}

impl From<multiplayer::Event> 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();
Expand Down Expand Up @@ -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) => {
Expand All @@ -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,
Expand Down
131 changes: 58 additions & 73 deletions src/bancho/bot/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Duration> {
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,
}
}
}

Expand Down Expand Up @@ -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<Duration> {
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
Expand Down
5 changes: 3 additions & 2 deletions src/bancho/multiplayer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down

0 comments on commit 4637c76

Please sign in to comment.