rust/hedgewars-server/src/server/handlers/common.rs
author alfadur
Fri, 12 Apr 2019 23:40:32 +0300
changeset 14798 d11481978633
parent 14791 b889d9e1115f
child 14799 e41131607167
permissions -rw-r--r--
fix room exiting
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13524
diff changeset
     1
use crate::{
14688
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
     2
    protocol::messages::server_chat,
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13524
diff changeset
     3
    protocol::messages::{
14782
50fcef24003f add typed protocol flags
alfadur
parents: 14781
diff changeset
     4
        add_flags, remove_flags,
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
     5
        HWProtocolMessage::{self, Rnd},
14688
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
     6
        HWServerMessage::{self, *},
14782
50fcef24003f add typed protocol flags
alfadur
parents: 14781
diff changeset
     7
        ProtocolFlags as Flags,
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
     8
    },
14688
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
     9
    server::{
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
    10
        client::HWClient,
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
    11
        core::HWServer,
14785
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14784
diff changeset
    12
        coretypes::{ClientId, GameCfg, RoomId, TeamInfo, Vote, VoteType},
14688
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
    13
        room::HWRoom,
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
    14
    },
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
    15
    utils::to_engine_msg,
13444
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
    16
};
14688
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
    17
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
    18
use super::Response;
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
    19
14785
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14784
diff changeset
    20
use crate::server::coretypes::RoomConfig;
14697
f64e21f164a5 fix some warnings
alfadur
parents: 14696
diff changeset
    21
use rand::{self, seq::SliceRandom, thread_rng, Rng};
14688
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
    22
use std::{iter::once, mem::replace};
13444
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
    23
13524
5359ff75da3a indulge clippy
alfadur
parents: 13521
diff changeset
    24
pub fn rnd_reply(options: &[String]) -> HWServerMessage {
13521
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13446
diff changeset
    25
    let mut rng = thread_rng();
14697
f64e21f164a5 fix some warnings
alfadur
parents: 14696
diff changeset
    26
13521
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13446
diff changeset
    27
    let reply = if options.is_empty() {
14697
f64e21f164a5 fix some warnings
alfadur
parents: 14696
diff changeset
    28
        (*&["heads", "tails"].choose(&mut rng).unwrap()).to_string()
13445
d3c86ade3d4d Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13444
diff changeset
    29
    } else {
14697
f64e21f164a5 fix some warnings
alfadur
parents: 14696
diff changeset
    30
        options.choose(&mut rng).unwrap().clone()
13521
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13446
diff changeset
    31
    };
13445
d3c86ade3d4d Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13444
diff changeset
    32
13521
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13446
diff changeset
    33
    ChatMsg {
14697
f64e21f164a5 fix some warnings
alfadur
parents: 14696
diff changeset
    34
        nick: "[random]".to_string(),
f64e21f164a5 fix some warnings
alfadur
parents: 14696
diff changeset
    35
        msg: reply,
13521
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13446
diff changeset
    36
    }
13444
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
    37
}
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
    38
14781
01f8ab45f806 fix lobby joining
alfadur
parents: 14697
diff changeset
    39
pub fn join_lobby(server: &mut HWServer, response: &mut Response) {
14683
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14675
diff changeset
    40
    let client_id = response.client_id();
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14675
diff changeset
    41
14791
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    42
    let client = &server.clients[client_id];
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    43
    let nick = vec![client.nick.clone()];
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    44
    let mut flags = vec![];
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    45
    if client.is_registered() {
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    46
        flags.push(Flags::Registered)
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    47
    }
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    48
    if client.is_admin() {
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    49
        flags.push(Flags::Admin)
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    50
    }
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    51
    if client.is_contributor() {
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    52
        flags.push(Flags::Contributor)
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    53
    }
14683
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14675
diff changeset
    54
14791
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    55
    let all_nicks: Vec<_> = server.collect_nicks(|_| true);
14683
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14675
diff changeset
    56
14791
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    57
    let mut flag_selectors = [
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    58
        (
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    59
            Flags::Registered,
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    60
            server.collect_nicks(|(_, c)| c.is_registered()),
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    61
        ),
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    62
        (Flags::Admin, server.collect_nicks(|(_, c)| c.is_admin())),
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    63
        (
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    64
            Flags::Contributor,
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    65
            server.collect_nicks(|(_, c)| c.is_contributor()),
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    66
        ),
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    67
        (
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    68
            Flags::InRoom,
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    69
            server.collect_nicks(|(_, c)| c.room_id.is_some()),
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    70
        ),
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    71
    ];
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    72
14783
b3adc030104b implement server vars
alfadur
parents: 14782
diff changeset
    73
    let server_msg = ServerMessage(server.get_greetings(client_id).to_string());
14683
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14675
diff changeset
    74
14781
01f8ab45f806 fix lobby joining
alfadur
parents: 14697
diff changeset
    75
    let rooms_msg = Rooms(
01f8ab45f806 fix lobby joining
alfadur
parents: 14697
diff changeset
    76
        server
01f8ab45f806 fix lobby joining
alfadur
parents: 14697
diff changeset
    77
            .rooms
14683
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14675
diff changeset
    78
            .iter()
14781
01f8ab45f806 fix lobby joining
alfadur
parents: 14697
diff changeset
    79
            .flat_map(|(_, r)| r.info(r.master_id.map(|id| &server.clients[id])))
01f8ab45f806 fix lobby joining
alfadur
parents: 14697
diff changeset
    80
            .collect(),
01f8ab45f806 fix lobby joining
alfadur
parents: 14697
diff changeset
    81
    );
14683
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14675
diff changeset
    82
14791
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    83
    response.add(LobbyJoined(nick).send_all().but_self());
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    84
    response.add(
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    85
        ClientFlags(add_flags(&flags), all_nicks.clone())
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    86
            .send_all()
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    87
            .but_self(),
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    88
    );
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    89
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    90
    response.add(LobbyJoined(all_nicks).send_self());
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    91
    for (flag, nicks) in &mut flag_selectors {
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    92
        response.add(ClientFlags(add_flags(&[*flag]), replace(nicks, vec![])).send_self());
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    93
    }
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    94
14781
01f8ab45f806 fix lobby joining
alfadur
parents: 14697
diff changeset
    95
    response.add(server_msg.send_self());
01f8ab45f806 fix lobby joining
alfadur
parents: 14697
diff changeset
    96
    response.add(rooms_msg.send_self());
14683
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14675
diff changeset
    97
}
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14675
diff changeset
    98
