Tons of fixes

Added movement updates
Updated how entities are checked
Events sending between packet service all the way to the logic service
This commit is contained in:
2025-06-25 12:30:07 -04:00
parent f75782885b
commit d906cd8d64
34 changed files with 3550 additions and 186 deletions

View File

@@ -2,13 +2,14 @@ use crate::character_client::CharacterClient;
use crate::connection_service::ConnectionService;
use crate::packet::{send_packet, Packet, PacketPayload};
use crate::packet_type::PacketType;
use crate::world_client::WorldClientManager;
use chrono::{Local, Timelike};
use std::error::Error;
use std::sync::Arc;
use tokio::net::TcpStream;
use tokio::sync::Mutex;
use tonic::transport::Channel;
use tracing::debug;
use tracing::{debug, error, info, warn};
use utils::service_discovery::get_kube_service_endpoints_by_dns;
fn distance(x1: f64, y1: f64, x2: f64, y2: f64) -> u16 {
@@ -21,6 +22,7 @@ pub(crate) async fn handle_change_map_req(
character_client: Arc<Mutex<CharacterClient>>,
connection_service: Arc<ConnectionService>,
connection_id: String,
world_client_manager: Arc<WorldClientManager>,
) -> Result<(), Box<dyn Error + Send + Sync>> {
use crate::packets::cli_change_map_req::*;
use crate::packets::srv_change_map_reply::*;
@@ -51,6 +53,16 @@ pub(crate) async fn handle_change_map_req(
let now = Local::now();
let time_as_u16 = (now.hour() * 100 + now.minute()) as u16;
// if let Err(e) = world_client_manager
// .send_client_map_change_event(&*session_id, request.map_id as u32)
// .await
// {
// warn!("Failed to send map change event to world service: {}", e);
// // Don't return error as the map change itself was successful
// }
//
// debug!("Sent map change event for client {} to world service: map {}", session_id, request.map_id);
let data = SrvChangeMapReply {
object_index: client_id,
hp: stats.hp as u16,
@@ -80,6 +92,7 @@ pub(crate) async fn handle_mouse_cmd_req(
packet: Packet,
connection_service: Arc<ConnectionService>,
connection_id: String,
world_client_manager: Arc<WorldClientManager>,
) -> Result<(), Box<dyn Error + Send + Sync>> {
use crate::packets::cli_mouse_cmd::*;
use crate::packets::srv_mouse_cmd::*;
@@ -89,12 +102,25 @@ pub(crate) async fn handle_mouse_cmd_req(
let mut char_id = 0;
let mut client_id = 0;
let mut character_id_list: Vec<u32> = Vec::new();
let mut session_id = "".to_string();
if let Some(mut state) = connection_service.get_connection(&connection_id) {
char_id = state.character_id.expect("Missing character id in connection state");
character_id_list = state.character_list.clone().expect("Missing character id list");
client_id = state.client_id;
session_id = state.session_id.clone().expect("Missing session id in connection state");
}
if let Err(e) = world_client_manager
.send_client_move_event(&*session_id, request.x, request.y, request.z as f32)
.await
{
error!("Failed to send move event to world service: {}", e);
return Err(e);
}
debug!("Sent move event for client {} to world service: ({}, {}, {})",
session_id, request.x, request.y, request.z);
let data = SrvMouseCmd {
id: client_id,
target_id: request.target_id,
@@ -112,6 +138,8 @@ pub(crate) async fn handle_mouse_cmd_req(
Ok(())
}
// World service integration - movement events
pub(crate) async fn handle_togggle_move_req(
packet: Packet,
connection_service: Arc<ConnectionService>,
@@ -188,4 +216,97 @@ pub(crate) async fn handle_set_animation_req(
send_packet(&mut locked_stream, &response_packet).await?;
}
Ok(())
}
// World service integration - movement events
async fn handle_world_events(
world_client_manager: Arc<WorldClientManager>,
connection_service: Arc<ConnectionService>,
session_id: String,
) {
info!("Starting world event handler for session {}", session_id);
loop {
match world_client_manager.receive_world_event(&session_id).await {
Some(world_event) => {
// debug!("Received world event for session {}: {:?}", session_id, world_event);
// Process the world event and convert to game packets
match world_event.event {
Some(crate::world_client::world::world_event::Event::NpcSpawn(npc_spawn)) => {
debug!("Processing NPC spawn event: {:?}", npc_spawn);
// Convert to the appropriate game packet and send to client
// This would involve creating a spawn packet and sending it through the connection's writer
}
Some(crate::world_client::world::world_event::Event::MobSpawn(mob_spawn)) => {
debug!("Processing mob spawn event: {:?}", mob_spawn);
// Convert to the appropriate game packet and send to client
}
Some(crate::world_client::world::world_event::Event::ObjectDespawn(despawn)) => {
debug!("Processing object despawn event: {:?}", despawn);
// Convert to the appropriate game packet and send to client
}
Some(crate::world_client::world::world_event::Event::NearbyUpdate(nearby_update)) => {
debug!("Processing nearby objects update: {} objects", nearby_update.objects.len());
// Convert to the appropriate game packet and send to client
let npcs = nearby_update.objects.iter().filter(|obj| obj.object_type == 2).collect::<Vec<_>>();
let mobs = nearby_update.objects.iter().filter(|obj| obj.object_type == 3).collect::<Vec<_>>();
debug!("There are {} npcs and {} mobs", npcs.len(), mobs.len());
debug!("Nearby npcs: {:?}", npcs);
debug!("Nearby mobs: {:?}", mobs);
// if !npcs.is_empty() {
// // Send all nearby npcs in a single batch update
// let batch_event = SrvNpcChar {
// id: 0,
// x: 0.0,
// y: 0.0,
// dest_x: 0.0,
// dest_y: 0.0,
// command: 0,
// target_id: 0,
// move_mode: 0,
// hp: 0,
// team_id: 0,
// status_flag: 0,
// npc_id: 0,
// quest_id: 0,
// angle: 0.0,
// event_status: 0,
// };
// let response_packet = Packet::new(PacketType::PakwcNpcChar, &batch_event)?;
// 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?;
// }
// }
}
None => {
warn!("Received world event with no event data for session {}", session_id);
}
}
}
None => {
debug!("World event stream ended for session {}", session_id);
break;
}
}
}
info!("World event handler ended for session {}", session_id);
}
// Helper function to start world event handling for a client
pub fn start_world_event_handler(
world_client_manager: Arc<WorldClientManager>,
connection_service: Arc<ConnectionService>,
session_id: String,
) {
tokio::spawn(async move {
handle_world_events(world_client_manager, connection_service, session_id).await;
});
}