rust/hedgewars-server/src/handlers/inlobby.rs
author alfadur
Mon, 07 Oct 2019 23:53:47 +0300
changeset 15444 c03b2e263488
parent 15439 a158ff8f84ef
child 15482 4cc9ec732392
permissions -rw-r--r--
add smth
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
15439
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15075
diff changeset
     3
use super::{common::rnd_reply, strings::*};
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13523
diff changeset
     4
use crate::{
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
     5
    core::{
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
     6
        client::HwClient,
15439
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15075
diff changeset
     7
        server::{AccessError, CreateRoomError, HwServer, JoinRoomError},
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
     8
        types::{ClientId, ServerVar},
14782
50fcef24003f add typed protocol flags
alfadur
parents: 14694
diff changeset
     9
    },
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
    10
    protocol::messages::{
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
    11
        add_flags, remove_flags, server_chat, HwProtocolMessage, HwServerMessage::*,
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
    12
        ProtocolFlags as Flags,
14783
b3adc030104b implement server vars
alfadur
parents: 14782
diff changeset
    13
    },
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    14
    utils::is_name_illegal,
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    15
};
13805
0463a4221327 cleanup crate imports
alfadur
parents: 13666
diff changeset
    16
use log::*;
14789
18240b308505 implement stats message
alfadur
parents: 14787
diff changeset
    17
use std::{collections::HashSet, convert::identity};
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    18
14671
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14504
diff changeset
    19
