gameServer2/src/server/actions.rs
author alfadur
Sat, 07 Jul 2018 20:22:31 +0300
changeset 13478 d79795acaa73
parent 13477 f748a72432f2
child 13480 fb37745c5bca
permissions -rw-r--r--
Mostly implement voting
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
     1
use std::{
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
     2
    io, io::Write,
13426
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
     3
    iter::once,
13478
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
     4
    mem::replace
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
     5
};
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
     6
use super::{
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
     7
    server::HWServer,
13478
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
     8
    room::{GameInfo},
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
     9
    client::HWClient,
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
    10
    coretypes::{ClientId, RoomId, VoteType},
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    11
    room::HWRoom,
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    12
    handlers
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    13
};
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    14
use protocol::messages::{
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    15
    HWProtocolMessage,
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    16
    HWServerMessage,
13478
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
    17
    HWServerMessage::*,
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
    18
    server_chat
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    19
};
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    20
use utils::to_engine_msg;
12138
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents:
diff changeset
    21
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    22
pub enum Destination {
13426
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
    23
    ToId(ClientId),
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    24
    ToSelf,
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    25
    ToAll {
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    26
        room_id: Option<RoomId>,
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    27
        protocol: Option<u32>,
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    28
        skip_self: bool
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    29
    }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    30
}
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    31
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    32
pub struct PendingMessage {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    33
    pub destination: Destination,
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    34
    pub message: HWServerMessage
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    35
}
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    36
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    37
impl PendingMessage {
13426
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
    38
    pub fn send(message: HWServerMessage, client_id: ClientId) -> PendingMessage {
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
    39
        PendingMessage{ destination: Destination::ToId(client_id), message}
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
    40
    }
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
    41
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    42
    pub fn send_self(message: HWServerMessage) -> PendingMessage {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    43
        PendingMessage{ destination: Destination::ToSelf, message }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    44
    }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    45
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    46
    pub fn send_all(message: HWServerMessage) -> PendingMessage {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    47
        let destination = Destination::ToAll {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    48
            room_id: None,
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    49
            protocol: None,
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    50
            skip_self: false,
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    51
        };
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    52
        PendingMessage{ destination, message }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    53
    }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    54
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    55
    pub fn in_room(mut self, clients_room_id: RoomId) -> PendingMessage {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    56
        if let Destination::ToAll {ref mut room_id, ..} = self.destination {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    57
            *room_id = Some(clients_room_id)
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    58
        }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    59
        self
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    60
    }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    61
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    62
    pub fn with_protocol(mut self, protocol_number: u32) -> PendingMessage {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    63
        if let Destination::ToAll {ref mut protocol, ..} = self.destination {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    64
            *protocol = Some(protocol_number)
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    65
        }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    66
        self
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    67
    }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    68
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    69
    pub fn but_self(mut self) -> PendingMessage {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    70
        if let Destination::ToAll {ref mut skip_self, ..} = self.destination {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    71
            *skip_self = true
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    72
        }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    73
        self
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    74
    }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    75
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    76
    pub fn action(self) -> Action { Send(self) }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    77
}
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    78
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    79
impl Into<Action> for PendingMessage {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    80
    fn into(self) -> Action { self.action() }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    81
}
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    82
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    83
impl HWServerMessage {
13426
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
    84
    pub fn send(self, client_id: ClientId) -> PendingMessage { PendingMessage::send(self, client_id) }
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    85
    pub fn send_self(self) -> PendingMessage { PendingMessage::send_self(self) }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    86
    pub fn send_all(self) -> PendingMessage { PendingMessage::send_all(self) }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    87
}
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    88
12138
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents:
diff changeset
    89
