rust/hedgewars-server/src/server/actions.rs
author alfadur <mail@none>
Sat, 02 Feb 2019 15:06:39 +0300
changeset 14671 455865ccd36c
parent 14504 6cc0fce249f9
child 14673 08a8605bafaf
permissions -rw-r--r--
Server action refactoring part 2 of N
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
     1
use super::{
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
     2
    client::HWClient,
14374
e5db279308d7 dispose of server mods
alfadur
parents: 14350
diff changeset
     3
    core::HWServer,
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
     4
    coretypes::{ClientId, GameCfg, RoomId, VoteType},
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
     5
    handlers,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
     6
    room::HWRoom,
13523
8c5dd562c9f7 Add room flags
alfadur
parents: 13520
diff changeset
     7
    room::{GameInfo, RoomFlags},
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
     8
};
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13527
diff changeset
     9
use crate::{
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    10
    protocol::messages::{server_chat, HWProtocolMessage, HWServerMessage, HWServerMessage::*},
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    11
    utils::to_engine_msg,
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    12
};
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    13
use rand::{distributions::Uniform, thread_rng, Rng};
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    14
use std::{io, io::Write, iter::once, mem::replace};
12138
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents:
diff changeset
    15
14456
a077aac9df01 Start database interaction implementation
unc0rr
parents: 14415
diff changeset
    16
#[cfg(feature = "official-server")]
a077aac9df01 Start database interaction implementation
unc0rr
parents: 14415
diff changeset
    17
use super::database;
a077aac9df01 Start database interaction implementation
unc0rr
parents: 14415
diff changeset
    18
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    19
pub enum Destination {
13426
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
    20
    ToId(ClientId),
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    21
    ToSelf,
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    22
    ToAll {
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    23
        room_id: Option<RoomId>,
13520
1ee192f13456 Better packing for clients
alfadur
parents: 13480
diff changeset
    24
        protocol: Option<u16>,
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    25
        skip_self: bool,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    26
    },
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    27
}
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    28
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    29
pub struct PendingMessage {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    30
    pub destination: Destination,
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    31
    pub message: HWServerMessage,
13419
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
impl PendingMessage {
13426
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
    35
    pub fn send(message: HWServerMessage, client_id: ClientId) -> PendingMessage {
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    36
        PendingMessage {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    37
            destination: Destination::ToId(client_id),
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    38
            message,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    39
        }
13426
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
    40
    }
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
    41
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    42
    pub fn send_self(message: HWServerMessage) -> PendingMessage {
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    43
        PendingMessage {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    44
            destination: Destination::ToSelf,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    45
            message,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    46
        }
13419
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
        };
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    55
        PendingMessage {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    56
            destination,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    57
            message,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    58
        }
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    59
    }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    60
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    61
    pub fn in_room(mut self, clients_room_id: RoomId) -> PendingMessage {
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    62
        if let Destination::ToAll {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    63
            ref mut room_id, ..
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    64
        } = self.destination
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    65
        {
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    66
            *room_id = Some(clients_room_id)
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    67
        }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    68
        self
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    69
    }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    70
13520
1ee192f13456 Better packing for clients
alfadur
parents: 13480
diff changeset
    71
    pub fn with_protocol(mut self, protocol_number: u16) -> PendingMessage {
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    72
        if let Destination::ToAll {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    73
            ref mut protocol, ..
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    74
        } = self.destination
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    75
        {
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    76
            *protocol = Some(protocol_number)
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    77
        }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    78
        self
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    79
    }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    80
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    81
    pub fn but_self(mut self) -> PendingMessage {
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    82
        if let Destination::ToAll {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    83
            ref mut skip_self, ..
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    84
        } = self.destination
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    85
        {
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    86
            *skip_self = true
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    87
        }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    88
        self
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    89
    }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    90
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    91
    pub fn action(self) -> Action {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    92
        Send(self)
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    93
    }
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    94
}
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    95
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    96
impl Into<Action> for PendingMessage {
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    97
    fn into(self) -> Action {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    98
        self.action()
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    99
    }
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   100
}
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   101
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   102
impl HWServerMessage {
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   103
    pub fn send(self, client_id: ClientId) -> PendingMessage {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   104
        PendingMessage::send(self, client_id)
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   105
    }
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   106
    pub fn send_self(self) -> PendingMessage {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   107
        PendingMessage::send_self(self)
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   108
    }
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   109
    pub fn send_all(self) -> PendingMessage {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   110
        PendingMessage::send_all(self)
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   111
    }
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   112
}
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   113
12138
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents:
diff changeset
   114
