Server action refactoring part N of N
authoralfadur <mail@none>
Wed, 06 Feb 2019 22:29:02 +0300
changeset 14695 f61ce544d436
parent 14694 aae29ba56aec
child 14696 2071da901c63
Server action refactoring part N of N
rust/hedgewars-server/src/server/actions.rs
rust/hedgewars-server/src/server/handlers.rs
rust/hedgewars-server/src/server/handlers/common.rs
rust/hedgewars-server/src/server/handlers/inroom.rs
rust/hedgewars-server/src/server/handlers/lobby.rs
rust/hedgewars-server/src/server/handlers/loggingin.rs
--- a/rust/hedgewars-server/src/server/actions.rs	Wed Feb 06 21:33:22 2019 +0300
+++ b/rust/hedgewars-server/src/server/actions.rs	Wed Feb 06 22:29:02 2019 +0300
@@ -100,64 +100,3 @@
         PendingMessage::send_all(self)
     }
 }
-
-pub enum Action {
-    ChangeMaster(RoomId, Option<ClientId>),
-}
-
-use self::Action::*;
-
-pub fn run_action(server: &mut HWServer, client_id: usize, action: Action) {
-    match action {
-        ChangeMaster(room_id, new_id) => {
-            let room_client_ids = server.room_clients(room_id);
-            let new_id = if server
-                .room(client_id)
-                .map(|r| r.is_fixed())
-                .unwrap_or(false)
-            {
-                new_id
-            } else {
-                new_id.or_else(|| room_client_ids.iter().find(|id| **id != client_id).cloned())
-            };
-            let new_nick = new_id.map(|id| server.clients[id].nick.clone());
-
-            if let (c, Some(r)) = server.client_and_room(client_id) {
-                match r.master_id {
-                    Some(id) if id == c.id => {
-                        c.set_is_master(false);
-                        r.master_id = None;
-                        /*actions.push(
-                            ClientFlags("-h".to_string(), vec![c.nick.clone()])
-                                .send_all()
-                                .in_room(r.id)
-                                .action(),
-                        );*/
-                    }
-                    Some(_) => unreachable!(),
-                    None => {}
-                }
-                r.master_id = new_id;
-                if !r.is_fixed() && c.protocol_number < 42 {
-                    r.name
-                        .replace_range(.., new_nick.as_ref().map_or("[]", String::as_str));
-                }
-                r.set_join_restriction(false);
-                r.set_team_add_restriction(false);
-                let is_fixed = r.is_fixed();
-                r.set_unregistered_players_restriction(is_fixed);
-                if let Some(nick) = new_nick {
-                    /*actions.push(
-                        ClientFlags("+h".to_string(), vec![nick])
-                            .send_all()
-                            .in_room(r.id)
-                            .action(),
-                    );*/
-                }
-            }
-            if let Some(id) = new_id {
-                server.clients[id].set_is_master(true)
-            }
-        }
-    }
-}
--- a/rust/hedgewars-server/src/server/handlers.rs	Wed Feb 06 21:33:22 2019 +0300
+++ b/rust/hedgewars-server/src/server/handlers.rs	Wed Feb 06 22:29:02 2019 +0300
@@ -1,11 +1,7 @@
 use mio;
 use std::{io, io::Write};
 
-use super::{
-    actions::{Action, Action::*, Destination},
-    core::HWServer,
-    coretypes::ClientId,
-};
+use super::{actions::Destination, core::HWServer, coretypes::ClientId};
 use crate::{
     protocol::messages::{HWProtocolMessage, HWServerMessage, HWServerMessage::*},
     server::actions::PendingMessage,
--- a/rust/hedgewars-server/src/server/handlers/common.rs	Wed Feb 06 21:33:22 2019 +0300
+++ b/rust/hedgewars-server/src/server/handlers/common.rs	Wed Feb 06 22:29:02 2019 +0300
@@ -5,7 +5,6 @@
         HWServerMessage::{self, *},
     },
     server::{
-        actions::Action,
         client::HWClient,
         core::HWServer,
         coretypes::{ClientId, GameCfg, RoomId, Vote, VoteType},
@@ -137,7 +136,12 @@
     }
 }
 