14675
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14674
diff changeset
    99
pub fn remove_teams(
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14674
diff changeset
   100
    room: &mut HWRoom,
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14674
diff changeset
   101
    team_names: Vec<String>,
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14674
diff changeset
   102
    is_in_game: bool,
14688
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   103
    response: &mut Response,
14675
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14674
diff changeset
   104
) {
14674
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14673
diff changeset
   105
    if let Some(ref mut info) = room.game_info {
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14673
diff changeset
   106
        for team_name in &team_names {
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14673
diff changeset
   107
            info.left_teams.push(team_name.clone());
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14673
diff changeset
   108
14675
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14674
diff changeset
   109
            if is_in_game {
14674
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14673
diff changeset
   110
                let msg = once(b'F').chain(team_name.bytes());
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14673
diff changeset
   111
                response.add(
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14673
diff changeset
   112
                    ForwardEngineMessage(vec![to_engine_msg(msg)])
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14673
diff changeset
   113
                        .send_all()
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14673
diff changeset
   114
                        .in_room(room.id)
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14673
diff changeset
   115
                        .but_self(),
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14673
diff changeset
   116
                );
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14673
diff changeset
   117
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14673
diff changeset
   118
                info.teams_in_game -= 1;
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14673
diff changeset
   119
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14673
diff changeset
   120
                let remove_msg = to_engine_msg(once(b'F').chain(team_name.bytes()));
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14673
diff changeset
   121
                if let Some(m) = &info.sync_msg {
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14673
diff changeset
   122
                    info.msg_log.push(m.clone());
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14673
diff changeset
   123
                    info.sync_msg = None
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14673
diff changeset
   124
                }
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14673
diff changeset
   125
                info.msg_log.push(remove_msg.clone());
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14673
diff changeset
   126
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14673
diff changeset
   127
                response.add(
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14673
diff changeset
   128
                    ForwardEngineMessage(vec![remove_msg])
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14673
diff changeset
   129
                        .send_all()
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14673
diff changeset
   130
                        .in_room(room.id)
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14673
diff changeset
   131
                        .but_self(),
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14673
diff changeset
   132
                );
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14673
diff changeset
   133
            }
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14673
diff changeset
   134
        }
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14673
diff changeset
   135
    }
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14673
diff changeset
   136
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14673
diff changeset
   137
    for team_name in team_names {
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14673
diff changeset
   138
        room.remove_team(&team_name);
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14673
diff changeset
   139
        response.add(TeamRemove(team_name).send_all().in_room(room.id));
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14673
diff changeset
   140
    }
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14673
diff changeset
   141
}
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14673
diff changeset
   142
14690
f61ce544d436 Server action refactoring part N of N
alfadur <mail@none>
parents: 14689
diff changeset
   143
