gameServer2/src/server/actions.rs
author unc0rr
Thu, 26 Jan 2017 14:45:44 +0300
changeset 12145 a482c7a5f6e3
parent 12144 589a2d7d3dc5
child 12146 8d8fb85bc09c
permissions -rw-r--r--
Lobby joining action
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),
12139
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    12
    RemoveClient,
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    13
    ByeClient(String),
12142
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12141
diff changeset
    14
    ReactProtocolMessage(HWProtocolMessage),
12145
a482c7a5f6e3 Lobby joining action
unc0rr
parents: 12144
diff changeset
    15
    CheckRegistered,
a482c7a5f6e3 Lobby joining action
unc0rr
parents: 12144
diff changeset
    16
    JoinLobby,
12138
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents:
diff changeset
    17
}
12144
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
    18
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
    19
use self::Action::*;
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
    20
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
    21
pub fn run_action(server: &mut HWServer, token: mio::Token, poll: &mio::Poll, action: Action) {
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
    22
    match action {
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
    23
        SendMe(msg) =>
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
    24
            server.send(token, &msg),
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
    25
        ByeClient(msg) => {
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
    26
            server.react(token, poll, vec![
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
    27
                SendMe(Bye(&msg).to_raw_protocol()),
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
    28
                RemoveClient,
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
    29
                ]);
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
    30
        },
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
    31
        RemoveClient => {
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
    32
            server.clients[token].deregister(poll);
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
    33
            server.clients.remove(token);
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
    34
        },
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
    35
        ReactProtocolMessage(msg) =>
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
    36
            handlers::handle(server, token, poll, msg),
12145
a482c7a5f6e3 Lobby joining action
unc0rr
parents: 12144
diff changeset
    37
        CheckRegistered =>
a482c7a5f6e3 Lobby joining action
unc0rr
parents: 12144
diff changeset
    38
            if server.clients[token].protocolNumber > 0 && server.clients[token].nick != "" {
a482c7a5f6e3 Lobby joining action
unc0rr
parents: 12144
diff changeset
    39
            server.react(token, poll, vec![
a482c7a5f6e3 Lobby joining action
unc0rr
parents: 12144
diff changeset
    40
                JoinLobby,
a482c7a5f6e3 Lobby joining action
unc0rr
parents: 12144
diff changeset
    41
                ]);
a482c7a5f6e3 Lobby joining action
unc0rr
parents: 12144
diff changeset
    42
        },
a482c7a5f6e3 Lobby joining action
unc0rr
parents: 12144
diff changeset
    43
        JoinLobby => {
a482c7a5f6e3 Lobby joining action
unc0rr
parents: 12144
diff changeset
    44
            let msg_string = LobbyJoined(&[&server.clients[token].nick]).to_raw_protocol();
a482c7a5f6e3 Lobby joining action
unc0rr
parents: 12144
diff changeset
    45
            server.react(token, poll, vec![
a482c7a5f6e3 Lobby joining action
unc0rr
parents: 12144
diff changeset
    46
                SendMe(msg_string),
a482c7a5f6e3 Lobby joining action
unc0rr
parents: 12144
diff changeset
    47
                ]);
a482c7a5f6e3 Lobby joining action
unc0rr
parents: 12144
diff changeset
    48
        },
12144
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
    49
        //_ => unimplemented!(),
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
    50
    }
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
    51
}