gameServer2/src/server/handlers/common.rs
changeset 13446 dd2e51f7303d
parent 13445 d3c86ade3d4d
child 13521 ba5211dddb21
equal deleted inserted replaced
13445:d3c86ade3d4d 13446:dd2e51f7303d
     1 use protocol::messages::{
     1 use protocol::messages::{
     2     HWProtocolMessage::{self, Rnd},
     2     HWProtocolMessage::{self, Rnd}, HWServerMessage::{self, ChatMsg},
     3     HWServerMessage::{self, ChatMsg},
       
     4 };
     3 };
     5 use rand::{self, Rng};
     4 use rand::{self, Rng};
     6 use server::{
     5 use server::{actions::Action, room::HWRoom, server::HWServer};
     7     actions::Action,
       
     8     room::HWRoom,
       
     9     server::HWServer
       
    10 };
       
    11 
     6 
    12 pub fn rnd_action(options: Vec<String>, room: Option<&mut HWRoom>) -> Vec<Action> {
     7 pub fn rnd_action(options: Vec<String>, room: Option<&mut HWRoom>) -> Vec<Action> {
    13     if let Some(room) = room {
     8     if let Some(room) = room {
    14         let msg = rnd_reply(options);
     9         let msg = rnd_reply(options);
    15         vec![msg.send_all().in_room(room.id).action()]
    10         vec![msg.send_all().in_room(room.id).action()]
    52         for opt in opts {
    47         for opt in opts {
    53             while reply2string(rnd_reply(opts2.clone())) != opt {}
    48             while reply2string(rnd_reply(opts2.clone())) != opt {}
    54         }
    49         }
    55     }
    50     }
    56 
    51 
       
    52     /// This test terminates almost surely.
    57     #[test]
    53     #[test]
    58     fn test_handle_rnd_empty() {
    54     fn test_handle_rnd_empty() {
    59         run_handle_test(vec![])
    55         run_handle_test(vec![])
    60     }
    56     }
    61 
    57 
       
    58     /// This test terminates almost surely.
    62     #[test]
    59     #[test]
    63     fn test_handle_rnd_nonempty() {
    60     fn test_handle_rnd_nonempty() {
    64         run_handle_test(vec!["A".to_owned(), "B".to_owned(), "C".to_owned()])
    61         run_handle_test(vec!["A".to_owned(), "B".to_owned(), "C".to_owned()])
    65     }
    62     }
       
    63 
       
    64     /// This test terminates almost surely (strong law of large numbers)
       
    65     #[test]
       
    66     fn test_distribution() {
       
    67         let eps = 0.000001;
       
    68         let lim = 0.5;
       
    69         let opts = vec![0.to_string(), 1.to_string()];
       
    70         let mut ones = 0;
       
    71         let mut tries = 0;
       
    72 
       
    73         while tries < 1000 || ((ones as f64 / tries as f64) - lim).abs() >= eps {
       
    74             tries += 1;
       
    75             if reply2string(rnd_reply(opts.clone())) == 1.to_string() {
       
    76                 ones += 1;
       
    77             }
       
    78         }
       
    79     }
    66 }
    80 }