gameServer2/src/server/handlers/lobby.rs
author alfadur
Fri, 07 Dec 2018 18:32:36 +0300
changeset 14380 cc99f7c673c7
parent 14379 e5db279308d7
permissions -rw-r--r--
try again 🙃
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
13671
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13528
diff changeset
     3
use crate::{
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13528
diff changeset
     4
    server::{
14379
e5db279308d7 dispose of server mods
alfadur
parents: 13810
diff changeset
     5
        core::HWServer,
13671
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13528
diff changeset
     6
        coretypes::ClientId,
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13528
diff changeset
     7
        actions::{Action, Action::*}
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13528
diff changeset
     8
    },
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13528
diff changeset
     9
    protocol::messages::{
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13528
diff changeset
    10
        HWProtocolMessage,
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13528
diff changeset
    11
        HWServerMessage::*
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13528
diff changeset
    12
    },
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13528
diff changeset
    13
    utils::is_name_illegal
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    14
};
13526
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13483
diff changeset
    15
use super::common::rnd_reply;
13810
0463a4221327 cleanup crate imports
alfadur
parents: 13671
diff changeset
    16
use log::*;
12152
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    17
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    18
pub fn handle(server: &mut HWServer, client_id: ClientId, message: HWProtocolMessage) {
13671
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13528
diff changeset
    19
    use crate::protocol::messages::HWProtocolMessage::*;
12152
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    20
    match message {
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    21
        CreateRoom(name, password) => {
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    22
            let actions =
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    23
                if is_name_illegal(&name) {
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    24
                    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
    25
                } else if server.has_room(&name) {
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    26
                    vec![Warn("A room with the same name already exists.".to_string())]
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    27
                } else {
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    28
                    let flags_msg = ClientFlags(
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    29
                        "+hr".to_string(),
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    30
                        vec![server.clients[client_id].nick.clone()]);
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    31
                    vec![AddRoom(name, password),
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    32
                         flags_msg.send_self().action()]
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    33
                };
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    34
            server.react(client_id, actions);
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    35
        },
13124
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12857
diff changeset
    36
        Chat(msg) => {
13526
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13483
diff changeset
    37
            let actions = vec![ChatMsg {nick: server.clients[client_id].nick.clone(), msg}
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13483
diff changeset
    38
                .send_all().in_room(server.lobby_id).but_self().action()];
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13483
diff changeset
    39
            server.react(client_id, actions);
12152
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    40
        },
13671
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13528
diff changeset
    41
        JoinRoom(name, _password) => {
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13528
diff changeset
    42
            let room = server.rooms.iter().find(|(_, r)| r.name == name);
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13528
diff changeset
    43
            let room_id = room.map(|(_, r)| r.id);
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13528
diff changeset
    44
            let nicks = server.clients.iter()
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13528
diff changeset
    45
                .filter(|(_, c)| c.room_id == room_id)
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13528
diff changeset
    46
                .map(|(_, c)| c.nick.clone())
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13528
diff changeset
    47
                .collect();
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13528
diff changeset
    48
            let c = &mut server.clients[client_id];
13432
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13424
diff changeset
    49
13671
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13528
diff changeset
    50
            let actions = if let Some((_, r)) = room {
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13528
diff changeset
    51
                if c.protocol_number != r.protocol_number {
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13528
diff changeset
    52
                    vec![Warn("Room version incompatible to your Hedgewars version!".to_string())]
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13528
diff changeset
    53
                } else if r.is_join_restricted() {
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13528
diff changeset
    54
                    vec![Warn("Access denied. This room currently doesn't allow joining.".to_string())]
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13528
diff changeset
    55
                } else if r.players_number == u8::max_value() {
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13528
diff changeset
    56
                    vec![Warn("This room is already full".to_string())]
13432
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13424
diff changeset
    57
                } else {
13671
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13528
diff changeset
    58
                    vec![MoveToRoom(r.id),
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13528
diff changeset
    59
                         RoomJoined(nicks).send_self().action()]
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13528
diff changeset
    60
                }
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13528
diff changeset
    61
            } else {
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13528
diff changeset
    62
                vec![Warn("No such room.".to_string())]
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13528
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
}