1 use mio; |
1 use mio; |
2 |
2 |
3 use server::{ |
3 use crate::{ |
4 server::HWServer, |
4 server::{ |
5 coretypes::ClientId, |
5 server::HWServer, |
6 actions::{Action, Action::*} |
6 coretypes::ClientId, |
|
7 actions::{Action, Action::*} |
|
8 }, |
|
9 protocol::messages::{ |
|
10 HWProtocolMessage, |
|
11 HWServerMessage::* |
|
12 }, |
|
13 utils::is_name_illegal |
7 }; |
14 }; |
8 use protocol::messages::{ |
|
9 HWProtocolMessage, |
|
10 HWServerMessage::* |
|
11 }; |
|
12 use utils::is_name_illegal; |
|
13 use super::common::rnd_reply; |
15 use super::common::rnd_reply; |
14 |
16 |
15 pub fn handle(server: &mut HWServer, client_id: ClientId, message: HWProtocolMessage) { |
17 pub fn handle(server: &mut HWServer, client_id: ClientId, message: HWProtocolMessage) { |
16 use protocol::messages::HWProtocolMessage::*; |
18 use crate::protocol::messages::HWProtocolMessage::*; |
17 match message { |
19 match message { |
18 CreateRoom(name, password) => { |
20 CreateRoom(name, password) => { |
19 let actions = |
21 let actions = |
20 if is_name_illegal(&name) { |
22 if is_name_illegal(&name) { |
21 vec![Warn("Illegal room name! A room name must be between 1-40 characters long, must not have a trailing or leading space and must not have any of these characters: $()*+?[]^{|}".to_string())] |
23 vec![Warn("Illegal room name! A room name must be between 1-40 characters long, must not have a trailing or leading space and must not have any of these characters: $()*+?[]^{|}".to_string())] |
33 Chat(msg) => { |
35 Chat(msg) => { |
34 let actions = vec![ChatMsg {nick: server.clients[client_id].nick.clone(), msg} |
36 let actions = vec![ChatMsg {nick: server.clients[client_id].nick.clone(), msg} |
35 .send_all().in_room(server.lobby_id).but_self().action()]; |
37 .send_all().in_room(server.lobby_id).but_self().action()]; |
36 server.react(client_id, actions); |
38 server.react(client_id, actions); |
37 }, |
39 }, |
38 JoinRoom(name, password) => { |
40 JoinRoom(name, _password) => { |
39 let actions; |
41 let room = server.rooms.iter().find(|(_, r)| r.name == name); |
40 { |
42 let room_id = room.map(|(_, r)| r.id); |
41 let room = server.rooms.iter().find(|(_, r)| r.name == name); |
43 let nicks = server.clients.iter() |
42 let room_id = room.map(|(_, r)| r.id); |
44 .filter(|(_, c)| c.room_id == room_id) |
43 let nicks = server.clients.iter() |
45 .map(|(_, c)| c.nick.clone()) |
44 .filter(|(_, c)| c.room_id == room_id) |
46 .collect(); |
45 .map(|(_, c)| c.nick.clone()) |
47 let c = &mut server.clients[client_id]; |
46 .collect(); |
|
47 let c = &mut server.clients[client_id]; |
|
48 |
48 |
49 actions = if let Some((_, r)) = room { |
49 let actions = if let Some((_, r)) = room { |
50 if c.protocol_number != r.protocol_number { |
50 if c.protocol_number != r.protocol_number { |
51 vec![Warn("Room version incompatible to your Hedgewars version!".to_string())] |
51 vec![Warn("Room version incompatible to your Hedgewars version!".to_string())] |
52 } else if r.is_join_restricted() { |
52 } else if r.is_join_restricted() { |
53 vec![Warn("Access denied. This room currently doesn't allow joining.".to_string())] |
53 vec![Warn("Access denied. This room currently doesn't allow joining.".to_string())] |
54 } else if r.players_number == u8::max_value() { |
54 } else if r.players_number == u8::max_value() { |
55 vec![Warn("This room is already full".to_string())] |
55 vec![Warn("This room is already full".to_string())] |
56 } else { |
|
57 vec![MoveToRoom(r.id), |
|
58 RoomJoined(nicks).send_self().action()] |
|
59 } |
|
60 } else { |
56 } else { |
61 vec![Warn("No such room.".to_string())] |
57 vec![MoveToRoom(r.id), |
62 }; |
58 RoomJoined(nicks).send_self().action()] |
63 } |
59 } |
|
60 } else { |
|
61 vec![Warn("No such room.".to_string())] |
|
62 }; |
64 server.react(client_id, actions); |
63 server.react(client_id, actions); |
65 }, |
64 }, |
66 Rnd(v) => { |
65 Rnd(v) => { |
67 server.react(client_id, vec![rnd_reply(&v).send_self().action()]); |
66 server.react(client_id, vec![rnd_reply(&v).send_self().action()]); |
68 }, |
67 }, |