Browse Source

improve error handling in new character

pbs
jake 4 years ago
parent
commit
75260c40a0
  1. 29
      src/login/character.rs

29
src/login/character.rs

@ -17,7 +17,7 @@ use crate::common::interserver::{ServerId, InterserverActor, LoginMessage, ShipM
use crate::common::leveltable::CharacterLevelTable; use crate::common::leveltable::CharacterLevelTable;
use libpso::{utf8_to_array, utf8_to_utf16_array}; use libpso::{utf8_to_array, utf8_to_utf16_array};
use crate::entity::gateway::EntityGateway;
use crate::entity::gateway::{EntityGateway, GatewayError};
use crate::entity::account::{UserAccountEntity, NewUserSettingsEntity, USERFLAG_NEWCHAR, USERFLAG_DRESSINGROOM}; use crate::entity::account::{UserAccountEntity, NewUserSettingsEntity, USERFLAG_NEWCHAR, USERFLAG_DRESSINGROOM};
use crate::entity::item::{NewItemEntity, ItemDetail, ItemLocation, InventoryItemEntity, InventoryEntity, BankEntity, BankName, EquippedEntity}; use crate::entity::item::{NewItemEntity, ItemDetail, ItemLocation, InventoryItemEntity, InventoryEntity, BankEntity, BankName, EquippedEntity};
use crate::entity::item::weapon::Weapon; use crate::entity::item::weapon::Weapon;
@ -41,7 +41,7 @@ pub enum CharacterError {
CouldNotLoadSettings, CouldNotLoadSettings,
CouldNotLoadCharacters, CouldNotLoadCharacters,
CouldNotLoadGuildcard, CouldNotLoadGuildcard,
DbError,
GatewayError(#[from] GatewayError),
} }
#[derive(Debug)] #[derive(Debug)]
@ -181,7 +181,7 @@ pub struct CharacterServerState<EG: EntityGateway> {
} }
async fn new_character<EG: EntityGateway>(entity_gateway: &mut EG, user: &UserAccountEntity, preview: &CharacterPreview) {
async fn new_character<EG: EntityGateway>(entity_gateway: &mut EG, user: &UserAccountEntity, preview: &CharacterPreview) -> Result<(), anyhow::Error> {
let mut character = new_character_from_preview(user, preview); let mut character = new_character_from_preview(user, preview);
match character.char_class { match character.char_class {
CharacterClass::FOmar | CharacterClass::FOmarl| CharacterClass::FOnewm | CharacterClass::FOnewearl => character.techs.set_tech(Technique::Foie, TechLevel(1)), CharacterClass::FOmar | CharacterClass::FOmarl| CharacterClass::FOnewm | CharacterClass::FOnewearl => character.techs.set_tech(Technique::Foie, TechLevel(1)),
@ -189,7 +189,7 @@ async fn new_character<EG: EntityGateway>(entity_gateway: &mut EG, user: &UserAc
} }
character.meseta = 300; character.meseta = 300;
let character = entity_gateway.create_character(character).await.unwrap();
let character = entity_gateway.create_character(character).await?;
let new_weapon = match character.char_class { let new_weapon = match character.char_class {
CharacterClass::HUmar | CharacterClass::HUnewearl | CharacterClass::HUcast | CharacterClass::HUcaseal => item::weapon::WeaponType::Saber, CharacterClass::HUmar | CharacterClass::HUnewearl | CharacterClass::HUcast | CharacterClass::HUcaseal => item::weapon::WeaponType::Saber,
@ -211,7 +211,7 @@ async fn new_character<EG: EntityGateway>(entity_gateway: &mut EG, user: &UserAc
}), }),
location: ItemLocation::Inventory { location: ItemLocation::Inventory {
character_id: character.id, character_id: character.id,
}}).await.unwrap();
}}).await?;
let armor = entity_gateway.create_item( let armor = entity_gateway.create_item(
NewItemEntity { NewItemEntity {
@ -225,7 +225,7 @@ async fn new_character<EG: EntityGateway>(entity_gateway: &mut EG, user: &UserAc
}), }),
location: ItemLocation::Inventory { location: ItemLocation::Inventory {
character_id: character.id, character_id: character.id,
}}).await.unwrap();
}}).await?;
let mut mag = { let mut mag = {
if character.char_class.is_android() { if character.char_class.is_android() {
@ -240,7 +240,7 @@ async fn new_character<EG: EntityGateway>(entity_gateway: &mut EG, user: &UserAc
item: ItemDetail::Mag(mag), item: ItemDetail::Mag(mag),
location: ItemLocation::Inventory { location: ItemLocation::Inventory {
character_id: character.id, character_id: character.id,
}}).await.unwrap();
}}).await?;
let mut monomates = Vec::new(); let mut monomates = Vec::new();
for _ in 0..4usize { for _ in 0..4usize {
@ -252,7 +252,7 @@ async fn new_character<EG: EntityGateway>(entity_gateway: &mut EG, user: &UserAc
}), }),
location: ItemLocation::Inventory { location: ItemLocation::Inventory {
character_id: character.id, character_id: character.id,
}}).await.unwrap())
}}).await?)
} }
let mut monofluids = Vec::new(); let mut monofluids = Vec::new();
@ -265,20 +265,21 @@ async fn new_character<EG: EntityGateway>(entity_gateway: &mut EG, user: &UserAc
}), }),
location: ItemLocation::Inventory { location: ItemLocation::Inventory {
character_id: character.id, character_id: character.id,
}}).await.unwrap())
}}).await?)
} }
let inventory = InventoryEntity { let inventory = InventoryEntity {
items: vec![InventoryItemEntity::Individual(weapon.clone()), InventoryItemEntity::Individual(armor.clone()), InventoryItemEntity::Individual(mag.clone()), items: vec![InventoryItemEntity::Individual(weapon.clone()), InventoryItemEntity::Individual(armor.clone()), InventoryItemEntity::Individual(mag.clone()),
InventoryItemEntity::Stacked(monomates), InventoryItemEntity::Stacked(monofluids)], InventoryItemEntity::Stacked(monomates), InventoryItemEntity::Stacked(monofluids)],
}; };
entity_gateway.set_character_inventory(&character.id, &inventory).await;
entity_gateway.set_character_bank(&character.id, &BankEntity::default(), BankName("".into())).await;
entity_gateway.set_character_inventory(&character.id, &inventory).await?;
entity_gateway.set_character_bank(&character.id, &BankEntity::default(), BankName("".into())).await?;
let mut equipped = EquippedEntity::default(); let mut equipped = EquippedEntity::default();
equipped.weapon = Some(weapon.id); equipped.weapon = Some(weapon.id);
equipped.armor = Some(armor.id); equipped.armor = Some(armor.id);
equipped.mag = Some(mag.id); equipped.mag = Some(mag.id);
entity_gateway.set_character_equips(&character.id, &equipped).await;
entity_gateway.set_character_equips(&character.id, &equipped).await?;
Ok(())
} }
@ -303,7 +304,7 @@ impl<EG: EntityGateway> CharacterServerState<EG> {
Ok(match get_login_status(&self.entity_gateway, pkt).await.and_then(check_if_already_online) { Ok(match get_login_status(&self.entity_gateway, pkt).await.and_then(check_if_already_online) {
Ok(mut user) => { Ok(mut user) => {
user.at_character = true; user.at_character = true;
self.entity_gateway.save_user(&user).await.map_err(|_| CharacterError::DbError)?;
self.entity_gateway.save_user(&user).await?;
let mut response = LoginResponse::by_status(AccountStatus::Ok, Session::new()); let mut response = LoginResponse::by_status(AccountStatus::Ok, Session::new());
response.guildcard = user.guildcard; response.guildcard = user.guildcard;
@ -457,7 +458,7 @@ impl<EG: EntityGateway> CharacterServerState<EG> {
let client = self.clients.get_mut(&id).ok_or(CharacterError::ClientNotFound(id))?; let client = self.clients.get_mut(&id).ok_or(CharacterError::ClientNotFound(id))?;
let mut user = client.user.as_mut().unwrap(); let mut user = client.user.as_mut().unwrap();
if user.flags == USERFLAG_NEWCHAR { if user.flags == USERFLAG_NEWCHAR {
new_character(&mut self.entity_gateway, &user, preview).await
new_character(&mut self.entity_gateway, &user, preview).await?
} }
if user.flags == USERFLAG_DRESSINGROOM { if user.flags == USERFLAG_DRESSINGROOM {
// TODO: dressing room stuff // TODO: dressing room stuff

Loading…
Cancel
Save