- add: handlers for character create/delete/select

This commit is contained in:
2024-12-21 18:30:05 -05:00
parent c40293e354
commit 0bf8fd9bee
2 changed files with 122 additions and 5 deletions

View File

@@ -3,6 +3,7 @@ use crate::packet::{send_packet, Packet, PacketPayload};
use crate::packet_type::PacketType; use crate::packet_type::PacketType;
use crate::packets::cli_char_list_req::CliCharListReq; use crate::packets::cli_char_list_req::CliCharListReq;
use crate::packets::*; use crate::packets::*;
use crate::dataconsts::*;
use std::collections::HashMap; use std::collections::HashMap;
use std::env; use std::env;
use std::error::Error; use std::error::Error;
@@ -11,11 +12,123 @@ use tokio::net::TcpStream;
use tokio::sync::Mutex; use tokio::sync::Mutex;
use tonic::{Code, Status}; use tonic::{Code, Status};
use tracing::{debug, error, info, warn}; use tracing::{debug, error, info, warn};
use utils::service_discovery; use crate::connection_service::ConnectionService;
use crate::packets::cli_create_char_req::CliCreateCharReq;
use crate::packets::cli_delete_char_req::CliDeleteCharReq;
use crate::packets::cli_select_char_req::CliSelectCharReq;
use crate::packets::srv_char_list_reply::SrvCharListReply;
use crate::packets::srv_create_char_reply::SrvCreateCharReply;
use crate::packets::srv_delete_char_reply::SrvDeleteCharReply;
use crate::packets::srv_select_char_reply::SrvSelectCharReply;
pub(crate) async fn handle_char_list_req(stream: &mut TcpStream, packet: Packet) -> Result<(), Box<dyn Error + Send + Sync>> { pub(crate) async fn handle_char_list_req(stream: &mut TcpStream, packet: Packet, connection_service: Arc<ConnectionService>, connection_id: String) -> Result<(), Box<dyn Error + Send + Sync>> {
let request = CliCharListReq::decode(packet.payload.as_slice()); let request = CliCharListReq::decode(packet.payload.as_slice());
debug!("{:?}", request); debug!("{:?}", request);
let mut user_id = 0;
if let Some(mut state) = connection_service.get_connection(&connection_id) {
user_id = state.user_id.expect("Missing user id in connection");
}
// query the database for the character to this user
let data = SrvCharListReply { characters: vec![] };
let response_packet = Packet::new(PacketType::PakccCharListReply, &data)?;
send_packet(stream, &response_packet).await?;
Ok(()) Ok(())
} }
pub(crate) async fn handle_create_char_req(stream: &mut TcpStream, packet: Packet, connection_service: Arc<ConnectionService>, connection_id: String) -> Result<(), Box<dyn Error + Send + Sync>> {
let request = CliCreateCharReq::decode(packet.payload.as_slice())?;
debug!("{:?}", request);
// if let Some(mut state) = connection_service.get_connection(&connection_id) {
// // state.user_id = Some(response.user_id.parse().unwrap());
// // state.session_id = Some(response.session_id);
// }
let data = SrvCreateCharReply { result: srv_create_char_reply::Result::Ok, platininum: 0 };
let response_packet = Packet::new(PacketType::PakccCreateCharReply, &data)?;
send_packet(stream, &response_packet).await?;
Ok(())
}
pub(crate) async fn handle_delete_char_req(stream: &mut TcpStream, packet: Packet, connection_service: Arc<ConnectionService>, connection_id: String) -> Result<(), Box<dyn Error + Send + Sync>> {
let request = CliDeleteCharReq::decode(packet.payload.as_slice())?;
debug!("{:?}", request);
if let Some(mut state) = connection_service.get_connection(&connection_id) {
// state.user_id = Some(response.user_id.parse().unwrap());
// state.session_id = Some(response.session_id);
}
let character_name = request.name;
let data = SrvDeleteCharReply { remaining_time: 0, name: character_name };
let response_packet = Packet::new(PacketType::PakccDeleteCharReply, &data)?;
send_packet(stream, &response_packet).await?;
Ok(())
}
pub(crate) async fn handle_select_char_req(stream: &mut TcpStream, packet: Packet, connection_service: Arc<ConnectionService>, connection_id: String) -> Result<(), Box<dyn Error + Send + Sync>> {
let request = CliSelectCharReq::decode(packet.payload.as_slice())?;
debug!("{:?}", request);
if let Some(mut state) = connection_service.get_connection_mut(&connection_id) {
// state.user_id = Some(response.user_id.parse().unwrap());
// state.session_id = Some(response.session_id);
state.character_id = Some(request.char_id as i8);
}
let data = SrvSelectCharReply {
race: 0,
map: 0,
x: 0.0,
y: 0.0,
spawn: 0,
body_face: 0,
body_hair: 0,
equipped_items: vec![],
stone: 0,
face: 0,
hair: 0,
job: 0,
faction_id: 0,
faction_rank: 0,
fame: 0,
str: 0,
dex: 0,
int: 0,
con: 0,
charm: 0,
sense: 0,
hp: 0,
mp: 0,
xp: 0,
level: 0,
stat_points: 0,
skill_points: 0,
body_size: 0,
head_size: 0,
penalty_xp: 0,
faction_fame: [0u16; 2],
faction_points: [0u16; 10],
guild_id: 0,
guild_contribution: 0,
guild_rank: 0,
pk_flag: 0,
stamina: 0,
effects: vec![],
pat_hp: 0,
pat_cooldown_time: 0,
skills: [0u16; (MAX_SKILL_COUNT as usize)],
hotbar: vec![],
tag: 0,
name: request.name,
};
let response_packet = Packet::new(PacketType::PakwcSelectCharReply, &data)?;
send_packet(stream, &response_packet).await?;
Ok(())
}

