Browse Source

move chat functionality

pbs
jake 5 years ago
parent
commit
90d0af87a4
  1. 52
      src/ship/packet/handler/communication.rs
  2. 1
      src/ship/packet/handler/mod.rs
  3. 49
      src/ship/ship.rs

52
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<ClientId, ClientState>) -> Result<Box<dyn Iterator<Item = (ClientId, SendShipPacket)> + 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<ClientId, ClientState>)
-> Box<dyn Iterator<Item = (ClientId, SendShipPacket)> + 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<EG: EntityGateway>(id: ClientId,
new_infoboard: &WriteInfoboard,
clients: &mut HashMap<ClientId, ClientState>,
entity_gateway: &mut EG)
-> Box<dyn Iterator<Item = (ClientId, SendShipPacket)> + 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())
}

1
src/ship/packet/handler/mod.rs

@ -1,3 +1,4 @@
pub mod auth; pub mod auth;
pub mod communication;
pub mod lobby; pub mod lobby;
pub mod room; pub mod room;

49
src/ship/ship.rs

@ -208,9 +208,6 @@ impl<EG: EntityGateway> ShipServerState<EG> {
]) ])
} }
fn message(&mut self, id: ClientId, msg: &Message) -> Box<dyn Iterator<Item = (ClientId, SendShipPacket)> + Send> { fn message(&mut self, id: ClientId, msg: &Message) -> Box<dyn Iterator<Item = (ClientId, SendShipPacket)> + Send> {
match &msg.msg { match &msg.msg {
GameMessage::RequestExp(killmonster) => { GameMessage::RequestExp(killmonster) => {
@ -232,11 +229,6 @@ impl<EG: EntityGateway> ShipServerState<EG> {
})) }))
} }
/*fn generate_item_drop(&mut self, id: ClientId, monster: MonsterType) -> Option<ActiveItem> {
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<dyn Iterator<Item = (ClientId, SendShipPacket)> + Send> { fn direct_message(&mut self, id: ClientId, msg: &DirectMessage) -> Box<dyn Iterator<Item = (ClientId, SendShipPacket)> + Send> {
let cmsg = msg.clone(); let cmsg = msg.clone();
let client = self.clients.get_mut(&id).unwrap(); let client = self.clients.get_mut(&id).unwrap();
@ -292,45 +284,12 @@ impl<EG: EntityGateway> ShipServerState<EG> {
} }
} }
fn player_chat(&mut self, id: ClientId, msg: &PlayerChat) -> Result<Box<dyn Iterator<Item = (ClientId, SendShipPacket)> + 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<dyn Iterator<Item = (ClientId, SendShipPacket)> + Send> { fn update_config(&mut self, id: ClientId, update_config: &UpdateConfig) -> Box<dyn Iterator<Item = (ClientId, SendShipPacket)> + Send> {
let client = self.clients.get_mut(&id).ok_or(ShipError::ClientNotFound(id)).unwrap(); let client = self.clients.get_mut(&id).ok_or(ShipError::ClientNotFound(id)).unwrap();
client.character.config.update(update_config); client.character.config.update(update_config);
self.entity_gateway.save_character(&client.character); self.entity_gateway.save_character(&client.character);
Box::new(None.into_iter()) Box::new(None.into_iter())
} }
fn request_infoboard(&mut self, id: ClientId, request_infoboard: &ViewInfoboardRequest) -> Box<dyn Iterator<Item = (ClientId, SendShipPacket)> + 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<dyn Iterator<Item = (ClientId, SendShipPacket)> + 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<EG: EntityGateway> ServerState for ShipServerState<EG> { impl<EG: EntityGateway> ServerState for ShipServerState<EG> {
@ -374,9 +333,8 @@ impl<EG: EntityGateway> ServerState for ShipServerState<EG> {
RecvShipPacket::DirectMessage(msg) => { RecvShipPacket::DirectMessage(msg) => {
self.direct_message(id, msg) self.direct_message(id, msg)
}, },
RecvShipPacket::PlayerChat(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) => { RecvShipPacket::CreateRoom(create_room) => {
handler::room::create_room(id, create_room, &mut self.client_location, &mut self.clients, &mut self.rooms) handler::room::create_room(id, create_room, &mut self.client_location, &mut self.clients, &mut self.rooms)
@ -387,12 +345,11 @@ impl<EG: EntityGateway> ServerState for ShipServerState<EG> {
RecvShipPacket::UpdateConfig(pkt) => { RecvShipPacket::UpdateConfig(pkt) => {
self.update_config(id, pkt) self.update_config(id, pkt)
}, },
RecvShipPacket::ViewInfoboardRequest(pkt) => { RecvShipPacket::ViewInfoboardRequest(pkt) => {
self.request_infoboard(id, pkt)
handler::communication::request_infoboard(id, pkt, &self.client_location, &self.clients)
}, },
RecvShipPacket::WriteInfoboard(pkt) => { RecvShipPacket::WriteInfoboard(pkt) => {
self.write_infoboard(id, pkt)
handler::communication::write_infoboard(id, pkt, &mut self.clients, &mut self.entity_gateway)
}, },
RecvShipPacket::RoomListRequest(_req) => { RecvShipPacket::RoomListRequest(_req) => {
handler::room::request_room_list(id, &self.client_location, &self.rooms) handler::room::request_room_list(id, &self.client_location, &self.rooms)

Loading…
Cancel
Save