Fix cmd parsing & update tests
authoralfadur
Mon, 02 Jul 2018 16:25:49 +0300
changeset 13432 ee3fa3b8809d
parent 13431 6a818f9192f4
child 13433 fb104e150878
Fix cmd parsing & update tests
gameServer2/src/protocol/messages.rs
gameServer2/src/protocol/parser.rs
gameServer2/src/protocol/test.rs
--- a/gameServer2/src/protocol/messages.rs	Fri Jun 29 18:27:51 2018 +0300
+++ b/gameServer2/src/protocol/messages.rs	Mon Jul 02 16:25:49 2018 +0300
@@ -130,85 +130,6 @@
     }
 }
 
-impl<'a> HWProtocolMessage {
-    pub fn to_raw_protocol(&self) -> String {
-        use self::HWProtocolMessage::*;
-        match self {
-            Ping => "PING\n\n".to_string(),
-            Pong => "PONG\n\n".to_string(),
-            Quit(None) => format!("QUIT\n\n"),
-            Quit(Some(msg)) => format!("QUIT\n{}\n\n", msg),
-            Global(msg) => format!("CMD\nGLOBAL\n{}\n\n", msg),
-            Watch(name) => format!("CMD\nWATCH\n{}\n\n", name),
-            ToggleServerRegisteredOnly => "CMD\nREGISTERED_ONLY\n\n".to_string(),
-            SuperPower => "CMD\nSUPER_POWER\n\n".to_string(),
-            Info(info) => format!("CMD\nINFO\n{}\n\n", info),
-            Nick(nick) => format!("NICK\n{}\n\n", nick),
-            Proto(version) => format!("PROTO\n{}\n\n", version),
-            Password(p, s) => format!("PASSWORD\n{}\n{}\n\n", p, s), //?
-            Checker(i, n, p) =>
-                format!("CHECKER\n{}\n{}\n{}\n\n", i, n, p), //?,
-            List => "LIST\n\n".to_string(),
-            Chat(msg) => format!("CHAT\n{}\n\n", msg),
-            CreateRoom(name, None) =>
-                format!("CREATE_ROOM\n{}\n\n", name),
-            CreateRoom(name, Some(password)) =>
-                format!("CREATE_ROOM\n{}\n{}\n\n", name, password),
-            JoinRoom(name, None) =>
-                format!("JOIN\n{}\n\n", name),
-            JoinRoom(name, Some(arg)) =>
-                format!("JOIN\n{}\n{}\n\n", name, arg),
-            Follow(name) =>
-                format!("FOLLOW\n{}\n\n", name),
-            //Rnd(Vec<String>), ???
-            Kick(name) => format!("KICK\n{}\n\n", name),
-            Ban(name, reason, time) =>
-                format!("BAN\n{}\n{}\n{}\n\n", name, reason, time),
-            BanIP(ip, reason, time) =>
-                format!("BAN_IP\n{}\n{}\n{}\n\n", ip, reason, time),
-            BanNick(nick, reason, time) =>
-                format!("BAN_NICK\n{}\n{}\n{}\n\n", nick, reason, time),
-            BanList => "BANLIST\n\n".to_string(),
-            Unban(name) => format!("UNBAN\n{}\n\n", name),
-            //SetServerVar(ServerVar), ???
-            GetServerVar => "GET_SERVER_VAR\n\n".to_string(),
-            RestartServer => "CMD\nRESTART_SERVER\nYES\n\n".to_string(),
-            Stats => "CMD\nSTATS\n\n".to_string(),
-            Part(None) => "CMD\nPART\n\n".to_string(),
-            Part(Some(msg)) => format!("CMD\nPART\n{}\n\n", msg),
-            //Cfg(GameCfg) ??
-            //AddTeam(TeamInfo) ??,
-            RemoveTeam(name) => format!("REMOVE_TEAM\n{}\n\n", name),
-            //SetHedgehogsNumber(String, u8), ??
-            //SetTeamColor(String, u8), ??
-            ToggleReady => "TOGGLE_READY\n\n".to_string(),
-            StartGame => "START_GAME\n\n".to_string(),
-            EngineMessage(msg) => format!("EM\n{}\n\n", msg),
-            RoundFinished => "ROUNDFINISHED\n\n".to_string(),
-            ToggleRestrictJoin => "TOGGLE_RESTRICT_JOINS\n\n".to_string(),
-            ToggleRestrictTeams => "TOGGLE_RESTRICT_TEAMS\n\n".to_string(),
-            ToggleRegisteredOnly => "TOGGLE_REGISTERED_ONLY\n\n".to_string(),
-            RoomName(name) => format!("ROOM_NAME\n{}\n\n", name),
-            Delegate(name) => format!("CMD\nDELEGATE\n{}\n\n", name),
-            TeamChat(msg) => format!("TEAMCHAT\n{}\n\n", msg),
-            MaxTeams(count) => format!("CMD\nMAXTEAMS\n{}\n\n", count) ,
-            Fix => "CMD\nFIX\n\n".to_string(),
-            Unfix => "CMD\nUNFIX\n\n".to_string(),
-            Greeting(msg) => format!("CMD\nGREETING\n{}\n\n", msg),
-            //CallVote(Option<(String, Option<String>)>) =>, ??
-            Vote(msg) => format!("CMD\nVOTE\n{}\n\n", msg),
-            ForceVote(msg) => format!("CMD\nFORCE\n{}\n\n", msg),
-            //Save(String, String), ??
-            Delete(room) => format!("CMD\nDELETE\n{}\n\n", room),
-            SaveRoom(room) => format!("CMD\nSAVEROOM\n{}\n\n", room),
-            LoadRoom(room) => format!("CMD\nLOADROOM\n{}\n\n", room),
-            Malformed => "A\nQUICK\nBROWN\nHOG\nJUMPS\nOVER\nTHE\nLAZY\nDOG\n\n".to_string(),
-            Empty => "\n\n".to_string(),
-            _ => panic!("Protocol message not yet implemented")
-        }
-    }
-}
-
 macro_rules! const_braces {
     ($e: expr) => { "{}\n" }
 }
