|
@ -4,9 +4,10 @@ use crate::common::serverstate::ClientId; |
|
|
use crate::common::leveltable::CharacterLevelTable;
|
|
|
use crate::common::leveltable::CharacterLevelTable;
|
|
|
use crate::ship::ship::{SendShipPacket, ShipError, ClientState, Clients};
|
|
|
use crate::ship::ship::{SendShipPacket, ShipError, ClientState, Clients};
|
|
|
use crate::ship::character::{CharacterBytesBuilder, FullCharacterBytesBuilder};
|
|
|
use crate::ship::character::{CharacterBytesBuilder, FullCharacterBytesBuilder};
|
|
|
use crate::ship::location::{ClientLocation, LobbyId, RoomId, RoomLobby, MAX_ROOMS};
|
|
|
|
|
|
|
|
|
use crate::ship::location::{ClientLocation, LobbyId, RoomId, RoomLobby, MAX_ROOMS, ClientLocationError};
|
|
|
use crate::ship::packet;
|
|
|
use crate::ship::packet;
|
|
|
use libpso::character::character;
|
|
|
use libpso::character::character;
|
|
|
|
|
|
use crate::ship::location::ClientLocationError::GetAreaError;
|
|
|
|
|
|
|
|
|
// this function needs a better home
|
|
|
// this function needs a better home
|
|
|
pub fn block_selected(id: ClientId,
|
|
|
pub fn block_selected(id: ClientId,
|
|
@ -35,6 +36,7 @@ pub fn block_selected(id: ClientId, |
|
|
character: fc,
|
|
|
character: fc,
|
|
|
}),
|
|
|
}),
|
|
|
SendShipPacket::CharDataRequest(CharDataRequest {}),
|
|
|
SendShipPacket::CharDataRequest(CharDataRequest {}),
|
|
|
|
|
|
SendShipPacket::LobbyList(LobbyList::new()),
|
|
|
])
|
|
|
])
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
@ -47,10 +49,46 @@ pub fn send_player_to_lobby(id: ClientId, |
|
|
let lobby = client_location.add_client_to_next_available_lobby(id, LobbyId(0)).map_err(|_| ShipError::TooManyClients)?;
|
|
|
let lobby = client_location.add_client_to_next_available_lobby(id, LobbyId(0)).map_err(|_| ShipError::TooManyClients)?;
|
|
|
let join_lobby = packet::builder::lobby::join_lobby(id, lobby, client_location, clients, level_table)?;
|
|
|
let join_lobby = packet::builder::lobby::join_lobby(id, lobby, client_location, clients, level_table)?;
|
|
|
let addto = packet::builder::lobby::add_to_lobby(id, lobby, client_location, clients, level_table)?;
|
|
|
let addto = packet::builder::lobby::add_to_lobby(id, lobby, client_location, clients, level_table)?;
|
|
|
|
|
|
|
|
|
let neighbors = client_location.get_client_neighbors(id).unwrap();
|
|
|
let neighbors = client_location.get_client_neighbors(id).unwrap();
|
|
|
Ok(vec![(id, SendShipPacket::JoinLobby(join_lobby))]
|
|
|
Ok(vec![(id, SendShipPacket::JoinLobby(join_lobby))]
|
|
|
.into_iter()
|
|
|
.into_iter()
|
|
|
.chain(neighbors.into_iter()
|
|
|
.chain(neighbors.into_iter()
|
|
|
.map(|c| (c.client, SendShipPacket::AddToLobby(addto.clone())))).collect())
|
|
|
.map(|c| (c.client, SendShipPacket::AddToLobby(addto.clone())))).collect())
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn change_lobby(id: ClientId,
|
|
|
|
|
|
requested_lobby: u32,
|
|
|
|
|
|
client_location: &mut ClientLocation,
|
|
|
|
|
|
clients: &Clients,
|
|
|
|
|
|
level_table: &CharacterLevelTable)
|
|
|
|
|
|
-> Result<Vec<(ClientId, SendShipPacket)>, ShipError> {
|
|
|
|
|
|
let prev_area = client_location.get_area(id).map_err(|err| -> ClientLocationError {err.into()})?;
|
|
|
|
|
|
if prev_area == RoomLobby::Lobby(LobbyId(requested_lobby as usize)) { // If the client is already in the selected lobby,
|
|
|
|
|
|
let dialog = SmallDialog::new(String::from("You are already in this Lobby!")); // Send a SmallDialog to prevent client softlock / server crash
|
|
|
|
|
|
return Ok(vec![(id, SendShipPacket::SmallDialog(dialog))])
|
|
|
|
|
|
}
|
|
|
|
|
|
let leave_lobby = packet::builder::lobby::remove_from_lobby(id, client_location)?;
|
|
|
|
|
|
let old_neighbors = client_location.get_client_neighbors(id).unwrap();
|
|
|
|
|
|
let mut lobby = LobbyId(requested_lobby as usize);
|
|
|
|
|
|
if let Err(_) = client_location.add_client_to_lobby(id, lobby) {
|
|
|
|
|
|
match prev_area {
|
|
|
|
|
|
RoomLobby::Lobby(lobby) => {
|
|
|
|
|
|
let dialog = SmallDialog::new(String::from("Lobby is full."));
|
|
|
|
|
|
return Ok(vec![(id, SendShipPacket::SmallDialog(dialog))])
|
|
|
|
|
|
}
|
|
|
|
|
|
RoomLobby::Room(room) => {
|
|
|
|
|
|
lobby = client_location.add_client_to_next_available_lobby(id, lobby).map_err(|_| ShipError::TooManyClients)?;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
let join_lobby = packet::builder::lobby::join_lobby(id, lobby, client_location, clients, level_table)?;
|
|
|
|
|
|
let addto = packet::builder::lobby::add_to_lobby(id, lobby, client_location, clients, level_table)?;
|
|
|
|
|
|
let neighbors = client_location.get_client_neighbors(id).unwrap();
|
|
|
|
|
|
Ok(vec![(id, SendShipPacket::JoinLobby(join_lobby))]
|
|
|
|
|
|
.into_iter()
|
|
|
|
|
|
.chain(neighbors.into_iter()
|
|
|
|
|
|
.map(|c| (c.client, SendShipPacket::AddToLobby(addto.clone()))))
|
|
|
|
|
|
.chain(old_neighbors.into_iter()
|
|
|
|
|
|
.map(|c| (c.client, SendShipPacket::LeaveLobby(leave_lobby.clone()))))
|
|
|
|
|
|
.collect())
|
|
|
|
|
|
}
|