Andy Newjack 4 years ago
parent
commit
d64528d679
  1. 2
      src/main.rs
  2. 2
      src/ship/location.rs
  3. 6
      src/ship/packet/builder/lobby.rs
  4. 32
      src/ship/packet/handler/lobby.rs
  5. 21
      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"))).unwrap());
.chain(fern::log_file(format!("elseware-{}.log", chrono::Local::now().format("%Y-%m-%d_%H:%M:%S"))).unwrap());
fern::Dispatch::new()
.chain(stdio)
.chain(fileout)

2
src/ship/location.rs

@ -159,7 +159,6 @@ impl ClientLocation {
Ok(())
}
pub fn add_client_to_next_available_lobby(&mut self, id: ClientId, lobby: LobbyId) -> Result<LobbyId, JoinLobbyError> {
let l = (0..15)
.map(|lobby_index| {
@ -207,7 +206,6 @@ impl ClientLocation {
Ok(())
}
pub fn get_all_clients_by_client(&self, id: ClientId) -> Result<Vec<AreaClient>, GetNeighborError> {
let area = self.client_location.get(&id).ok_or(GetNeighborError::InvalidClient)?;
match area {

6
src/ship/packet/builder/lobby.rs

@ -65,8 +65,10 @@ pub fn add_to_lobby(id: ClientId,
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();
let prev_area_index = client_location.get_local_client(id).map_err(|err| -> ClientLocationError { err.into() })?.local_client.id();
let prev_area_leader_index = client_location.get_area_leader(client_location.get_area(id)
.map_err(|err| -> ClientLocationError { err.into() })?)
.map_err(|err| -> ClientLocationError { err.into() })?.local_client.id();
Ok(LeaveLobby {
client: prev_area_index,
leader: prev_area_leader_index,

32
src/ship/packet/handler/lobby.rs

@ -4,9 +4,10 @@ use crate::common::serverstate::ClientId;
use crate::common::leveltable::CharacterLevelTable;
use crate::ship::ship::{SendShipPacket, ShipError, ClientState, Clients};
use crate::ship::character::{CharacterBytesBuilder, FullCharacterBytesBuilder};
use crate::ship::location::{ClientLocation, LobbyId, RoomId, RoomLobby, MAX_ROOMS};
use crate::ship::location::{ClientLocation, LobbyId, RoomId, RoomLobby, MAX_ROOMS, ClientLocationError};
use crate::ship::packet;
use libpso::character::character;
use crate::ship::location::ClientLocationError::GetAreaError;
// this function needs a better home
pub fn block_selected(id: ClientId,
@ -61,19 +62,24 @@ pub fn change_lobby(id: ClientId,
clients: &Clients,
level_table: &CharacterLevelTable)
-> Result<Vec<(ClientId, SendShipPacket)>, ShipError> {
let leave_lobby = packet::builder::lobby::remove_from_lobby(id, client_location).unwrap();
let prev_area = client_location.get_area(id).map_err(|err| -> ClientLocationError {err.into()})?;
if prev_area == RoomLobby::Lobby(LobbyId(requested_lobby as usize)) { // If the client is already in the selected lobby,
let dialog = SmallDialog::new(String::from("You are already in this Lobby!")); // Send a SmallDialog to prevent client softlock / server crash
return Ok(vec![(id, SendShipPacket::SmallDialog(dialog))])
}
let leave_lobby = packet::builder::lobby::remove_from_lobby(id, client_location)?;
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 mut lobby = LobbyId(requested_lobby as usize);
if let Err(_) = client_location.add_client_to_lobby(id, lobby) {
match prev_area {
RoomLobby::Lobby(lobby) => {
let dialog = SmallDialog::new(String::from("Lobby is full."));
return Ok(vec![(id, SendShipPacket::SmallDialog(dialog))])
}
RoomLobby::Room(room) => {
lobby = client_location.add_client_to_next_available_lobby(id, lobby).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)?;

21
src/ship/ship.rs

@ -51,6 +51,7 @@ pub enum ShipError {
pub enum RecvShipPacket {
Login(Login),
MenuSelect(MenuSelect),
RoomPasswordReq(RoomPasswordReq),
CharData(CharData),
Message(Message),
DirectMessage(DirectMessage),
@ -71,7 +72,11 @@ impl RecvServerPacket for RecvShipPacket {
fn from_bytes(data: &[u8]) -> Result<RecvShipPacket, PacketParseError> {
match u16::from_le_bytes([data[2], data[3]]) {
0x93 => Ok(RecvShipPacket::Login(Login::from_bytes(data)?)),
0x10 => Ok(RecvShipPacket::MenuSelect(MenuSelect::from_bytes(data)?)),
0x10 => match data[0] {
16 => Ok(RecvShipPacket::MenuSelect(MenuSelect::from_bytes(data)?)),
48 => Ok(RecvShipPacket::RoomPasswordReq(RoomPasswordReq::from_bytes(data)?)),
_ => Err(PacketParseError::WrongPacketForServerType(u16::from_le_bytes([data[2], data[3]]), data.to_vec())),
},
0x61 => Ok(RecvShipPacket::CharData(CharData::from_bytes(data)?)),
0x60 => Ok(RecvShipPacket::Message(Message::from_bytes(data)?)),
0x62 => Ok(RecvShipPacket::DirectMessage(DirectMessage::from_bytes(data)?)),
@ -265,6 +270,20 @@ impl<EG: EntityGateway> ServerState for ShipServerState<EG> {
_ => unreachable!(),
}
},
RecvShipPacket::RoomPasswordReq(room_password_req) => {
if room_password_req.password == self.rooms[room_password_req.item as usize].as_ref()
.ok_or(ShipError::InvalidRoom(room_password_req.item))?
.password {
let menuselect = MenuSelect {
menu: room_password_req.menu,
item: room_password_req.item,
};
handler::room::join_room(id, &menuselect, &mut self.client_location, &mut self.clients, &self.level_table, &mut self.rooms)?
}
else {
Box::new(vec![(id, SendShipPacket::SmallDialog(SmallDialog::new("Incorrect password".into())))].into_iter())
}
},
RecvShipPacket::CharData(chardata) => {
Box::new(handler::lobby::send_player_to_lobby(id, chardata, &mut self.client_location, &self.clients, &self.level_table)?.into_iter())
},

Loading…
Cancel
Save