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