gameServer2/src/server/actions.rs
author alfadur
Wed, 04 Jul 2018 04:42:16 +0300
changeset 13444 c4f917c6be51
parent 13433 c8425fbcf1d9
child 13448 2501428303a2
permissions -rw-r--r--
add missing message tests
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
13427
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
   226
                let mut v = vec![
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
   227
                    RoomJoined(vec![c.nick.clone()]).send_all().in_room(room_id).action(),
13432
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
   228
                    ClientFlags("+i".to_string(), vec![c.nick.clone()]).send_all().action(),
13427
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
   229
                    SendRoomUpdate(None)];
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
   230
                if !c.is_master {
13432
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
   231
                    let team_names: Vec<_>;
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
   232
                    if let Some(ref mut info) = r.game_info {
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
   233
                        c.is_in_game = true;
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
   234
                        c.is_joined_mid_game = true;
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
   235
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
   236
                        {
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
   237
                            let teams = info.client_teams(c.id);
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
   238
                            c.teams_in_game = teams.clone().count() as u8;
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
   239
                            c.clan = teams.clone().next().map(|t| t.color);
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
   240
                            team_names = teams.map(|t| t.name.clone()).collect();
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
   241
                        }
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
   242
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
   243
                        if !team_names.is_empty() {
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
   244
                            info.left_teams.retain(|name|
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
   245
                                !team_names.contains(&name));
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
   246
                            info.teams_in_game += team_names.len() as u8;
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
   247
                            r.teams = info.teams_at_start.iter()
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
   248
                                .filter(|(_, t)| !team_names.contains(&t.name))
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
   249
                                .cloned().collect();
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
   250
                        }
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
   251
                    } else {
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
   252
                        team_names = Vec::new();
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
   253
                    }
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
   254
13431
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   255
                    v.push(SendRoomData{ to: client_id, teams: true, config: true, flags: true});
13432
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
   256
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
   257
                    if let Some(ref info) = r.game_info {
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
   258
                        v.push(RunGame.send_self().action());
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
   259
                        v.push(ClientFlags("+g".to_string(), vec![c.nick.clone()])
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
   260
                            .send_all().in_room(r.id).action());
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
   261
                        v.push(ForwardEngineMessage(
13433
c8425fbcf1d9 Fix spectators joining midgame
alfadur
parents: 13432
diff changeset
   262
                            vec![to_engine_msg("e$spectate 1".bytes())])
c8425fbcf1d9 Fix spectators joining midgame
alfadur
parents: 13432
diff changeset
   263
                            .send_self().action());
c8425fbcf1d9 Fix spectators joining midgame
alfadur
parents: 13432
diff changeset
   264
                        v.push(ForwardEngineMessage(info.msg_log.clone())
13432
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
   265
                            .send_self().action());
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
   266
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
   267
                        for name in team_names.iter() {
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
   268
                            v.push(ForwardEngineMessage(
13433
c8425fbcf1d9 Fix spectators joining midgame
alfadur
parents: 13432
diff changeset
   269
                                vec![to_engine_msg(once(b'G').chain(name.bytes()))])
13432
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
   270
                                .send_all().in_room(r.id).action());
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
   271
                        }
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
   272
                        if info.is_paused {
13433
c8425fbcf1d9 Fix spectators joining midgame
alfadur
parents: 13432
diff changeset
   273
                            v.push(ForwardEngineMessage(vec![to_engine_msg(once(b'I'))])
13432
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
   274
                                .send_all().in_room(r.id).action())
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
   275
                        }
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
   276
                    }
13427
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
   277
                }
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
   278
                v
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   279
            };
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   280
            server.react(client_id, actions);
13429
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13428
diff changeset
   281
        }
