rust/hedgewars-server/src/handlers/inlobby.rs
author Wuzzy <Wuzzy2@mail.ru>
Thu, 18 Jul 2019 01:46:40 +0200
changeset 15255 8dd3602a4500
parent 15080 e935b1ad23f3
child 15444 a158ff8f84ef
permissions -rw-r--r--
Pagevideos: Create header object explicitly
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
14462
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
     3
use super::common::rnd_reply;
13671
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13528
diff changeset
     4
use crate::{
15080
e935b1ad23f3 normalize type names
alfadur
parents: 15079
diff changeset
     5
    core::{
e935b1ad23f3 normalize type names
alfadur
parents: 15079
diff changeset
     6
        client::HwClient,
e935b1ad23f3 normalize type names
alfadur
parents: 15079
diff changeset
     7
        server::HwServer,
e935b1ad23f3 normalize type names
alfadur
parents: 15079
diff changeset
     8
        types::{ClientId, ServerVar},
14787
50fcef24003f add typed protocol flags
alfadur
parents: 14699
diff changeset
     9
    },
15080
e935b1ad23f3 normalize type names
alfadur
parents: 15079
diff changeset
    10
    protocol::messages::{
e935b1ad23f3 normalize type names
alfadur
parents: 15079
diff changeset
    11
        add_flags, remove_flags, server_chat, HwProtocolMessage, HwServerMessage::*,
e935b1ad23f3 normalize type names
alfadur
parents: 15079
diff changeset
    12
        ProtocolFlags as Flags,
14788
b3adc030104b implement server vars
alfadur
parents: 14787
diff changeset
    13
    },
14462
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
    14
    utils::is_name_illegal,
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    15
};
13810
0463a4221327 cleanup crate imports
alfadur
parents: 13671
diff changeset
    16
use log::*;
14794
18240b308505 implement stats message
alfadur
parents: 14792
diff changeset
    17
use std::{collections::HashSet, convert::identity};
12152
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    18
14676
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14509
diff changeset
    19
