author | alfadur |
Sat, 07 Jul 2018 20:22:31 +0300 | |
changeset 13450 | d79795acaa73 |
parent 13445 | d3c86ade3d4d |
child 13492 | ba5211dddb21 |
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; |
|
13445
d3c86ade3d4d
Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13444
diff
changeset
|
13 |
use super::common::rnd_action; |
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) => { |
13444
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13427
diff
changeset
|
34 |
let chat_msg = ChatMsg {nick: server.clients[client_id].nick.clone(), msg: msg}; |
13419 | 35 |
server.react(client_id, vec![chat_msg.send_all().but_self().action()]); |
12147 | 36 |
}, |
13416 | 37 |
JoinRoom(name, password) => { |
38 |
let actions; |
|
39 |
{ |
|
40 |
let room = server.rooms.iter().find(|(_, r)| r.name == name); |
|
41 |
let room_id = room.map(|(_, r)| r.id); |
|
42 |
let nicks = server.clients.iter() |
|
43 |
.filter(|(_, c)| c.room_id == room_id) |
|
44 |
.map(|(_, c)| c.nick.clone()) |
|
45 |
.collect(); |
|
13419 | 46 |
let c = &mut server.clients[client_id]; |
13427 | 47 |
|
48 |
actions = if let Some((_, r)) = room { |
|
49 |
if c.protocol_number != r.protocol_number { |
|
50 |
vec![Warn("Room version incompatible to your Hedgewars version!".to_string())] |
|
51 |
} else { |
|
52 |
vec![MoveToRoom(r.id), |
|
53 |
RoomJoined(nicks).send_self().action()] |
|
13416 | 54 |
} |
13427 | 55 |
} else { |
56 |
vec![Warn("No such room.".to_string())] |
|
13416 | 57 |
}; |
12147 | 58 |
} |
13419 | 59 |
server.react(client_id, actions); |
12147 | 60 |
}, |
13445
d3c86ade3d4d
Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13444
diff
changeset
|
61 |
Rnd(v) => { |
d3c86ade3d4d
Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13444
diff
changeset
|
62 |
let actions = rnd_action(v, server.room(client_id)); |
d3c86ade3d4d
Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13444
diff
changeset
|
63 |
server.react(client_id, actions) |
d3c86ade3d4d
Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13444
diff
changeset
|
64 |
}, |
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12852
diff
changeset
|
65 |
List => warn!("Deprecated LIST message received"), |
12147 | 66 |
_ => warn!("Incorrect command in lobby state"), |
67 |
} |
|
68 |
} |