backout a798e6441a36
authoralfadur <mail@none>
Sat, 11 Jan 2020 00:44:25 +0300
changeset 15562 479911540e17
parent 15561 d8326878e933
child 15563 d122b65bdf6f
backout a798e6441a36
rust/hedgewars-server/src/core/room.rs
rust/hedgewars-server/src/core/server.rs
rust/hedgewars-server/src/handlers/common.rs
rust/hedgewars-server/src/handlers/inanteroom.rs
rust/hedgewars-server/src/handlers/inroom.rs
--- a/rust/hedgewars-server/src/core/room.rs	Tue Jan 07 15:17:22 2020 +0100
+++ b/rust/hedgewars-server/src/core/room.rs	Sat Jan 11 00:44:25 2020 +0300
@@ -13,7 +13,19 @@
 pub const MAX_TEAMS_IN_ROOM: u8 = 8;
 pub const MAX_HEDGEHOGS_IN_ROOM: u8 = MAX_TEAMS_IN_ROOM * MAX_HEDGEHOGS_PER_TEAM;
 
+fn client_teams_impl(
+    teams: &[(ClientId, TeamInfo)],
+    client_id: ClientId,
+) -> impl Iterator<Item = &TeamInfo> + Clone {
+    teams
+        .iter()
+        .filter(move |(id, _)| *id == client_id)
+        .map(|(_, t)| t)
+}
+
 pub struct GameInfo {
+    pub teams_in_game: u8,
+    pub teams_at_start: Vec<(ClientId, TeamInfo)>,
     pub left_teams: Vec<String>,
     pub msg_log: Vec<String>,
     pub sync_msg: Option<String>,
@@ -28,9 +40,15 @@
             msg_log: Vec::new(),
             sync_msg: None,
             is_paused: false,
+            teams_in_game: teams.len() as u8,
+            teams_at_start: teams,
             config,
         }
     }
+
+    pub fn client_teams(&self, client_id: ClientId) -> impl Iterator<Item = &TeamInfo> + Clone {
+        client_teams_impl(&self.teams_at_start, client_id)
+    }
 }
 
 #[derive(Serialize, Deserialize)]
