gameServer2/src/server/actions.rs
author Wuzzy <almikes@aol.com>
Sun, 16 Apr 2017 03:24:17 +0200
changeset 12272 dad24eb53873
parent 12153 8591375271b8
child 12857 bd35cb2302b3
permissions -rw-r--r--
Fix broken URL in pagenetserver
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12149
589a2d7d3dc5 More refactoring
unc0rr
parents: 12148
diff changeset
     1
use mio;
589a2d7d3dc5 More refactoring
unc0rr
parents: 12148
diff changeset
     2
use std::io::Write;
589a2d7d3dc5 More refactoring
unc0rr
parents: 12148
diff changeset
     3
use std::io;
589a2d7d3dc5 More refactoring
unc0rr
parents: 12148
diff changeset
     4
589a2d7d3dc5 More refactoring
unc0rr
parents: 12148
diff changeset
     5
use super::server::HWServer;
12152
03ccb89820f3 Room creation halfplemented
unc0rr
parents: 12151
diff changeset
     6
use super::server::HWRoom;
12149
589a2d7d3dc5 More refactoring
unc0rr
parents: 12148
diff changeset
     7
use protocol::messages::HWProtocolMessage;
589a2d7d3dc5 More refactoring
unc0rr
parents: 12148
diff changeset
     8
use protocol::messages::HWServerMessage::*;
589a2d7d3dc5 More refactoring
unc0rr
parents: 12148
diff changeset
     9
use super::handlers;
12143
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents:
diff changeset
    10
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents:
diff changeset
    11
pub enum Action {
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents:
diff changeset
    12
    SendMe(String),
12151
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12150
diff changeset
    13
    SendAllButMe(String),
12144
f3121d7dedec - Handle errors
unc0rr
parents: 12143
diff changeset
    14
    RemoveClient,
f3121d7dedec - Handle errors
unc0rr
parents: 12143
diff changeset
    15
    ByeClient(String),
12147
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12146
diff changeset
    16
    ReactProtocolMessage(HWProtocolMessage),
12150
a482c7a5f6e3 Lobby joining action
unc0rr
parents: 12149
diff changeset
    17
    CheckRegistered,
a482c7a5f6e3 Lobby joining action
unc0rr
parents: 12149
diff changeset
    18
    JoinLobby,
12152
03ccb89820f3 Room creation halfplemented
unc0rr
parents: 12151
diff changeset
    19
    AddRoom(String, Option<String>),
03ccb89820f3 Room creation halfplemented
unc0rr
parents: 12151
diff changeset
    20
    Warn(String),
12143
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents:
diff changeset
    21
}
12149
589a2d7d3dc5 More refactoring
unc0rr
parents: 12148
diff changeset
    22
589a2d7d3dc5 More refactoring
unc0rr
parents: 12148
diff changeset
    23
use self::Action::*;
589a2d7d3dc5 More refactoring
unc0rr
parents: 12148
diff changeset
    24
589a2d7d3dc5 More refactoring
unc0rr
parents: 12148
diff changeset
    25
