From 4a01beb9e8e053815267bf610e857e3fbbb6ac7d Mon Sep 17 00:00:00 2001 From: jake Date: Tue, 21 Apr 2020 22:18:57 -0600 Subject: [PATCH] move validate_login --- src/ship/packet/handler/auth.rs | 40 ++++++++++++++++++++++++++++++++ src/ship/packet/handler/lobby.rs | 3 ++- src/ship/packet/handler/mod.rs | 1 + src/ship/ship.rs | 29 ++--------------------- 4 files changed, 45 insertions(+), 28 deletions(-) create mode 100644 src/ship/packet/handler/auth.rs diff --git a/src/ship/packet/handler/auth.rs b/src/ship/packet/handler/auth.rs new file mode 100644 index 0000000..0268ba4 --- /dev/null +++ b/src/ship/packet/handler/auth.rs @@ -0,0 +1,40 @@ +use std::collections::HashMap; +use libpso::packet::login::{Login, LoginResponse, AccountStatus, Session}; +use libpso::packet::ship::*; +use crate::common::serverstate::ClientId; +use crate::ship::ship::{SendShipPacket, ShipError, ClientState}; +use crate::login::login::get_login_status; +use crate::entity::gateway::EntityGateway; +use crate::ship::items::ActiveItemDatabase; + +pub fn validate_login(id: ClientId, + pkt: &Login, + entity_gateway: &mut EG, + clients: &mut HashMap, + item_database: &mut ActiveItemDatabase, + ship_name: &String) + -> Result, ShipError> { + Ok(match get_login_status(entity_gateway, pkt) { + Ok(user) => { + let mut response = LoginResponse::by_status(AccountStatus::Ok, Session::new()); + response.guildcard = user.id.0 as u32; + response.team_id = user.team_id.map_or(31, |ti| ti) as u32; + let characters = entity_gateway.get_characters_by_user(&user); + let character = characters + .get(pkt.session.character_slot as usize) + .ok_or(ShipError::InvalidSlot(id, pkt.session.character_slot as u32))?.as_ref() + .ok_or(ShipError::NoCharacterInSlot(id, pkt.session.character_slot as u32))? + .clone(); + let settings = entity_gateway.get_user_settings_by_user(&user) + .ok_or(ShipError::ClientNotFound(id))?; + let inventory = item_database.get_character_inventory(entity_gateway, &character); + + clients.insert(id, ClientState::new(user, settings, character, inventory, pkt.session)); + vec![SendShipPacket::LoginResponse(response), SendShipPacket::ShipBlockList(ShipBlockList::new(&&ship_name, 3))] + }, + Err(err) => { + vec![SendShipPacket::LoginResponse(LoginResponse::by_status(err, Session::new()))] + } + }) +} + diff --git a/src/ship/packet/handler/lobby.rs b/src/ship/packet/handler/lobby.rs index 9efe3bd..2972cc3 100644 --- a/src/ship/packet/handler/lobby.rs +++ b/src/ship/packet/handler/lobby.rs @@ -12,7 +12,8 @@ pub fn send_player_to_lobby(id: ClientId, _pkt: &CharData, client_location: &mut ClientLocation, clients: &HashMap, - level_table: &CharacterLevelTable) -> Result, ShipError> { + level_table: &CharacterLevelTable) + -> Result, ShipError> { let lobby = client_location.add_client_to_next_available_lobby(id, LobbyId(0)).map_err(|_| ShipError::TooManyClients)?; let lobby_clients = client_location.get_clients_in_lobby(lobby).map_err(|err| ShipError::ClientError(format!("{:?}", err)))?; let playerinfo = lobby_clients.iter() diff --git a/src/ship/packet/handler/mod.rs b/src/ship/packet/handler/mod.rs index 36bd53c..337c58f 100644 --- a/src/ship/packet/handler/mod.rs +++ b/src/ship/packet/handler/mod.rs @@ -1 +1,2 @@ +pub mod auth; pub mod lobby; diff --git a/src/ship/ship.rs b/src/ship/ship.rs index 709805f..b13ec3a 100644 --- a/src/ship/ship.rs +++ b/src/ship/ship.rs @@ -20,7 +20,6 @@ use crate::entity::gateway::EntityGateway; use crate::entity::account::{UserAccountEntity, UserSettingsEntity, USERFLAG_NEWCHAR, USERFLAG_DRESSINGROOM}; use crate::entity::character::CharacterEntity; use crate::entity::item::{ItemLocation, ItemEntity}; -use crate::login::login::get_login_status; use crate::ship::location::{ClientLocation, LobbyId, RoomId, RoomLobby, MAX_ROOMS}; use crate::ship::character::{CharacterBytesBuilder, FullCharacterBytesBuilder}; use crate::ship::items; @@ -146,7 +145,7 @@ pub struct ClientState { } impl ClientState { - fn new(user: UserAccountEntity, settings: UserSettingsEntity, character: CharacterEntity, inventory: items::ActiveInventory, /*bank: Bank,*/ session: Session) -> ClientState { + pub fn new(user: UserAccountEntity, settings: UserSettingsEntity, character: CharacterEntity, inventory: items::ActiveInventory, /*bank: Bank,*/ session: Session) -> ClientState { ClientState { user: user, settings: settings, @@ -183,30 +182,6 @@ impl ShipServerState { } } - fn validate_login(&mut self, id: ClientId, pkt: &Login) -> Result, ShipError> { - Ok(match get_login_status(&self.entity_gateway, pkt) { - Ok(user) => { - let mut response = LoginResponse::by_status(AccountStatus::Ok, Session::new()); - response.guildcard = user.id.0 as u32; - response.team_id = user.team_id.map_or(31, |ti| ti) as u32; - let characters = self.entity_gateway.get_characters_by_user(&user); - let character = characters - .get(pkt.session.character_slot as usize) - .ok_or(ShipError::InvalidSlot(id, pkt.session.character_slot as u32))?.as_ref() - .ok_or(ShipError::NoCharacterInSlot(id, pkt.session.character_slot as u32))? - .clone(); - let settings = self.entity_gateway.get_user_settings_by_user(&user) - .ok_or(ShipError::ClientNotFound(id))?; - let inventory = self.item_database.get_character_inventory(&mut self.entity_gateway, &character); - - self.clients.insert(id, ClientState::new(user, settings, character, inventory, pkt.session)); - vec![SendShipPacket::LoginResponse(response), SendShipPacket::ShipBlockList(ShipBlockList::new(&self.name, 3))] - }, - Err(err) => { - vec![SendShipPacket::LoginResponse(LoginResponse::by_status(err, Session::new()))] - } - }) - } fn block_selected(&mut self, id: ClientId, pkt: &MenuSelect) -> Result, ShipError> { let client = self.clients.get_mut(&id).ok_or(ShipError::ClientNotFound(id))?; @@ -598,7 +573,7 @@ impl ServerState for ShipServerState { -> Result + Send>, ShipError> { Ok(match pkt { RecvShipPacket::Login(login) => { - Box::new(self.validate_login(id, login)?.into_iter().map(move |pkt| (id, pkt))) + Box::new(handler::auth::validate_login(id, login, &mut self.entity_gateway, &mut self.clients, &mut self.item_database, &self.name)?.into_iter().map(move |pkt| (id, pkt))) }, RecvShipPacket::MenuSelect(menuselect) => { match menuselect.menu {