remove lobby room
authoralfadur <mail@none>
Thu, 07 Feb 2019 17:17:42 +0300
changeset 14699 25c564f77b7d
parent 14698 6a2e13e36b7f
child 14700 216d39de1a44
remove lobby room
rust/hedgewars-server/src/server/actions.rs
rust/hedgewars-server/src/server/core.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
--- a/rust/hedgewars-server/src/server/actions.rs	Thu Feb 07 17:02:24 2019 +0300
+++ b/rust/hedgewars-server/src/server/actions.rs	Thu Feb 07 17:17:42 2019 +0300
@@ -16,11 +16,17 @@
 #[cfg(feature = "official-server")]
 use super::database;
 
+pub enum DestinationRoom {
+    All,
+    Lobby,
+    Room(RoomId),
+}
+
 pub enum Destination {
     ToId(ClientId),
     ToSelf,
     ToAll {
-        room_id: Option<RoomId>,
+        room_id: DestinationRoom,
         protocol: Option<u16>,
         skip_self: bool,
     },
@@ -48,7 +54,7 @@
 
     pub fn send_all(message: HWServerMessage) -> PendingMessage {
         let destination = Destination::ToAll {
-            room_id: None,
+            room_id: DestinationRoom::All,
             protocol: None,
             skip_self: false,
         };
@@ -63,7 +69,17 @@
             ref mut room_id, ..
         } = self.destination
         {
-            *room_id = Some(clients_room_id)
+            *room_id = DestinationRoom::Room(clients_room_id)
+        }
+        self
+    }
+
+    pub fn in_lobby(mut self) -> PendingMessage {
+        if let Destination::ToAll {
+            ref mut room_id, ..
+        } = self.destination
+        {
+            *room_id = DestinationRoom::Lobby
         }
         self
     }
--- a/rust/hedgewars-server/src/server/core.rs	Thu Feb 07 17:02:24 2019 +0300
+++ b/rust/hedgewars-server/src/server/core.rs	Thu Feb 07 17:17:42 2019 +0300
@@ -57,7 +57,6 @@
 pub struct HWServer {
     pub clients: IndexSlab<HWClient>,
     pub rooms: Slab<HWRoom>,
-    pub lobby_id: RoomId,
     pub output: Vec<(Vec<ClientId>, HWServerMessage)>,
     pub removed_clients: Vec<ClientId>,
     pub io: Box<dyn HWServerIO>,
@@ -68,17 +67,14 @@
     pub fn new(clients_limit: usize, rooms_limit: usize, io: Box<dyn HWServerIO>) -> Self {
         let rooms = Slab::with_capacity(rooms_limit);
         let clients = IndexSlab::with_capacity(clients_limit);
-        let mut server = Self {
+        Self {
             clients,
             rooms,
-            lobby_id: 0,
             output: vec![],
             removed_clients: vec![],
             io,
             anteroom: HWAnteroom::new(clients_limit),
-        };
-        server.lobby_id = server.add_room().id;
-        server
+        }
     }
 
     pub fn add_client(&mut self, client_id: ClientId, data: HWAnteClient) {
@@ -119,10 +115,6 @@
         move_to_room(&mut self.clients[client_id], &mut self.rooms[room_id])
     }
 
-    pub fn lobby(&self) -> &HWRoom {
-        &self.rooms[self.lobby_id]
-    }
-
     pub fn has_room(&self, name: &str) -> bool {
         self.rooms.iter().any(|(_, r)| r.name == name)
     }
@@ -158,6 +150,10 @@
         self.clients.iter().filter(f).map(|(_, c)| c.id).collect()
     }
 
+    pub fn lobby_clients(&self) -> Vec<ClientId> {
+        self.select_clients(|(_, c)| c.room_id == None)
+    }
+
     pub fn room_clients(&self, room_id: RoomId) -> Vec<ClientId> {
         self.select_clients(|(_, c)| c.room_id == Some(room_id))
     }
