gameServer2/src/server/actions.rs
author unc0rr
Fri, 27 Jan 2017 23:03:31 +0300
changeset 12146 8d8fb85bc09c
parent 12145 a482c7a5f6e3
child 12147 03ccb89820f3
permissions -rw-r--r--
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
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;
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
     6
use protocol::messages::HWProtocolMessage;
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
     7
use protocol::messages::HWServerMessage::*;
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
     8
use super::handlers;
12138
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents:
diff changeset
     9
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents:
diff changeset
    10
pub enum Action {
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents:
diff changeset
    11
    SendMe(String),
12146
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12145
diff changeset
    12
    SendAllButMe(String),
12139
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    13
    RemoveClient,
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    14
    ByeClient(String),
12142
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12141
diff changeset
    15
    ReactProtocolMessage(HWProtocolMessage),
12145
a482c7a5f6e3 Lobby joining action
unc0rr
parents: 12144
diff changeset
    16
    CheckRegistered,
a482c7a5f6e3 Lobby joining action
unc0rr
parents: 12144
diff changeset
    17
    JoinLobby,
12138
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents:
diff changeset
    18
}
12144
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
    19
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
    20
use self::Action::*;
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
    21
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
    22
pub fn run_action(server: &mut HWServer, token: mio::Token, poll: &mio::Poll, action: Action) {
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
    23
    match action {
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
    24
        SendMe(msg) =>
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
    25
            server.send(token, &msg),
12146
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12145
diff changeset
    26
        SendAllButMe(msg) => {
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12145
diff changeset
    27
            for c in server.clients.iter_mut() {
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12145
diff changeset
    28
                if c.id != token {
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12145
diff changeset
    29
                    c.send_string(&msg)
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12145
diff changeset
    30
                }
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12145
diff changeset
    31
            }
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) => {
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
    34
            server.react(token, poll, vec![
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
    35
                SendMe(Bye(&msg).to_raw_protocol()),
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 => {
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
    40
            server.clients[token].deregister(poll);
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
    41
            server.clients.remove(token);
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
    42
        },
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
    43
        ReactProtocolMessage(msg) =>
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
    44
            handlers::handle(server, token, poll, msg),
12145
a482c7a5f6e3 Lobby joining action
unc0rr
parents: 12144
diff changeset
    45
        CheckRegistered =>
12146
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12145
diff changeset
    46
            if server.clients[token].protocol_number > 0 && server.clients[token].nick != "" {
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12145
diff changeset
    47
                server.react(token, poll, vec![
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12145
diff changeset
    48
                    JoinLobby,
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12145
diff changeset
    49
                    ]);
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12145
diff changeset
    50
            },
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12145
diff changeset
    51
        JoinLobby => {
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12145
diff changeset
    52
            let joined_msg;
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12145
diff changeset
    53
            {
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12145
diff changeset
    54
                let mut lobby_nicks: Vec<&str> = Vec::new();
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12145
diff changeset
    55
                for c in server.clients.iter() {
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12145
diff changeset
    56
                    if c.room_id.is_some() {
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12145
diff changeset
    57
                        lobby_nicks.push(&c.nick);
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12145
diff changeset
    58
                    }
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12145
diff changeset
    59
                }
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12145
diff changeset
    60
                joined_msg = LobbyJoined(&lobby_nicks).to_raw_protocol();
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12145
diff changeset
    61
            }
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12145
diff changeset
    62
            let everyone_msg = LobbyJoined(&[&server.clients[token].nick]).to_raw_protocol();
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12145
diff changeset
    63
            server.clients[token].room_id = Some(server.lobby_id);
12145
a482c7a5f6e3 Lobby joining action
unc0rr
parents: 12144
diff changeset
    64
            server.react(token, poll, vec![
12146
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12145
diff changeset
    65
                SendAllButMe(everyone_msg),
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12145
diff changeset
    66
                SendMe(joined_msg),
12145
a482c7a5f6e3 Lobby joining action
unc0rr
parents: 12144
diff changeset
    67
                ]);
a482c7a5f6e3 Lobby joining action
unc0rr
parents: 12144
diff changeset
    68
        },
12144
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
    69
        //_ => unimplemented!(),
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
    70
    }
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
    71
}