From d206814394fd1be3b2ae4086c4efea8343402cd4 Mon Sep 17 00:00:00 2001 From: mht8355 Date: Mon, 27 Apr 2020 20:17:35 -0400 Subject: [PATCH 1/5] added change_lobby --- src/main.rs | 2 +- src/ship/packet/builder/lobby.rs | 12 +++++++++++ src/ship/packet/handler/lobby.rs | 34 +++++++++++++++++++++++++++++++- src/ship/ship.rs | 7 +++++++ 4 files changed, 53 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index 4e06152..36dea68 100644 --- a/src/main.rs +++ b/src/main.rs @@ -47,7 +47,7 @@ fn setup_logger() { .chain(std::io::stdout()); let fileout = fern::Dispatch::new() .level(log::LevelFilter::Trace) - .chain(fern::log_file(format!("elseware-{}.log", chrono::Local::now().format("%Y-%m-%d_%H:%M:%S"))).unwrap()); + .chain(fern::log_file(format!("elseware-{}.log", chrono::Local::now().format("%Y-%m-%d"))).unwrap()); fern::Dispatch::new() .chain(stdio) .chain(fileout) diff --git a/src/ship/packet/builder/lobby.rs b/src/ship/packet/builder/lobby.rs index c595a6f..8677da1 100644 --- a/src/ship/packet/builder/lobby.rs +++ b/src/ship/packet/builder/lobby.rs @@ -61,3 +61,15 @@ pub fn add_to_lobby(id: ClientId, playerinfo: player_info(0x100, &client, &area_client, level_table), }) } + +pub fn remove_from_lobby(id: ClientId, + client_location: &ClientLocation) + -> Result { + let prev_area_index = client_location.get_local_client(id).unwrap().local_client.id(); + let prev_area_leader_index = client_location.get_area_leader(client_location.get_area(id).unwrap()).unwrap().local_client.id(); + Ok(LeaveLobby { + client: prev_area_index, + leader: prev_area_leader_index, + _padding: 0, + }) +} \ No newline at end of file diff --git a/src/ship/packet/handler/lobby.rs b/src/ship/packet/handler/lobby.rs index 9be8556..9ac951d 100644 --- a/src/ship/packet/handler/lobby.rs +++ b/src/ship/packet/handler/lobby.rs @@ -35,6 +35,7 @@ pub fn block_selected(id: ClientId, character: fc, }), SendShipPacket::CharDataRequest(CharDataRequest {}), + SendShipPacket::LobbyList(LobbyList::new()), ]) } @@ -47,10 +48,41 @@ 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 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())))).collect()) } + +pub fn change_lobby(id: ClientId, + requested_lobby: u32, + client_location: &mut ClientLocation, + clients: &Clients, + level_table: &CharacterLevelTable) + -> Result, ShipError> { + let leave_lobby = packet::builder::lobby::remove_from_lobby(id, client_location).unwrap(); + let old_neighbors = client_location.get_client_neighbors(id).unwrap(); + let lobby = LobbyId(requested_lobby as usize); + match client_location.add_client_to_lobby(id, lobby) { + Ok(lobby) => { + } + Err(err) => { + let dialog = SmallDialog { + padding: [0, 0], + msg: String::from("Lobby is full."), + }; + return Ok(vec![(id, SendShipPacket::SmallDialog(dialog))]) + } + } + 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()) +} diff --git a/src/ship/ship.rs b/src/ship/ship.rs index 10d2db5..8845676 100644 --- a/src/ship/ship.rs +++ b/src/ship/ship.rs @@ -64,6 +64,7 @@ pub enum RecvShipPacket { Like62ButCooler(Like62ButCooler), ClientCharacterData(ClientCharacterData), DoneBursting(DoneBursting), + LobbySelect(LobbySelect), } impl RecvServerPacket for RecvShipPacket { @@ -84,6 +85,7 @@ impl RecvServerPacket for RecvShipPacket { 0x6D => Ok(RecvShipPacket::Like62ButCooler(Like62ButCooler::from_bytes(data)?)), 0x98 => Ok(RecvShipPacket::ClientCharacterData(ClientCharacterData::from_bytes(data)?)), 0x6F => Ok(RecvShipPacket::DoneBursting(DoneBursting::from_bytes(data)?)), + 0x84 => Ok(RecvShipPacket::LobbySelect(LobbySelect::from_bytes(data)?)), _ => Err(PacketParseError::WrongPacketForServerType(u16::from_le_bytes([data[2], data[3]]), data.to_vec())) } } @@ -112,6 +114,7 @@ pub enum SendShipPacket { Like62ButCooler(Like62ButCooler), BurstDone72(BurstDone72), DoneBursting(DoneBursting), + LobbyList(LobbyList), } impl SendServerPacket for SendShipPacket { @@ -138,6 +141,7 @@ impl SendServerPacket for SendShipPacket { SendShipPacket::Like62ButCooler(pkt) => pkt.as_bytes(), SendShipPacket::BurstDone72(pkt) => pkt.as_bytes(), SendShipPacket::DoneBursting(pkt) => pkt.as_bytes(), + SendShipPacket::LobbyList(pkt) => pkt.as_bytes(), } } } @@ -300,6 +304,9 @@ impl ServerState for ShipServerState { }, RecvShipPacket::DoneBursting(_) => { handler::room::done_bursting(id, &self.client_location, &mut self.rooms) + }, + RecvShipPacket::LobbySelect(pkt) => { + Box::new(handler::lobby::change_lobby(id, pkt.lobby, &mut self.client_location, &self.clients, &self.level_table)?.into_iter()) } }) } From 062fed231a7109302578edd1acc0269f2f4cac94 Mon Sep 17 00:00:00 2001 From: mht8355 Date: Mon, 27 Apr 2020 21:22:12 -0400 Subject: [PATCH 2/5] change_lobby added for changing lobbies and leaving a room --- src/main.rs | 2 +- src/ship/packet/handler/lobby.rs | 20 ++++++++++++++------ 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/main.rs b/src/main.rs index 36dea68..4e06152 100644 --- a/src/main.rs +++ b/src/main.rs @@ -47,7 +47,7 @@ fn setup_logger() { .chain(std::io::stdout()); let fileout = fern::Dispatch::new() .level(log::LevelFilter::Trace) - .chain(fern::log_file(format!("elseware-{}.log", chrono::Local::now().format("%Y-%m-%d"))).unwrap()); + .chain(fern::log_file(format!("elseware-{}.log", chrono::Local::now().format("%Y-%m-%d_%H:%M:%S"))).unwrap()); fern::Dispatch::new() .chain(stdio) .chain(fileout) diff --git a/src/ship/packet/handler/lobby.rs b/src/ship/packet/handler/lobby.rs index 9ac951d..8d0cba4 100644 --- a/src/ship/packet/handler/lobby.rs +++ b/src/ship/packet/handler/lobby.rs @@ -61,18 +61,26 @@ pub fn change_lobby(id: ClientId, clients: &Clients, level_table: &CharacterLevelTable) -> Result, ShipError> { + let prev_area = client_location.get_area(id).unwrap(); let leave_lobby = packet::builder::lobby::remove_from_lobby(id, client_location).unwrap(); let old_neighbors = client_location.get_client_neighbors(id).unwrap(); - let lobby = LobbyId(requested_lobby as usize); + let mut lobby = LobbyId(requested_lobby as usize); match client_location.add_client_to_lobby(id, lobby) { Ok(lobby) => { } Err(err) => { - let dialog = SmallDialog { - padding: [0, 0], - msg: String::from("Lobby is full."), - }; - return Ok(vec![(id, SendShipPacket::SmallDialog(dialog))]) + match prev_area { + RoomLobby::Lobby(lobby) => { + let dialog = SmallDialog { + padding: [0, 0], + msg: 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).unwrap(); + } + } } } let join_lobby = packet::builder::lobby::join_lobby(id, lobby, client_location, clients, level_table)?; From a24381bc66108da197d6b04f30b1319cf96bd7fe Mon Sep 17 00:00:00 2001 From: mht8355 Date: Mon, 27 Apr 2020 22:58:43 -0400 Subject: [PATCH 3/5] sends the client a message when selected lobby = current lobby --- src/ship/packet/handler/lobby.rs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/ship/packet/handler/lobby.rs b/src/ship/packet/handler/lobby.rs index 8d0cba4..cc31ba1 100644 --- a/src/ship/packet/handler/lobby.rs +++ b/src/ship/packet/handler/lobby.rs @@ -4,9 +4,10 @@ use crate::common::serverstate::ClientId; use crate::common::leveltable::CharacterLevelTable; use crate::ship::ship::{SendShipPacket, ShipError, ClientState, Clients}; 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 libpso::character::character; +use crate::ship::location::ClientLocationError::GetAreaError; // this function needs a better home pub fn block_selected(id: ClientId, @@ -61,8 +62,12 @@ pub fn change_lobby(id: ClientId, clients: &Clients, level_table: &CharacterLevelTable) -> Result, ShipError> { - let prev_area = client_location.get_area(id).unwrap(); - let leave_lobby = packet::builder::lobby::remove_from_lobby(id, client_location).unwrap(); + 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); match client_location.add_client_to_lobby(id, lobby) { @@ -71,14 +76,11 @@ pub fn change_lobby(id: ClientId, Err(err) => { match prev_area { RoomLobby::Lobby(lobby) => { - let dialog = SmallDialog { - padding: [0, 0], - msg: String::from("Lobby is full."), - }; + 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).unwrap(); + lobby = client_location.add_client_to_next_available_lobby(id, lobby).map_err(|_| ShipError::TooManyClients)?; } } } From 880bfc620a9484c0010c4f0b3ee3e4fc5925a027 Mon Sep 17 00:00:00 2001 From: mht8355 Date: Tue, 28 Apr 2020 01:25:50 -0400 Subject: [PATCH 4/5] added some error handling --- src/ship/packet/builder/lobby.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/ship/packet/builder/lobby.rs b/src/ship/packet/builder/lobby.rs index 8677da1..84f1d14 100644 --- a/src/ship/packet/builder/lobby.rs +++ b/src/ship/packet/builder/lobby.rs @@ -65,8 +65,10 @@ pub fn add_to_lobby(id: ClientId, pub fn remove_from_lobby(id: ClientId, client_location: &ClientLocation) -> Result { - let prev_area_index = client_location.get_local_client(id).unwrap().local_client.id(); - let prev_area_leader_index = client_location.get_area_leader(client_location.get_area(id).unwrap()).unwrap().local_client.id(); + let prev_area_index = client_location.get_local_client(id).map_err(|err| -> ClientLocationError { err.into() })?.local_client.id(); + let prev_area_leader_index = client_location.get_area_leader(client_location.get_area(id) + .map_err(|err| -> ClientLocationError { err.into() })?) + .map_err(|err| -> ClientLocationError { err.into() })?.local_client.id(); Ok(LeaveLobby { client: prev_area_index, leader: prev_area_leader_index, From 7c6cdd30cc78c2e0e561db65a367386074e51efa Mon Sep 17 00:00:00 2001 From: mht8355 Date: Tue, 28 Apr 2020 01:41:32 -0400 Subject: [PATCH 5/5] better code --- src/ship/packet/handler/lobby.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/ship/packet/handler/lobby.rs b/src/ship/packet/handler/lobby.rs index cc31ba1..5192646 100644 --- a/src/ship/packet/handler/lobby.rs +++ b/src/ship/packet/handler/lobby.rs @@ -70,10 +70,7 @@ pub fn change_lobby(id: ClientId, 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); - match client_location.add_client_to_lobby(id, lobby) { - Ok(lobby) => { - } - Err(err) => { + 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.")); @@ -83,7 +80,6 @@ pub fn change_lobby(id: ClientId, 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)?;