pub fn handle(
15080
e935b1ad23f3 normalize type names
alfadur
parents: 15079
diff changeset
    20
    server: &mut HwServer,
14676
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14509
diff changeset
    21
    client_id: ClientId,
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14509
diff changeset
    22
    response: &mut super::Response,
15080
e935b1ad23f3 normalize type names
alfadur
parents: 15079
diff changeset
    23
    message: HwProtocolMessage,
14676
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14509
diff changeset
    24
) {
15080
e935b1ad23f3 normalize type names
alfadur
parents: 15079
diff changeset
    25
    use crate::protocol::messages::HwProtocolMessage::*;
12152
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    26
    match message {
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    27
        CreateRoom(name, password) => {
14677
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14676
diff changeset
    28
            if is_name_illegal(&name) {
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14676
diff changeset
    29
                response.add(Warning("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()).send_self());
14462
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
    30
            } else if server.has_room(&name) {
14677
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14676
diff changeset
    31
                response.add(
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14676
diff changeset
    32
                    Warning("A room with the same name already exists.".to_string()).send_self(),
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14676
diff changeset
    33
                );
14462
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
    34
            } else {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
    35
                let flags_msg = ClientFlags(
14787
50fcef24003f add typed protocol flags
alfadur
parents: 14699
diff changeset
    36
                    add_flags(&[Flags::RoomMaster, Flags::Ready]),
14462
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
    37
                    vec![server.clients[client_id].nick.clone()],
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
    38
                );
14509
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14462
diff changeset
    39
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14462
diff changeset
    40
                let room_id = server.create_room(client_id, name, password);
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14462
diff changeset
    41
                let room = &server.rooms[room_id];
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14462
diff changeset
    42
                let client = &server.clients[client_id];
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14462
diff changeset
    43
14676
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14509
diff changeset
    44
                response.add(
14509
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14462
diff changeset
    45
                    RoomAdd(room.info(Some(&client)))
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14462
diff changeset
    46
                        .send_all()
14676
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14509
diff changeset
    47
                        .with_protocol(room.protocol_number),
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14509
diff changeset
    48
                );
14802
ce2268ae261f add missing room joined message
alfadur
parents: 14794
diff changeset
    49
                response.add(RoomJoined(vec![client.nick.clone()]).send_self());
14676
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14509
diff changeset
    50
                response.add(flags_msg.send_self());
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14509
diff changeset
    51
14787
50fcef24003f add typed protocol flags
alfadur
parents: 14699
diff changeset
    52
                response.add(
50fcef24003f add typed protocol flags
alfadur
parents: 14699
diff changeset
    53
                    ClientFlags(add_flags(&[Flags::InRoom]), vec![client.nick.clone()]).send_self(),
50fcef24003f add typed protocol flags
alfadur
parents: 14699
diff changeset
    54
                );
14462
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
    55
            };
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
    56
        }
13124
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12857
diff changeset
    57
        Chat(msg) => {
14677
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14676
diff changeset
    58
            response.add(
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14676
diff changeset
    59
                ChatMsg {
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14676
diff changeset
    60
                    nick: server.clients[client_id].nick.clone(),
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14676
diff changeset
    61
                    msg,
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14676
diff changeset
    62
                }
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14676
diff changeset
    63
                .send_all()
14699
25c564f77b7d remove lobby room
alfadur <mail@none>
parents: 14695
diff changeset
    64
                .in_lobby()
14677
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14676
diff changeset
    65
                .but_self(),
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14676
diff changeset
    66
            );
14462
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
    67
        }
13671
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13528
diff changeset
    68
        JoinRoom(name, _password) => {
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13528
diff changeset
    69
            let room = server.rooms.iter().find(|(_, r)| r.name == name);
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13528
diff changeset
    70
            let room_id = room.map(|(_, r)| r.id);
14790
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14788
diff changeset
    71
14691
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14677
diff changeset
    72
            let client = &mut server.clients[client_id];
13432
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13424
diff changeset
    73
14691
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14677
diff changeset
    74
            if let Some((_, room)) = room {
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14677
diff changeset
    75
                if client.protocol_number != room.protocol_number {
14677
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14676
diff changeset
    76
                    response.add(
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14676
diff changeset
    77
                        Warning("Room version incompatible to your Hedgewars version!".to_string())
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14676
diff changeset
    78
                            .send_self(),
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14676
diff changeset
    79
                    );
14691
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14677
diff changeset
    80
                } else if room.is_join_restricted() {
14677
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14676
diff changeset
    81
                    response.add(
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14676
diff changeset
    82
                        Warning(
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14676
diff changeset
    83
                            "Access denied. This room currently doesn't allow joining.".to_string(),
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14676
diff changeset
    84
                        )
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14676
diff changeset
    85
                        .send_self(),
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14676
diff changeset
    86
                    );
14691
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14677
diff changeset
    87
                } else if room.players_number == u8::max_value() {
14677
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14676
diff changeset
    88
                    response.add(Warning("This room is already full".to_string()).send_self());
14676
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14509
diff changeset
    89
                } else if let Some(room_id) = room_id {
14792
0e64acbc3f8b implement follow message
alfadur
parents: 14790
diff changeset
    90
                    super::common::enter_room(server, client_id, room_id, response);
13671
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13528
diff changeset
    91
                }
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13528
diff changeset
    92
            } else {
14677
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14676
diff changeset
    93
                response.add(Warning("No such room.".to_string()).send_self());
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14676
diff changeset
    94
            }
14462
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
    95
        }
14792
0e64acbc3f8b implement follow message
alfadur
parents: 14790
diff changeset
    96
        Follow(nick) => {
15080
e935b1ad23f3 normalize type names
alfadur
parents: 15079
diff changeset
    97
            if let Some(HwClient {
14792
0e64acbc3f8b implement follow message
alfadur
parents: 14790
diff changeset
    98
                room_id: Some(room_id),
0e64acbc3f8b implement follow message
alfadur
parents: 14790
diff changeset
    99
                ..
0e64acbc3f8b implement follow message
alfadur
parents: 14790
diff changeset
   100
            }) = server.find_client(&nick)
0e64acbc3f8b implement follow message
alfadur
parents: 14790
diff changeset
   101
            {
0e64acbc3f8b implement follow message
alfadur
parents: 14790
diff changeset
   102
                let room = &server.rooms[*room_id];
0e64acbc3f8b implement follow message
alfadur
parents: 14790
diff changeset
   103
                response.add(Joining(room.name.clone()).send_self());
0e64acbc3f8b implement follow message
alfadur
parents: 14790
diff changeset
   104
                super::common::enter_room(server, client_id, *room_id, response);
0e64acbc3f8b implement follow message
alfadur
parents: 14790
diff changeset
   105
            }
0e64acbc3f8b implement follow message
alfadur
parents: 14790
diff changeset
   106
        }
14788
b3adc030104b implement server vars
alfadur
parents: 14787
diff changeset
   107
        SetServerVar(var) => {
b3adc030104b implement server vars
alfadur
parents: 14787
diff changeset
   108
            if !server.clients[client_id].is_admin() {
b3adc030104b implement server vars
alfadur
parents: 14787
diff changeset
   109
                response.add(Warning("Access denied.".to_string()).send_self());
b3adc030104b implement server vars
alfadur
parents: 14787
diff changeset
   110
            } else {
b3adc030104b implement server vars
alfadur
parents: 14787
diff changeset
   111
                match var {
b3adc030104b implement server vars
alfadur
parents: 14787
diff changeset
   112
                    ServerVar::MOTDNew(msg) => server.greetings.for_latest_protocol = msg,
b3adc030104b implement server vars
alfadur
parents: 14787
diff changeset
   113
                    ServerVar::MOTDOld(msg) => server.greetings.for_old_protocols = msg,
b3adc030104b implement server vars
alfadur
parents: 14787
diff changeset
   114
                    ServerVar::LatestProto(n) => server.latest_protocol = n,
b3adc030104b implement server vars
alfadur
parents: 14787
diff changeset
   115
                }
b3adc030104b implement server vars
alfadur
parents: 14787
diff changeset
   116
            }
b3adc030104b implement server vars
alfadur
parents: 14787
diff changeset
   117
        }
b3adc030104b implement server vars
alfadur
parents: 14787
diff changeset
   118
        GetServerVar => {
b3adc030104b implement server vars
alfadur
parents: 14787
diff changeset
   119
            if !server.clients[client_id].is_admin() {
b3adc030104b implement server vars
alfadur
parents: 14787
diff changeset
   120
                response.add(Warning("Access denied.".to_string()).send_self());
b3adc030104b implement server vars
alfadur
parents: 14787
diff changeset
   121
            } else {
b3adc030104b implement server vars
alfadur
parents: 14787
diff changeset
   122
                let vars: Vec<_> = [
b3adc030104b implement server vars
alfadur
parents: 14787
diff changeset
   123
                    ServerVar::MOTDNew(server.greetings.for_latest_protocol.clone()),
b3adc030104b implement server vars
alfadur
parents: 14787
diff changeset
   124
                    ServerVar::MOTDOld(server.greetings.for_old_protocols.clone()),
b3adc030104b implement server vars
alfadur
parents: 14787
diff changeset
   125
                    ServerVar::LatestProto(server.latest_protocol),
b3adc030104b implement server vars
alfadur
parents: 14787
diff changeset
   126
                ]
b3adc030104b implement server vars
alfadur
parents: 14787
diff changeset
   127
                .iter()
b3adc030104b implement server vars
alfadur
parents: 14787
diff changeset
   128
                .flat_map(|v| v.to_protocol())
b3adc030104b implement server vars
alfadur
parents: 14787
diff changeset
   129
                .collect();
b3adc030104b implement server vars
alfadur
parents: 14787
diff changeset
   130
                response.add(ServerVars(vars).send_self());
b3adc030104b implement server vars
alfadur
parents: 14787
diff changeset
   131
            }
b3adc030104b implement server vars
alfadur
parents: 14787
diff changeset
   132
        }
13450
d3c86ade3d4d Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13449
diff changeset
   133
        Rnd(v) => {
14677
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14676
diff changeset
   134
            response.add(rnd_reply(&v).send_self());
14462
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
   135
        }
14794
18240b308505 implement stats message
alfadur
parents: 14792
diff changeset
   136
        Stats => {
18240b308505 implement stats message
alfadur
parents: 14792
diff changeset
   137
            let mut protocols: HashSet<_> = server
18240b308505 implement stats message
alfadur
parents: 14792
diff changeset
   138
                .clients
18240b308505 implement stats message
alfadur
parents: 14792
diff changeset
   139
                .iter()
18240b308505 implement stats message
alfadur
parents: 14792
diff changeset
   140
                .map(|(_, c)| c.protocol_number)
18240b308505 implement stats message
alfadur
parents: 14792
diff changeset
   141
                .chain(server.rooms.iter().map(|(_, r)| r.protocol_number))
18240b308505 implement stats message
alfadur
parents: 14792
diff changeset
   142
                .collect();
18240b308505 implement stats message
alfadur
parents: 14792
diff changeset
   143
            let mut protocols: Vec<_> = protocols.drain().collect();
18240b308505 implement stats message
alfadur
parents: 14792
diff changeset
   144
            protocols.sort();
18240b308505 implement stats message
alfadur
parents: 14792
diff changeset
   145
18240b308505 implement stats message
alfadur
parents: 14792
diff changeset
   146
            let mut html = Vec::with_capacity(protocols.len() + 2);
18240b308505 implement stats message
alfadur
parents: 14792
diff changeset
   147
18240b308505 implement stats message
alfadur
parents: 14792
diff changeset
   148
            html.push("<table>".to_string());
18240b308505 implement stats message
alfadur
parents: 14792
diff changeset
   149
            for protocol in protocols {
18240b308505 implement stats message
alfadur
parents: 14792
diff changeset
   150
                html.push(format!(
18240b308505 implement stats message
alfadur
parents: 14792
diff changeset
   151
                    "<tr><td>{}</td><td>{}</td><td>{}</td></tr>",
18240b308505 implement stats message
alfadur
parents: 14792
diff changeset
   152
                    super::utils::protocol_version_string(protocol),
18240b308505 implement stats message
alfadur
parents: 14792
diff changeset
   153
                    server.protocol_clients(protocol).count(),
18240b308505 implement stats message
alfadur
parents: 14792
diff changeset
   154
                    server.protocol_rooms(protocol).count()
18240b308505 implement stats message
alfadur
parents: 14792
diff changeset
   155
                ));
18240b308505 implement stats message
alfadur
parents: 14792
diff changeset
   156
            }
18240b308505 implement stats message
alfadur
parents: 14792
diff changeset
   157
            html.push("</table>".to_string());
18240b308505 implement stats message
alfadur
parents: 14792
diff changeset
   158
18240b308505 implement stats message
alfadur
parents: 14792
diff changeset
   159
            response.add(Warning(html.join("")).send_self());
18240b308505 implement stats message
alfadur
parents: 14792
diff changeset
   160
        }
13124
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12857
diff changeset
   161
        List => warn!("Deprecated LIST message received"),
12152
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
   162
        _ => warn!("Incorrect command in lobby state"),
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
   163
    }
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
   164
}