author | alfadur |
Tue, 17 Jul 2018 00:27:24 +0300 | |
changeset 13494 | 8c5dd562c9f7 |
parent 13492 | ba5211dddb21 |
child 13666 | 09f4a30e50cc |
permissions | -rw-r--r-- |
12147 | 1 |
use mio; |
2 |
||
13416 | 3 |
use server::{ |
4 |
server::HWServer, |
|
13450 | 5 |
coretypes::ClientId, |
13416 | 6 |
actions::{Action, Action::*} |
7 |
}; |
|
8 |
use protocol::messages::{ |
|
9 |
HWProtocolMessage, |
|
10 |
HWServerMessage::* |
|
11 |
}; |
|
12 |
use utils::is_name_illegal; |
|
13492 | 13 |
use super::common::rnd_reply; |
12147 | 14 |
|
13419 | 15 |
pub fn handle(server: &mut HWServer, client_id: ClientId, message: HWProtocolMessage) { |
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12852
diff
changeset
|
16 |
use protocol::messages::HWProtocolMessage::*; |
12147 | 17 |
match message { |
13416 | 18 |
CreateRoom(name, password) => { |
19 |
let actions = |
|
20 |
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())] |
|
22 |
} else if server.has_room(&name) { |
|
23 |
vec![Warn("A room with the same name already exists.".to_string())] |
|
24 |
} else { |
|
25 |
let flags_msg = ClientFlags( |
|
26 |
"+hr".to_string(), |
|
13419 | 27 |
vec![server.clients[client_id].nick.clone()]); |
13416 | 28 |
vec![AddRoom(name, password), |
13419 | 29 |
flags_msg.send_self().action()] |
13416 | 30 |
}; |
13419 | 31 |
server.react(client_id, actions); |
13416 | 32 |
}, |
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12852
diff
changeset
|
33 |
Chat(msg) => { |
13492 | 34 |
let actions = vec![ChatMsg {nick: server.clients[client_id].nick.clone(), msg} |
35 |
.send_all().in_room(server.lobby_id).but_self().action()]; |
|
36 |
server.react(client_id, actions); |
|
12147 | 37 |
}, |
13416 | 38 |
JoinRoom(name, password) => { |
39 |
let actions; |
|
40 |
{ |
|
41 |
let room = server.rooms.iter().find(|(_, r)| r.name == name); |
|
42 |
let room_id = room.map(|(_, r)| r.id); |
|
43 |
let nicks = server.clients.iter() |
|
44 |
.filter(|(_, c)| c.room_id == room_id) |
|
45 |
.map(|(_, c)| c.nick.clone()) |
|
46 |
.collect(); |
|
13419 | 47 |
let c = &mut server.clients[client_id]; |
13427 | 48 |
|
49 |
actions = if let Some((_, r)) = room { |
|
50 |
if c.protocol_number != r.protocol_number { |
|
51 |
vec![Warn("Room version incompatible to your Hedgewars version!".to_string())] |
|
13494 | 52 |
} else if r.is_join_restricted() { |
53 |
vec![Warn("Access denied. This room currently doesn't allow joining.".to_string())] |
|
54 |
} else if r.players_number == u8::max_value() { |
|
55 |
vec![Warn("This room is already full".to_string())] |
|
13427 | 56 |
} else { |
57 |
vec![MoveToRoom(r.id), |
|
58 |
RoomJoined(nicks).send_self().action()] |
|
13416 | 59 |
} |
13427 | 60 |
} else { |
61 |
vec![Warn("No such room.".to_string())] |
|
13416 | 62 |
}; |
12147 | 63 |
} |
13419 | 64 |
server.react(client_id, actions); |
12147 | 65 |
}, |
13445
d3c86ade3d4d
Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13444
diff
changeset
|
66 |
Rnd(v) => { |
13492 | 67 |
server.react(client_id, vec![rnd_reply(&v).send_self().action()]); |
13445
d3c86ade3d4d
Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13444
diff
changeset
|
68 |
}, |
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12852
diff
changeset
|
69 |
List => warn!("Deprecated LIST message received"), |
12147 | 70 |
_ => warn!("Incorrect command in lobby state"), |
71 |
} |
|
72 |
} |