Browse Source

move validate_login

pbs
jake 5 years ago
parent
commit
4a01beb9e8
  1. 40
      src/ship/packet/handler/auth.rs
  2. 3
      src/ship/packet/handler/lobby.rs
  3. 1
      src/ship/packet/handler/mod.rs
  4. 29
      src/ship/ship.rs

40
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<EG: EntityGateway>(id: ClientId,
pkt: &Login,
entity_gateway: &mut EG,
clients: &mut HashMap<ClientId, ClientState>,
item_database: &mut ActiveItemDatabase,
ship_name: &String)
-> Result<Vec<SendShipPacket>, 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()))]
}
})
}

3
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<ClientId, ClientState>,
level_table: &CharacterLevelTable) -> Result<Vec<(ClientId, SendShipPacket)>, ShipError> {
level_table: &CharacterLevelTable)
-> Result<Vec<(ClientId, SendShipPacket)>, 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()

1
src/ship/packet/handler/mod.rs

@ -1 +1,2 @@
pub mod auth;
pub mod lobby;

29
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<EG: EntityGateway> ShipServerState<EG> {
}
}
fn validate_login(&mut self, id: ClientId, pkt: &Login) -> Result<Vec<SendShipPacket>, 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<Vec<SendShipPacket>, ShipError> {
let client = self.clients.get_mut(&id).ok_or(ShipError::ClientNotFound(id))?;
@ -598,7 +573,7 @@ impl<EG: EntityGateway> ServerState for ShipServerState<EG> {
-> Result<Box<dyn Iterator<Item = (ClientId, SendShipPacket)> + 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 {

Loading…
Cancel
Save