From 90d0af87a40bfbcc7f435a501287ec3031f48fb4 Mon Sep 17 00:00:00 2001 From: jake Date: Tue, 21 Apr 2020 23:40:44 -0600 Subject: [PATCH] move chat functionality --- src/ship/packet/handler/communication.rs | 52 ++++++++++++++++++++++++ src/ship/packet/handler/mod.rs | 1 + src/ship/ship.rs | 49 ++-------------------- 3 files changed, 56 insertions(+), 46 deletions(-) create mode 100644 src/ship/packet/handler/communication.rs diff --git a/src/ship/packet/handler/communication.rs b/src/ship/packet/handler/communication.rs new file mode 100644 index 0000000..17916e4 --- /dev/null +++ b/src/ship/packet/handler/communication.rs @@ -0,0 +1,52 @@ +use std::collections::HashMap; +use libpso::packet::ship::*; +use crate::common::serverstate::ClientId; +use crate::common::leveltable::CharacterLevelTable; +use crate::ship::ship::{SendShipPacket, ShipError, ClientState}; +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 player_chat(id: ClientId, + msg: &PlayerChat, + client_location: &ClientLocation, + clients: &HashMap) -> Result + Send>, ShipError> { + let client = clients.get(&id).ok_or(ShipError::ClientNotFound(id))?; + let cmsg = PlayerChat::new(client.user.id.0, msg.message.clone()); + + Ok(Box::new(client_location.get_all_clients_by_client(id).unwrap().into_iter() + .map(move |client| { + (client.client, SendShipPacket::PlayerChat(cmsg.clone())) + }))) +} + +pub fn request_infoboard(id: ClientId, + request_infoboard: &ViewInfoboardRequest, + client_location: &ClientLocation, + clients: &HashMap) + -> Box + Send> { + let area_clients = client_location.get_client_neighbors(id).unwrap(); + let r = area_clients.iter() + .filter_map(|c| { + clients.get(&c.client) + }) + .map(|client| { + InfoboardResponse { + name: libpso::utf8_to_utf16_array!(client.character.name, 16), + message: client.character.info_board.as_bytes(), + } + }).collect(); + Box::new(vec![(id, SendShipPacket::ViewInfoboardResponse(ViewInfoboardResponse {response: r}))].into_iter()) +} + +pub fn write_infoboard(id: ClientId, + new_infoboard: &WriteInfoboard, + clients: &mut HashMap, + entity_gateway: &mut EG) + -> Box + Send> { + let client = clients.get_mut(&id).ok_or(ShipError::ClientNotFound(id)).unwrap(); + client.character.info_board.update_infoboard(new_infoboard); + entity_gateway.save_character(&client.character); + Box::new(None.into_iter()) +} diff --git a/src/ship/packet/handler/mod.rs b/src/ship/packet/handler/mod.rs index df41e32..0928362 100644 --- a/src/ship/packet/handler/mod.rs +++ b/src/ship/packet/handler/mod.rs @@ -1,3 +1,4 @@ pub mod auth; +pub mod communication; pub mod lobby; pub mod room; diff --git a/src/ship/ship.rs b/src/ship/ship.rs index dc47b89..02f6926 100644 --- a/src/ship/ship.rs +++ b/src/ship/ship.rs @@ -208,9 +208,6 @@ impl ShipServerState { ]) } - - - fn message(&mut self, id: ClientId, msg: &Message) -> Box + Send> { match &msg.msg { GameMessage::RequestExp(killmonster) => { @@ -232,11 +229,6 @@ impl ShipServerState { })) } - /*fn generate_item_drop(&mut self, id: ClientId, monster: MonsterType) -> Option { - let room = self.rooms[self.client_location.get_area_by_user(id).index]; - let item_drop = room.drop_table.get_drop() - }*/ - fn direct_message(&mut self, id: ClientId, msg: &DirectMessage) -> Box + Send> { let cmsg = msg.clone(); let client = self.clients.get_mut(&id).unwrap(); @@ -292,45 +284,12 @@ impl ShipServerState { } } - fn player_chat(&mut self, id: ClientId, msg: &PlayerChat) -> Result + Send>, ShipError> { - let client = self.clients.get_mut(&id).ok_or(ShipError::ClientNotFound(id))?; - let cmsg = PlayerChat::new(client.user.id.0, msg.message.clone()); - - Ok(Box::new(self.client_location.get_all_clients_by_client(id).unwrap().into_iter() - .map(move |client| { - (client.client, SendShipPacket::PlayerChat(cmsg.clone())) - }))) - } - fn update_config(&mut self, id: ClientId, update_config: &UpdateConfig) -> Box + Send> { let client = self.clients.get_mut(&id).ok_or(ShipError::ClientNotFound(id)).unwrap(); client.character.config.update(update_config); self.entity_gateway.save_character(&client.character); Box::new(None.into_iter()) } - - fn request_infoboard(&mut self, id: ClientId, request_infoboard: &ViewInfoboardRequest) -> Box + Send> { - let clients = self.client_location.get_client_neighbors(id).unwrap(); - let r = clients.iter() - .filter_map(|c| { - self.clients.get(&c.client) - }) - .map(|client| { - InfoboardResponse { - name: libpso::utf8_to_utf16_array!(client.character.name, 16), - message: client.character.info_board.as_bytes(), - } - }).collect(); - Box::new(vec![(id, SendShipPacket::ViewInfoboardResponse(ViewInfoboardResponse {response: r}))].into_iter()) - } - - fn write_infoboard(&mut self, id: ClientId, new_infoboard: &WriteInfoboard) -> Box + Send> { - let client = self.clients.get_mut(&id).ok_or(ShipError::ClientNotFound(id)).unwrap(); - client.character.info_board.update_infoboard(new_infoboard); - self.entity_gateway.save_character(&client.character); - Box::new(None.into_iter()) - } - } impl ServerState for ShipServerState { @@ -374,9 +333,8 @@ impl ServerState for ShipServerState { RecvShipPacket::DirectMessage(msg) => { self.direct_message(id, msg) }, - RecvShipPacket::PlayerChat(msg) => { - Box::new(self.player_chat(id, msg)?.into_iter()) + Box::new(handler::communication::player_chat(id, msg, &self.client_location, &self.clients)?.into_iter()) }, RecvShipPacket::CreateRoom(create_room) => { handler::room::create_room(id, create_room, &mut self.client_location, &mut self.clients, &mut self.rooms) @@ -387,12 +345,11 @@ impl ServerState for ShipServerState { RecvShipPacket::UpdateConfig(pkt) => { self.update_config(id, pkt) }, - RecvShipPacket::ViewInfoboardRequest(pkt) => { - self.request_infoboard(id, pkt) + handler::communication::request_infoboard(id, pkt, &self.client_location, &self.clients) }, RecvShipPacket::WriteInfoboard(pkt) => { - self.write_infoboard(id, pkt) + handler::communication::write_infoboard(id, pkt, &mut self.clients, &mut self.entity_gateway) }, RecvShipPacket::RoomListRequest(_req) => { handler::room::request_room_list(id, &self.client_location, &self.rooms)