fix callvote parsing
authoralfadur
Tue, 04 Jun 2019 01:32:08 +0300
changeset 15112 6a1ba3540fa0
parent 15111 1e45db229f9f
child 15113 7d6e519a88c5
fix callvote parsing
rust/hedgewars-server/src/protocol/messages.rs
rust/hedgewars-server/src/protocol/parser.rs
rust/hedgewars-server/src/protocol/test.rs
--- a/rust/hedgewars-server/src/protocol/messages.rs	Mon Jun 03 23:50:26 2019 +0300
+++ b/rust/hedgewars-server/src/protocol/messages.rs	Tue Jun 04 01:32:08 2019 +0300
@@ -173,17 +173,32 @@
 
 impl ServerVar {
     pub fn to_protocol(&self) -> Vec<String> {
+        use ServerVar::*;
         match self {
-            ServerVar::MOTDNew(s) => vec!["MOTD_NEW".to_string(), s.clone()],
-            ServerVar::MOTDOld(s) => vec!["MOTD_OLD".to_string(), s.clone()],
-            ServerVar::LatestProto(n) => vec!["LATEST_PROTO".to_string(), n.to_string()],
+            MOTDNew(s) => vec!["MOTD_NEW".to_string(), s.clone()],
+            MOTDOld(s) => vec!["MOTD_OLD".to_string(), s.clone()],
+            LatestProto(n) => vec!["LATEST_PROTO".to_string(), n.to_string()],
+        }
+    }
+}
+
+impl VoteType {
+    pub fn to_protocol(&self) -> Vec<String> {
+        use VoteType::*;
+        match self {
+            Kick(nick) => vec!["KICK".to_string(), nick.clone()],
+            Map(None) => vec!["MAP".to_string()],
+            Map(Some(name)) => vec!["MAP".to_string(), name.clone()],
+            Pause => vec!["PAUSE".to_string()],
+            NewSeed => vec!["NEWSEED".to_string()],
+            HedgehogsPerTeam(count) => vec!["HEDGEHOGS".to_string(), count.to_string()],
         }
     }
 }
 
 impl GameCfg {
     pub fn to_protocol(&self) -> (String, Vec<String>) {
-        use crate::core::types::GameCfg::*;
+        use GameCfg::*;
         match self {
             FeatureSize(s) => ("FEATURE_SIZE".to_string(), vec![s.to_string()]),
             MapType(t) => ("MAP".to_string(), vec![t.to_string()]),
@@ -325,7 +340,6 @@
             StartGame => msg!["START_GAME"],
             EngineMessage(msg) => msg!["EM", msg],
             RoundFinished => msg!["ROUNDFINISHED"],
-            ReplayStart => msg!["REPLAY_START"],
             ToggleRestrictJoin => msg!["TOGGLE_RESTRICT_JOINS"],
             ToggleRestrictTeams => msg!["TOGGLE_RESTRICT_TEAMS"],
             ToggleRegisteredOnly => msg!["TOGGLE_REGISTERED_ONLY"],
@@ -337,7 +351,10 @@
             Unfix => msg!["CMD", "UNFIX"],
             Greeting(None) => msg!["CMD", "GREETING"],
             Greeting(Some(msg)) => msg!["CMD", format!("GREETING {}", msg)],
-            //CallVote(Option<(String, Option<String>)>) =>, ??
+            CallVote(None) => msg!["CMD", "CALLVOTE"],
+            CallVote(Some(vote)) => {
+                msg!["CMD", format!("CALLVOTE {}", &vote.to_protocol().join(" "))]
+            }
             Vote(msg) => msg!["CMD", format!("VOTE {}", if *msg { "YES" } else { "NO" })],
             ForceVote(msg) => msg!["CMD", format!("FORCE {}", if *msg { "YES" } else { "NO" })],
             Save(name, location) => msg!["CMD", format!("SAVE {} {}", name, location)],
@@ -400,6 +417,7 @@
             Notice(msg) => msg!["NOTICE", msg],
             Warning(msg) => msg!["WARNING", msg],
             Error(msg) => msg!["ERROR", msg],
+            ReplayStart => msg!["REPLAY_START"],
 
             LegacyReady(is_ready, nicks) => {
                 construct_message(&[if *is_ready { "READY" } else { "NOT_READY" }], &nicks)
--- a/rust/hedgewars-server/src/protocol/parser.rs	Mon Jun 03 23:50:26 2019 +0300
+++ b/rust/hedgewars-server/src/protocol/parser.rs	Tue Jun 04 01:32:08 2019 +0300
@@ -277,7 +277,7 @@
             |i| cmdc_single_arg(i, "FORCE", yes_no_line, ForceVote),
             |i| cmdc_single_arg(i, "INFO", a_line, Info),
             |i| cmdc_single_arg(i, "MAXTEAMS", u8_line, MaxTeams),
-            |i| cmdc_single_arg(i, "CALLVOTE", |i| opt!(i, voting), CallVote),
+            |i| cmdc_single_arg(i, "CALLVOTE", voting, |v| CallVote(Some(v))),
         ))(input)
     }
 
@@ -287,6 +287,7 @@
         alt((
             cmd_no_arg_message,
             cmd_single_arg_message,
+            |i| tag_no_case("CALLVOTE")(i).map(|(i, _)| (i, CallVote(None))),
             |i| {
                 precededc(i, hw_tag_no_case("GREETING"), opt_space_arg)
                     .map(|(i, s)| (i, Greeting(s)))
@@ -560,15 +561,12 @@
     )
 }
 
-fn extract_messages(input: &[u8]) -> HwResult<Vec<HwProtocolMessage>> {
-    many0(message)(input)
-}
-
 #[cfg(test)]
 mod test {
-    use super::{extract_messages, message};
-    use crate::protocol::parser::HwProtocolError;
-    use crate::protocol::{messages::HwProtocolMessage::*, test::gen_proto_msg};
+    use super::message;
+    use crate::protocol::{
+        messages::HwProtocolMessage::*, parser::HwProtocolError, test::gen_proto_msg,
+    };
     use proptest::{proptest, proptest_helper};
 
     #[cfg(test)]
@@ -619,14 +617,5 @@
             message(b"QUIT\n1\n2\n\n"),
             Err(nom::Err::Error(HwProtocolError::new()))
         );
-
-        assert_eq!(
-            extract_messages(b"\n\n\n\nPING\n\n"),
-            Ok((&b""[..], vec![Ping]))
-        );
-        assert_eq!(
-            extract_messages(b"\n\n\nPING\n\n"),
-            Ok((&b""[..], vec![Ping]))
-        );
     }
 }
