rust/hedgewars-server/src/server/handlers/lobby.rs
author alfadur
Wed, 10 Apr 2019 23:56:53 +0300
changeset 14785 a1077e8d26f4
parent 14783 b3adc030104b
child 14787 0e64acbc3f8b
permissions -rw-r--r--
implement watch message apart from replay deserializing
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::{
14782
50fcef24003f add typed protocol flags
alfadur
parents: 14694
diff changeset
     5
    protocol::messages::{
50fcef24003f add typed protocol flags
alfadur
parents: 14694
diff changeset
     6
        add_flags, remove_flags, HWProtocolMessage, HWServerMessage::*, ProtocolFlags as Flags,
50fcef24003f add typed protocol flags
alfadur
parents: 14694
diff changeset
     7
    },
14783
b3adc030104b implement server vars
alfadur
parents: 14782
diff changeset
     8
    server::{
b3adc030104b implement server vars
alfadur
parents: 14782
diff changeset
     9
        core::HWServer,
b3adc030104b implement server vars
alfadur
parents: 14782
diff changeset
    10
        coretypes::{ClientId, ServerVar},
b3adc030104b implement server vars
alfadur
parents: 14782
diff changeset
    11
    },
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    12
    utils::is_name_illegal,
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    13
};
13805
0463a4221327 cleanup crate imports
alfadur
parents: 13666
diff changeset
    14
use log::*;
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    15
14671
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14504
diff changeset
    16
