From 906a2dcd16b9a41d4dd80df37b4b8358bf398b65 Mon Sep 17 00:00:00 2001 From: Andy Newjack Date: Mon, 27 Apr 2020 21:59:15 -0300 Subject: [PATCH 1/7] add room password handling --- src/ship/location.rs | 2 -- src/ship/ship.rs | 21 ++++++++++++++++++++- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/ship/location.rs b/src/ship/location.rs index bc15201..08af31f 100644 --- a/src/ship/location.rs +++ b/src/ship/location.rs @@ -159,7 +159,6 @@ impl ClientLocation { Ok(()) } - pub fn add_client_to_next_available_lobby(&mut self, id: ClientId, lobby: LobbyId) -> Result { let l = (0..15) .map(|lobby_index| { @@ -207,7 +206,6 @@ impl ClientLocation { Ok(()) } - pub fn get_all_clients_by_client(&self, id: ClientId) -> Result, GetNeighborError> { let area = self.client_location.get(&id).ok_or(GetNeighborError::InvalidClient)?; match area { diff --git a/src/ship/ship.rs b/src/ship/ship.rs index 10d2db5..3cf03d2 100644 --- a/src/ship/ship.rs +++ b/src/ship/ship.rs @@ -51,6 +51,7 @@ pub enum ShipError { pub enum RecvShipPacket { Login(Login), MenuSelect(MenuSelect), + RoomPasswordReq(RoomPasswordReq), CharData(CharData), Message(Message), DirectMessage(DirectMessage), @@ -70,7 +71,13 @@ impl RecvServerPacket for RecvShipPacket { fn from_bytes(data: &[u8]) -> Result { match u16::from_le_bytes([data[2], data[3]]) { 0x93 => Ok(RecvShipPacket::Login(Login::from_bytes(data)?)), - 0x10 => Ok(RecvShipPacket::MenuSelect(MenuSelect::from_bytes(data)?)), + 0x10 => { + match data[0] { + 16 => Ok(RecvShipPacket::MenuSelect(MenuSelect::from_bytes(data)?)), + 48 => Ok(RecvShipPacket::RoomPasswordReq(RoomPasswordReq::from_bytes(data)?)), + _ => Err(PacketParseError::WrongPacketForServerType(u16::from_le_bytes([data[2], data[3]]), data.to_vec())), + } + }, 0x61 => Ok(RecvShipPacket::CharData(CharData::from_bytes(data)?)), 0x60 => Ok(RecvShipPacket::Message(Message::from_bytes(data)?)), 0x62 => Ok(RecvShipPacket::DirectMessage(DirectMessage::from_bytes(data)?)), @@ -261,6 +268,18 @@ impl ServerState for ShipServerState { _ => unreachable!(), } }, + RecvShipPacket::RoomPasswordReq(room_password_req) => { + if room_password_req.password == self.rooms[room_password_req.item as usize].as_ref().unwrap().password { + let menuselect = MenuSelect { + menu: room_password_req.menu, + item: room_password_req.item, + }; + handler::room::join_room(id, &menuselect, &mut self.client_location, &mut self.clients, &self.level_table, &mut self.rooms)? + } + else { + Box::new(vec![(id, SendShipPacket::SmallDialog(SmallDialog::new("Incorrect password".into())))].into_iter()) + } + }, RecvShipPacket::CharData(chardata) => { Box::new(handler::lobby::send_player_to_lobby(id, chardata, &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/7] 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 a83aa2d9df0c88607978b99e65c15c6224967910 Mon Sep 17 00:00:00 2001 From: Andy Newjack Date: Mon, 27 Apr 2020 22:57:08 -0300 Subject: [PATCH 3/7] and i oop braces --- src/ship/ship.rs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/ship/ship.rs b/src/ship/ship.rs index 3cf03d2..750af72 100644 --- a/src/ship/ship.rs +++ b/src/ship/ship.rs @@ -71,12 +71,10 @@ impl RecvServerPacket for RecvShipPacket { fn from_bytes(data: &[u8]) -> Result { match u16::from_le_bytes([data[2], data[3]]) { 0x93 => Ok(RecvShipPacket::Login(Login::from_bytes(data)?)), - 0x10 => { - match data[0] { - 16 => Ok(RecvShipPacket::MenuSelect(MenuSelect::from_bytes(data)?)), - 48 => Ok(RecvShipPacket::RoomPasswordReq(RoomPasswordReq::from_bytes(data)?)), - _ => Err(PacketParseError::WrongPacketForServerType(u16::from_le_bytes([data[2], data[3]]), data.to_vec())), - } + 0x10 => match data[0] { + 16 => Ok(RecvShipPacket::MenuSelect(MenuSelect::from_bytes(data)?)), + 48 => Ok(RecvShipPacket::RoomPasswordReq(RoomPasswordReq::from_bytes(data)?)), + _ => Err(PacketParseError::WrongPacketForServerType(u16::from_le_bytes([data[2], data[3]]), data.to_vec())), }, 0x61 => Ok(RecvShipPacket::CharData(CharData::from_bytes(data)?)), 0x60 => Ok(RecvShipPacket::Message(Message::from_bytes(data)?)), From a24381bc66108da197d6b04f30b1319cf96bd7fe Mon Sep 17 00:00:00 2001 From: mht8355 Date: Mon, 27 Apr 2020 22:58:43 -0400 Subject: [PATCH 4/7] 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 5/7] 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 6/7] 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)?; From 36ee49d28e5b144bed516a60bbdd4d33d8b1fcd4 Mon Sep 17 00:00:00 2001 From: Andy Newjack Date: Wed, 29 Apr 2020 00:45:48 -0300 Subject: [PATCH 7/7] remove unwrap --- src/ship/ship.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/ship/ship.rs b/src/ship/ship.rs index 750af72..e329a84 100644 --- a/src/ship/ship.rs +++ b/src/ship/ship.rs @@ -267,7 +267,9 @@ impl ServerState for ShipServerState { } }, RecvShipPacket::RoomPasswordReq(room_password_req) => { - if room_password_req.password == self.rooms[room_password_req.item as usize].as_ref().unwrap().password { + if room_password_req.password == self.rooms[room_password_req.item as usize].as_ref() + .ok_or(ShipError::InvalidRoom(room_password_req.item))? + .password { let menuselect = MenuSelect { menu: room_password_req.menu, item: room_password_req.item,