make irc update handler a separate function

This commit is contained in:
Nikita Vilunov 2023-02-16 16:33:44 +01:00
parent 1e17e017cf
commit 30db029390
1 changed files with 66 additions and 50 deletions

View File

@ -221,13 +221,34 @@ async fn handle_registered_socket<'a>(
} else { } else {
len len
}; };
handle_incoming_message(&buffer[0..len], &config, &user, &mut connection, &player_id, writer).await?; handle_incoming_message(&buffer[0..len], &config, &user, &mut connection, writer).await?;
buffer.clear(); buffer.clear();
}, },
update = connection.receiver.recv() => { update = connection.receiver.recv() => {
match update.unwrap() { if let Some(update) = update {
handle_update(&config, &user, &player_id, writer, &rooms, update).await?;
} else {
log::warn!("Player is terminated, must terminate the connection");
break;
}
}
}
}
connection.terminate().await;
Ok(())
}
async fn handle_update(
config: &ServerConfig,
user: &RegisteredUser,
player_id: &PlayerId,
writer: &mut (impl AsyncWrite + Unpin),
rooms: &RoomRegistry,
update: Updates
) -> Result<()> {
match update {
Updates::RoomJoined { new_member_id, room_id } => { Updates::RoomJoined { new_member_id, room_id } => {
if player_id == new_member_id { if player_id == &new_member_id {
if let Some(room) = rooms.get_room(&room_id) { if let Some(room) = rooms.get_room(&room_id) {
let room_info = room.get_room_info().await; let room_info = room.get_room_info().await;
let chan = Chan::Global(room_id.as_bytes().clone()); let chan = Chan::Global(room_id.as_bytes().clone());
@ -274,10 +295,6 @@ async fn handle_registered_socket<'a>(
writer.flush().await? writer.flush().await?
}, },
} }
}
}
}
connection.terminate().await;
Ok(()) Ok(())
} }
@ -286,7 +303,6 @@ async fn handle_incoming_message(
config: &ServerConfig, config: &ServerConfig,
user: &RegisteredUser, user: &RegisteredUser,
user_handle: &mut PlayerConnection, user_handle: &mut PlayerConnection,
player_id: &PlayerId,
writer: &mut (impl AsyncWrite + Unpin), writer: &mut (impl AsyncWrite + Unpin),
) -> Result<()> { ) -> Result<()> {
let parsed = client_message(buffer); let parsed = client_message(buffer);