use std::io::{Read, Write};
use crate::tbc::{
LfgData, LfgType, LfgUpdateLookingForMore,
};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Default)]
pub struct SMSG_LFG_UPDATE {
pub queued: bool,
pub is_looking_for_group: bool,
pub looking_for_more: SMSG_LFG_UPDATE_LfgUpdateLookingForMore,
}
impl crate::private::Sealed for SMSG_LFG_UPDATE {}
impl SMSG_LFG_UPDATE {
fn read_inner(mut r: &mut &[u8], body_size: u32) -> Result<Self, crate::errors::ParseErrorKind> {
if !(3..=7).contains(&body_size) {
return Err(crate::errors::ParseErrorKind::InvalidSize);
}
let queued = crate::util::read_bool_u8(&mut r)?;
let is_looking_for_group = crate::util::read_bool_u8(&mut r)?;
let looking_for_more = crate::util::read_u8_le(&mut r)?.try_into()?;
let looking_for_more_if = match looking_for_more {
LfgUpdateLookingForMore::NotLookingForMore => SMSG_LFG_UPDATE_LfgUpdateLookingForMore::NotLookingForMore,
LfgUpdateLookingForMore::LookingForMore => {
let data = LfgData::read(&mut r)?;
SMSG_LFG_UPDATE_LfgUpdateLookingForMore::LookingForMore {
data,
}
}
};
Ok(Self {
queued,
is_looking_for_group,
looking_for_more: looking_for_more_if,
})
}
}
impl crate::Message for SMSG_LFG_UPDATE {
const OPCODE: u32 = 0x036c;
#[cfg(feature = "print-testcase")]
fn message_name(&self) -> &'static str {
"SMSG_LFG_UPDATE"
}
#[cfg(feature = "print-testcase")]
fn to_test_case_string(&self) -> Option<String> {
use std::fmt::Write;
use crate::traits::Message;
let mut s = String::new();
writeln!(s, "test SMSG_LFG_UPDATE {{").unwrap();
writeln!(s, " queued = {};", if self.queued { "TRUE" } else { "FALSE" }).unwrap();
writeln!(s, " is_looking_for_group = {};", if self.is_looking_for_group { "TRUE" } else { "FALSE" }).unwrap();
writeln!(s, " looking_for_more = {};", LfgUpdateLookingForMore::try_from(self.looking_for_more.as_int()).unwrap().as_test_case_value()).unwrap();
match &self.looking_for_more {
crate::tbc::SMSG_LFG_UPDATE_LfgUpdateLookingForMore::LookingForMore {
data,
} => {
writeln!(s, " data = {{").unwrap();
writeln!(s, " entry = {};", data.entry).unwrap();
writeln!(s, " lfg_type = {};", data.lfg_type.as_test_case_value()).unwrap();
writeln!(s, " }};").unwrap();
}
_ => {}
}
writeln!(s, "}} [").unwrap();
let [a, b] = (u16::try_from(self.size() + 2).unwrap()).to_be_bytes();
writeln!(s, " {a:#04X}, {b:#04X}, /* size */").unwrap();
let [a, b] = 876_u16.to_le_bytes();
writeln!(s, " {a:#04X}, {b:#04X}, /* opcode */").unwrap();
let mut bytes: Vec<u8> = Vec::new();
self.write_into_vec(&mut bytes).unwrap();
let mut bytes = bytes.into_iter();
crate::util::write_bytes(&mut s, &mut bytes, 1, "queued", " ");
crate::util::write_bytes(&mut s, &mut bytes, 1, "is_looking_for_group", " ");
crate::util::write_bytes(&mut s, &mut bytes, 1, "looking_for_more", " ");
match &self.looking_for_more {
crate::tbc::SMSG_LFG_UPDATE_LfgUpdateLookingForMore::LookingForMore {
data,
} => {
writeln!(s, " /* data: LfgData start */").unwrap();
crate::util::write_bytes(&mut s, &mut bytes, 2, "entry", " ");
crate::util::write_bytes(&mut s, &mut bytes, 2, "lfg_type", " ");
writeln!(s, " /* data: LfgData end */").unwrap();
}
_ => {}
}
writeln!(s, "] {{").unwrap();
writeln!(s, " versions = \"{}\";", std::env::var("WOWM_TEST_CASE_WORLD_VERSION").unwrap_or("2.4.3".to_string())).unwrap();
writeln!(s, "}}\n").unwrap();
Some(s)
}
fn size_without_header(&self) -> u32 {
self.size() as u32
}
fn write_into_vec(&self, mut w: impl Write) -> Result<(), std::io::Error> {
w.write_all(u8::from(self.queued).to_le_bytes().as_slice())?;
w.write_all(u8::from(self.is_looking_for_group).to_le_bytes().as_slice())?;
w.write_all(&(self.looking_for_more.as_int().to_le_bytes()))?;
match &self.looking_for_more {
SMSG_LFG_UPDATE_LfgUpdateLookingForMore::LookingForMore {
data,
} => {
data.write_into_vec(&mut w)?;
}
_ => {}
}
Ok(())
}
fn read_body<S: crate::private::Sealed>(r: &mut &[u8], body_size: u32) -> Result<Self, crate::errors::ParseError> {
Self::read_inner(r, body_size).map_err(|a| crate::errors::ParseError::new(876, "SMSG_LFG_UPDATE", body_size, a))
}
}
#[cfg(feature = "tbc")]
impl crate::tbc::ServerMessage for SMSG_LFG_UPDATE {}
impl SMSG_LFG_UPDATE {
pub(crate) const fn size(&self) -> usize {
1 + 1 + self.looking_for_more.size() }
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum SMSG_LFG_UPDATE_LfgUpdateLookingForMore {
NotLookingForMore,
LookingForMore {
data: LfgData,
},
}
impl Default for SMSG_LFG_UPDATE_LfgUpdateLookingForMore {
fn default() -> Self {
Self::NotLookingForMore
}
}
impl SMSG_LFG_UPDATE_LfgUpdateLookingForMore {
pub(crate) const fn as_int(&self) -> u8 {
match self {
Self::NotLookingForMore => 0,
Self::LookingForMore { .. } => 1,
}
}
}
impl std::fmt::Display for SMSG_LFG_UPDATE_LfgUpdateLookingForMore {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::NotLookingForMore => f.write_str("NotLookingForMore"),
Self::LookingForMore{ .. } => f.write_str("LookingForMore"),
}
}
}
impl SMSG_LFG_UPDATE_LfgUpdateLookingForMore {
pub(crate) const fn size(&self) -> usize {
match self {
Self::LookingForMore {
..
} => {
1
+ 4 }
_ => 1,
}
}
}