fn remove_client_from_room(
f61ce544d436 Server action refactoring part N of N
alfadur <mail@none>
parents: 14689
diff changeset
   144
    client: &mut HWClient,
f61ce544d436 Server action refactoring part N of N
alfadur <mail@none>
parents: 14689
diff changeset
   145
    room: &mut HWRoom,
f61ce544d436 Server action refactoring part N of N
alfadur <mail@none>
parents: 14689
diff changeset
   146
    response: &mut Response,
f61ce544d436 Server action refactoring part N of N
alfadur <mail@none>
parents: 14689
diff changeset
   147
    msg: &str,
f61ce544d436 Server action refactoring part N of N
alfadur <mail@none>
parents: 14689
diff changeset
   148
) {
14798
d11481978633 fix room exiting
alfadur
parents: 14791
diff changeset
   149
    room.players_number -= 1;
d11481978633 fix room exiting
alfadur
parents: 14791
diff changeset
   150
    if room.players_number > 0 || room.is_fixed() {
14675
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14674
diff changeset
   151
        if client.is_ready() && room.ready_players_number > 0 {
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14674
diff changeset
   152
            room.ready_players_number -= 1;
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14674
diff changeset
   153
        }
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14674
diff changeset
   154
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14674
diff changeset
   155
        let team_names: Vec<_> = room
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14674
diff changeset
   156
            .client_teams(client.id)
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14674
diff changeset
   157
            .map(|t| t.name.clone())
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14674
diff changeset
   158
            .collect();
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14674
diff changeset
   159
        remove_teams(room, team_names, client.is_in_game(), response);
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14674
diff changeset
   160
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14674
diff changeset
   161
        if room.players_number > 0 {
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14674
diff changeset
   162
            response.add(
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14674
diff changeset
   163
                RoomLeft(client.nick.clone(), msg.to_string())
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14674
diff changeset
   164
                    .send_all()
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14674
diff changeset
   165
                    .in_room(room.id)
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14674
diff changeset
   166
                    .but_self(),
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14674
diff changeset
   167
            );
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14674
diff changeset
   168
        }
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14674
diff changeset
   169
14690
f61ce544d436 Server action refactoring part N of N
alfadur <mail@none>
parents: 14689
diff changeset
   170
        if client.is_master() && !room.is_fixed() {
f61ce544d436 Server action refactoring part N of N
alfadur <mail@none>
parents: 14689
diff changeset
   171
            client.set_is_master(false);
f61ce544d436 Server action refactoring part N of N
alfadur <mail@none>
parents: 14689
diff changeset
   172
            response.add(
14783
b3adc030104b implement server vars
alfadur
parents: 14782
diff changeset
   173
                ClientFlags(
b3adc030104b implement server vars
alfadur
parents: 14782
diff changeset
   174
                    remove_flags(&[Flags::RoomMaster]),
b3adc030104b implement server vars
alfadur
parents: 14782
diff changeset
   175
                    vec![client.nick.clone()],
b3adc030104b implement server vars
alfadur
parents: 14782
diff changeset
   176
                )
b3adc030104b implement server vars
alfadur
parents: 14782
diff changeset
   177
                .send_all()
b3adc030104b implement server vars
alfadur
parents: 14782
diff changeset
   178
                .in_room(room.id),
14690
f61ce544d436 Server action refactoring part N of N
alfadur <mail@none>
parents: 14689
diff changeset
   179
            );
f61ce544d436 Server action refactoring part N of N
alfadur <mail@none>
parents: 14689
diff changeset
   180
            room.master_id = None;
f61ce544d436 Server action refactoring part N of N
alfadur <mail@none>
parents: 14689
diff changeset
   181
        }
14675
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14674
diff changeset
   182
    }
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14674
diff changeset
   183
14694
25c564f77b7d remove lobby room
alfadur <mail@none>
parents: 14691
diff changeset
   184
    client.room_id = None;
25c564f77b7d remove lobby room
alfadur <mail@none>
parents: 14691
diff changeset
   185
14675
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14674
diff changeset
   186
    let update_msg = if room.players_number == 0 && !room.is_fixed() {
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14674
diff changeset
   187
        RoomRemove(room.name.clone())
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14674
diff changeset
   188
    } else {
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14674
diff changeset
   189
        RoomUpdated(room.name.clone(), room.info(Some(&client)))
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14674
diff changeset
   190
    };
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14674
diff changeset
   191
    response.add(update_msg.send_all().with_protocol(room.protocol_number));
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14674
diff changeset
   192
14782
50fcef24003f add typed protocol flags
alfadur
parents: 14781
diff changeset
   193
    response.add(ClientFlags(remove_flags(&[Flags::InRoom]), vec![client.nick.clone()]).send_all());
14675
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14674
diff changeset
   194
}
dfe652c53470 Server action refactoring part 6 of N
alfadur <mail@none>
parents: 14674
diff changeset
   195
14784
8390d5e4e39c implememg global&delegate messages
alfadur
parents: 14783
diff changeset
   196
pub fn change_master(
8390d5e4e39c implememg global&delegate messages
alfadur
parents: 14783
diff changeset
   197
    server: &mut HWServer,
8390d5e4e39c implememg global&delegate messages
alfadur
parents: 14783
diff changeset
   198
    room_id: RoomId,
14790
e94fbf6cad2b fix room master change
alfadur
parents: 14789
diff changeset
   199
    new_master_id: ClientId,
14784
8390d5e4e39c implememg global&delegate messages
alfadur
parents: 14783
diff changeset
   200
    response: &mut Response,
8390d5e4e39c implememg global&delegate messages
alfadur
parents: 14783
diff changeset
   201
) {
14790
e94fbf6cad2b fix room master change
alfadur
parents: 14789
diff changeset
   202
    let room = &mut server.rooms[room_id];
e94fbf6cad2b fix room master change
alfadur
parents: 14789
diff changeset
   203
    if let Some(master_id) = room.master_id {
e94fbf6cad2b fix room master change
alfadur
parents: 14789
diff changeset
   204
        server.clients[master_id].set_is_master(false);
e94fbf6cad2b fix room master change
alfadur
parents: 14789
diff changeset
   205
        response.add(
e94fbf6cad2b fix room master change
alfadur
parents: 14789
diff changeset
   206
            ClientFlags(
e94fbf6cad2b fix room master change
alfadur
parents: 14789
diff changeset
   207
                remove_flags(&[Flags::RoomMaster]),
e94fbf6cad2b fix room master change
alfadur
parents: 14789
diff changeset
   208
                vec![server.clients[master_id].nick.clone()],
e94fbf6cad2b fix room master change
alfadur
parents: 14789
diff changeset
   209
            )
e94fbf6cad2b fix room master change
alfadur
parents: 14789
diff changeset
   210
            .send_all()
e94fbf6cad2b fix room master change
alfadur
parents: 14789
diff changeset
   211
            .in_room(room_id),
e94fbf6cad2b fix room master change
alfadur
parents: 14789
diff changeset
   212
        )
e94fbf6cad2b fix room master change
alfadur
parents: 14789
diff changeset
   213
    }
e94fbf6cad2b fix room master change
alfadur
parents: 14789
diff changeset
   214
e94fbf6cad2b fix room master change
alfadur
parents: 14789
diff changeset
   215
    room.master_id = Some(new_master_id);
e94fbf6cad2b fix room master change
alfadur
parents: 14789
diff changeset
   216
    server.clients[new_master_id].set_is_master(true);
14784
8390d5e4e39c implememg global&delegate messages
alfadur
parents: 14783
diff changeset
   217
8390d5e4e39c implememg global&delegate messages
alfadur
parents: 14783
diff changeset
   218
    response.add(
8390d5e4e39c implememg global&delegate messages
alfadur
parents: 14783
diff changeset
   219
        ClientFlags(
8390d5e4e39c implememg global&delegate messages
alfadur
parents: 14783
diff changeset
   220
            add_flags(&[Flags::RoomMaster]),
14790
e94fbf6cad2b fix room master change
alfadur
parents: 14789
diff changeset
   221
            vec![server.clients[new_master_id].nick.clone()],
14784
8390d5e4e39c implememg global&delegate messages
alfadur
parents: 14783
diff changeset
   222
        )
8390d5e4e39c implememg global&delegate messages
alfadur
parents: 14783
diff changeset
   223
        .send_all()
8390d5e4e39c implememg global&delegate messages
alfadur
parents: 14783
diff changeset
   224
        .in_room(room_id),
8390d5e4e39c implememg global&delegate messages
alfadur
parents: 14783
diff changeset
   225
    );
8390d5e4e39c implememg global&delegate messages
alfadur
parents: 14783
diff changeset
   226
}
8390d5e4e39c implememg global&delegate messages
alfadur
parents: 14783
diff changeset
   227
