author | alfadur |
Thu, 19 Jul 2018 22:50:46 +0300 | |
changeset 13531 | 3f69acc7e268 |
parent 13528 | 8c5dd562c9f7 |
child 13671 | 09f4a30e50cc |
permissions | -rw-r--r-- |
12152 | 1 |
use mio; |
2 |
||
13421 | 3 |
use server::{ |
4 |
server::HWServer, |
|
13483 | 5 |
coretypes::ClientId, |
13421 | 6 |
actions::{Action, Action::*} |
7 |
}; |
|
8 |
use protocol::messages::{ |
|
9 |
HWProtocolMessage, |
|
10 |
HWServerMessage::* |
|
11 |
}; |
|
12 |
use utils::is_name_illegal; |
|
13526 | 13 |
use super::common::rnd_reply; |
12152 | 14 |
|
13424 | 15 |
pub fn handle(server: &mut HWServer, client_id: ClientId, message: HWProtocolMessage) { |
13124
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12857
diff
changeset
|
16 |
use protocol::messages::HWProtocolMessage::*; |
12152 | 17 |
match message { |
13421 | 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(), |
|
13424 | 27 |
vec![server.clients[client_id].nick.clone()]); |
13421 | 28 |
vec![AddRoom(name, password), |
13424 | 29 |
flags_msg.send_self().action()] |
13421 | 30 |
}; |
13424 | 31 |
server.react(client_id, actions); |
13421 | 32 |
}, |
13124
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12857
diff
changeset
|
33 |
Chat(msg) => { |
13526 | 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); |
|
12152 | 37 |
}, |
13421 | 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(); |
|
13424 | 47 |
let c = &mut server.clients[client_id]; |
13432 | 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())] |
|
13528 | 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())] |
|
13432 | 56 |
} else { |
57 |
vec![MoveToRoom(r.id), |
|
58 |
RoomJoined(nicks).send_self().action()] |
|
13421 | 59 |
} |
13432 | 60 |
} else { |
61 |
vec![Warn("No such room.".to_string())] |
|
13421 | 62 |
}; |
12152 | 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 |
} |