From f11ee7f53f42fad4eeba3692e9415706fb1337d6 Mon Sep 17 00:00:00 2001 From: Andy Newjack Date: Tue, 17 Mar 2020 08:02:17 -0300 Subject: [PATCH] wip. initial room list stuff --- src/ship/room.rs | 18 ++++++++++++++---- src/ship/ship.rs | 41 +++++++++++++++++++++++++++++++++-------- 2 files changed, 47 insertions(+), 12 deletions(-) diff --git a/src/ship/room.rs b/src/ship/room.rs index 825aec6..aa43382 100644 --- a/src/ship/room.rs +++ b/src/ship/room.rs @@ -97,7 +97,7 @@ pub enum RoomMode { impl RoomMode { - fn difficulty(&self) -> Difficulty { + pub fn difficulty(&self) -> Difficulty { match self { RoomMode::Single {difficulty, ..} => *difficulty, RoomMode::Multi {difficulty, ..} => *difficulty, @@ -106,7 +106,7 @@ impl RoomMode { } } - fn episode(&self) -> Episode { + pub fn episode(&self) -> Episode { match self { RoomMode::Single {episode, ..} => *episode, RoomMode::Multi {episode, ..} => *episode, @@ -114,19 +114,28 @@ impl RoomMode { RoomMode::Challenge {episode, ..} => *episode, } } + + pub fn get_flags(&self) -> u8 { + match self { + RoomMode::Battle {..} => 0x10, + RoomMode::Challenge {..} => 0x20, + _ => 0x0, + } + } } #[derive(Debug)] pub struct RoomState { - mode: RoomMode, + pub mode: RoomMode, pub name: String, - password: [u16; 16], + pub password: [u16; 16], //pub maps: [u32; 0x20], pub maps: Maps, // drop_table // items on ground // enemy info + pub players: u8, } impl RoomState { @@ -172,6 +181,7 @@ impl RoomState { password: create_room.password, //maps: [0; 0x20], maps: Maps::new(ep), + players: 1, }) } diff --git a/src/ship/ship.rs b/src/ship/ship.rs index ced4ba0..fa09c3b 100644 --- a/src/ship/ship.rs +++ b/src/ship/ship.rs @@ -46,6 +46,7 @@ pub enum RecvShipPacket { UpdateConfig(UpdateConfig), ViewInfoboardRequest(ViewInfoboardRequest), WriteInfoboard(WriteInfoboard), + RoomListRequest(RoomListRequest), } impl RecvServerPacket for RecvShipPacket { @@ -62,6 +63,7 @@ impl RecvServerPacket for RecvShipPacket { 0x7ED => Ok(RecvShipPacket::UpdateConfig(UpdateConfig::from_bytes(data)?)), 0xD8 => Ok(RecvShipPacket::ViewInfoboardRequest(ViewInfoboardRequest::from_bytes(data)?)), 0xD9 => Ok(RecvShipPacket::WriteInfoboard(WriteInfoboard::from_bytes(data)?)), + 0x08 => Ok(RecvShipPacket::RoomListRequest(RoomListRequest::from_bytes(data)?)), _ => Err(PacketParseError::WrongPacketForServerType(u16::from_le_bytes([data[2], data[3]]), data.to_vec())) } } @@ -86,6 +88,7 @@ pub enum SendShipPacket { LeaveRoom(LeaveRoom), RoomNameResponse(RoomNameResponse), ViewInfoboardResponse(ViewInfoboardResponse), + RoomListResponse(RoomListResponse), } impl SendServerPacket for SendShipPacket { @@ -108,11 +111,11 @@ impl SendServerPacket for SendShipPacket { SendShipPacket::LeaveRoom(pkt) => pkt.as_bytes(), SendShipPacket::RoomNameResponse(pkt) => pkt.as_bytes(), SendShipPacket::ViewInfoboardResponse(pkt) => pkt.as_bytes(), + SendShipPacket::RoomListResponse(pkt) => pkt.as_bytes(), } } } - struct ClientState { user: UserAccount, settings: UserSettings, @@ -137,12 +140,6 @@ impl ClientState { } } - - - - - - pub struct ShipServerState { entity_gateway: EG, clients: HashMap, @@ -439,8 +436,32 @@ impl ShipServerState { self.entity_gateway.set_character(&client.character); Box::new(None.into_iter()) } -} + fn request_room_list(&mut self, id: ClientId, request_roomlist: &RoomListRequest) -> Box + Send> { + let active_room_list = self.rooms.iter() + .enumerate() + .filter_map(|(i, r)| { + r.as_ref().map(|room| { + RoomList { + menu_id: 1, + item_id: i as u32, + difficulty: 0x22,//room.mode.difficulty().into(), + players: room.players, + name: libpso::utf8_to_utf16_array!(room.name, 16), + episode: 0x40,//room.mode.episode().into(), + flags: room.mode.get_flags(), + } + }) + }); + Box::new(vec![(id, SendShipPacket::RoomListResponse(RoomListResponse { + menu_id: 1, + title_id: 1, + zero: 0, + menu_title: libpso::utf8_to_utf16_array!("base_room", 17), + room_list: active_room_list.collect() + }))].into_iter()) + } +} impl ServerState for ShipServerState { type SendPacket = SendShipPacket; @@ -503,6 +524,10 @@ impl ServerState for ShipServerState { RecvShipPacket::WriteInfoboard(pkt) => { self.write_infoboard(id, pkt) }, + + RecvShipPacket::RoomListRequest(pkt) => { + self.request_room_list(id, pkt) + }, }) }