14787
0e64acbc3f8b implement follow message
alfadur
parents: 14785
diff changeset
   228
pub fn enter_room(
0e64acbc3f8b implement follow message
alfadur
parents: 14785
diff changeset
   229
    server: &mut HWServer,
0e64acbc3f8b implement follow message
alfadur
parents: 14785
diff changeset
   230
    client_id: ClientId,
0e64acbc3f8b implement follow message
alfadur
parents: 14785
diff changeset
   231
    room_id: RoomId,
0e64acbc3f8b implement follow message
alfadur
parents: 14785
diff changeset
   232
    response: &mut Response,
0e64acbc3f8b implement follow message
alfadur
parents: 14785
diff changeset
   233
) {
0e64acbc3f8b implement follow message
alfadur
parents: 14785
diff changeset
   234
    let nick = server.clients[client_id].nick.clone();
0e64acbc3f8b implement follow message
alfadur
parents: 14785
diff changeset
   235
    server.move_to_room(client_id, room_id);
0e64acbc3f8b implement follow message
alfadur
parents: 14785
diff changeset
   236
0e64acbc3f8b implement follow message
alfadur
parents: 14785
diff changeset
   237
    response.add(RoomJoined(vec![nick.clone()]).send_all().in_room(room_id));
0e64acbc3f8b implement follow message
alfadur
parents: 14785
diff changeset
   238
    response.add(ClientFlags(add_flags(&[Flags::InRoom]), vec![nick]).send_all());
0e64acbc3f8b implement follow message
alfadur
parents: 14785
diff changeset
   239
    let nicks = server.collect_nicks(|(_, c)| c.room_id == Some(room_id));
0e64acbc3f8b implement follow message
alfadur
parents: 14785
diff changeset
   240
    response.add(RoomJoined(nicks).send_self());
0e64acbc3f8b implement follow message
alfadur
parents: 14785
diff changeset
   241
0e64acbc3f8b implement follow message
alfadur
parents: 14785
diff changeset
   242
    let room = &server.rooms[room_id];
0e64acbc3f8b implement follow message
alfadur
parents: 14785
diff changeset
   243
0e64acbc3f8b implement follow message
alfadur
parents: 14785
diff changeset
   244
    if !room.greeting.is_empty() {
0e64acbc3f8b implement follow message
alfadur
parents: 14785
diff changeset
   245
        response.add(
0e64acbc3f8b implement follow message
alfadur
parents: 14785
diff changeset
   246
            ChatMsg {
0e64acbc3f8b implement follow message
alfadur
parents: 14785
diff changeset
   247
                nick: "[greeting]".to_string(),
0e64acbc3f8b implement follow message
alfadur
parents: 14785
diff changeset
   248
                msg: room.greeting.clone(),
0e64acbc3f8b implement follow message
alfadur
parents: 14785
diff changeset
   249
            }
0e64acbc3f8b implement follow message
alfadur
parents: 14785
diff changeset
   250
            .send_self(),
0e64acbc3f8b implement follow message
alfadur
parents: 14785
diff changeset
   251
        );
0e64acbc3f8b implement follow message
alfadur
parents: 14785
diff changeset
   252
    }
0e64acbc3f8b implement follow message
alfadur
parents: 14785
diff changeset
   253
}
0e64acbc3f8b implement follow message
alfadur
parents: 14785
diff changeset
   254
14690
f61ce544d436 Server action refactoring part N of N
alfadur <mail@none>
parents: 14689
diff changeset
   255
