better response grouping
authoralfadur <mail@none>
Tue, 14 Jan 2020 01:30:03 +0300
changeset 15565 02f648f7cbe1
parent 15564 3b0b6ce76303
child 15566 77551d5e0a3b
better response grouping
rust/hedgewars-server/src/handlers.rs
rust/hedgewars-server/src/handlers/actions.rs
rust/hedgewars-server/src/handlers/common.rs
--- a/rust/hedgewars-server/src/handlers.rs	Sat Jan 11 05:15:01 2020 +0100
+++ b/rust/hedgewars-server/src/handlers.rs	Tue Jan 14 01:30:03 2020 +0300
@@ -460,8 +460,8 @@
             };
             response.add(start_msg.send_self());
 
-            common::get_room_config_impl(&replay.config, client_id, response);
-            common::get_teams(replay.teams.iter(), client_id, response);
+            common::get_room_config_impl(&replay.config, Destination::ToSelf, response);
+            common::get_teams(replay.teams.iter(), Destination::ToSelf, response);
             response.add(RunGame.send_self());
             response.add(ForwardEngineMessage(replay.message_log).send_self());
 
--- a/rust/hedgewars-server/src/handlers/actions.rs	Sat Jan 11 05:15:01 2020 +0100
+++ b/rust/hedgewars-server/src/handlers/actions.rs	Tue Jan 14 01:30:03 2020 +0300
@@ -12,6 +12,7 @@
 use rand::{distributions::Uniform, thread_rng, Rng};
 use std::{io, io::Write, iter::once, mem::replace};
 
+#[derive(Clone)]
 pub enum DestinationGroup {
     All,
     Lobby,
@@ -19,6 +20,7 @@
     Protocol(u16),
 }
 
+#[derive(Clone)]
 pub enum Destination {
     ToId(ClientId),
     ToIds(Vec<ClientId>),
@@ -112,4 +114,10 @@
     pub fn send_all(self) -> PendingMessage {
         PendingMessage::send_all(self)
     }
+    pub fn send_to_destination(self, destination: Destination) -> PendingMessage {
+        PendingMessage {
+            destination,
+            message: self,
+        }
+    }
 }
--- a/rust/hedgewars-server/src/handlers/common.rs	Sat Jan 11 05:15:01 2020 +0100
+++ b/rust/hedgewars-server/src/handlers/common.rs	Tue Jan 14 01:30:03 2020 +0300
@@ -17,7 +17,10 @@
     utils::to_engine_msg,
 };
 
-use super::Response;
+use super::{
+    actions::{Destination, DestinationGroup},
+    Response,
+};
 
 use crate::core::types::RoomConfig;
 use rand::{self, seq::SliceRandom, thread_rng, Rng};
@@ -165,8 +168,8 @@
         }
     }
 
-    get_room_teams(room, client.id, response);
-    get_room_config(room, client.id, response);
+    get_room_teams(room, Destination::ToSelf, response);
+    get_room_config(room, Destination::ToSelf, response);
 
     if !room.greeting.is_empty() {
         response.add(
@@ -341,41 +344,52 @@
     response.add(update_msg.send_all().with_protocol(room.protocol_number));
 }
 
-pub fn get_room_config_impl(config: &RoomConfig, to_client: ClientId, response: &mut Response) {
-    response.add(ConfigEntry("FULLMAPCONFIG".to_string(), config.to_map_config()).send(to_client));
+pub fn get_room_config_impl(
+    config: &RoomConfig,
+    destination: Destination,
+    response: &mut Response,
+) {
+    response.add(
+        ConfigEntry("FULLMAPCONFIG".to_string(), config.to_map_config())
+            .send_to_destination(destination.clone()),
+    );
     for cfg in config.to_game_config() {
-        response.add(cfg.to_server_msg().send(to_client));
+        response.add(cfg.to_server_msg().send_to_destination(destination.clone()));
     }
 }
 
-pub fn get_room_config(room: &HwRoom, to_client: ClientId, response: &mut Response) {
-    get_room_config_impl(room.active_config(), to_client, response);
+pub fn get_room_config(room: &HwRoom, destination: Destination, response: &mut Response) {
+    get_room_config_impl(room.active_config(), destination, response);
 }
 
-pub fn get_teams<'a, I>(teams: I, to_client: ClientId, response: &mut Response)
+pub fn get_teams<'a, I>(teams: I, destination: Destination, response: &mut Response)
 where
     I: Iterator<Item = &'a TeamInfo>,
 {
     for team in teams {
-        response.add(TeamAdd(team.to_protocol()).send(to_client));
-        response.add(TeamColor(team.name.clone(), team.color).send(to_client));
-        response.add(HedgehogsNumber(team.name.clone(), team.hedgehogs_number).send(to_client));
+        response.add(TeamAdd(team.to_protocol()).send_to_destination(destination.clone()));
+        response
+            .add(TeamColor(team.name.clone(), team.color).send_to_destination(destination.clone()));
+        response.add(
+            HedgehogsNumber(team.name.clone(), team.hedgehogs_number)
+                .send_to_destination(destination.clone()),
+        );
     }
 }
 
-pub fn get_room_teams(room: &HwRoom, to_client: ClientId, response: &mut Response) {
+pub fn get_room_teams(room: &HwRoom, destination: Destination, response: &mut Response) {
     let current_teams = match room.game_info {
         Some(ref info) => &info.original_teams,
         None => &room.teams,
     };
 
-    get_teams(current_teams.iter().map(|(_, t)| t), to_client, response);
+    get_teams(current_teams.iter().map(|(_, t)| t), destination, response);
 }
 
 pub fn get_room_flags(
     server: &HwServer,
     room_id: RoomId,
-    to_client: ClientId,
+    destination: Destination,
     response: &mut Response,
 ) {
     let room = server.room(room_id);
@@ -385,13 +399,14 @@
                 add_flags(&[Flags::RoomMaster]),
                 vec![server.client(id).nick.clone()],
             )
-            .send(to_client),
+            .send_to_destination(destination.clone()),
         );
     }
     let nicks = server.collect_nicks(|(_, c)| c.room_id == Some(room_id) && c.is_ready());
 
     if !nicks.is_empty() {
-        response.add(ClientFlags(add_flags(&[Flags::Ready]), nicks).send(to_client));
+        response
+            .add(ClientFlags(add_flags(&[Flags::Ready]), nicks).send_to_destination(destination));
     }
 }
 
@@ -511,9 +526,11 @@
 
                     super::common::get_room_update(None, room, room_master, response);
 
-                    for client_id in room_control.server().room_client_ids(room.id) {
-                        super::common::get_room_config(room, client_id, response);
-                    }
+                    let room_destination = Destination::ToAll {
+                        group: DestinationGroup::Room(room.id),
+                        skip_self: false,
+                    };
+                    super::common::get_room_config(room, room_destination, response);
                 }
             }
             VoteType::Pause => {
@@ -595,7 +612,7 @@
     );
 
     for client_id in result.joined_mid_game_clients {
-        super::common::get_room_config(room, client_id, response);
+        super::common::get_room_config(room, Destination::ToId(client_id), response);
     }
 
     if !result.unreadied_nicks.is_empty() {