diff --git a/src/ship/ship.rs b/src/ship/ship.rs index b63e49b..a26819e 100644 --- a/src/ship/ship.rs +++ b/src/ship/ship.rs @@ -70,20 +70,20 @@ impl SendServerPacket for SendShipPacket { struct ClientState { - user: Option, - settings: Option, + user: UserAccount, + settings: UserSettings, character: Character, session: Session, block: u32, } impl ClientState { - fn new() -> ClientState { + fn new(user: UserAccount, settings: UserSettings, character: Character, session: Session) -> ClientState { ClientState { - user: None, - settings: None, - character: Character::default(), - session: Session::new(), + user: user, + settings: settings, + character: character, + session: session, block: 0, } } @@ -106,21 +106,20 @@ impl ShipServerState { } fn validate_login(&mut self, id: ClientId, pkt: &Login) -> Result, ShipError> { - let client = self.clients.get_mut(&id).ok_or(ShipError::ClientNotFound(id))?; Ok(match get_login_status(&self.entity_gateway, pkt) { Ok(user) => { let mut response = LoginResponse::by_status(AccountStatus::Ok, Session::new()); response.guildcard = user.guildcard.map_or(24, |gc| gc) 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); - client.character = characters + let character = characters .get(pkt.session.character_slot as usize) .ok_or(ShipError::InvalidSlot(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))] }, Err(err) => { @@ -131,13 +130,12 @@ impl ShipServerState { fn block_selected(&mut self, id: ClientId, pkt: &MenuSelect) -> Result, ShipError> { 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; let mut fc = character::FullCharacter::default(); 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![ SendShipPacket::FullCharacter(FullCharacter { @@ -193,8 +191,6 @@ impl ServerState for ShipServerState { type PacketError = ShipError; fn on_connect(&mut self, id: ClientId) -> Vec> { - self.clients.insert(id, ClientState::new()); - let mut rng = rand::thread_rng(); let mut server_key = [0u8; 48];