gameServer2/src/protocol/messages.rs
changeset 12136 e25a82ce2374
parent 12135 23ee939ba66a
child 12137 193dfdcb0620
--- a/gameServer2/src/protocol/messages.rs	Wed Jan 11 22:42:59 2017 +0300
+++ b/gameServer2/src/protocol/messages.rs	Sat Jan 14 00:46:52 2017 +0300
@@ -9,6 +9,8 @@
     Ping,
     Pong,
     Quit(Option<&'a str>),
+    Bye(&'a str),
+    LobbyLeft(&'a str),
     //Cmd(&'a str, Vec<&'a str>),
     Global(&'a str),
     Watch(&'a str),
@@ -79,3 +81,31 @@
     }
     value
 }
+
+fn construct_message(msg: & [&str]) -> String {
+    let mut m = String::with_capacity(64);
+
+    for s in msg {
+        m.push_str(s);
+        m.push('\n');
+    }
+    m.push('\n');
+
+    m
+}
+
+impl<'a> HWProtocolMessage<'a> {
+    pub fn to_raw_protocol(&self) -> String {
+        match self {
+            &HWProtocolMessage::Ping
+                => "PING\n\n".to_string(),
+            &HWProtocolMessage::Pong
+                => "PONG\n\n".to_string(),
+            &HWProtocolMessage::Bye(msg)
+                => construct_message(&["BYE", msg]),
+            &HWProtocolMessage::LobbyLeft(msg)
+                => construct_message(&["LOBBY_LEFT", msg]),
+            _ => String::new()
+        }
+    }
+}