pub fn exit_room(server: &mut HWServer, client_id: ClientId, response: &mut Response, msg: &str) {
f61ce544d436 Server action refactoring part N of N
alfadur <mail@none>
parents: 14689
diff changeset
   256
    let client = &mut server.clients[client_id];
f61ce544d436 Server action refactoring part N of N
alfadur <mail@none>
parents: 14689
diff changeset
   257
f61ce544d436 Server action refactoring part N of N
alfadur <mail@none>
parents: 14689
diff changeset
   258
    if let Some(room_id) = client.room_id {
14694
25c564f77b7d remove lobby room
alfadur <mail@none>
parents: 14691
diff changeset
   259
        let room = &mut server.rooms[room_id];
14690
f61ce544d436 Server action refactoring part N of N
alfadur <mail@none>
parents: 14689
diff changeset
   260
14694
25c564f77b7d remove lobby room
alfadur <mail@none>
parents: 14691
diff changeset
   261
        remove_client_from_room(client, room, response, msg);
14690
f61ce544d436 Server action refactoring part N of N
alfadur <mail@none>
parents: 14689
diff changeset
   262
14798
d11481978633 fix room exiting
alfadur
parents: 14791
diff changeset
   263
        if !room.is_fixed() {
d11481978633 fix room exiting
alfadur
parents: 14791
diff changeset
   264
            if room.players_number == 0 {
d11481978633 fix room exiting
alfadur
parents: 14791
diff changeset
   265
                server.rooms.remove(room_id);
d11481978633 fix room exiting
alfadur
parents: 14791
diff changeset
   266
            } else if room.master_id == None {
d11481978633 fix room exiting
alfadur
parents: 14791
diff changeset
   267
                let new_master_id = server.room_clients(room_id).next();
d11481978633 fix room exiting
alfadur
parents: 14791
diff changeset
   268
                if let Some(new_master_id) = new_master_id {
d11481978633 fix room exiting
alfadur
parents: 14791
diff changeset
   269
                    let new_master_nick = server.clients[new_master_id].nick.clone();
d11481978633 fix room exiting
alfadur
parents: 14791
diff changeset
   270
                    let room = &mut server.rooms[room_id];
d11481978633 fix room exiting
alfadur
parents: 14791
diff changeset
   271
                    room.master_id = Some(new_master_id);
d11481978633 fix room exiting
alfadur
parents: 14791
diff changeset
   272
                    server.clients[new_master_id].set_is_master(true);
14690
f61ce544d436 Server action refactoring part N of N
alfadur <mail@none>
parents: 14689
diff changeset
   273
14798
d11481978633 fix room exiting
alfadur
parents: 14791
diff changeset
   274
                    if room.protocol_number < 42 {
d11481978633 fix room exiting
alfadur
parents: 14791
diff changeset
   275
                        room.name = new_master_nick.clone();
d11481978633 fix room exiting
alfadur
parents: 14791
diff changeset
   276
                    }
d11481978633 fix room exiting
alfadur
parents: 14791
diff changeset
   277
d11481978633 fix room exiting
alfadur
parents: 14791
diff changeset
   278
                    room.set_join_restriction(false);
d11481978633 fix room exiting
alfadur
parents: 14791
diff changeset
   279
                    room.set_team_add_restriction(false);
d11481978633 fix room exiting
alfadur
parents: 14791
diff changeset
   280
                    room.set_unregistered_players_restriction(true);
d11481978633 fix room exiting
alfadur
parents: 14791
diff changeset
   281
d11481978633 fix room exiting
alfadur
parents: 14791
diff changeset
   282
                    response.add(
d11481978633 fix room exiting
alfadur
parents: 14791
diff changeset
   283
                        ClientFlags(add_flags(&[Flags::RoomMaster]), vec![new_master_nick])
d11481978633 fix room exiting
alfadur
parents: 14791
diff changeset
   284
                            .send_all()
d11481978633 fix room exiting
alfadur
parents: 14791
diff changeset
   285
                            .in_room(room.id),
d11481978633 fix room exiting
alfadur
parents: 14791
diff changeset
   286
                    );
14694
25c564f77b7d remove lobby room
alfadur <mail@none>
parents: 14691
diff changeset
   287
                }
14690
f61ce544d436 Server action refactoring part N of N
alfadur <mail@none>
parents: 14689
diff changeset
   288
            }
f61ce544d436 Server action refactoring part N of N
alfadur <mail@none>
parents: 14689
diff changeset
   289
        }
f61ce544d436 Server action refactoring part N of N
alfadur <mail@none>
parents: 14689
diff changeset
   290
    }
f61ce544d436 Server action refactoring part N of N
alfadur <mail@none>
parents: 14689
diff changeset
   291
}
f61ce544d436 Server action refactoring part N of N
alfadur <mail@none>
parents: 14689
diff changeset
   292
14688
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   293
pub fn remove_client(server: &mut HWServer, response: &mut Response, msg: String) {
14674
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14673
diff changeset
   294
    let client_id = response.client_id();
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14673
diff changeset
   295
    let client = &mut server.clients[client_id];
14697
f64e21f164a5 fix some warnings
alfadur
parents: 14696
diff changeset
   296
    let nick = client.nick.clone();
14674
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14673
diff changeset
   297
14690
f61ce544d436 Server action refactoring part N of N
alfadur <mail@none>
parents: 14689
diff changeset
   298
    exit_room(server, client_id, response, &msg);
14674
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14673
diff changeset
   299
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14673
diff changeset
   300
    server.remove_client(client_id);
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14673
diff changeset
   301
14673
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14457
diff changeset
   302
    response.add(LobbyLeft(nick, msg.to_string()).send_all());
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14457
diff changeset
   303
    response.add(Bye("User quit: ".to_string() + &msg).send_self());
14696
8a45c90f4580 fix client removal
alfadur
parents: 14694
diff changeset
   304
    response.remove_client(client_id);
14673
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14457
diff changeset
   305
}
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14457
diff changeset
   306
14686
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14683
diff changeset
   307
