--- a/rust/hedgewars-server/src/server/core.rs Sun Dec 16 00:09:20 2018 +0100
+++ b/rust/hedgewars-server/src/server/core.rs Sun Dec 16 00:12:29 2018 +0100
@@ -1,15 +1,18 @@
-use slab;
-use crate::utils;
use super::{
+ actions,
+ actions::{Destination, PendingMessage},
+ client::HWClient,
+ coretypes::{ClientId, RoomId},
+ handlers,
io::HWServerIO,
- client::HWClient, room::HWRoom, actions, handlers,
- coretypes::{ClientId, RoomId},
- actions::{Destination, PendingMessage}
+ room::HWRoom,
};
use crate::protocol::messages::*;
-use rand::{RngCore, thread_rng};
-use base64::{encode};
+use crate::utils;
+use base64::encode;
use log::*;
+use rand::{thread_rng, RngCore};
+use slab;
type Slab<T> = slab::Slab<T>;
@@ -19,7 +22,7 @@
pub lobby_id: RoomId,
pub output: Vec<(Vec<ClientId>, HWServerMessage)>,
pub removed_clients: Vec<ClientId>,
- pub io: Box<dyn HWServerIO>
+ pub io: Box<dyn HWServerIO>,
}
impl HWServer {
@@ -27,11 +30,12 @@
let rooms = Slab::with_capacity(rooms_limit);
let clients = Slab::with_capacity(clients_limit);
let mut server = HWServer {
- clients, rooms,
+ clients,
+ rooms,
lobby_id: 0,
output: vec![],
removed_clients: vec![],
- io
+ io,
};
server.lobby_id = server.add_room();
server
@@ -48,13 +52,20 @@
let client = HWClient::new(entry.key(), encode(&salt));
entry.insert(client);
}
- self.send(key, &Destination::ToSelf, HWServerMessage::Connected(utils::PROTOCOL_VERSION));
+ self.send(
+ key,
+ &Destination::ToSelf,
+ HWServerMessage::Connected(utils::PROTOCOL_VERSION),
+ );
key
}
pub fn client_lost(&mut self, client_id: ClientId) {
- actions::run_action(self, client_id,
- actions::Action::ByeClient("Connection reset".to_string()));
+ actions::run_action(
+ self,
+ client_id,
+ actions::Action::ByeClient("Connection reset".to_string()),
+ );
}
pub fn add_room(&mut self) -> RoomId {
@@ -76,14 +87,19 @@
let mut ids = match *destination {
Destination::ToSelf => vec![client_id],
Destination::ToId(id) => vec![id],
- Destination::ToAll {room_id: Some(id), ..} =>
- self.room_clients(id),
- Destination::ToAll {protocol: Some(proto), ..} =>
- self.protocol_clients(proto),
- Destination::ToAll {..} =>
- self.clients.iter().map(|(id, _)| id).collect::<Vec<_>>()
+ Destination::ToAll {
+ room_id: Some(id), ..
+ } => self.room_clients(id),
+ Destination::ToAll {
+ protocol: Some(proto),
+ ..
+ } => self.protocol_clients(proto),
+ Destination::ToAll { .. } => self.clients.iter().map(|(id, _)| id).collect::<Vec<_>>(),
};
- if let Destination::ToAll {skip_self: true, ..} = destination {
+ if let Destination::ToAll {
+ skip_self: true, ..
+ } = destination
+ {
if let Some(index) = ids.iter().position(|id| *id == client_id) {
ids.remove(index);
}
@@ -91,7 +107,12 @@
ids
}
- pub fn send(&mut self, client_id: ClientId, destination: &Destination, message: HWServerMessage) {
+ pub fn send(
+ &mut self,
+ client_id: ClientId,
+ destination: &Destination,
+ message: HWServerMessage,
+ ) {
let ids = self.get_recipients(client_id, &destination);
self.output.push((ids, message));
}
@@ -102,32 +123,43 @@
}
}
- pub fn lobby(&self) -> &HWRoom { &self.rooms[self.lobby_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)
}
pub fn find_room(&self, name: &str) -> Option<&HWRoom> {
- self.rooms.iter().find_map(|(_, r)| Some(r).filter(|r| r.name == name))
+ self.rooms
+ .iter()
+ .find_map(|(_, r)| Some(r).filter(|r| r.name == name))
}
pub fn find_room_mut(&mut self, name: &str) -> Option<&mut HWRoom> {
- self.rooms.iter_mut().find_map(|(_, r)| Some(r).filter(|r| r.name == name))
+ self.rooms
+ .iter_mut()
+ .find_map(|(_, r)| Some(r).filter(|r| r.name == name))
}
pub fn find_client(&self, nick: &str) -> Option<&HWClient> {
- self.clients.iter().find_map(|(_, c)| Some(c).filter(|c| c.nick == nick))
+ self.clients
+ .iter()
+ .find_map(|(_, c)| Some(c).filter(|c| c.nick == nick))
}
pub fn find_client_mut(&mut self, nick: &str) -> Option<&mut HWClient> {
- self.clients.iter_mut().find_map(|(_, c)| Some(c).filter(|c| c.nick == nick))
+ self.clients
+ .iter_mut()
+ .find_map(|(_, c)| Some(c).filter(|c| c.nick == nick))
}
pub fn select_clients<F>(&self, f: F) -> Vec<ClientId>
- where F: Fn(&(usize, &HWClient)) -> bool {
- self.clients.iter().filter(f)
- .map(|(_, c)| c.id).collect()
+ where
+ F: Fn(&(usize, &HWClient)) -> bool,
+ {
+ self.clients.iter().filter(f).map(|(_, c)| c.id).collect()
}
pub fn room_clients(&self, room_id: RoomId) -> Vec<ClientId> {
@@ -140,7 +172,7 @@
pub fn other_clients_in_room(&self, self_id: ClientId) -> Vec<ClientId> {
let room_id = self.clients[self_id].room_id;
- self.select_clients(|(id, c)| *id != self_id && c.room_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>) {