pub enum Action {
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   115
    Send(PendingMessage),
12139
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
   116
    RemoveClient,
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
   117
    ByeClient(String),
12145
a482c7a5f6e3 Lobby joining action
unc0rr
parents: 12144
diff changeset
   118
    CheckRegistered,
a482c7a5f6e3 Lobby joining action
unc0rr
parents: 12144
diff changeset
   119
    JoinLobby,
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   120
    RemoveRoom(RoomId),
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   121
    MoveToRoom(RoomId),
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   122
    MoveToLobby(String),
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   123
    ChangeMaster(RoomId, Option<ClientId>),
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   124
    RemoveTeam(String),
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   125
    RemoveClientTeams,
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   126
    SendRoomUpdate(Option<String>),
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   127
    StartRoomGame(RoomId),
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   128
    SendTeamRemovalMessage(String),
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   129
    FinishRoomGame(RoomId),
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   130
    SendRoomData {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   131
        to: ClientId,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   132
        teams: bool,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   133
        config: bool,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   134
        flags: bool,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   135
    },
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   136
    AddVote {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   137
        vote: bool,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   138
        is_forced: bool,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   139
    },
13478
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   140
    ApplyVoting(VoteType, RoomId),
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents: 12146
diff changeset
   141
    Warn(String),
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   142
    ProtocolError(String),
12138
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents:
diff changeset
   143
}
12144
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
   144
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
   145
