From ae696888e91afb06232c2c59767ac47c5d0e90e2 Mon Sep 17 00:00:00 2001 From: jake Date: Wed, 25 Nov 2020 22:08:41 -0700 Subject: [PATCH] remove player from lobby before sending to another ship/block --- src/ship/packet/handler/lobby.rs | 24 +++++++++++++++++++----- src/ship/ship.rs | 14 +++++++++++--- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/src/ship/packet/handler/lobby.rs b/src/ship/packet/handler/lobby.rs index 7570591..87d9131 100644 --- a/src/ship/packet/handler/lobby.rs +++ b/src/ship/packet/handler/lobby.rs @@ -15,7 +15,7 @@ pub fn block_selected(id: ClientId, clients: &mut Clients, item_manager: &ItemManager, level_table: &CharacterLevelTable) - -> Result, anyhow::Error> { + -> Result, anyhow::Error> { let client = clients.get_mut(&id).ok_or(ShipError::ClientNotFound(id))?; client.block = pkt.item as usize - 1; @@ -38,11 +38,11 @@ pub fn block_selected(id: ClientId, .build(); Ok(vec![ - SendShipPacket::FullCharacter(Box::new(FullCharacter { + (id, SendShipPacket::FullCharacter(Box::new(FullCharacter { character: fc, - })), - SendShipPacket::CharDataRequest(CharDataRequest {}), - SendShipPacket::LobbyList(LobbyList::new()), + }))), + (id, SendShipPacket::CharDataRequest(CharDataRequest {})), + (id, SendShipPacket::LobbyList(LobbyList::new())), ]) } @@ -113,3 +113,17 @@ pub async fn change_lobby(id: ClientId, .map(|c| (c.client, SendShipPacket::LeaveLobby(leave_lobby.clone())))) .collect()) } + +pub fn remove_from_lobby(id: ClientId, + client_location: &mut ClientLocation) + -> Result, anyhow::Error> { + let area_client = client_location.get_local_client(id)?; + let neighbors = client_location.get_client_neighbors(id)?; + let leader = client_location.get_leader_by_client(id)?; + let leave_lobby_pkt = SendShipPacket::LeaveLobby(LeaveLobby::new(area_client.local_client.id(), leader.local_client.id())); + + client_location.remove_client_from_area(id)?; + Ok(neighbors.into_iter().map(|n| { + (n.client, leave_lobby_pkt.clone()) + }).collect()) +} diff --git a/src/ship/ship.rs b/src/ship/ship.rs index 91ec726..7810e12 100644 --- a/src/ship/ship.rs +++ b/src/ship/ship.rs @@ -571,8 +571,16 @@ impl ServerState for ShipServerState { RecvShipPacket::MenuSelect(menuselect) => { let block = self.blocks.with_client(id, &self.clients)?; match menuselect.menu { - SHIP_MENU_ID => handler::ship::selected_ship(id, menuselect, &self.ship_list)?, - BLOCK_MENU_ID => Box::new(handler::lobby::block_selected(id, menuselect, &mut self.clients, &self.item_manager, &self.level_table)?.into_iter().map(move |pkt| (id, pkt))), + SHIP_MENU_ID => { + let leave_lobby = handler::lobby::remove_from_lobby(id, &mut block.client_location).into_iter().into_iter().flatten(); + let select_ship = handler::ship::selected_ship(id, menuselect, &self.ship_list)?; + Box::new(leave_lobby.chain(select_ship)) + } + BLOCK_MENU_ID => { + let leave_lobby = handler::lobby::remove_from_lobby(id, &mut block.client_location).into_iter().into_iter().flatten(); + let select_block = handler::lobby::block_selected(id, menuselect, &mut self.clients, &self.item_manager, &self.level_table)?.into_iter(); + Box::new(leave_lobby.chain(select_block)) + } ROOM_MENU_ID => handler::room::join_room(id, menuselect, &mut block.client_location, &mut self.clients, &mut self.item_manager, &self.level_table, &mut block.rooms)?, QUEST_CATEGORY_MENU_ID => handler::quest::select_quest_category(id, menuselect, &self.quests)?, _ => unreachable!(), @@ -680,7 +688,7 @@ impl ServerState for ShipServerState { handler::ship::ship_list(id, &self.ship_list) }, RecvShipPacket::RequestShipBlockList(_) => { - handler::ship::block_list(id, &self.name, 1) + handler::ship::block_list(id, &self.name, self.blocks.0.len()) } }) }