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