rust/hedgewars-server/src/handlers/common.rs
author alfadur <mail@none>
Sat, 21 Dec 2019 00:26:17 +0300
changeset 15518 e705d30e0f10
parent 15516 b907b9071ec5
child 15519 b3157d218ae2
permissions -rw-r--r--
some more room handler cleanup
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13524
diff changeset
     1
use crate::{
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
     2
    core::{
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
     3
        client::HwClient,
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
     4
        room::HwRoom,
15516
b907b9071ec5 move end_game into server
alfadur
parents: 15492
diff changeset
     5
        server::{
b907b9071ec5 move end_game into server
alfadur
parents: 15492
diff changeset
     6
            EndGameResult, HwServer, JoinRoomError, LeaveRoomError, LeaveRoomResult, StartGameError,
b907b9071ec5 move end_game into server
alfadur
parents: 15492
diff changeset
     7
        },
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
     8
        types::{ClientId, GameCfg, RoomId, TeamInfo, Vote, VoteType},
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
     9
    },
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13524
diff changeset
    10
    protocol::messages::{
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
    11
        add_flags, remove_flags, server_chat,
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
    12
        HwProtocolMessage::{self, Rnd},
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
    13
        HwServerMessage::{self, *},
14782
50fcef24003f add typed protocol flags
alfadur
parents: 14781
diff changeset
    14
        ProtocolFlags as Flags,
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    15
    },
14688
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
    16
    utils::to_engine_msg,
13444
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
    17
};
14688
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
    18
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
    19
use super::Response;
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
    20
15074
c5a6e8566425 shuffle server files
alfadur
parents: 14891
diff changeset
    21
use crate::core::types::RoomConfig;
14697
f64e21f164a5 fix some warnings
alfadur
parents: 14696
diff changeset
    22
use rand::{self, seq::SliceRandom, thread_rng, Rng};
14688
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
    23
use std::{iter::once, mem::replace};
13444
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
    24
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
    25