@@ -219,6 +140,79 @@
     };
 }
 
+impl<'a> HWProtocolMessage {
+    pub fn to_raw_protocol(&self) -> String {
+        use self::HWProtocolMessage::*;
+        match self {
+            Ping => msg!["PING"],
+            Pong => msg!["PONG"],
+            Quit(None) => msg!["QUIT"],
+            Quit(Some(msg)) => msg!["QUIT", msg],
+            Global(msg) => msg!["CMD", format!("GLOBAL {}", msg)],
+            Watch(name) => msg!["CMD", format!("WATCH {}", name)],
+            ToggleServerRegisteredOnly => msg!["CMD", "REGISTERED_ONLY"],
+            SuperPower =>  msg!["CMD", "SUPER_POWER"],
+            Info(info) => msg!["CMD", format!("INFO {}", info)],
+            Nick(nick) => msg!("NICK", nick),
+            Proto(version) => msg!["PROTO", version],
+            Password(p, s) => msg!["PASSWORD", p, s],
+            Checker(i, n, p) => msg!["CHECKER", i, n, p],
+            List => msg!["LIST"],
+            Chat(msg) => msg!["CHAT", msg],
+            CreateRoom(name, None) => msg!["CREATE_ROOM", name],
+            CreateRoom(name, Some(password)) =>
+                msg!["CREATE_ROOM", name, password],
+            JoinRoom(name, None) => msg!["JOIN_ROOM", name],
+            JoinRoom(name, Some(password)) =>
+                msg!["JOIN_ROOM", name, password],
+            Follow(name) => msg!["FOLLOW", name],
+            Rnd(args) => msg!["RND", args.join(" ")],
+            Kick(name) => msg!["KICK", name],
+            Ban(name, reason, time) => msg!["BAN", name, reason, time],
+            BanIP(ip, reason, time) => msg!["BAN_IP", ip, reason, time],
+            BanNick(nick, reason, time) =>
+                msg!("BAN_NICK", nick, reason, time),
+            BanList => msg!["BANLIST"],
+            Unban(name) => msg!["UNBAN", name],
+            //SetServerVar(ServerVar), ???
+            GetServerVar => msg!["GET_SERVER_VAR"],
+            RestartServer => msg!["CMD", "RESTART_SERVER YES"],
+            Stats => msg!["CMD", "STATS"],
+            Part(None) => msg!["PART"],
+            Part(Some(msg)) => msg!["PART", msg],
+            //Cfg(GameCfg) =>
+            //AddTeam(info) =>
+            RemoveTeam(name) => msg!["REMOVE_TEAM", name],
+            //SetHedgehogsNumber(team, number), ??
+            //SetTeamColor(team, color), ??
+            ToggleReady => msg!["TOGGLE_READY"],
+            StartGame => msg!["START_GAME"],
+            EngineMessage(msg) => msg!["EM", msg],
+            RoundFinished => msg!["ROUNDFINISHED"],
+            ToggleRestrictJoin => msg!["TOGGLE_RESTRICT_JOINS"],
+            ToggleRestrictTeams => msg!["TOGGLE_RESTRICT_TEAMS"],
+            ToggleRegisteredOnly => msg!["TOGGLE_REGISTERED_ONLY"],
+            RoomName(name) => msg!["ROOM_NAME", name],
+            Delegate(name) => msg!["CMD", format!("DELEGATE {}", name)],
+            TeamChat(msg) => msg!["TEAMCHAT", msg],
+            MaxTeams(count) => msg!["CMD", format!("MAXTEAMS {}", count)] ,
+            Fix => msg!["CMD", "FIX"],
+            Unfix => msg!["CMD", "UNFIX"],
+            Greeting(msg) => msg!["CMD", format!("GREETING {}", msg)],
+            //CallVote(Option<(String, Option<String>)>) =>, ??
+            Vote(msg) => msg!["CMD", format!("VOTE {}", msg)],
+            ForceVote(msg) => msg!["CMD", format!("FORCE {}", msg)],
+            //Save(String, String), ??
+            Delete(room) => msg!["CMD", format!("DELETE {}", room)],
+            SaveRoom(room) => msg!["CMD", format!("SAVEROOM {}", room)],
+            LoadRoom(room) => msg!["CMD", format!("LOADROOM {}", room)],
+            Malformed => msg!["A", "QUICK", "BROWN", "HOG", "JUMPS", "OVER", "THE", "LAZY", "DOG"],
+            Empty => msg![""],
+            _ => panic!("Protocol message not yet implemented")
+        }
+    }
+}
+
 fn construct_message(header: &[&str], msg: &Vec<String>) -> String {
     let mut v: Vec<_> = header.iter().map(|s| *s).collect();
     v.extend(msg.iter().map(|s| &s[..]));
--- a/gameServer2/src/protocol/parser.rs	Fri Jun 29 18:27:51 2018 +0300
+++ b/gameServer2/src/protocol/parser.rs	Mon Jul 02 16:25:49 2018 +0300
@@ -27,6 +27,8 @@
 named!( u8_line<&[u8],     u8>, map_res!(str_line, FromStr::from_str));
 named!(u32_line<&[u8],    u32>, map_res!(str_line, FromStr::from_str));
 named!(opt_param<&[u8], Option<String> >, opt!(map!(flat_map!(preceded!(eol, str_line), non_empty), String::from)));
+named!(spaces<&[u8], &[u8]>, preceded!(tag!(" "), eat_separator!(" ")));
+named!(opt_space_param<&[u8], Option<String> >, opt!(map!(flat_map!(preceded!(spaces, str_line), non_empty), String::from)));
 named!(hog_line<&[u8], HedgehogInfo>,
     do_parse!(name: str_line >> eol >> hat: str_line >>
         (HedgehogInfo{name: name.to_string(), hat: hat.to_string()})));
@@ -76,24 +78,24 @@
       do_parse!(tag_no_case!("STATS") >> (Stats))
     | do_parse!(tag_no_case!("FIX")   >> (Fix))
     | do_parse!(tag_no_case!("UNFIX") >> (Unfix))
-    | do_parse!(tag_no_case!("RESTART_SERVER") >> eol >> tag!("YES") >> (RestartServer))
+    | do_parse!(tag_no_case!("RESTART_SERVER") >> spaces >> tag!("YES") >> (RestartServer))
     | do_parse!(tag_no_case!("REGISTERED_ONLY") >> (ToggleServerRegisteredOnly))
     | do_parse!(tag_no_case!("SUPER_POWER")     >> (SuperPower))
-    | do_parse!(tag_no_case!("PART")     >> m: opt_param >> (Part(m)))
-    | do_parse!(tag_no_case!("QUIT")     >> m: opt_param >> (Quit(m)))
-    | do_parse!(tag_no_case!("DELEGATE") >> eol >> n: a_line  >> (Delegate(n)))
-    | do_parse!(tag_no_case!("SAVEROOM") >> eol >> r: a_line  >> (SaveRoom(r)))
-    | do_parse!(tag_no_case!("LOADROOM") >> eol >> r: a_line  >> (LoadRoom(r)))
-    | do_parse!(tag_no_case!("DELETE")   >> eol >> r: a_line  >> (Delete(r)))
-    | do_parse!(tag_no_case!("GLOBAL")   >> eol >> m: a_line  >> (Global(m)))
-    | do_parse!(tag_no_case!("WATCH")    >> eol >> i: a_line  >> (Watch(i)))
-    | do_parse!(tag_no_case!("GREETING") >> eol >> m: a_line  >> (Greeting(m)))
-    | do_parse!(tag_no_case!("VOTE")     >> eol >> m: a_line  >> (Vote(m)))
-    | do_parse!(tag_no_case!("FORCE")    >> eol >> m: a_line  >> (ForceVote(m)))
-    | do_parse!(tag_no_case!("INFO")     >> eol >> n: a_line  >> (Info(n)))
-    | do_parse!(tag_no_case!("MAXTEAMS") >> eol >> n: u8_line >> (MaxTeams(n)))
+    | do_parse!(tag_no_case!("PART")     >> m: opt_space_param >> (Part(m)))
+    | do_parse!(tag_no_case!("QUIT")     >> m: opt_space_param >> (Quit(m)))
+    | do_parse!(tag_no_case!("DELEGATE") >> spaces >> n: a_line  >> (Delegate(n)))
+    | do_parse!(tag_no_case!("SAVEROOM") >> spaces >> r: a_line  >> (SaveRoom(r)))
+    | do_parse!(tag_no_case!("LOADROOM") >> spaces >> r: a_line  >> (LoadRoom(r)))
+    | do_parse!(tag_no_case!("DELETE")   >> spaces >> r: a_line  >> (Delete(r)))
+    | do_parse!(tag_no_case!("GLOBAL")   >> spaces >> m: a_line  >> (Global(m)))
+    | do_parse!(tag_no_case!("WATCH")    >> spaces >> i: a_line  >> (Watch(i)))
+    | do_parse!(tag_no_case!("GREETING") >> spaces >> m: a_line  >> (Greeting(m)))
+    | do_parse!(tag_no_case!("VOTE")     >> spaces >> m: a_line  >> (Vote(m)))
+    | do_parse!(tag_no_case!("FORCE")    >> spaces >> m: a_line  >> (ForceVote(m)))
+    | do_parse!(tag_no_case!("INFO")     >> spaces >> n: a_line  >> (Info(n)))
+    | do_parse!(tag_no_case!("MAXTEAMS") >> spaces >> n: u8_line >> (MaxTeams(n)))
     | do_parse!(
-        tag_no_case!("RND") >>
+        tag_no_case!("RND") >> alt!(spaces | peek!(end_of_message)) >>
         v: str_line >>
         (Rnd(v.split_whitespace().map(String::from).collect())))
 )));
