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