JaneDoe-ZS/nap_gameserver/src/logic/role/role_model.rs
xeon 99123a15ef newtype fun
use newtypes for template ids, pros:
1) enforces id validation
2) ease of use (helper method for getting template struct right away)
2024-07-25 01:24:48 +03:00

52 lines
1.3 KiB
Rust

use data::tables::AvatarBaseID;
use proto::{AvatarSync, RoleModelBin};
use crate::logic::role::Avatar;
pub struct RoleModel {
pub avatar_list: Vec<Avatar>,
}
impl Default for RoleModel {
fn default() -> Self {
Self {
avatar_list: Self::DEFAULT_AVATARS
.iter()
.map(|tmpl_id| Avatar::new(AvatarBaseID::new_unchecked(*tmpl_id)))
.collect(),
}
}
}
impl RoleModel {
const DEFAULT_AVATARS: [u32; 2] = [1011, 1081];
pub fn add_avatar(&mut self, template_id: AvatarBaseID) {
if !self
.avatar_list
.iter()
.any(|a| a.template_id == template_id)
{
self.avatar_list.push(Avatar::new(template_id));
}
}
pub fn avatar_sync(&self) -> AvatarSync {
AvatarSync {
avatar_list: self.avatar_list.iter().map(Avatar::to_client).collect(),
..Default::default()
}
}
pub fn from_bin(bin: RoleModelBin) -> Self {
Self {
avatar_list: bin.avatar_list.into_iter().map(Avatar::from_bin).collect(),
}
}
pub fn to_bin(&self) -> RoleModelBin {
RoleModelBin {
avatar_list: self.avatar_list.iter().map(Avatar::to_bin).collect(),
}
}
}