use crate::{chat, chat::OutboundChat, GameDialect};
use anyhow::Result;
#[derive(Clone, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum GameCommand {
SendChat(OutboundChat),
}
#[derive(Clone, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub struct EncodedCommand {
pub opcode: u16,
pub body: Vec<u8>,
}
pub fn encode(
dialect: GameDialect,
command: &GameCommand,
character: &str,
) -> Result<EncodedCommand> {
match command {
GameCommand::SendChat(message) => Ok(EncodedCommand {
opcode: match dialect {
GameDialect::TitaniumP99 => 0x1004,
GameDialect::EqMac => 0x0741,
},
body: chat::encode_outbound_for(dialect, message, character)?,
}),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn chat_command_selects_each_dialects_opcode_and_layout() {
let command = GameCommand::SendChat(OutboundChat::Say("ok".into()));
let titanium = encode(GameDialect::TitaniumP99, &command, "Example").unwrap();
let eqmac = encode(GameDialect::EqMac, &command, "Example").unwrap();
assert_eq!(titanium.opcode, 0x1004);
assert_eq!(eqmac.opcode, 0x0741);
assert_ne!(titanium.body, eqmac.body);
}
}