pub fn get_room_update(
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14683
diff changeset
   308
    room_name: Option<String>,
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14683
diff changeset
   309
    room: &HWRoom,
14687
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   310
    master: Option<&HWClient>,
14688
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   311
    response: &mut Response,
14686
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14683
diff changeset
   312
) {
14687
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   313
    let update_msg = RoomUpdated(room_name.unwrap_or(room.name.clone()), room.info(master));
14686
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14683
diff changeset
   314
    response.add(update_msg.send_all().with_protocol(room.protocol_number));
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14683
diff changeset
   315
}
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14683
diff changeset
   316
14785
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14784
diff changeset
   317
pub fn get_room_config_impl(config: &RoomConfig, to_client: ClientId, response: &mut Response) {
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14784
diff changeset
   318
    response.add(ConfigEntry("FULLMAPCONFIG".to_string(), config.to_map_config()).send(to_client));
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14784
diff changeset
   319
    for cfg in config.to_game_config() {
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14784
diff changeset
   320
        response.add(cfg.to_server_msg().send(to_client));
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14784
diff changeset
   321
    }
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14784
diff changeset
   322
}
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14784
diff changeset
   323
14689
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   324
pub fn get_room_config(room: &HWRoom, to_client: ClientId, response: &mut Response) {
14785
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14784
diff changeset
   325
    get_room_config_impl(room.active_config(), to_client, response);
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14784
diff changeset
   326
}
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14784
diff changeset
   327
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14784
diff changeset
   328
pub fn get_teams<'a, I>(teams: I, to_client: ClientId, response: &mut Response)
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14784
diff changeset
   329
