33 lines
1.3 KiB
Rust
33 lines
1.3 KiB
Rust
use async_std::sync::{Arc, RwLock};
|
|
use libpso::packet::ship::*;
|
|
use libpso::packet::login::RedirectClient;
|
|
use crate::common::serverstate::ClientId;
|
|
use crate::common::interserver::Ship;
|
|
use crate::ship::ship::{SendShipPacket, ShipError};
|
|
use crate::ship::packet::builder;
|
|
|
|
pub async fn ship_list(id: ClientId, ship_list: &Arc<RwLock<Vec<Ship>>>) -> Vec<(ClientId, SendShipPacket)> {
|
|
let ship_list = ship_list
|
|
.read()
|
|
.await
|
|
.clone();
|
|
vec![(id, SendShipPacket::ShipList(builder::ship::ship_list(&ship_list)))]
|
|
}
|
|
|
|
pub fn block_list(id: ClientId, shipname: &str, num_blocks: usize) -> Vec<(ClientId, SendShipPacket)> {
|
|
vec![(id, SendShipPacket::ShipBlockList(ShipBlockList::new(shipname, num_blocks)))]
|
|
}
|
|
|
|
pub async fn selected_ship(id: ClientId, menuselect: MenuSelect, ship_list: &Arc<RwLock<Vec<Ship>>>)
|
|
-> Result<Vec<(ClientId, SendShipPacket)>, ShipError> {
|
|
let (ip, port) = ship_list
|
|
.read()
|
|
.await
|
|
.get(menuselect.item as usize)
|
|
.map(|ship| {
|
|
(u32::from_ne_bytes(ship.ip.octets()), ship.port)
|
|
})
|
|
.ok_or_else(|| ShipError::InvalidShip(menuselect.item as usize))?;
|
|
Ok(vec![(id, SendShipPacket::RedirectClient(RedirectClient::new(ip, port)))])
|
|
}
|