Server action refactoring part C of N
authoralfadur <mail@none>
Wed, 06 Feb 2019 21:33:22 +0300
changeset 14694 aae29ba56aec
parent 14693 4569d8d50286
child 14695 f61ce544d436
Server action refactoring part C of N
rust/hedgewars-server/src/server/actions.rs
rust/hedgewars-server/src/server/handlers/common.rs
rust/hedgewars-server/src/server/handlers/inroom.rs
--- a/rust/hedgewars-server/src/server/actions.rs	Wed Feb 06 20:48:40 2019 +0300
+++ b/rust/hedgewars-server/src/server/actions.rs	Wed Feb 06 21:33:22 2019 +0300
@@ -103,80 +103,12 @@
 
 pub enum Action {
     ChangeMaster(RoomId, Option<ClientId>),
-    SendTeamRemovalMessage(String),
-    SendRoomData {
-        to: ClientId,
-        teams: bool,
-        config: bool,
-        flags: bool,
-    },
 }
 
 use self::Action::*;
 
 pub fn run_action(server: &mut HWServer, client_id: usize, action: Action) {
     match action {
-        SendRoomData {
-            to,
-            teams,
-            config,
-            flags,
-        } => {
-            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(to)
-                            .action(),
-                    )*/
-;
-                    for cfg in r.game_config() {
-                        //actions.push(cfg.to_server_msg().send(to).action());
-                    }
-                }
-                if teams {
-                    let current_teams = match r.game_info {
-                        Some(ref info) => &info.teams_at_start,
-                        None => &r.teams,
-                    };
-                    for (owner_id, team) in current_teams.iter() {
-                        /*actions.push(
-                            TeamAdd(HWRoom::team_info(&server.clients[*owner_id], &team))
-                                .send(to)
-                                .action(),
-                        );
-                        actions.push(TeamColor(team.name.clone(), team.color).send(to).action());
-                        actions.push(
-                            HedgehogsNumber(team.name.clone(), team.hedgehogs_number)
-                                .send(to)
-                                .action(),
-                        );*/
-                    }
-                }
-                if flags {
-                    if let Some(id) = r.master_id {
-                        /*
-                                                actions.push(
-                                                    ClientFlags("+h".to_string(), vec![server.clients[id].nick.clone()])
-                                                        .send(to)
-                                                        .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(to).action())*/
-;
-                    }
-                }
-            }
-        }
         ChangeMaster(room_id, new_id) => {
             let room_client_ids = server.room_clients(room_id);
             let new_id = if server
@@ -227,38 +159,5 @@
                 server.clients[id].set_is_master(true)
             }
         }
-        SendTeamRemovalMessage(team_name) => {
-            if let Some(r) = server.room(client_id) {
-                if let Some(ref mut info) = r.game_info {
-                    let msg = once(b'F').chain(team_name.bytes());
-                    /*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 {
-                        //actions.push(FinishRoomGame(r.id));
-                    }
-                    let remove_msg = to_engine_msg(once(b'F').chain(team_name.bytes()));
-                    if let Some(m) = &info.sync_msg {
-                        info.msg_log.push(m.clone());
-                    }
-                    if info.sync_msg.is_some() {
-                        info.sync_msg = None
-                    }
-                    info.msg_log.push(remove_msg.clone());
-                    /*actions.push(
-                        ForwardEngineMessage(vec![remove_msg])
-                            .send_all()
-                            .in_room(r.id)
-                            .but_self()
-                            .action(),
-                    );*/
-                }
-            }
-        }
     }
 }
--- a/rust/hedgewars-server/src/server/handlers/common.rs	Wed Feb 06 20:48:40 2019 +0300
+++ b/rust/hedgewars-server/src/server/handlers/common.rs	Wed Feb 06 21:33:22 2019 +0300
@@ -200,6 +200,55 @@
     response.add(update_msg.send_all().with_protocol(room.protocol_number));
 }
 
