Browse Source

added change_lobby

pbs
mht8355 5 years ago
parent
commit
d206814394
  1. 2
      src/main.rs
  2. 12
      src/ship/packet/builder/lobby.rs
  3. 34
      src/ship/packet/handler/lobby.rs
  4. 7
      src/ship/ship.rs

2
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)

12
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<LeaveLobby, ShipError> {
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,
})
}

34
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<Vec<(ClientId, SendShipPacket)>, 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())
}

7
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<EG: EntityGateway> ServerState for ShipServerState<EG> {
},
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())
}
})
}

Loading…
Cancel
Save