From 5f2cb01bd6a4a45b300473dede46b8378541edeb Mon Sep 17 00:00:00 2001 From: jake Date: Sun, 12 Jan 2020 21:54:18 -0800 Subject: [PATCH] fix room creation, remove char from lobby on creation --- src/ship/location.rs | 4 ++-- src/ship/ship.rs | 16 +++++++++++++--- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/ship/location.rs b/src/ship/location.rs index d848b35..c7a0176 100644 --- a/src/ship/location.rs +++ b/src/ship/location.rs @@ -234,16 +234,16 @@ impl ClientLocation { } pub fn new_room(&mut self, id: ClientId) -> Result { - self.err_if_client_is_in_area(id, CreateRoomError::ClientInAreaAlready)?; let (room_id, empty_room) = self.rooms.iter_mut() .enumerate() - .filter(|(_, k)| k.is_some()) + .filter(|(_, k)| k.is_none()) .nth(0) .ok_or(CreateRoomError::NoOpenSlots)?; let mut new_room = Room::new(); new_room.add(id); *empty_room = Some(Arc::new(RwLock::new(new_room))); + self.remove_from_location(id); Ok(RoomId(room_id)) } diff --git a/src/ship/ship.rs b/src/ship/ship.rs index 58386ca..9a8637a 100644 --- a/src/ship/ship.rs +++ b/src/ship/ship.rs @@ -341,9 +341,17 @@ impl ShipServerState { } fn create_room(&mut self, id: ClientId, create_room: &CreateRoom) -> Box> { + let area = self.client_location.get_area_by_user(id); + let area_client = area.clients().into_iter().filter(|client| { + client.client_id == id + }).next().unwrap(); + let other_clients = area.clients().into_iter() + .filter(move |c| { + c.client_id != id + }); let room_id = match self.client_location.new_room(id) { Ok(room_id) => room_id, - Err(e) => return Box::new(vec![(id, SendShipPacket::SmallDialog(SmallDialog::new("could not create room".to_owned())))].into_iter()) + Err(err) => return Box::new(vec![(id, SendShipPacket::SmallDialog(SmallDialog::new(format!("could not create room: {:?}", err))))].into_iter()) }; let room = room::RoomState::from_create_room(create_room).unwrap(); @@ -377,8 +385,10 @@ impl ShipServerState { unknown: 0, }; - Box::new(vec![(id, SendShipPacket::JoinRoom(join_room))].into_iter()) - //self.rooms.insert(room_id, room); + let leader = area.leader(); + Box::new(vec![(id, SendShipPacket::JoinRoom(join_room))].into_iter().chain(other_clients.map(move |c| { + (c.client_id, SendShipPacket::LeaveLobby(LeaveLobby::new(area_client.index as u8, leader.index as u8))) + }))) } }