rust/hedgewars-server/src/server/handlers/common.rs
author alfadur <mail@none>
Mon, 04 Feb 2019 23:41:18 +0300
changeset 14695 b87c71ccd17d
parent 14694 08a8605bafaf
child 14696 dfe652c53470
permissions -rw-r--r--
Server action refactoring part 5 of N
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14695
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
     1
use crate::server::client::HWClient;
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
     2
use crate::server::room::HWRoom;
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
     3
use crate::utils::to_engine_msg;
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13500
diff changeset
     4
use crate::{
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13500
diff changeset
     5
    protocol::messages::{
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
     6
        HWProtocolMessage::{self, Rnd},
14695
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
     7
        HWServerMessage::{
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
     8
            self, Bye, ChatMsg, ClientFlags, ForwardEngineMessage, LobbyLeft, RoomLeft, RoomRemove,
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
     9
            RoomUpdated, TeamRemove,
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
    10
        },
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
    11
    },
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
    12
    server::{actions::Action, core::HWServer},
13444
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
    13
};
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
    14
use rand::{self, thread_rng, Rng};
14695
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
    15
use std::iter::once;
13444
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
    16
13500
5359ff75da3a indulge clippy
alfadur
parents: 13492
diff changeset
    17
pub fn rnd_reply(options: &[String]) -> HWServerMessage {
13492
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13446
diff changeset
    18
    let mut rng = thread_rng();
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13446
diff changeset
    19
    let reply = if options.is_empty() {
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13446
diff changeset
    20
        (*rng.choose(&["heads", "tails"]).unwrap()).to_owned()
13445
d3c86ade3d4d Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13444
diff changeset
    21
    } else {
13492
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13446
diff changeset
    22
        rng.choose(&options).unwrap().clone()
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13446
diff changeset
    23
    };
13445
d3c86ade3d4d Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13444
diff changeset
    24
13492
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13446
diff changeset
    25
    ChatMsg {
13444
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
    26
        nick: "[random]".to_owned(),
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
    27
        msg: reply.clone(),
13492
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13446
diff changeset
    28
    }
13444
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
    29
}
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
    30
14695
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
    31
pub fn remove_teams(room: &mut HWRoom, client: &HWClient, response: &mut super::Response) {
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
    32
    let team_names: Vec<_> = room
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
    33
        .client_teams(client.id)
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
    34
        .map(|t| t.name.clone())
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
    35
        .collect();
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
    36
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
    37
    if let Some(ref mut info) = room.game_info {
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
    38
        for team_name in &team_names {
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
    39
            info.left_teams.push(team_name.clone());
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
    40
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
    41
            if client.is_in_game() {
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
    42
                let msg = once(b'F').chain(team_name.bytes());
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
    43
                response.add(
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
    44
                    ForwardEngineMessage(vec![to_engine_msg(msg)])
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
    45
                        .send_all()
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
    46
                        .in_room(room.id)
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
    47
                        .but_self(),
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
    48
                );
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
    49
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
    50
                info.teams_in_game -= 1;
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
    51
                if info.teams_in_game == 0 {
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
    52
                    //FinishRoomGame(room.id)
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
    53
                }
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
    54
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
    55
                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: 14694
diff changeset
    56
                if let Some(m) = &info.sync_msg {
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
    57
                    info.msg_log.push(m.clone());
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
    58
                    info.sync_msg = None
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
    59
                }
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
    60
                info.msg_log.push(remove_msg.clone());
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
    61
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
    62
                response.add(
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
    63
                    ForwardEngineMessage(vec![remove_msg])
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
    64
                        .send_all()
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
    65
                        .in_room(room.id)
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
    66
                        .but_self(),
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
    67
                );
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
    68
            }
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
    69
        }
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
    70
    }
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
    71
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
    72
    for team_name in team_names {
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
    73
        room.remove_team(&team_name);
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
    74
        response.add(TeamRemove(team_name).send_all().in_room(room.id));
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
    75
    }
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
    76
}
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
    77
14694
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14478
diff changeset
    78