where
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14784
diff changeset
   330
    I: Iterator<Item = &'a TeamInfo>,
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14784
diff changeset
   331
{
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14784
diff changeset
   332
    for team in teams {
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14784
diff changeset
   333
        response.add(TeamAdd(team.to_protocol()).send(to_client));
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14784
diff changeset
   334
        response.add(TeamColor(team.name.clone(), team.color).send(to_client));
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14784
diff changeset
   335
        response.add(HedgehogsNumber(team.name.clone(), team.hedgehogs_number).send(to_client));
14689
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   336
    }
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   337
}
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   338
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   339
pub fn get_room_teams(
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   340
    server: &HWServer,
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   341
    room_id: RoomId,
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   342
    to_client: ClientId,
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   343
    response: &mut Response,
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   344
) {
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   345
    let room = &server.rooms[room_id];
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   346
    let current_teams = match room.game_info {
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   347
        Some(ref info) => &info.teams_at_start,
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   348
        None => &room.teams,
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   349
    };
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   350
14785
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14784
diff changeset
   351
    get_teams(current_teams.iter().map(|(_, t)| t), to_client, response);
14689
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   352
}
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   353
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   354
pub fn get_room_flags(
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   355
    server: &HWServer,
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   356
    room_id: RoomId,
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   357
    to_client: ClientId,
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   358
    response: &mut Response,
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   359
) {
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   360
    let room = &server.rooms[room_id];
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   361
    if let Some(id) = room.master_id {
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   362
        response.add(
14782
50fcef24003f add typed protocol flags
alfadur
parents: 14781
diff changeset
   363
            ClientFlags(
50fcef24003f add typed protocol flags
alfadur
parents: 14781
diff changeset
   364
                add_flags(&[Flags::RoomMaster]),
50fcef24003f add typed protocol flags
alfadur
parents: 14781
diff changeset
   365
                vec![server.clients[id].nick.clone()],
50fcef24003f add typed protocol flags
alfadur
parents: 14781
diff changeset
   366
            )
50fcef24003f add typed protocol flags
alfadur
parents: 14781
diff changeset
   367
            .send(to_client),
14689
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   368
        );
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   369
    }
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   370
    let nicks: Vec<_> = server
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   371
        .clients
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   372
        .iter()
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   373
        .filter(|(_, c)| c.room_id == Some(room_id) && c.is_ready())
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   374
        .map(|(_, c)| c.nick.clone())
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   375
        .collect();
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   376
    if !nicks.is_empty() {
14782
50fcef24003f add typed protocol flags
alfadur
parents: 14781
diff changeset
   377
        response.add(ClientFlags(add_flags(&[Flags::Ready]), nicks).send(to_client));
14689
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   378
    }
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   379
}
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   380
14687
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   381
pub fn apply_voting_result(
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   382
    server: &mut HWServer,
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   383
    room_id: RoomId,
14688
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   384
    response: &mut Response,
14687
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   385
    kind: VoteType,
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   386
) {
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   387
    match kind {
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   388
        VoteType::Kick(nick) => {
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   389
            if let Some(client) = server.find_client(&nick) {
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   390
                if client.room_id == Some(room_id) {
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   391
                    let id = client.id;
14690
f61ce544d436 Server action refactoring part N of N
alfadur <mail@none>
parents: 14689
diff changeset
   392
                    response.add(Kicked.send(id));
f61ce544d436 Server action refactoring part N of N
alfadur <mail@none>
parents: 14689
diff changeset
   393
                    exit_room(server, id, response, "kicked");
14687
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   394
                }
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   395
            }
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   396
        }
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   397
        VoteType::Map(None) => (),
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   398
        VoteType::Map(Some(name)) => {
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   399
            if let Some(location) = server.rooms[room_id].load_config(&name) {
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   400
                response.add(
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   401
                    server_chat(location.to_string())
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   402
                        .send_all()
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   403
                        .in_room(room_id),
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   404
                );
14688
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   405
                let room = &server.rooms[room_id];
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   406
                let room_master = if let Some(id) = room.master_id {
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   407
                    Some(&server.clients[id])
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   408
                } else {
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   409
                    None
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   410
                };
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   411
                get_room_update(None, room, room_master, response);
14687
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   412
14689
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   413
                for (_, client) in server.clients.iter() {
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   414
                    if client.room_id == Some(room_id) {
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   415
                        super::common::get_room_config(&server.rooms[room_id], client.id, response);
14687
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   416
                    }
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   417
                }
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   418
            }
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   419
        }
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   420
        VoteType::Pause => {
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   421
            if let Some(ref mut info) = server.rooms[room_id].game_info {
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   422
                info.is_paused = !info.is_paused;
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   423
                response.add(
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   424
                    server_chat("Pause toggled.".to_string())
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   425
                        .send_all()
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   426
                        .in_room(room_id),
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   427
                );
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   428
                response.add(
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   429
                    ForwardEngineMessage(vec![to_engine_msg(once(b'I'))])
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   430
                        .send_all()
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   431
                        .in_room(room_id),
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   432
                );
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   433
            }
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   434
        }
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   435
        VoteType::NewSeed => {
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   436
            let seed = thread_rng().gen_range(0, 1_000_000_000).to_string();
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   437
            let cfg = GameCfg::Seed(seed);
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   438
            response.add(cfg.to_server_msg().send_all().in_room(room_id));
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   439
            server.rooms[room_id].set_config(cfg);
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   440
        }
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   441
        VoteType::HedgehogsPerTeam(number) => {
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   442
            let r = &mut server.rooms[room_id];
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   443
            let nicks = r.set_hedgehogs_number(number);
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   444
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   445
            response.extend(
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   446
                nicks
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   447
                    .into_iter()
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   448
                    .map(|n| HedgehogsNumber(n, number).send_all().in_room(room_id)),
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   449
            );
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   450
        }
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   451
    }
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   452
}
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   453
14688
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   454
fn add_vote(room: &mut HWRoom, response: &mut Response, vote: Vote) -> Option<bool> {
14686
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14683
diff changeset
   455
    let client_id = response.client_id;
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14683
diff changeset
   456
    let mut result = None;
14687
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   457
14686
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14683
diff changeset
   458
    if let Some(ref mut voting) = room.voting {
14687
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   459
        if vote.is_forced || voting.votes.iter().all(|(id, _)| client_id != *id) {
14686
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14683
diff changeset
   460
            response.add(server_chat("Your vote has been counted.".to_string()).send_self());
14687
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   461
            voting.votes.push((client_id, vote.is_pro));
14686
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14683
diff changeset
   462
            let i = voting.votes.iter();
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14683
diff changeset
   463
            let pro = i.clone().filter(|(_, v)| *v).count();
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14683
diff changeset
   464
            let contra = i.filter(|(_, v)| !*v).count();
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14683
diff changeset
   465
            let success_quota = voting.voters.len() / 2 + 1;
14687
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   466
            if vote.is_forced && vote.is_pro || pro >= success_quota {
14686
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14683
diff changeset
   467
                result = Some(true);
14687
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   468
            } else if vote.is_forced && !vote.is_pro || contra > voting.voters.len() - success_quota
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   469
            {
14686
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14683
diff changeset
   470
                result = Some(false);
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14683
diff changeset
   471
            }
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14683
diff changeset
   472
        } else {
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14683
diff changeset
   473
            response.add(server_chat("You already have voted.".to_string()).send_self());
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14683
diff changeset
   474
        }
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14683
diff changeset
   475
    } else {
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14683
diff changeset
   476
        response.add(server_chat("There's no voting going on.".to_string()).send_self());
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14683
diff changeset
   477
    }
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14683
diff changeset
   478
14687
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   479
    result
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   480
}
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   481
14688
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   482
pub fn submit_vote(server: &mut HWServer, vote: Vote, response: &mut Response) {
14687
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   483
    let client_id = response.client_id;
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   484
    let client = &server.clients[client_id];
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   485
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   486
    if let Some(room_id) = client.room_id {
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   487
        let room = &mut server.rooms[room_id];
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   488
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   489
        if let Some(res) = add_vote(room, response, vote) {
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   490
            response.add(
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   491
                server_chat("Voting closed.".to_string())
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   492
                    .send_all()
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   493
                    .in_room(room.id),
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   494
            );
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   495
            let voting = replace(&mut room.voting, None).unwrap();
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   496
            if res {
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   497
                apply_voting_result(server, room_id, response, voting.kind);
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   498
            }
14686
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14683
diff changeset
   499
        }
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14683
diff changeset
   500
    }
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14683
diff changeset
   501
}
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14683
diff changeset
   502
