1 use mio; |
|
2 |
|
3 use crate::{ |
|
4 server::{ |
|
5 core::HWServer, |
|
6 coretypes::ClientId, |
|
7 actions::{Action, Action::*} |
|
8 }, |
|
9 protocol::messages::{ |
|
10 HWProtocolMessage, |
|
11 HWServerMessage::* |
|
12 }, |
|
13 utils::is_name_illegal |
|
14 }; |
|
15 use super::common::rnd_reply; |
|
16 use log::*; |
|
17 |
|
18 pub fn handle(server: &mut HWServer, client_id: ClientId, message: HWProtocolMessage) { |
|
19 use crate::protocol::messages::HWProtocolMessage::*; |
|
20 match message { |
|
21 CreateRoom(name, password) => { |
|
22 let actions = |
|
23 if is_name_illegal(&name) { |
|
24 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())] |
|
25 } else if server.has_room(&name) { |
|
26 vec![Warn("A room with the same name already exists.".to_string())] |
|
27 } else { |
|
28 let flags_msg = ClientFlags( |
|
29 "+hr".to_string(), |
|
30 vec![server.clients[client_id].nick.clone()]); |
|
31 vec![AddRoom(name, password), |
|
32 flags_msg.send_self().action()] |
|
33 }; |
|
34 server.react(client_id, actions); |
|
35 }, |
|
36 Chat(msg) => { |
|
37 let actions = vec![ChatMsg {nick: server.clients[client_id].nick.clone(), msg} |
|
38 .send_all().in_room(server.lobby_id).but_self().action()]; |
|
39 server.react(client_id, actions); |
|
40 }, |
|
41 JoinRoom(name, _password) => { |
|
42 let room = server.rooms.iter().find(|(_, r)| r.name == name); |
|
43 let room_id = room.map(|(_, r)| r.id); |
|
44 let nicks = server.clients.iter() |
|
45 .filter(|(_, c)| c.room_id == room_id) |
|
46 .map(|(_, c)| c.nick.clone()) |
|
47 .collect(); |
|
48 let c = &mut server.clients[client_id]; |
|
49 |
|
50 let actions = if let Some((_, r)) = room { |
|
51 if c.protocol_number != r.protocol_number { |
|
52 vec![Warn("Room version incompatible to your Hedgewars version!".to_string())] |
|
53 } else if r.is_join_restricted() { |
|
54 vec![Warn("Access denied. This room currently doesn't allow joining.".to_string())] |
|
55 } else if r.players_number == u8::max_value() { |
|
56 vec![Warn("This room is already full".to_string())] |
|
57 } else { |
|
58 vec![MoveToRoom(r.id), |
|
59 RoomJoined(nicks).send_self().action()] |
|
60 } |
|
61 } else { |
|
62 vec![Warn("No such room.".to_string())] |
|
63 }; |
|
64 server.react(client_id, actions); |
|
65 }, |
|
66 Rnd(v) => { |
|
67 server.react(client_id, vec![rnd_reply(&v).send_self().action()]); |
|
68 }, |
|
69 List => warn!("Deprecated LIST message received"), |
|
70 _ => warn!("Incorrect command in lobby state"), |
|
71 } |
|
72 } |
|