@@ -232,11 +234,11 @@
     assert_eq!(message(b"PROTO\n51\n\n"),     IResult::Done(&b""[..], Proto(51)));
     assert_eq!(message(b"QUIT\nbye-bye\n\n"), IResult::Done(&b""[..], Quit(Some("bye-bye".to_string()))));
     assert_eq!(message(b"QUIT\n\n"),          IResult::Done(&b""[..], Quit(None)));
-    assert_eq!(message(b"CMD\nwatch\ndemo\n\n"), IResult::Done(&b""[..], Watch("demo".to_string())));
+    assert_eq!(message(b"CMD\nwatch demo\n\n"), IResult::Done(&b""[..], Watch("demo".to_string())));
     assert_eq!(message(b"BAN\nme\nbad\n77\n\n"), IResult::Done(&b""[..], Ban("me".to_string(), "bad".to_string(), 77)));
 
     assert_eq!(message(b"CMD\nPART\n\n"),      IResult::Done(&b""[..], Part(None)));
-    assert_eq!(message(b"CMD\nPART\n_msg_\n\n"), IResult::Done(&b""[..], Part(Some("_msg_".to_string()))));
+    assert_eq!(message(b"CMD\nPART _msg_\n\n"), IResult::Done(&b""[..], Part(Some("_msg_".to_string()))));
 
     assert_eq!(message(b"CMD\nRND\n\n"), IResult::Done(&b""[..], Rnd(vec![])));
     assert_eq!(
--- a/gameServer2/src/protocol/test.rs	Fri Jun 29 18:27:51 2018 +0300
+++ b/gameServer2/src/protocol/test.rs	Mon Jul 02 16:25:49 2018 +0300
@@ -37,6 +37,7 @@
     )
 }
 
+/// Wrapper type for generating non-empty strings
 #[derive(Debug)]
 struct Ascii(String);
 
@@ -121,7 +122,7 @@
         47 => Unfix(),
         48 => Greeting(Ascii),
         //49 => CallVote(Option<(String, Option<String>)>),
-        50 => Vote(String),
+        50 => Vote(Ascii),
         51 => ForceVote(Ascii),
         //52 => Save(String, String),
         53 => Delete(Ascii),