From d206814394fd1be3b2ae4086c4efea8343402cd4 Mon Sep 17 00:00:00 2001 From: mht8355 Date: Mon, 27 Apr 2020 20:17:35 -0400 Subject: [PATCH] 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()) } }) }