fix in-game team handling
authoralfadur
Sun, 12 Apr 2020 00:02:11 +0300
changeset 15575 852682c2003f
parent 15574 0031683bfa76
child 15576 36687bc9d5c1
fix in-game team handling
rust/hedgewars-server/src/core/room.rs
rust/hedgewars-server/src/core/server.rs
rust/hedgewars-server/src/handlers/common.rs
--- a/rust/hedgewars-server/src/core/room.rs	Sat Apr 11 23:20:09 2020 +0300
+++ b/rust/hedgewars-server/src/core/room.rs	Sun Apr 12 00:02:11 2020 +0300
@@ -26,6 +26,7 @@
 pub struct GameInfo {
     pub original_teams: Vec<(ClientId, TeamInfo)>,
     pub left_teams: Vec<String>,
+    pub ingame_teams_count: u8,
     pub msg_log: Vec<String>,
     pub sync_msg: Option<String>,
     pub is_paused: bool,
@@ -39,6 +40,7 @@
             msg_log: Vec::new(),
             sync_msg: None,
             is_paused: false,
+            ingame_teams_count: teams.len() as u8,
             original_teams: teams,
             original_config: config,
         }
@@ -145,6 +147,7 @@
             self.teams.remove(index);
 
             if let Some(info) = &mut self.game_info {
+                info.ingame_teams_count -= 1;
                 info.left_teams.push(team_name.to_string());
 
                 if let Some(m) = &info.sync_msg {
@@ -173,9 +176,7 @@
     }
 
     pub fn teams_in_game(&self) -> Option<u8> {
-        self.game_info
-            .as_ref()
-            .map(|info| (info.original_teams.len() - info.left_teams.len()) as u8)
+        self.game_info.as_ref().map(|info| info.ingame_teams_count)
     }
 
     pub fn find_team_and_owner_mut<F>(&mut self, f: F) -> Option<(ClientId, &mut TeamInfo)>
--- a/rust/hedgewars-server/src/core/server.rs	Sat Apr 11 23:20:09 2020 +0300
+++ b/rust/hedgewars-server/src/core/server.rs	Sun Apr 12 00:02:11 2020 +0300
@@ -989,12 +989,15 @@
         if client_left {
             client.set_is_in_game(false);
 
-            let team_names: Vec<_> = room
-                .client_teams(client.id)
-                .map(|t| t.name.clone())
-                .collect();
+            if let Some(ref mut info) = room.game_info {
+                let team_names: Vec<_> = info
+                    .client_teams(client.id)
+                    .map(|t| t.name.clone())
+                    .collect();
 
-            if let Some(ref mut info) = room.game_info {
+                info.left_teams.extend(team_names.iter().cloned());
+                info.ingame_teams_count -= team_names.len() as u8;
+
                 for team_name in &team_names {
                     let remove_msg =
                         utils::to_engine_msg(std::iter::once(b'F').chain(team_name.bytes()));
@@ -1008,7 +1011,7 @@
                 }
                 Some(team_names)
             } else {
-                unreachable!();
+                None
             }
         } else {
             None
@@ -1125,6 +1128,7 @@
 
         if !team_names.is_empty() {
             info.left_teams.retain(|name| !team_names.contains(&name));
+            info.ingame_teams_count += team_names.len() as u8;
         }
     }
 }
--- a/rust/hedgewars-server/src/handlers/common.rs	Sat Apr 11 23:20:09 2020 +0300
+++ b/rust/hedgewars-server/src/handlers/common.rs	Sun Apr 12 00:02:11 2020 +0300
@@ -168,8 +168,8 @@
         }
     }
 
-    get_room_teams(room, Destination::ToSelf, response);
-    get_room_config(room, Destination::ToSelf, response);
+    get_active_room_teams(room, Destination::ToSelf, response);
+    get_active_room_config(room, Destination::ToSelf, response);
 
     if !room.greeting.is_empty() {
         response.add(
@@ -358,7 +358,7 @@
     }
 }
 
-pub fn get_room_config(room: &HwRoom, destination: Destination, response: &mut Response) {
+pub fn get_active_room_config(room: &HwRoom, destination: Destination, response: &mut Response) {
     get_room_config_impl(room.active_config(), destination, response);
 }
 
@@ -377,7 +377,7 @@
     }
 }
 
-pub fn get_room_teams(room: &HwRoom, destination: Destination, response: &mut Response) {
+pub fn get_active_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,
@@ -530,7 +530,7 @@
                         group: DestinationGroup::Room(room.id),
                         skip_self: false,
                     };
-                    super::common::get_room_config(room, room_destination, response);
+                    super::common::get_active_room_config(room, room_destination, response);
                 }
             }
             VoteType::Pause => {
@@ -618,7 +618,7 @@
                 .send_to_destination(midgame_destination.clone()),
         );
     }
-    super::common::get_room_config(room, midgame_destination.clone(), response);
+    super::common::get_active_room_config(room, midgame_destination.clone(), response);
 
     if !result.unreadied_nicks.is_empty() {
         response.add(