rust/hedgewars-server/src/server/handlers.rs
changeset 14781 01f8ab45f806
parent 14780 65861ba8b4e8
child 14783 b3adc030104b
equal deleted inserted replaced
14780:65861ba8b4e8 14781:01f8ab45f806
     6     core::HWServer,
     6     core::HWServer,
     7     coretypes::{ClientId, RoomId},
     7     coretypes::{ClientId, RoomId},
     8     room::RoomSave,
     8     room::RoomSave,
     9 };
     9 };
    10 use crate::{
    10 use crate::{
    11     protocol::messages::{HWProtocolMessage, HWServerMessage, HWServerMessage::*, server_chat},
    11     protocol::messages::{server_chat, HWProtocolMessage, HWServerMessage, HWServerMessage::*},
    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::*;
    63         filename: String,
    63         filename: String,
    64         contents: String,
    64         contents: String,
    65     },
    65     },
    66     LoadRoom {
    66     LoadRoom {
    67         room_id: RoomId,
    67         room_id: RoomId,
    68         filename: String
    68         filename: String,
    69     }
    69     },
    70 }
    70 }
    71 
    71 
    72 pub enum IoResult {
    72 pub enum IoResult {
    73     Account(Option<AccountInfo>),
    73     Account(Option<AccountInfo>),
    74     SaveRoom(RoomId, bool),
    74     SaveRoom(RoomId, bool),
    75     LoadRoom(RoomId, Option<String>)
    75     LoadRoom(RoomId, Option<String>),
    76 }
    76 }
    77 
    77 
    78 pub struct Response {
    78 pub struct Response {
    79     client_id: ClientId,
    79     client_id: ClientId,
    80     messages: Vec<PendingMessage>,
    80     messages: Vec<PendingMessage>,
   158         Destination::ToSelf => vec![client_id],
   158         Destination::ToSelf => vec![client_id],
   159         Destination::ToId(id) => vec![id],
   159         Destination::ToId(id) => vec![id],
   160         Destination::ToAll {
   160         Destination::ToAll {
   161             room_id: DestinationRoom::Lobby,
   161             room_id: DestinationRoom::Lobby,
   162             ..
   162             ..
   163         } => server.lobby_clients(),
   163         } => server.collect_lobby_clients(),
   164         Destination::ToAll {
   164         Destination::ToAll {
   165             room_id: DestinationRoom::Room(id),
   165             room_id: DestinationRoom::Room(id),
   166             ..
   166             ..
   167         } => server.room_clients(id),
   167         } => server.collect_room_clients(id),
   168         Destination::ToAll {
   168         Destination::ToAll {
   169             protocol: Some(proto),
   169             protocol: Some(proto),
   170             ..
   170             ..
   171         } => server.protocol_clients(proto),
   171         } => server.protocol_clients(proto),
   172         Destination::ToAll { .. } => server.clients.iter().map(|(id, _)| id).collect::<Vec<_>>(),
   172         Destination::ToAll { .. } => server.clients.iter().map(|(id, _)| id).collect::<Vec<_>>(),
   192         HWProtocolMessage::Ping => response.add(Pong.send_self()),
   192         HWProtocolMessage::Ping => response.add(Pong.send_self()),
   193         HWProtocolMessage::Malformed => warn!("Malformed/unknown message"),
   193         HWProtocolMessage::Malformed => warn!("Malformed/unknown message"),
   194         HWProtocolMessage::Empty => warn!("Empty message"),
   194         HWProtocolMessage::Empty => warn!("Empty message"),
   195         _ => {
   195         _ => {
   196             if server.anteroom.clients.contains(client_id) {
   196             if server.anteroom.clients.contains(client_id) {
   197                 match loggingin::handle(&mut server.anteroom, client_id, response, message) {
   197                 match loggingin::handle(server, client_id, response, message) {
   198                     LoginResult::Unchanged => (),
   198                     LoginResult::Unchanged => (),
   199                     LoginResult::Complete => {
   199                     LoginResult::Complete => {
   200                         if let Some(client) = server.anteroom.remove_client(client_id) {
   200                         if let Some(client) = server.anteroom.remove_client(client_id) {
   201                             server.add_client(client_id, client);
   201                             server.add_client(client_id, client);
       
   202                             common::join_lobby(server, response);
   202                         }
   203                         }
   203                     }
   204                     }
   204                     LoginResult::Exit => {
   205                     LoginResult::Exit => {
   205                         server.anteroom.remove_client(client_id);
   206                         server.anteroom.remove_client(client_id);
   206                         response.remove_client(client_id);
   207                         response.remove_client(client_id);
   263         }
   264         }
   264         IoResult::SaveRoom(_, true) => {
   265         IoResult::SaveRoom(_, true) => {
   265             response.add(server_chat("Room configs saved successfully.".to_string()).send_self());
   266             response.add(server_chat("Room configs saved successfully.".to_string()).send_self());
   266         }
   267         }
   267         IoResult::SaveRoom(_, false) => {
   268         IoResult::SaveRoom(_, false) => {
   268             response.add(
   269             response.add(Warning("Unable to save the room configs.".to_string()).send_self());
   269                 Warning("Unable to save the room configs.".to_string()).send_self(),
       
   270             );
       
   271         }
   270         }
   272         IoResult::LoadRoom(room_id, Some(contents)) => {
   271         IoResult::LoadRoom(room_id, Some(contents)) => {
   273             if let Some(ref mut room) = server.rooms.get_mut(room_id) {
   272             if let Some(ref mut room) = server.rooms.get_mut(room_id) {
   274                 match room.set_saves(&contents) {
   273                 match room.set_saves(&contents) {
   275                     Ok(_) => response.add(
   274                     Ok(_) => response.add(
   276                         server_chat("Room configs loaded successfully.".to_string())
   275                         server_chat("Room configs loaded successfully.".to_string()).send_self(),
   277                             .send_self(),
       
   278                     ),
   276                     ),
   279                     Err(e) => {
   277                     Err(e) => {
   280                         warn!("Error while deserializing the room configs: {}", e);
   278                         warn!("Error while deserializing the room configs: {}", e);
   281                         response.add(
   279                         response.add(
   282                             Warning("Unable to deserialize the room configs.".to_string())
   280                             Warning("Unable to deserialize the room configs.".to_string())
   284                         );
   282                         );
   285                     }
   283                     }
   286                 }
   284                 }
   287             }
   285             }
   288         }
   286         }
   289         IoResult::LoadRoom(_,None) => {
   287         IoResult::LoadRoom(_, None) => {
   290             response.add(
   288             response.add(Warning("Unable to load the room configs.".to_string()).send_self());
   291                 Warning("Unable to load the room configs.".to_string()).send_self(),
   289         }
   292             );
   290     }
   293         }
   291 }
   294     }
       
   295 }