diff options
| author | Eskil Queseth <eskilq@kth.se> | 2020-11-16 02:02:46 +0100 |
|---|---|---|
| committer | Eskil Queseth <eskilq@kth.se> | 2020-11-16 02:02:46 +0100 |
| commit | c3fabb1f9224cdf8fd68ad186baeffc93c96ce5c (patch) | |
| tree | 0aa213b808210be84d5a53f5a40d03c83d038155 /mumd/src | |
| parent | 8b942cffeb81dbf068dbe7c76584bf68e74e7f17 (diff) | |
| download | mum-c3fabb1f9224cdf8fd68ad186baeffc93c96ce5c.tar.gz | |
add sound on users moving
Diffstat (limited to 'mumd/src')
| -rw-r--r-- | mumd/src/audio.rs | 10 | ||||
| -rw-r--r-- | mumd/src/state.rs | 64 |
2 files changed, 52 insertions, 22 deletions
diff --git a/mumd/src/audio.rs b/mumd/src/audio.rs index 53ec2d4..d1ef106 100644 --- a/mumd/src/audio.rs +++ b/mumd/src/audio.rs @@ -17,6 +17,10 @@ use crate::audio::output::SaturatingAdd; pub const EVENT_SOUNDS: &[(&str, NotificationEvents)] = &[ ("resources/connect.wav", NotificationEvents::ServerConnect), ("resources/disconnect.wav", NotificationEvents::ServerDisconnect), + ("resources/channel_join.wav", NotificationEvents::UserConnected), + ("resources/channel_leave.wav", NotificationEvents::UserDisconnected), + ("resources/channel_join.wav", NotificationEvents::UserJoinedChannel), + ("resources/channel_leave.wav", NotificationEvents::UserLeftChannel), ]; const SAMPLE_RATE: u32 = 48000; @@ -25,6 +29,10 @@ const SAMPLE_RATE: u32 = 48000; pub enum NotificationEvents { ServerConnect, ServerDisconnect, + UserConnected, + UserDisconnected, + UserJoinedChannel, + UserLeftChannel, } pub struct Audio { @@ -296,7 +304,7 @@ impl Audio { } } - pub fn play_effect(&mut self, effect: NotificationEvents) { + pub fn play_effect(&self, effect: NotificationEvents) { let samples = self.sounds.get(&effect).unwrap(); let mut play_sounds = self.play_sounds.lock().unwrap(); diff --git a/mumd/src/state.rs b/mumd/src/state.rs index e7c16ec..69bf4df 100644 --- a/mumd/src/state.rs +++ b/mumd/src/state.rs @@ -218,6 +218,7 @@ impl State { .0 .broadcast(StatePhase::Disconnected) .unwrap(); + self.audio.play_effect(NotificationEvents::ServerDisconnect); now!(Ok(None)) } Command::ConfigReload => { @@ -416,12 +417,17 @@ impl State { // send notification only if we've passed the connecting phase if *self.phase_receiver().borrow() == StatePhase::Connected { let channel_id = msg.get_channel_id(); - if let Some(channel) = self.server().unwrap().channels().get(&channel_id) { - notify::send(format!( - "{} connected and joined {}", - &msg.get_name(), - channel.name() - )); + + if channel_id == self.get_users_channel(self.server().unwrap().session_id().unwrap()) { + if let Some(channel) = self.server().unwrap().channels().get(&channel_id) { + notify::send(format!( + "{} connected and joined {}", + &msg.get_name(), + channel.name() + )); + } + + self.audio.play_effect(NotificationEvents::UserConnected); } } } @@ -434,9 +440,11 @@ impl State { fn update_user(&mut self, msg: msgs::UserState) { let session = msg.get_session(); - let server = self.server_mut().unwrap(); - let user = server + let from_channel = self.get_users_channel(session); + + let user = self.server_mut() + .unwrap() .users_mut() .get_mut(&session) .unwrap(); @@ -455,22 +463,27 @@ impl State { let diff = UserDiff::from(msg); user.apply_user_diff(&diff); - let user = server + let user = self.server() + .unwrap() .users() .get(&session) .unwrap(); - if Some(session) != server.session_id() { + if Some(session) != self.server().unwrap().session_id() { //send notification if the user moved to or from any channel - if let Some(channel_id) = diff.channel_id { - if let Some(channel) = server.channels().get(&channel_id) { - notify::send(format!( - "{} moved to channel {}", - user.name(), - channel.name() - )); - } else { - warn!("{} moved to invalid channel {}", user.name(), channel_id); + if let Some(to_channel) = diff.channel_id { + let this_channel = self.get_users_channel(self.server().unwrap().session_id().unwrap()); + if from_channel == this_channel || to_channel == this_channel { + if let Some(channel) = self.server().unwrap().channels().get(&to_channel) { + notify::send(format!( + "{} moved to channel {}", + user.name(), + channel.name() + )); + } else { + warn!("{} moved to invalid channel {}", user.name(), to_channel); + } + self.audio.play_effect(if from_channel == this_channel { NotificationEvents::UserJoinedChannel } else { NotificationEvents::UserLeftChannel }); } } @@ -497,8 +510,14 @@ impl State { warn!("Tried to remove user state without session"); return; } - if let Some(user) = self.server().unwrap().users().get(&msg.get_session()) { - notify::send(format!("{} disconnected", &user.name())); + + let this_channel = self.get_users_channel(self.server().unwrap().session_id().unwrap()); + let other_channel = self.get_users_channel(msg.get_session()); + if this_channel == other_channel { + self.audio.play_effect(NotificationEvents::UserDisconnected); + if let Some(user) = self.server().unwrap().users().get(&msg.get_session()) { + notify::send(format!("{} disconnected", &user.name())); + } } self.audio().remove_client(msg.get_session()); @@ -548,4 +567,7 @@ impl State { pub fn username(&self) -> Option<&str> { self.server.as_ref().map(|e| e.username()).flatten() } + fn get_users_channel(&self, user_id: u32) -> u32 { + self.server().unwrap().users().iter().find(|e| *e.0 == user_id).unwrap().1.channel() + } } |
