smo-server/src/faker.rs
2024-12-21 05:35:44 -06:00

78 lines
1.6 KiB
Rust

use glam::Vec3;
use xtra::{Actor, Address, Handler};
use crate::{
manager,
packet::{
Packet, PacketData,
PacketData_variants::{Connect, Player},
},
player::broadcast_packet,
protocol::String,
server::PlayerConnected,
};
pub struct Faker {
pub address: Address<Faker>,
}
impl Actor for Faker {
type Stop = ();
async fn started(&mut self, _: &xtra::Mailbox<Self>) -> Result<(), Self::Stop> {
manager()
.send(PlayerConnected {
id: 1,
name: "Bot".try_into().unwrap(),
})
.await
.unwrap();
Ok(())
}
async fn stopped(self) -> Self::Stop {}
}
impl Handler<Packet> for Faker {
type Return = ();
async fn handle(&mut self, message: Packet, _: &mut xtra::Context<Self>) -> Self::Return {
// trace!("got packet {message:?}");
match message.data {
PacketData::Connect(connect) => {
broadcast_packet(Packet {
user_id: 1,
udp: message.udp,
data: PacketData::Connect(Connect {
client_name: String::try_from("Bot").unwrap(),
..connect
}),
})
.await;
}
PacketData::Player(player) => {
let pos = Vec3::from_array(player.position);
broadcast_packet(Packet {
user_id: 1,
udp: message.udp,
data: PacketData::Player(Player {
position: (pos * Vec3::new(1.0, 1.0, -1.0) + Vec3::new(0., -150., 0.)).into(),
..player
}),
})
.await;
}
data => {
broadcast_packet(Packet {
user_id: 1,
udp: message.udp,
data,
})
.await;
}
}
}
}