- add: mouse cmd packet handling
- add: change map handler - update: logout handler to send the reply packet - update: character id list change from u8 to u32
This commit is contained in:
88
packet-service/src/handlers/world.rs
Normal file
88
packet-service/src/handlers/world.rs
Normal file
@@ -0,0 +1,88 @@
|
||||
use crate::character_client::CharacterClient;
|
||||
use crate::connection_service::ConnectionService;
|
||||
use crate::packet::{send_packet, Packet, PacketPayload};
|
||||
use crate::packet_type::PacketType;
|
||||
use chrono::{Local, Timelike};
|
||||
use std::error::Error;
|
||||
use std::sync::Arc;
|
||||
use tokio::net::TcpStream;
|
||||
use tokio::sync::Mutex;
|
||||
use tracing::debug;
|
||||
|
||||
fn distance(x1: f64, y1: f64, x2: f64, y2: f64) -> u16 {
|
||||
let dist = ((x2 - x1).powi(2) + (y2 - y1).powi(2)).sqrt();
|
||||
dist.round() as u16
|
||||
}
|
||||
|
||||
pub(crate) async fn handle_change_map_req(stream: &mut TcpStream, packet: Packet, character_client: Arc<Mutex<CharacterClient>>, connection_service: Arc<ConnectionService>, connection_id: String) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
use crate::packets::cli_change_map_req::*;
|
||||
use crate::packets::srv_change_map_reply::*;
|
||||
let request = CliChangeMapReq::decode(packet.payload.as_slice())?;
|
||||
debug!("{:?}", request);
|
||||
|
||||
let mut user_id = 0;
|
||||
let mut char_id = 0;
|
||||
let mut character_id_list: Vec<u32> = Vec::new();
|
||||
let session_id;
|
||||
if let Some(mut state) = connection_service.get_connection(&connection_id) {
|
||||
user_id = state.user_id.expect("Missing user id in connection state");
|
||||
session_id = state.session_id.expect("Missing session id in connection state");
|
||||
char_id = state.character_id.expect("Missing character id in connection state");
|
||||
character_id_list = state.character_list.clone().expect("Missing character id list");
|
||||
}
|
||||
|
||||
let mut character_client = character_client.lock().await;
|
||||
let character_data = character_client.get_character(&user_id.to_string(), character_id_list[char_id as usize] as u8).await?;
|
||||
|
||||
let character = character_data.character.unwrap_or_default();
|
||||
let stats = character.stats.unwrap();
|
||||
|
||||
let now = Local::now();
|
||||
let time_as_u16 = (now.hour() * 100 + now.minute()) as u16;
|
||||
|
||||
let data = SrvChangeMapReply {
|
||||
object_index: character_id_list[char_id as usize] as u16,
|
||||
hp: stats.hp as u16,
|
||||
mp: stats.mp as u16,
|
||||
xp: stats.xp as u16,
|
||||
penalize_xp: stats.penalty_xp as u16,
|
||||
craft_rate: 0,
|
||||
update_time: 0,
|
||||
world_rate: 1,
|
||||
town_rate: 1,
|
||||
item_rate: [0u8; 11],
|
||||
flags: 1,
|
||||
world_time: time_as_u16,
|
||||
team_number: 10,
|
||||
};
|
||||
let response_packet = Packet::new(PacketType::PakwcChangeMapReply, &data)?;
|
||||
send_packet(stream, &response_packet).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) async fn handle_mouse_cmd_req(stream: &mut TcpStream, packet: Packet, connection_service: Arc<ConnectionService>, connection_id: String) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
use crate::packets::cli_mouse_cmd::*;
|
||||
use crate::packets::srv_mouse_cmd::*;
|
||||
let request = CliMouseCmd::decode(packet.payload.as_slice())?;
|
||||
debug!("{:?}", request);
|
||||
|
||||
let mut char_id = 0;
|
||||
let mut character_id_list: Vec<u32> = Vec::new();
|
||||
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");
|
||||
}
|
||||
|
||||
let data = SrvMouseCmd {
|
||||
id: character_id_list[char_id as usize] as u16,
|
||||
target_id: request.target_id,
|
||||
distance: distance(520000 as f64, 520000 as f64, request.x as f64, request.y as f64),
|
||||
x: request.x,
|
||||
y: request.y,
|
||||
z: request.z,
|
||||
};
|
||||
let response_packet = Packet::new(PacketType::PakwcMouseCmd, &data)?;
|
||||
send_packet(stream, &response_packet).await?;
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user