rust/hedgewars-server/src/server/handlers/common.rs
author alfadur <mail@none>
Mon, 04 Feb 2019 20:25:35 +0300
changeset 14673 08a8605bafaf
parent 14457 98ef2913ec73
child 14674 b87c71ccd17d
permissions -rw-r--r--
Server action refactoring part 4 of N
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13524
diff changeset
     1
use crate::{
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13524
diff changeset
     2
    protocol::messages::{
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
     3
        HWProtocolMessage::{self, Rnd},
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
     4
        HWServerMessage::{self, ChatMsg},
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
     5
    },
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
     6
    server::{actions::Action, core::HWServer},
13444
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
     7
};
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
     8
use rand::{self, thread_rng, Rng};
13444
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
     9
13524
5359ff75da3a indulge clippy
alfadur
parents: 13521
diff changeset
    10
pub fn rnd_reply(options: &[String]) -> HWServerMessage {
13521
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13446
diff changeset
    11
    let mut rng = thread_rng();
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13446
diff changeset
    12
    let reply = if options.is_empty() {
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13446
diff changeset
    13
        (*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
    14
    } else {
13521
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13446
diff changeset
    15
        rng.choose(&options).unwrap().clone()
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13446
diff changeset
    16
    };
13445
d3c86ade3d4d Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13444
diff changeset
    17
13521
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13446
diff changeset
    18
    ChatMsg {
13444
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
    19
        nick: "[random]".to_owned(),
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
    20
        msg: reply.clone(),
13521
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13446
diff changeset
    21
    }
13444
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
    22
}
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
    23
14673
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14457
diff changeset
    24
pub fn remove_client(server: &mut HWServer, response: &mut super::Response, msg: String) {
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14457
diff changeset
    25
    use HWServerMessage::*;
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14457
diff changeset
    26
    let nick = server.clients[response.client_id()].nick.clone();
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14457
diff changeset
    27
    response.add(LobbyLeft(nick, msg.to_string()).send_all());
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14457
diff changeset
    28
    response.add(Bye("User quit: ".to_string() + &msg).send_self());
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14457
diff changeset
    29
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14457
diff changeset
    30
    server.remove_client(response.client_id());
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14457
diff changeset
    31
}
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14457
diff changeset
    32
13444
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
    33
#[cfg(test)]
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
    34
mod tests {
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
    35
    use super::*;
13796
59ea2403f62d move everything test related into test cfg
alfadur
parents: 13666
diff changeset
    36
    use crate::protocol::messages::HWServerMessage::ChatMsg;
59ea2403f62d move everything test related into test cfg
alfadur
parents: 13666
diff changeset
    37
    use crate::server::actions::{
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    38
        Action::{self, Send},
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
    39
        PendingMessage,
13444
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
    40
    };
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
    41
13445
d3c86ade3d4d Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13444
diff changeset
    42
    fn reply2string(r: HWServerMessage) -> String {
d3c86ade3d4d Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13444
diff changeset
    43
        match r {
d3c86ade3d4d Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13444
diff changeset
    44
            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
    45
            _ => panic!("expected a ChatMsg"),
13444
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
    46
        }
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
    47
    }
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
    48
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
    49
    fn run_handle_test(opts: Vec<String>) {
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
    50
        let opts2 = opts.clone();
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
    51
        for opt in opts {
13521
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13446
diff changeset
    52
            while reply2string(rnd_reply(&opts2)) != opt {}
13444
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
    53
        }
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
    54
    }
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
    55
13446
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
    56
    /// This test terminates almost surely.
13444
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
    57
    #[test]
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
    58
    fn test_handle_rnd_empty() {
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
    59
        run_handle_test(vec![])
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
    60
    }
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
    61
13446
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
    62
    /// This test terminates almost surely.
13444
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
    63
    #[test]
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
    64
    fn test_handle_rnd_nonempty() {
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
    65
        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
    66
    }
13446
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
    67
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
    68
    /// 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
    69
    #[test]
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
    70
    fn test_distribution() {
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
    71
        let eps = 0.000001;
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
    72
        let lim = 0.5;
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
    73
        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
    74
        let mut ones = 0;
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
    75
        let mut tries = 0;
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
    76
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
    77
        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
    78
            tries += 1;
13521
ba5211dddb21 Assorted chat fixes
alfadur
parents: 13446
diff changeset
    79
            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
    80
                ones += 1;
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
    81
            }
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
    82
        }
dd2e51f7303d Add an extra test for Rnd's distribution
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13445
diff changeset
    83
    }
13444
914f9b970f4d Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
diff changeset
    84
}