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