pub enum Action {
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    90
    Send(PendingMessage),
12139
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    91
    RemoveClient,
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    92
    ByeClient(String),
12142
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12141
diff changeset
    93
    ReactProtocolMessage(HWProtocolMessage),
12145
a482c7a5f6e3 Lobby joining action
unc0rr
parents: 12144
diff changeset
    94
    CheckRegistered,
a482c7a5f6e3 Lobby joining action
unc0rr
parents: 12144
diff changeset
    95
    JoinLobby,
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents: 12146
diff changeset
    96
    AddRoom(String, Option<String>),
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    97
    RemoveRoom(RoomId),
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    98
    MoveToRoom(RoomId),
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    99
    MoveToLobby(String),
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   100
    ChangeMaster(RoomId, Option<ClientId>),
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   101
    RemoveTeam(String),
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   102
    RemoveClientTeams,
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   103
    SendRoomUpdate(Option<String>),
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   104
    StartRoomGame(RoomId),
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   105
    SendTeamRemovalMessage(String),
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   106
    FinishRoomGame(RoomId),
13426
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
   107
    SendRoomData{to: ClientId, teams: bool, config: bool, flags: bool},
13478
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   108
    AddVote{vote: bool, is_forced: bool},
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   109
    ApplyVoting(VoteType, RoomId),
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents: 12146
diff changeset
   110
    Warn(String),
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   111
    ProtocolError(String)
12138
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents:
diff changeset
   112
}
12144
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
   113
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
   114
