gameServer2/src/server/actions.rs
author alfadur
Thu, 21 Jun 2018 17:23:10 -0400
changeset 13419 81e0ed105f5d
parent 13416 cdf69667593b
child 13422 5fb27f94fc3b
permissions -rw-r--r--
implementation of team related messages
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,
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
     6
    room::RoomId,
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
     7
    client::{ClientId, HWClient},
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
     8
    room::HWRoom,
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
     9
    handlers
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    10
};
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    11
use protocol::messages::{
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    12
    HWProtocolMessage,
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    13
    HWServerMessage,
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    14
    HWServerMessage::*
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    15
};
12138
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents:
diff changeset
    16
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    17
pub enum Destination {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    18
    ToSelf,
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    19
        ToAll {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    20
        room_id: Option<RoomId>,
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    21
        protocol: Option<u32>,
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    22
        skip_self: bool
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    23
    }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    24
}
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    25
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    26
pub struct PendingMessage {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    27
    pub destination: Destination,
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    28
    pub message: HWServerMessage
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    29
}
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    30
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    31
impl PendingMessage {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    32
    pub fn send_self(message: HWServerMessage) -> PendingMessage {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    33
        PendingMessage{ destination: Destination::ToSelf, message }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    34
    }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    35
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    36
    pub fn send_all(message: HWServerMessage) -> PendingMessage {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    37
        let destination = Destination::ToAll {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    38
            room_id: None,
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    39
            protocol: None,
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    40
            skip_self: false,
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    41
        };
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    42
        PendingMessage{ destination, message }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    43
    }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    44
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    45
    pub fn in_room(mut self, clients_room_id: RoomId) -> PendingMessage {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    46
        if let Destination::ToAll {ref mut room_id, ..} = self.destination {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    47
            *room_id = Some(clients_room_id)
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    48
        }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    49
        self
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    50
    }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    51
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    52
    pub fn with_protocol(mut self, protocol_number: u32) -> PendingMessage {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    53
        if let Destination::ToAll {ref mut protocol, ..} = self.destination {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    54
            *protocol = Some(protocol_number)
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    55
        }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    56
        self
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    57
    }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    58
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    59
    pub fn but_self(mut self) -> PendingMessage {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    60
        if let Destination::ToAll {ref mut skip_self, ..} = self.destination {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    61
            *skip_self = true
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    62
        }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    63
        self
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    64
    }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    65
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    66
    pub fn action(self) -> Action { Send(self) }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    67
}
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    68
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    69
impl Into<Action> for PendingMessage {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    70
    fn into(self) -> Action { self.action() }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    71
}
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    72
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    73
impl HWServerMessage {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    74
    pub fn send_self(self) -> PendingMessage { PendingMessage::send_self(self) }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    75
    pub fn send_all(self) -> PendingMessage { PendingMessage::send_all(self) }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    76
}
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    77
12138
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents:
diff changeset
    78
pub enum Action {
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    79
    Send(PendingMessage),
12139
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    80
    RemoveClient,
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    81
    ByeClient(String),
12142
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12141
diff changeset
    82
    ReactProtocolMessage(HWProtocolMessage),
12145
a482c7a5f6e3 Lobby joining action
unc0rr
parents: 12144
diff changeset
    83
    CheckRegistered,
a482c7a5f6e3 Lobby joining action
unc0rr
parents: 12144
diff changeset
    84
    JoinLobby,
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents: 12146
diff changeset
    85
    AddRoom(String, Option<String>),
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    86
    RemoveRoom(RoomId),
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    87
    MoveToRoom(RoomId),
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    88
    MoveToLobby(String),
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    89
    ChangeMaster(RoomId, Option<ClientId>),
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    90
    RemoveTeam(String),
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    91
    RemoveClientTeams,
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    92
    SendRoomUpdate(Option<String>),
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents: 12146
diff changeset
    93
    Warn(String),
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    94
    ProtocolError(String)
12138
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents:
diff changeset
    95
}
12144
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
    96
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
    97