pub fn rnd_reply(options: &[String]) -> HwServerMessage {
13521
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13446
diff changeset
    26
    let mut rng = thread_rng();
14697
f64e21f164a5 fix some warnings
alfadur
parents: 14696
diff changeset
    27
13521
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13446
diff changeset
    28
    let reply = if options.is_empty() {
14697
f64e21f164a5 fix some warnings
alfadur
parents: 14696
diff changeset
    29
        (*&["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
    30
    } else {
14697
f64e21f164a5 fix some warnings
alfadur
parents: 14696
diff changeset
    31
        options.choose(&mut rng).unwrap().clone()
13521
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13446
diff changeset
    32
    };
13445
d3c86ade3d4d Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13444
diff changeset
    33
13521
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13446
diff changeset
    34
    ChatMsg {
14697
f64e21f164a5 fix some warnings
alfadur
parents: 14696
diff changeset
    35
        nick: "[random]".to_string(),
f64e21f164a5 fix some warnings
alfadur
parents: 14696
diff changeset
    36
        msg: reply,
13521
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13446
diff changeset
    37
    }
13444
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
    38
}
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
    39
15441
61a0bd0bb021 refactor root handler
alfadur
parents: 15439
diff changeset
    40
pub fn get_lobby_join_data(server: &HwServer, response: &mut Response) {
14683
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14675
diff changeset
    41
    let client_id = response.client_id();
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14675
diff changeset
    42
15441
61a0bd0bb021 refactor root handler
alfadur
parents: 15439
diff changeset
    43
    let client = server.client(client_id);
14791
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    44
    let nick = vec![client.nick.clone()];
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    45
    let mut flags = vec![];
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    46
    if client.is_registered() {
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    47
        flags.push(Flags::Registered)
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    48
    }
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    49
    if client.is_admin() {
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    50
        flags.push(Flags::Admin)
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    51
    }
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    52
    if client.is_contributor() {
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    53
        flags.push(Flags::Contributor)
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    54
    }
14683
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14675
diff changeset
    55
14791
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    56
    let all_nicks: Vec<_> = server.collect_nicks(|_| true);
14683
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14675
diff changeset
    57
14791
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    58
    let mut flag_selectors = [
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    59
        (
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    60
            Flags::Registered,
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    61
            server.collect_nicks(|(_, c)| c.is_registered()),
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    62
        ),
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    63
        (Flags::Admin, server.collect_nicks(|(_, c)| c.is_admin())),
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    64
        (
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    65
            Flags::Contributor,
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    66
            server.collect_nicks(|(_, c)| c.is_contributor()),
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    67
        ),
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    68
        (
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    69
            Flags::InRoom,
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    70
            server.collect_nicks(|(_, c)| c.room_id.is_some()),
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    71
        ),
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    72
    ];
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    73
15441
61a0bd0bb021 refactor root handler
alfadur
parents: 15439
diff changeset
    74
    let server_msg = ServerMessage(server.get_greetings(client).to_string());
14683
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14675
diff changeset
    75
14781
01f8ab45f806 fix lobby joining
alfadur
parents: 14697
diff changeset
    76
    let rooms_msg = Rooms(
01f8ab45f806 fix lobby joining
alfadur
parents: 14697
diff changeset
    77
        server
01f8ab45f806 fix lobby joining
alfadur
parents: 14697
diff changeset
    78
            .rooms
14683
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14675
diff changeset
    79
            .iter()
14801
da0cfeee9d96 don't show incompatible rooms
alfadur
parents: 14800
diff changeset
    80
            .filter(|(_, r)| r.protocol_number == client.protocol_number)
15492
395be40faa51 move start_game into the server
alfadur
parents: 15487
diff changeset
    81
            .flat_map(|(_, r)| r.info(r.master_id.map(|id| server.client(id))))
14781
01f8ab45f806 fix lobby joining
alfadur
parents: 14697
diff changeset
    82
            .collect(),
01f8ab45f806 fix lobby joining
alfadur
parents: 14697
diff changeset
    83
    );
14683
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14675
diff changeset
    84
14791
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    85
    response.add(LobbyJoined(nick).send_all().but_self());
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    86
    response.add(
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    87
        ClientFlags(add_flags(&flags), all_nicks.clone())
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    88
            .send_all()
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    89
            .but_self(),
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    90
    );
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    91
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    92
    response.add(LobbyJoined(all_nicks).send_self());
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    93
    for (flag, nicks) in &mut flag_selectors {
14891
e72d8c2299dd fix broken messages on lobby join
alfadur
parents: 14801
diff changeset
    94
        if !nicks.is_empty() {
e72d8c2299dd fix broken messages on lobby join
alfadur
parents: 14801
diff changeset
    95
            response.add(ClientFlags(add_flags(&[*flag]), replace(nicks, vec![])).send_self());
e72d8c2299dd fix broken messages on lobby join
alfadur
parents: 14801
diff changeset
    96
        }
14791
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    97
    }
b889d9e1115f improve lobby joining
alfadur
parents: 14790
diff changeset
    98
14781
01f8ab45f806 fix lobby joining
alfadur
parents: 14697
diff changeset
    99
    response.add(server_msg.send_self());
01f8ab45f806 fix lobby joining
alfadur
parents: 14697
diff changeset
   100
    response.add(rooms_msg.send_self());
14683
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14675
diff changeset
   101
}
932ff7683653 Server action refactoring part 8 of N
alfadur <mail@none>
parents: 14675
diff changeset
   102
15439
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15111
diff changeset
   103
pub fn get_room_join_data<'a, I: Iterator<Item = &'a HwClient> + Clone>(
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15111
diff changeset
   104
    client: &HwClient,
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15111
diff changeset
   105
    room: &HwRoom,
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15111
diff changeset
   106
    room_clients: I,
14787
0e64acbc3f8b implement follow message
alfadur
parents: 14785
diff changeset
   107
    response: &mut Response,
0e64acbc3f8b implement follow message
alfadur
parents: 14785
diff changeset
   108
) {
15439
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15111
diff changeset
   109
    #[inline]
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15111
diff changeset
   110
    fn collect_nicks<'a, I, F>(clients: I, f: F) -> Vec<String>
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15111
diff changeset
   111
    where
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15111
diff changeset
   112
        I: Iterator<Item = &'a HwClient>,
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15111
diff changeset
   113
        F: Fn(&&'a HwClient) -> bool,
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15111
diff changeset
   114
    {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15111
diff changeset
   115
        clients.filter(f).map(|c| &c.nick).cloned().collect()
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15111
diff changeset
   116
    }
14787
0e64acbc3f8b implement follow message
alfadur
parents: 14785
diff changeset
   117
15439
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15111
diff changeset
   118
    let nick = client.nick.clone();
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15111
diff changeset
   119
    response.add(RoomJoined(vec![nick.clone()]).send_all().in_room(room.id));
14787
0e64acbc3f8b implement follow message
alfadur
parents: 14785
diff changeset
   120
    response.add(ClientFlags(add_flags(&[Flags::InRoom]), vec![nick]).send_all());
15439
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15111
diff changeset
   121
    let nicks = collect_nicks(room_clients.clone(), |c| c.room_id == Some(room.id));
14787
0e64acbc3f8b implement follow message
alfadur
parents: 14785
diff changeset
   122
    response.add(RoomJoined(nicks).send_self());
0e64acbc3f8b implement follow message
alfadur
parents: 14785
diff changeset
   123
15439
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15111
diff changeset
   124
    get_room_teams(room, client.id, response);
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15111
diff changeset
   125
    get_room_config(room, client.id, response);
14799
e41131607167 restore setup sending on room join
alfadur
parents: 14798
diff changeset
   126
e41131607167 restore setup sending on room join
alfadur
parents: 14798
diff changeset
   127
    let mut flag_selectors = [
e41131607167 restore setup sending on room join
alfadur
parents: 14798
diff changeset
   128
        (
e41131607167 restore setup sending on room join
alfadur
parents: 14798
diff changeset
   129
            Flags::RoomMaster,
15439
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15111
diff changeset
   130
            collect_nicks(room_clients.clone(), |c| c.is_master()),
14799
e41131607167 restore setup sending on room join
alfadur
parents: 14798
diff changeset
   131
        ),
15439
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15111
diff changeset
   132
        (
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15111
diff changeset
   133
            Flags::Ready,
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15111
diff changeset
   134
            collect_nicks(room_clients.clone(), |c| c.is_ready()),
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15111
diff changeset
   135
        ),
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15111
diff changeset
   136
        (
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15111
diff changeset
   137
            Flags::InGame,
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15111
diff changeset
   138
            collect_nicks(room_clients.clone(), |c| c.is_in_game()),
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15111
diff changeset
   139
        ),
14799
e41131607167 restore setup sending on room join
alfadur
parents: 14798
diff changeset
   140
    ];
e41131607167 restore setup sending on room join
alfadur
parents: 14798
diff changeset
   141
e41131607167 restore setup sending on room join
alfadur
parents: 14798
diff changeset
   142
    for (flag, nicks) in &mut flag_selectors {
e41131607167 restore setup sending on room join
alfadur
parents: 14798
diff changeset
   143
        response.add(ClientFlags(add_flags(&[*flag]), replace(nicks, vec![])).send_self());
e41131607167 restore setup sending on room join
alfadur
parents: 14798
diff changeset
   144
    }
14787
0e64acbc3f8b implement follow message
alfadur
parents: 14785
diff changeset
   145
0e64acbc3f8b implement follow message
alfadur
parents: 14785
diff changeset
   146
    if !room.greeting.is_empty() {
0e64acbc3f8b implement follow message
alfadur
parents: 14785
diff changeset
   147
        response.add(
0e64acbc3f8b implement follow message
alfadur
parents: 14785
diff changeset
   148
            ChatMsg {
0e64acbc3f8b implement follow message
alfadur
parents: 14785
diff changeset
   149
                nick: "[greeting]".to_string(),
0e64acbc3f8b implement follow message
alfadur
parents: 14785
diff changeset
   150
                msg: room.greeting.clone(),
0e64acbc3f8b implement follow message
alfadur
parents: 14785
diff changeset
   151
            }
0e64acbc3f8b implement follow message
alfadur
parents: 14785
diff changeset
   152
            .send_self(),
0e64acbc3f8b implement follow message
alfadur
parents: 14785
diff changeset
   153
        );
0e64acbc3f8b implement follow message
alfadur
parents: 14785
diff changeset
   154
    }
0e64acbc3f8b implement follow message
alfadur
parents: 14785
diff changeset
   155
}
0e64acbc3f8b implement follow message
alfadur
parents: 14785
diff changeset
   156
15439
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15111
diff changeset
   157
pub fn get_room_join_error(error: JoinRoomError, response: &mut Response) {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15111
diff changeset
   158
    use super::strings::*;
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15111
diff changeset
   159
    match error {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15111
diff changeset
   160
        JoinRoomError::DoesntExist => response.warn(NO_ROOM),
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15111
diff changeset
   161
        JoinRoomError::WrongProtocol => response.warn(WRONG_PROTOCOL),
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15111
diff changeset
   162
        JoinRoomError::Full => response.warn(ROOM_FULL),
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15111
diff changeset
   163
        JoinRoomError::Restricted => response.warn(ROOM_JOIN_RESTRICTED),
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15111
diff changeset
   164
    }
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15111
diff changeset
   165
}
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15111
diff changeset
   166
15482
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   167
pub fn get_remove_teams_data(
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   168
    room_id: RoomId,
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   169
    was_in_game: bool,
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   170
    removed_teams: Vec<String>,
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   171
    response: &mut Response,
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   172
) {
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   173
    if was_in_game {
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   174
        for team_name in &removed_teams {
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   175
            let msg = once(b'F').chain(team_name.bytes());
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   176
            response.add(
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   177
                ForwardEngineMessage(vec![to_engine_msg(msg)])
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   178
                    .send_all()
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   179
                    .in_room(room_id)
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   180
                    .but_self(),
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   181
            );
14690
f61ce544d436 Server action refactoring part N of N
alfadur <mail@none>
parents: 14689
diff changeset
   182
15482
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   183
            let remove_msg = to_engine_msg(once(b'F').chain(team_name.bytes()));
14690
f61ce544d436 Server action refactoring part N of N
alfadur <mail@none>
parents: 14689
diff changeset
   184
15482
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   185
            response.add(
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   186
                ForwardEngineMessage(vec![remove_msg])
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   187
                    .send_all()
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   188
                    .in_room(room_id)
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   189
                    .but_self(),
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   190
            );
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   191
        }
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   192
    }
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   193
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   194
    for team_name in removed_teams {
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   195
        response.add(TeamRemove(team_name).send_all().in_room(room_id));
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   196
    }
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   197
}
14690
f61ce544d436 Server action refactoring part N of N
alfadur <mail@none>
parents: 14689
diff changeset
   198
15492
395be40faa51 move start_game into the server
alfadur
parents: 15487
diff changeset
   199
pub fn get_room_leave_result(
15482
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   200
    server: &HwServer,
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   201
    room: &HwRoom,
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   202
    leave_message: &str,
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   203
    result: LeaveRoomResult,
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   204
    response: &mut Response,
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   205
) {
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   206
    let client = server.client(response.client_id);
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   207
    response.add(ClientFlags(remove_flags(&[Flags::InRoom]), vec![client.nick.clone()]).send_all());
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   208
15516
b907b9071ec5 move end_game into server
alfadur
parents: 15492
diff changeset
   209
    match result {
15482
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   210
        LeaveRoomResult::RoomRemoved => {
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   211
            response.add(
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   212
                RoomRemove(room.name.clone())
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   213
                    .send_all()
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   214
                    .with_protocol(room.protocol_number),
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   215
            );
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   216
        }
14690
f61ce544d436 Server action refactoring part N of N
alfadur <mail@none>
parents: 14689
diff changeset
   217
15482
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   218
        LeaveRoomResult::RoomRemains {
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   219
            is_empty,
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   220
            was_master,
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   221
            new_master,
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   222
            was_in_game,
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   223
            removed_teams,
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   224
        } => {
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   225
            if !is_empty {
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   226
                response.add(
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   227
                    RoomLeft(client.nick.clone(), leave_message.to_string())
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   228
                        .send_all()
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   229
                        .in_room(room.id)
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   230
                        .but_self(),
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   231
                );
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   232
            }
14798
d11481978633 fix room exiting
alfadur
parents: 14791
diff changeset
   233
15482
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   234
            if was_master {
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   235
                response.add(
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   236
                    ClientFlags(
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   237
                        remove_flags(&[Flags::RoomMaster]),
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   238
                        vec![client.nick.clone()],
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   239
                    )
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   240
                    .send_all()
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   241
                    .in_room(room.id),
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   242
                );
14798
d11481978633 fix room exiting
alfadur
parents: 14791
diff changeset
   243
15482
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   244
                if let Some(new_master_id) = new_master {
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   245
                    let new_master_nick = server.client(new_master_id).nick.clone();
14798
d11481978633 fix room exiting
alfadur
parents: 14791
diff changeset
   246
                    response.add(
d11481978633 fix room exiting
alfadur
parents: 14791
diff changeset
   247
                        ClientFlags(add_flags(&[Flags::RoomMaster]), vec![new_master_nick])
d11481978633 fix room exiting
alfadur
parents: 14791
diff changeset
   248
                            .send_all()
d11481978633 fix room exiting
alfadur
parents: 14791
diff changeset
   249
                            .in_room(room.id),
d11481978633 fix room exiting
alfadur
parents: 14791
diff changeset
   250
                    );
14694
25c564f77b7d remove lobby room
alfadur <mail@none>
parents: 14691
diff changeset
   251
                }
14690
f61ce544d436 Server action refactoring part N of N
alfadur <mail@none>
parents: 14689
diff changeset
   252
            }
15482
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   253
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   254
            get_remove_teams_data(room.id, was_in_game, removed_teams, response);
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   255
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   256
            response.add(
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   257
                RoomUpdated(room.name.clone(), room.info(Some(&client)))
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   258
                    .send_all()
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   259
                    .with_protocol(room.protocol_number),
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   260
            );
14690
f61ce544d436 Server action refactoring part N of N
alfadur <mail@none>
parents: 14689
diff changeset
   261
        }
f61ce544d436 Server action refactoring part N of N
alfadur <mail@none>
parents: 14689
diff changeset
   262
    }
f61ce544d436 Server action refactoring part N of N
alfadur <mail@none>
parents: 14689
diff changeset
   263
}
f61ce544d436 Server action refactoring part N of N
alfadur <mail@none>
parents: 14689
diff changeset
   264
15492
395be40faa51 move start_game into the server
alfadur
parents: 15487
diff changeset
   265
pub fn get_room_leave_data(
395be40faa51 move start_game into the server
alfadur
parents: 15487
diff changeset
   266
    server: &HwServer,
395be40faa51 move start_game into the server
alfadur
parents: 15487
diff changeset
   267
    room_id: RoomId,
395be40faa51 move start_game into the server
alfadur
parents: 15487
diff changeset
   268
    leave_message: &str,
395be40faa51 move start_game into the server
alfadur
parents: 15487
diff changeset
   269
    result: Result<LeaveRoomResult, LeaveRoomError>,
395be40faa51 move start_game into the server
alfadur
parents: 15487
diff changeset
   270
    response: &mut Response,
395be40faa51 move start_game into the server
alfadur
parents: 15487
diff changeset
   271
) {
395be40faa51 move start_game into the server
alfadur
parents: 15487
diff changeset
   272
    match result {
395be40faa51 move start_game into the server
alfadur
parents: 15487
diff changeset
   273
        Ok(result) => {
395be40faa51 move start_game into the server
alfadur
parents: 15487
diff changeset
   274
            let room = server.room(room_id);
395be40faa51 move start_game into the server
alfadur
parents: 15487
diff changeset
   275
            get_room_leave_result(server, room, leave_message, result, response)
395be40faa51 move start_game into the server
alfadur
parents: 15487
diff changeset
   276
        }
395be40faa51 move start_game into the server
alfadur
parents: 15487
diff changeset
   277
        Err(_) => (),
395be40faa51 move start_game into the server
alfadur
parents: 15487
diff changeset
   278
    }
395be40faa51 move start_game into the server
alfadur
parents: 15487
diff changeset
   279
}
395be40faa51 move start_game into the server
alfadur
parents: 15487
diff changeset
   280
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
   281
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
   282
    let client_id = response.client_id();
15482
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   283
    let client = server.client(client_id);
14697
f64e21f164a5 fix some warnings
alfadur
parents: 14696
diff changeset
   284
    let nick = client.nick.clone();
14674
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14673
diff changeset
   285
15482
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   286
    if let Some(room_id) = client.room_id {
15492
395be40faa51 move start_game into the server
alfadur
parents: 15487
diff changeset
   287
        let result = server.leave_room(client_id);
395be40faa51 move start_game into the server
alfadur
parents: 15487
diff changeset
   288
        get_room_leave_data(server, room_id, &msg, result, response);
15482
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15441
diff changeset
   289
    }
14674
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14673
diff changeset
   290
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14673
diff changeset
   291
    server.remove_client(client_id);
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14673
diff changeset
   292
15441
61a0bd0bb021 refactor root handler
alfadur
parents: 15439
diff changeset
   293
    response.add(LobbyLeft(nick, msg.clone()).send_all());
61a0bd0bb021 refactor root handler
alfadur
parents: 15439
diff changeset
   294
    response.add(Bye(msg).send_self());
14696
8a45c90f4580 fix client removal
alfadur
parents: 14694
diff changeset
   295
    response.remove_client(client_id);
14673
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14457
diff changeset
   296
}
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14457
diff changeset
   297
14686
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14683
diff changeset
   298
pub fn get_room_update(
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14683
diff changeset
   299
    room_name: Option<String>,
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
   300
    room: &HwRoom,
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
   301
    master: Option<&HwClient>,
14688
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   302
    response: &mut Response,
14686
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14683
diff changeset
   303
) {
14687
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   304
    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
   305
    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
   306
}
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14683
diff changeset
   307
14785
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14784
diff changeset
   308
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
   309
    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
   310
    for cfg in config.to_game_config() {
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14784
diff changeset
   311
        response.add(cfg.to_server_msg().send(to_client));
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14784
diff changeset
   312
    }
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14784
diff changeset
   313
}
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14784
diff changeset
   314
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
   315
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
   316
    get_room_config_impl(room.active_config(), to_client, response);
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14784
diff changeset
   317
}
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14784
diff changeset
   318
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14784
diff changeset
   319
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
   320