View File

@@ -13,16 +13,20 @@ pub async fn route_packet(stream: &mut TcpStream, packet: Packet, auth_client: A
debug!("Routing packet: {:?}", packet); debug!("Routing packet: {:?}", packet);
match packet.packet_type { match packet.packet_type {
PacketType::PakcsAlive => Ok(()), PacketType::PakcsAlive => Ok(()),
// Generic Server Packets
PacketType::PakcsAcceptReq => auth::handle_accept_req(stream, packet).await, PacketType::PakcsAcceptReq => auth::handle_accept_req(stream, packet).await,
PacketType::PakcsJoinServerTokenReq => auth::handle_join_server_req(stream, packet, auth_client, connection_service, connection_id).await, PacketType::PakcsJoinServerTokenReq => auth::handle_join_server_req(stream, packet, auth_client, connection_service, connection_id).await,
// Login Stuff // Login Packets
PacketType::PakcsLoginTokenReq => auth::handle_login_req(stream, packet, auth_client, connection_service, connection_id, stream.peer_addr()?).await, PacketType::PakcsLoginTokenReq => auth::handle_login_req(stream, packet, auth_client, connection_service, connection_id, stream.peer_addr()?).await,
PacketType::PakcsLogoutReq => auth::handle_logout_req(stream, packet, auth_client, connection_service, connection_id).await, PacketType::PakcsLogoutReq => auth::handle_logout_req(stream, packet, auth_client, connection_service, connection_id).await,
PacketType::PakcsSrvSelectReq => auth::handle_server_select_req(stream, packet, connection_service, connection_id).await, PacketType::PakcsSrvSelectReq => auth::handle_server_select_req(stream, packet, connection_service, connection_id).await,
PacketType::PakcsChannelListReq => auth::handle_channel_list_req(stream, packet).await, PacketType::PakcsChannelListReq => auth::handle_channel_list_req(stream, packet).await,
// Character Stuff // Character Packets
PacketType::PakcsCharListReq => character::handle_char_list_req(stream, packet).await, PacketType::PakcsCharListReq => character::handle_char_list_req(stream, packet, connection_service, connection_id).await,
PacketType::PakcsCreateCharReq => character::handle_create_char_req(stream, packet, connection_service, connection_id).await,
PacketType::PakcsDeleteCharReq => character::handle_delete_char_req(stream, packet, connection_service, connection_id).await,
PacketType::PakcsSelectCharReq => character::handle_select_char_req(stream, packet, connection_service, connection_id).await,
// 1 => chat::handle_chat(packet).await?, // 1 => chat::handle_chat(packet).await?,
// 2 => movement::handle_movement(packet).await?, // 2 => movement::handle_movement(packet).await?,