elseware/src/ship/packet/handler/communication.rs

48 lines
2.2 KiB
Rust
Raw Normal View History

2020-04-21 23:40:44 -06:00
use libpso::packet::ship::*;
use crate::common::serverstate::ClientId;
use crate::ship::ship::{SendShipPacket, ShipError, Clients};
use crate::ship::location::{ClientLocation};
2020-04-21 23:40:44 -06:00
use crate::entity::gateway::EntityGateway;
2022-09-18 21:01:32 -06:00
pub async fn player_chat(id: ClientId,
msg: &PlayerChat,
client_location: &ClientLocation,
clients: &Clients) -> Result<Box<dyn Iterator<Item = (ClientId, SendShipPacket)> + Send>, ShipError> {
2020-04-21 23:40:44 -06:00
let client = clients.get(&id).ok_or(ShipError::ClientNotFound(id))?;
let cmsg = PlayerChat::new(client.user.id.0, msg.message.clone());
2022-09-18 21:01:32 -06:00
Ok(Box::new(client_location.get_all_clients_by_client(id).await.unwrap().into_iter()
2020-04-21 23:40:44 -06:00
.map(move |client| {
(client.client, SendShipPacket::PlayerChat(cmsg.clone()))
})))
}
2022-09-18 21:01:32 -06:00
pub async fn request_infoboard(id: ClientId,
client_location: &ClientLocation,
clients: &Clients)
-> Box<dyn Iterator<Item = (ClientId, SendShipPacket)> + Send> {
let area_clients = client_location.get_client_neighbors(id).await.unwrap();
2020-04-21 23:40:44 -06:00
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())
}
2020-06-02 18:51:18 -06:00
pub async fn write_infoboard<EG: EntityGateway>(id: ClientId,
2020-04-21 23:40:44 -06:00
new_infoboard: &WriteInfoboard,
clients: &mut Clients,
mut entity_gateway: EG)
2020-04-21 23:40:44 -06:00
-> 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);
2020-10-26 00:02:48 -06:00
entity_gateway.save_character(&client.character).await.unwrap();
2020-04-21 23:40:44 -06:00
Box::new(None.into_iter())
}