aboutsummaryrefslogtreecommitdiff
path: root/crates/whirl_server/src/cmd/commands
diff options
context:
space:
mode:
authorFuwn <[email protected]>2024-06-12 01:28:50 -0700
committerFuwn <[email protected]>2024-06-12 01:28:50 -0700
commita47eee894e431f039afe76cc39ecc67966d94183 (patch)
tree5954a0d262547450d7fedd5aed69d9e983d93cee /crates/whirl_server/src/cmd/commands
parentrefactor(crates): update idioms (diff)
downloadwhirl-a47eee894e431f039afe76cc39ecc67966d94183.tar.xz
whirl-a47eee894e431f039afe76cc39ecc67966d94183.zip
feat(whirl_server): short object id sending
Diffstat (limited to 'crates/whirl_server/src/cmd/commands')
-rw-r--r--crates/whirl_server/src/cmd/commands/appear_actor.rs25
-rw-r--r--crates/whirl_server/src/cmd/commands/long_location.rs53
-rw-r--r--crates/whirl_server/src/cmd/commands/mod.rs1
-rw-r--r--crates/whirl_server/src/cmd/commands/register_object_id.rs30
-rw-r--r--crates/whirl_server/src/cmd/commands/subscribe_distance.rs7
-rw-r--r--crates/whirl_server/src/cmd/commands/teleport.rs34
6 files changed, 128 insertions, 22 deletions
diff --git a/crates/whirl_server/src/cmd/commands/appear_actor.rs b/crates/whirl_server/src/cmd/commands/appear_actor.rs
index 6100fcc..1eb9ad0 100644
--- a/crates/whirl_server/src/cmd/commands/appear_actor.rs
+++ b/crates/whirl_server/src/cmd/commands/appear_actor.rs
@@ -8,29 +8,28 @@ use {
#[derive(Debug)]
pub struct AppearActor {
- pub short_object_id: i8,
- pub room_id: u16,
- pub x: i16,
- pub y: i16,
- pub z: i16,
- pub direction: i16,
+ pub room_id: u16,
+ pub x: i16,
+ pub y: i16,
+ pub z: i16,
+ pub direction: i16,
}
+
impl Creatable for AppearActor {
- fn create(&self) -> Vec<u8> {
+ fn create_with_short_object_id(&self, short_object_id: u8) -> Vec<u8> {
let mut command = BytesMut::new();
// Header
- command.put_u8(0xFE); // ObjId
+ command.put_u8(short_object_id); // ObjId
#[allow(clippy::cast_possible_truncation)]
command.put_i8(Command::ApprActr as i32 as i8); // Type
// Content
- command.put_i8(self.short_object_id); // ObjId, why is it here? Worlds...
command.put_u16(self.room_id); // Room ID
- command.put_u16(self.x as u16); // X
- command.put_u16(self.y as u16); // Y
- command.put_u16(self.z as u16); // Z
- command.put_u16(self.direction as u16); // Direction
+ command.put_i16(self.x); // X
+ command.put_i16(self.y); // Y
+ command.put_i16(self.z); // Z
+ command.put_i16(self.direction); // Direction
// Length
let mut command_as_vec = command.to_vec();
diff --git a/crates/whirl_server/src/cmd/commands/long_location.rs b/crates/whirl_server/src/cmd/commands/long_location.rs
new file mode 100644
index 0000000..1cb054f
--- /dev/null
+++ b/crates/whirl_server/src/cmd/commands/long_location.rs
@@ -0,0 +1,53 @@
+use {
+ crate::cmd::{
+ constants::Command,
+ extendable::{Creatable, Parsable},
+ },
+ byteorder::{BigEndian, ReadBytesExt},
+ bytes::{Buf, BufMut, BytesMut},
+};
+
+#[derive(Debug)]
+pub struct LongLocation {
+ pub x: i16,
+ pub y: i16,
+ pub z: i16,
+ pub direction: i16,
+}
+
+impl Parsable for LongLocation {
+ fn parse(data: Vec<u8>) -> Self {
+ let mut data = BytesMut::from(data.as_slice()).reader();
+
+ Self {
+ x: data.read_i16::<BigEndian>().unwrap(),
+ y: data.read_i16::<BigEndian>().unwrap(),
+ z: data.read_i16::<BigEndian>().unwrap(),
+ direction: data.read_i16::<BigEndian>().unwrap(),
+ }
+ }
+}
+
+impl Creatable for LongLocation {
+ fn create_with_short_object_id(&self, short_object_id: u8) -> Vec<u8> {
+ let mut command = BytesMut::new();
+
+ // Header
+ command.put_u8(short_object_id); // ObjId
+ #[allow(clippy::cast_possible_truncation)]
+ command.put_i8(Command::LongLoc as i32 as i8); // Type
+
+ // Content
+ command.put_i16(self.x);
+ command.put_i16(self.y);
+ command.put_i16(self.z);
+ command.put_i16(self.direction);
+
+ // Length
+ let mut command_as_vec = command.to_vec();
+ command_as_vec.insert(0, command.len() as u8 + 1);
+
+ // Return bytes
+ command_as_vec
+ }
+}
diff --git a/crates/whirl_server/src/cmd/commands/mod.rs b/crates/whirl_server/src/cmd/commands/mod.rs
index 0129d47..0da031f 100644
--- a/crates/whirl_server/src/cmd/commands/mod.rs
+++ b/crates/whirl_server/src/cmd/commands/mod.rs
@@ -4,6 +4,7 @@
pub mod action;
pub mod appear_actor;
pub mod buddy_list;
+pub mod long_location;
pub mod property;
pub mod redirect_id;
pub mod register_object_id;
diff --git a/crates/whirl_server/src/cmd/commands/register_object_id.rs b/crates/whirl_server/src/cmd/commands/register_object_id.rs
index bdf4bca..8c73d8c 100644
--- a/crates/whirl_server/src/cmd/commands/register_object_id.rs
+++ b/crates/whirl_server/src/cmd/commands/register_object_id.rs
@@ -2,15 +2,21 @@
// SPDX-License-Identifier: GPL-3.0-only
use {
- crate::cmd::{constants::Command, extendable::Creatable},
- bytes::{BufMut, BytesMut},
+ crate::cmd::{
+ constants::Command,
+ extendable::{Creatable, Parsable},
+ },
+ byteorder::ReadBytesExt,
+ bytes::{Buf, BufMut, BytesMut},
+ std::io::Read,
};
-#[derive(Debug)]
+#[derive(Debug, Clone)]
pub struct RegisterObjectId {
pub long_object_id: String,
pub short_object_id: i8,
}
+
impl Creatable for RegisterObjectId {
fn create(&self) -> Vec<u8> {
let mut command = BytesMut::new();
@@ -33,3 +39,21 @@ impl Creatable for RegisterObjectId {
command_as_vec
}
}
+
+impl Parsable for RegisterObjectId {
+ fn parse(data: Vec<u8>) -> Self {
+ // https://stackoverflow.com/questions/41034635/how-do-i-convert-between-string-str-vecu8-and-u8
+ let mut data = BytesMut::from(data.as_slice()).reader();
+
+ // Content
+ let long_object_id_length = data.read_u8().unwrap() as usize;
+ let mut long_object_id = vec![0; long_object_id_length];
+ data.read_exact(&mut long_object_id).unwrap();
+ let short_object_id = data.read_i8().unwrap();
+
+ Self {
+ long_object_id: String::from_utf8(long_object_id).unwrap(),
+ short_object_id,
+ }
+ }
+}
diff --git a/crates/whirl_server/src/cmd/commands/subscribe_distance.rs b/crates/whirl_server/src/cmd/commands/subscribe_distance.rs
index 9877a5d..61d1975 100644
--- a/crates/whirl_server/src/cmd/commands/subscribe_distance.rs
+++ b/crates/whirl_server/src/cmd/commands/subscribe_distance.rs
@@ -16,10 +16,9 @@ impl Parsable for SubscribeDistance {
fn parse(data: Vec<u8>) -> Self {
// https://stackoverflow.com/questions/41034635/how-do-i-convert-between-string-str-vecu8-and-u8
let mut data = BytesMut::from(data.as_slice()).reader();
+ let room_number = data.read_i16::<BigEndian>().unwrap();
+ let distance = data.read_i16::<BigEndian>().unwrap();
- Self {
- distance: data.read_i16::<BigEndian>().unwrap(),
- room_number: data.read_i16::<BigEndian>().unwrap(),
- }
+ Self { distance, room_number }
}
}
diff --git a/crates/whirl_server/src/cmd/commands/teleport.rs b/crates/whirl_server/src/cmd/commands/teleport.rs
index aa4b7dd..f3a5290 100644
--- a/crates/whirl_server/src/cmd/commands/teleport.rs
+++ b/crates/whirl_server/src/cmd/commands/teleport.rs
@@ -2,9 +2,12 @@
// SPDX-License-Identifier: GPL-3.0-only
use {
- crate::cmd::extendable::Parsable,
+ crate::cmd::{
+ constants::Command,
+ extendable::{Creatable, Parsable},
+ },
byteorder::{BigEndian, ReadBytesExt},
- bytes::{Buf, BytesMut},
+ bytes::{Buf, BufMut, BytesMut},
};
#[derive(Debug)]
@@ -17,6 +20,7 @@ pub struct Teleport {
pub z: f32,
pub direction: f32,
}
+
impl Parsable for Teleport {
fn parse(data: Vec<u8>) -> Self {
// https://stackoverflow.com/questions/41034635/how-do-i-convert-between-string-str-vecu8-and-u8
@@ -33,3 +37,29 @@ impl Parsable for Teleport {
}
}
}
+
+impl Creatable for Teleport {
+ fn create_with_short_object_id(&self, short_object_id: u8) -> Vec<u8> {
+ let mut command = BytesMut::new();
+
+ // Header
+ command.put_u8(short_object_id); // ObjId
+ command.put_u8(Command::Teleport as u8); // Type
+
+ // Content
+ command.put_i16(i16::from(self.room_id));
+ command.put_u8(self.exit_type);
+ command.put_u8(self.entry_type);
+ command.put_i16(self.x as i16);
+ command.put_i16(self.y as i16);
+ command.put_i16(self.z as i16);
+ command.put_i16(self.direction as i16);
+
+ // Length
+ let mut command_as_vec = command.to_vec();
+ command_as_vec.insert(0, command.len() as u8 + 1);
+
+ // Return bytes
+ command_as_vec
+ }
+}