where
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14784
diff changeset
   321
    I: Iterator<Item = &'a TeamInfo>,
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
    for team in teams {
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14784
diff changeset
   324
        response.add(TeamAdd(team.to_protocol()).send(to_client));
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14784
diff changeset
   325
        response.add(TeamColor(team.name.clone(), team.color).send(to_client));
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14784
diff changeset
   326
        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
   327
    }
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   328
}
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   329
15439
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15111
diff changeset
   330
pub fn get_room_teams(room: &HwRoom, to_client: ClientId, response: &mut Response) {
14689
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   331
    let current_teams = match room.game_info {
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   332
        Some(ref info) => &info.teams_at_start,
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   333
        None => &room.teams,
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   334
    };
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   335
14785
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14784
diff changeset
   336
    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
   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_flags(
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
   340
    server: &HwServer,
14689
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
    if let Some(id) = room.master_id {
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   347
        response.add(
14782
50fcef24003f add typed protocol flags
alfadur
parents: 14781
diff changeset
   348
            ClientFlags(
50fcef24003f add typed protocol flags
alfadur
parents: 14781
diff changeset
   349
                add_flags(&[Flags::RoomMaster]),
15492
395be40faa51 move start_game into the server
alfadur
parents: 15487
diff changeset
   350
                vec![server.client(id).nick.clone()],
14782
50fcef24003f add typed protocol flags
alfadur
parents: 14781
diff changeset
   351
            )
50fcef24003f add typed protocol flags
alfadur
parents: 14781
diff changeset
   352
            .send(to_client),
14689
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
    }
15492
395be40faa51 move start_game into the server
alfadur
parents: 15487
diff changeset
   355
    let nicks = server.collect_nicks(|(_, c)| c.room_id == Some(room_id) && c.is_ready());
395be40faa51 move start_game into the server
alfadur
parents: 15487
diff changeset
   356
14689
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   357
    if !nicks.is_empty() {
14782
50fcef24003f add typed protocol flags
alfadur
parents: 14781
diff changeset
   358
        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
   359
    }
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   360
}
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   361
14687
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   362
pub fn apply_voting_result(
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
   363
    server: &mut HwServer,
14687
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   364
    room_id: RoomId,
14688
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   365
    response: &mut Response,
14687
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   366
    kind: VoteType,
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   367
) {
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   368
    match kind {
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   369
        VoteType::Kick(nick) => {
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   370
            if let Some(client) = server.find_client(&nick) {
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   371
                if client.room_id == Some(room_id) {
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   372
                    let id = client.id;
14690
f61ce544d436 Server action refactoring part N of N
alfadur <mail@none>
parents: 14689
diff changeset
   373
                    response.add(Kicked.send(id));
15492
395be40faa51 move start_game into the server
alfadur
parents: 15487
diff changeset
   374
                    let result = server.leave_room(id);
395be40faa51 move start_game into the server
alfadur
parents: 15487
diff changeset
   375
                    get_room_leave_data(server, room_id, "kicked", result, response);
14687
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   376
                }
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   377
            }
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   378
        }
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   379
        VoteType::Map(None) => (),
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   380
        VoteType::Map(Some(name)) => {
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   381
            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
   382
                response.add(
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   383
                    server_chat(location.to_string())
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   384
                        .send_all()
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   385
                        .in_room(room_id),
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   386
                );
14688
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   387
                let room = &server.rooms[room_id];
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   388
                let room_master = if let Some(id) = room.master_id {
15492
395be40faa51 move start_game into the server
alfadur
parents: 15487
diff changeset
   389
                    Some(server.client(id))
14688
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   390
                } else {
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   391
                    None
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   392
                };
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   393
                get_room_update(None, room, room_master, response);
14687
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   394
14689
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   395
                for (_, client) in server.clients.iter() {
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   396
                    if client.room_id == Some(room_id) {
aae29ba56aec Server action refactoring part C of N
alfadur <mail@none>
parents: 14688
diff changeset
   397
                        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
   398
                    }
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   399
                }
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   400
            }
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   401
        }
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   402
        VoteType::Pause => {
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   403
            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
   404
                info.is_paused = !info.is_paused;
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   405
                response.add(
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   406
                    server_chat("Pause toggled.".to_string())
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   407
                        .send_all()
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   408
                        .in_room(room_id),
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   409
                );
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   410
                response.add(
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   411
                    ForwardEngineMessage(vec![to_engine_msg(once(b'I'))])
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   412
                        .send_all()
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   413
                        .in_room(room_id),
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   414
                );
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   415
            }
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
        VoteType::NewSeed => {
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   418
            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
   419
            let cfg = GameCfg::Seed(seed);
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   420
            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
   421
            server.rooms[room_id].set_config(cfg);
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   422
        }
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   423
        VoteType::HedgehogsPerTeam(number) => {
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   424
            let r = &mut server.rooms[room_id];
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   425
            let nicks = r.set_hedgehogs_number(number);
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   426
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   427
            response.extend(
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   428
                nicks
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   429
                    .into_iter()
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   430
                    .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
   431
            );
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
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
   436
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
   437
    let client_id = response.client_id;
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14683
diff changeset
   438
    let mut result = None;
14687
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   439
14686
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14683
diff changeset
   440
    if let Some(ref mut voting) = room.voting {
14687
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   441
        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
   442
            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
   443
            voting.votes.push((client_id, vote.is_pro));
14686
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14683
diff changeset
   444
            let i = voting.votes.iter();
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14683
diff changeset
   445
            let pro = i.clone().filter(|(_, v)| *v).count();
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14683
diff changeset
   446
            let contra = i.filter(|(_, v)| !*v).count();
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14683
diff changeset
   447
            let success_quota = voting.voters.len() / 2 + 1;
14687
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   448
            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
   449
                result = Some(true);
14687
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   450
            } 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
   451
            {
14686
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14683
diff changeset
   452
                result = Some(false);
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14683
diff changeset
   453
            }
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14683
diff changeset
   454
        } else {
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14683
diff changeset
   455
            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
   456
        }
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14683
diff changeset
   457
    } else {
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14683
diff changeset
   458
        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
   459
    }
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14683
diff changeset
   460
14687
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   461
    result
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   462
}
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   463
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
   464
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
   465
    let client_id = response.client_id;
15492
395be40faa51 move start_game into the server
alfadur
parents: 15487
diff changeset
   466
    let client = server.client(client_id);
14687
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   467
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   468
    if let Some(room_id) = client.room_id {
15492
395be40faa51 move start_game into the server
alfadur
parents: 15487
diff changeset
   469
        let room = server.room_mut(room_id);
14687
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   470
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   471
        if let Some(res) = add_vote(room, response, vote) {
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   472
            response.add(
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   473
                server_chat("Voting closed.".to_string())
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   474
                    .send_all()
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   475
                    .in_room(room.id),
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   476
            );
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   477
            let voting = replace(&mut room.voting, None).unwrap();
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   478
            if res {
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   479
                apply_voting_result(server, room_id, response, voting.kind);
5122c584804e Server action refactoring part A of N
alfadur <mail@none>
parents: 14686
diff changeset
   480
            }
14686
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14683
diff changeset
   481
        }
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14683
diff changeset
   482
    }
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14683
diff changeset
   483
}
9f98086de1b6 Server action refactoring part 9 of N
alfadur <mail@none>
parents: 14683
diff changeset
   484