@@ -126,8 +144,11 @@
 
     pub fn remove_team(&mut self, team_name: &str) {
         if let Some(index) = self.teams.iter().position(|(_, t)| t.name == team_name) {
+            self.teams.remove(index);
+
             if let Some(info) = &mut self.game_info {
                 info.left_teams.push(team_name.to_string());
+                info.teams_in_game -= 1;
 
                 if let Some(m) = &info.sync_msg {
                     info.msg_log.push(m.clone());
@@ -136,15 +157,17 @@
                 let remove_msg =
                     crate::utils::to_engine_msg(iter::once(b'F').chain(team_name.bytes()));
                 info.msg_log.push(remove_msg.clone());
-            } else {
-                self.teams.remove(index);
             }
         }
     }
 
     pub fn set_hedgehogs_number(&mut self, n: u8) -> Vec<String> {
         let mut names = Vec::new();
-        let teams = &mut self.teams;
+        let teams = match self.game_info {
+            Some(ref mut info) => &mut info.teams_at_start,
+            None => &mut self.teams,
+        };
+
         if teams.len() as u8 * n <= MAX_HEDGEHOGS_IN_ROOM {
             for (_, team) in teams.iter_mut() {
                 team.hedgehogs_number = n;
@@ -155,12 +178,6 @@
         names
     }
 
-    pub fn teams_in_game(&self) -> Option<u8> {
-        self.game_info
-            .as_ref()
-            .map(|info| (self.teams.len() - info.left_teams.len()) as u8)
-    }
-
     pub fn find_team_and_owner_mut<F>(&mut self, f: F) -> Option<(ClientId, &mut TeamInfo)>
     where
         F: Fn(&TeamInfo) -> bool,
@@ -180,11 +197,8 @@
             .find_map(|(_, t)| Some(t).filter(|t| f(&t)))
     }
 
-    pub fn client_teams(&self, client_id: ClientId) -> impl Iterator<Item = &TeamInfo> + Clone {
-        self.teams
-            .iter()
-            .filter(move |(id, _)| *id == client_id)
-            .map(|(_, t)| t)
+    pub fn client_teams(&self, client_id: ClientId) -> impl Iterator<Item = &TeamInfo> {
+        client_teams_impl(&self.teams, client_id)
     }
 
     pub fn client_team_indices(&self, client_id: ClientId) -> Vec<u8> {
--- a/rust/hedgewars-server/src/core/server.rs	Tue Jan 07 15:17:22 2020 +0100
+++ b/rust/hedgewars-server/src/core/server.rs	Sat Jan 11 00:44:25 2020 +0300
@@ -986,6 +986,8 @@
                 .collect();
 
             if let Some(ref mut info) = room.game_info {
+                info.teams_in_game -= 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()));
@@ -1104,15 +1106,21 @@
     client.set_is_joined_mid_game(room.game_info.is_some());
     client.set_is_in_game(room.game_info.is_some());
 
-    let teams = room.client_teams(client.id);
-    client.teams_in_game = teams.clone().count() as u8;
-    client.clan = teams.clone().next().map(|t| t.color);
-    let team_names: Vec<_> = teams.map(|t| t.name.clone()).collect();
+    if let Some(ref mut info) = room.game_info {
+        let teams = info.client_teams(client.id);
+        client.teams_in_game = teams.clone().count() as u8;
+        client.clan = teams.clone().next().map(|t| t.color);
+        let team_names: Vec<_> = teams.map(|t| t.name.clone()).collect();
 
-    match room.game_info {
-        Some(ref mut info) if !team_names.is_empty() => {
-            info.left_teams.retain(|name| !team_names.contains(&name))
+        if !team_names.is_empty() {
+            info.left_teams.retain(|name| !team_names.contains(&name));
+            info.teams_in_game += team_names.len() as u8;
+            room.teams = info
+                .teams_at_start
+                .iter()
+                .filter(|(_, t)| !team_names.contains(&t.name))
+                .cloned()
+                .collect();
         }
-        _ => (),
     }
 }
--- a/rust/hedgewars-server/src/handlers/common.rs	Tue Jan 07 15:17:22 2020 +0100
+++ b/rust/hedgewars-server/src/handlers/common.rs	Sat Jan 11 00:44:25 2020 +0300
@@ -195,7 +195,7 @@
             .send_self(),
         );
 
-        for team in room.client_teams(client.id) {
+        for team in info.client_teams(client.id) {
             response.add(
                 ForwardEngineMessage(vec![to_engine_msg(once(b'G').chain(team.name.bytes()))])
                     .send_all()
@@ -372,7 +372,12 @@
 }
 
 pub fn get_room_teams(room: &HwRoom, to_client: ClientId, response: &mut Response) {
-    get_teams(room.teams.iter().map(|(_, t)| t), to_client, response);
+    let current_teams = match room.game_info {
+        Some(ref info) => &info.teams_at_start,
+        None => &room.teams,
+    };
+
+    get_teams(current_teams.iter().map(|(_, t)| t), to_client, response);
 }
 
 pub fn get_room_flags(
--- a/rust/hedgewars-server/src/handlers/inanteroom.rs	Tue Jan 07 15:17:22 2020 +0100
+++ b/rust/hedgewars-server/src/handlers/inanteroom.rs	Sat Jan 11 00:44:25 2020 +0300
@@ -92,7 +92,7 @@
             if client.protocol_number.is_some() {
                 response.error(PROTOCOL_PROVIDED);
                 LoginResult::Unchanged
-            } else if proto < 51 {
+            } else if proto < 48 {
                 response.add(Bye(PROTOCOL_TOO_OLD.to_string()).send_self());
                 LoginResult::Exit
             } else {
--- a/rust/hedgewars-server/src/handlers/inroom.rs	Tue Jan 07 15:17:22 2020 +0100
+++ b/rust/hedgewars-server/src/handlers/inroom.rs	Sat Jan 11 00:44:25 2020 +0300
@@ -259,15 +259,18 @@
                         response,
                     );
 
-                    if let Some(0) = room.teams_in_game() {
-                        if let Some(result) = room_control.end_game() {
-                            super::common::get_end_game_result(
-                                room_control.server(),
-                                room_id,
-                                result,
-                                response,
-                            );
+                    match room.game_info {
+                        Some(ref info) if info.teams_in_game == 0 => {
+                            if let Some(result) = room_control.end_game() {
+                                super::common::get_end_game_result(
+                                    room_control.server(),
+                                    room_id,
+                                    result,
+                                    response,
+                                );
+                            }
                         }
+                        _ => (),
                     }
                 }
                 Err(RemoveTeamError::NoTeam) => response.warn(NO_TEAM_TO_REMOVE),
@@ -458,7 +461,10 @@
                     );
                 }
 
-                if let Some(0) = room.teams_in_game() {
+                if let Some(GameInfo {
+                    teams_in_game: 0, ..
+                }) = room.game_info
+                {
                     if let Some(result) = room_control.end_game() {
                         super::common::get_end_game_result(
                             room_control.server(),