gameServer2/src/server/handlers/common.rs
changeset 13445 d3c86ade3d4d
parent 13444 914f9b970f4d
child 13446 dd2e51f7303d
equal deleted inserted replaced
13444:914f9b970f4d 13445:d3c86ade3d4d
     1 use protocol::messages::{
     1 use protocol::messages::{
     2     HWProtocolMessage::{self, Rnd}, HWServerMessage::ChatMsg,
     2     HWProtocolMessage::{self, Rnd},
       
     3     HWServerMessage::{self, ChatMsg},
     3 };
     4 };
     4 use rand::{self, Rng};
     5 use rand::{self, Rng};
     5 use server::{actions::Action, server::HWServer};
     6 use server::{
       
     7     actions::Action,
       
     8     room::HWRoom,
       
     9     server::HWServer
       
    10 };
     6 
    11 
     7 pub fn rnd_reply(options: Vec<String>) -> Vec<Action> {
    12 pub fn rnd_action(options: Vec<String>, room: Option<&mut HWRoom>) -> Vec<Action> {
       
    13     if let Some(room) = room {
       
    14         let msg = rnd_reply(options);
       
    15         vec![msg.send_all().in_room(room.id).action()]
       
    16     } else {
       
    17         Vec::new()
       
    18     }
       
    19 }
       
    20 
       
    21 fn rnd_reply(options: Vec<String>) -> HWServerMessage {
     8     let options = if options.is_empty() {
    22     let options = if options.is_empty() {
     9         vec!["heads".to_owned(), "tails".to_owned()]
    23         vec!["heads".to_owned(), "tails".to_owned()]
    10     } else {
    24     } else {
    11         options
    25         options
    12     };
    26     };
    13     let reply = rand::thread_rng().choose(&options).unwrap();
    27     let reply = rand::thread_rng().choose(&options).unwrap();
    14     let msg = ChatMsg {
    28     let msg = ChatMsg {
    15         nick: "[random]".to_owned(),
    29         nick: "[random]".to_owned(),
    16         msg: reply.clone(),
    30         msg: reply.clone(),
    17     };
    31     };
    18     let msg = msg.send_all().action();
    32     msg
    19     vec![msg]
       
    20 }
    33 }
    21 
    34 
    22 #[cfg(test)]
    35 #[cfg(test)]
    23 mod tests {
    36 mod tests {
    24     use super::*;
    37     use super::*;
    25     use protocol::messages::HWServerMessage::ChatMsg;
    38     use protocol::messages::HWServerMessage::ChatMsg;
    26     use server::actions::{
    39     use server::actions::{
    27         Action::{self, Send}, PendingMessage,
    40         Action::{self, Send}, PendingMessage,
    28     };
    41     };
    29 
    42 
    30     fn reply2string(mut r: Vec<Action>) -> String {
    43     fn reply2string(r: HWServerMessage) -> String {
    31         assert_eq!(r.len(), 1);
    44         match r {
    32         match r.remove(0) {
    45             ChatMsg { msg: p, .. } => String::from(p),
    33             Send(PendingMessage {
    46             _ => panic!("expected a ChatMsg"),
    34                 message: ChatMsg { msg: p, .. },
       
    35                 ..
       
    36             }) => String::from(p),
       
    37             _ => panic!("reply should be a string"),
       
    38         }
    47         }
    39     }
    48     }
    40 
    49 
    41     fn run_handle_test(opts: Vec<String>) {
    50     fn run_handle_test(opts: Vec<String>) {
    42         let opts2 = opts.clone();
    51         let opts2 = opts.clone();