gameServer2/src/server/actions.rs
author alfadur
Wed, 27 Jun 2018 17:58:33 +0300
changeset 13431 f091f69d59e4
parent 13429 d8354cb98b98
child 13432 6f6a866c86a2
permissions -rw-r--r--
Additional round cleanup
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
     1
use std::{
13428
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
     2
    io, io::Write,
13431
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
     3
    iter::once,
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
     4
    mem::swap
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
     5
};
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
     6
use super::{
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
     7
    server::HWServer,
13428
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
     8
    room::{RoomId, GameInfo},
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
     9
    client::{ClientId, HWClient},
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    10
    room::HWRoom,
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    11
    handlers
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    12
};
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    13
use protocol::messages::{
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    14
    HWProtocolMessage,
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    15
    HWServerMessage,
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    16
    HWServerMessage::*
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    17
};
13428
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
    18
use utils::to_engine_msg;
12143
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents:
diff changeset
    19
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    20
pub enum Destination {
13431
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
    21
    ToId(ClientId),
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    22
    ToSelf,
13428
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
    23
    ToAll {
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    24
        room_id: Option<RoomId>,
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    25
        protocol: Option<u32>,
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    26
        skip_self: bool
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    27
    }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    28
}
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    29
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    30
pub struct PendingMessage {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    31
    pub destination: Destination,
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    32
    pub message: HWServerMessage
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    33
}
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    34
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    35
impl PendingMessage {
13431
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
    36
    pub fn send(message: HWServerMessage, client_id: ClientId) -> PendingMessage {
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
    37
        PendingMessage{ destination: Destination::ToId(client_id), message}
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
    38
    }
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
    39
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    40
    pub fn send_self(message: HWServerMessage) -> PendingMessage {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    41
        PendingMessage{ destination: Destination::ToSelf, message }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    42
    }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    43
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    44
    pub fn send_all(message: HWServerMessage) -> PendingMessage {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    45
        let destination = Destination::ToAll {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    46
            room_id: None,
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    47
            protocol: None,
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    48
            skip_self: false,
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    49
        };
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    50
        PendingMessage{ destination, message }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    51
    }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    52
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    53
    pub fn in_room(mut self, clients_room_id: RoomId) -> PendingMessage {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    54
        if let Destination::ToAll {ref mut room_id, ..} = self.destination {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    55
            *room_id = Some(clients_room_id)
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    56
        }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    57
        self
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    58
    }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    59
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    60
    pub fn with_protocol(mut self, protocol_number: u32) -> PendingMessage {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    61
        if let Destination::ToAll {ref mut protocol, ..} = self.destination {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    62
            *protocol = Some(protocol_number)
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    63
        }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    64
        self
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    65
    }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    66
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    67
    pub fn but_self(mut self) -> PendingMessage {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    68
        if let Destination::ToAll {ref mut skip_self, ..} = self.destination {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    69
            *skip_self = true
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    70
        }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    71
        self
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    72
    }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    73
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    74
    pub fn action(self) -> Action { Send(self) }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    75
}
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    76
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    77
impl Into<Action> for PendingMessage {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    78
    fn into(self) -> Action { self.action() }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    79
}
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    80
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    81
impl HWServerMessage {
13431
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
    82
    pub fn send(self, client_id: ClientId) -> PendingMessage { PendingMessage::send(self, client_id) }
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    83
    pub fn send_self(self) -> PendingMessage { PendingMessage::send_self(self) }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    84
    pub fn send_all(self) -> PendingMessage { PendingMessage::send_all(self) }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    85
}
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    86
12143
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents:
diff changeset
    87
