dzura/server/src/protocol/packet/user_profile.rs
2024-10-18 12:51:53 -07:00

80 lines
1.7 KiB
Rust

use crate::util::pack::{pack_u8, pack_u16, unpack_u8};
use super::{
Packet,
PacketSessionListResponseRecord,
};
#[derive(Clone)]
pub struct PacketUserProfile {
pub handle:String,
}
impl PacketUserProfile {
pub fn new() -> Self
{
Self {
handle:String::new(),
}
}
}
impl Packet for PacketUserProfile {
type Data = Self;
fn decode(data:&Vec<u8>, index:&mut usize) -> Result<Self::Data, ()>
{
let mut result = Self::new();
let length = unpack_u8(data, index) as usize;
if data.len() - *index >= length {
result.handle = String::from_utf8(data[*index..*index + length].to_vec()).map_err(|_| ())?;
}
Ok(result)
}
}
#[derive(Clone)]
pub struct PacketUserProfileResponse {
pub status:u16,
pub handle:String,
pub is_online:bool,
pub history:Vec<PacketSessionListResponseRecord>,
}
impl PacketUserProfileResponse {
pub fn new() -> Self
{
Self {
status:0,
handle:String::new(),
is_online:false,
history:Vec::new(),
}
}
}
impl Packet for PacketUserProfileResponse {
type Data = Self;
fn encode(&self) -> Vec<u8>
{
let handle_bytes = self.handle.as_bytes().to_vec();
let flags = self.is_online as u16;
let mut history_bytes = pack_u16(self.history.len() as u16);
for record in &self.history {
history_bytes.append(&mut record.encode());
}
[
pack_u16(self.status as u16),
pack_u16(flags),
pack_u8(handle_bytes.len() as u8),
handle_bytes,
history_bytes,
].concat()
}
}