-pub fn exit_room(client: &HWClient, room: &mut HWRoom, response: &mut Response, msg: &str) {
+fn remove_client_from_room(
+    client: &mut HWClient,
+    room: &mut HWRoom,
+    response: &mut Response,
+    msg: &str,
+) {
     if room.players_number > 1 || room.is_fixed() {
         room.players_number -= 1;
         if client.is_ready() && room.ready_players_number > 0 {
@@ -159,7 +163,15 @@
             );
         }
 
-        //ChangeMaster(room.id, None));
+        if client.is_master() && !room.is_fixed() {
+            client.set_is_master(false);
+            response.add(
+                ClientFlags("-h".to_string(), vec![client.nick.clone()])
+                    .send_all()
+                    .in_room(room.id),
+            );
+            room.master_id = None;
+        }
     }
 
     let update_msg = if room.players_number == 0 && !room.is_fixed() {
@@ -172,17 +184,49 @@
     response.add(ClientFlags("-i".to_string(), vec![client.nick.clone()]).send_all());
 }
 
+pub fn exit_room(server: &mut HWServer, client_id: ClientId, response: &mut Response, msg: &str) {
+    let client = &mut server.clients[client_id];
+
+    if let Some(room_id) = client.room_id {
+        if room_id != server.lobby_id {
+            let room = &mut server.rooms[room_id];
+
+            remove_client_from_room(client, room, response, msg);
+            client.room_id = Some(server.lobby_id);
+
+            if !room.is_fixed() && room.master_id == None {
+                if let Some(new_master_id) = server.room_clients(room_id).first().cloned() {
+                    let new_master_nick = server.clients[new_master_id].nick.clone();
+                    let room = &mut server.rooms[room_id];
+                    room.master_id = Some(new_master_id);
+                    server.clients[new_master_id].set_is_master(true);
+
+                    if room.protocol_number < 42 {
+                        room.name = new_master_nick.clone();
+                    }
+
+                    room.set_join_restriction(false);
+                    room.set_team_add_restriction(false);
+                    room.set_unregistered_players_restriction(true);
+
+                    response.add(
+                        ClientFlags("+h".to_string(), vec![new_master_nick])
+                            .send_all()
+                            .in_room(room.id),
+                    );
+                }
+            }
+        }
+    }
+}
+
 pub fn remove_client(server: &mut HWServer, response: &mut Response, msg: String) {
     let client_id = response.client_id();
     let lobby_id = server.lobby_id;
     let client = &mut server.clients[client_id];
     let (nick, room_id) = (client.nick.clone(), client.room_id);
 
-    if let Some(room_id) = room_id {
-        let room = &mut server.rooms[room_id];
-        exit_room(client, room, response, &msg);
-        client.room_id = Some(lobby_id);
-    }
+    exit_room(server, client_id, response, &msg);
 
     server.remove_client(client_id);
 
@@ -262,13 +306,8 @@
             if let Some(client) = server.find_client(&nick) {
                 if client.room_id == Some(room_id) {
                     let id = client.id;
-                    response.add(Kicked.send_self());
-                    exit_room(
-                        &mut server.clients[client_id],
-                        &mut server.rooms[room_id],
-                        response,
-                        "kicked",
-                    );
+                    response.add(Kicked.send(id));
+                    exit_room(server, id, response, "kicked");
                 }
             }
         }
--- a/rust/hedgewars-server/src/server/handlers/inroom.rs	Wed Feb 06 21:33:22 2019 +0300
+++ b/rust/hedgewars-server/src/server/handlers/inroom.rs	Wed Feb 06 22:29:02 2019 +0300
@@ -5,7 +5,6 @@
 use crate::{
     protocol::messages::{server_chat, HWProtocolMessage, HWServerMessage::*},
     server::{
-        actions::{Action, Action::*},
         core::HWServer,
         coretypes,
         coretypes::{ClientId, GameCfg, RoomId, VoteType, Voting, MAX_HEDGEHOGS_PER_TEAM},
@@ -118,8 +117,7 @@
                     Some(s) => format!("part: {}", s),
                     None => "part".to_string(),
                 };
-                super::common::exit_room(client, room, response, &msg);
-                client.room_id = Some(lobby_id);
+                super::common::exit_room(server, client_id, response, &msg);
             }
         }
         Chat(msg) => {
@@ -136,7 +134,10 @@
         Fix => {
             if let (client, Some(room)) = server.client_and_room(client_id) {
                 if client.is_admin() {
-                    room.set_is_fixed(true)
+                    room.set_is_fixed(true);
+                    room.set_join_restriction(false);
+                    room.set_team_add_restriction(false);
+                    room.set_unregistered_players_restriction(true);
                 }
             }
         }
--- a/rust/hedgewars-server/src/server/handlers/lobby.rs	Wed Feb 06 21:33:22 2019 +0300
+++ b/rust/hedgewars-server/src/server/handlers/lobby.rs	Wed Feb 06 22:29:02 2019 +0300
@@ -3,11 +3,7 @@
 use super::common::rnd_reply;
 use crate::{
     protocol::messages::{HWProtocolMessage, HWServerMessage::*},
-    server::{
-        actions::{Action, Action::*},
-        core::HWServer,
-        coretypes::ClientId,
-    },
+    server::{core::HWServer, coretypes::ClientId},
     utils::is_name_illegal,
 };
 use log::*;
--- a/rust/hedgewars-server/src/server/handlers/loggingin.rs	Wed Feb 06 21:33:22 2019 +0300
+++ b/rust/hedgewars-server/src/server/handlers/loggingin.rs	Wed Feb 06 22:29:02 2019 +0300
@@ -2,12 +2,7 @@
 
 use crate::{
     protocol::messages::{HWProtocolMessage, HWServerMessage::*},
-    server::{
-        actions::{Action, Action::*},
-        client::HWClient,
-        core::HWServer,
-        coretypes::ClientId,
-    },
+    server::{client::HWClient, core::HWServer, coretypes::ClientId},
     utils::is_name_illegal,
 };
 use log::*;