pub enum Action {
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    88
    Send(PendingMessage),
12144
f3121d7dedec - Handle errors
unc0rr
parents: 12143
diff changeset
    89
    RemoveClient,
f3121d7dedec - Handle errors
unc0rr
parents: 12143
diff changeset
    90
    ByeClient(String),
12147
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12146
diff changeset
    91
    ReactProtocolMessage(HWProtocolMessage),
12150
a482c7a5f6e3 Lobby joining action
unc0rr
parents: 12149
diff changeset
    92
    CheckRegistered,
a482c7a5f6e3 Lobby joining action
unc0rr
parents: 12149
diff changeset
    93
    JoinLobby,
12152
03ccb89820f3 Room creation halfplemented
unc0rr
parents: 12151
diff changeset
    94
    AddRoom(String, Option<String>),
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    95
    RemoveRoom(RoomId),
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    96
    MoveToRoom(RoomId),
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    97
    MoveToLobby(String),
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    98
    ChangeMaster(RoomId, Option<ClientId>),
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    99
    RemoveTeam(String),
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   100
    RemoveClientTeams,
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   101
    SendRoomUpdate(Option<String>),
13428
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   102
    StartRoomGame(RoomId),
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   103
    SendTeamRemovalMessage(String),
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   104
    FinishRoomGame(RoomId),
13431
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   105
    SendRoomData{to: ClientId, teams: bool, config: bool, flags: bool},
12152
03ccb89820f3 Room creation halfplemented
unc0rr
parents: 12151
diff changeset
   106
    Warn(String),
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   107
    ProtocolError(String)
12143
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents:
diff changeset
   108
}
12149
589a2d7d3dc5 More refactoring
unc0rr
parents: 12148
diff changeset
   109
589a2d7d3dc5 More refactoring
unc0rr
parents: 12148
diff changeset
   110