--- a/rust/hedgewars-server/src/server/handlers.rs	Thu Feb 07 17:02:24 2019 +0300
+++ b/rust/hedgewars-server/src/server/handlers.rs	Thu Feb 07 17:17:42 2019 +0300
@@ -1,7 +1,11 @@
 use mio;
 use std::{io, io::Write};
 
-use super::{actions::Destination, core::HWServer, coretypes::ClientId};
+use super::{
+    actions::{Destination, DestinationRoom},
+    core::HWServer,
+    coretypes::ClientId,
+};
 use crate::{
     protocol::messages::{HWProtocolMessage, HWServerMessage, HWServerMessage::*},
     server::actions::PendingMessage,
@@ -81,7 +85,12 @@
         Destination::ToSelf => vec![client_id],
         Destination::ToId(id) => vec![id],
         Destination::ToAll {
-            room_id: Some(id), ..
+            room_id: DestinationRoom::Lobby,
+            ..
+        } => server.lobby_clients(),
+        Destination::ToAll {
+            room_id: DestinationRoom::Room(id),
+            ..
         } => server.room_clients(id),
         Destination::ToAll {
             protocol: Some(proto),
--- a/rust/hedgewars-server/src/server/handlers/common.rs	Thu Feb 07 17:02:24 2019 +0300
+++ b/rust/hedgewars-server/src/server/handlers/common.rs	Thu Feb 07 17:17:42 2019 +0300
@@ -51,7 +51,7 @@
             response.add(Notice("NickAlreadyInUse".to_string()).send_self());
         }
     } else {
-        server.clients[client_id].room_id = Some(server.lobby_id);
+        server.clients[client_id].room_id = None;
 
         let lobby_nicks: Vec<_> = server
             .clients
@@ -76,7 +76,6 @@
             server
                 .rooms
                 .iter()
-                .filter(|(id, _)| *id != server.lobby_id)
                 .flat_map(|(_, r)| r.info(r.master_id.map(|id| &server.clients[id])))
                 .collect(),
         );
@@ -95,7 +94,6 @@
     is_in_game: bool,
     response: &mut Response,
 ) {
-    let mut game_ended = false;
     if let Some(ref mut info) = room.game_info {
         for team_name in &team_names {
             info.left_teams.push(team_name.clone());
@@ -172,6 +170,8 @@
         }
     }
 
+    client.room_id = None;
+
     let update_msg = if room.players_number == 0 && !room.is_fixed() {
         RoomRemove(room.name.clone())
     } else {
@@ -186,33 +186,30 @@
     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];
+        let room = &mut server.rooms[room_id];
 
-            remove_client_from_room(client, room, response, msg);
-            client.room_id = Some(server.lobby_id);
+        remove_client_from_room(client, room, response, msg);
 
-            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.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();
-                    }
+                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);
+                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),
-                    );
-                }
+                response.add(
+                    ClientFlags("+h".to_string(), vec![new_master_nick])
+                        .send_all()
+                        .in_room(room.id),
+                );
             }
         }
     }
@@ -220,7 +217,6 @@
 
 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);
 
--- a/rust/hedgewars-server/src/server/handlers/inroom.rs	Thu Feb 07 17:02:24 2019 +0300
+++ b/rust/hedgewars-server/src/server/handlers/inroom.rs	Thu Feb 07 17:17:42 2019 +0300
@@ -111,7 +111,6 @@
     use crate::protocol::messages::HWProtocolMessage::*;
     match message {
         Part(msg) => {
-            let lobby_id = server.lobby_id;
             if let (client, Some(room)) = server.client_and_room(client_id) {
                 let msg = match msg {
                     Some(s) => format!("part: {}", s),
--- a/rust/hedgewars-server/src/server/handlers/lobby.rs	Thu Feb 07 17:02:24 2019 +0300
+++ b/rust/hedgewars-server/src/server/handlers/lobby.rs	Thu Feb 07 17:17:42 2019 +0300
@@ -50,7 +50,7 @@
                     msg,
                 }
                 .send_all()
-                .in_room(server.lobby_id)
+                .in_lobby()
                 .but_self(),
             );
         }