author | alfadur |
Fri, 05 Oct 2018 21:43:48 -0400 | |
changeset 13876 | 5df16be168ff |
parent 13810 | 0463a4221327 |
child 14379 | e5db279308d7 |
permissions | -rw-r--r-- |
12152 | 1 |
use mio; |
2 |
||
13671 | 3 |
use crate::{ |
4 |
server::{ |
|
5 |
server::HWServer, |
|
6 |
coretypes::ClientId, |
|
7 |
actions::{Action, Action::*} |
|
8 |
}, |
|
9 |
protocol::messages::{ |
|
10 |
HWProtocolMessage, |
|
11 |
HWServerMessage::* |
|
12 |
}, |
|
13 |
utils::is_name_illegal |
|
13421 | 14 |
}; |
13526 | 15 |
use super::common::rnd_reply; |
13810 | 16 |
use log::*; |
12152 | 17 |
|
13424 | 18 |
pub fn handle(server: &mut HWServer, client_id: ClientId, message: HWProtocolMessage) { |
13671 | 19 |
use crate::protocol::messages::HWProtocolMessage::*; |
12152 | 20 |
match message { |
13421 | 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(), |
|
13424 | 30 |
vec![server.clients[client_id].nick.clone()]); |
13421 | 31 |
vec![AddRoom(name, password), |
13424 | 32 |
flags_msg.send_self().action()] |
13421 | 33 |
}; |
13424 | 34 |
server.react(client_id, actions); |
13421 | 35 |
}, |
13124
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12857
diff
changeset
|
36 |
Chat(msg) => { |
13526 | 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); |
|
12152 | 40 |
}, |
13671 | 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]; |
|
13432 | 49 |
|
13671 | 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())] |
|
13432 | 57 |
} else { |
13671 | 58 |
vec![MoveToRoom(r.id), |
59 |
RoomJoined(nicks).send_self().action()] |
|
60 |
} |
|
61 |
} else { |
|
62 |
vec![Warn("No such room.".to_string())] |
|
63 |
}; |
|
13424 | 64 |
server.react(client_id, actions); |
12152 | 65 |
}, |
13450
d3c86ade3d4d
Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13449
diff
changeset
|
66 |
Rnd(v) => { |
13526 | 67 |
server.react(client_id, vec![rnd_reply(&v).send_self().action()]); |
13450
d3c86ade3d4d
Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13449
diff
changeset
|
68 |
}, |
13124
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12857
diff
changeset
|
69 |
List => warn!("Deprecated LIST message received"), |
12152 | 70 |
_ => warn!("Incorrect command in lobby state"), |
71 |
} |
|
72 |
} |