2020-04-21 23:40:44 -06:00
|
|
|
use libpso::packet::ship::*;
|
|
|
|
use crate::common::serverstate::ClientId;
|
2020-05-02 22:08:37 -03:00
|
|
|
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-10-18 04:46:21 -06:00
|
|
|
use futures::future::join_all;
|
|
|
|
|
2022-09-18 21:01:32 -06:00
|
|
|
pub async fn player_chat(id: ClientId,
|
2022-10-18 04:46:21 -06:00
|
|
|
msg: PlayerChat,
|
2022-09-18 21:01:32 -06:00
|
|
|
client_location: &ClientLocation,
|
2022-10-18 04:46:21 -06:00
|
|
|
clients: &Clients)
|
|
|
|
-> Result<Vec<(ClientId, SendShipPacket)>, ShipError> {
|
|
|
|
let cmsg = clients.with(id, |client| Box::pin(async move {
|
|
|
|
PlayerChat::new(client.user.id.0, msg.message)
|
|
|
|
})).await?;
|
2020-04-21 23:40:44 -06:00
|
|
|
|
2022-10-18 04:46:21 -06:00
|
|
|
Ok(client_location.get_all_clients_by_client(id).await.unwrap().into_iter()
|
|
|
|
.map(move |client| {
|
|
|
|
(client.client, SendShipPacket::PlayerChat(cmsg.clone()).clone())
|
|
|
|
})
|
|
|
|
.collect())
|
2020-04-21 23:40:44 -06:00
|
|
|
}
|
|
|
|
|
2022-09-18 21:01:32 -06:00
|
|
|
pub async fn request_infoboard(id: ClientId,
|
|
|
|
client_location: &ClientLocation,
|
|
|
|
clients: &Clients)
|
2022-10-18 04:46:21 -06:00
|
|
|
-> Result<Vec<(ClientId, SendShipPacket)>, ShipError> {
|
2022-09-18 21:01:32 -06:00
|
|
|
let area_clients = client_location.get_client_neighbors(id).await.unwrap();
|
2022-10-18 04:46:21 -06:00
|
|
|
let infoboards = join_all(
|
|
|
|
area_clients.iter()
|
|
|
|
.map(|client| async {
|
|
|
|
clients.with(client.client, |client| Box::pin(async move {
|
|
|
|
InfoboardResponse {
|
|
|
|
name: libpso::utf8_to_utf16_array!(client.character.name, 16),
|
|
|
|
message: client.character.info_board.as_bytes(),
|
|
|
|
}
|
|
|
|
})).await
|
|
|
|
}))
|
|
|
|
.await
|
|
|
|
.into_iter()
|
|
|
|
.collect::<Result<Vec<_>, ShipError>>()?;
|
|
|
|
Ok(vec![(id, SendShipPacket::ViewInfoboardResponse(ViewInfoboardResponse {response: infoboards}))])
|
2020-04-21 23:40:44 -06:00
|
|
|
}
|
|
|
|
|
2022-10-18 04:46:21 -06:00
|
|
|
pub async fn write_infoboard<EG>(id: ClientId,
|
|
|
|
new_infoboard: WriteInfoboard,
|
|
|
|
clients: &Clients,
|
|
|
|
entity_gateway: &mut EG)
|
|
|
|
-> Result<Vec<(ClientId, SendShipPacket)>, ShipError>
|
|
|
|
where
|
|
|
|
EG: EntityGateway + Clone + 'static,
|
|
|
|
{
|
|
|
|
clients.with_mut(id, |client| {
|
|
|
|
let mut entity_gateway = entity_gateway.clone();
|
|
|
|
Box::pin(async move {
|
|
|
|
client.character.info_board.update_infoboard(&new_infoboard);
|
|
|
|
entity_gateway.save_character(&client.character).await
|
|
|
|
})}).await??;
|
|
|
|
Ok(Vec::new())
|
2020-04-21 23:40:44 -06:00
|
|
|
}
|