use self::Action::*;
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
   146
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   147
pub fn run_action(server: &mut HWServer, client_id: usize, action: Action) {
12144
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
   148
    match action {
13524
5359ff75da3a indulge clippy
alfadur
parents: 13523
diff changeset
   149
        Send(msg) => server.send(client_id, &msg.destination, msg.message),
12144
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
   150
        ByeClient(msg) => {
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13527
diff changeset
   151
            let c = &server.clients[client_id];
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13527
diff changeset
   152
            let nick = c.nick.clone();
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   153
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   154
            if let Some(id) = c.room_id {
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   155
                if id != server.lobby_id {
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   156
                    server.react(
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   157
                        client_id,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   158
                        vec![MoveToLobby(format!("quit: {}", msg.clone()))],
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   159
                    );
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   160
                }
13524
5359ff75da3a indulge clippy
alfadur
parents: 13523
diff changeset
   161
            }
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   162
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   163
            server.react(
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   164
                client_id,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   165
                vec![
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   166
                    LobbyLeft(nick, msg.clone()).send_all().action(),
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   167
                    Bye(msg).send_self().action(),
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   168
                    RemoveClient,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   169
                ],
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   170
            );
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   171
        }
12144
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
   172
        RemoveClient => {
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   173
            server.removed_clients.push(client_id);
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   174
            if server.clients.contains(client_id) {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   175
                server.clients.remove(client_id);
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12853
diff changeset
   176
            }
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   177
        }
13801
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13666
diff changeset
   178
        CheckRegistered => {
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13666
diff changeset
   179
            let client = &server.clients[client_id];
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13666
diff changeset
   180
            if client.protocol_number > 0 && client.nick != "" {
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   181
                let has_nick_clash = server
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   182
                    .clients
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   183
                    .iter()
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   184
                    .any(|(id, c)| id != client_id && c.nick == client.nick);
13801
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13666
diff changeset
   185
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13666
diff changeset
   186
                let actions = if !client.is_checker() && has_nick_clash {
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13666
diff changeset
   187
                    if client.protocol_number < 38 {
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13666
diff changeset
   188
                        vec![ByeClient("Nickname is already in use".to_string())]
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13666
diff changeset
   189
                    } else {
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13666
diff changeset
   190
                        server.clients[client_id].nick.clear();
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13666
diff changeset
   191
                        vec![Notice("NickAlreadyInUse".to_string()).send_self().action()]
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13666
diff changeset
   192
                    }
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13666
diff changeset
   193
                } else {
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13666
diff changeset
   194
                    vec![JoinLobby]
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13666
diff changeset
   195
                };
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13666
diff changeset
   196
                server.react(client_id, actions);
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13666
diff changeset
   197
            }
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   198
        }
12146
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12145
diff changeset
   199
        JoinLobby => {
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   200
            server.clients[client_id].room_id = Some(server.lobby_id);
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents: 12146
diff changeset
   201
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13527
diff changeset
   202
            let mut lobby_nicks = Vec::new();
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13527
diff changeset
   203
            for (_, c) in server.clients.iter() {
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13527
diff changeset
   204
                if c.room_id.is_some() {
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13527
diff changeset
   205
                    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
   206
                }
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12145
diff changeset
   207
            }
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13527
diff changeset
   208
            let joined_msg = LobbyJoined(lobby_nicks);
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13527
diff changeset
   209
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   210
            let everyone_msg = LobbyJoined(vec![server.clients[client_id].nick.clone()]);
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   211
            let flags_msg = ClientFlags(
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   212
                "+i".to_string(),
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   213
                server
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   214
                    .clients
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   215
                    .iter()
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   216
                    .filter(|(_, c)| c.room_id.is_some())
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   217
                    .map(|(_, c)| c.nick.clone())
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   218
                    .collect(),
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   219
            );
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   220
            let server_msg = ServerMessage("\u{1f994} is watching".to_string());
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   221
            let rooms_msg = Rooms(
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   222
                server
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   223
                    .rooms
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   224
                    .iter()
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   225
                    .filter(|(id, _)| *id != server.lobby_id)
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   226
                    .flat_map(|(_, r)| r.info(r.master_id.map(|id| &server.clients[id])))
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   227
                    .collect(),
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   228
            );
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   229
            server.react(
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   230
                client_id,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   231
                vec![
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   232
                    everyone_msg.send_all().but_self().action(),
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   233
                    joined_msg.send_self().action(),
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   234
                    flags_msg.send_self().action(),
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   235
                    server_msg.send_self().action(),
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   236
                    rooms_msg.send_self().action(),
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   237
                ],
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   238
            );
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   239
        }
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   240
        RemoveRoom(room_id) => {
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13527
diff changeset
   241
            let r = &mut server.rooms[room_id];
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   242
            let actions = vec![RoomRemove(r.name.clone())
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   243
                .send_all()
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   244
                .with_protocol(r.protocol_number)
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   245
                .action()];
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   246
            server.rooms.remove(room_id);
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   247
            server.react(client_id, actions);
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   248
        }
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   249
        MoveToRoom(room_id) => {
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13527
diff changeset
   250
            let r = &mut server.rooms[room_id];
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13527
diff changeset
   251
            let c = &mut server.clients[client_id];
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13527
diff changeset
   252
            r.players_number += 1;
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13527
diff changeset
   253
            c.room_id = Some(room_id);
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13527
diff changeset
   254
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13527
diff changeset
   255
            let is_master = r.master_id == Some(c.id);
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13527
diff changeset
   256
            c.set_is_master(is_master);
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13527
diff changeset
   257
            c.set_is_ready(is_master);
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13527
diff changeset
   258
            c.set_is_joined_mid_game(false);
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13527
diff changeset
   259
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13527
diff changeset
   260
            if is_master {
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13527
diff changeset
   261
                r.ready_players_number += 1;
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13527
diff changeset
   262
            }
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13527
diff changeset
   263
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13527
diff changeset
   264
            let mut v = vec![
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   265
                RoomJoined(vec![c.nick.clone()])
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   266
                    .send_all()
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   267
                    .in_room(room_id)
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   268
                    .action(),
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   269
                ClientFlags("+i".to_string(), vec![c.nick.clone()])
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   270
                    .send_all()
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   271
                    .action(),
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   272
                SendRoomUpdate(None),
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   273
            ];
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13527
diff changeset
   274
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13527
diff changeset
   275
            if !r.greeting.is_empty() {
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   276
                v.push(
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   277
                    ChatMsg {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   278
                        nick: "[greeting]".to_string(),
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   279
                        msg: r.greeting.clone(),
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   280
                    }
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   281
                    .send_self()
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   282
                    .action(),
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   283
                );
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13527
diff changeset
   284
            }
13520
1ee192f13456 Better packing for clients
alfadur
parents: 13480
diff changeset
   285
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13527
diff changeset
   286
            if !c.is_master() {
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13527
diff changeset
   287
                let team_names: Vec<_>;
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13527
diff changeset
   288
                if let Some(ref mut info) = r.game_info {
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13527
diff changeset
   289
                    c.set_is_in_game(true);
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13527
diff changeset
   290
                    c.set_is_joined_mid_game(true);
13520
1ee192f13456 Better packing for clients
alfadur
parents: 13480
diff changeset
   291
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13527
diff changeset
   292
                    {
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13527
diff changeset
   293
                        let teams = info.client_teams(c.id);
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13527
diff changeset
   294
                        c.teams_in_game = teams.clone().count() as u8;
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13527
diff changeset
   295
                        c.clan = teams.clone().next().map(|t| t.color);
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13527
diff changeset
   296
                        team_names = teams.map(|t| t.name.clone()).collect();
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13527
diff changeset
   297
                    }
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13527
diff changeset
   298
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13527
diff changeset
   299
                    if !team_names.is_empty() {
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   300
                        info.left_teams.retain(|name| !team_names.contains(&name));
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13527
diff changeset
   301
                        info.teams_in_game += team_names.len() as u8;
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   302
                        r.teams = info
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   303
                            .teams_at_start
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   304
                            .iter()
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13527
diff changeset
   305
                            .filter(|(_, t)| !team_names.contains(&t.name))
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   306
                            .cloned()
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   307
                            .collect();
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13527
diff changeset
   308
                    }
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13527
diff changeset
   309
                } else {
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13527
diff changeset
   310
                    team_names = Vec::new();
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   311
                }
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   312
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   313
                v.push(SendRoomData {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   314
                    to: client_id,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   315
                    teams: true,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   316
                    config: true,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   317
                    flags: true,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   318
                });
13427
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   319
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13527
diff changeset
   320
                if let Some(ref info) = r.game_info {
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13527
diff changeset
   321
                    v.push(RunGame.send_self().action());
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   322
                    v.push(
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   323
                        ClientFlags("+g".to_string(), vec![c.nick.clone()])
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   324
                            .send_all()
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   325
                            .in_room(r.id)
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   326
                            .action(),
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   327
                    );
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   328
                    v.push(
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   329
                        ForwardEngineMessage(vec![to_engine_msg("e$spectate 1".bytes())])
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   330
                            .send_self()
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   331
                            .action(),
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   332
                    );
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   333
                    v.push(
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   334
                        ForwardEngineMessage(info.msg_log.clone())
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   335
                            .send_self()
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   336
                            .action(),
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   337
                    );
13427
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   338
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13527
diff changeset
   339
                    for name in &team_names {
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   340
                        v.push(
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   341
                            ForwardEngineMessage(vec![to_engine_msg(
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   342
                                once(b'G').chain(name.bytes()),
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   343
                            )])
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   344
                            .send_all()
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   345
                            .in_room(r.id)
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   346
                            .action(),
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   347
                        );
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13527
diff changeset
   348
                    }
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13527
diff changeset
   349
                    if info.is_paused {
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   350
                        v.push(
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   351
                            ForwardEngineMessage(vec![to_engine_msg(once(b'I'))])
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   352
                                .send_all()
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   353
                                .in_room(r.id)
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   354
                                .action(),
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   355
                        )
13427
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   356
                    }
13422
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   357
                }
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13527
diff changeset
   358
            }
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13527
diff changeset
   359
            server.react(client_id, v);
13424
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13423
diff changeset
   360
        }
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   361
        SendRoomData {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   362
            to,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   363
            teams,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   364
            config,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   365
            flags,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   366
        } => {
13424
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13423
diff changeset
   367
            let mut actions = Vec::new();
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13423
diff changeset
   368
            let room_id = server.clients[client_id].room_id;
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13423
diff changeset
   369
            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
   370
                if config {
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   371
                    actions.push(
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   372
                        ConfigEntry("FULLMAPCONFIG".to_string(), r.map_config())
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   373
                            .send(to)
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   374
                            .action(),
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   375
                    );
13524
5359ff75da3a indulge clippy
alfadur
parents: 13523
diff changeset
   376
                    for cfg in r.game_config() {
13439
c4f917c6be51 add missing message tests
alfadur
parents: 13428
diff changeset
   377
                        actions.push(cfg.to_server_msg().send(to).action());
13424
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13423
diff changeset
   378
                    }
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13423
diff changeset
   379
                }
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13423
diff changeset
   380
                if teams {
13427
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   381
                    let current_teams = match r.game_info {
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   382
                        Some(ref info) => &info.teams_at_start,
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   383
                        None => &r.teams,
13427
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   384
                    };
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   385
                    for (owner_id, team) in current_teams.iter() {
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   386
                        actions.push(
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   387
                            TeamAdd(HWRoom::team_info(&server.clients[*owner_id], &team))
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   388
                                .send(to)
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   389
                                .action(),
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   390
                        );
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   391
                        actions.push(TeamColor(team.name.clone(), team.color).send(to).action());
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   392
                        actions.push(
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   393
                            HedgehogsNumber(team.name.clone(), team.hedgehogs_number)
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   394
                                .send(to)
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   395
                                .action(),
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   396
                        );
13424
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13423
diff changeset
   397
                    }
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13423
diff changeset
   398
                }
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13423
diff changeset
   399
                if flags {
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13423
diff changeset
   400
                    if let Some(id) = r.master_id {
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   401
                        actions.push(
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   402
                            ClientFlags("+h".to_string(), vec![server.clients[id].nick.clone()])
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   403
                                .send(to)
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   404
                                .action(),
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   405
                        );
13424
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13423
diff changeset
   406
                    }
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   407
                    let nicks: Vec<_> = server
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   408
                        .clients
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   409
                        .iter()
13520
1ee192f13456 Better packing for clients
alfadur
parents: 13480
diff changeset
   410
                        .filter(|(_, c)| c.room_id == Some(r.id) && c.is_ready())
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   411
                        .map(|(_, c)| c.nick.clone())
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   412
                        .collect();
13424
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13423
diff changeset
   413
                    if !nicks.is_empty() {
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   414
                        actions.push(ClientFlags("+r".to_string(), nicks).send(to).action());
13424
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13423
diff changeset
   415
                    }
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13423
diff changeset
   416
                }
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13423
diff changeset
   417
            }
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13423
diff changeset
   418
            server.react(client_id, actions);
d8354cb98b98 Send teams&flags on entering a room
alfadur
parents: 13423
diff changeset
   419
        }
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   420
        AddVote { vote, is_forced } => {
13478
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   421
            let mut actions = Vec::new();
13527
e3ae9eea0689 Implement map voting
alfadur
parents: 13524
diff changeset
   422
            if let Some(r) = server.room(client_id) {
13478
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   423
                let mut result = None;
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   424
                if let Some(ref mut voting) = r.voting {
14350
31717e1436cd recruit some newly stabilized functions
alfadur
parents: 13801
diff changeset
   425
                    if is_forced || voting.votes.iter().all(|(id, _)| client_id != *id) {
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   426
                        actions.push(
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   427
                            server_chat("Your vote has been counted.".to_string())
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   428
                                .send_self()
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   429
                                .action(),
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   430
                        );
13478
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   431
                        voting.votes.push((client_id, vote));
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   432
                        let i = voting.votes.iter();
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   433
                        let pro = i.clone().filter(|(_, v)| *v).count();
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   434
                        let contra = i.filter(|(_, v)| !*v).count();
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   435
                        let success_quota = voting.voters.len() / 2 + 1;
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   436
                        if is_forced && vote || pro >= success_quota {
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   437
                            result = Some(true);
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   438
                        } else if is_forced && !vote || contra > voting.voters.len() - success_quota
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   439
                        {
13478
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   440
                            result = Some(false);
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   441
                        }
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   442
                    } else {
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   443
                        actions.push(
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   444
                            server_chat("You already have voted.".to_string())
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   445
                                .send_self()
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   446
                                .action(),
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   447
                        );
13478
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   448
                    }
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   449
                } else {
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   450
                    actions.push(
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   451
                        server_chat("There's no voting going on.".to_string())
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   452
                            .send_self()
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   453
                            .action(),
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   454
                    );
13478
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   455
                }
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   456
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   457
                if let Some(res) = result {
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   458
                    actions.push(
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   459
                        server_chat("Voting closed.".to_string())
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   460
                            .send_all()
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   461
                            .in_room(r.id)
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   462
                            .action(),
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   463
                    );
13478
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   464
                    let voting = replace(&mut r.voting, None).unwrap();
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   465
                    if res {
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   466
                        actions.push(ApplyVoting(voting.kind, r.id));
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   467
                    }
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   468
                }
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   469
            }
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   470
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   471
            server.react(client_id, actions);
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   472
        }
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   473
        ApplyVoting(kind, room_id) => {
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   474
            let mut actions = Vec::new();
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   475
            let mut id = client_id;
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   476
            match kind {
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   477
                VoteType::Kick(nick) => {
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   478
                    if let Some(c) = server.find_client(&nick) {
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   479
                        if c.room_id == Some(room_id) {
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   480
                            id = c.id;
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   481
                            actions.push(Kicked.send_self().action());
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   482
                            actions.push(MoveToLobby("kicked".to_string()));
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   483
                        }
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   484
                    }
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   485
                }
13527
e3ae9eea0689 Implement map voting
alfadur
parents: 13524
diff changeset
   486
                VoteType::Map(None) => (),
e3ae9eea0689 Implement map voting
alfadur
parents: 13524
diff changeset
   487
                VoteType::Map(Some(name)) => {
e3ae9eea0689 Implement map voting
alfadur
parents: 13524
diff changeset
   488
                    if let Some(location) = server.rooms[room_id].load_config(&name) {
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   489
                        actions.push(
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   490
                            server_chat(location.to_string())
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   491
                                .send_all()
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   492
                                .in_room(room_id)
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   493
                                .action(),
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   494
                        );
13527
e3ae9eea0689 Implement map voting
alfadur
parents: 13524
diff changeset
   495
                        actions.push(SendRoomUpdate(None));
e3ae9eea0689 Implement map voting
alfadur
parents: 13524
diff changeset
   496
                        for (_, c) in server.clients.iter() {
e3ae9eea0689 Implement map voting
alfadur
parents: 13524
diff changeset
   497
                            if c.room_id == Some(room_id) {
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   498
                                actions.push(SendRoomData {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   499
                                    to: c.id,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   500
                                    teams: false,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   501
                                    config: true,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   502
                                    flags: false,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   503
                                })
13527
e3ae9eea0689 Implement map voting
alfadur
parents: 13524
diff changeset
   504
                            }
e3ae9eea0689 Implement map voting
alfadur
parents: 13524
diff changeset
   505
                        }
e3ae9eea0689 Implement map voting
alfadur
parents: 13524
diff changeset
   506
                    }
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   507
                }
13478
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   508
                VoteType::Pause => {
13480
fb37745c5bca complete newseed voting
alfadur
parents: 13478
diff changeset
   509
                    if let Some(ref mut info) = server.rooms[room_id].game_info {
13478
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   510
                        info.is_paused = !info.is_paused;
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   511
                        actions.push(
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   512
                            server_chat("Pause toggled.".to_string())
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   513
                                .send_all()
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   514
                                .in_room(room_id)
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   515
                                .action(),
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   516
                        );
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   517
                        actions.push(
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   518
                            ForwardEngineMessage(vec![to_engine_msg(once(b'I'))])
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   519
                                .send_all()
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   520
                                .in_room(room_id)
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   521
                                .action(),
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   522
                        );
13478
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   523
                    }
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   524
                }
13478
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   525
                VoteType::NewSeed => {
13480
fb37745c5bca complete newseed voting
alfadur
parents: 13478
diff changeset
   526
                    let seed = thread_rng().gen_range(0, 1_000_000_000).to_string();
fb37745c5bca complete newseed voting
alfadur
parents: 13478
diff changeset
   527
                    let cfg = GameCfg::Seed(seed);
fb37745c5bca complete newseed voting
alfadur
parents: 13478
diff changeset
   528
                    actions.push(cfg.to_server_msg().send_all().in_room(room_id).action());
fb37745c5bca complete newseed voting
alfadur
parents: 13478
diff changeset
   529
                    server.rooms[room_id].set_config(cfg);
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   530
                }
13478
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   531
                VoteType::HedgehogsPerTeam(number) => {
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   532
                    let r = &mut server.rooms[room_id];
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   533
                    let nicks = r.set_hedgehogs_number(number);
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   534
                    actions.extend(nicks.into_iter().map(|n| {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   535
                        HedgehogsNumber(n, number)
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   536
                            .send_all()
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   537
                            .in_room(room_id)
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   538
                            .action()
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   539
                    }));
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   540
                }
13478
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   541
            }
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   542
            server.react(id, actions);
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   543
        }
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   544
        MoveToLobby(msg) => {
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   545
            let mut actions = Vec::new();
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   546
            let lobby_id = server.lobby_id;
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   547
            if let (c, Some(r)) = server.client_and_room(client_id) {
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   548
                r.players_number -= 1;
13520
1ee192f13456 Better packing for clients
alfadur
parents: 13480
diff changeset
   549
                if c.is_ready() && r.ready_players_number > 0 {
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   550
                    r.ready_players_number -= 1;
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   551
                }
13523
8c5dd562c9f7 Add room flags
alfadur
parents: 13520
diff changeset
   552
                if c.is_master() && (r.players_number > 0 || r.is_fixed()) {
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   553
                    actions.push(ChangeMaster(r.id, None));
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   554
                }
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   555
                actions.push(
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   556
                    ClientFlags("-i".to_string(), vec![c.nick.clone()])
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   557
                        .send_all()
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   558
                        .action(),
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   559
                );
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   560
            }
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   561
            server.react(client_id, actions);
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   562
            actions = Vec::new();
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   563
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   564
            if let (c, Some(r)) = server.client_and_room(client_id) {
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   565
                c.room_id = Some(lobby_id);
13523
8c5dd562c9f7 Add room flags
alfadur
parents: 13520
diff changeset
   566
                if r.players_number == 0 && !r.is_fixed() {
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   567
                    actions.push(RemoveRoom(r.id));
13477
f748a72432f2 Implement greetings & fixed rooms
alfadur
parents: 13443
diff changeset
   568
                } else {
f748a72432f2 Implement greetings & fixed rooms
alfadur
parents: 13443
diff changeset
   569
                    actions.push(RemoveClientTeams);
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   570
                    actions.push(
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   571
                        RoomLeft(c.nick.clone(), msg)
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   572
                            .send_all()
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   573
                            .in_room(r.id)
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   574
                            .but_self()
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   575
                            .action(),
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   576
                    );
13477
f748a72432f2 Implement greetings & fixed rooms
alfadur
parents: 13443
diff changeset
   577
                    actions.push(SendRoomUpdate(Some(r.name.clone())));
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   578
                }
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   579
            }
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   580
            server.react(client_id, actions)
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   581
        }
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   582
        ChangeMaster(room_id, new_id) => {
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   583
            let mut actions = Vec::new();
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   584
            let room_client_ids = server.room_clients(room_id);
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   585
            let new_id = if server
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   586
                .room(client_id)
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   587
                .map(|r| r.is_fixed())
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   588
                .unwrap_or(false)
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   589
            {
13477
f748a72432f2 Implement greetings & fixed rooms
alfadur
parents: 13443
diff changeset
   590
                new_id
f748a72432f2 Implement greetings & fixed rooms
alfadur
parents: 13443
diff changeset
   591
            } else {
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   592
                new_id.or_else(|| room_client_ids.iter().find(|id| **id != client_id).cloned())
13477
f748a72432f2 Implement greetings & fixed rooms
alfadur
parents: 13443
diff changeset
   593
            };
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   594
            let new_nick = new_id.map(|id| server.clients[id].nick.clone());
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   595
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   596
            if let (c, Some(r)) = server.client_and_room(client_id) {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   597
                match r.master_id {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   598
                    Some(id) if id == c.id => {
13520
1ee192f13456 Better packing for clients
alfadur
parents: 13480
diff changeset
   599
                        c.set_is_master(false);
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   600
                        r.master_id = None;
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   601
                        actions.push(
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   602
                            ClientFlags("-h".to_string(), vec![c.nick.clone()])
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   603
                                .send_all()
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   604
                                .in_room(r.id)
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   605
                                .action(),
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   606
                        );
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   607
                    }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   608
                    Some(_) => unreachable!(),
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   609
                    None => {}
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   610
                }
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   611
                r.master_id = new_id;
13801
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13666
diff changeset
   612
                if !r.is_fixed() && c.protocol_number < 42 {
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   613
                    r.name
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   614
                        .replace_range(.., new_nick.as_ref().map_or("[]", String::as_str));
13801
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13666
diff changeset
   615
                }
13523
8c5dd562c9f7 Add room flags
alfadur
parents: 13520
diff changeset
   616
                r.set_join_restriction(false);
8c5dd562c9f7 Add room flags
alfadur
parents: 13520
diff changeset
   617
                r.set_team_add_restriction(false);
8c5dd562c9f7 Add room flags
alfadur
parents: 13520
diff changeset
   618
                let is_fixed = r.is_fixed();
8c5dd562c9f7 Add room flags
alfadur
parents: 13520
diff changeset
   619
                r.set_unregistered_players_restriction(is_fixed);
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   620
                if let Some(nick) = new_nick {
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   621
                    actions.push(
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   622
                        ClientFlags("+h".to_string(), vec![nick])
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   623
                            .send_all()
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   624
                            .in_room(r.id)
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   625
                            .action(),
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   626
                    );
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   627
                }
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   628
            }
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13527
diff changeset
   629
            if let Some(id) = new_id {
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13527
diff changeset
   630
                server.clients[id].set_is_master(true)
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13527
diff changeset
   631
            }
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   632
            server.react(client_id, actions);
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   633
        }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   634
        RemoveTeam(name) => {
13426
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
   635
            let mut actions = Vec::new();
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
   636
            if let (c, Some(r)) = server.client_and_room(client_id) {
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   637
                r.remove_team(&name);
13426
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
   638
                if let Some(ref mut info) = r.game_info {
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
   639
                    info.left_teams.push(name.clone());
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
   640
                }
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
   641
                actions.push(TeamRemove(name.clone()).send_all().in_room(r.id).action());
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
   642
                actions.push(SendRoomUpdate(None));
13520
1ee192f13456 Better packing for clients
alfadur
parents: 13480
diff changeset
   643
                if r.game_info.is_some() && c.is_in_game() {
13426
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
   644
                    actions.push(SendTeamRemovalMessage(name));
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
   645
                }
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
   646
            }
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   647
            server.react(client_id, actions);
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   648
        }
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   649
        RemoveClientTeams => {
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13527
diff changeset
   650
            if let (c, Some(r)) = server.client_and_room(client_id) {
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   651
                let actions = r
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   652
                    .client_teams(c.id)
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   653
                    .map(|t| RemoveTeam(t.name.clone()))
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   654
                    .collect();
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13527
diff changeset
   655
                server.react(client_id, actions);
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13527
diff changeset
   656
            }
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   657
        }
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   658
        SendRoomUpdate(old_name) => {
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13527
diff changeset
   659
            if let (c, Some(r)) = server.client_and_room(client_id) {
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   660
                let name = old_name.unwrap_or_else(|| r.name.clone());
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13527
diff changeset
   661
                let actions = vec![RoomUpdated(name, r.info(Some(&c)))
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   662
                    .send_all()
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   663
                    .with_protocol(r.protocol_number)
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   664
                    .action()];
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13527
diff changeset
   665
                server.react(client_id, actions);
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13527
diff changeset
   666
            }
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   667
        }
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   668
        StartRoomGame(room_id) => {
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   669
            let actions = {
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   670
                let (room_clients, room_nicks): (Vec<_>, Vec<_>) = server
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   671
                    .clients
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   672
                    .iter()
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   673
                    .map(|(id, c)| (id, c.nick.clone()))
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   674
                    .unzip();
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   675
                let room = &mut server.rooms[room_id];
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   676
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   677
                if !room.has_multiple_clans() {
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   678
                    vec![Warn(
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   679
                        "The game can't be started with less than two clans!".to_string(),
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   680
                    )]
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   681
                } else if room.protocol_number <= 43
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   682
                    && room.players_number != room.ready_players_number
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   683
                {
13801
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13666
diff changeset
   684
                    vec![Warn("Not all players are ready".to_string())]
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   685
                } else if room.game_info.is_some() {
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   686
                    vec![Warn("The game is already in progress".to_string())]
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   687
                } else {
13427
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   688
                    room.start_round();
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   689
                    for id in room_clients {
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   690
                        let c = &mut server.clients[id];
13520
1ee192f13456 Better packing for clients
alfadur
parents: 13480
diff changeset
   691
                        c.set_is_in_game(false);
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   692
                        c.team_indices = room.client_team_indices(c.id);
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   693
                    }
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   694
                    vec![
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   695
                        RunGame.send_all().in_room(room.id).action(),
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   696
                        SendRoomUpdate(None),
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   697
                        ClientFlags("+g".to_string(), room_nicks)
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   698
                            .send_all()
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   699
                            .in_room(room.id)
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   700
                            .action(),
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   701
                    ]
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   702
                }
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   703
            };
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   704
            server.react(client_id, actions);
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   705
        }
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   706
        SendTeamRemovalMessage(team_name) => {
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   707
            let mut actions = Vec::new();
13527
e3ae9eea0689 Implement map voting
alfadur
parents: 13524
diff changeset
   708
            if let Some(r) = server.room(client_id) {
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   709
                if let Some(ref mut info) = r.game_info {
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   710
                    let msg = once(b'F').chain(team_name.bytes());
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   711
                    actions.push(
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   712
                        ForwardEngineMessage(vec![to_engine_msg(msg)])
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   713
                            .send_all()
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   714
                            .in_room(r.id)
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   715
                            .but_self()
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   716
                            .action(),
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   717
                    );
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   718
                    info.teams_in_game -= 1;
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   719
                    if info.teams_in_game == 0 {
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   720
                        actions.push(FinishRoomGame(r.id));
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   721
                    }
13427
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   722
                    let remove_msg = to_engine_msg(once(b'F').chain(team_name.bytes()));
13443
2501428303a2 Fix team remove synchronization
alfadur
parents: 13439
diff changeset
   723
                    if let Some(m) = &info.sync_msg {
2501428303a2 Fix team remove synchronization
alfadur
parents: 13439
diff changeset
   724
                        info.msg_log.push(m.clone());
13427
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   725
                    }
13443
2501428303a2 Fix team remove synchronization
alfadur
parents: 13439
diff changeset
   726
                    if info.sync_msg.is_some() {
2501428303a2 Fix team remove synchronization
alfadur
parents: 13439
diff changeset
   727
                        info.sync_msg = None
2501428303a2 Fix team remove synchronization
alfadur
parents: 13439
diff changeset
   728
                    }
2501428303a2 Fix team remove synchronization
alfadur
parents: 13439
diff changeset
   729
                    info.msg_log.push(remove_msg.clone());
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   730
                    actions.push(
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   731
                        ForwardEngineMessage(vec![remove_msg])
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   732
                            .send_all()
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   733
                            .in_room(r.id)
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   734
                            .but_self()
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   735
                            .action(),
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   736
                    );
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   737
                }
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   738
            }
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   739
            server.react(client_id, actions);
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   740
        }
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   741
        FinishRoomGame(room_id) => {
13426
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
   742
            let mut actions = Vec::new();
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
   743
13801
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13666
diff changeset
   744
            let r = &mut server.rooms[room_id];
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13666
diff changeset
   745
            r.ready_players_number = 1;
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13666
diff changeset
   746
            actions.push(SendRoomUpdate(None));
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13666
diff changeset
   747
            actions.push(RoundFinished.send_all().in_room(r.id).action());
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13666
diff changeset
   748
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13666
diff changeset
   749
            if let Some(info) = replace(&mut r.game_info, None) {
13426
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
   750
                for (_, c) in server.clients.iter() {
13520
1ee192f13456 Better packing for clients
alfadur
parents: 13480
diff changeset
   751
                    if c.room_id == Some(room_id) && c.is_joined_mid_game() {
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   752
                        actions.push(SendRoomData {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   753
                            to: c.id,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   754
                            teams: false,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   755
                            config: true,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   756
                            flags: false,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   757
                        });
13524
5359ff75da3a indulge clippy
alfadur
parents: 13523
diff changeset
   758
                        for name in &info.left_teams {
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   759
                            actions.push(TeamRemove(name.clone()).send(c.id).action());
13426
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
   760
                        }
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
   761
                    }
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
   762
                }
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
   763
            }
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
   764
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   765
            let nicks: Vec<_> = server
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   766
                .clients
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   767
                .iter_mut()
13426
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
   768
                .filter(|(_, c)| c.room_id == Some(room_id))
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
   769
                .map(|(_, c)| {
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13527
diff changeset
   770
                    c.set_is_ready(c.is_master());
13520
1ee192f13456 Better packing for clients
alfadur
parents: 13480
diff changeset
   771
                    c.set_is_joined_mid_game(false);
13426
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
   772
                    c
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   773
                })
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   774
                .filter_map(|c| {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   775
                    if !c.is_master() {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   776
                        Some(c.nick.clone())
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   777
                    } else {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   778
                        None
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   779
                    }
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   780
                })
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   781
                .collect();
13801
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13666
diff changeset
   782
13426
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
   783
            if !nicks.is_empty() {
13801
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13666
diff changeset
   784
                let msg = if r.protocol_number < 38 {
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13666
diff changeset
   785
                    LegacyReady(false, nicks)
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13666
diff changeset
   786
                } else {
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13666
diff changeset
   787
                    ClientFlags("-r".to_string(), nicks)
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13666
diff changeset
   788
                };
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13666
diff changeset
   789
                actions.push(msg.send_all().in_room(room_id).action());
13426
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
   790
            }
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   791
            server.react(client_id, actions);
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   792
        }
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents: 12146
diff changeset
   793
        Warn(msg) => {
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   794
            run_action(server, client_id, Warning(msg).send_self().action());
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents: 12146
diff changeset
   795
        }
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   796
        ProtocolError(msg) => run_action(server, client_id, Error(msg).send_self().action()),
12144
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
   797
    }
589a2d7d3dc5 More refactoring
unc0rr
parents: 12143
diff changeset
   798
}