gameServer2/src/server/handlers/inroom.rs
changeset 13534 662f7df89d06
parent 13533 c8b626b0a3ad
child 13535 80db7232b4b5
--- a/gameServer2/src/server/handlers/inroom.rs	Fri Jul 20 22:14:20 2018 +0300
+++ b/gameServer2/src/server/handlers/inroom.rs	Sat Jul 21 02:13:55 2018 +0300
@@ -12,7 +12,10 @@
     actions::{Action, Action::*}
 };
 use utils::is_name_illegal;
-use std::mem::swap;
+use std::{
+    mem::swap, fs::{File, OpenOptions},
+    io::{Read, Write, Result, Error, ErrorKind}
+};
 use base64::{encode, decode};
 use super::common::rnd_reply;
 
@@ -91,6 +94,18 @@
     }
 }
 
+fn read_file(filename: &str) -> Result<String> {
+    let mut reader = File::open(filename)?;
+    let mut result = String::new();
+    reader.read_to_string(&mut result)?;
+    Ok(result)
+}
+
+fn write_file(filename: &str, content: &str) -> Result<()> {
+    let mut writer = OpenOptions::new().create(true).write(true).open(filename)?;
+    writer.write_all(content.as_bytes())
+}
+
 pub fn handle(server: &mut HWServer, client_id: ClientId, room_id: RoomId, message: HWProtocolMessage) {
     use protocol::messages::HWProtocolMessage::*;
     match message {
@@ -281,6 +296,48 @@
             server.rooms[room_id].save_config(name, location);
             server.react(client_id, actions);
         }
+        SaveRoom(filename) => {
+            let actions = if server.clients[client_id].is_admin() {
+                match server.rooms[room_id].get_saves() {
+                    Ok(text) => match write_file(&filename, &text) {
+                        Ok(_) => vec![server_chat("Room configs saved successfully.".to_string())
+                            .send_self().action()],
+                        Err(e) => {
+                            warn!("Error while writing the config file \"{}\": {}", filename, e);
+                            vec![Warn("Unable to save the room configs.".to_string())]
+                        }
+                    }
+                    Err(e) => {
+                        warn!("Error while serializing the room configs: {}", e);
+                        vec![Warn("Unable to serialize the room configs.".to_string())]
+                    }
+                }
+            } else {
+                Vec::new()
+            };
+            server.react(client_id, actions);
+        }
+        LoadRoom(filename) => {
+            let actions = if server.clients[client_id].is_admin() {
+                match read_file(&filename) {
+                    Ok(text) => match server.rooms[room_id].set_saves(&text) {
+                        Ok(_) => vec![server_chat("Room configs loaded successfully.".to_string())
+                            .send_self().action()],
+                        Err(e) => {
+                            warn!("Error while deserializing the room configs: {}", e);
+                            vec![Warn("Unable to deserialize the room configs.".to_string())]
+                        }
+                    }
+                    Err(e) => {
+                        warn!("Error while reading the config file \"{}\": {}", filename, e);
+                        vec![Warn("Unable to load the room configs.".to_string())]
+                    }
+                }
+            } else {
+                Vec::new()
+            };
+            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))]