gameServer2/src/server/actions.rs
author alfadur
Mon, 18 Jun 2018 09:22:53 -0400
changeset 13416 cdf69667593b
parent 13119 1e39b8749072
child 13419 81e0ed105f5d
permissions -rw-r--r--
partial room implementation
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
     1
use std::{
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
     2
    io, io::Write
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
     3
};
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
     4
use super::{
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
     5
    server::HWServer,
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
     6
    client::ClientId,
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
     7
    room::HWRoom,
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
     8
    handlers
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
     9
};
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    10
use protocol::messages::{
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    11
    HWProtocolMessage,
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    12
    HWServerMessage,
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    13
    HWServerMessage::*
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    14
};
12138
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents:
diff changeset
    15
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents:
diff changeset
    16
pub enum Action {
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    17
    SendAll(HWServerMessage),
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12853
diff changeset
    18
    SendMe(HWServerMessage),
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12853
diff changeset
    19
    SendAllButMe(HWServerMessage),
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    20
    SendToSelected(Vec<ClientId>, HWServerMessage),
12139
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    21
    RemoveClient,
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    22
    ByeClient(String),
12142
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12141
diff changeset
    23
    ReactProtocolMessage(HWProtocolMessage),
12145
a482c7a5f6e3 Lobby joining action
unc0rr
parents: 12144
diff changeset
    24
    CheckRegistered,
a482c7a5f6e3 Lobby joining action
unc0rr
parents: 12144
diff changeset
    25
    JoinLobby,
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents: 12146
diff changeset
    26
    AddRoom(String, Option<String>),
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    27
    RemoveRoom(RoomId),
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    28
    MoveToRoom(RoomId),
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    29
    MoveToLobby(String),
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    30
    ChangeMaster(RoomId, Option<ClientId>),
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    31
    SendRoomUpdate(Option<String>),
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents: 12146
diff changeset
    32
    Warn(String),
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    33
    ProtocolError(String)
12138
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents:
diff changeset
    34
}
12144
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
    35
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
    36
use self::Action::*;
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    37
use server::room::RoomId;
12144
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
    38
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12853
diff changeset
    39
