aboutsummaryrefslogtreecommitdiffstats
path: root/mumd/src
diff options
context:
space:
mode:
Diffstat (limited to 'mumd/src')
-rw-r--r--mumd/src/audio.rs1
-rw-r--r--mumd/src/network/tcp.rs8
-rw-r--r--mumd/src/state.rs102
3 files changed, 54 insertions, 57 deletions
diff --git a/mumd/src/audio.rs b/mumd/src/audio.rs
index 65da656..53ec2d4 100644
--- a/mumd/src/audio.rs
+++ b/mumd/src/audio.rs
@@ -188,7 +188,6 @@ impl Audio {
.map(|(path, event)| {
let reader = hound::WavReader::open(path).unwrap();
let spec = reader.spec();
- debug!("{:?}", spec);
let samples = match spec.sample_format {
hound::SampleFormat::Float => reader.into_samples::<f32>().map(|e| e.unwrap()).collect::<Vec<_>>(),
hound::SampleFormat::Int => reader.into_samples::<i16>().map(|e| cpal::Sample::to_f32(&e.unwrap())).collect::<Vec<_>>(),
diff --git a/mumd/src/network/tcp.rs b/mumd/src/network/tcp.rs
index 131f066..2a0d01e 100644
--- a/mumd/src/network/tcp.rs
+++ b/mumd/src/network/tcp.rs
@@ -247,13 +247,7 @@ async fn listen(
warn!("Login rejected: {:?}", msg);
}
ControlPacket::UserState(msg) => {
- let mut state = state.lock().unwrap();
- if *state.phase_receiver().borrow() == StatePhase::Connecting {
- state.audio_mut().add_client(msg.get_session());
- state.parse_user_state(*msg);
- } else {
- state.parse_user_state(*msg);
- }
+ state.lock().unwrap().parse_user_state(*msg);
}
ControlPacket::UserRemove(msg) => {
state.lock().unwrap().remove_client(*msg);
diff --git a/mumd/src/state.rs b/mumd/src/state.rs
index 2c90b7c..e7c16ec 100644
--- a/mumd/src/state.rs
+++ b/mumd/src/state.rs
@@ -384,25 +384,29 @@ impl State {
}
}
- pub fn parse_user_state(&mut self, msg: msgs::UserState) -> Option<UserDiff> {
+ pub fn parse_user_state(&mut self, msg: msgs::UserState) {
if !msg.has_session() {
warn!("Can't parse user state without session");
- return None;
+ return;
}
let session = msg.get_session();
// check if this is initial state
if !self.server().unwrap().users().contains_key(&session) {
- self.parse_initial_user_state(session, msg);
- None
+ self.create_user(msg);
} else {
- Some(self.parse_updated_user_state(session, msg))
+ self.update_user(msg);
}
}
- fn parse_initial_user_state(&mut self, session: u32, msg: msgs::UserState) {
+ fn create_user(&mut self, msg: msgs::UserState) {
if !msg.has_name() {
warn!("Missing name in initial user state");
- } else if msg.get_name() == self.server().unwrap().username().unwrap() {
+ return;
+ }
+
+ let session = msg.get_session();
+
+ if msg.get_name() == self.server().unwrap().username().unwrap() {
// this is us
*self.server_mut().unwrap().session_id_mut() = Some(session);
} else {
@@ -411,31 +415,28 @@ impl State {
// send notification only if we've passed the connecting phase
if *self.phase_receiver().borrow() == StatePhase::Connected {
- let channel_id = if msg.has_channel_id() {
- msg.get_channel_id()
- } else {
- 0
- };
+ 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()
));
- self.audio.play_effect(NotificationEvents::ServerConnect);
}
}
}
+
self.server_mut()
.unwrap()
.users_mut()
.insert(session, user::User::new(msg));
}
- fn parse_updated_user_state(&mut self, session: u32, msg: msgs::UserState) -> UserDiff {
- let user = self
- .server_mut()
- .unwrap()
+ fn update_user(&mut self, msg: msgs::UserState) {
+ let session = msg.get_session();
+ let server = self.server_mut().unwrap();
+
+ let user = server
.users_mut()
.get_mut(&session)
.unwrap();
@@ -453,40 +454,42 @@ impl State {
let diff = UserDiff::from(msg);
user.apply_user_diff(&diff);
- let user = self.server().unwrap().users().get(&session).unwrap();
-
- // send notification if the user moved to or from any channel
- //TODO our channel only
- if let Some(channel_id) = diff.channel_id {
- if let Some(channel) = self.server().unwrap().channels().get(&channel_id) {
- notify::send(format!(
- "{} moved to channel {}",
- &user.name(),
- channel.name()
- ));
- } else {
- warn!("{} moved to invalid channel {}", &user.name(), channel_id);
+
+ let user = server
+ .users()
+ .get(&session)
+ .unwrap();
+
+ if Some(session) != server.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);
+ }
}
- }
- // send notification if a user muted/unmuted
- //TODO our channel only
- let notify_desc = match (mute, deaf) {
- (Some(true), Some(true)) => Some(format!("{} muted and deafend themselves", &user.name())),
- (Some(false), Some(false)) => Some(format!("{} unmuted and undeafend themselves", &user.name())),
- (None, Some(true)) => Some(format!("{} deafend themselves", &user.name())),
- (None, Some(false)) => Some(format!("{} undeafend themselves", &user.name())),
- (Some(true), None) => Some(format!("{} muted themselves", &user.name())),
- (Some(false), None) => Some(format!("{} unmuted themselves", &user.name())),
- (Some(true), Some(false)) => Some(format!("{} muted and undeafened themselves", &user.name())),
- (Some(false), Some(true)) => Some(format!("{} unmuted and deafened themselves", &user.name())),
- (None, None) => None,
- };
- if let Some(notify_desc) = notify_desc {
- notify::send(notify_desc);
+ //send notification if a user muted/unmuted
+ let notify_desc = match (mute, deaf) {
+ (Some(true), Some(true)) => Some(format!("{} muted and deafend themselves", &user.name())),
+ (Some(false), Some(false)) => Some(format!("{} unmuted and undeafend themselves", &user.name())),
+ (None, Some(true)) => Some(format!("{} deafend themselves", &user.name())),
+ (None, Some(false)) => Some(format!("{} undeafend themselves", &user.name())),
+ (Some(true), None) => Some(format!("{} muted themselves", &user.name())),
+ (Some(false), None) => Some(format!("{} unmuted themselves", &user.name())),
+ (Some(true), Some(false)) => Some(format!("{} muted and undeafened themselves", &user.name())),
+ (Some(false), Some(true)) => Some(format!("{} unmuted and deafened themselves", &user.name())),
+ (None, None) => None,
+ };
+ if let Some(notify_desc) = notify_desc {
+ notify::send(notify_desc);
+ }
}
-
- diff
}
pub fn remove_client(&mut self, msg: msgs::UserRemove) {
@@ -516,11 +519,12 @@ impl State {
}
}
- pub fn initialized(&self) {
+ pub fn initialized(&mut self) {
self.phase_watcher
.0
.broadcast(StatePhase::Connected)
.unwrap();
+ self.audio.play_effect(NotificationEvents::ServerConnect);
}
pub fn audio(&self) -> &Audio {