move chat functionality
This commit is contained in:
parent
f38e3357f4
commit
90d0af87a4
52
src/ship/packet/handler/communication.rs
Normal file
52
src/ship/packet/handler/communication.rs
Normal file
@ -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,3 +1,4 @@
|
||||
pub mod auth;
|
||||
pub mod communication;
|
||||
pub mod lobby;
|
||||
pub mod room;
|
||||
|
@ -208,9 +208,6 @@ impl<EG: EntityGateway> ShipServerState<EG> {
|
||||
])
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
fn message(&mut self, id: ClientId, msg: &Message) -> Box<dyn Iterator<Item = (ClientId, SendShipPacket)> + Send> {
|
||||
match &msg.msg {
|
||||
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> {
|
||||
let cmsg = msg.clone();
|
||||
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> {
|
||||
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<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> {
|
||||
@ -374,9 +333,8 @@ impl<EG: EntityGateway> ServerState for ShipServerState<EG> {
|
||||
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<EG: EntityGateway> ServerState for ShipServerState<EG> {
|
||||
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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user