+pub fn get_room_config(room: &HWRoom, to_client: ClientId, response: &mut Response) {
+    response.add(ConfigEntry("FULLMAPCONFIG".to_string(), room.map_config()).send(to_client));
+    for cfg in room.game_config() {
+        response.add(cfg.to_server_msg().send(to_client));
+    }
+}
+
+pub fn get_room_teams(
+    server: &HWServer,
+    room_id: RoomId,
+    to_client: ClientId,
+    response: &mut Response,
+) {
+    let room = &server.rooms[room_id];
+    let current_teams = match room.game_info {
+        Some(ref info) => &info.teams_at_start,
+        None => &room.teams,
+    };
+
+    for (owner_id, team) in current_teams.iter() {
+        response.add(TeamAdd(HWRoom::team_info(&server.clients[*owner_id], &team)).send(to_client));
+        response.add(TeamColor(team.name.clone(), team.color).send(to_client));
+        response.add(HedgehogsNumber(team.name.clone(), team.hedgehogs_number).send(to_client));
+    }
+}
+
+pub fn get_room_flags(
+    server: &HWServer,
+    room_id: RoomId,
+    to_client: ClientId,
+    response: &mut Response,
+) {
+    let room = &server.rooms[room_id];
+    if let Some(id) = room.master_id {
+        response.add(
+            ClientFlags("+h".to_string(), vec![server.clients[id].nick.clone()]).send(to_client),
+        );
+    }
+    let nicks: Vec<_> = server
+        .clients
+        .iter()
+        .filter(|(_, c)| c.room_id == Some(room_id) && c.is_ready())
+        .map(|(_, c)| c.nick.clone())
+        .collect();
+    if !nicks.is_empty() {
+        response.add(ClientFlags("+r".to_string(), nicks).send(to_client));
+    }
+}
+
 pub fn apply_voting_result(
     server: &mut HWServer,
     room_id: RoomId,
@@ -239,14 +288,9 @@
                 };
                 get_room_update(None, room, room_master, response);
 
-                for (_, c) in server.clients.iter() {
-                    if c.room_id == Some(room_id) {
-                        /*SendRoomData {
-                            to: c.id,
-                            teams: false,
-                            config: true,
-                            flags: false,
-                        }*/
+                for (_, client) in server.clients.iter() {
+                    if client.room_id == Some(room_id) {
+                        super::common::get_room_config(&server.rooms[room_id], client.id, response);
                     }
                 }
             }
@@ -387,8 +431,7 @@
     if let Some(info) = replace(&mut room.game_info, None) {
         for (_, client) in server.clients.iter() {
             if client.room_id == Some(room_id) && client.is_joined_mid_game() {
-                //SendRoomData { to: client.id, teams: false, config: true, flags: false}
-
+                super::common::get_room_config(room, client.id, response);
                 response.extend(
                     info.left_teams
                         .iter()
--- a/rust/hedgewars-server/src/server/handlers/inroom.rs	Wed Feb 06 20:48:40 2019 +0300
+++ b/rust/hedgewars-server/src/server/handlers/inroom.rs	Wed Feb 06 21:33:22 2019 +0300
@@ -1,6 +1,7 @@
 use mio;
 
 use super::common::rnd_reply;
+use crate::utils::to_engine_msg;
 use crate::{
     protocol::messages::{server_chat, HWProtocolMessage, HWServerMessage::*},
     server::{
@@ -14,6 +15,7 @@
 };
 use base64::{decode, encode};
 use log::*;
+use std::iter::once;
 use std::mem::swap;
 
 #[derive(Clone)]
@@ -551,21 +553,57 @@
             }
         }
         RoundFinished => {
-            if let (c, Some(r)) = server.client_and_room(client_id) {
-                if c.is_in_game() {
-                    c.set_is_in_game(false);
-                    response.add(
-                        ClientFlags("-g".to_string(), vec![c.nick.clone()])
-                            .send_all()
-                            .in_room(r.id),
-                    );
-                    if r.game_info.is_some() {
-                        for team in r.client_teams(c.id) {
-                            //SendTeamRemovalMessage(team.name.clone());
+            let mut game_ended = false;
+            let client = &mut server.clients[client_id];
+            if client.is_in_game() {
+                let room = &mut server.rooms[room_id];
+
+                client.set_is_in_game(false);
+                response.add(
+                    ClientFlags("-g".to_string(), vec![client.nick.clone()])
+                        .send_all()
+                        .in_room(room.id),
+                );
+                let team_names: Vec<_> = room
+                    .client_teams(client_id)
+                    .map(|t| t.name.clone())
+                    .collect();
+
+                if let Some(ref mut info) = room.game_info {
+                    info.teams_in_game -= team_names.len() as u8;
+                    if info.teams_in_game == 0 {
+                        game_ended = true;
+                    }
+
+                    for team_name in team_names {
+                        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()));
+                        if let Some(m) = &info.sync_msg {
+                            info.msg_log.push(m.clone());
                         }
+                        if info.sync_msg.is_some() {
+                            info.sync_msg = None
+                        }
+                        info.msg_log.push(remove_msg.clone());
+                        response.add(
+                            ForwardEngineMessage(vec![remove_msg])
+                                .send_all()
+                                .in_room(room_id)
+                                .but_self(),
+                        );
                     }
                 }
             }
+            if game_ended {
+                super::common::end_game(server, room_id, response)
+            }
         }
         Rnd(v) => {
             let result = rnd_reply(&v);