use self::Action::*;
589a2d7d3dc5 More refactoring
unc0rr
parents: 12148
diff changeset
   111
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   112
pub fn run_action(server: &mut HWServer, client_id: usize, action: Action) {
12149
589a2d7d3dc5 More refactoring
unc0rr
parents: 12148
diff changeset
   113
    match action {
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   114
        Send(msg) => server.send(client_id, msg.destination, msg.message),
12149
589a2d7d3dc5 More refactoring
unc0rr
parents: 12148
diff changeset
   115
        ByeClient(msg) => {
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   116
            let room_id;
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   117
            let nick;
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   118
            {
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   119
                let c = &server.clients[client_id];
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   120
                room_id = c.room_id;
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   121
                nick = c.nick.clone();
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   122
            }
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   123
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   124
            room_id.map (|id| {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   125
                if id != server.lobby_id {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   126
                    server.react(client_id, vec![
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   127
                        MoveToLobby(format!("quit: {}", msg.clone()))]);
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   128
                }
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   129
            });
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   130
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   131
            server.react(client_id, vec![
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   132
                LobbyLeft(nick, msg.clone()).send_all().action(),
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   133
                Bye(msg).send_self().action(),
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   134
                RemoveClient]);
12149
589a2d7d3dc5 More refactoring
unc0rr
parents: 12148
diff changeset
   135
        },
589a2d7d3dc5 More refactoring
unc0rr
parents: 12148
diff changeset
   136
        RemoveClient => {
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   137
            server.removed_clients.push(client_id);
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   138
            if server.clients.contains(client_id) {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   139
                server.clients.remove(client_id);
13124
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12858
diff changeset
   140
            }
12149
589a2d7d3dc5 More refactoring
unc0rr
parents: 12148
diff changeset
   141
        },
589a2d7d3dc5 More refactoring
unc0rr
parents: 12148
diff changeset
   142
        ReactProtocolMessage(msg) =>
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   143
            handlers::handle(server, client_id, msg),
12150
a482c7a5f6e3 Lobby joining action
unc0rr
parents: 12149
diff changeset
   144
        CheckRegistered =>
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   145
            if server.clients[client_id].protocol_number > 0 && server.clients[client_id].nick != "" {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   146
                server.react(client_id, vec![
12151
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12150
diff changeset
   147
                    JoinLobby,
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12150
diff changeset
   148
                    ]);
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12150
diff changeset
   149
            },
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12150
diff changeset
   150
        JoinLobby => {
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   151
            server.clients[client_id].room_id = Some(server.lobby_id);
12152
03ccb89820f3 Room creation halfplemented
unc0rr
parents: 12151
diff changeset
   152
12151
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12150
diff changeset
   153
            let joined_msg;
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12150
diff changeset
   154
            {
13124
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12858
diff changeset
   155
                let mut lobby_nicks = Vec::new();
12857
bd35cb2302b3 Quick dirty fix for building
unc0rr
parents: 12153
diff changeset
   156
                for (_, c) in server.clients.iter() {
12151
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12150
diff changeset
   157
                    if c.room_id.is_some() {
13124
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12858
diff changeset
   158
                        lobby_nicks.push(c.nick.clone());
12151
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12150
diff changeset
   159
                    }
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12150
diff changeset
   160
                }
13124
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12858
diff changeset
   161
                joined_msg = LobbyJoined(lobby_nicks);
12151
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12150
diff changeset
   162
            }
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   163
            let everyone_msg = LobbyJoined(vec![server.clients[client_id].nick.clone()]);
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   164
            let flags_msg = ClientFlags(
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   165
                "+i".to_string(),
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   166
                server.clients.iter()
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   167
                    .filter(|(_, c)| c.room_id.is_some())
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   168
                    .map(|(_, c)| c.nick.clone())
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   169
                    .collect());
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   170
            let server_msg = ServerMessage("\u{1f994} is watching".to_string());
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   171
            let rooms_msg = Rooms(server.rooms.iter()
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   172
                .filter(|(id, _)| *id != server.lobby_id)
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   173
                .flat_map(|(_, r)|
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   174
                    r.info(r.master_id.map(|id| &server.clients[id])))
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   175
                .collect());
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   176
            server.react(client_id, vec![
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   177
                everyone_msg.send_all().but_self().action(),
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   178
                joined_msg.send_self().action(),
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   179
                flags_msg.send_self().action(),
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   180
                server_msg.send_self().action(),
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   181
                rooms_msg.send_self().action(),
12150
a482c7a5f6e3 Lobby joining action
unc0rr
parents: 12149
diff changeset
   182
                ]);
a482c7a5f6e3 Lobby joining action
unc0rr
parents: 12149
diff changeset
   183
        },
12152
03ccb89820f3 Room creation halfplemented
unc0rr
parents: 12151
diff changeset
   184
        AddRoom(name, password) => {
13124
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12858
diff changeset
   185
            let room_id = server.add_room();;
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   186
            let actions = {
12153
8591375271b8 Store room protocol number
unc0rr
parents: 12152
diff changeset
   187
                let r = &mut server.rooms[room_id];
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   188
                let c = &mut server.clients[client_id];
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   189
                r.master_id = Some(c.id);
12153
8591375271b8 Store room protocol number
unc0rr
parents: 12152
diff changeset
   190
                r.name = name;
8591375271b8 Store room protocol number
unc0rr
parents: 12152
diff changeset
   191
                r.password = password;
8591375271b8 Store room protocol number
unc0rr
parents: 12152
diff changeset
   192
                r.protocol_number = c.protocol_number;
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   193
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   194
                vec![
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   195
                    RoomAdd(r.info(Some(&c))).send_all()
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   196
                        .with_protocol(r.protocol_number).action(),
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   197
                    MoveToRoom(room_id)]
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   198
            };
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   199
            server.react(client_id, actions);
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   200
        },
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   201
        RemoveRoom(room_id) => {
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   202
            let actions = {
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   203
                let r = &mut server.rooms[room_id];
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   204
                vec![RoomRemove(r.name.clone()).send_all()
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   205
                        .with_protocol(r.protocol_number).action()]
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   206
            };
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   207
            server.rooms.remove(room_id);
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   208
            server.react(client_id, actions);
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   209
        }
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   210
        MoveToRoom(room_id) => {
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   211
            let actions = {
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   212
                let r = &mut server.rooms[room_id];
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   213
                let c = &mut server.clients[client_id];
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   214
                r.players_number += 1;
12153
8591375271b8 Store room protocol number
unc0rr
parents: 12152
diff changeset
   215
                c.room_id = Some(room_id);
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   216
                c.is_joined_mid_game = false;
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   217
                if r.master_id == Some(c.id) {
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   218
                    r.ready_players_number += 1;
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   219
                    c.is_master = true;
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   220
                    c.is_ready = true;
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   221
                } else {
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   222
                    c.is_ready = false;
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   223
                    c.is_master = false;
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   224
                }
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   225
                let flags_msg = ClientFlags("+i".to_string(), vec![c.nick.clone()]);
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   226
13427
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
   227
                let mut v = vec![
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
   228
                    RoomJoined(vec![c.nick.clone()]).send_all().in_room(room_id).action(),
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
   229
                    flags_msg.send_all().action(),
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
   230
                    SendRoomUpdate(None)];
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
   231
                if !c.is_master {
13431
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   232
                    v.push(SendRoomData{ to: client_id, teams: true, config: true, flags: true});
13427
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
   233
                }
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
   234
                v
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   235
            };
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   236
            server.react(client_id, actions);
13429
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13428
diff changeset
   237
        }
13431
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   238
        SendRoomData {to, teams, config, flags} => {
13429
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13428
diff changeset
   239
            let mut actions = Vec::new();
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13428
diff changeset
   240
            let room_id = server.clients[client_id].room_id;
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13428
diff changeset
   241
            if let Some(r) = room_id.and_then(|id| server.rooms.get(id)) {
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13428
diff changeset
   242
                if config {
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13428
diff changeset
   243
                    actions.push(ConfigEntry("FULLMAPCONFIG".to_string(), r.map_config())
13431
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   244
                        .send(to).action());
13429
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13428
diff changeset
   245
                    for cfg in r.game_config().into_iter() {
13431
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   246
                        actions.push(cfg.into_server_msg().send(to).action());
13429
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13428
diff changeset
   247
                    }
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13428
diff changeset
   248
                }
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13428
diff changeset
   249
                if teams {
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13428
diff changeset
   250
                    for (owner_id, team) in r.teams.iter() {
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13428
diff changeset
   251
                        actions.push(TeamAdd(HWRoom::team_info(&server.clients[*owner_id], &team))
13431
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   252
                            .send(to).action());
13429
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13428
diff changeset
   253
                    }
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13428
diff changeset
   254
                }
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13428
diff changeset
   255
                if flags {
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13428
diff changeset
   256
                    if let Some(id) = r.master_id {
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13428
diff changeset
   257
                        actions.push(ClientFlags("+h".to_string(), vec![server.clients[id].nick.clone()])
13431
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   258
                            .send(to).action());
13429
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13428
diff changeset
   259
                    }
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13428
diff changeset
   260
                    let nicks: Vec<_> = server.clients.iter()
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13428
diff changeset
   261
                        .filter(|(_, c)| c.room_id == Some(r.id) && c.is_ready)
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13428
diff changeset
   262
                        .map(|(_, c)| c.nick.clone()).collect();
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13428
diff changeset
   263
                    if !nicks.is_empty() {
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13428
diff changeset
   264
                        actions.push(ClientFlags("+r".to_string(), nicks)
13431
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   265
                            .send(to).action());
13429
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13428
diff changeset
   266
                    }
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13428
diff changeset
   267
                }
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13428
diff changeset
   268
            }
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13428
diff changeset
   269
            server.react(client_id, actions);
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13428
diff changeset
   270
        }
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   271
        MoveToLobby(msg) => {
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   272
            let mut actions = Vec::new();
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   273
            let lobby_id = server.lobby_id;
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   274
            if let (c, Some(r)) = server.client_and_room(client_id) {
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   275
                r.players_number -= 1;
13429
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13428
diff changeset
   276
                if c.is_ready && r.ready_players_number > 0 {
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   277
                    r.ready_players_number -= 1;
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   278
                }
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   279
                if r.players_number > 0 && c.is_master {
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   280
                    actions.push(ChangeMaster(r.id, None));
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   281
                }
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   282
                actions.push(RemoveClientTeams);
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   283
                actions.push(RoomLeft(c.nick.clone(), msg)
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   284
                    .send_all().in_room(r.id).but_self().action());
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   285
                actions.push(ClientFlags("-i".to_string(), vec![c.nick.clone()])
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   286
                    .send_all().action());
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   287
                actions.push(SendRoomUpdate(Some(r.name.clone())));
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   288
            }
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   289
            server.react(client_id, actions);
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   290
            actions = Vec::new();
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   291
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   292
            if let (c, Some(r)) = server.client_and_room(client_id) {
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   293
                c.room_id = Some(lobby_id);
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   294
                if r.players_number == 0 {
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   295
                    actions.push(RemoveRoom(r.id));
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   296
                }
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   297
            }
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   298
            server.react(client_id, actions)
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   299
        }
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   300
        ChangeMaster(room_id, new_id) => {
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   301
            let mut actions = Vec::new();
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   302
            let room_client_ids = server.room_clients(room_id);
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   303
            let new_id = new_id.or_else(||
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   304
                room_client_ids.iter().find(|id| **id != client_id).map(|id| *id));
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   305
            let new_nick = new_id.map(|id| server.clients[id].nick.clone());
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   306
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   307
            if let (c, Some(r)) = server.client_and_room(client_id) {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   308
                match r.master_id {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   309
                    Some(id) if id == c.id => {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   310
                        c.is_master = false;
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   311
                        r.master_id = None;
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   312
                        actions.push(ClientFlags("-h".to_string(), vec![c.nick.clone()])
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   313
                            .send_all().in_room(r.id).action());
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   314
                    }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   315
                    Some(_) => unreachable!(),
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   316
                    None => {}
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   317
                }
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   318
                r.master_id = new_id;
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   319
                if let Some(nick) = new_nick {
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   320
                    actions.push(ClientFlags("+h".to_string(), vec![nick])
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   321
                        .send_all().in_room(r.id).action());
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   322
                }
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   323
            }
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   324
            new_id.map(|id| server.clients[id].is_master = true);
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   325
            server.react(client_id, actions);
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   326
        }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   327
        RemoveTeam(name) => {
13431
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   328
            let mut actions = Vec::new();
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   329
            if let (c, Some(r)) = server.client_and_room(client_id) {
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   330
                r.remove_team(&name);
13431
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   331
                if let Some(ref mut info) = r.game_info {
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   332
                    info.left_teams.push(name.clone());
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   333
                }
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   334
                actions.push(TeamRemove(name.clone()).send_all().in_room(r.id).action());
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   335
                actions.push(SendRoomUpdate(None));
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   336
                if r.game_info.is_some() && c.is_in_game {
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   337
                    actions.push(SendTeamRemovalMessage(name));
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   338
                }
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   339
            }
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   340
            server.react(client_id, actions);
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   341
        },
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   342
        RemoveClientTeams => {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   343
            let actions = if let (c, Some(r)) = server.client_and_room(client_id) {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   344
                r.client_teams(c.id).map(|t| RemoveTeam(t.name.clone())).collect()
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   345
            } else {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   346
                Vec::new()
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   347
            };
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   348
            server.react(client_id, actions);
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   349
        }
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   350
        SendRoomUpdate(old_name) => {
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   351
            let actions = if let (c, Some(r)) = server.client_and_room(client_id) {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   352
                let name = old_name.unwrap_or_else(|| r.name.clone());
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   353
                vec![RoomUpdated(name, r.info(Some(&c)))
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   354
                    .send_all().with_protocol(r.protocol_number).action()]
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   355
            } else {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   356
                Vec::new()
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   357
            };
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   358
            server.react(client_id, actions);
13428
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   359
        },
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   360
        StartRoomGame(room_id) => {
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   361
            let actions = {
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   362
                let (room_clients, room_nicks): (Vec<_>, Vec<_>) = server.clients.iter()
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   363
                    .map(|(id, c)| (id, c.nick.clone())).unzip();
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   364
                let room = &mut server.rooms[room_id];
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   365
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   366
                if !room.has_multiple_clans() {
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   367
                    vec![Warn("The game can't be started with less than two clans!".to_string())]
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   368
                } else if room.game_info.is_some() {
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   369
                    vec![Warn("The game is already in progress".to_string())]
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   370
                } else {
13431
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   371
                    room.game_info = Some(GameInfo::new(room.teams.len() as u8));
13428
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   372
                    for id in room_clients {
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   373
                        let c = &mut server.clients[id];
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   374
                        c.is_in_game = true;
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   375
                        c.team_indices = room.client_team_indices(c.id);
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   376
                    }
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   377
                    vec![RunGame.send_all().in_room(room.id).action(),
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   378
                         SendRoomUpdate(None),
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   379
                         ClientFlags("+g".to_string(), room_nicks)
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   380
                             .send_all().in_room(room.id).action()]
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   381
                }
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   382
            };
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   383
            server.react(client_id, actions);
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   384
        }
13428
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   385
        SendTeamRemovalMessage(team_name) => {
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   386
            let mut actions = Vec::new();
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   387
            if let (c, Some(r)) = server.client_and_room(client_id) {
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   388
                if let Some(ref mut info) = r.game_info {
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   389
                    let msg = once(b'F').chain(team_name.bytes());
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   390
                    actions.push(ForwardEngineMessage(to_engine_msg(msg)).
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   391
                        send_all().in_room(r.id).but_self().action());
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   392
                    info.teams_in_game -= 1;
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   393
                    if info.teams_in_game == 0 {
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   394
                        actions.push(FinishRoomGame(r.id));
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   395
                    }
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   396
                }
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   397
            }
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   398
            server.react(client_id, actions);
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   399
        }
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   400
        FinishRoomGame(room_id) => {
13431
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   401
            let mut actions = Vec::new();
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   402
            let mut old_info = None;
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   403
            {
13428
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   404
                let r = &mut server.rooms[room_id];
13431
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   405
                swap(&mut old_info, &mut r.game_info);
13428
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   406
                r.game_info = None;
13431
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   407
                r.ready_players_number = 1;
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   408
                actions.push(SendRoomUpdate(None));
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   409
                actions.push(RoundFinished.send_all().in_room(r.id).action());
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   410
            }
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   411
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   412
            if let Some(info) = old_info {
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   413
                for (_, c) in server.clients.iter() {
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   414
                    if c.room_id == Some(room_id) && c.is_joined_mid_game {
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   415
                        actions.push(SendRoomData{
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   416
                            to: c.id, teams: false,
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   417
                            config: true, flags: false});
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   418
                        for name in info.left_teams.iter() {
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   419
                            actions.push(TeamRemove(name.clone())
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   420
                                .send(c.id).action());
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   421
                        }
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   422
                    }
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   423
                }
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   424
            }
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   425
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   426
            let nicks: Vec<_> = server.clients.iter_mut()
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   427
                .filter(|(_, c)| c.room_id == Some(room_id))
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   428
                .map(|(_, c)| {
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   429
                    c.is_ready = c.is_master;
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   430
                    c.is_joined_mid_game = false;
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   431
                    c
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   432
                }).filter_map(|c| if !c.is_master {
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   433
                    Some(c.nick.clone())
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   434
                } else {
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   435
                    None
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   436
                }).collect();
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   437
            if !nicks.is_empty() {
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   438
                actions.push(ClientFlags("-r".to_string(), nicks)
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   439
                    .send_all().in_room(room_id).action());
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   440
            }
13428
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   441
            server.react(client_id, actions);
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   442
        }
12152
03ccb89820f3 Room creation halfplemented
unc0rr
parents: 12151
diff changeset
   443
        Warn(msg) => {
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   444
            run_action(server, client_id, Warning(msg).send_self().action());
12152
03ccb89820f3 Room creation halfplemented
unc0rr
parents: 12151
diff changeset
   445
        }
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   446
        ProtocolError(msg) => {
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   447
            run_action(server, client_id, Error(msg).send_self().action())
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   448
        }
12149
589a2d7d3dc5 More refactoring
unc0rr
parents: 12148
diff changeset
   449
    }
589a2d7d3dc5 More refactoring
unc0rr
parents: 12148
diff changeset
   450
}