fix some warnings
authoralfadur
Thu, 07 Feb 2019 23:34:24 +0300
changeset 14702 f64e21f164a5
parent 14701 8a45c90f4580
child 14703 4a4ff2354b58
fix some warnings
rust/hedgewars-server/src/protocol/messages.rs
rust/hedgewars-server/src/server/core.rs
rust/hedgewars-server/src/server/handlers/checker.rs
rust/hedgewars-server/src/server/handlers/common.rs
rust/hedgewars-server/src/server/handlers/inroom.rs
rust/hedgewars-server/src/server/network.rs
--- a/rust/hedgewars-server/src/protocol/messages.rs	Thu Feb 07 22:26:56 2019 +0300
+++ b/rust/hedgewars-server/src/protocol/messages.rs	Thu Feb 07 23:34:24 2019 +0300
@@ -3,22 +3,21 @@
 
 #[derive(PartialEq, Eq, Clone, Debug)]
 pub enum HWProtocolMessage {
-    // core
+    // common messages
     Ping,
     Pong,
     Quit(Option<String>),
-    //Cmd(String, Vec<String>),
     Global(String),
     Watch(String),
     ToggleServerRegisteredOnly,
     SuperPower,
     Info(String),
-    // not entered state
+    // anteroom messages
     Nick(String),
     Proto(u16),
     Password(String, String),
     Checker(u16, String, String),
-    // lobby
+    // lobby messages
     List,
     Chat(String),
     CreateRoom(String, Option<String>),
@@ -35,7 +34,7 @@
     GetServerVar,
     RestartServer,
     Stats,
-    // in room
+    // room messages
     Part(Option<String>),
     Cfg(GameCfg),
     AddTeam(Box<TeamInfo>),
--- a/rust/hedgewars-server/src/server/core.rs	Thu Feb 07 22:26:56 2019 +0300
+++ b/rust/hedgewars-server/src/server/core.rs	Thu Feb 07 23:34:24 2019 +0300
@@ -83,10 +83,6 @@
         self.clients.remove(client_id);
     }
 
-    pub fn add_room(&mut self) -> &mut HWRoom {
-        allocate_room(&mut self.rooms)
-    }
-
     #[inline]
     pub fn create_room(
         &mut self,
@@ -108,7 +104,7 @@
     }
 
     pub fn has_room(&self, name: &str) -> bool {
-        self.rooms.iter().any(|(_, r)| r.name == name)
+        self.find_room(name).is_some()
     }
 
     pub fn find_room(&self, name: &str) -> Option<&HWRoom> {
@@ -158,24 +154,10 @@
         let room_id = self.clients[self_id].room_id;
         self.select_clients(|(id, c)| *id != self_id && c.room_id == room_id)
     }
-
-    pub fn client_and_room(&mut self, client_id: ClientId) -> (&mut HWClient, Option<&mut HWRoom>) {
-        let c = &mut self.clients[client_id];
-        if let Some(room_id) = c.room_id {
-            (c, Some(&mut self.rooms[room_id]))
-        } else {
-            (c, None)
-        }
-    }
-
-    pub fn room(&mut self, client_id: ClientId) -> Option<&mut HWRoom> {
-        self.client_and_room(client_id).1
-    }
 }
 
 fn allocate_room(rooms: &mut Slab<HWRoom>) -> &mut HWRoom {
     let entry = rooms.vacant_entry();
-    let key = entry.key();
     let room = HWRoom::new(entry.key());
     entry.insert(room)
 }
--- a/rust/hedgewars-server/src/server/handlers/checker.rs	Thu Feb 07 22:26:56 2019 +0300
+++ b/rust/hedgewars-server/src/server/handlers/checker.rs	Thu Feb 07 23:34:24 2019 +0300
@@ -6,7 +6,7 @@
     server::{core::HWServer, coretypes::ClientId},
 };
 
