gameServer2/src/server/handlers/lobby.rs
author alfadur
Thu, 16 Aug 2018 19:33:03 +0300
changeset 13666 09f4a30e50cc
parent 13494 8c5dd562c9f7
child 13810 0463a4221327
permissions -rw-r--r--
Rust 2018 conversion
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
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13494
diff changeset
     3
use crate::{
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13494
diff changeset
     4
    server::{
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13494
diff changeset
     5
        server::HWServer,
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13494
diff changeset
     6
        coretypes::ClientId,
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13494
diff changeset
     7
        actions::{Action, Action::*}
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13494
diff changeset
     8
    },
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13494
diff changeset
     9
    protocol::messages::{
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13494
diff changeset
    10
        HWProtocolMessage,
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13494
diff changeset
    11
        HWServerMessage::*
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13494
diff changeset
    12
    },
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13494
diff changeset
    13
    utils::is_name_illegal
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    14
};
13492
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13450
diff changeset
    15
use super::common::rnd_reply;
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    16
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    17
pub fn handle(server: &mut HWServer, client_id: ClientId, message: HWProtocolMessage) {
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13494
diff changeset
    18
    use crate::protocol::messages::HWProtocolMessage::*;
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    19
    match message {
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    20
        CreateRoom(name, password) => {
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    21
            let actions =
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    22
                if is_name_illegal(&name) {
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    23
                    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
    24
                } else if server.has_room(&name) {
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    25
                    vec![Warn("A room with the same name already exists.".to_string())]
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    26
                } else {
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    27
                    let flags_msg = ClientFlags(
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    28
                        "+hr".to_string(),
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    29
                        vec![server.clients[client_id].nick.clone()]);
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    30
                    vec![AddRoom(name, password),
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    31
                         flags_msg.send_self().action()]
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    32
                };
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    33
            server.react(client_id, actions);
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    34
        },
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12852
diff changeset
    35
        Chat(msg) => {
13492
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13450
diff changeset
    36
            let actions = vec![ChatMsg {nick: server.clients[client_id].nick.clone(), msg}
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13450
diff changeset
    37
                .send_all().in_room(server.lobby_id).but_self().action()];
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13450
diff changeset
    38
            server.react(client_id, actions);
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    39
        },
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13494
diff changeset
    40
        JoinRoom(name, _password) => {
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13494
diff changeset
    41
            let room = server.rooms.iter().find(|(_, r)| r.name == name);
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13494
diff changeset
    42
            let room_id = room.map(|(_, r)| r.id);
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13494
diff changeset
    43
            let nicks = server.clients.iter()
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13494
diff changeset
    44
                .filter(|(_, c)| c.room_id == room_id)
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13494
diff changeset
    45
                .map(|(_, c)| c.nick.clone())
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13494
diff changeset
    46
                .collect();
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13494
diff changeset
    47
            let c = &mut server.clients[client_id];
13427
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13419
diff changeset
    48
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13494
diff changeset
    49
            let actions = if let Some((_, r)) = room {
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13494
diff changeset
    50
                if c.protocol_number != r.protocol_number {
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13494
diff changeset
    51
                    vec![Warn("Room version incompatible to your Hedgewars version!".to_string())]
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13494
diff changeset
    52
                } else if r.is_join_restricted() {
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13494
diff changeset
    53
                    vec![Warn("Access denied. This room currently doesn't allow joining.".to_string())]
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13494
diff changeset
    54
                } else if r.players_number == u8::max_value() {
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13494
diff changeset
    55
                    vec![Warn("This room is already full".to_string())]
13427
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13419
diff changeset
    56
                } else {
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13494
diff changeset
    57
                    vec![MoveToRoom(r.id),
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13494
diff changeset
    58
                         RoomJoined(nicks).send_self().action()]
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13494
diff changeset
    59
                }
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13494
diff changeset
    60
            } else {
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13494
diff changeset
    61
                vec![Warn("No such room.".to_string())]
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13494
diff changeset
    62
            };
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    63
            server.react(client_id, actions);
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    64
        },
13445
d3c86ade3d4d Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13444
diff changeset
    65
        Rnd(v) => {
13492
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13450
diff changeset
    66
            server.react(client_id, vec![rnd_reply(&v).send_self().action()]);
13445
d3c86ade3d4d Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13444
diff changeset
    67
        },
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12852
diff changeset
    68
        List => warn!("Deprecated LIST message received"),
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    69
        _ => warn!("Incorrect command in lobby state"),
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    70
    }
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    71
}