13431
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   282
        SendRoomData {to, teams, config, flags} => {
13429
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13428
diff changeset
   283
            let mut actions = Vec::new();
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13428
diff changeset
   284
            let room_id = server.clients[client_id].room_id;
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13428
diff changeset
   285
            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
   286
                if config {
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13428
diff changeset
   287
                    actions.push(ConfigEntry("FULLMAPCONFIG".to_string(), r.map_config())
13431
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   288
                        .send(to).action());
13429
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13428
diff changeset
   289
                    for cfg in r.game_config().into_iter() {
13444
c4f917c6be51 add missing message tests
alfadur
parents: 13433
diff changeset
   290
                        actions.push(cfg.to_server_msg().send(to).action());
13429
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13428
diff changeset
   291
                    }
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13428
diff changeset
   292
                }
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13428
diff changeset
   293
                if teams {
13432
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
   294
                    let current_teams = match r.game_info {
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
   295
                        Some(ref info) => &info.teams_at_start,
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
   296
                        None => &r.teams
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
   297
                    };
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
   298
                    for (owner_id, team) in current_teams.iter() {
13429
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13428
diff changeset
   299
                        actions.push(TeamAdd(HWRoom::team_info(&server.clients[*owner_id], &team))
13431
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   300
                            .send(to).action());
13432
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
   301
                        actions.push(TeamColor(team.name.clone(), team.color)
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
   302
                            .send(to).action());
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
   303
                        actions.push(HedgehogsNumber(team.name.clone(), team.hedgehogs_number)
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
   304
                            .send(to).action());
13429
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13428
diff changeset
   305
                    }
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13428
diff changeset
   306
                }
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13428
diff changeset
   307
                if flags {
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13428
diff changeset
   308
                    if let Some(id) = r.master_id {
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13428
diff changeset
   309
                        actions.push(ClientFlags("+h".to_string(), vec![server.clients[id].nick.clone()])
13431
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   310
                            .send(to).action());
13429
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13428
diff changeset
   311
                    }
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13428
diff changeset
   312
                    let nicks: Vec<_> = server.clients.iter()
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13428
diff changeset
   313
                        .filter(|(_, c)| c.room_id == Some(r.id) && c.is_ready)
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13428
diff changeset
   314
                        .map(|(_, c)| c.nick.clone()).collect();
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13428
diff changeset
   315
                    if !nicks.is_empty() {
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13428
diff changeset
   316
                        actions.push(ClientFlags("+r".to_string(), nicks)
13431
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   317
                            .send(to).action());
13429
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13428
diff changeset
   318
                    }
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13428
diff changeset
   319
                }
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13428
diff changeset
   320
            }
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13428
diff changeset
   321
            server.react(client_id, actions);
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13428
diff changeset
   322
        }
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   323
        MoveToLobby(msg) => {
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   324
            let mut actions = Vec::new();
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   325
            let lobby_id = server.lobby_id;
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   326
            if let (c, Some(r)) = server.client_and_room(client_id) {
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   327
                r.players_number -= 1;
13429
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13428
diff changeset
   328
                if c.is_ready && r.ready_players_number > 0 {
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   329
                    r.ready_players_number -= 1;
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   330
                }
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   331
                if r.players_number > 0 && c.is_master {
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   332
                    actions.push(ChangeMaster(r.id, None));
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   333
                }
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   334
                actions.push(RemoveClientTeams);
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   335
                actions.push(RoomLeft(c.nick.clone(), msg)
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   336
                    .send_all().in_room(r.id).but_self().action());
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   337
                actions.push(ClientFlags("-i".to_string(), vec![c.nick.clone()])
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   338
                    .send_all().action());
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   339
                actions.push(SendRoomUpdate(Some(r.name.clone())));
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   340
            }
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   341
            server.react(client_id, actions);
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   342
            actions = Vec::new();
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   343
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   344
            if let (c, Some(r)) = server.client_and_room(client_id) {
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   345
                c.room_id = Some(lobby_id);
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   346
                if r.players_number == 0 {
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   347
                    actions.push(RemoveRoom(r.id));
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   348
                }
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   349
            }
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   350
            server.react(client_id, actions)
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   351
        }
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   352
        ChangeMaster(room_id, new_id) => {
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   353
            let mut actions = Vec::new();
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   354
            let room_client_ids = server.room_clients(room_id);
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   355
            let new_id = new_id.or_else(||
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   356
                room_client_ids.iter().find(|id| **id != client_id).map(|id| *id));
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   357
            let new_nick = new_id.map(|id| server.clients[id].nick.clone());
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   358
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   359
            if let (c, Some(r)) = server.client_and_room(client_id) {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   360
                match r.master_id {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   361
                    Some(id) if id == c.id => {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   362
                        c.is_master = false;
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   363
                        r.master_id = None;
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   364
                        actions.push(ClientFlags("-h".to_string(), vec![c.nick.clone()])
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   365
                            .send_all().in_room(r.id).action());
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   366
                    }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   367
                    Some(_) => unreachable!(),
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   368
                    None => {}
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   369
                }
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   370
                r.master_id = new_id;
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   371
                if let Some(nick) = new_nick {
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   372
                    actions.push(ClientFlags("+h".to_string(), vec![nick])
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   373
                        .send_all().in_room(r.id).action());
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   374
                }
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   375
            }
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   376
            new_id.map(|id| server.clients[id].is_master = true);
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   377
            server.react(client_id, actions);
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   378
        }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   379
        RemoveTeam(name) => {
13431
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   380
            let mut actions = Vec::new();
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   381
            if let (c, Some(r)) = server.client_and_room(client_id) {
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   382
                r.remove_team(&name);
13431
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   383
                if let Some(ref mut info) = r.game_info {
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   384
                    info.left_teams.push(name.clone());
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   385
                }
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   386
                actions.push(TeamRemove(name.clone()).send_all().in_room(r.id).action());
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   387
                actions.push(SendRoomUpdate(None));
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   388
                if r.game_info.is_some() && c.is_in_game {
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   389
                    actions.push(SendTeamRemovalMessage(name));
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   390
                }
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   391
            }
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   392
            server.react(client_id, actions);
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   393
        },
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   394
        RemoveClientTeams => {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   395
            let actions = if let (c, Some(r)) = server.client_and_room(client_id) {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   396
                r.client_teams(c.id).map(|t| RemoveTeam(t.name.clone())).collect()
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   397
            } else {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   398
                Vec::new()
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   399
            };
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   400
            server.react(client_id, actions);
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   401
        }
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   402
        SendRoomUpdate(old_name) => {
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   403
            let actions = if let (c, Some(r)) = server.client_and_room(client_id) {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   404
                let name = old_name.unwrap_or_else(|| r.name.clone());
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   405
                vec![RoomUpdated(name, r.info(Some(&c)))
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   406
                    .send_all().with_protocol(r.protocol_number).action()]
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   407
            } else {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   408
                Vec::new()
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   409
            };
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   410
            server.react(client_id, actions);
13428
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   411
        },
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   412
        StartRoomGame(room_id) => {
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   413
            let actions = {
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   414
                let (room_clients, room_nicks): (Vec<_>, Vec<_>) = server.clients.iter()
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   415
                    .map(|(id, c)| (id, c.nick.clone())).unzip();
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   416
                let room = &mut server.rooms[room_id];
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   417
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   418
                if !room.has_multiple_clans() {
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   419
                    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
   420
                } else if room.game_info.is_some() {
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   421
                    vec![Warn("The game is already in progress".to_string())]
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   422
                } else {
13432
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
   423
                    room.start_round();
13428
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   424
                    for id in room_clients {
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   425
                        let c = &mut server.clients[id];
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   426
                        c.is_in_game = true;
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   427
                        c.team_indices = room.client_team_indices(c.id);
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   428
                    }
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   429
                    vec![RunGame.send_all().in_room(room.id).action(),
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   430
                         SendRoomUpdate(None),
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   431
                         ClientFlags("+g".to_string(), room_nicks)
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   432
                             .send_all().in_room(room.id).action()]
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   433
                }
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   434
            };
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   435
            server.react(client_id, actions);
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   436
        }
