33 lines
1.3 KiB
Rust
Raw Normal View History

2022-10-19 22:32:30 -06:00
use async_std::sync::{Arc, RwLock};
2020-11-21 23:45:27 -07:00
use libpso::packet::ship::*;
use libpso::packet::login::RedirectClient;
use crate::common::serverstate::ClientId;
use crate::common::interserver::Ship;
2021-06-18 11:53:23 -06:00
use crate::ship::ship::{SendShipPacket, ShipError};
2020-11-21 23:45:27 -07:00
use crate::ship::packet::builder;
2022-10-19 22:32:30 -06:00
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)))]
2020-11-21 23:45:27 -07:00
}
2022-10-18 04:46:21 -06:00
pub fn block_list(id: ClientId, shipname: &str, num_blocks: usize) -> Vec<(ClientId, SendShipPacket)> {
vec![(id, SendShipPacket::ShipBlockList(ShipBlockList::new(shipname, num_blocks)))]
2020-11-21 23:45:27 -07:00
}
2022-10-19 22:32:30 -06:00
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)))])
2020-11-21 23:45:27 -07:00
}