Browse Source

fix room creation, remove char from lobby on creation

pbs
jake 5 years ago
parent
commit
5f2cb01bd6
  1. 4
      src/ship/location.rs
  2. 16
      src/ship/ship.rs

4
src/ship/location.rs

@ -234,16 +234,16 @@ impl ClientLocation {
} }
pub fn new_room(&mut self, id: ClientId) -> Result<RoomId, CreateRoomError> { pub fn new_room(&mut self, id: ClientId) -> Result<RoomId, CreateRoomError> {
self.err_if_client_is_in_area(id, CreateRoomError::ClientInAreaAlready)?;
let (room_id, empty_room) = self.rooms.iter_mut() let (room_id, empty_room) = self.rooms.iter_mut()
.enumerate() .enumerate()
.filter(|(_, k)| k.is_some())
.filter(|(_, k)| k.is_none())
.nth(0) .nth(0)
.ok_or(CreateRoomError::NoOpenSlots)?; .ok_or(CreateRoomError::NoOpenSlots)?;
let mut new_room = Room::new(); let mut new_room = Room::new();
new_room.add(id); new_room.add(id);
*empty_room = Some(Arc::new(RwLock::new(new_room))); *empty_room = Some(Arc::new(RwLock::new(new_room)));
self.remove_from_location(id);
Ok(RoomId(room_id)) Ok(RoomId(room_id))
} }

16
src/ship/ship.rs

@ -341,9 +341,17 @@ impl<EG: EntityGateway> ShipServerState<EG> {
} }
fn create_room(&mut self, id: ClientId, create_room: &CreateRoom) -> Box<dyn Iterator<Item = (ClientId, SendShipPacket)>> { fn create_room(&mut self, id: ClientId, create_room: &CreateRoom) -> Box<dyn Iterator<Item = (ClientId, SendShipPacket)>> {
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) { let room_id = match self.client_location.new_room(id) {
Ok(room_id) => 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(); let room = room::RoomState::from_create_room(create_room).unwrap();
@ -377,8 +385,10 @@ impl<EG: EntityGateway> ShipServerState<EG> {
unknown: 0, 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)))
})))
} }
} }

Loading…
Cancel
Save