pub fn run_action(server: &mut HWServer, token: mio::Token, poll: &mio::Poll, action: Action) {
589a2d7d3dc5 More refactoring
unc0rr
parents: 12148
diff changeset
    26
    match action {
589a2d7d3dc5 More refactoring
unc0rr
parents: 12148
diff changeset
    27
        SendMe(msg) =>
589a2d7d3dc5 More refactoring
unc0rr
parents: 12148
diff changeset
    28
            server.send(token, &msg),
12151
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12150
diff changeset
    29
        SendAllButMe(msg) => {
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12150
diff changeset
    30
            for c in server.clients.iter_mut() {
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12150
diff changeset
    31
                if c.id != token {
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12150
diff changeset
    32
                    c.send_string(&msg)
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12150
diff changeset
    33
                }
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12150
diff changeset
    34
            }
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12150
diff changeset
    35
        },
12149
589a2d7d3dc5 More refactoring
unc0rr
parents: 12148
diff changeset
    36
        ByeClient(msg) => {
589a2d7d3dc5 More refactoring
unc0rr
parents: 12148
diff changeset
    37
            server.react(token, poll, vec![
589a2d7d3dc5 More refactoring
unc0rr
parents: 12148
diff changeset
    38
                SendMe(Bye(&msg).to_raw_protocol()),
589a2d7d3dc5 More refactoring
unc0rr
parents: 12148
diff changeset
    39
                RemoveClient,
589a2d7d3dc5 More refactoring
unc0rr
parents: 12148
diff changeset
    40
                ]);
589a2d7d3dc5 More refactoring
unc0rr
parents: 12148
diff changeset
    41
        },
589a2d7d3dc5 More refactoring
unc0rr
parents: 12148
diff changeset
    42
        RemoveClient => {
589a2d7d3dc5 More refactoring
unc0rr
parents: 12148
diff changeset
    43
            server.clients[token].deregister(poll);
589a2d7d3dc5 More refactoring
unc0rr
parents: 12148
diff changeset
    44
            server.clients.remove(token);
589a2d7d3dc5 More refactoring
unc0rr
parents: 12148
diff changeset
    45
        },
589a2d7d3dc5 More refactoring
unc0rr
parents: 12148
diff changeset
    46
        ReactProtocolMessage(msg) =>
589a2d7d3dc5 More refactoring
unc0rr
parents: 12148
diff changeset
    47
            handlers::handle(server, token, poll, msg),
12150
a482c7a5f6e3 Lobby joining action
unc0rr
parents: 12149
diff changeset
    48
        CheckRegistered =>
12151
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12150
diff changeset
    49
            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: 12150
diff changeset
    50
                server.react(token, poll, vec![
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12150
diff changeset
    51
                    JoinLobby,
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12150
diff changeset
    52
                    ]);
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12150
diff changeset
    53
            },
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12150
diff changeset
    54
        JoinLobby => {
12152
03ccb89820f3 Room creation halfplemented
unc0rr
parents: 12151
diff changeset
    55
            server.clients[token].room_id = Some(server.lobby_id);
03ccb89820f3 Room creation halfplemented
unc0rr
parents: 12151
diff changeset
    56
12151
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12150
diff changeset
    57
            let joined_msg;
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12150
diff changeset
    58
            {
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12150
diff changeset
    59
                let mut lobby_nicks: Vec<&str> = Vec::new();
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12150
diff changeset
    60
                for c in server.clients.iter() {
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12150
diff changeset
    61
                    if c.room_id.is_some() {
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12150
diff changeset
    62
                        lobby_nicks.push(&c.nick);
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12150
diff changeset
    63
                    }
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12150
diff changeset
    64
                }
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12150
diff changeset
    65
                joined_msg = LobbyJoined(&lobby_nicks).to_raw_protocol();
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12150
diff changeset
    66
            }
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12150
diff changeset
    67
            let everyone_msg = LobbyJoined(&[&server.clients[token].nick]).to_raw_protocol();
12150
a482c7a5f6e3 Lobby joining action
unc0rr
parents: 12149
diff changeset
    68
            server.react(token, poll, vec![
12151
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12150
diff changeset
    69
                SendAllButMe(everyone_msg),
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12150
diff changeset
    70
                SendMe(joined_msg),
12150
a482c7a5f6e3 Lobby joining action
unc0rr
parents: 12149
diff changeset
    71
                ]);
a482c7a5f6e3 Lobby joining action
unc0rr
parents: 12149
diff changeset
    72
        },
12152
03ccb89820f3 Room creation halfplemented
unc0rr
parents: 12151
diff changeset
    73
        AddRoom(name, password) => {
03ccb89820f3 Room creation halfplemented
unc0rr
parents: 12151
diff changeset
    74
            let room_id = server.rooms.insert(HWRoom::new()).ok().expect("Cannot add room");
12153
8591375271b8 Store room protocol number
unc0rr
parents: 12152
diff changeset
    75
            {
8591375271b8 Store room protocol number
unc0rr
parents: 12152
diff changeset
    76
                let r = &mut server.rooms[room_id];
8591375271b8 Store room protocol number
unc0rr
parents: 12152
diff changeset
    77
                let c = &mut server.clients[token];
8591375271b8 Store room protocol number
unc0rr
parents: 12152
diff changeset
    78
                r.name = name;
8591375271b8 Store room protocol number
unc0rr
parents: 12152
diff changeset
    79
                r.password = password;
8591375271b8 Store room protocol number
unc0rr
parents: 12152
diff changeset
    80
                r.id = room_id.clone();
8591375271b8 Store room protocol number
unc0rr
parents: 12152
diff changeset
    81
                r.ready_players_number = 1;
8591375271b8 Store room protocol number
unc0rr
parents: 12152
diff changeset
    82
                r.protocol_number = c.protocol_number;
8591375271b8 Store room protocol number
unc0rr
parents: 12152
diff changeset
    83
                c.room_id = Some(room_id);
8591375271b8 Store room protocol number
unc0rr
parents: 12152
diff changeset
    84
            }
8591375271b8 Store room protocol number
unc0rr
parents: 12152
diff changeset
    85
12152
03ccb89820f3 Room creation halfplemented
unc0rr
parents: 12151
diff changeset
    86
        },
03ccb89820f3 Room creation halfplemented
unc0rr
parents: 12151
diff changeset
    87
        Warn(msg) => {
03ccb89820f3 Room creation halfplemented
unc0rr
parents: 12151
diff changeset
    88
            run_action(server, token, poll, SendMe(Warning(&msg).to_raw_protocol()));
03ccb89820f3 Room creation halfplemented
unc0rr
parents: 12151
diff changeset
    89
        }
12149
589a2d7d3dc5 More refactoring
unc0rr
parents: 12148
diff changeset
    90
        //_ => unimplemented!(),
589a2d7d3dc5 More refactoring
unc0rr
parents: 12148
diff changeset
    91
    }
589a2d7d3dc5 More refactoring
unc0rr
parents: 12148
diff changeset
    92
}