pub fn handle(
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14504
diff changeset
    17
    server: &mut HWServer,
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14504
diff changeset
    18
    client_id: ClientId,
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14504
diff changeset
    19
    response: &mut super::Response,
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14504
diff changeset
    20
    message: HWProtocolMessage,
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14504
diff changeset
    21
) {
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13523
diff changeset
    22
    use crate::protocol::messages::HWProtocolMessage::*;
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    23
    match message {
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    24
        CreateRoom(name, password) => {
14672
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
    25
            if is_name_illegal(&name) {
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
    26
                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());
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    27
            } else if server.has_room(&name) {
14672
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
    28
                response.add(
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
    29
                    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: 14671
diff changeset
    30
                );
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    31
            } else {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    32
                let flags_msg = ClientFlags(
14782
50fcef24003f add typed protocol flags
alfadur
parents: 14694
diff changeset
    33
                    add_flags(&[Flags::RoomMaster, Flags::Ready]),
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    34
                    vec![server.clients[client_id].nick.clone()],
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    35
                );
14504
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14457
diff changeset
    36
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14457
diff changeset
    37
                let room_id = server.create_room(client_id, name, password);
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14457
diff changeset
    38
                let room = &server.rooms[room_id];
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14457
diff changeset
    39
                let client = &server.clients[client_id];
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14457
diff changeset
    40
14671
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14504
diff changeset
    41
                response.add(
14504
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14457
diff changeset
    42
                    RoomAdd(room.info(Some(&client)))
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14457
diff changeset
    43
                        .send_all()
14671
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14504
diff changeset
    44
                        .with_protocol(room.protocol_number),
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14504
diff changeset
    45
                );
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14504
diff changeset
    46
                response.add(flags_msg.send_self());
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14504
diff changeset
    47
14782
50fcef24003f add typed protocol flags
alfadur
parents: 14694
diff changeset
    48
                response.add(
50fcef24003f add typed protocol flags
alfadur
parents: 14694
diff changeset
    49
                    ClientFlags(add_flags(&[Flags::InRoom]), vec![client.nick.clone()]).send_self(),
50fcef24003f add typed protocol flags
alfadur
parents: 14694
diff changeset
    50
                );
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    51
            };
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    52
        }
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12852
diff changeset
    53
        Chat(msg) => {
14672
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
    54
            response.add(
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
    55
                ChatMsg {
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
    56
                    nick: server.clients[client_id].nick.clone(),
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
    57
                    msg,
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
    58
                }
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
    59
                .send_all()
14694
25c564f77b7d remove lobby room
alfadur <mail@none>
parents: 14690
diff changeset
    60
                .in_lobby()
14672
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
    61
                .but_self(),
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
    62
            );
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    63
        }
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13523
diff changeset
    64
        JoinRoom(name, _password) => {
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13523
diff changeset
    65
            let room = server.rooms.iter().find(|(_, r)| r.name == name);
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13523
diff changeset
    66
            let room_id = room.map(|(_, r)| r.id);
14785
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14783
diff changeset
    67
14686
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14672
diff changeset
    68
            let client = &mut server.clients[client_id];
13427
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13419
diff changeset
    69
14686
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14672
diff changeset
    70
            if let Some((_, room)) = room {
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14672
diff changeset
    71
                if client.protocol_number != room.protocol_number {
14672
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
    72
                    response.add(
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
    73
                        Warning("Room version incompatible to your Hedgewars version!".to_string())
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
    74
                            .send_self(),
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
    75
                    );
14686
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14672
diff changeset
    76
                } else if room.is_join_restricted() {
14672
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
    77
                    response.add(
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
    78
                        Warning(
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
    79
                            "Access denied. This room currently doesn't allow joining.".to_string(),
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
    80
                        )
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
    81
                        .send_self(),
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
    82
                    );
14686
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14672
diff changeset
    83
                } else if room.players_number == u8::max_value() {
14672
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
    84
                    response.add(Warning("This room is already full".to_string()).send_self());
14671
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14504
diff changeset
    85
                } else if let Some(room_id) = room_id {
14686
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14672
diff changeset
    86
                    let nick = client.nick.clone();
14671
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14504
diff changeset
    87
                    server.move_to_room(client_id, room_id);
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14504
diff changeset
    88
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14504
diff changeset
    89
                    response.add(RoomJoined(vec![nick.clone()]).send_all().in_room(room_id));
14782
50fcef24003f add typed protocol flags
alfadur
parents: 14694
diff changeset
    90
                    response.add(ClientFlags(add_flags(&[Flags::InRoom]), vec![nick]).send_all());
14785
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14783
diff changeset
    91
                    let nicks = server.collect_nicks(|(_, c)| c.room_id == Some(room_id));
14671
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14504
diff changeset
    92
                    response.add(RoomJoined(nicks).send_self());
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14504
diff changeset
    93
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14504
diff changeset
    94
                    let room = &server.rooms[room_id];
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14504
diff changeset
    95
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14504
diff changeset
    96
                    if !room.greeting.is_empty() {
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14504
diff changeset
    97
                        response.add(
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14504
diff changeset
    98
                            ChatMsg {
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14504
diff changeset
    99
                                nick: "[greeting]".to_string(),
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14504
diff changeset
   100
                                msg: room.greeting.clone(),
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14504
diff changeset
   101
                            }
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14504
diff changeset
   102
                            .send_self(),
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14504
diff changeset
   103
                        );
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14504
diff changeset
   104
                    }
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13523
diff changeset
   105
                }
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13523
diff changeset
   106
            } else {
14672
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
   107
                response.add(Warning("No such room.".to_string()).send_self());
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
   108
            }
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   109
        }
14783
b3adc030104b implement server vars
alfadur
parents: 14782
diff changeset
   110
        SetServerVar(var) => {
b3adc030104b implement server vars
alfadur
parents: 14782
diff changeset
   111
            if !server.clients[client_id].is_admin() {
b3adc030104b implement server vars
alfadur
parents: 14782
diff changeset
   112
                response.add(Warning("Access denied.".to_string()).send_self());
b3adc030104b implement server vars
alfadur
parents: 14782
diff changeset
   113
            } else {
b3adc030104b implement server vars
alfadur
parents: 14782
diff changeset
   114
                match var {
b3adc030104b implement server vars
alfadur
parents: 14782
diff changeset
   115
                    ServerVar::MOTDNew(msg) => server.greetings.for_latest_protocol = msg,
b3adc030104b implement server vars
alfadur
parents: 14782
diff changeset
   116
                    ServerVar::MOTDOld(msg) => server.greetings.for_old_protocols = msg,
b3adc030104b implement server vars
alfadur
parents: 14782
diff changeset
   117
                    ServerVar::LatestProto(n) => server.latest_protocol = n,
b3adc030104b implement server vars
alfadur
parents: 14782
diff changeset
   118
                }
b3adc030104b implement server vars
alfadur
parents: 14782
diff changeset
   119
            }
b3adc030104b implement server vars
alfadur
parents: 14782
diff changeset
   120
        }
b3adc030104b implement server vars
alfadur
parents: 14782
diff changeset
   121
        GetServerVar => {
b3adc030104b implement server vars
alfadur
parents: 14782
diff changeset
   122
            if !server.clients[client_id].is_admin() {
b3adc030104b implement server vars
alfadur
parents: 14782
diff changeset
   123
                response.add(Warning("Access denied.".to_string()).send_self());
b3adc030104b implement server vars
alfadur
parents: 14782
diff changeset
   124
            } else {
b3adc030104b implement server vars
alfadur
parents: 14782
diff changeset
   125
                let vars: Vec<_> = [
b3adc030104b implement server vars
alfadur
parents: 14782
diff changeset
   126
                    ServerVar::MOTDNew(server.greetings.for_latest_protocol.clone()),
b3adc030104b implement server vars
alfadur
parents: 14782
diff changeset
   127
                    ServerVar::MOTDOld(server.greetings.for_old_protocols.clone()),
b3adc030104b implement server vars
alfadur
parents: 14782
diff changeset
   128
                    ServerVar::LatestProto(server.latest_protocol),
b3adc030104b implement server vars
alfadur
parents: 14782
diff changeset
   129
                ]
b3adc030104b implement server vars
alfadur
parents: 14782
diff changeset
   130
                .iter()
b3adc030104b implement server vars
alfadur
parents: 14782
diff changeset
   131
                .flat_map(|v| v.to_protocol())
b3adc030104b implement server vars
alfadur
parents: 14782
diff changeset
   132
                .collect();
b3adc030104b implement server vars
alfadur
parents: 14782
diff changeset
   133
                response.add(ServerVars(vars).send_self());
b3adc030104b implement server vars
alfadur
parents: 14782
diff changeset
   134
            }
b3adc030104b implement server vars
alfadur
parents: 14782
diff changeset
   135
        }
13445
d3c86ade3d4d Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13444
diff changeset
   136
        Rnd(v) => {
14672
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
   137
            response.add(rnd_reply(&v).send_self());
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
   138
        }
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12852
diff changeset
   139
        List => warn!("Deprecated LIST message received"),
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
   140
        _ => warn!("Incorrect command in lobby state"),
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
   141
    }
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
   142
}