Merge pull request 'clean_empty_room' (#118) from clean_empty_room into master
Reviewed-by: jake <jake@sharnoth.com>
This commit is contained in:
commit
57a44fde16
@ -2,7 +2,7 @@ use std::collections::HashMap;
|
|||||||
use libpso::packet::ship::*;
|
use libpso::packet::ship::*;
|
||||||
use crate::common::serverstate::ClientId;
|
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, Rooms};
|
||||||
use crate::ship::character::{CharacterBytesBuilder, FullCharacterBytesBuilder};
|
use crate::ship::character::{CharacterBytesBuilder, FullCharacterBytesBuilder};
|
||||||
use crate::ship::location::{ClientLocation, LobbyId, RoomId, RoomLobby, MAX_ROOMS, ClientLocationError};
|
use crate::ship::location::{ClientLocation, LobbyId, RoomId, RoomLobby, MAX_ROOMS, ClientLocationError};
|
||||||
use crate::ship::packet;
|
use crate::ship::packet;
|
||||||
@ -60,23 +60,32 @@ pub fn change_lobby(id: ClientId,
|
|||||||
requested_lobby: u32,
|
requested_lobby: u32,
|
||||||
client_location: &mut ClientLocation,
|
client_location: &mut ClientLocation,
|
||||||
clients: &Clients,
|
clients: &Clients,
|
||||||
level_table: &CharacterLevelTable)
|
level_table: &CharacterLevelTable,
|
||||||
|
ship_rooms: &mut Rooms)
|
||||||
-> Result<Vec<(ClientId, SendShipPacket)>, ShipError> {
|
-> Result<Vec<(ClientId, SendShipPacket)>, ShipError> {
|
||||||
let prev_area = client_location.get_area(id).map_err(|err| -> ClientLocationError {err.into()})?;
|
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,
|
match prev_area {
|
||||||
let dialog = SmallDialog::new(String::from("You are already in this Lobby!")); // Send a SmallDialog to prevent client softlock / server crash
|
RoomLobby::Lobby(old_lobby) => {
|
||||||
return Ok(vec![(id, SendShipPacket::SmallDialog(dialog))])
|
if old_lobby.0 == requested_lobby as usize {
|
||||||
|
return Ok(vec![(id, SendShipPacket::SmallDialog(SmallDialog::new("You are already in this Lobby!".into())))])
|
||||||
|
}
|
||||||
|
},
|
||||||
|
RoomLobby::Room(old_room) => {
|
||||||
|
if client_location.get_client_neighbors(id).map_err(|err| -> ClientLocationError {err.into()})?.len() == 0 {
|
||||||
|
ship_rooms[old_room.0] = None;
|
||||||
|
}
|
||||||
|
},
|
||||||
}
|
}
|
||||||
let leave_lobby = packet::builder::lobby::remove_from_lobby(id, client_location)?;
|
let leave_lobby = packet::builder::lobby::remove_from_lobby(id, client_location)?;
|
||||||
let old_neighbors = client_location.get_client_neighbors(id).unwrap();
|
let old_neighbors = client_location.get_client_neighbors(id).unwrap();
|
||||||
let mut lobby = LobbyId(requested_lobby as usize);
|
let mut lobby = LobbyId(requested_lobby as usize);
|
||||||
if let Err(_) = client_location.add_client_to_lobby(id, lobby) {
|
if let Err(_) = client_location.add_client_to_lobby(id, lobby) {
|
||||||
match prev_area {
|
match prev_area {
|
||||||
RoomLobby::Lobby(lobby) => {
|
RoomLobby::Lobby(_lobby) => {
|
||||||
let dialog = SmallDialog::new(String::from("Lobby is full."));
|
let dialog = SmallDialog::new(String::from("Lobby is full."));
|
||||||
return Ok(vec![(id, SendShipPacket::SmallDialog(dialog))])
|
return Ok(vec![(id, SendShipPacket::SmallDialog(dialog))])
|
||||||
}
|
}
|
||||||
RoomLobby::Room(room) => {
|
RoomLobby::Room(_room) => {
|
||||||
lobby = client_location.add_client_to_next_available_lobby(id, lobby).map_err(|_| ShipError::TooManyClients)?;
|
lobby = client_location.add_client_to_next_available_lobby(id, lobby).map_err(|_| ShipError::TooManyClients)?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -325,17 +325,21 @@ impl<EG: EntityGateway> ServerState for ShipServerState<EG> {
|
|||||||
handler::room::done_bursting(id, &self.client_location, &mut self.rooms)
|
handler::room::done_bursting(id, &self.client_location, &mut self.rooms)
|
||||||
},
|
},
|
||||||
RecvShipPacket::LobbySelect(pkt) => {
|
RecvShipPacket::LobbySelect(pkt) => {
|
||||||
Box::new(handler::lobby::change_lobby(id, pkt.lobby, &mut self.client_location, &self.clients, &self.level_table)?.into_iter())
|
Box::new(handler::lobby::change_lobby(id, pkt.lobby, &mut self.client_location, &self.clients, &self.level_table, &mut self.rooms)?.into_iter())
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn on_disconnect(&mut self, id: ClientId) -> Vec<(ClientId, SendShipPacket)> {
|
fn on_disconnect(&mut self, id: ClientId) -> Vec<(ClientId, SendShipPacket)> {
|
||||||
|
// TODO: don't unwrap!
|
||||||
let client = self.client_location.get_local_client(id).unwrap();
|
let client = self.client_location.get_local_client(id).unwrap();
|
||||||
let neighbors = self.client_location.get_client_neighbors(id).unwrap();
|
let neighbors = self.client_location.get_client_neighbors(id).unwrap();
|
||||||
|
|
||||||
let pkt = match self.client_location.get_area(id).unwrap() {
|
let pkt = match self.client_location.get_area(id).unwrap() {
|
||||||
RoomLobby::Room(room) => {
|
RoomLobby::Room(room) => {
|
||||||
|
if neighbors.len() == 0 {
|
||||||
|
self.rooms[room.0] = None;
|
||||||
|
}
|
||||||
let leader = self.client_location.get_room_leader(room).unwrap();
|
let leader = self.client_location.get_room_leader(room).unwrap();
|
||||||
SendShipPacket::LeaveRoom(LeaveRoom::new(client.local_client.id(), leader.local_client.id()))
|
SendShipPacket::LeaveRoom(LeaveRoom::new(client.local_client.id(), leader.local_client.id()))
|
||||||
},
|
},
|
||||||
|
Loading…
x
Reference in New Issue
Block a user