gameServer2/src/server/actions.rs
author Wuzzy <Wuzzy2@mail.ru>
Wed, 14 Mar 2018 23:02:11 +0100
changeset 13210 25ebc1ac0967
parent 13119 1e39b8749072
child 13416 cdf69667593b
permissions -rw-r--r--
Update frontend translations
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12144
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
     1
use mio;
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
     2
use std::io::Write;
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
     3
use std::io;
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
     4
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
     5
use super::server::HWServer;
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12853
diff changeset
     6
use super::room::HWRoom;
12144
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
     7
use protocol::messages::HWProtocolMessage;
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12853
diff changeset
     8
use protocol::messages::HWServerMessage;
12144
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
     9
use protocol::messages::HWServerMessage::*;
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
    10
use super::handlers;
12138
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents:
diff changeset
    11
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents:
diff changeset
    12
pub enum Action {
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12853
diff changeset
    13
    SendMe(HWServerMessage),
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12853
diff changeset
    14
    SendAllButMe(HWServerMessage),
12139
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    15
    RemoveClient,
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    16
    ByeClient(String),
12142
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12141
diff changeset
    17
    ReactProtocolMessage(HWProtocolMessage),
12145
a482c7a5f6e3 Lobby joining action
unc0rr
parents: 12144
diff changeset
    18
    CheckRegistered,
a482c7a5f6e3 Lobby joining action
unc0rr
parents: 12144
diff changeset
    19
    JoinLobby,
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents: 12146
diff changeset
    20
    AddRoom(String, Option<String>),
03ccb89820f3 Room creation halfplemented
unc0rr
parents: 12146
diff changeset
    21
    Warn(String),
12138
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents:
diff changeset
    22
}
12144
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
    23
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
    24
use self::Action::*;
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
    25
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12853
diff changeset
    26
pub fn run_action(server: &mut HWServer, token: usize, action: Action) {
12144
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
    27
    match action {
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
    28
        SendMe(msg) =>
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12853
diff changeset
    29
            server.send_self(token, msg),
12146
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12145
diff changeset
    30
        SendAllButMe(msg) => {
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12853
diff changeset
    31
            server.send_others(token, msg)
12146
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12145
diff changeset
    32
        },
12144
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
    33
        ByeClient(msg) => {
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12853
diff changeset
    34
            server.react(token, vec![
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12853
diff changeset
    35
                SendMe(Bye(msg)),
12144
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
    36
                RemoveClient,
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
    37
                ]);
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
    38
        },
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
    39
        RemoveClient => {
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12853
diff changeset
    40
            server.removed_clients.push(token);
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12853
diff changeset
    41
            if server.clients.contains(token) {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12853
diff changeset
    42
                server.clients.remove(token);
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12853
diff changeset
    43
            }
12144
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
    44
        },
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
    45
        ReactProtocolMessage(msg) =>
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12853
diff changeset
    46
            handlers::handle(server, token, msg),
12145
a482c7a5f6e3 Lobby joining action
unc0rr
parents: 12144
diff changeset
    47
        CheckRegistered =>
12146
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12145
diff changeset
    48
            if server.clients[token].protocol_number > 0 && server.clients[token].nick != "" {
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12853
diff changeset
    49
                server.react(token, vec![
12146
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12145
diff changeset
    50
                    JoinLobby,
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12145
diff changeset
    51
                    ]);
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12145
diff changeset
    52
            },
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12145
diff changeset
    53
        JoinLobby => {
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents: 12146
diff changeset
    54
            server.clients[token].room_id = Some(server.lobby_id);
03ccb89820f3 Room creation halfplemented
unc0rr
parents: 12146
diff changeset
    55
12146
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12145
diff changeset
    56
            let joined_msg;
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12145
diff changeset
    57
            {
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12853
diff changeset
    58
                let mut lobby_nicks = Vec::new();
12852
bd35cb2302b3 Quick dirty fix for building
unc0rr
parents: 12148
diff changeset
    59
                for (_, c) in server.clients.iter() {
12146
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12145
diff changeset
    60
                    if c.room_id.is_some() {
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12853
diff changeset
    61
                        lobby_nicks.push(c.nick.clone());
12146
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12145
diff changeset
    62
                    }
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12145
diff changeset
    63
                }
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12853
diff changeset
    64
                joined_msg = LobbyJoined(lobby_nicks);
12146
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12145
diff changeset
    65
            }
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12853
diff changeset
    66
            let everyone_msg = LobbyJoined(vec![server.clients[token].nick.clone()]);
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12853
diff changeset
    67
            server.react(token, vec![
12146
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12145
diff changeset
    68
                SendAllButMe(everyone_msg),
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12145
diff changeset
    69
                SendMe(joined_msg),
12145
a482c7a5f6e3 Lobby joining action
unc0rr
parents: 12144
diff changeset
    70
                ]);
a482c7a5f6e3 Lobby joining action
unc0rr
parents: 12144
diff changeset
    71
        },
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents: 12146
diff changeset
    72
        AddRoom(name, password) => {
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12853
diff changeset
    73
            let room_id = server.add_room();;
12148
8591375271b8 Store room protocol number
unc0rr
parents: 12147
diff changeset
    74
            {
8591375271b8 Store room protocol number
unc0rr
parents: 12147
diff changeset
    75
                let r = &mut server.rooms[room_id];
8591375271b8 Store room protocol number
unc0rr
parents: 12147
diff changeset
    76
                let c = &mut server.clients[token];
8591375271b8 Store room protocol number
unc0rr
parents: 12147
diff changeset
    77
                r.name = name;
8591375271b8 Store room protocol number
unc0rr
parents: 12147
diff changeset
    78
                r.password = password;
8591375271b8 Store room protocol number
unc0rr
parents: 12147
diff changeset
    79
                r.ready_players_number = 1;
8591375271b8 Store room protocol number
unc0rr
parents: 12147
diff changeset
    80
                r.protocol_number = c.protocol_number;
8591375271b8 Store room protocol number
unc0rr
parents: 12147
diff changeset
    81
                c.room_id = Some(room_id);
8591375271b8 Store room protocol number
unc0rr
parents: 12147
diff changeset
    82
            }
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents: 12146
diff changeset
    83
        },
03ccb89820f3 Room creation halfplemented
unc0rr
parents: 12146
diff changeset
    84
        Warn(msg) => {
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12853
diff changeset
    85
            run_action(server, token,SendMe(Warning(msg)));
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents: 12146
diff changeset
    86
        }
12144
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
    87
        //_ => unimplemented!(),
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
    88
    }
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
    89
}