use AvatarBaseID instead of raw integer in more places

This commit is contained in:
xeon 2024-07-25 13:03:37 +03:00
parent a7da14c109
commit d3cd213d27
6 changed files with 44 additions and 23 deletions

View file

@ -27,7 +27,7 @@ pub async fn avatar(
let should_save = { let should_save = {
let mut player = player_lock.lock().await; let mut player = player_lock.lock().await;
player.basic_data_model.frontend_avatar_id = avatar_id.value() as i32; player.basic_data_model.frontend_avatar_id = Some(avatar_id);
player.current_session_id().is_none() player.current_session_id().is_none()
}; };

View file

@ -1,3 +1,5 @@
use data::tables::AvatarBaseID;
use crate::logic::game::{GameInstance, LogicError}; use crate::logic::game::{GameInstance, LogicError};
use crate::logic::procedure::ProcedureAction; use crate::logic::procedure::ProcedureAction;
@ -20,6 +22,8 @@ pub async fn on_create_role(
player: &mut Player, player: &mut Player,
req: CreateRoleCsReq, req: CreateRoleCsReq,
) -> NetResult<CreateRoleScRsp> { ) -> NetResult<CreateRoleScRsp> {
let avatar_id = AvatarBaseID::new(req.avatar_id).ok_or(Retcode::RetFail)?;
let GameInstance::Fresh(fresh_game) = &mut player.game_instance else { let GameInstance::Fresh(fresh_game) = &mut player.game_instance else {
return Err(NetError::from(Retcode::RetFail)); return Err(NetError::from(Retcode::RetFail));
}; };
@ -29,7 +33,7 @@ pub async fn on_create_role(
.on_action(ProcedureAction::SelectRole) .on_action(ProcedureAction::SelectRole)
.map_err(LogicError::from)?; .map_err(LogicError::from)?;
player.set_frontend_avatar(req.avatar_id as i32)?; player.set_frontend_avatar(avatar_id)?;
session session
.notify(PlayerSyncScNotify { .notify(PlayerSyncScNotify {

View file

@ -1,4 +1,4 @@
use data::tables::SectionConfigID; use data::tables::{AvatarBaseID, SectionConfigID};
use proto::*; use proto::*;
use thiserror::Error; use thiserror::Error;
@ -8,7 +8,7 @@ use super::NapGameMode;
pub struct FrontendGame { pub struct FrontendGame {
section_id: SectionConfigID, section_id: SectionConfigID,
frontend_avatar_id: i32, frontend_avatar_id: AvatarBaseID,
camera_x: u32, camera_x: u32,
camera_y: u32, camera_y: u32,
born_pos: String, born_pos: String,
@ -18,12 +18,15 @@ pub struct FrontendGame {
} }
#[derive(Error, Debug)] #[derive(Error, Debug)]
pub enum FrontendGameError {} pub enum FrontendGameError {
#[error("player's frontend avatar is None")]
NoFrontendAvatar,
}
impl FrontendGame { impl FrontendGame {
pub fn new( pub fn new(
section_id: SectionConfigID, section_id: SectionConfigID,
avatar_id: i32, avatar_id: AvatarBaseID,
main_city_time: MainCityTime, main_city_time: MainCityTime,
avatar_pos: Vector3f, avatar_pos: Vector3f,
avatar_rot: Vector3f, avatar_rot: Vector3f,
@ -50,7 +53,7 @@ impl NapGameMode for FrontendGame {
scene_type: self.scene_type() as u32, scene_type: self.scene_type() as u32,
hall_scene_info: Some(HallSceneInfo { hall_scene_info: Some(HallSceneInfo {
section_id: self.section_id.value(), section_id: self.section_id.value(),
frontend_avatar_id: self.frontend_avatar_id as u32, frontend_avatar_id: self.frontend_avatar_id.value(),
camera_x: self.camera_x, camera_x: self.camera_x,
camera_y: self.camera_y, camera_y: self.camera_y,
born_pos: self born_pos: self

View file

@ -1,4 +1,4 @@
use data::tables::ProcedureConfigID; use data::tables::{AvatarBaseID, ProcedureConfigID};
use proto::{BasicDataModelBin, PlayerBasicInfo}; use proto::{BasicDataModelBin, PlayerBasicInfo};
pub struct BasicDataModel { pub struct BasicDataModel {
@ -6,7 +6,7 @@ pub struct BasicDataModel {
pub exp: u32, pub exp: u32,
pub profile_icon: u32, pub profile_icon: u32,
pub nick_name: Option<String>, pub nick_name: Option<String>,
pub frontend_avatar_id: i32, pub frontend_avatar_id: Option<AvatarBaseID>,
pub beginner_procedure_id: Option<ProcedureConfigID>, pub beginner_procedure_id: Option<ProcedureConfigID>,
} }
@ -17,7 +17,7 @@ impl Default for BasicDataModel {
exp: 0, exp: 0,
profile_icon: 3200000, profile_icon: 3200000,
nick_name: None, nick_name: None,
frontend_avatar_id: 0, frontend_avatar_id: None,
beginner_procedure_id: Some(ProcedureConfigID::new_unchecked(1)), beginner_procedure_id: Some(ProcedureConfigID::new_unchecked(1)),
} }
} }
@ -25,12 +25,17 @@ impl Default for BasicDataModel {
impl BasicDataModel { impl BasicDataModel {
pub fn player_basic_info(&self) -> PlayerBasicInfo { pub fn player_basic_info(&self) -> PlayerBasicInfo {
let avatar_id = self
.frontend_avatar_id
.map(|i| i.value())
.unwrap_or_default();
PlayerBasicInfo { PlayerBasicInfo {
nick_name: self.nick_name.clone().unwrap_or_default(), nick_name: self.nick_name.clone().unwrap_or_default(),
exp: self.exp, exp: self.exp,
level: self.level, level: self.level,
avatar_id: self.frontend_avatar_id as u32, avatar_id,
frontend_avatar_id: self.frontend_avatar_id as u32, frontend_avatar_id: avatar_id,
kbjleelonfe: self.profile_icon, kbjleelonfe: self.profile_icon,
..Default::default() ..Default::default()
} }
@ -41,7 +46,10 @@ impl BasicDataModel {
level: bin.level, level: bin.level,
exp: bin.exp, exp: bin.exp,
profile_icon: bin.profile_icon, profile_icon: bin.profile_icon,
frontend_avatar_id: bin.frontend_avatar_id, frontend_avatar_id: match bin.frontend_avatar_id {
1.. => AvatarBaseID::new(bin.frontend_avatar_id as u32),
_ => None,
},
beginner_procedure_id: match bin.beginner_procedure_id { beginner_procedure_id: match bin.beginner_procedure_id {
1.. => ProcedureConfigID::new(bin.beginner_procedure_id as u32), 1.. => ProcedureConfigID::new(bin.beginner_procedure_id as u32),
_ => None, _ => None,
@ -58,7 +66,10 @@ impl BasicDataModel {
level: self.level, level: self.level,
exp: self.exp, exp: self.exp,
profile_icon: self.profile_icon, profile_icon: self.profile_icon,
frontend_avatar_id: self.frontend_avatar_id, frontend_avatar_id: self
.frontend_avatar_id
.map(|i| i.value() as i32)
.unwrap_or(-1),
nick_name: self.nick_name.clone().unwrap_or_default(), nick_name: self.nick_name.clone().unwrap_or_default(),
beginner_procedure_id: self beginner_procedure_id: self
.beginner_procedure_id .beginner_procedure_id

View file

@ -1,7 +1,7 @@
use data::tables::{self, AvatarBaseID}; use data::tables::{self, AvatarBaseID};
use proto::{ItemStatic, PlayerDataBin, Retcode}; use proto::{ItemStatic, PlayerDataBin, Retcode};
use super::game::{FrontendGame, GameInstance, LogicError}; use super::game::{FrontendGame, FrontendGameError, GameInstance, LogicError};
use super::item::{ItemModel, ItemUID}; use super::item::{ItemModel, ItemUID};
use super::main_city_model::MainCityModel; use super::main_city_model::MainCityModel;
use super::role::RoleModel; use super::role::RoleModel;
@ -19,7 +19,7 @@ pub struct Player {
} }
impl Player { impl Player {
const MAIN_AVATAR_IDS: [i32; 2] = [2011, 2021]; const MAIN_AVATAR_IDS: [u32; 2] = [2011, 2021];
pub fn save_to_bin(&self) -> PlayerDataBin { pub fn save_to_bin(&self) -> PlayerDataBin {
PlayerDataBin { PlayerDataBin {
@ -62,11 +62,15 @@ impl Player {
} }
pub fn init_frontend_game(&mut self) -> Result<(), LogicError> { pub fn init_frontend_game(&mut self) -> Result<(), LogicError> {
let Some(frontend_avatar_id) = self.basic_data_model.frontend_avatar_id else {
return Err(LogicError::from(FrontendGameError::NoFrontendAvatar));
};
let main_city = &self.main_city_model; let main_city = &self.main_city_model;
self.game_instance = GameInstance::Frontend( self.game_instance = GameInstance::Frontend(
FrontendGame::new( FrontendGame::new(
main_city.section_id, main_city.section_id,
self.basic_data_model.frontend_avatar_id, frontend_avatar_id,
main_city.main_city_time.clone(), main_city.main_city_time.clone(),
main_city.position.clone(), main_city.position.clone(),
main_city.rotation.clone(), main_city.rotation.clone(),
@ -77,16 +81,16 @@ impl Player {
Ok(()) Ok(())
} }
pub fn set_frontend_avatar(&mut self, id: i32) -> Result<(), Retcode> { pub fn set_frontend_avatar(&mut self, id: AvatarBaseID) -> Result<(), Retcode> {
(self.basic_data_model.frontend_avatar_id == 0) (self.basic_data_model.frontend_avatar_id.is_none())
.then_some(()) .then_some(())
.ok_or(Retcode::RetFail)?; .ok_or(Retcode::RetFail)?;
(Self::MAIN_AVATAR_IDS.contains(&id)) (Self::MAIN_AVATAR_IDS.contains(&id.value()))
.then_some(()) .then_some(())
.ok_or(Retcode::RetFail)?; .ok_or(Retcode::RetFail)?;
self.basic_data_model.frontend_avatar_id = id; self.basic_data_model.frontend_avatar_id = Some(id);
Ok(()) Ok(())
} }

View file

@ -43,9 +43,8 @@ async fn main() -> Result<(), Box<dyn Error>> {
config, config,
}); });
let command_mgr = CommandManager::new(state.clone());
if let Some((rl, out)) = rl { if let Some((rl, out)) = rl {
let command_mgr = CommandManager::new(state.clone());
tokio::spawn(async move { command_mgr.run(rl, out).await }); tokio::spawn(async move { command_mgr.run(rl, out).await });
} }