-pub fn handle(server: &mut HWServer, client_id: ClientId, message: HWProtocolMessage) {
+pub fn handle(_server: &mut HWServer, _client_id: ClientId, message: HWProtocolMessage) {
     match message {
         _ => warn!("Unknown command"),
     }
--- a/rust/hedgewars-server/src/server/handlers/common.rs	Thu Feb 07 22:26:56 2019 +0300
+++ b/rust/hedgewars-server/src/server/handlers/common.rs	Thu Feb 07 23:34:24 2019 +0300
@@ -15,20 +15,21 @@
 
 use super::Response;
 
-use rand::{self, thread_rng, Rng};
+use rand::{self, seq::SliceRandom, thread_rng, Rng};
 use std::{iter::once, mem::replace};
 
 pub fn rnd_reply(options: &[String]) -> HWServerMessage {
     let mut rng = thread_rng();
+
     let reply = if options.is_empty() {
-        (*rng.choose(&["heads", "tails"]).unwrap()).to_owned()
+        (*&["heads", "tails"].choose(&mut rng).unwrap()).to_string()
     } else {
-        rng.choose(&options).unwrap().clone()
+        options.choose(&mut rng).unwrap().clone()
     };
 
     ChatMsg {
-        nick: "[random]".to_owned(),
-        msg: reply.clone(),
+        nick: "[random]".to_string(),
+        msg: reply,
     }
 }
 
@@ -218,7 +219,7 @@
 pub fn remove_client(server: &mut HWServer, response: &mut Response, msg: String) {
     let client_id = response.client_id();
     let client = &mut server.clients[client_id];
-    let (nick, room_id) = (client.nick.clone(), client.room_id);
+    let nick = client.nick.clone();
 
     exit_room(server, client_id, response, &msg);
 
@@ -294,8 +295,6 @@
     response: &mut Response,
     kind: VoteType,
 ) {
-    let client_id = response.client_id;
-
     match kind {
         VoteType::Kick(nick) => {
             if let Some(client) = server.find_client(&nick) {
--- a/rust/hedgewars-server/src/server/handlers/inroom.rs	Thu Feb 07 22:26:56 2019 +0300
+++ b/rust/hedgewars-server/src/server/handlers/inroom.rs	Thu Feb 07 23:34:24 2019 +0300
@@ -108,19 +108,19 @@
     room_id: RoomId,
     message: HWProtocolMessage,
 ) {
+    let client = &mut server.clients[client_id];
+    let room = &mut server.rooms[room_id];
+
     use crate::protocol::messages::HWProtocolMessage::*;
     match message {
         Part(msg) => {
-            if let (client, Some(room)) = server.client_and_room(client_id) {
-                let msg = match msg {
-                    Some(s) => format!("part: {}", s),
-                    None => "part".to_string(),
-                };
-                super::common::exit_room(server, client_id, response, &msg);
-            }
+            let msg = match msg {
+                Some(s) => format!("part: {}", s),
+                None => "part".to_string(),
+            };
+            super::common::exit_room(server, client_id, response, &msg);
         }
         Chat(msg) => {
-            let client = &mut server.clients[client_id];
             response.add(
                 ChatMsg {
                     nick: client.nick.clone(),
@@ -131,27 +131,21 @@
             );
         }
         Fix => {
-            if let (client, Some(room)) = server.client_and_room(client_id) {
-                if client.is_admin() {
-                    room.set_is_fixed(true);
-                    room.set_join_restriction(false);
-                    room.set_team_add_restriction(false);
-                    room.set_unregistered_players_restriction(true);
-                }
+            if client.is_admin() {
+                room.set_is_fixed(true);
+                room.set_join_restriction(false);
+                room.set_team_add_restriction(false);
+                room.set_unregistered_players_restriction(true);
             }
         }
         Unfix => {
-            if let (client, Some(room)) = server.client_and_room(client_id) {
-                if client.is_admin() {
-                    room.set_is_fixed(false)
-                }
+            if client.is_admin() {
+                room.set_is_fixed(false);
             }
         }
         Greeting(text) => {
-            if let (clienr, Some(room)) = server.client_and_room(client_id) {
-                if clienr.is_admin() || clienr.is_master() && !room.is_fixed() {
-                    room.greeting = text
-                }
+            if client.is_admin() || client.is_master() && !room.is_fixed() {
+                room.greeting = text;
             }
         }
         RoomName(new_name) => {
@@ -174,31 +168,27 @@
             }
         }
         ToggleReady => {
-            if let (client, Some(room)) = server.client_and_room(client_id) {
-                let flags = if client.is_ready() {
-                    room.ready_players_number -= 1;
-                    "-r"
-                } else {
-                    room.ready_players_number += 1;
-                    "+r"
-                };
+            let flags = if client.is_ready() {
+                room.ready_players_number -= 1;
+                "-r"
+            } else {
+                room.ready_players_number += 1;
+                "+r"
+            };
 
-                let msg = if client.protocol_number < 38 {
-                    LegacyReady(client.is_ready(), vec![client.nick.clone()])
-                } else {
-                    ClientFlags(flags.to_string(), vec![client.nick.clone()])
-                };
-                response.add(msg.send_all().in_room(room.id));
-                client.set_is_ready(!client.is_ready());
+            let msg = if client.protocol_number < 38 {
+                LegacyReady(client.is_ready(), vec![client.nick.clone()])
+            } else {
+                ClientFlags(flags.to_string(), vec![client.nick.clone()])
+            };
+            response.add(msg.send_all().in_room(room.id));
+            client.set_is_ready(!client.is_ready());
 
-                if room.is_fixed() && room.ready_players_number == room.players_number {
-                    super::common::start_game(server, room_id, response);
-                }
+            if room.is_fixed() && room.ready_players_number == room.players_number {
+                super::common::start_game(server, room_id, response);
             }
         }
         AddTeam(info) => {
-            let client = &mut server.clients[client_id];
-            let room = &mut server.rooms[room_id];
             if room.teams.len() >= room.team_limit as usize {
                 response.add(Warning("Too many teams!".to_string()).send_self());
             } else if room.addable_hedgehogs() == 0 {
@@ -247,104 +237,93 @@
                 super::common::get_room_update(None, room, room_master, response);
             }
         }
-        RemoveTeam(name) => {
-            let client = &mut server.clients[client_id];
-            let room = &mut server.rooms[room_id];
-            match room.find_team_owner(&name) {
-                None => response.add(
-                    Warning("Error: The team you tried to remove does not exist.".to_string())
-                        .send_self(),
-                ),
-                Some((id, _)) if id != client_id => response
-                    .add(Warning("You can't remove a team you don't own.".to_string()).send_self()),
-                Some((_, name)) => {
-                    client.teams_in_game -= 1;
-                    client.clan = room.find_team_color(client.id);
-                    super::common::remove_teams(
-                        room,
-                        vec![name.to_string()],
-                        client.is_in_game(),
-                        response,
-                    );
+        RemoveTeam(name) => match room.find_team_owner(&name) {
+            None => response.add(
+                Warning("Error: The team you tried to remove does not exist.".to_string())
+                    .send_self(),
+            ),
+            Some((id, _)) if id != client_id => response
+                .add(Warning("You can't remove a team you don't own.".to_string()).send_self()),
+            Some((_, name)) => {
+                client.teams_in_game -= 1;
+                client.clan = room.find_team_color(client.id);
+                super::common::remove_teams(
+                    room,
+                    vec![name.to_string()],
+                    client.is_in_game(),
+                    response,
+                );
 
-                    match room.game_info {
-                        Some(ref info) if info.teams_in_game == 0 => {
-                            super::common::end_game(server, room_id, response)
-                        }
-                        _ => (),
+                match room.game_info {
+                    Some(ref info) if info.teams_in_game == 0 => {
+                        super::common::end_game(server, room_id, response)
                     }
+                    _ => (),
                 }
             }
-        }
+        },
         SetHedgehogsNumber(team_name, number) => {
-            if let (client, Some(room)) = server.client_and_room(client_id) {
-                let addable_hedgehogs = room.addable_hedgehogs();
-                if let Some((_, team)) = room.find_team_and_owner_mut(|t| t.name == team_name) {
-                    if !client.is_master() {
-                        response.add(Error("You're not the room master!".to_string()).send_self());
-                    } else if number < 1
-                        || number > MAX_HEDGEHOGS_PER_TEAM
-                        || number > addable_hedgehogs + team.hedgehogs_number
-                    {
-                        response.add(
-                            HedgehogsNumber(team.name.clone(), team.hedgehogs_number).send_self(),
-                        );
-                    } else {
-                        team.hedgehogs_number = number;
-                        response.add(
-                            HedgehogsNumber(team.name.clone(), number)
-                                .send_all()
-                                .in_room(room_id)
-                                .but_self(),
-                        );
-                    }
+            let addable_hedgehogs = room.addable_hedgehogs();
+            if let Some((_, team)) = room.find_team_and_owner_mut(|t| t.name == team_name) {
+                if !client.is_master() {
+                    response.add(Error("You're not the room master!".to_string()).send_self());
+                } else if number < 1
+                    || number > MAX_HEDGEHOGS_PER_TEAM
+                    || number > addable_hedgehogs + team.hedgehogs_number
+                {
+                    response
+                        .add(HedgehogsNumber(team.name.clone(), team.hedgehogs_number).send_self());
                 } else {
-                    response.add(Warning("No such team.".to_string()).send_self());
+                    team.hedgehogs_number = number;
+                    response.add(
+                        HedgehogsNumber(team.name.clone(), number)
+                            .send_all()
+                            .in_room(room_id)
+                            .but_self(),
+                    );
                 }
+            } else {
+                response.add(Warning("No such team.".to_string()).send_self());
             }
         }
         SetTeamColor(team_name, color) => {
-            if let (client, Some(room)) = server.client_and_room(client_id) {
-                if let Some((owner, team)) = room.find_team_and_owner_mut(|t| t.name == team_name) {
-                    if !client.is_master() {
-                        response.add(Error("You're not the room master!".to_string()).send_self());
-                    } else {
-                        team.color = color;
-                        response.add(
-                            TeamColor(team.name.clone(), color)
-                                .send_all()
-                                .in_room(room_id)
-                                .but_self(),
-                        );
-                        server.clients[owner].clan = Some(color);
-                    }
+            if let Some((owner, team)) = room.find_team_and_owner_mut(|t| t.name == team_name) {
+                if !client.is_master() {
+                    response.add(Error("You're not the room master!".to_string()).send_self());
                 } else {
-                    response.add(Warning("No such team.".to_string()).send_self());
+                    team.color = color;
+                    response.add(
+                        TeamColor(team.name.clone(), color)
+                            .send_all()
+                            .in_room(room_id)
+                            .but_self(),
+                    );
+                    server.clients[owner].clan = Some(color);
                 }
+            } else {
+                response.add(Warning("No such team.".to_string()).send_self());
             }
         }
         Cfg(cfg) => {
-            if let (client, Some(room)) = server.client_and_room(client_id) {
-                if room.is_fixed() {
-                    response.add(Warning("Access denied.".to_string()).send_self());
-                } else if !client.is_master() {
-                    response.add(Error("You're not the room master!".to_string()).send_self());
-                } else {
-                    let cfg = match cfg {
-                        GameCfg::Scheme(name, mut values) => {
-                            if client.protocol_number == 49 && values.len() >= 2 {
-                                let mut s = "X".repeat(50);
-                                s.push_str(&values.pop().unwrap());
-                                values.push(s);
-                            }
-                            GameCfg::Scheme(name, values)
+            if room.is_fixed() {
+                response.add(Warning("Access denied.".to_string()).send_self());
+            } else if !client.is_master() {
+                response.add(Error("You're not the room master!".to_string()).send_self());
+            } else {
+                let cfg = match cfg {
+                    GameCfg::Scheme(name, mut values) => {
+                        if client.protocol_number == 49 && values.len() >= 2 {
+                            let mut s = "X".repeat(50);
+                            s.push_str(&values.pop().unwrap());
+                            values.push(s);
                         }
-                        cfg => cfg,
-                    };
+                        GameCfg::Scheme(name, values)
+                    }
+                    cfg => cfg,
+                };
 
-                    response.add(cfg.to_server_msg().send_all().in_room(room.id).but_self());
-                    room.set_config(cfg);
-                }
+                response.add(cfg.to_server_msg().send_all().in_room(room.id).but_self());
+                room.set_config(cfg);
             }
         }
         Save(name, location) => {
@@ -353,11 +332,11 @@
                     .send_all()
                     .in_room(room_id),
             );
-            server.rooms[room_id].save_config(name, location);
+            room.save_config(name, location);
         }
         SaveRoom(filename) => {
-            if server.clients[client_id].is_admin() {
-                match server.rooms[room_id].get_saves() {
+            if client.is_admin() {
+                match room.get_saves() {
                     Ok(text) => match server.io.write_file(&filename, &text) {
                         Ok(_) => response.add(
                             server_chat("Room configs saved successfully.".to_string()).send_self(),
@@ -383,9 +362,9 @@
             }
         }
         LoadRoom(filename) => {
-            if server.clients[client_id].is_admin() {
+            if client.is_admin() {
                 match server.io.read_file(&filename) {
-                    Ok(text) => match server.rooms[room_id].set_saves(&text) {
+                    Ok(text) => match room.set_saves(&text) {
                         Ok(_) => response.add(
                             server_chat("Room configs loaded successfully.".to_string())
                                 .send_self(),
@@ -411,7 +390,7 @@
             }
         }
         Delete(name) => {
-            if !server.rooms[room_id].delete_config(&name) {
+            if !room.delete_config(&name) {
                 response.add(Warning(format!("Save doesn't exist: {}", name)).send_self());
             } else {
                 response.add(
@@ -426,7 +405,7 @@
                 .send_self());
         }
         CallVote(Some(kind)) => {
-            let is_in_game = server.rooms[room_id].game_info.is_some();
+            let is_in_game = room.game_info.is_some();
             let error = match &kind {
                 VoteType::Kick(nick) => {
                     if server
@@ -448,7 +427,7 @@
                     }
                 }
                 VoteType::Map(Some(name)) => {
-                    if server.rooms[room_id].saves.get(&name[..]).is_some() {
+                    if room.saves.get(&name[..]).is_some() {
                         None
                     } else {
                         Some("/callvote map: No such map!".to_string())
@@ -467,6 +446,7 @@
                     _ => Some("/callvote hedgehogs: Specify number from 1 to 8.".to_string()),
                 },
             };
+
             match error {
                 None => {
                     let msg = voting_description(&kind);
@@ -499,7 +479,7 @@
             );
         }
         ForceVote(vote) => {
-            let is_forced = server.clients[client_id].is_admin();
+            let is_forced = client.is_admin();
             super::common::submit_vote(
                 server,
                 coretypes::Vote {
@@ -510,8 +490,6 @@
             );
         }
         ToggleRestrictJoin | ToggleRestrictTeams | ToggleRegisteredOnly => {
-            let client = &server.clients[client_id];
-            let room = &mut server.rooms[room_id];
             if client.is_master() {
                 room.flags.toggle(room_message_flag(&message));
                 super::common::get_room_update(None, room, Some(&client), response);
@@ -521,49 +499,42 @@
             super::common::start_game(server, room_id, response);
         }
         EngineMessage(em) => {
-            if let (c, Some(r)) = server.client_and_room(client_id) {
-                if c.teams_in_game > 0 {
-                    let decoding = decode(&em[..]).unwrap();
-                    let messages = by_msg(&decoding);
-                    let valid = messages.filter(|m| is_msg_valid(m, &c.team_indices));
-                    let non_empty = valid.clone().filter(|m| !is_msg_empty(m));
-                    let sync_msg = valid.clone().filter(|m| is_msg_timed(m)).last().map(|m| {
-                        if is_msg_empty(m) {
-                            Some(encode(m))
-                        } else {
-                            None
-                        }
-                    });
+            if client.teams_in_game > 0 {
+                let decoding = decode(&em[..]).unwrap();
+                let messages = by_msg(&decoding);
+                let valid = messages.filter(|m| is_msg_valid(m, &client.team_indices));
+                let non_empty = valid.clone().filter(|m| !is_msg_empty(m));
+                let sync_msg = valid.clone().filter(|m| is_msg_timed(m)).last().map(|m| {
+                    if is_msg_empty(m) {
+                        Some(encode(m))
+                    } else {
+                        None
+                    }
+                });
 
-                    let em_response =
-                        encode(&valid.flat_map(|msg| msg).cloned().collect::<Vec<_>>());
-                    if !em_response.is_empty() {
-                        response.add(
-                            ForwardEngineMessage(vec![em_response])
-                                .send_all()
-                                .in_room(r.id)
-                                .but_self(),
-                        );
+                let em_response = encode(&valid.flat_map(|msg| msg).cloned().collect::<Vec<_>>());
+                if !em_response.is_empty() {
+                    response.add(
+                        ForwardEngineMessage(vec![em_response])
+                            .send_all()
+                            .in_room(room.id)
+                            .but_self(),
+                    );
+                }
+                let em_log = encode(&non_empty.flat_map(|msg| msg).cloned().collect::<Vec<_>>());
+                if let Some(ref mut info) = room.game_info {
+                    if !em_log.is_empty() {
+                        info.msg_log.push(em_log);
                     }
-                    let em_log =
-                        encode(&non_empty.flat_map(|msg| msg).cloned().collect::<Vec<_>>());
-                    if let Some(ref mut info) = r.game_info {
-                        if !em_log.is_empty() {
-                            info.msg_log.push(em_log);
-                        }
-                        if let Some(msg) = sync_msg {
-                            info.sync_msg = msg;
-                        }
+                    if let Some(msg) = sync_msg {
+                        info.sync_msg = msg;
                     }
                 }
             }
         }
         RoundFinished => {
             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()])
--- a/rust/hedgewars-server/src/server/network.rs	Thu Feb 07 22:26:56 2019 +0300
+++ b/rust/hedgewars-server/src/server/network.rs	Thu Feb 07 23:34:24 2019 +0300
@@ -227,10 +227,6 @@
     pub fn send_string(&mut self, msg: &str) {
         self.send_raw_msg(&msg.as_bytes());
     }
-
-    pub fn send_msg(&mut self, msg: &HWServerMessage) {
-        self.send_string(&msg.to_raw_protocol());
-    }
 }
 
 #[cfg(feature = "tls-connections")]