simplify team handling
authoralfadur <mail@none>
Sat, 04 Jan 2020 01:39:13 +0300
changeset 15558 a798e6441a36
parent 15557 3f6a7a867040
child 15559 bbb98e22a303
simplify team handling
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 Dec 31 04:42:20 2019 +0300
+++ b/rust/hedgewars-server/src/core/room.rs	Sat Jan 04 01:39:13 2020 +0300
@@ -13,19 +13,7 @@
 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>,
@@ -40,15 +28,9 @@
             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)]
@@ -144,11 +126,8 @@
 
     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());
@@ -157,17 +136,15 @@
                 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 = match self.game_info {
-            Some(ref mut info) => &mut info.teams_at_start,
-            None => &mut self.teams,
-        };
-
+        let teams = &mut self.teams;
         if teams.len() as u8 * n <= MAX_HEDGEHOGS_IN_ROOM {
             for (_, team) in teams.iter_mut() {
                 team.hedgehogs_number = n;
@@ -178,6 +155,12 @@
         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,
@@ -197,8 +180,11 @@
             .find_map(|(_, t)| Some(t).filter(|t| f(&t)))
     }
 
-    pub fn client_teams(&self, client_id: ClientId) -> impl Iterator<Item = &TeamInfo> {
-        client_teams_impl(&self.teams, client_id)
+    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_team_indices(&self, client_id: ClientId) -> Vec<u8> {
--- a/rust/hedgewars-server/src/core/server.rs	Tue Dec 31 04:42:20 2019 +0300
+++ b/rust/hedgewars-server/src/core/server.rs	Sat Jan 04 01:39:13 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()));
@@ -1106,21 +1104,15 @@
     client.set_is_joined_mid_game(room.game_info.is_some());
     client.set_is_in_game(room.game_info.is_some());
 
-    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();
+    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 !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();
+    match room.game_info {
+        Some(ref mut info) if !team_names.is_empty() => {
+            info.left_teams.retain(|name| !team_names.contains(&name))
         }
+        _ => (),
     }
 }
--- a/rust/hedgewars-server/src/handlers/common.rs	Tue Dec 31 04:42:20 2019 +0300
+++ b/rust/hedgewars-server/src/handlers/common.rs	Sat Jan 04 01:39:13 2020 +0300
@@ -195,7 +195,7 @@
             .send_self(),
         );
 
-        for team in info.client_teams(client.id) {
+        for team in room.client_teams(client.id) {
             response.add(
                 ForwardEngineMessage(vec![to_engine_msg(once(b'G').chain(team.name.bytes()))])
                     .send_all()
@@ -372,12 +372,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,
-        None => &room.teams,
-    };
-
-    get_teams(current_teams.iter().map(|(_, t)| t), to_client, response);
+    get_teams(room.teams.iter().map(|(_, t)| t), to_client, response);
 }
 
 pub fn get_room_flags(
--- a/rust/hedgewars-server/src/handlers/inanteroom.rs	Tue Dec 31 04:42:20 2019 +0300
+++ b/rust/hedgewars-server/src/handlers/inanteroom.rs	Sat Jan 04 01:39:13 2020 +0300
@@ -92,7 +92,7 @@
             if client.protocol_number.is_some() {
                 response.error(PROTOCOL_PROVIDED);
                 LoginResult::Unchanged
-            } else if proto < 48 {
+            } else if proto < 51 {
                 response.add(Bye(PROTOCOL_TOO_OLD.to_string()).send_self());
                 LoginResult::Exit
             } else {
--- a/rust/hedgewars-server/src/handlers/inroom.rs	Tue Dec 31 04:42:20 2019 +0300
+++ b/rust/hedgewars-server/src/handlers/inroom.rs	Sat Jan 04 01:39:13 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(),