gameServer2/src/server/handlers/inroom.rs
changeset 13529 662f7df89d06
parent 13528 c8b626b0a3ad
child 13530 80db7232b4b5
equal deleted inserted replaced
13528:c8b626b0a3ad 13529:662f7df89d06
    10     server::HWServer,
    10     server::HWServer,
    11     room::{HWRoom, RoomFlags},
    11     room::{HWRoom, RoomFlags},
    12     actions::{Action, Action::*}
    12     actions::{Action, Action::*}
    13 };
    13 };
    14 use utils::is_name_illegal;
    14 use utils::is_name_illegal;
    15 use std::mem::swap;
    15 use std::{
       
    16     mem::swap, fs::{File, OpenOptions},
       
    17     io::{Read, Write, Result, Error, ErrorKind}
       
    18 };
    16 use base64::{encode, decode};
    19 use base64::{encode, decode};
    17 use super::common::rnd_reply;
    20 use super::common::rnd_reply;
    18 
    21 
    19 #[derive(Clone)]
    22 #[derive(Clone)]
    20 struct ByMsg<'a> {
    23 struct ByMsg<'a> {
    87         ToggleRestrictJoin => RoomFlags::RESTRICTED_JOIN,
    90         ToggleRestrictJoin => RoomFlags::RESTRICTED_JOIN,
    88         ToggleRestrictTeams => RoomFlags::RESTRICTED_TEAM_ADD,
    91         ToggleRestrictTeams => RoomFlags::RESTRICTED_TEAM_ADD,
    89         ToggleRegisteredOnly => RoomFlags::RESTRICTED_UNREGISTERED_PLAYERS,
    92         ToggleRegisteredOnly => RoomFlags::RESTRICTED_UNREGISTERED_PLAYERS,
    90         _ => RoomFlags::empty()
    93         _ => RoomFlags::empty()
    91     }
    94     }
       
    95 }
       
    96 
       
    97 fn read_file(filename: &str) -> Result<String> {
       
    98     let mut reader = File::open(filename)?;
       
    99     let mut result = String::new();
       
   100     reader.read_to_string(&mut result)?;
       
   101     Ok(result)
       
   102 }
       
   103 
       
   104 fn write_file(filename: &str, content: &str) -> Result<()> {
       
   105     let mut writer = OpenOptions::new().create(true).write(true).open(filename)?;
       
   106     writer.write_all(content.as_bytes())
    92 }
   107 }
    93 
   108 
    94 pub fn handle(server: &mut HWServer, client_id: ClientId, room_id: RoomId, message: HWProtocolMessage) {
   109 pub fn handle(server: &mut HWServer, client_id: ClientId, room_id: RoomId, message: HWProtocolMessage) {
    95     use protocol::messages::HWProtocolMessage::*;
   110     use protocol::messages::HWProtocolMessage::*;
    96     match message {
   111     match message {
   279             let actions = vec![server_chat(format!("Room config saved as {}", name))
   294             let actions = vec![server_chat(format!("Room config saved as {}", name))
   280                 .send_all().in_room(room_id).action()];
   295                 .send_all().in_room(room_id).action()];
   281             server.rooms[room_id].save_config(name, location);
   296             server.rooms[room_id].save_config(name, location);
   282             server.react(client_id, actions);
   297             server.react(client_id, actions);
   283         }
   298         }
       
   299         SaveRoom(filename) => {
       
   300             let actions = if server.clients[client_id].is_admin() {
       
   301                 match server.rooms[room_id].get_saves() {
       
   302                     Ok(text) => match write_file(&filename, &text) {
       
   303                         Ok(_) => vec![server_chat("Room configs saved successfully.".to_string())
       
   304                             .send_self().action()],
       
   305                         Err(e) => {
       
   306                             warn!("Error while writing the config file \"{}\": {}", filename, e);
       
   307                             vec![Warn("Unable to save the room configs.".to_string())]
       
   308                         }
       
   309                     }
       
   310                     Err(e) => {
       
   311                         warn!("Error while serializing the room configs: {}", e);
       
   312                         vec![Warn("Unable to serialize the room configs.".to_string())]
       
   313                     }
       
   314                 }
       
   315             } else {
       
   316                 Vec::new()
       
   317             };
       
   318             server.react(client_id, actions);
       
   319         }
       
   320         LoadRoom(filename) => {
       
   321             let actions = if server.clients[client_id].is_admin() {
       
   322                 match read_file(&filename) {
       
   323                     Ok(text) => match server.rooms[room_id].set_saves(&text) {
       
   324                         Ok(_) => vec![server_chat("Room configs loaded successfully.".to_string())
       
   325                             .send_self().action()],
       
   326                         Err(e) => {
       
   327                             warn!("Error while deserializing the room configs: {}", e);
       
   328                             vec![Warn("Unable to deserialize the room configs.".to_string())]
       
   329                         }
       
   330                     }
       
   331                     Err(e) => {
       
   332                         warn!("Error while reading the config file \"{}\": {}", filename, e);
       
   333                         vec![Warn("Unable to load the room configs.".to_string())]
       
   334                     }
       
   335                 }
       
   336             } else {
       
   337                 Vec::new()
       
   338             };
       
   339             server.react(client_id, actions);
       
   340         }
   284         Delete(name) => {
   341         Delete(name) => {
   285             let actions = if !server.rooms[room_id].delete_config(&name) {
   342             let actions = if !server.rooms[room_id].delete_config(&name) {
   286                 vec![Warn(format!("Save doesn't exist: {}", name))]
   343                 vec![Warn(format!("Save doesn't exist: {}", name))]
   287             } else {
   344             } else {
   288                 vec![server_chat(format!("Room config {} has been deleted", name))
   345                 vec![server_chat(format!("Room config {} has been deleted", name))