use self::Action::*;
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
    98
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    99
pub fn run_action(server: &mut HWServer, client_id: usize, action: Action) {
12144
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
   100
    match action {
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   101
        Send(msg) => server.send(client_id, msg.destination, msg.message),
12144
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
   102
        ByeClient(msg) => {
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   103
            let room_id;
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   104
            let nick;
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   105
            {
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   106
                let c = &server.clients[client_id];
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   107
                room_id = c.room_id;
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   108
                nick = c.nick.clone();
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   109
            }
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   110
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   111
            room_id.map (|id| {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   112
                if id != server.lobby_id {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   113
                    server.react(client_id, vec![
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   114
                        MoveToLobby(format!("quit: {}", msg.clone()))]);
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   115
                }
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   116
            });
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   117
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   118
            server.react(client_id, vec![
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   119
                LobbyLeft(nick, msg.clone()).send_all().action(),
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   120
                Bye(msg).send_self().action(),
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   121
                RemoveClient]);
12144
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
   122
        },
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
   123
        RemoveClient => {
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   124
            server.removed_clients.push(client_id);
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   125
            if server.clients.contains(client_id) {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   126
                server.clients.remove(client_id);
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12853
diff changeset
   127
            }
12144
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
   128
        },
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
   129
        ReactProtocolMessage(msg) =>
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   130
            handlers::handle(server, client_id, msg),
12145
a482c7a5f6e3 Lobby joining action
unc0rr
parents: 12144
diff changeset
   131
        CheckRegistered =>
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   132
            if server.clients[client_id].protocol_number > 0 && server.clients[client_id].nick != "" {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   133
                server.react(client_id, vec![
12146
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12145
diff changeset
   134
                    JoinLobby,
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12145
diff changeset
   135
                    ]);
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12145
diff changeset
   136
            },
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12145
diff changeset
   137
        JoinLobby => {
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   138
            server.clients[client_id].room_id = Some(server.lobby_id);
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents: 12146
diff changeset
   139
12146
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12145
diff changeset
   140
            let joined_msg;
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12145
diff changeset
   141
            {
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12853
diff changeset
   142
                let mut lobby_nicks = Vec::new();
12852
bd35cb2302b3 Quick dirty fix for building
unc0rr
parents: 12148
diff changeset
   143
                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
   144
                    if c.room_id.is_some() {
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12853
diff changeset
   145
                        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
   146
                    }
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12145
diff changeset
   147
                }
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12853
diff changeset
   148
                joined_msg = LobbyJoined(lobby_nicks);
12146
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12145
diff changeset
   149
            }
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   150
            let everyone_msg = LobbyJoined(vec![server.clients[client_id].nick.clone()]);
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   151
            let flags_msg = ClientFlags(
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   152
                "+i".to_string(),
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   153
                server.clients.iter()
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   154
                    .filter(|(_, c)| c.room_id.is_some())
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   155
                    .map(|(_, c)| c.nick.clone())
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   156
                    .collect());
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   157
            let server_msg = ServerMessage("\u{1f994} is watching".to_string());
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   158
            let rooms_msg = Rooms(server.rooms.iter()
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   159
                .filter(|(id, _)| *id != server.lobby_id)
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   160
                .flat_map(|(_, r)|
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   161
                    r.info(r.master_id.map(|id| &server.clients[id])))
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   162
                .collect());
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   163
            server.react(client_id, vec![
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   164
                everyone_msg.send_all().but_self().action(),
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   165
                joined_msg.send_self().action(),
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   166
                flags_msg.send_self().action(),
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   167
                server_msg.send_self().action(),
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   168
                rooms_msg.send_self().action(),
12145
a482c7a5f6e3 Lobby joining action
unc0rr
parents: 12144
diff changeset
   169
                ]);
a482c7a5f6e3 Lobby joining action
unc0rr
parents: 12144
diff changeset
   170
        },
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents: 12146
diff changeset
   171
        AddRoom(name, password) => {
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12853
diff changeset
   172
            let room_id = server.add_room();;
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   173
            let actions = {
12148
8591375271b8 Store room protocol number
unc0rr
parents: 12147
diff changeset
   174
                let r = &mut server.rooms[room_id];
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   175
                let c = &mut server.clients[client_id];
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   176
                r.master_id = Some(c.id);
12148
8591375271b8 Store room protocol number
unc0rr
parents: 12147
diff changeset
   177
                r.name = name;
8591375271b8 Store room protocol number
unc0rr
parents: 12147
diff changeset
   178
                r.password = password;
8591375271b8 Store room protocol number
unc0rr
parents: 12147
diff changeset
   179
                r.protocol_number = c.protocol_number;
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   180
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   181
                vec![
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   182
                    RoomAdd(r.info(Some(&c))).send_all()
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   183
                        .with_protocol(r.protocol_number).action(),
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   184
                    MoveToRoom(room_id)]
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   185
            };
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   186
            server.react(client_id, actions);
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   187
        },
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   188
        RemoveRoom(room_id) => {
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   189
            let actions = {
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   190
                let r = &mut server.rooms[room_id];
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   191
                vec![RoomRemove(r.name.clone()).send_all()
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   192
                        .with_protocol(r.protocol_number).action()]
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   193
            };
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   194
            server.rooms.remove(room_id);
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   195
            server.react(client_id, actions);
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   196
        }
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   197
        MoveToRoom(room_id) => {
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   198
            let actions = {
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   199
                let r = &mut server.rooms[room_id];
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   200
                let c = &mut server.clients[client_id];
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   201
                r.players_number += 1;
12148
8591375271b8 Store room protocol number
unc0rr
parents: 12147
diff changeset
   202
                c.room_id = Some(room_id);
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   203
                c.is_joined_mid_game = false;
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   204
                if r.master_id == Some(c.id) {
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   205
                    r.ready_players_number += 1;
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   206
                    c.is_master = true;
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   207
                    c.is_ready = true;
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   208
                } else {
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   209
                    c.is_ready = false;
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   210
                    c.is_master = false;
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   211
                }
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   212
                let flags_msg = ClientFlags("+i".to_string(), vec![c.nick.clone()]);
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   213
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   214
                vec![RoomJoined(vec![c.nick.clone()]).send_all().in_room(room_id).action(),
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   215
                     flags_msg.send_all().action(),
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   216
                     SendRoomUpdate(None)]
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   217
            };
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   218
            server.react(client_id, actions);
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents: 12146
diff changeset
   219
        },
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   220
        MoveToLobby(msg) => {
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   221
            let mut actions = Vec::new();
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   222
            let lobby_id = server.lobby_id;
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   223
            if let (c, Some(r)) = server.client_and_room(client_id) {
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   224
                r.players_number -= 1;
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   225
                if c.is_ready {
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   226
                    r.ready_players_number -= 1;
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   227
                }
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   228
                if r.players_number > 0 && c.is_master {
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   229
                    actions.push(ChangeMaster(r.id, None));
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   230
                }
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   231
                actions.push(RemoveClientTeams);
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   232
                actions.push(RoomLeft(c.nick.clone(), msg)
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   233
                    .send_all().in_room(r.id).but_self().action());
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   234
                actions.push(ClientFlags("-i".to_string(), vec![c.nick.clone()])
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   235
                    .send_all().action());
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   236
                actions.push(SendRoomUpdate(Some(r.name.clone())));
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   237
            }
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   238
            server.react(client_id, actions);
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   239
            actions = Vec::new();
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   240
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   241
            if let (c, Some(r)) = server.client_and_room(client_id) {
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   242
                c.room_id = Some(lobby_id);
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   243
                if r.players_number == 0 {
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   244
                    actions.push(RemoveRoom(r.id));
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   245
                }
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   246
            }
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   247
            server.react(client_id, actions)
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   248
        }
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   249
        ChangeMaster(room_id, new_id) => {
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   250
            let mut actions = Vec::new();
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   251
            let room_client_ids = server.room_clients(room_id);
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   252
            let new_id = new_id.or_else(||
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   253
                room_client_ids.iter().find(|id| **id != client_id).map(|id| *id));
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   254
            let new_nick = new_id.map(|id| server.clients[id].nick.clone());
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   255
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   256
            if let (c, Some(r)) = server.client_and_room(client_id) {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   257
                match r.master_id {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   258
                    Some(id) if id == c.id => {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   259
                        c.is_master = false;
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   260
                        r.master_id = None;
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   261
                        actions.push(ClientFlags("-h".to_string(), vec![c.nick.clone()])
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   262
                            .send_all().in_room(r.id).action());
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   263
                    }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   264
                    Some(_) => unreachable!(),
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   265
                    None => {}
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   266
                }
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   267
                r.master_id = new_id;
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   268
                if let Some(nick) = new_nick {
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   269
                    actions.push(ClientFlags("+h".to_string(), vec![nick])
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   270
                        .send_all().in_room(r.id).action());
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   271
                }
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   272
            }
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   273
            new_id.map(|id| server.clients[id].is_master = true);
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   274
            server.react(client_id, actions);
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   275
        }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   276
        RemoveTeam(name) => {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   277
            let actions = if let (c, Some(r)) = server.client_and_room(client_id) {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   278
                r.remove_team(&name);
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   279
                vec![TeamRemove(name).send_all().in_room(r.id).action(),
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   280
                     SendRoomUpdate(None)]
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   281
            } else {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   282
                Vec::new()
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   283
            };
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   284
            server.react(client_id, actions);
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   285
        },
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   286
        RemoveClientTeams => {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   287
            let actions = if let (c, Some(r)) = server.client_and_room(client_id) {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   288
                r.client_teams(c.id).map(|t| RemoveTeam(t.name.clone())).collect()
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   289
            } else {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   290
                Vec::new()
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   291
            };
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   292
            server.react(client_id, actions);
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   293
        }
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   294
        SendRoomUpdate(old_name) => {
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   295
            let actions = if let (c, Some(r)) = server.client_and_room(client_id) {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   296
                let name = old_name.unwrap_or_else(|| r.name.clone());
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   297
                vec![RoomUpdated(name, r.info(Some(&c)))
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   298
                    .send_all().with_protocol(r.protocol_number).action()]
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   299
            } else {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   300
                Vec::new()
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   301
            };
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   302
            server.react(client_id, actions);
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   303
        }
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   304
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents: 12146
diff changeset
   305
        Warn(msg) => {
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   306
            run_action(server, client_id, Warning(msg).send_self().action());
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents: 12146
diff changeset
   307
        }
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   308
        ProtocolError(msg) => {
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   309
            run_action(server, client_id, Error(msg).send_self().action())
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   310
        }
12144
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
   311
    }
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
   312
}