diff --git a/src/ship/location.rs b/src/ship/location.rs index 586bd14..ddf35d4 100644 --- a/src/ship/location.rs +++ b/src/ship/location.rs @@ -1,6 +1,7 @@ use crate::common::serverstate::ClientId; // TODO: room passwords? +// TODO: remove clients from areas (or upon insert, remove that id from anywhere else) #[derive(Copy, Clone)] pub struct ClientArea { @@ -52,12 +53,12 @@ pub struct ClientLocation { } -enum JoinRoomError { +pub enum JoinRoomError { RoomDoesNotExist, RoomFull, } -enum JoinLobbyError { +pub enum JoinLobbyError { LobbyDoesNotExist, LobbyFull, } diff --git a/src/ship/ship.rs b/src/ship/ship.rs index a26819e..ada5bc3 100644 --- a/src/ship/ship.rs +++ b/src/ship/ship.rs @@ -4,6 +4,7 @@ use rand::Rng; use libpso::packet::ship::*; use libpso::packet::login::{Login, LoginResponse, AccountStatus, Session}; +use libpso::packet::messages::*; use libpso::{PacketParseError, PSOPacket}; use libpso::crypto::bb::PSOBBCipher; use libpso::character::character; @@ -32,6 +33,7 @@ pub enum RecvShipPacket { Login(Login), MenuSelect(MenuSelect), CharData(CharData), + Message(Message), } impl RecvServerPacket for RecvShipPacket { @@ -40,6 +42,7 @@ impl RecvServerPacket for RecvShipPacket { 0x93 => Ok(RecvShipPacket::Login(Login::from_bytes(data)?)), 0x10 => Ok(RecvShipPacket::MenuSelect(MenuSelect::from_bytes(data)?)), 0x61 => Ok(RecvShipPacket::CharData(CharData::from_bytes(data)?)), + 0x60 => Ok(RecvShipPacket::Message(Message::from_bytes(data)?)), _ => Err(PacketParseError::WrongPacketForServerType(u16::from_le_bytes([data[2], data[3]]), data.to_vec())) } } @@ -53,6 +56,7 @@ pub enum SendShipPacket { FullCharacter(FullCharacter), CharDataRequest(CharDataRequest), JoinLobby(JoinLobby), + Message(Message), } impl SendServerPacket for SendShipPacket { @@ -64,6 +68,7 @@ impl SendServerPacket for SendShipPacket { SendShipPacket::FullCharacter(pkt) => pkt.as_bytes(), SendShipPacket::CharDataRequest(pkt) => pkt.as_bytes(), SendShipPacket::JoinLobby(pkt) => pkt.as_bytes(), + SendShipPacket::Message(pkt) => pkt.as_bytes(), } } } @@ -176,12 +181,19 @@ impl ShipServerState { } ] }; - + self.client_location.add_to_lobby(id, 0); Ok(vec![ SendShipPacket::JoinLobby(joinlobby) ]) } + + fn message(&mut self, id: ClientId, msg: &Message) -> Box> { + let cmsg = msg.clone(); + Box::new(self.client_location.get_client_neighbors(id).into_iter().map(move |client| { + (client, SendShipPacket::Message(cmsg.clone())) + })) + } } @@ -218,6 +230,9 @@ impl ServerState for ShipServerState { }, RecvShipPacket::CharData(chardata) => { Box::new(self.send_player_to_lobby(id, chardata)?.into_iter().map(move |pkt| (id, pkt))) + }, + RecvShipPacket::Message(msg) => { + self.message(id, msg) } }) }