gameServer2/src/server/handlers/lobby.rs
author alfadur
Thu, 19 Jul 2018 22:50:46 +0300
changeset 13531 3f69acc7e268
parent 13528 8c5dd562c9f7
child 13671 09f4a30e50cc
permissions -rw-r--r--
clean up room lookups
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12152
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
     1
use mio;
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
     2
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
     3
use server::{
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
     4
    server::HWServer,
13483
d79795acaa73 Mostly implement voting
alfadur
parents: 13450
diff changeset
     5
    coretypes::ClientId,
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
     6
    actions::{Action, Action::*}
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
     7
};
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
     8
use protocol::messages::{
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
     9
    HWProtocolMessage,
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    10
    HWServerMessage::*
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    11
};
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    12
use utils::is_name_illegal;
13526
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13483
diff changeset
    13
use super::common::rnd_reply;
12152
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    14
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    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
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    17
    match message {
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    18
        CreateRoom(name, password) => {
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    19
            let actions =
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    20
                if is_name_illegal(&name) {
cdf69667593b partial room implementation
alfadur
parents: 13124
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: 13124
diff changeset
    22
                } else if server.has_room(&name) {
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    23
                    vec![Warn("A room with the same name already exists.".to_string())]
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    24
                } else {
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    25
                    let flags_msg = ClientFlags(
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    26
                        "+hr".to_string(),
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    27
                        vec![server.clients[client_id].nick.clone()]);
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    28
                    vec![AddRoom(name, password),
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    29
                         flags_msg.send_self().action()]
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    30
                };
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    31
            server.react(client_id, actions);
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    32
        },
13124
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12857
diff changeset
    33
        Chat(msg) => {
13526
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13483
diff changeset
    34
            let actions = vec![ChatMsg {nick: server.clients[client_id].nick.clone(), msg}
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13483
diff changeset
    35
                .send_all().in_room(server.lobby_id).but_self().action()];
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13483
diff changeset
    36
            server.react(client_id, actions);
12152
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    37
        },
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    38
        JoinRoom(name, password) => {
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    39
            let actions;
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    40
            {
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    41
                let room = server.rooms.iter().find(|(_, r)| r.name == name);
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    42
                let room_id = room.map(|(_, r)| r.id);
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    43
                let nicks = server.clients.iter()
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    44
                    .filter(|(_, c)| c.room_id == room_id)
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    45
                    .map(|(_, c)| c.nick.clone())
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    46
                    .collect();
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    47
                let c = &mut server.clients[client_id];
13432
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13424
diff changeset
    48
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13424
diff changeset
    49
                actions = if let Some((_, r)) = room {
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13424
diff changeset
    50
                    if c.protocol_number != r.protocol_number {
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13424
diff changeset
    51
                        vec![Warn("Room version incompatible to your Hedgewars version!".to_string())]
13528
8c5dd562c9f7 Add room flags
alfadur
parents: 13526
diff changeset
    52
                    } else if r.is_join_restricted() {
8c5dd562c9f7 Add room flags
alfadur
parents: 13526
diff changeset
    53
                        vec![Warn("Access denied. This room currently doesn't allow joining.".to_string())]
8c5dd562c9f7 Add room flags
alfadur
parents: 13526
diff changeset
    54
                    } else if r.players_number == u8::max_value() {
8c5dd562c9f7 Add room flags
alfadur
parents: 13526
diff changeset
    55
                        vec![Warn("This room is already full".to_string())]
13432
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13424
diff changeset
    56
                    } else {
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13424
diff changeset
    57
                        vec![MoveToRoom(r.id),
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13424
diff changeset
    58
                             RoomJoined(nicks).send_self().action()]
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    59
                    }
13432
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13424
diff changeset
    60
                } else {
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13424
diff changeset
    61
                    vec![Warn("No such room.".to_string())]
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    62
                };
12152
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    63
            }
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    64
            server.react(client_id, actions);
12152
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    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
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13483
diff changeset
    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
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    70
        _ => warn!("Incorrect command in lobby state"),
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    71
    }
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    72
}