13428
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   437
        SendTeamRemovalMessage(team_name) => {
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   438
            let mut actions = Vec::new();
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   439
            if let (c, Some(r)) = server.client_and_room(client_id) {
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   440
                if let Some(ref mut info) = r.game_info {
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   441
                    let msg = once(b'F').chain(team_name.bytes());
13433
c8425fbcf1d9 Fix spectators joining midgame
alfadur
parents: 13432
diff changeset
   442
                    actions.push(ForwardEngineMessage(vec![to_engine_msg(msg)]).
13428
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   443
                        send_all().in_room(r.id).but_self().action());
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   444
                    info.teams_in_game -= 1;
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   445
                    if info.teams_in_game == 0 {
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   446
                        actions.push(FinishRoomGame(r.id));
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   447
                    }
13432
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
   448
                    let remove_msg = to_engine_msg(once(b'F').chain(team_name.bytes()));
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
   449
                    match &info.last_msg {
13433
c8425fbcf1d9 Fix spectators joining midgame
alfadur
parents: 13432
diff changeset
   450
                        Some(m) => info.msg_log.push(m.clone()),
c8425fbcf1d9 Fix spectators joining midgame
alfadur
parents: 13432
diff changeset
   451
                        None => info.msg_log.push(remove_msg.clone())
13432
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
   452
                    }
13433
c8425fbcf1d9 Fix spectators joining midgame
alfadur
parents: 13432
diff changeset
   453
                    actions.push(ForwardEngineMessage(vec![remove_msg])
13432
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
   454
                        .send_all().in_room(r.id).but_self().action());
13428
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   455
                }
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   456
            }
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   457
            server.react(client_id, actions);
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   458
        }
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   459
        FinishRoomGame(room_id) => {
13431
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   460
            let mut actions = Vec::new();
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   461
            let mut old_info = None;
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   462
            {
13428
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   463
                let r = &mut server.rooms[room_id];
13431
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   464
                swap(&mut old_info, &mut r.game_info);
13428
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   465
                r.game_info = None;
13431
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   466
                r.ready_players_number = 1;
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   467
                actions.push(SendRoomUpdate(None));
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   468
                actions.push(RoundFinished.send_all().in_room(r.id).action());
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   469
            }
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   470
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   471
            if let Some(info) = old_info {
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   472
                for (_, c) in server.clients.iter() {
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   473
                    if c.room_id == Some(room_id) && c.is_joined_mid_game {
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   474
                        actions.push(SendRoomData{
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   475
                            to: c.id, teams: false,
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   476
                            config: true, flags: false});
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   477
                        for name in info.left_teams.iter() {
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   478
                            actions.push(TeamRemove(name.clone())
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   479
                                .send(c.id).action());
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   480
                        }
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   481
                    }
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   482
                }
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   483
            }
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   484
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   485
            let nicks: Vec<_> = server.clients.iter_mut()
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   486
                .filter(|(_, c)| c.room_id == Some(room_id))
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   487
                .map(|(_, c)| {
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   488
                    c.is_ready = c.is_master;
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   489
                    c.is_joined_mid_game = false;
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   490
                    c
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   491
                }).filter_map(|c| if !c.is_master {
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   492
                    Some(c.nick.clone())
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   493
                } else {
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   494
                    None
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   495
                }).collect();
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   496
            if !nicks.is_empty() {
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   497
                actions.push(ClientFlags("-r".to_string(), nicks)
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   498
                    .send_all().in_room(room_id).action());
f091f69d59e4 Additional round cleanup
alfadur
parents: 13429
diff changeset
   499
            }
13428
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   500
            server.react(client_id, actions);
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   501
        }
12152
03ccb89820f3 Room creation halfplemented
unc0rr
parents: 12151
diff changeset
   502
        Warn(msg) => {
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   503
            run_action(server, client_id, Warning(msg).send_self().action());
12152
03ccb89820f3 Room creation halfplemented
unc0rr
parents: 12151
diff changeset
   504
        }
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   505
        ProtocolError(msg) => {
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   506
            run_action(server, client_id, Error(msg).send_self().action())
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   507
        }
12149
589a2d7d3dc5 More refactoring
unc0rr
parents: 12148
diff changeset
   508
    }
589a2d7d3dc5 More refactoring
unc0rr
parents: 12148
diff changeset
   509
}