pub fn handle(
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
    20
    server: &mut HwServer,
14671
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14504
diff changeset
    21
    client_id: ClientId,
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14504
diff changeset
    22
    response: &mut super::Response,
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
    23
    message: HwProtocolMessage,
14671
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14504
diff changeset
    24
) {
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
    25
    use crate::protocol::messages::HwProtocolMessage::*;
15439
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15075
diff changeset
    26
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    27
    match message {
15439
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15075
diff changeset
    28
        CreateRoom(name, password) => match server.create_room(client_id, name, password) {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15075
diff changeset
    29
            Err(CreateRoomError::InvalidName) => response.warn(ILLEGAL_ROOM_NAME),
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15075
diff changeset
    30
            Err(CreateRoomError::AlreadyExists) => response.warn(ROOM_EXISTS),
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15075
diff changeset
    31
            Ok((client, room)) => {
14671
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14504
diff changeset
    32
                response.add(
14504
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14457
diff changeset
    33
                    RoomAdd(room.info(Some(&client)))
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14457
diff changeset
    34
                        .send_all()
14671
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14504
diff changeset
    35
                        .with_protocol(room.protocol_number),
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14504
diff changeset
    36
                );
14797
ce2268ae261f add missing room joined message
alfadur
parents: 14789
diff changeset
    37
                response.add(RoomJoined(vec![client.nick.clone()]).send_self());
15439
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15075
diff changeset
    38
                response.add(
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15075
diff changeset
    39
                    ClientFlags(
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15075
diff changeset
    40
                        add_flags(&[Flags::RoomMaster, Flags::Ready]),
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15075
diff changeset
    41
                        vec![client.nick.clone()],
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15075
diff changeset
    42
                    )
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15075
diff changeset
    43
                    .send_self(),
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15075
diff changeset
    44
                );
14782
50fcef24003f add typed protocol flags
alfadur
parents: 14694
diff changeset
    45
                response.add(
50fcef24003f add typed protocol flags
alfadur
parents: 14694
diff changeset
    46
                    ClientFlags(add_flags(&[Flags::InRoom]), vec![client.nick.clone()]).send_self(),
50fcef24003f add typed protocol flags
alfadur
parents: 14694
diff changeset
    47
                );
15439
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15075
diff changeset
    48
            }
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15075
diff changeset
    49
        },
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12852
diff changeset
    50
        Chat(msg) => {
14672
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
    51
            response.add(
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
    52
                ChatMsg {
15439
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15075
diff changeset
    53
                    nick: server.get_client_nick(client_id).to_string(),
14672
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
    54
                    msg,
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
    55
                }
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
    56
                .send_all()
14694
25c564f77b7d remove lobby room
alfadur <mail@none>
parents: 14690
diff changeset
    57
                .in_lobby()
14672
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
    58
                .but_self(),
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
    59
            );
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    60
        }
15439
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15075
diff changeset
    61
        JoinRoom(name, _password) => match server.join_room_by_name(client_id, &name) {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15075
diff changeset
    62
            Err(error) => super::common::get_room_join_error(error, response),
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15075
diff changeset
    63
            Ok((client, room, room_clients)) => {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15075
diff changeset
    64
                super::common::get_room_join_data(client, room, room_clients, response)
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15075
diff changeset
    65
            }
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15075
diff changeset
    66
        },
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15075
diff changeset
    67
        Follow(nick) => {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15075
diff changeset
    68
            if let Some(client) = server.find_client(&nick) {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15075
diff changeset
    69
                if let Some(room_id) = client.room_id {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15075
diff changeset
    70
                    match server.join_room(client_id, room_id) {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15075
diff changeset
    71
                        Err(error) => super::common::get_room_join_error(error, response),
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15075
diff changeset
    72
                        Ok((client, room, room_clients)) => {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15075
diff changeset
    73
                            super::common::get_room_join_data(client, room, room_clients, response)
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15075
diff changeset
    74
                        }
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15075
diff changeset
    75
                    }
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15075
diff changeset
    76
                } else {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15075
diff changeset
    77
                    response.warn(NO_ROOM);
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13523
diff changeset
    78
                }
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13523
diff changeset
    79
            } else {
15439
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15075
diff changeset
    80
                response.warn(NO_USER);
14672
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
    81
            }
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    82
        }
15439
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15075
diff changeset
    83
        SetServerVar(var) => match server.set_var(client_id, var) {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15075
diff changeset
    84
            Err(AccessError()) => response.warn(ACCESS_DENIED),
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15075
diff changeset
    85
            Ok(()) => response.add(server_chat(VARIABLE_UPDATED.to_string()).send_self()),
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15075
diff changeset
    86
        },
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15075
diff changeset
    87
        GetServerVar => match server.get_vars(client_id) {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15075
diff changeset
    88
            Err(AccessError()) => response.warn(ACCESS_DENIED),
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15075
diff changeset
    89
            Ok(vars) => {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15075
diff changeset
    90
                response.add(
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15075
diff changeset
    91
                    ServerVars(vars.iter().flat_map(|v| v.to_protocol()).collect()).send_self(),
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15075
diff changeset
    92
                );
14787
0e64acbc3f8b implement follow message
alfadur
parents: 14785
diff changeset
    93
            }
15439
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15075
diff changeset
    94
        },
13445
d3c86ade3d4d Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13444
diff changeset
    95
        Rnd(v) => {
14672
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14671
diff changeset
    96
            response.add(rnd_reply(&v).send_self());
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    97
        }
15439
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15075
diff changeset
    98
        Stats => match server.get_used_protocols(client_id) {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15075
diff changeset
    99
            Err(AccessError()) => response.warn(ACCESS_DENIED),
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15075
diff changeset
   100
            Ok(protocols) => {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15075
diff changeset
   101
                let mut html = Vec::with_capacity(protocols.len() + 2);
14789
18240b308505 implement stats message
alfadur
parents: 14787
diff changeset
   102
15439
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15075
diff changeset
   103
                html.push("<table>".to_string());
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15075
diff changeset
   104
                for protocol in protocols {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15075
diff changeset
   105
                    html.push(format!(
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15075
diff changeset
   106
                        "<tr><td>{}</td><td>{}</td><td>{}</td></tr>",
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15075
diff changeset
   107
                        super::utils::protocol_version_string(protocol),
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15075
diff changeset
   108
                        server.protocol_clients(protocol).count(),
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15075
diff changeset
   109
                        server.protocol_rooms(protocol).count()
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15075
diff changeset
   110
                    ));
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15075
diff changeset
   111
                }
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15075
diff changeset
   112
                html.push("</table>".to_string());
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15075
diff changeset
   113
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15075
diff changeset
   114
                response.add(Warning(html.join("")).send_self());
14789
18240b308505 implement stats message
alfadur
parents: 14787
diff changeset
   115
            }
15439
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15075
diff changeset
   116
        },
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12852
diff changeset
   117
        List => warn!("Deprecated LIST message received"),
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
   118
        _ => warn!("Incorrect command in lobby state"),
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
   119
    }
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
   120
}