rust/hedgewars-server/src/server/handlers.rs
changeset 14780 65861ba8b4e8
parent 14779 f43ab2bd76ae
child 14781 01f8ab45f806
equal deleted inserted replaced
14779:f43ab2bd76ae 14780:65861ba8b4e8
     2 use std::{collections::HashMap, io, io::Write};
     2 use std::{collections::HashMap, io, io::Write};
     3 
     3 
     4 use super::{
     4 use super::{
     5     actions::{Destination, DestinationRoom},
     5     actions::{Destination, DestinationRoom},
     6     core::HWServer,
     6     core::HWServer,
     7     coretypes::ClientId,
     7     coretypes::{ClientId, RoomId},
     8     room::RoomSave,
     8     room::RoomSave,
     9 };
     9 };
    10 use crate::{
    10 use crate::{
    11     protocol::messages::{HWProtocolMessage, HWServerMessage, HWServerMessage::*},
    11     protocol::messages::{HWProtocolMessage, HWServerMessage, HWServerMessage::*, server_chat},
    12     server::actions::PendingMessage,
    12     server::actions::PendingMessage,
    13     utils,
    13     utils,
    14 };
    14 };
    15 use base64::encode;
    15 use base64::encode;
    16 use log::*;
    16 use log::*;
    56         protocol: u16,
    56         protocol: u16,
    57         password_hash: String,
    57         password_hash: String,
    58         client_salt: String,
    58         client_salt: String,
    59         server_salt: String,
    59         server_salt: String,
    60     },
    60     },
       
    61     SaveRoom {
       
    62         room_id: RoomId,
       
    63         filename: String,
       
    64         contents: String,
       
    65     },
       
    66     LoadRoom {
       
    67         room_id: RoomId,
       
    68         filename: String
       
    69     }
    61 }
    70 }
    62 
    71 
    63 pub enum IoResult {
    72 pub enum IoResult {
    64     Account(Option<AccountInfo>),
    73     Account(Option<AccountInfo>),
       
    74     SaveRoom(RoomId, bool),
       
    75     LoadRoom(RoomId, Option<String>)
    65 }
    76 }
    66 
    77 
    67 pub struct Response {
    78 pub struct Response {
    68     client_id: ClientId,
    79     client_id: ClientId,
    69     messages: Vec<PendingMessage>,
    80     messages: Vec<PendingMessage>,
   248         }
   259         }
   249         IoResult::Account(None) => {
   260         IoResult::Account(None) => {
   250             response.add(Error("Authentication failed.".to_string()).send_self());
   261             response.add(Error("Authentication failed.".to_string()).send_self());
   251             response.remove_client(client_id);
   262             response.remove_client(client_id);
   252         }
   263         }
   253     }
   264         IoResult::SaveRoom(_, true) => {
   254 }
   265             response.add(server_chat("Room configs saved successfully.".to_string()).send_self());
       
   266         }
       
   267         IoResult::SaveRoom(_, false) => {
       
   268             response.add(
       
   269                 Warning("Unable to save the room configs.".to_string()).send_self(),
       
   270             );
       
   271         }
       
   272         IoResult::LoadRoom(room_id, Some(contents)) => {
       
   273             if let Some(ref mut room) = server.rooms.get_mut(room_id) {
       
   274                 match room.set_saves(&contents) {
       
   275                     Ok(_) => response.add(
       
   276                         server_chat("Room configs loaded successfully.".to_string())
       
   277                             .send_self(),
       
   278                     ),
       
   279                     Err(e) => {
       
   280                         warn!("Error while deserializing the room configs: {}", e);
       
   281                         response.add(
       
   282                             Warning("Unable to deserialize the room configs.".to_string())
       
   283                                 .send_self(),
       
   284                         );
       
   285                     }
       
   286                 }
       
   287             }
       
   288         }
       
   289         IoResult::LoadRoom(_,None) => {
       
   290             response.add(
       
   291                 Warning("Unable to load the room configs.".to_string()).send_self(),
       
   292             );
       
   293         }
       
   294     }
       
   295 }