gameServer2/src/protocol/messages.rs
changeset 12136 e25a82ce2374
parent 12135 23ee939ba66a
child 12137 193dfdcb0620
equal deleted inserted replaced
12135:23ee939ba66a 12136:e25a82ce2374
     7 pub enum HWProtocolMessage<'a> {
     7 pub enum HWProtocolMessage<'a> {
     8     // core
     8     // core
     9     Ping,
     9     Ping,
    10     Pong,
    10     Pong,
    11     Quit(Option<&'a str>),
    11     Quit(Option<&'a str>),
       
    12     Bye(&'a str),
       
    13     LobbyLeft(&'a str),
    12     //Cmd(&'a str, Vec<&'a str>),
    14     //Cmd(&'a str, Vec<&'a str>),
    13     Global(&'a str),
    15     Global(&'a str),
    14     Watch(&'a str),
    16     Watch(&'a str),
    15     ToggleServerRegisteredOnly,
    17     ToggleServerRegisteredOnly,
    16     SuperPower,
    18     SuperPower,
    77         value *= T::from(10);
    79         value *= T::from(10);
    78         value += T::from(digit);
    80         value += T::from(digit);
    79     }
    81     }
    80     value
    82     value
    81 }
    83 }
       
    84 
       
    85 fn construct_message(msg: & [&str]) -> String {
       
    86     let mut m = String::with_capacity(64);
       
    87 
       
    88     for s in msg {
       
    89         m.push_str(s);
       
    90         m.push('\n');
       
    91     }
       
    92     m.push('\n');
       
    93 
       
    94     m
       
    95 }
       
    96 
       
    97 impl<'a> HWProtocolMessage<'a> {
       
    98     pub fn to_raw_protocol(&self) -> String {
       
    99         match self {
       
   100             &HWProtocolMessage::Ping
       
   101                 => "PING\n\n".to_string(),
       
   102             &HWProtocolMessage::Pong
       
   103                 => "PONG\n\n".to_string(),
       
   104             &HWProtocolMessage::Bye(msg)
       
   105                 => construct_message(&["BYE", msg]),
       
   106             &HWProtocolMessage::LobbyLeft(msg)
       
   107                 => construct_message(&["LOBBY_LEFT", msg]),
       
   108             _ => String::new()
       
   109         }
       
   110     }
       
   111 }