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