14688
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   503
pub fn start_game(server: &mut HWServer, room_id: RoomId, response: &mut Response) {
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   504
    let (room_clients, room_nicks): (Vec<_>, Vec<_>) = server
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   505
        .clients
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   506
        .iter()
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   507
        .map(|(id, c)| (id, c.nick.clone()))
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   508
        .unzip();
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   509
    let room = &mut server.rooms[room_id];
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   510
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   511
    if !room.has_multiple_clans() {
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   512
        response.add(
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   513
            Warning("The game can't be started with less than two clans!".to_string()).send_self(),
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   514
        );
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   515
    } else if room.protocol_number <= 43 && room.players_number != room.ready_players_number {
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   516
        response.add(Warning("Not all players are ready".to_string()).send_self());
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   517
    } else if room.game_info.is_some() {
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   518
        response.add(Warning("The game is already in progress".to_string()).send_self());
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   519
    } else {
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   520
        room.start_round();
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   521
        for id in room_clients {
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   522
            let c = &mut server.clients[id];
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   523
            c.set_is_in_game(false);
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   524
            c.team_indices = room.client_team_indices(c.id);
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   525
        }
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   526
        response.add(RunGame.send_all().in_room(room.id));
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   527
        response.add(
14782
50fcef24003f add typed protocol flags
alfadur
parents: 14781
diff changeset
   528
            ClientFlags(add_flags(&[Flags::InGame]), room_nicks)
14688
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   529
                .send_all()
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   530
                .in_room(room.id),
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   531
        );
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   532
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   533
        let room_master = if let Some(id) = room.master_id {
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   534
            Some(&server.clients[id])
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   535
        } else {
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   536
            None
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   537
        };
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   538
        get_room_update(None, room, room_master, response);
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   539
    }
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   540
}
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   541
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   542
pub fn end_game(server: &mut HWServer, room_id: RoomId, response: &mut Response) {
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   543
    let room = &mut server.rooms[room_id];
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   544
    room.ready_players_number = 1;
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   545
    let room_master = if let Some(id) = room.master_id {
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   546
        Some(&server.clients[id])
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   547
    } else {
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   548
        None
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   549
    };
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   550
    get_room_update(None, room, room_master, response);
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   551
    response.add(RoundFinished.send_all().in_room(room_id));
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   552
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   553
    if let Some(info) = replace(&mut room.game_info, None) {
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   554
        for (_, client) in server.clients.iter() {
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   555
            if client.room_id == Some(room_id) && client.is_joined_mid_game() {
14689
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   556
                super::common::get_room_config(room, client.id, response);
14688
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   557
                response.extend(
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   558
                    info.left_teams
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   559
                        .iter()
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   560
                        .map(|name| TeamRemove(name.clone()).send(client.id)),
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   561
                );
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   562
            }
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   563
        }
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   564
    }
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   565
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   566
    let nicks: Vec<_> = server
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   567
        .clients
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   568
        .iter_mut()
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   569
        .filter(|(_, c)| c.room_id == Some(room_id))
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   570
        .map(|(_, c)| {
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   571
            c.set_is_ready(c.is_master());
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   572
            c.set_is_joined_mid_game(false);
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   573
            c
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   574
        })
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   575
        .filter_map(|c| {
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   576
            if !c.is_master() {
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   577
                Some(c.nick.clone())
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   578
            } else {
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   579
                None
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   580
            }
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   581
        })
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   582
        .collect();
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   583
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   584
    if !nicks.is_empty() {
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   585
        let msg = if room.protocol_number < 38 {
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   586
            LegacyReady(false, nicks)
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   587
        } else {
14782
50fcef24003f add typed protocol flags
alfadur
parents: 14781
diff changeset
   588
            ClientFlags(remove_flags(&[Flags::Ready]), nicks)
14688
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   589
        };
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   590
        response.add(msg.send_all().in_room(room_id));
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   591
    }
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   592
}
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   593
13444
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   594
#[cfg(test)]
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   595
mod tests {
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   596
    use super::*;
13796
59ea2403f62d move everything test related into test cfg
alfadur
parents: 13666
diff changeset
   597
    use crate::protocol::messages::HWServerMessage::ChatMsg;
14683
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14675
diff changeset
   598
    use crate::server::actions::PendingMessage;
13444
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   599
13445
d3c86ade3d4d Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13444
diff changeset
   600
    fn reply2string(r: HWServerMessage) -> String {
d3c86ade3d4d Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13444
diff changeset
   601
        match r {
d3c86ade3d4d Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13444
diff changeset
   602
            ChatMsg { msg: p, .. } => String::from(p),
d3c86ade3d4d Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13444
diff changeset
   603
            _ => panic!("expected a ChatMsg"),
13444
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   604
        }
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   605
    }
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   606
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   607
    fn run_handle_test(opts: Vec<String>) {
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   608
        let opts2 = opts.clone();
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   609
        for opt in opts {
13521
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13446
diff changeset
   610
            while reply2string(rnd_reply(&opts2)) != opt {}
13444
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   611
        }
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   612
    }
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   613
13446
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   614
    /// This test terminates almost surely.
13444
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   615
    #[test]
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   616
    fn test_handle_rnd_empty() {
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   617
        run_handle_test(vec![])
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   618
    }
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   619
13446
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   620
    /// This test terminates almost surely.
13444
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   621
    #[test]
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   622
    fn test_handle_rnd_nonempty() {
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   623
        run_handle_test(vec!["A".to_owned(), "B".to_owned(), "C".to_owned()])
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   624
    }
13446
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   625
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   626
    /// This test terminates almost surely (strong law of large numbers)
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   627
    #[test]
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   628
    fn test_distribution() {
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   629
        let eps = 0.000001;
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   630
        let lim = 0.5;
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   631
        let opts = vec![0.to_string(), 1.to_string()];
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   632
        let mut ones = 0;
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   633
        let mut tries = 0;
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   634
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   635
        while tries < 1000 || ((ones as f64 / tries as f64) - lim).abs() >= eps {
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   636
            tries += 1;
13521
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13446
diff changeset
   637
            if reply2string(rnd_reply(&opts)) == 1.to_string() {
13446
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   638
                ones += 1;
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   639
            }
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   640
        }
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   641
    }
13444
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   642
}