pub fn run_action(server: &mut HWServer, token: usize, action: Action) {
12144
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
    40
    match action {
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    41
        SendAll(msg) =>
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    42
            server.send_all(msg),
12144
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
    43
        SendMe(msg) =>
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12853
diff changeset
    44
            server.send_self(token, msg),
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    45
        SendAllButMe(msg) =>
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    46
            server.send_others(token, msg),
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    47
        SendToSelected(client_ids, msg) =>
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    48
            server.send_to_selected(client_ids, msg),
12144
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
    49
        ByeClient(msg) => {
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    50
            let room_id;
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    51
            let nick;
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    52
            {
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    53
                let c = &server.clients[token];
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    54
                room_id = c.room_id;
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    55
                nick = c.nick.clone();
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    56
            }
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    57
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    58
            let action = room_id.map (|id| {
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    59
                if id == server.lobby_id {
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    60
                    SendAll(LobbyLeft(nick, msg.clone()))
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    61
                } else {
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    62
                    MoveToLobby(format!("quit: {}", msg.clone()))
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    63
                }
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    64
            });
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    65
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    66
            if let Some(action) = action {
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    67
                server.react(token, vec![action]);
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    68
            }
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    69
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12853
diff changeset
    70
            server.react(token, vec![
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12853
diff changeset
    71
                SendMe(Bye(msg)),
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    72
                RemoveClient]);
12144
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
    73
        },
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
    74
        RemoveClient => {
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12853
diff changeset
    75
            server.removed_clients.push(token);
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12853
diff changeset
    76
            if server.clients.contains(token) {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12853
diff changeset
    77
                server.clients.remove(token);
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12853
diff changeset
    78
            }
12144
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
    79
        },
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
    80
        ReactProtocolMessage(msg) =>
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12853
diff changeset
    81
            handlers::handle(server, token, msg),
12145
a482c7a5f6e3 Lobby joining action
unc0rr
parents: 12144
diff changeset
    82
        CheckRegistered =>
12146
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12145
diff changeset
    83
            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
    84
                server.react(token, vec![
12146
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12145
diff changeset
    85
                    JoinLobby,
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12145
diff changeset
    86
                    ]);
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12145
diff changeset
    87
            },
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12145
diff changeset
    88
        JoinLobby => {
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents: 12146
diff changeset
    89
            server.clients[token].room_id = Some(server.lobby_id);
03ccb89820f3 Room creation halfplemented
unc0rr
parents: 12146
diff changeset
    90
12146
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12145
diff changeset
    91
            let joined_msg;
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12145
diff changeset
    92
            {
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12853
diff changeset
    93
                let mut lobby_nicks = Vec::new();
12852
bd35cb2302b3 Quick dirty fix for building
unc0rr
parents: 12148
diff changeset
    94
                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
    95
                    if c.room_id.is_some() {
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12853
diff changeset
    96
                        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
    97
                    }
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12145
diff changeset
    98
                }
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12853
diff changeset
    99
                joined_msg = LobbyJoined(lobby_nicks);
12146
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12145
diff changeset
   100
            }
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12853
diff changeset
   101
            let everyone_msg = LobbyJoined(vec![server.clients[token].nick.clone()]);
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   102
            let flags_msg = ClientFlags(
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   103
                "+i".to_string(),
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   104
                server.clients.iter()
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   105
                    .filter(|(_, c)| c.room_id.is_some())
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   106
                    .map(|(_, c)| c.nick.clone())
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   107
                    .collect());
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   108
            let server_msg = ServerMessage("\u{1f994} is watching".to_string());
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   109
            let rooms_msg = Rooms(server.rooms.iter()
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   110
                .filter(|(id, _)| *id != server.lobby_id)
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   111
                .flat_map(|(_, r)|
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   112
                    r.info(r.master_id.map(|id| &server.clients[id])))
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   113
                .collect());
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12853
diff changeset
   114
            server.react(token, vec![
12146
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12145
diff changeset
   115
                SendAllButMe(everyone_msg),
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12145
diff changeset
   116
                SendMe(joined_msg),
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   117
                SendMe(flags_msg),
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   118
                SendMe(server_msg),
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   119
                SendMe(rooms_msg),
12145
a482c7a5f6e3 Lobby joining action
unc0rr
parents: 12144
diff changeset
   120
                ]);
a482c7a5f6e3 Lobby joining action
unc0rr
parents: 12144
diff changeset
   121
        },
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents: 12146
diff changeset
   122
        AddRoom(name, password) => {
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   123
            let room_protocol;
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   124
            let room_info;
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12853
diff changeset
   125
            let room_id = server.add_room();;
12148
8591375271b8 Store room protocol number
unc0rr
parents: 12147
diff changeset
   126
            {
8591375271b8 Store room protocol number
unc0rr
parents: 12147
diff changeset
   127
                let r = &mut server.rooms[room_id];
8591375271b8 Store room protocol number
unc0rr
parents: 12147
diff changeset
   128
                let c = &mut server.clients[token];
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   129
                r.master_id = Some(c.id);
12148
8591375271b8 Store room protocol number
unc0rr
parents: 12147
diff changeset
   130
                r.name = name;
8591375271b8 Store room protocol number
unc0rr
parents: 12147
diff changeset
   131
                r.password = password;
8591375271b8 Store room protocol number
unc0rr
parents: 12147
diff changeset
   132
                r.protocol_number = c.protocol_number;
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   133
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   134
                room_protocol = r.protocol_number;
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   135
                room_info = r.info(Some(&c));
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   136
            }
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   137
            let protocol_client_ids = server.protocol_clients(room_protocol);
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   138
            server.react(token, vec![
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   139
                SendToSelected(protocol_client_ids, RoomAdd(room_info)),
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   140
                MoveToRoom(room_id)]);
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   141
        },
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   142
        RemoveRoom(room_id) => {
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   143
            let room_protocol;
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   144
            let room_name;
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   145
            {
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   146
                let r = &mut server.rooms[room_id];
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   147
                room_protocol = r.protocol_number;
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   148
                room_name = r.name.clone();
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   149
            }
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   150
            server.rooms.remove(room_id);
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   151
            let protocol_client_ids = server.protocol_clients(room_protocol);
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   152
            server.react(token, vec![
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   153
                SendToSelected(protocol_client_ids, RoomRemove(room_name))]);
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   154
        }
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   155
        MoveToRoom(room_id) => {
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   156
            let flags_msg;
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   157
            let nick;
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   158
            {
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   159
                let r = &mut server.rooms[room_id];
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   160
                let c = &mut server.clients[token];
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   161
                r.players_number += 1;
12148
8591375271b8 Store room protocol number
unc0rr
parents: 12147
diff changeset
   162
                c.room_id = Some(room_id);
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   163
                c.is_joined_mid_game = false;
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   164
                if r.master_id == Some(c.id) {
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   165
                    r.ready_players_number += 1;
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   166
                    c.is_master = true;
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   167
                    c.is_ready = true;
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   168
                } else {
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   169
                    c.is_ready = false;
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   170
                    c.is_master = false;
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   171
                }
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   172
                flags_msg = ClientFlags("+i".to_string(), vec![c.nick.clone()]);
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   173
                nick = c.nick.clone();
12148
8591375271b8 Store room protocol number
unc0rr
parents: 12147
diff changeset
   174
            }
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   175
            let rooms_client_ids = server.room_clients(room_id);
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   176
            server.react(token, vec![
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   177
                SendToSelected(rooms_client_ids, RoomJoined(vec![nick])),
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   178
                SendAll(flags_msg),
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   179
                SendRoomUpdate(None)]);
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents: 12146
diff changeset
   180
        },
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   181
        MoveToLobby(msg) => {
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   182
            let mut actions = Vec::new();
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   183
            let other_client_ids = server.other_clients_in_room(token);
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   184
            let lobby_id = server.lobby_id;
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   185
            if let (c, Some(r)) = server.client_and_room(token) {
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   186
                r.players_number -= 1;
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   187
                if c.is_ready {
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   188
                    r.ready_players_number -= 1;
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   189
                }
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   190
                if r.players_number > 0 && c.is_master {
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   191
                    actions.push(ChangeMaster(r.id, None));
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   192
                }
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   193
                actions.push(SendToSelected(other_client_ids, RoomLeft(c.nick.clone(), msg)));
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   194
                actions.push(SendAll(ClientFlags("-i".to_string(), vec![c.nick.clone()])));
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   195
                actions.push(SendRoomUpdate(Some(r.name.clone())));
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   196
            }
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   197
            server.react(token, actions);
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   198
            actions = Vec::new();
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   199
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   200
            if let (c, Some(r)) = server.client_and_room(token) {
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   201
                c.room_id = Some(lobby_id);
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   202
                if r.players_number == 0 {
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   203
                    actions.push(RemoveRoom(r.id));
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   204
                }
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   205
            }
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   206
            server.react(token, actions)
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   207
        }
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   208
        ChangeMaster(room_id, new_id) => {
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   209
            let mut actions = Vec::new();
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   210
            let room_client_ids = server.room_clients(room_id);
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   211
            let new_id = new_id.or_else(||
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   212
                room_client_ids.iter().find(|id| **id != token).map(|id| *id));
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   213
            let new_nick = new_id.map(|id| server.clients[id].nick.clone());
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   214
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   215
            if let (c, Some(r)) = server.client_and_room(token) {
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   216
                if let Some(id) = r.master_id {
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   217
                    c.is_master = false;
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   218
                    r.master_id = None;
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   219
                    actions.push(SendToSelected(room_client_ids.clone(), ClientFlags("-h".to_string(), vec![c.nick.clone()])));
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   220
                }
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   221
                r.master_id = new_id;
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   222
                if let Some(nick) = new_nick {
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   223
                    actions.push(SendToSelected(room_client_ids, ClientFlags("+h".to_string(), vec![nick])));
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   224
                }
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   225
            }
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   226
            new_id.map(|id| server.clients[id].is_master = true);
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   227
            server.react(token, actions);
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   228
        }
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   229
        SendRoomUpdate(old_name) => {
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   230
            let room_data =
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   231
                if let (c, Some(r)) = server.client_and_room(token) {
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   232
                    let name = old_name.unwrap_or_else(|| r.name.clone());
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   233
                    Some((name, r.protocol_number, r.info(Some(&c))))
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   234
                } else {
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   235
                    None
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   236
                };
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   237
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   238
            if let Some((room_name, protocol, room_info)) = room_data {
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   239
                let protocol_clients = server.protocol_clients(protocol);
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   240
                server.react(token,
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   241
                             vec![SendToSelected(protocol_clients, RoomUpdated(room_name, room_info))]);
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   242
            }
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   243
        }
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   244
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents: 12146
diff changeset
   245
        Warn(msg) => {
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12853
diff changeset
   246
            run_action(server, token,SendMe(Warning(msg)));
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents: 12146
diff changeset
   247
        }
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   248
        ProtocolError(msg) => {
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   249
            run_action(server, token, SendMe(Error(msg)))
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   250
        }
12144
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
   251
    }
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
   252
}