More work.

Added chat service
Updated packet service to pass the tcp stream around in a Arc type.
Updated character position data to not require multiplying the coords
Added more debug logs
Added an interceptor for gRPC comms with the chat server
Updated build and push script for the chat server changes
This commit is contained in:
2025-06-06 17:52:29 -04:00
parent 85d41c0239
commit ad6ba2c8e6
32 changed files with 787 additions and 92 deletions

View File

@@ -17,7 +17,7 @@ use tonic::{Code, Status};
use tracing::{debug, error, info, warn};
use utils::null_string::NullTerminatedString;
fn string_to_u32(s: &str) -> u32 {
pub(crate) fn string_to_u32(s: &str) -> u32 {
let mut hasher = DefaultHasher::new();
s.hash(&mut hasher);
// Convert the 64-bit hash to a 32-bit number.
@@ -68,7 +68,7 @@ pub(crate) fn convert_type_to_body_part(slot: i32) -> ItemType {
}
pub(crate) async fn handle_char_list_req(
stream: &mut TcpStream,
stream: Arc<Mutex<TcpStream>>,
packet: Packet,
character_client: Arc<Mutex<CharacterClient>>,
connection_service: Arc<ConnectionService>,
@@ -91,6 +91,8 @@ pub(crate) async fn handle_char_list_req(
let character_list = character_client.get_character_list(&user_id).await?;
let mut characters = vec![];
let mut character_id_list: Vec<u32> = Vec::new();
// Build the visible inventory
for character in character_list.characters {
let mut item_list: [EquippedItem; (MAX_VISIBLE_ITEMS as usize)] =
core::array::from_fn(|i| EquippedItem::default());
@@ -128,13 +130,14 @@ pub(crate) async fn handle_char_list_req(
let data = SrvCharListReply { characters };
let response_packet = Packet::new(PacketType::PakccCharListReply, &data)?;
send_packet(stream, &response_packet).await?;
let mut locked_stream = stream.lock().await;
send_packet(&mut locked_stream, &response_packet).await?;
Ok(())
}
pub(crate) async fn handle_create_char_req(
stream: &mut TcpStream,
stream: Arc<Mutex<TcpStream>>,
packet: Packet,
character_client: Arc<Mutex<CharacterClient>>,
connection_service: Arc<ConnectionService>,
@@ -176,13 +179,14 @@ pub(crate) async fn handle_create_char_req(
let data = SrvCreateCharReply { result, platininum: 0 };
let response_packet = Packet::new(PacketType::PakccCreateCharReply, &data)?;
send_packet(stream, &response_packet).await?;
let mut locked_stream = stream.lock().await;
send_packet(&mut locked_stream, &response_packet).await?;
Ok(())
}
pub(crate) async fn handle_delete_char_req(
stream: &mut TcpStream,
stream: Arc<Mutex<TcpStream>>,
packet: Packet,
character_client: Arc<Mutex<CharacterClient>>,
connection_service: Arc<ConnectionService>,
@@ -218,13 +222,14 @@ pub(crate) async fn handle_delete_char_req(
name: character_name,
};
let response_packet = Packet::new(PacketType::PakccDeleteCharReply, &data)?;
send_packet(stream, &response_packet).await?;
let mut locked_stream = stream.lock().await;
send_packet(&mut locked_stream, &response_packet).await?;
Ok(())
}
pub(crate) async fn handle_select_char_req(
stream: &mut TcpStream,
stream: Arc<Mutex<TcpStream>>,
packet: Packet,
character_client: Arc<Mutex<CharacterClient>>,
connection_service: Arc<ConnectionService>,
@@ -255,7 +260,10 @@ pub(crate) async fn handle_select_char_req(
ip: NullTerminatedString("".to_string()),
};
let response_packet = Packet::new(PacketType::PakccSwitchServer, &data)?;
send_packet(stream, &response_packet).await?;
{
let mut locked_stream = stream.lock().await;
send_packet(&mut locked_stream, &response_packet).await?;
}
let mut character_client = character_client.lock().await;
let character_data = character_client
@@ -275,12 +283,14 @@ pub(crate) async fn handle_select_char_req(
core::array::from_fn(|i| EquippedItem::default());
let mut inventory: [srv_inventory_data::Item; (MAX_ITEMS as usize)] =
core::array::from_fn(|i| srv_inventory_data::Item::default());
// Build the character learned skill list
let mut skill_list: [u16; (MAX_SKILL_COUNT as usize)] = [0u16; MAX_SKILL_COUNT as usize];
for index in 0..skills.len() {
skill_list[index] = skills[index].id as u16;
}
// Build the character inventory list
for item in items {
if item.slot < MAX_VISIBLE_ITEMS as i32 {
let slot = convert_type_to_body_part(item.slot) as isize - 2;
@@ -318,8 +328,8 @@ pub(crate) async fn handle_select_char_req(
let data = SrvSelectCharReply {
race: looks.race as u8,
map: position.map_id as u16,
x: position.x * 100.0,
y: position.y * 100.0,
x: position.x,
y: position.y,
spawn: position.spawn_id as u16,
body_face: looks.face as u32,
body_hair: looks.hair as u32,
@@ -362,7 +372,10 @@ pub(crate) async fn handle_select_char_req(
name,
};
let response_packet = Packet::new(PacketType::PakwcSelectCharReply, &data)?;
send_packet(stream, &response_packet).await?;
{
let mut locked_stream = stream.lock().await;
send_packet(&mut locked_stream, &response_packet).await?;
}
// here we build the inventory
let data = SrvInventoryData {
@@ -370,7 +383,10 @@ pub(crate) async fn handle_select_char_req(
items: inventory,
};
let response_packet = Packet::new(PacketType::PakwcInventoryData, &data)?;
send_packet(stream, &response_packet).await?;
{
let mut locked_stream = stream.lock().await;
send_packet(&mut locked_stream, &response_packet).await?;
}
// Now we need to build the Quest data
let mut quests: [srv_quest_data::Quest; (MAX_QUESTS as usize)] =
@@ -388,15 +404,21 @@ pub(crate) async fn handle_select_char_req(
wishlist,
};
let response_packet = Packet::new(PacketType::PakwcQuestData, &data)?;
send_packet(stream, &response_packet).await?;
// Send the billing message (we don't actually use this so we just send the defaults to allow)
{
let mut locked_stream = stream.lock().await;
send_packet(&mut locked_stream, &response_packet).await?;
}
// Send the billing message (we don't use this, so we just send the defaults to allow)
let data = SrvBillingMessage {
function_type: 0x1001,
pay_flag: 2,
};
let response_packet = Packet::new(PacketType::PakwcBillingMessage, &data)?;
send_packet(stream, &response_packet).await?;
{
let mut locked_stream = stream.lock().await;
send_packet(&mut locked_stream, &response_packet).await?;
}
Ok(())
}