use self::Action::*;
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
   115
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   116
pub fn run_action(server: &mut HWServer, client_id: usize, action: Action) {
12144
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
   117
    match action {
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   118
        Send(msg) => server.send(client_id, msg.destination, msg.message),
12144
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
   119
        ByeClient(msg) => {
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   120
            let room_id;
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   121
            let nick;
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   122
            {
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   123
                let c = &server.clients[client_id];
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   124
                room_id = c.room_id;
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   125
                nick = c.nick.clone();
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   126
            }
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   127
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   128
            room_id.map (|id| {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   129
                if id != server.lobby_id {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   130
                    server.react(client_id, vec![
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   131
                        MoveToLobby(format!("quit: {}", msg.clone()))]);
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   132
                }
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   133
            });
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   134
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   135
            server.react(client_id, vec![
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   136
                LobbyLeft(nick, msg.clone()).send_all().action(),
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   137
                Bye(msg).send_self().action(),
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   138
                RemoveClient]);
12144
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
   139
        },
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
   140
        RemoveClient => {
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   141
            server.removed_clients.push(client_id);
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   142
            if server.clients.contains(client_id) {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   143
                server.clients.remove(client_id);
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12853
diff changeset
   144
            }
12144
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
   145
        },
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
   146
        ReactProtocolMessage(msg) =>
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   147
            handlers::handle(server, client_id, msg),
12145
a482c7a5f6e3 Lobby joining action
unc0rr
parents: 12144
diff changeset
   148
        CheckRegistered =>
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   149
            if server.clients[client_id].protocol_number > 0 && server.clients[client_id].nick != "" {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   150
                server.react(client_id, vec![
12146
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12145
diff changeset
   151
                    JoinLobby,
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12145
diff changeset
   152
                    ]);
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12145
diff changeset
   153
            },
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12145
diff changeset
   154
        JoinLobby => {
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   155
            server.clients[client_id].room_id = Some(server.lobby_id);
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents: 12146
diff changeset
   156
12146
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12145
diff changeset
   157
            let joined_msg;
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12145
diff changeset
   158
            {
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12853
diff changeset
   159
                let mut lobby_nicks = Vec::new();
12852
bd35cb2302b3 Quick dirty fix for building
unc0rr
parents: 12148
diff changeset
   160
                for (_, c) in server.clients.iter() {
12146
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12145
diff changeset
   161
                    if c.room_id.is_some() {
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12853
diff changeset
   162
                        lobby_nicks.push(c.nick.clone());
12146
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12145
diff changeset
   163
                    }
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12145
diff changeset
   164
                }
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12853
diff changeset
   165
                joined_msg = LobbyJoined(lobby_nicks);
12146
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12145
diff changeset
   166
            }
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   167
            let everyone_msg = LobbyJoined(vec![server.clients[client_id].nick.clone()]);
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   168
            let flags_msg = ClientFlags(
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   169
                "+i".to_string(),
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   170
                server.clients.iter()
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   171
                    .filter(|(_, c)| c.room_id.is_some())
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   172
                    .map(|(_, c)| c.nick.clone())
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   173
                    .collect());
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   174
            let server_msg = ServerMessage("\u{1f994} is watching".to_string());
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   175
            let rooms_msg = Rooms(server.rooms.iter()
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   176
                .filter(|(id, _)| *id != server.lobby_id)
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   177
                .flat_map(|(_, r)|
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   178
                    r.info(r.master_id.map(|id| &server.clients[id])))
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   179
                .collect());
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   180
            server.react(client_id, vec![
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   181
                everyone_msg.send_all().but_self().action(),
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   182
                joined_msg.send_self().action(),
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   183
                flags_msg.send_self().action(),
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   184
                server_msg.send_self().action(),
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   185
                rooms_msg.send_self().action(),
12145
a482c7a5f6e3 Lobby joining action
unc0rr
parents: 12144
diff changeset
   186
                ]);
a482c7a5f6e3 Lobby joining action
unc0rr
parents: 12144
diff changeset
   187
        },
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents: 12146
diff changeset
   188
        AddRoom(name, password) => {
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12853
diff changeset
   189
            let room_id = server.add_room();;
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   190
            let actions = {
12148
8591375271b8 Store room protocol number
unc0rr
parents: 12147
diff changeset
   191
                let r = &mut server.rooms[room_id];
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   192
                let c = &mut server.clients[client_id];
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   193
                r.master_id = Some(c.id);
12148
8591375271b8 Store room protocol number
unc0rr
parents: 12147
diff changeset
   194
                r.name = name;
8591375271b8 Store room protocol number
unc0rr
parents: 12147
diff changeset
   195
                r.password = password;
8591375271b8 Store room protocol number
unc0rr
parents: 12147
diff changeset
   196
                r.protocol_number = c.protocol_number;
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   197
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   198
                vec![
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   199
                    RoomAdd(r.info(Some(&c))).send_all()
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   200
                        .with_protocol(r.protocol_number).action(),
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   201
                    MoveToRoom(room_id)]
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   202
            };
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   203
            server.react(client_id, actions);
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   204
        },
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   205
        RemoveRoom(room_id) => {
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   206
            let actions = {
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   207
                let r = &mut server.rooms[room_id];
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   208
                vec![RoomRemove(r.name.clone()).send_all()
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   209
                        .with_protocol(r.protocol_number).action()]
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   210
            };
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   211
            server.rooms.remove(room_id);
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   212
            server.react(client_id, actions);
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   213
        }
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   214
        MoveToRoom(room_id) => {
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   215
            let actions = {
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   216
                let r = &mut server.rooms[room_id];
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   217
                let c = &mut server.clients[client_id];
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   218
                r.players_number += 1;
12148
8591375271b8 Store room protocol number
unc0rr
parents: 12147
diff changeset
   219
                c.room_id = Some(room_id);
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   220
                c.is_joined_mid_game = false;
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   221
                if r.master_id == Some(c.id) {
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   222
                    r.ready_players_number += 1;
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   223
                    c.is_master = true;
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   224
                    c.is_ready = true;
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   225
                } else {
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   226
                    c.is_ready = false;
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   227
                    c.is_master = false;
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   228
                }
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   229
13422
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   230
                let mut v = vec![
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   231
                    RoomJoined(vec![c.nick.clone()]).send_all().in_room(room_id).action(),
13427
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   232
                    ClientFlags("+i".to_string(), vec![c.nick.clone()]).send_all().action(),
13422
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   233
                    SendRoomUpdate(None)];
13477
f748a72432f2 Implement greetings & fixed rooms
alfadur
parents: 13443
diff changeset
   234
                if !r.greeting.is_empty() {
f748a72432f2 Implement greetings & fixed rooms
alfadur
parents: 13443
diff changeset
   235
                    v.push(ChatMsg {nick: "[greeting]".to_string(), msg: r.greeting.clone()}
f748a72432f2 Implement greetings & fixed rooms
alfadur
parents: 13443
diff changeset
   236
                        .send_self().action());
f748a72432f2 Implement greetings & fixed rooms
alfadur
parents: 13443
diff changeset
   237
                }
13422
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   238
                if !c.is_master {
13427
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   239
                    let team_names: Vec<_>;
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   240
                    if let Some(ref mut info) = r.game_info {
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   241
                        c.is_in_game = true;
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   242
                        c.is_joined_mid_game = true;
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   243
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   244
                        {
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   245
                            let teams = info.client_teams(c.id);
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   246
                            c.teams_in_game = teams.clone().count() as u8;
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   247
                            c.clan = teams.clone().next().map(|t| t.color);
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   248
                            team_names = teams.map(|t| t.name.clone()).collect();
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   249
                        }
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   250
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   251
                        if !team_names.is_empty() {
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   252
                            info.left_teams.retain(|name|
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   253
                                !team_names.contains(&name));
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   254
                            info.teams_in_game += team_names.len() as u8;
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   255
                            r.teams = info.teams_at_start.iter()
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   256
                                .filter(|(_, t)| !team_names.contains(&t.name))
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   257
                                .cloned().collect();
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   258
                        }
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   259
                    } else {
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   260
                        team_names = Vec::new();
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   261
                    }
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   262
13426
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
   263
                    v.push(SendRoomData{ to: client_id, teams: true, config: true, flags: true});
13427
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   264
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   265
                    if let Some(ref info) = r.game_info {
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   266
                        v.push(RunGame.send_self().action());
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   267
                        v.push(ClientFlags("+g".to_string(), vec![c.nick.clone()])
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   268
                            .send_all().in_room(r.id).action());
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   269
                        v.push(ForwardEngineMessage(
13428
c8425fbcf1d9 Fix spectators joining midgame
alfadur
parents: 13427
diff changeset
   270
                            vec![to_engine_msg("e$spectate 1".bytes())])
c8425fbcf1d9 Fix spectators joining midgame
alfadur
parents: 13427
diff changeset
   271
                            .send_self().action());
c8425fbcf1d9 Fix spectators joining midgame
alfadur
parents: 13427
diff changeset
   272
                        v.push(ForwardEngineMessage(info.msg_log.clone())
13427
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   273
                            .send_self().action());
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   274
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   275
                        for name in team_names.iter() {
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   276
                            v.push(ForwardEngineMessage(
13428
c8425fbcf1d9 Fix spectators joining midgame
alfadur
parents: 13427
diff changeset
   277
                                vec![to_engine_msg(once(b'G').chain(name.bytes()))])
13427
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   278
                                .send_all().in_room(r.id).action());
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   279
                        }
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   280
                        if info.is_paused {
13428
c8425fbcf1d9 Fix spectators joining midgame
alfadur
parents: 13427
diff changeset
   281
                            v.push(ForwardEngineMessage(vec![to_engine_msg(once(b'I'))])
13427
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   282
                                .send_all().in_room(r.id).action())
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   283
                        }
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   284
                    }
13422
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   285
                }
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   286
                v
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   287
            };
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   288
            server.react(client_id, actions);
13424
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13423
diff changeset
   289
        }
13426
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
   290
        SendRoomData {to, teams, config, flags} => {
13424
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13423
diff changeset
   291
            let mut actions = Vec::new();
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13423
diff changeset
   292
            let room_id = server.clients[client_id].room_id;
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13423
diff changeset
   293
            if let Some(r) = room_id.and_then(|id| server.rooms.get(id)) {
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13423
diff changeset
   294
                if config {
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13423
diff changeset
   295
                    actions.push(ConfigEntry("FULLMAPCONFIG".to_string(), r.map_config())
13426
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
   296
                        .send(to).action());
13424
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13423
diff changeset
   297
                    for cfg in r.game_config().into_iter() {
13439
c4f917c6be51 add missing message tests
alfadur
parents: 13428
diff changeset
   298
                        actions.push(cfg.to_server_msg().send(to).action());
13424
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13423
diff changeset
   299
                    }
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13423
diff changeset
   300
                }
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13423
diff changeset
   301
                if teams {
13427
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   302
                    let current_teams = match r.game_info {
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   303
                        Some(ref info) => &info.teams_at_start,
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   304
                        None => &r.teams
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   305
                    };
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   306
                    for (owner_id, team) in current_teams.iter() {
13424
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13423
diff changeset
   307
                        actions.push(TeamAdd(HWRoom::team_info(&server.clients[*owner_id], &team))
13426
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
   308
                            .send(to).action());
13427
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   309
                        actions.push(TeamColor(team.name.clone(), team.color)
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   310
                            .send(to).action());
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   311
                        actions.push(HedgehogsNumber(team.name.clone(), team.hedgehogs_number)
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   312
                            .send(to).action());
13424
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13423
diff changeset
   313
                    }
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13423
diff changeset
   314
                }
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13423
diff changeset
   315
                if flags {
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13423
diff changeset
   316
                    if let Some(id) = r.master_id {
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13423
diff changeset
   317
                        actions.push(ClientFlags("+h".to_string(), vec![server.clients[id].nick.clone()])
13426
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
   318
                            .send(to).action());
13424
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13423
diff changeset
   319
                    }
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13423
diff changeset
   320
                    let nicks: Vec<_> = server.clients.iter()
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13423
diff changeset
   321
                        .filter(|(_, c)| c.room_id == Some(r.id) && c.is_ready)
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13423
diff changeset
   322
                        .map(|(_, c)| c.nick.clone()).collect();
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13423
diff changeset
   323
                    if !nicks.is_empty() {
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13423
diff changeset
   324
                        actions.push(ClientFlags("+r".to_string(), nicks)
13426
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
   325
                            .send(to).action());
13424
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13423
diff changeset
   326
                    }
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13423
diff changeset
   327
                }
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13423
diff changeset
   328
            }
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13423
diff changeset
   329
            server.react(client_id, actions);
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13423
diff changeset
   330
        }
13478
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   331
        AddVote{vote, is_forced} => {
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   332
            let mut actions = Vec::new();
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   333
            if let (c, Some(r)) = server.client_and_room(client_id) {
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   334
                let mut result = None;
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   335
                if let Some(ref mut voting) = r.voting {
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   336
                    if is_forced || voting.votes.iter().find(|(id, _)| client_id == *id).is_none() {
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   337
                        actions.push(server_chat("Your vote has been counted.").send_self().action());
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   338
                        voting.votes.push((client_id, vote));
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   339
                        let i = voting.votes.iter();
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   340
                        let pro = i.clone().filter(|(_, v)| *v).count();
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   341
                        let contra = i.filter(|(_, v)| !*v).count();
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   342
                        let success_quota = voting.voters.len() / 2 + 1;
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   343
                        if is_forced && vote || pro >= success_quota {
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   344
                            result = Some(true);
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   345
                        } else if is_forced && !vote || contra > voting.voters.len() - success_quota {
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   346
                            result = Some(false);
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   347
                        }
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   348
                    } else {
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   349
                        actions.push(server_chat("You already have voted.").send_self().action());
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   350
                    }
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   351
                } else {
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   352
                    actions.push(server_chat("There's no voting going on.").send_self().action());
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   353
                }
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   354
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   355
                if let Some(res) = result {
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   356
                    actions.push(server_chat("Voting closed.")
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   357
                        .send_all().in_room(r.id).action());
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   358
                    let voting = replace(&mut r.voting, None).unwrap();
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   359
                    if res {
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   360
                        actions.push(ApplyVoting(voting.kind, r.id));
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   361
                    }
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   362
                }
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   363
            }
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   364
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   365
            server.react(client_id, actions);
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   366
        }
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   367
        ApplyVoting(kind, room_id) => {
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   368
            let mut actions = Vec::new();
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   369
            let mut id = client_id;
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   370
            match kind {
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   371
                VoteType::Kick(nick) => {
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   372
                    if let Some(c) = server.find_client(&nick) {
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   373
                        if c.room_id == Some(room_id) {
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   374
                            id = c.id;
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   375
                            actions.push(Kicked.send_self().action());
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   376
                            actions.push(MoveToLobby("kicked".to_string()));
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   377
                        }
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   378
                    }
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   379
                },
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   380
                VoteType::Map(_) => {
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   381
                    unimplemented!();
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   382
                },
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   383
                VoteType::Pause => {
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   384
                    if let Some(ref mut info) = server.room(client_id).unwrap().game_info {
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   385
                        info.is_paused = !info.is_paused;
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   386
                        actions.push(server_chat("Pause toggled.")
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   387
                            .send_all().in_room(room_id).action());
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   388
                        actions.push(ForwardEngineMessage(vec![to_engine_msg(once(b'I'))])
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   389
                            .send_all().in_room(room_id).action());
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   390
                    }
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   391
                },
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   392
                VoteType::NewSeed => {
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   393
                    unimplemented!();
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   394
                },
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   395
                VoteType::HedgehogsPerTeam(number) => {
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   396
                    let r = &mut server.rooms[room_id];
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   397
                    let nicks = r.set_hedgehogs_number(number);
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   398
                    actions.extend(nicks.into_iter().map(|n|
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   399
                        HedgehogsNumber(n, number).send_all().in_room(room_id).action()
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   400
                    ));
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   401
                },
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   402
            }
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   403
            server.react(id, actions);
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   404
        }
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   405
        MoveToLobby(msg) => {
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   406
            let mut actions = Vec::new();
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   407
            let lobby_id = server.lobby_id;
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   408
            if let (c, Some(r)) = server.client_and_room(client_id) {
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   409
                r.players_number -= 1;
13424
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13423
diff changeset
   410
                if c.is_ready && r.ready_players_number > 0 {
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   411
                    r.ready_players_number -= 1;
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   412
                }
13477
f748a72432f2 Implement greetings & fixed rooms
alfadur
parents: 13443
diff changeset
   413
                if c.is_master && (r.players_number > 0 || r.is_fixed) {
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   414
                    actions.push(ChangeMaster(r.id, None));
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   415
                }
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   416
                actions.push(ClientFlags("-i".to_string(), vec![c.nick.clone()])
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   417
                    .send_all().action());
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   418
            }
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   419
            server.react(client_id, actions);
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   420
            actions = Vec::new();
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   421
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   422
            if let (c, Some(r)) = server.client_and_room(client_id) {
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   423
                c.room_id = Some(lobby_id);
13477
f748a72432f2 Implement greetings & fixed rooms
alfadur
parents: 13443
diff changeset
   424
                if r.players_number == 0 && !r.is_fixed {
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   425
                    actions.push(RemoveRoom(r.id));
13477
f748a72432f2 Implement greetings & fixed rooms
alfadur
parents: 13443
diff changeset
   426
                } else {
f748a72432f2 Implement greetings & fixed rooms
alfadur
parents: 13443
diff changeset
   427
                    actions.push(RemoveClientTeams);
f748a72432f2 Implement greetings & fixed rooms
alfadur
parents: 13443
diff changeset
   428
                    actions.push(RoomLeft(c.nick.clone(), msg)
f748a72432f2 Implement greetings & fixed rooms
alfadur
parents: 13443
diff changeset
   429
                        .send_all().in_room(r.id).but_self().action());
f748a72432f2 Implement greetings & fixed rooms
alfadur
parents: 13443
diff changeset
   430
                    actions.push(SendRoomUpdate(Some(r.name.clone())));
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   431
                }
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   432
            }
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   433
            server.react(client_id, actions)
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   434
        }
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   435
        ChangeMaster(room_id, new_id) => {
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   436
            let mut actions = Vec::new();
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   437
            let room_client_ids = server.room_clients(room_id);
13477
f748a72432f2 Implement greetings & fixed rooms
alfadur
parents: 13443
diff changeset
   438
            let new_id = if server.room(client_id).map(|r| r.is_fixed).unwrap_or(false) {
f748a72432f2 Implement greetings & fixed rooms
alfadur
parents: 13443
diff changeset
   439
                new_id
f748a72432f2 Implement greetings & fixed rooms
alfadur
parents: 13443
diff changeset
   440
            } else {
f748a72432f2 Implement greetings & fixed rooms
alfadur
parents: 13443
diff changeset
   441
                new_id.or_else(||
f748a72432f2 Implement greetings & fixed rooms
alfadur
parents: 13443
diff changeset
   442
                    room_client_ids.iter().find(|id| **id != client_id).map(|id| *id))
f748a72432f2 Implement greetings & fixed rooms
alfadur
parents: 13443
diff changeset
   443
            };
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   444
            let new_nick = new_id.map(|id| server.clients[id].nick.clone());
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   445
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   446
            if let (c, Some(r)) = server.client_and_room(client_id) {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   447
                match r.master_id {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   448
                    Some(id) if id == c.id => {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   449
                        c.is_master = false;
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   450
                        r.master_id = None;
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   451
                        actions.push(ClientFlags("-h".to_string(), vec![c.nick.clone()])
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   452
                            .send_all().in_room(r.id).action());
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   453
                    }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   454
                    Some(_) => unreachable!(),
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   455
                    None => {}
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   456
                }
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   457
                r.master_id = new_id;
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   458
                if let Some(nick) = new_nick {
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   459
                    actions.push(ClientFlags("+h".to_string(), vec![nick])
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   460
                        .send_all().in_room(r.id).action());
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   461
                }
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   462
            }
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   463
            new_id.map(|id| server.clients[id].is_master = true);
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   464
            server.react(client_id, actions);
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   465
        }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   466
        RemoveTeam(name) => {
13426
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
   467
            let mut actions = Vec::new();
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
   468
            if let (c, Some(r)) = server.client_and_room(client_id) {
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   469
                r.remove_team(&name);
13426
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
   470
                if let Some(ref mut info) = r.game_info {
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
   471
                    info.left_teams.push(name.clone());
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
   472
                }
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
   473
                actions.push(TeamRemove(name.clone()).send_all().in_room(r.id).action());
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
   474
                actions.push(SendRoomUpdate(None));
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
   475
                if r.game_info.is_some() && c.is_in_game {
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
   476
                    actions.push(SendTeamRemovalMessage(name));
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
   477
                }
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
   478
            }
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   479
            server.react(client_id, actions);
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   480
        },
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   481
        RemoveClientTeams => {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   482
            let actions = if let (c, Some(r)) = server.client_and_room(client_id) {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   483
                r.client_teams(c.id).map(|t| RemoveTeam(t.name.clone())).collect()
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   484
            } else {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   485
                Vec::new()
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   486
            };
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   487
            server.react(client_id, actions);
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   488
        }
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   489
        SendRoomUpdate(old_name) => {
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   490
            let actions = if let (c, Some(r)) = server.client_and_room(client_id) {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   491
                let name = old_name.unwrap_or_else(|| r.name.clone());
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   492
                vec![RoomUpdated(name, r.info(Some(&c)))
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   493
                    .send_all().with_protocol(r.protocol_number).action()]
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   494
            } else {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   495
                Vec::new()
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   496
            };
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   497
            server.react(client_id, actions);
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   498
        },
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   499
        StartRoomGame(room_id) => {
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   500
            let actions = {
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   501
                let (room_clients, room_nicks): (Vec<_>, Vec<_>) = server.clients.iter()
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   502
                    .map(|(id, c)| (id, c.nick.clone())).unzip();
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   503
                let room = &mut server.rooms[room_id];
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   504
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   505
                if !room.has_multiple_clans() {
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   506
                    vec![Warn("The game can't be started with less than two clans!".to_string())]
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   507
                } else if room.game_info.is_some() {
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   508
                    vec![Warn("The game is already in progress".to_string())]
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   509
                } else {
13427
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   510
                    room.start_round();
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   511
                    for id in room_clients {
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   512
                        let c = &mut server.clients[id];
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   513
                        c.is_in_game = true;
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   514
                        c.team_indices = room.client_team_indices(c.id);
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   515
                    }
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   516
                    vec![RunGame.send_all().in_room(room.id).action(),
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   517
                         SendRoomUpdate(None),
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   518
                         ClientFlags("+g".to_string(), room_nicks)
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   519
                             .send_all().in_room(room.id).action()]
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   520
                }
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   521
            };
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   522
            server.react(client_id, actions);
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   523
        }
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   524
        SendTeamRemovalMessage(team_name) => {
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   525
            let mut actions = Vec::new();
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   526
            if let (c, Some(r)) = server.client_and_room(client_id) {
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   527
                if let Some(ref mut info) = r.game_info {
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   528
                    let msg = once(b'F').chain(team_name.bytes());
13428
c8425fbcf1d9 Fix spectators joining midgame
alfadur
parents: 13427
diff changeset
   529
                    actions.push(ForwardEngineMessage(vec![to_engine_msg(msg)]).
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   530
                        send_all().in_room(r.id).but_self().action());
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   531
                    info.teams_in_game -= 1;
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   532
                    if info.teams_in_game == 0 {
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   533
                        actions.push(FinishRoomGame(r.id));
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   534
                    }
13427
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   535
                    let remove_msg = to_engine_msg(once(b'F').chain(team_name.bytes()));
13443
2501428303a2 Fix team remove synchronization
alfadur
parents: 13439
diff changeset
   536
                    if let Some(m) = &info.sync_msg {
2501428303a2 Fix team remove synchronization
alfadur
parents: 13439
diff changeset
   537
                        info.msg_log.push(m.clone());
13427
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   538
                    }
13443
2501428303a2 Fix team remove synchronization
alfadur
parents: 13439
diff changeset
   539
                    if info.sync_msg.is_some() {
2501428303a2 Fix team remove synchronization
alfadur
parents: 13439
diff changeset
   540
                        info.sync_msg = None
2501428303a2 Fix team remove synchronization
alfadur
parents: 13439
diff changeset
   541
                    }
2501428303a2 Fix team remove synchronization
alfadur
parents: 13439
diff changeset
   542
                    info.msg_log.push(remove_msg.clone());
13428
c8425fbcf1d9 Fix spectators joining midgame
alfadur
parents: 13427
diff changeset
   543
                    actions.push(ForwardEngineMessage(vec![remove_msg])
13427
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   544
                        .send_all().in_room(r.id).but_self().action());
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   545
                }
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   546
            }
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   547
            server.react(client_id, actions);
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   548
        }
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   549
        FinishRoomGame(room_id) => {
13426
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
   550
            let mut actions = Vec::new();
13478
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   551
            let old_info;
13426
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
   552
            {
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   553
                let r = &mut server.rooms[room_id];
13478
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   554
                old_info = replace(&mut r.game_info, None);
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   555
                r.game_info = None;
13426
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
   556
                r.ready_players_number = 1;
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
   557
                actions.push(SendRoomUpdate(None));
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
   558
                actions.push(RoundFinished.send_all().in_room(r.id).action());
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
   559
            }
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
   560
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
   561
            if let Some(info) = old_info {
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
   562
                for (_, c) in server.clients.iter() {
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
   563
                    if c.room_id == Some(room_id) && c.is_joined_mid_game {
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
   564
                        actions.push(SendRoomData{
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
   565
                            to: c.id, teams: false,
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
   566
                            config: true, flags: false});
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
   567
                        for name in info.left_teams.iter() {
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
   568
                            actions.push(TeamRemove(name.clone())
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
   569
                                .send(c.id).action());
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
   570
                        }
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
   571
                    }
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
   572
                }
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
   573
            }
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
   574
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
   575
            let nicks: Vec<_> = server.clients.iter_mut()
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
   576
                .filter(|(_, c)| c.room_id == Some(room_id))
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
   577
                .map(|(_, c)| {
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
   578
                    c.is_ready = c.is_master;
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
   579
                    c.is_joined_mid_game = false;
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
   580
                    c
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
   581
                }).filter_map(|c| if !c.is_master {
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
   582
                    Some(c.nick.clone())
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
   583
                } else {
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
   584
                    None
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
   585
                }).collect();
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
   586
            if !nicks.is_empty() {
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
   587
                actions.push(ClientFlags("-r".to_string(), nicks)
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
   588
                    .send_all().in_room(room_id).action());
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
   589
            }
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   590
            server.react(client_id, actions);
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   591
        }
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents: 12146
diff changeset
   592
        Warn(msg) => {
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   593
            run_action(server, client_id, Warning(msg).send_self().action());
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents: 12146
diff changeset
   594
        }
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   595
        ProtocolError(msg) => {
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   596
            run_action(server, client_id, Error(msg).send_self().action())
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   597
        }
12144
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
   598
    }
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
   599
}