Implement save/delete commands
authoralfadur
Fri, 20 Jul 2018 22:14:20 +0300
changeset 13533 c8b626b0a3ad
parent 13532 e3ae9eea0689
child 13534 662f7df89d06
Implement save/delete commands
gameServer2/src/protocol/messages.rs
gameServer2/src/protocol/parser.rs
gameServer2/src/protocol/test.rs
gameServer2/src/server/handlers/inroom.rs
gameServer2/src/server/room.rs
--- a/gameServer2/src/protocol/messages.rs	Fri Jul 20 00:02:52 2018 +0300
+++ b/gameServer2/src/protocol/messages.rs	Fri Jul 20 22:14:20 2018 +0300
@@ -234,10 +234,10 @@
             //CallVote(Option<(String, Option<String>)>) =>, ??
             Vote(msg) => msg!["CMD", format!("VOTE {}", if *msg {"YES"} else {"NO"})],
             ForceVote(msg) => msg!["CMD", format!("FORCE {}", if *msg {"YES"} else {"NO"})],
-            //Save(String, String), ??
-            Delete(room) => msg!["CMD", format!("DELETE {}", room)],
-            SaveRoom(room) => msg!["CMD", format!("SAVEROOM {}", room)],
-            LoadRoom(room) => msg!["CMD", format!("LOADROOM {}", room)],
+            Save(name, location) => msg!["CMD", format!("SAVE {} {}", name, location)],
+            Delete(name) => msg!["CMD", format!("DELETE {}", name)],
+            SaveRoom(name) => msg!["CMD", format!("SAVEROOM {}", name)],
+            LoadRoom(name) => msg!["CMD", format!("LOADROOM {}", name)],
             Malformed => msg!["A", "QUICK", "BROWN", "HOG", "JUMPS", "OVER", "THE", "LAZY", "DOG"],
             Empty => msg![""],
             _ => panic!("Protocol message not yet implemented")
--- a/gameServer2/src/protocol/parser.rs	Fri Jul 20 00:02:52 2018 +0300
+++ b/gameServer2/src/protocol/parser.rs	Fri Jul 20 22:14:20 2018 +0300
@@ -24,6 +24,8 @@
 named!(end_of_message, tag!("\n\n"));
 named!(str_line<&[u8],   &str>, map_res!(not_line_ending, str::from_utf8));
 named!(  a_line<&[u8], String>, map!(str_line, String::from));
+named!(cmd_arg<&[u8], String>,
+    map!(map_res!(take_until_either!(" \n"), str::from_utf8), String::from));
 named!( u8_line<&[u8],     u8>, map_res!(str_line, FromStr::from_str));
 named!(u16_line<&[u8],    u16>, map_res!(str_line, FromStr::from_str));
 named!(u32_line<&[u8],    u32>, map_res!(str_line, FromStr::from_str));
@@ -104,9 +106,10 @@
     | 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!("SAVE")     >> spaces >> n: cmd_arg >> spaces >> l: cmd_arg >> (Save(n, l)))
+    | do_parse!(tag_no_case!("DELETE")   >> spaces >> n: a_line  >> (Delete(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)))
--- a/gameServer2/src/protocol/test.rs	Fri Jul 20 00:02:52 2018 +0300
+++ b/gameServer2/src/protocol/test.rs	Fri Jul 20 22:14:20 2018 +0300
@@ -160,7 +160,7 @@
         //49 => CallVote(Option<(String, Option<String>)>),
         50 => Vote(bool),
         51 => ForceVote(bool),
-        //52 => Save(String, String),
+        52 => Save(Ascii, Ascii),
         53 => Delete(Ascii),
         54 => SaveRoom(Ascii),
         55 => LoadRoom(Ascii),
--- a/gameServer2/src/server/handlers/inroom.rs	Fri Jul 20 00:02:52 2018 +0300
+++ b/gameServer2/src/server/handlers/inroom.rs	Fri Jul 20 22:14:20 2018 +0300
@@ -275,6 +275,21 @@
             };
             server.react(client_id, actions);
         }
+        Save(name, location) => {
+            let actions = vec![server_chat(format!("Room config saved as {}", name))
+                .send_all().in_room(room_id).action()];
+            server.rooms[room_id].save_config(name, location);
+            server.react(client_id, actions);
+        }
+        Delete(name) => {
+            let actions = if !server.rooms[room_id].delete_config(&name) {
+                vec![Warn(format!("Save doesn't exist: {}", name))]
+            } else {
+                vec![server_chat(format!("Room config {} has been deleted", name))
+                    .send_all().in_room(room_id).action()]
+            };
+            server.react(client_id, actions);
+        }
         CallVote(None) => {
             server.react(client_id, vec![
                 server_chat("Available callvote commands: kick <nickname>, map <name>, pause, newseed, hedgehogs <number>".to_string())
--- a/gameServer2/src/server/room.rs	Fri Jul 20 00:02:52 2018 +0300
+++ b/gameServer2/src/server/room.rs	Fri Jul 20 22:14:20 2018 +0300
@@ -335,6 +335,10 @@
         }
     }
 
+    pub fn save_config(&mut self, name: String, location: String) {
+        self.saves.insert(name, RoomSave { location, config: self.config.clone() });
+    }
+
     pub fn load_config(&mut self, name: &str) -> Option<&str> {
         if let Some(save) = self.saves.get(name) {
             self.config = save.config.clone();
@@ -344,6 +348,10 @@
         }
     }
 
+    pub fn delete_config(&mut self, name: &str) -> bool {
+        self.saves.remove(name).is_some()
+    }
+
     pub fn team_info(owner: &HWClient, team: &TeamInfo) -> Vec<String> {
         let mut info = vec![
             team.name.clone(),