gameServer2/src/server/handlers/lobby.rs
author alfadur
Fri, 13 Jul 2018 19:52:19 +0300
changeset 13486 1ee192f13456
parent 13450 d79795acaa73
child 13492 ba5211dddb21
permissions -rw-r--r--
Better packing for clients
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
     1
use mio;
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
     2
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
     3
use server::{
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
     4
    server::HWServer,
13450
d79795acaa73 Mostly implement voting
alfadur
parents: 13445
diff changeset
     5
    coretypes::ClientId,
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
     6
    actions::{Action, Action::*}
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
     7
};
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
     8
use protocol::messages::{
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
     9
    HWProtocolMessage,
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    10
    HWServerMessage::*
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    11
};
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    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
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    14
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    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
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    17
    match message {
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    18
        CreateRoom(name, password) => {
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    19
            let actions =
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    20
                if is_name_illegal(&name) {
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    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())]
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    22
                } else if server.has_room(&name) {
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    23
                    vec![Warn("A room with the same name already exists.".to_string())]
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    24
                } else {
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    25
                    let flags_msg = ClientFlags(
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    26
                        "+hr".to_string(),
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    27
                        vec![server.clients[client_id].nick.clone()]);
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    28
                    vec![AddRoom(name, password),
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    29
                         flags_msg.send_self().action()]
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    30
                };
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    31
            server.react(client_id, actions);
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    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
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    35
            server.react(client_id, vec![chat_msg.send_all().but_self().action()]);
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    36
        },
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    37
        JoinRoom(name, password) => {
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    38
            let actions;
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    39
            {
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    40
                let room = server.rooms.iter().find(|(_, r)| r.name == name);
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    41
                let room_id = room.map(|(_, r)| r.id);
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    42
                let nicks = server.clients.iter()
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    43
                    .filter(|(_, c)| c.room_id == room_id)
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    44
                    .map(|(_, c)| c.nick.clone())
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    45
                    .collect();
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    46
                let c = &mut server.clients[client_id];
13427
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13419
diff changeset
    47
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13419
diff changeset
    48
                actions = if let Some((_, r)) = room {
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13419
diff changeset
    49
                    if c.protocol_number != r.protocol_number {
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13419
diff changeset
    50
                        vec![Warn("Room version incompatible to your Hedgewars version!".to_string())]
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13419
diff changeset
    51
                    } else {
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13419
diff changeset
    52
                        vec![MoveToRoom(r.id),
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13419
diff changeset
    53
                             RoomJoined(nicks).send_self().action()]
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    54
                    }
13427
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13419
diff changeset
    55
                } else {
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13419
diff changeset
    56
                    vec![Warn("No such room.".to_string())]
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    57
                };
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    58
            }
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    59
            server.react(client_id, actions);
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    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
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    66
        _ => warn!("Incorrect command in lobby state"),
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    67
    }
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    68
}