Send teams&flags on entering a room
authoralfadur
Wed, 27 Jun 2018 04:54:41 +0300 (2018-06-27)
changeset 13429 d8354cb98b98
parent 13428 87a6cad20c90
child 13430 50f852e7ad82
Send teams&flags on entering a room
gameServer2/src/server/actions.rs
gameServer2/src/server/handlers/inroom.rs
--- a/gameServer2/src/server/actions.rs	Wed Jun 27 02:34:46 2018 +0300
+++ b/gameServer2/src/server/actions.rs	Wed Jun 27 04:54:41 2018 +0300
@@ -95,6 +95,7 @@
     StartRoomGame(RoomId),
     SendTeamRemovalMessage(String),
     FinishRoomGame(RoomId),
+    SendRoomData{teams: bool, config: bool, flags: bool},
     Warn(String),
     ProtocolError(String)
 }
@@ -221,22 +222,51 @@
                     flags_msg.send_all().action(),
                     SendRoomUpdate(None)];
                 if !c.is_master {
-                    v.push(ConfigEntry("FULLMAPCONFIG".to_string(), r.map_config())
-                        .send_self().action());
-                    for cfg in r.game_config().into_iter() {
-                        v.push(cfg.into_server_msg().send_self().action());
-                    }
+                    v.push(SendRoomData{ teams: true, config: true, flags: true});
                 }
                 v
             };
             server.react(client_id, actions);
-        },
+        }
+        SendRoomData {teams, config, flags} => {
+            let mut actions = Vec::new();
+            let room_id = server.clients[client_id].room_id;
+            if let Some(r) = room_id.and_then(|id| server.rooms.get(id)) {
+                if config {
+                    actions.push(ConfigEntry("FULLMAPCONFIG".to_string(), r.map_config())
+                        .send_self().action());
+                    for cfg in r.game_config().into_iter() {
+                        actions.push(cfg.into_server_msg().send_self().action());
+                    }
+                }
+                if teams {
+                    for (owner_id, team) in r.teams.iter() {
+                        actions.push(TeamAdd(HWRoom::team_info(&server.clients[*owner_id], &team))
+                            .send_self().action());
+                    }
+                }
+                if flags {
+                    if let Some(id) = r.master_id {
+                        actions.push(ClientFlags("+h".to_string(), vec![server.clients[id].nick.clone()])
+                            .send_self().action());
+                    }
+                    let nicks: Vec<_> = server.clients.iter()
+                        .filter(|(_, c)| c.room_id == Some(r.id) && c.is_ready)
+                        .map(|(_, c)| c.nick.clone()).collect();
+                    if !nicks.is_empty() {
+                        actions.push(ClientFlags("+r".to_string(), nicks)
+                            .send_self().action());
+                    }
+                }
+            }
+            server.react(client_id, actions);
+        }
         MoveToLobby(msg) => {
             let mut actions = Vec::new();
             let lobby_id = server.lobby_id;
             if let (c, Some(r)) = server.client_and_room(client_id) {
                 r.players_number -= 1;
-                if c.is_ready {
+                if c.is_ready && r.ready_players_number > 0 {
                     r.ready_players_number -= 1;
                 }
                 if r.players_number > 0 && c.is_master {
--- a/gameServer2/src/server/handlers/inroom.rs	Wed Jun 27 02:34:46 2018 +0300
+++ b/gameServer2/src/server/handlers/inroom.rs	Wed Jun 27 04:54:41 2018 +0300
@@ -42,14 +42,13 @@
 const NON_TIMED_MESSAGES: &[u8] = b"M#hb";
 
 fn is_msg_valid(msg: &[u8], team_indices: &[u8]) -> bool {
-    if let [size, typ, body..] = msg {
-        VALID_MESSAGES.contains(typ) &&
-            match body {
+    match msg {
+        [size, typ, body..] => VALID_MESSAGES.contains(typ)
+            && match body {
                 [1...8, team, ..] if *typ == b'h' => team_indices.contains(team),
                 _ => *typ != b'h'
-            }
-    } else {
-        false
+            },
+        _ => false
     }
 }