Fix spectators joining midgame
authoralfadur
Wed, 27 Jun 2018 23:51:00 +0300
changeset 13428 c8425fbcf1d9
parent 13427 6f6a866c86a2
child 13429 4c5ed27b1ff8
Fix spectators joining midgame
gameServer2/src/protocol/messages.rs
gameServer2/src/server/actions.rs
gameServer2/src/server/handlers/inroom.rs
gameServer2/src/server/room.rs
--- a/gameServer2/src/protocol/messages.rs	Wed Jun 27 23:26:29 2018 +0300
+++ b/gameServer2/src/protocol/messages.rs	Wed Jun 27 23:51:00 2018 +0300
@@ -93,7 +93,7 @@
     HedgehogsNumber(String, u8),
     ConfigEntry(String, Vec<String>),
     RunGame,
-    ForwardEngineMessage(String),
+    ForwardEngineMessage(Vec<String>),
     RoundFinished,
 
     ServerMessage(String),
@@ -263,7 +263,8 @@
             ConfigEntry(name, values) =>
                 construct_message(&["CFG", name], &values),
             RunGame => msg!["RUN_GAME"],
-            ForwardEngineMessage(em) => msg!["EM", em],
+            ForwardEngineMessage(em) =>
+                construct_message(&["EM"], &em),
             RoundFinished => msg!["ROUND_FINISHED"],
             ChatMsg(nick, msg) => msg!["CHAT", nick, msg],
             ServerMessage(msg) => msg!["SERVER_MESSAGE", msg],
--- a/gameServer2/src/server/actions.rs	Wed Jun 27 23:26:29 2018 +0300
+++ b/gameServer2/src/server/actions.rs	Wed Jun 27 23:51:00 2018 +0300
@@ -259,16 +259,18 @@
                         v.push(ClientFlags("+g".to_string(), vec![c.nick.clone()])
                             .send_all().in_room(r.id).action());
                         v.push(ForwardEngineMessage(
-                            to_engine_msg("e$spectate 1".bytes()) + &info.msg_log)
+                            vec![to_engine_msg("e$spectate 1".bytes())])
+                            .send_self().action());
+                        v.push(ForwardEngineMessage(info.msg_log.clone())
                             .send_self().action());
 
                         for name in team_names.iter() {
                             v.push(ForwardEngineMessage(
-                                to_engine_msg(once(b'G').chain(name.bytes())))
+                                vec![to_engine_msg(once(b'G').chain(name.bytes()))])
                                 .send_all().in_room(r.id).action());
                         }
                         if info.is_paused {
-                            v.push(ForwardEngineMessage(to_engine_msg(once(b'I')))
+                            v.push(ForwardEngineMessage(vec![to_engine_msg(once(b'I'))])
                                 .send_all().in_room(r.id).action())
                         }
                     }
@@ -437,7 +439,7 @@
             if let (c, Some(r)) = server.client_and_room(client_id) {
                 if let Some(ref mut info) = r.game_info {
                     let msg = once(b'F').chain(team_name.bytes());
-                    actions.push(ForwardEngineMessage(to_engine_msg(msg)).
+                    actions.push(ForwardEngineMessage(vec![to_engine_msg(msg)]).
                         send_all().in_room(r.id).but_self().action());
                     info.teams_in_game -= 1;
                     if info.teams_in_game == 0 {
@@ -445,10 +447,10 @@
                     }
                     let remove_msg = to_engine_msg(once(b'F').chain(team_name.bytes()));
                     match &info.last_msg {
-                        Some(m) => info.msg_log.push_str(&m),
-                        None => info.msg_log.push_str(&remove_msg)
+                        Some(m) => info.msg_log.push(m.clone()),
+                        None => info.msg_log.push(remove_msg.clone())
                     }
-                    actions.push(ForwardEngineMessage(remove_msg)
+                    actions.push(ForwardEngineMessage(vec![remove_msg])
                         .send_all().in_room(r.id).but_self().action());
                 }
             }
--- a/gameServer2/src/server/handlers/inroom.rs	Wed Jun 27 23:26:29 2018 +0300
+++ b/gameServer2/src/server/handlers/inroom.rs	Wed Jun 27 23:51:00 2018 +0300
@@ -248,12 +248,14 @@
 
                     let em_response = encode(&valid.flat_map(|msg| msg).cloned().collect::<Vec<_>>());
                     if !em_response.is_empty() {
-                        actions.push(ForwardEngineMessage(em_response)
+                        actions.push(ForwardEngineMessage(vec![em_response])
                             .send_all().in_room(r.id).but_self().action());
                     }
                     let em_log = encode(&non_empty.flat_map(|msg| msg).cloned().collect::<Vec<_>>());
                     if let Some(ref mut info) = r.game_info {
-                        info.msg_log.push_str(&em_log);
+                        if (!em_log.is_empty()) {
+                            info.msg_log.push(em_log);
+                        }
                         if last_msg.is_some() {
                             info.last_msg = last_msg;
                         }
--- a/gameServer2/src/server/room.rs	Wed Jun 27 23:26:29 2018 +0300
+++ b/gameServer2/src/server/room.rs	Wed Jun 27 23:51:00 2018 +0300
@@ -83,7 +83,7 @@
     pub teams_in_game: u8,
     pub teams_at_start: Vec<(ClientId, TeamInfo)>,
     pub left_teams: Vec<String>,
-    pub msg_log: String,
+    pub msg_log: Vec<String>,
     pub last_msg: Option<String>,
     pub is_paused: bool,
     config: RoomConfig
@@ -93,7 +93,7 @@
     fn new(teams: Vec<(ClientId, TeamInfo)>, config: RoomConfig) -> GameInfo {
         GameInfo {
             left_teams: Vec::new(),
-            msg_log: String::new(),
+            msg_log: Vec::new(),
             last_msg: None,
             is_paused: false,
             teams_in_game: teams.len() as u8,