You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
2.4 KiB

use libpso::packet::ship::*;
use networking::serverstate::ClientId;
use crate::{SendShipPacket, Clients};
use location::ClientLocation;
use entity::gateway::EntityGateway;
use futures::future::join_all;
pub async fn player_chat(id: ClientId,
msg: PlayerChat,
client_location: &ClientLocation,
clients: &Clients)
-> Result<Vec<(ClientId, SendShipPacket)>, anyhow::Error> {
let cmsg = clients.with(id, |client| Box::pin(async move {
PlayerChat::new(client.user.id.0, msg.message)
})).await?;
Ok(client_location.get_all_clients_by_client(id).await.unwrap().into_iter()
.map(move |client| {
(client.client, SendShipPacket::PlayerChat(cmsg.clone()))
})
.collect())
}
pub async fn request_infoboard(id: ClientId,
client_location: &ClientLocation,
clients: &Clients)
-> Result<Vec<(ClientId, SendShipPacket)>, anyhow::Error> {
let area_clients = client_location.get_client_neighbors(id).await.unwrap();
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<_>, anyhow::Error>>()?;
Ok(vec![(id, SendShipPacket::ViewInfoboardResponse(ViewInfoboardResponse {response: infoboards}))])
}
pub async fn write_infoboard<EG>(id: ClientId,
new_infoboard: WriteInfoboard,
clients: &Clients,
entity_gateway: &mut EG)
-> Result<Vec<(ClientId, SendShipPacket)>, anyhow::Error>
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())
}