diff --git a/src/ship/packet/handler/direct_message.rs b/src/ship/packet/handler/direct_message.rs new file mode 100644 index 0000000..ceb4e05 --- /dev/null +++ b/src/ship/packet/handler/direct_message.rs @@ -0,0 +1,48 @@ +use std::collections::HashMap; +use log::warn; +use libpso::packet::ship::*; +use libpso::packet::messages::*; +use crate::common::serverstate::ClientId; +use crate::common::leveltable::CharacterLevelTable; +use crate::ship::ship::{SendShipPacket, ShipError, ClientState, Rooms}; +use crate::ship::character::{CharacterBytesBuilder, FullCharacterBytesBuilder}; +use crate::ship::location::{ClientLocation, LobbyId, RoomId, RoomLobby, MAX_ROOMS}; +use libpso::character::character; +use crate::entity::gateway::EntityGateway; +use libpso::{utf8_to_array, utf8_to_utf16_array}; + +fn send_to_client(id: ClientId, target: u8, msg: DirectMessage, client_location: &ClientLocation) + -> Box + Send> { + Box::new(client_location.get_all_clients_by_client(id).unwrap().into_iter() + .filter(move |client| client.local_client.id() == target) + .map(move |client| { + (client.client, SendShipPacket::DirectMessage(msg.clone())) + })) +} + + + +pub fn guildcard_send(id: ClientId, + guildcard_send: &GuildcardSend, + target: u32, + client_location: &ClientLocation, + clients: &HashMap) + -> Box + Send> { + let client = clients.get(&id).unwrap(); + let msg = DirectMessage{ + flag: target, + msg: GameMessage::GuildcardRecv(GuildcardRecv { + client: guildcard_send.client, + target: guildcard_send.target, + guildcard: client.user.id.0, + name: utf8_to_utf16_array!(client.character.name, 0x18), + team: [0; 0x10], // TODO: teams not yet implemented + desc: utf8_to_utf16_array!(client.character.guildcard.description, 0x58), + one: 1, + language: 0, // TODO: add language flag to character + section_id: client.character.section_id.into(), + class: client.character.char_class.into(), + }), + }; + send_to_client(id, target as u8, msg, &client_location) +} diff --git a/src/ship/packet/handler/message.rs b/src/ship/packet/handler/message.rs new file mode 100644 index 0000000..a58de7c --- /dev/null +++ b/src/ship/packet/handler/message.rs @@ -0,0 +1,27 @@ +use std::collections::HashMap; +use log::warn; +use libpso::packet::ship::*; +use libpso::packet::messages::*; +use crate::common::serverstate::ClientId; +use crate::common::leveltable::CharacterLevelTable; +use crate::ship::ship::{SendShipPacket, ShipError, ClientState, Rooms}; +use crate::ship::character::{CharacterBytesBuilder, FullCharacterBytesBuilder}; +use crate::ship::location::{ClientLocation, LobbyId, RoomId, RoomLobby, MAX_ROOMS}; +use libpso::character::character; +use crate::entity::gateway::EntityGateway; + +pub fn request_exp(id: ClientId, + request_exp: &RequestExp, + client_location: &ClientLocation, + rooms: &Rooms) + -> Box + Send> { + + match client_location.get_area(id).unwrap() { + RoomLobby::Room(room) => { + let r = rooms[room.0].as_ref().unwrap(); + warn!("killed a {:?}", r.maps.enemy_by_id(request_exp.enemy_id as usize).monster); + }, + _ => {} + }; + Box::new(None.into_iter()) +} diff --git a/src/ship/packet/handler/mod.rs b/src/ship/packet/handler/mod.rs index b9ba696..2021066 100644 --- a/src/ship/packet/handler/mod.rs +++ b/src/ship/packet/handler/mod.rs @@ -1,5 +1,7 @@ pub mod auth; pub mod communication; +pub mod direct_message; pub mod lobby; +pub mod message; pub mod room; pub mod settings; diff --git a/src/ship/ship.rs b/src/ship/ship.rs index 548ef33..7f1d60a 100644 --- a/src/ship/ship.rs +++ b/src/ship/ship.rs @@ -210,73 +210,29 @@ impl ShipServerState { fn message(&mut self, id: ClientId, msg: &Message) -> Box + Send> { match &msg.msg { - GameMessage::RequestExp(killmonster) => { - match self.client_location.get_area(id).unwrap() { - RoomLobby::Room(room) => { - let r = self.rooms[room.0].as_ref().unwrap(); - warn!("killed a {:?}", r.maps.enemy_by_id(killmonster.enemy_id as usize).monster); - }, - _ => {} - } + GameMessage::RequestExp(request_exp) => { + handler::message::request_exp(id, request_exp, &self.client_location, &self.rooms) + }, + _ => { + let cmsg = msg.clone(); + Box::new(self.client_location.get_client_neighbors(id).unwrap().into_iter() + .map(move |client| { + (client.client, SendShipPacket::Message(cmsg.clone())) + })) }, - _ => {}, } - - let cmsg = msg.clone(); - Box::new(self.client_location.get_client_neighbors(id).unwrap().into_iter() - .map(move |client| { - (client.client, SendShipPacket::Message(cmsg.clone())) - })) } fn direct_message(&mut self, id: ClientId, msg: &DirectMessage) -> Box + Send> { - let cmsg = msg.clone(); - let client = self.clients.get_mut(&id).unwrap(); - match &cmsg.msg { + let target = msg.flag; + match &msg.msg { GameMessage::GuildcardSend(guildcard_send) => { - let out_msg = DirectMessage{ - flag: cmsg.flag, - msg: GameMessage::GuildcardRecv(GuildcardRecv { - client: guildcard_send.client, - target: guildcard_send.target, - guildcard: client.user.id.0, - name: utf8_to_utf16_array!(client.character.name, 0x18), - team: [0; 0x10], // TODO: teams not yet implemented - desc: utf8_to_utf16_array!(client.character.guildcard.description, 0x58), - one: 1, - language: 0, // TODO: add language flag to character - section_id: client.character.section_id.into(), - class: client.character.char_class.into(), - }), - }; - let msg_flag = cmsg.flag as u8; - Box::new(self.client_location.get_all_clients_by_client(id).unwrap().into_iter() - .filter(move |client| client.local_client.id() == msg_flag) - .map(move |client| { - (client.client, SendShipPacket::DirectMessage(out_msg.clone())) - })) + handler::direct_message::guildcard_send(id, guildcard_send, target, &self.client_location, &self.clients) }, - /*GameMessage::RequestItem(req_item) => { - let item = self.generate_item_drop(id); - Box::new(vec![(id, SendShipPacket::Message(Message::new(GameMessage::ItemDrop(ItemDrop { - client: req_item.client, - target: req_item.target, - area: req_item.area, - variety: 0, - unknown: 0, - x: req_item.x, - z: req_item.z, - unknown2: 0, - item_bytes: item[0..12].try_into().unwrap(), - item_id: 0, - item_bytes2: item[12..16].try_into().unwrap(), - unknown3: 0, - }))))].into_iter()) - },*/ _ => { - let msg_flag = cmsg.flag as u8; + let cmsg = msg.clone(); Box::new(self.client_location.get_all_clients_by_client(id).unwrap().into_iter() - .filter(move |client| client.local_client.id() == msg_flag) + .filter(move |client| client.local_client.id() == target as u8) .map(move |client| { (client.client, SendShipPacket::DirectMessage(cmsg.clone())) }))