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