15492
395be40faa51 move start_game into the server
alfadur
parents: 15487
diff changeset
   485
pub fn get_start_game_data(
395be40faa51 move start_game into the server
alfadur
parents: 15487
diff changeset
   486
    server: &HwServer,
395be40faa51 move start_game into the server
alfadur
parents: 15487
diff changeset
   487
    room_id: RoomId,
395be40faa51 move start_game into the server
alfadur
parents: 15487
diff changeset
   488
    result: Result<Vec<String>, StartGameError>,
395be40faa51 move start_game into the server
alfadur
parents: 15487
diff changeset
   489
    response: &mut Response,
395be40faa51 move start_game into the server
alfadur
parents: 15487
diff changeset
   490
) {
395be40faa51 move start_game into the server
alfadur
parents: 15487
diff changeset
   491
    match result {
395be40faa51 move start_game into the server
alfadur
parents: 15487
diff changeset
   492
        Ok(room_nicks) => {
395be40faa51 move start_game into the server
alfadur
parents: 15487
diff changeset
   493
            let room = server.room(room_id);
395be40faa51 move start_game into the server
alfadur
parents: 15487
diff changeset
   494
            response.add(RunGame.send_all().in_room(room.id));
395be40faa51 move start_game into the server
alfadur
parents: 15487
diff changeset
   495
            response.add(
395be40faa51 move start_game into the server
alfadur
parents: 15487
diff changeset
   496
                ClientFlags(add_flags(&[Flags::InGame]), room_nicks)
395be40faa51 move start_game into the server
alfadur
parents: 15487
diff changeset
   497
                    .send_all()
395be40faa51 move start_game into the server
alfadur
parents: 15487
diff changeset
   498
                    .in_room(room.id),
395be40faa51 move start_game into the server
alfadur
parents: 15487
diff changeset
   499
            );
14688
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   500
15492
395be40faa51 move start_game into the server
alfadur
parents: 15487
diff changeset
   501
            let room_master = room.master_id.map(|id| server.client(id));
395be40faa51 move start_game into the server
alfadur
parents: 15487
diff changeset
   502
            get_room_update(None, room, room_master, response);
14688
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   503
        }
15492
395be40faa51 move start_game into the server
alfadur
parents: 15487
diff changeset
   504
        Err(StartGameError::NotEnoughClans) => {
395be40faa51 move start_game into the server
alfadur
parents: 15487
diff changeset
   505
            response.warn("The game can't be started with less than two clans!")
395be40faa51 move start_game into the server
alfadur
parents: 15487
diff changeset
   506
        }
395be40faa51 move start_game into the server
alfadur
parents: 15487
diff changeset
   507
        Err(StartGameError::NotEnoughTeams) => (),
395be40faa51 move start_game into the server
alfadur
parents: 15487
diff changeset
   508
        Err(StartGameError::NotReady) => response.warn("Not all players are ready"),
15516
b907b9071ec5 move end_game into server
alfadur
parents: 15492
diff changeset
   509
        Err(StartGameError::AlreadyInGame) => response.warn("The game is already in progress"),
14688
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
}
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   512
15516
b907b9071ec5 move end_game into server
alfadur
parents: 15492
diff changeset
   513
pub fn get_end_game_result(
b907b9071ec5 move end_game into server
alfadur
parents: 15492
diff changeset
   514
    server: &HwServer,
b907b9071ec5 move end_game into server
alfadur
parents: 15492
diff changeset
   515
    room_id: RoomId,
b907b9071ec5 move end_game into server
alfadur
parents: 15492
diff changeset
   516
    result: EndGameResult,
b907b9071ec5 move end_game into server
alfadur
parents: 15492
diff changeset
   517
    response: &mut Response,
b907b9071ec5 move end_game into server
alfadur
parents: 15492
diff changeset
   518
) {
b907b9071ec5 move end_game into server
alfadur
parents: 15492
diff changeset
   519
    let room = server.room(room_id);
14688
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   520
    let room_master = if let Some(id) = room.master_id {
15516
b907b9071ec5 move end_game into server
alfadur
parents: 15492
diff changeset
   521
        Some(server.client(id))
14688
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   522
    } else {
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   523
        None
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   524
    };
15516
b907b9071ec5 move end_game into server
alfadur
parents: 15492
diff changeset
   525
14688
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   526
    get_room_update(None, room, room_master, response);
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   527
    response.add(RoundFinished.send_all().in_room(room_id));
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   528
15516
b907b9071ec5 move end_game into server
alfadur
parents: 15492
diff changeset
   529
    for client_id in result.joined_mid_game_clients {
b907b9071ec5 move end_game into server
alfadur
parents: 15492
diff changeset
   530
        super::common::get_room_config(room, client_id, response);
b907b9071ec5 move end_game into server
alfadur
parents: 15492
diff changeset
   531
        response.extend(
b907b9071ec5 move end_game into server
alfadur
parents: 15492
diff changeset
   532
            result
b907b9071ec5 move end_game into server
alfadur
parents: 15492
diff changeset
   533
                .left_teams
b907b9071ec5 move end_game into server
alfadur
parents: 15492
diff changeset
   534
                .iter()
b907b9071ec5 move end_game into server
alfadur
parents: 15492
diff changeset
   535
                .map(|name| TeamRemove(name.clone()).send(client_id)),
b907b9071ec5 move end_game into server
alfadur
parents: 15492
diff changeset
   536
        );
14688
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
15516
b907b9071ec5 move end_game into server
alfadur
parents: 15492
diff changeset
   539
    if !result.unreadied_nicks.is_empty() {
14688
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   540
        let msg = if room.protocol_number < 38 {
15516
b907b9071ec5 move end_game into server
alfadur
parents: 15492
diff changeset
   541
            LegacyReady(false, result.unreadied_nicks)
14688
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   542
        } else {
15516
b907b9071ec5 move end_game into server
alfadur
parents: 15492
diff changeset
   543
            ClientFlags(remove_flags(&[Flags::Ready]), result.unreadied_nicks)
14688
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   544
        };
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   545
        response.add(msg.send_all().in_room(room_id));
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   546
    }
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   547
}
4569d8d50286 Server action refactoring part B of N
alfadur <mail@none>
parents: 14687
diff changeset
   548
13444
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   549
#[cfg(test)]
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   550
mod tests {
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   551
    use super::*;
15111
1e45db229f9f make greeting argument optional
alfadur
parents: 15075
diff changeset
   552
    use crate::handlers::actions::PendingMessage;
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
   553
    use crate::protocol::messages::HwServerMessage::ChatMsg;
13444
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   554
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
   555
    fn reply2string(r: HwServerMessage) -> String {
13445
d3c86ade3d4d Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13444
diff changeset
   556
        match r {
d3c86ade3d4d Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13444
diff changeset
   557
            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
   558
            _ => panic!("expected a ChatMsg"),
13444
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   559
        }
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   560
    }
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   561
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   562
    fn run_handle_test(opts: Vec<String>) {
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   563
        let opts2 = opts.clone();
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   564
        for opt in opts {
13521
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13446
diff changeset
   565
            while reply2string(rnd_reply(&opts2)) != opt {}
13444
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   566
        }
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   567
    }
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   568
13446
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   569
    /// This test terminates almost surely.
13444
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   570
    #[test]
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   571
    fn test_handle_rnd_empty() {
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   572
        run_handle_test(vec![])
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   573
    }
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   574
13446
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   575
    /// This test terminates almost surely.
13444
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   576
    #[test]
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   577
    fn test_handle_rnd_nonempty() {
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   578
        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
   579
    }
13446
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   580
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   581
    /// 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
   582
    #[test]
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   583
    fn test_distribution() {
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   584
        let eps = 0.000001;
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   585
        let lim = 0.5;
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   586
        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
   587
        let mut ones = 0;
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   588
        let mut tries = 0;
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   589
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   590
        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
   591
            tries += 1;
13521
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13446
diff changeset
   592
            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
   593
                ones += 1;
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   594
            }
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   595
        }
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   596
    }
13444
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   597
}