pub fn remove_client(server: &mut HWServer, response: &mut super::Response, msg: String) {
14695
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
    79
    let client_id = response.client_id();
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
    80
    let lobby_id = server.lobby_id;
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
    81
    let client = &mut server.clients[client_id];
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
    82
    let (nick, room_id) = (client.nick.clone(), client.room_id);
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
    83
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
    84
    if let Some(room_id) = room_id {
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
    85
        let room = &mut server.rooms[room_id];
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
    86
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
    87
        if room.players_number > 1 || room.is_fixed() {
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
    88
            room.players_number -= 1;
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
    89
            if client.is_ready() && room.ready_players_number > 0 {
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
    90
                room.ready_players_number -= 1;
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
    91
            }
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
    92
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
    93
            remove_teams(room, client, response);
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
    94
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
    95
            if room.players_number > 1 {
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
    96
                response.add(
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
    97
                    RoomLeft(client.nick.clone(), msg.clone())
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
    98
                        .send_all()
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
    99
                        .in_room(room.id)
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   100
                        .but_self(),
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   101
                );
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   102
            }
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   103
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   104
            //ChangeMaster(room.id, None));
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   105
        }
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   106
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   107
        let update_msg = if room.players_number == 0 && !room.is_fixed() {
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   108
            RoomRemove(room.name.clone())
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   109
        } else {
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   110
            RoomUpdated(room.name.clone(), room.info(Some(&client)))
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   111
        };
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   112
        response.add(update_msg.send_all().with_protocol(room.protocol_number));
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   113
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   114
        response.add(ClientFlags("-i".to_string(), vec![nick.clone()]).send_all());
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   115
        client.room_id = Some(lobby_id);
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   116
    }
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   117
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   118
    server.remove_client(client_id);
b87c71ccd17d Server action refactoring part 5 of N
alfadur <mail@none>
parents: 14694
diff changeset
   119
14694
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14478
diff changeset
   120
    response.add(LobbyLeft(nick, msg.to_string()).send_all());
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14478
diff changeset
   121
    response.add(Bye("User quit: ".to_string() + &msg).send_self());
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14478
diff changeset
   122
}
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14478
diff changeset
   123
13444
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   124
#[cfg(test)]
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   125
mod tests {
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   126
    use super::*;
13713
59ea2403f62d move everything test related into test cfg
alfadur
parents: 13666
diff changeset
   127
    use crate::protocol::messages::HWServerMessage::ChatMsg;
59ea2403f62d move everything test related into test cfg
alfadur
parents: 13666
diff changeset
   128
    use crate::server::actions::{
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   129
        Action::{self, Send},
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   130
        PendingMessage,
13444
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   131
    };
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   132
13445
d3c86ade3d4d Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13444
diff changeset
   133
    fn reply2string(r: HWServerMessage) -> String {
d3c86ade3d4d Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13444
diff changeset
   134
        match r {
d3c86ade3d4d Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13444
diff changeset
   135
            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
   136
            _ => panic!("expected a ChatMsg"),
13444
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   137
        }
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   138
    }
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   139
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   140
    fn run_handle_test(opts: Vec<String>) {
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   141
        let opts2 = opts.clone();
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   142
        for opt in opts {
13492
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13446
diff changeset
   143
            while reply2string(rnd_reply(&opts2)) != opt {}
13444
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   144
        }
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   145
    }
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   146
13446
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   147
    /// This test terminates almost surely.
13444
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   148
    #[test]
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   149
    fn test_handle_rnd_empty() {
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   150
        run_handle_test(vec![])
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   151
    }
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   152
13446
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   153
    /// This test terminates almost surely.
13444
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   154
    #[test]
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   155
    fn test_handle_rnd_nonempty() {
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   156
        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
   157
    }
13446
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   158
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   159
    /// 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
   160
    #[test]
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   161
    fn test_distribution() {
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   162
        let eps = 0.000001;
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   163
        let lim = 0.5;
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   164
        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
   165
        let mut ones = 0;
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   166
        let mut tries = 0;
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   167
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   168
        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
   169
            tries += 1;
13492
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13446
diff changeset
   170
            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
   171
                ones += 1;
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   172
            }
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   173
        }
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
   174
    }
13444
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
   175
}