Updated chat handler to use the MessageType enum values instead of numbers
Added local_id tracking for the packet service Added mob spawning packet in response to game logic events (only for a NearbyUpdate event right now)
This commit is contained in:
@@ -11,6 +11,7 @@ use tokio::sync::Mutex;
|
||||
use tonic::transport::Channel;
|
||||
use tracing::{debug, error, info, warn};
|
||||
use utils::service_discovery::get_kube_service_endpoints_by_dns;
|
||||
use crate::world_client::world::WorldObject;
|
||||
|
||||
fn distance(x1: f64, y1: f64, x2: f64, y2: f64) -> u16 {
|
||||
let dist = ((x2 - x1).powi(2) + (y2 - y1).powi(2)).sqrt();
|
||||
@@ -220,6 +221,50 @@ pub(crate) async fn handle_set_animation_req(
|
||||
|
||||
// World service integration - movement events
|
||||
|
||||
pub(crate) async fn spawn_mob(
|
||||
mob: &WorldObject,
|
||||
connection_service: Arc<ConnectionService>,
|
||||
session_id: String
|
||||
) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
use crate::packets::srv_mob_char::SrvMobChar;
|
||||
|
||||
let data = SrvMobChar {
|
||||
id: mob.id as u16,
|
||||
x: mob.x,
|
||||
y: mob.y,
|
||||
dest_x: 0.0,
|
||||
dest_y: 0.0,
|
||||
command: 0,
|
||||
target_id: 0,
|
||||
move_mode: 0,
|
||||
hp: mob.hp,
|
||||
team_id: 0,
|
||||
status_flag: 0,
|
||||
npc_id: mob.id as u16,
|
||||
quest_id: 0,
|
||||
};
|
||||
let response_packet = Packet::new(PacketType::PakwcMobChar, &data)?;
|
||||
if let Some(mut state) = connection_service.get_connection_mut(&session_id) {
|
||||
let writer_clone = state.writer.clone().unwrap();
|
||||
let mut locked_stream = writer_clone.lock().await;
|
||||
send_packet(&mut locked_stream, &response_packet).await?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) async fn spawn_mobs(
|
||||
mobs: Vec<&WorldObject>,
|
||||
connection_service: Arc<ConnectionService>,
|
||||
session_id: String
|
||||
) {
|
||||
for mob in mobs {
|
||||
let result = spawn_mob(mob, connection_service.clone(), session_id.clone()).await;
|
||||
if let Err(e) = result {
|
||||
error!("Failed to spawn mob for session {}: {}", session_id, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn handle_world_events(
|
||||
world_client_manager: Arc<WorldClientManager>,
|
||||
connection_service: Arc<ConnectionService>,
|
||||
@@ -258,6 +303,14 @@ async fn handle_world_events(
|
||||
debug!("Nearby npcs: {:?}", npcs);
|
||||
debug!("Nearby mobs: {:?}", mobs);
|
||||
|
||||
// if !npcs.is_empty() {
|
||||
// spawn_npcs(npcs, connection_service.clone(), session_id.clone()).await;
|
||||
// }
|
||||
|
||||
if !mobs.is_empty() {
|
||||
spawn_mobs(mobs, connection_service.clone(), session_id.clone()).await;
|
||||
}
|
||||
|
||||
// if !npcs.is_empty() {
|
||||
// // Send all nearby npcs in a single batch update
|
||||
// let batch_event = SrvNpcChar {
|
||||
|
||||
Reference in New Issue
Block a user