gameServer2/src/server/handlers/common.rs
changeset 13521 ba5211dddb21
parent 13446 dd2e51f7303d
child 13524 5359ff75da3a
equal deleted inserted replaced
13520:1ee192f13456 13521:ba5211dddb21
     1 use protocol::messages::{
     1 use protocol::messages::{
     2     HWProtocolMessage::{self, Rnd}, HWServerMessage::{self, ChatMsg},
     2     HWProtocolMessage::{self, Rnd}, HWServerMessage::{self, ChatMsg},
     3 };
     3 };
     4 use rand::{self, Rng};
     4 use rand::{self, Rng, thread_rng};
     5 use server::{actions::Action, room::HWRoom, server::HWServer};
     5 use server::{actions::Action, server::HWServer};
     6 
     6 
     7 pub fn rnd_action(options: Vec<String>, room: Option<&mut HWRoom>) -> Vec<Action> {
     7 pub fn rnd_reply(options: &Vec<String>) -> HWServerMessage {
     8     if let Some(room) = room {
     8     let mut rng = thread_rng();
     9         let msg = rnd_reply(options);
     9     let reply = if options.is_empty() {
    10         vec![msg.send_all().in_room(room.id).action()]
    10         (*rng.choose(&["heads", "tails"]).unwrap()).to_owned()
    11     } else {
    11     } else {
    12         Vec::new()
    12         rng.choose(&options).unwrap().clone()
    13     }
    13     };
    14 }
       
    15 
    14 
    16 fn rnd_reply(options: Vec<String>) -> HWServerMessage {
    15     ChatMsg {
    17     let options = if options.is_empty() {
       
    18         vec!["heads".to_owned(), "tails".to_owned()]
       
    19     } else {
       
    20         options
       
    21     };
       
    22     let reply = rand::thread_rng().choose(&options).unwrap();
       
    23     let msg = ChatMsg {
       
    24         nick: "[random]".to_owned(),
    16         nick: "[random]".to_owned(),
    25         msg: reply.clone(),
    17         msg: reply.clone(),
    26     };
    18     }
    27     msg
       
    28 }
    19 }
    29 
    20 
    30 #[cfg(test)]
    21 #[cfg(test)]
    31 mod tests {
    22 mod tests {
    32     use super::*;
    23     use super::*;
    43     }
    34     }
    44 
    35 
    45     fn run_handle_test(opts: Vec<String>) {
    36     fn run_handle_test(opts: Vec<String>) {
    46         let opts2 = opts.clone();
    37         let opts2 = opts.clone();
    47         for opt in opts {
    38         for opt in opts {
    48             while reply2string(rnd_reply(opts2.clone())) != opt {}
    39             while reply2string(rnd_reply(&opts2)) != opt {}
    49         }
    40         }
    50     }
    41     }
    51 
    42 
    52     /// This test terminates almost surely.
    43     /// This test terminates almost surely.
    53     #[test]
    44     #[test]
    70         let mut ones = 0;
    61         let mut ones = 0;
    71         let mut tries = 0;
    62         let mut tries = 0;
    72 
    63 
    73         while tries < 1000 || ((ones as f64 / tries as f64) - lim).abs() >= eps {
    64         while tries < 1000 || ((ones as f64 / tries as f64) - lim).abs() >= eps {
    74             tries += 1;
    65             tries += 1;
    75             if reply2string(rnd_reply(opts.clone())) == 1.to_string() {
    66             if reply2string(rnd_reply(&opts)) == 1.to_string() {
    76                 ones += 1;
    67                 ones += 1;
    77             }
    68             }
    78         }
    69         }
    79     }
    70     }
    80 }
    71 }