2020-04-21 21:47:41 -06:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use libpso::packet::ship::*;
|
|
|
|
use crate::common::serverstate::ClientId;
|
|
|
|
use crate::common::leveltable::CharacterLevelTable;
|
2020-04-22 23:49:49 -06:00
|
|
|
use crate::ship::ship::{SendShipPacket, ShipError, ClientState, Clients};
|
2020-04-21 21:47:41 -06:00
|
|
|
use crate::ship::character::{CharacterBytesBuilder, FullCharacterBytesBuilder};
|
2020-04-27 22:58:43 -04:00
|
|
|
use crate::ship::location::{ClientLocation, LobbyId, RoomId, RoomLobby, MAX_ROOMS, ClientLocationError};
|
2020-04-22 22:59:01 -06:00
|
|
|
use crate::ship::packet;
|
2020-04-21 21:47:41 -06:00
|
|
|
use libpso::character::character;
|
2020-04-27 22:58:43 -04:00
|
|
|
use crate::ship::location::ClientLocationError::GetAreaError;
|
2020-04-21 21:47:41 -06:00
|
|
|
|
2020-04-22 07:30:59 -06:00
|
|
|
// this function needs a better home
|
|
|
|
pub fn block_selected(id: ClientId,
|
|
|
|
pkt: &MenuSelect,
|
2020-04-22 23:49:49 -06:00
|
|
|
clients: &mut Clients,
|
2020-04-22 07:30:59 -06:00
|
|
|
level_table: &CharacterLevelTable)
|
|
|
|
-> Result<Vec<SendShipPacket>, ShipError> {
|
|
|
|
let client = clients.get_mut(&id).ok_or(ShipError::ClientNotFound(id))?;
|
|
|
|
client.block = pkt.item as u32;
|
|
|
|
|
|
|
|
let (level, stats) = level_table.get_stats_from_exp(client.character.char_class, client.character.exp);
|
|
|
|
|
|
|
|
let fc = FullCharacterBytesBuilder::new()
|
|
|
|
.character(&client.character)
|
|
|
|
.stats(&stats)
|
|
|
|
.level(level)
|
|
|
|
.inventory(&client.inventory)
|
|
|
|
.key_config(&client.settings.settings.key_config)
|
|
|
|
.joystick_config(&client.settings.settings.joystick_config)
|
|
|
|
.symbol_chat(&client.settings.settings.symbol_chats)
|
|
|
|
.tech_menu(&client.character.tech_menu.as_bytes())
|
|
|
|
.build();
|
|
|
|
|
|
|
|
Ok(vec![
|
|
|
|
SendShipPacket::FullCharacter(FullCharacter {
|
|
|
|
character: fc,
|
|
|
|
}),
|
|
|
|
SendShipPacket::CharDataRequest(CharDataRequest {}),
|
2020-04-27 20:17:35 -04:00
|
|
|
SendShipPacket::LobbyList(LobbyList::new()),
|
2020-04-22 07:30:59 -06:00
|
|
|
])
|
|
|
|
}
|
2020-04-21 21:47:41 -06:00
|
|
|
|
|
|
|
pub fn send_player_to_lobby(id: ClientId,
|
|
|
|
_pkt: &CharData,
|
|
|
|
client_location: &mut ClientLocation,
|
2020-04-22 23:49:49 -06:00
|
|
|
clients: &Clients,
|
2020-04-21 22:18:57 -06:00
|
|
|
level_table: &CharacterLevelTable)
|
|
|
|
-> Result<Vec<(ClientId, SendShipPacket)>, ShipError> {
|
2020-04-21 21:47:41 -06:00
|
|
|
let lobby = client_location.add_client_to_next_available_lobby(id, LobbyId(0)).map_err(|_| ShipError::TooManyClients)?;
|
2020-04-22 22:59:01 -06:00
|
|
|
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)?;
|
2020-04-21 21:47:41 -06:00
|
|
|
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())))).collect())
|
|
|
|
}
|
2020-04-27 20:17:35 -04:00
|
|
|
|
|
|
|
pub fn change_lobby(id: ClientId,
|
|
|
|
requested_lobby: u32,
|
|
|
|
client_location: &mut ClientLocation,
|
|
|
|
clients: &Clients,
|
|
|
|
level_table: &CharacterLevelTable)
|
|
|
|
-> Result<Vec<(ClientId, SendShipPacket)>, ShipError> {
|
2020-04-27 22:58:43 -04:00
|
|
|
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)?;
|
2020-04-27 20:17:35 -04:00
|
|
|
let old_neighbors = client_location.get_client_neighbors(id).unwrap();
|
2020-04-27 21:22:12 -04:00
|
|
|
let mut lobby = LobbyId(requested_lobby as usize);
|
2020-04-28 01:41:32 -04:00
|
|
|
if let Err(_) = client_location.add_client_to_lobby(id, lobby) {
|
2020-04-27 21:22:12 -04:00
|
|
|
match prev_area {
|
|
|
|
RoomLobby::Lobby(lobby) => {
|
2020-04-27 22:58:43 -04:00
|
|
|
let dialog = SmallDialog::new(String::from("Lobby is full."));
|
2020-04-27 21:22:12 -04:00
|
|
|
return Ok(vec![(id, SendShipPacket::SmallDialog(dialog))])
|
|
|
|
}
|
|
|
|
RoomLobby::Room(room) => {
|
2020-04-27 22:58:43 -04:00
|
|
|
lobby = client_location.add_client_to_next_available_lobby(id, lobby).map_err(|_| ShipError::TooManyClients)?;
|
2020-04-27 21:22:12 -04:00
|
|
|
}
|
|
|
|
}
|
2020-04-27 20:17:35 -04:00
|
|
|
}
|
|
|
|
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())
|
|
|
|
}
|