Browse Source

move ClientState instantiate from connect to valid login

pbs
jake 5 years ago
parent
commit
f149bb4dea
  1. 32
      src/ship/ship.rs

32
src/ship/ship.rs

@ -70,20 +70,20 @@ impl SendServerPacket for SendShipPacket {
struct ClientState { struct ClientState {
user: Option<UserAccount>,
settings: Option<UserSettings>,
user: UserAccount,
settings: UserSettings,
character: Character, character: Character,
session: Session, session: Session,
block: u32, block: u32,
} }
impl ClientState { impl ClientState {
fn new() -> ClientState {
fn new(user: UserAccount, settings: UserSettings, character: Character, session: Session) -> ClientState {
ClientState { ClientState {
user: None,
settings: None,
character: Character::default(),
session: Session::new(),
user: user,
settings: settings,
character: character,
session: session,
block: 0, block: 0,
} }
} }
@ -106,21 +106,20 @@ impl<EG: EntityGateway> ShipServerState<EG> {
} }
fn validate_login(&mut self, id: ClientId, pkt: &Login) -> Result<Vec<SendShipPacket>, ShipError> { fn validate_login(&mut self, id: ClientId, pkt: &Login) -> Result<Vec<SendShipPacket>, ShipError> {
let client = self.clients.get_mut(&id).ok_or(ShipError::ClientNotFound(id))?;
Ok(match get_login_status(&self.entity_gateway, pkt) { Ok(match get_login_status(&self.entity_gateway, pkt) {
Ok(user) => { Ok(user) => {
let mut response = LoginResponse::by_status(AccountStatus::Ok, Session::new()); let mut response = LoginResponse::by_status(AccountStatus::Ok, Session::new());
response.guildcard = user.guildcard.map_or(24, |gc| gc) as u32; response.guildcard = user.guildcard.map_or(24, |gc| gc) as u32;
response.team_id = user.team_id.map_or(31, |ti| ti) as u32; response.team_id = user.team_id.map_or(31, |ti| ti) as u32;
client.session = pkt.session;
let characters = self.entity_gateway.get_characters_by_user(&user); let characters = self.entity_gateway.get_characters_by_user(&user);
client.character = characters
let character = characters
.get(pkt.session.character_slot as usize) .get(pkt.session.character_slot as usize)
.ok_or(ShipError::InvalidSlot(id, pkt.session.character_slot as u32))? .ok_or(ShipError::InvalidSlot(id, pkt.session.character_slot as u32))?
.ok_or(ShipError::NoCharacterInSlot(id, pkt.session.character_slot as u32))?; .ok_or(ShipError::NoCharacterInSlot(id, pkt.session.character_slot as u32))?;
client.user = Some(user);
client.settings = Some(self.entity_gateway.get_user_settings_by_user(&client.user.as_ref().unwrap())
.ok_or(ShipError::ClientNotFound(id))?);
let settings = self.entity_gateway.get_user_settings_by_user(&user)
.ok_or(ShipError::ClientNotFound(id))?;
self.clients.insert(id, ClientState::new(user, settings, character, pkt.session));
vec![SendShipPacket::LoginResponse(response), SendShipPacket::ShipBlockList(ShipBlockList::new(3))] vec![SendShipPacket::LoginResponse(response), SendShipPacket::ShipBlockList(ShipBlockList::new(3))]
}, },
Err(err) => { Err(err) => {
@ -131,13 +130,12 @@ impl<EG: EntityGateway> ShipServerState<EG> {
fn block_selected(&mut self, id: ClientId, pkt: &MenuSelect) -> Result<Vec<SendShipPacket>, ShipError> { 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))?; let client = self.clients.get_mut(&id).ok_or(ShipError::ClientNotFound(id))?;
let settings = client.settings.as_ref().ok_or(ShipError::ClientNotFound(id))?.settings;
client.block = pkt.item as u32; client.block = pkt.item as u32;
let mut fc = character::FullCharacter::default(); let mut fc = character::FullCharacter::default();
fc.character = client.character.character; fc.character = client.character.character;
fc.key_team_config.key_config = settings.key_config;
fc.key_team_config.joystick_config = settings.joystick_config;
fc.key_team_config.key_config = client.settings.settings.key_config;
fc.key_team_config.joystick_config = client.settings.settings.joystick_config;
Ok(vec![ Ok(vec![
SendShipPacket::FullCharacter(FullCharacter { SendShipPacket::FullCharacter(FullCharacter {
@ -193,8 +191,6 @@ impl<EG: EntityGateway> ServerState for ShipServerState<EG> {
type PacketError = ShipError; type PacketError = ShipError;
fn on_connect(&mut self, id: ClientId) -> Vec<OnConnect<Self::SendPacket>> { fn on_connect(&mut self, id: ClientId) -> Vec<OnConnect<Self::SendPacket>> {
self.clients.insert(id, ClientState::new());
let mut rng = rand::thread_rng(); let mut rng = rand::thread_rng();
let mut server_key = [0u8; 48]; let mut server_key = [0u8; 48];

Loading…
Cancel
Save