--- a/rust/hedgewars-server/src/protocol/test.rs	Mon Jun 03 23:50:26 2019 +0300
+++ b/rust/hedgewars-server/src/protocol/test.rs	Tue Jun 04 01:32:08 2019 +0300
@@ -4,7 +4,7 @@
     test_runner::{Reason, TestRunner},
 };
 
-use crate::core::types::{GameCfg, HedgehogInfo, ServerVar, ServerVar::*, TeamInfo};
+use crate::core::types::{GameCfg, HedgehogInfo, ServerVar, ServerVar::*, TeamInfo, VoteType};
 
 use super::messages::{HwProtocolMessage, HwProtocolMessage::*};
 
@@ -166,6 +166,28 @@
     type Strategy = BoxedStrategy<ServerVar>;
 }
 
+impl Arbitrary for VoteType {
+    type Parameters = ();
+
+    fn arbitrary_with(args: Self::Parameters) -> Self::Strategy {
+        use VoteType::*;
+        (0..=4)
+            .no_shrink()
+            .prop_flat_map(|i| {
+                proto_msg_match!(i, def = VoteType::Pause,
+                    0 => Kick(Ascii),
+                    1 => Map(Option<Ascii>),
+                    2 => Pause(),
+                    3 => NewSeed(),
+                    4 => HedgehogsPerTeam(u8)
+                )
+            })
+            .boxed()
+    }
+
+    type Strategy = BoxedStrategy<VoteType>;
+}
+
 pub fn gen_proto_msg() -> BoxedStrategy<HwProtocolMessage> where {
     let res = (0..=55).no_shrink().prop_flat_map(|i| {
         proto_msg_match!(i, def = Ping,
@@ -217,7 +239,7 @@
             46 => Fix(),
             47 => Unfix(),
             48 => Greeting(Option<Ascii>),
-            //49 => CallVote(Option<(String, Option<String>)>),
+            49 => CallVote(Option<VoteType>),
             50 => Vote(bool),
             51 => ForceVote(bool),
             52 => Save(Ascii, Ascii),