reapply some simplifications
authoralfadur <mail@none>
Sat, 11 Jan 2020 01:06:41 +0300
changeset 15563 d122b65bdf6f
parent 15562 479911540e17
child 15564 3b0b6ce76303
reapply some simplifications
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/inroom.rs
--- a/rust/hedgewars-server/src/core/room.rs	Sat Jan 11 00:44:25 2020 +0300
+++ b/rust/hedgewars-server/src/core/room.rs	Sat Jan 11 01:06:41 2020 +0300
@@ -24,13 +24,12 @@
 }
 
 pub struct GameInfo {
-    pub teams_in_game: u8,
-    pub teams_at_start: Vec<(ClientId, TeamInfo)>,
+    pub original_teams: Vec<(ClientId, TeamInfo)>,
     pub left_teams: Vec<String>,
     pub msg_log: Vec<String>,
     pub sync_msg: Option<String>,
     pub is_paused: bool,
-    config: RoomConfig,
+    original_config: RoomConfig,
 }
 
 impl GameInfo {
@@ -40,14 +39,13 @@
             msg_log: Vec::new(),
             sync_msg: None,
             is_paused: false,
-            teams_in_game: teams.len() as u8,
-            teams_at_start: teams,
-            config,
+            original_teams: teams,
+            original_config: config,
         }
     }
 
     pub fn client_teams(&self, client_id: ClientId) -> impl Iterator<Item = &TeamInfo> + Clone {
-        client_teams_impl(&self.teams_at_start, client_id)
+        client_teams_impl(&self.original_teams, client_id)
     }
 }
 
@@ -148,7 +146,6 @@
 
             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());
@@ -164,7 +161,7 @@
     pub fn set_hedgehogs_number(&mut self, n: u8) -> Vec<String> {
         let mut names = Vec::new();
         let teams = match self.game_info {
-            Some(ref mut info) => &mut info.teams_at_start,
+            Some(ref mut info) => &mut info.original_teams,
             None => &mut self.teams,
         };
 
@@ -178,6 +175,12 @@
         names
     }
 
+    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)
+    }
+
     pub fn find_team_and_owner_mut<F>(&mut self, f: F) -> Option<(ClientId, &mut TeamInfo)>
     where
         F: Fn(&TeamInfo) -> bool,
@@ -303,21 +306,21 @@
 
     pub fn active_config(&self) -> &RoomConfig {
         match self.game_info {
-            Some(ref info) => &info.config,
+            Some(ref info) => &info.original_config,
             None => &self.config,
         }
     }
 
     pub fn map_config(&self) -> Vec<String> {
         match self.game_info {
-            Some(ref info) => info.config.to_map_config(),
+            Some(ref info) => info.original_config.to_map_config(),
             None => self.config.to_map_config(),
         }
     }
 
     pub fn game_config(&self) -> Vec<GameCfg> {
         match self.game_info {
-            Some(ref info) => info.config.to_game_config(),
+            Some(ref info) => info.original_config.to_game_config(),
             None => self.config.to_game_config(),
         }
     }
--- a/rust/hedgewars-server/src/core/server.rs	Sat Jan 11 00:44:25 2020 +0300
+++ b/rust/hedgewars-server/src/core/server.rs	Sat Jan 11 01:06:41 2020 +0300
@@ -986,8 +986,6 @@
                 .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()));
@@ -1012,8 +1010,12 @@
         let room = self.room_mut();
         room.ready_players_number = room.master_id.is_some() as u8;
 
-        if let Some(info) = replace(&mut room.game_info, None) {
+        if let Some(mut info) = replace(&mut room.game_info, None) {
             let room_id = room.id;
+            for team_name in &info.left_teams {
+                room.remove_team(team_name);
+            }
+
             let joined_mid_game_clients = self
                 .server
                 .clients
@@ -1043,7 +1045,7 @@
 
             Some(EndGameResult {
                 joined_mid_game_clients,
-                left_teams: info.left_teams.clone(),
+                left_teams: replace(&mut info.left_teams, vec![]),
                 unreadied_nicks,
             })
         } else {
@@ -1114,13 +1116,6 @@
 
         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	Sat Jan 11 00:44:25 2020 +0300
+++ b/rust/hedgewars-server/src/handlers/common.rs	Sat Jan 11 01:06:41 2020 +0300
@@ -231,14 +231,6 @@
 ) {
     if was_in_game {
         for team_name in &removed_teams {
-            let msg = once(b'F').chain(team_name.bytes());
-            response.add(
-                ForwardEngineMessage(vec![to_engine_msg(msg)])
-                    .send_all()
-                    .in_room(room_id)
-                    .but_self(),
-            );
-
             let remove_msg = to_engine_msg(once(b'F').chain(team_name.bytes()));
 
             response.add(
@@ -248,10 +240,10 @@
                     .but_self(),
             );
         }
-    }
-
-    for team_name in removed_teams {
-        response.add(TeamRemove(team_name).send_all().in_room(room_id));
+    } else {
+        for team_name in removed_teams {
+            response.add(TeamRemove(team_name).send_all().in_room(room_id));
+        }
     }
 }
 
@@ -373,7 +365,7 @@
 
 pub fn get_room_teams(room: &HwRoom, to_client: ClientId, response: &mut Response) {
     let current_teams = match room.game_info {
-        Some(ref info) => &info.teams_at_start,
+        Some(ref info) => &info.original_teams,
         None => &room.teams,
     };
 
@@ -595,23 +587,23 @@
     get_room_update(None, room, room_master, response);
     response.add(RoundFinished.send_all().in_room(room_id));
 
+    response.extend(
+        result
+            .left_teams
+            .iter()
+            .map(|name| TeamRemove(name.clone()).send_all().in_room(room.id)),
+    );
+
     for client_id in result.joined_mid_game_clients {
         super::common::get_room_config(room, client_id, response);
-        response.extend(
-            result
-                .left_teams
-                .iter()
-                .map(|name| TeamRemove(name.clone()).send(client_id)),
-        );
     }
 
     if !result.unreadied_nicks.is_empty() {
-        let msg = if room.protocol_number < 38 {
-            LegacyReady(false, result.unreadied_nicks)
-        } else {
+        response.add(
             ClientFlags(remove_flags(&[Flags::Ready]), result.unreadied_nicks)
-        };
-        response.add(msg.send_all().in_room(room_id));
+                .send_all()
+                .in_room(room_id),
+        );
     }
 }
 
--- a/rust/hedgewars-server/src/handlers/inroom.rs	Sat Jan 11 00:44:25 2020 +0300
+++ b/rust/hedgewars-server/src/handlers/inroom.rs	Sat Jan 11 01:06:41 2020 +0300
@@ -259,18 +259,15 @@
                         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,
-                                );
-                            }
+                    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,
+                            );
                         }
-                        _ => (),
                     }
                 }
                 Err(RemoveTeamError::NoTeam) => response.warn(NO_TEAM_TO_REMOVE),
@@ -461,10 +458,7 @@
                     );
                 }
 
-                if let Some(GameInfo {
-                    teams_in_game: 0, ..
-                }) = room.game_info
-                {
+                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(),