From 7f95f7e7934f95e30572c9f9be8511722dcf85c8 Mon Sep 17 00:00:00 2001 From: Andy Newjack Date: Wed, 18 Mar 2020 01:36:46 -0300 Subject: [PATCH] room list req/resp functions --- src/ship/room.rs | 49 ++++++++++++++++++++++++++++++++++++------------ src/ship/ship.rs | 23 ++++++++++++++--------- 2 files changed, 51 insertions(+), 21 deletions(-) diff --git a/src/ship/room.rs b/src/ship/room.rs index aa43382..e40d0fc 100644 --- a/src/ship/room.rs +++ b/src/ship/room.rs @@ -76,7 +76,7 @@ impl Into for Difficulty { } } -#[derive(Debug)] +#[derive(Debug, Copy, Clone)] pub enum RoomMode { Single { episode: Episode, @@ -114,14 +114,6 @@ impl RoomMode { RoomMode::Challenge {episode, ..} => *episode, } } - - pub fn get_flags(&self) -> u8 { - match self { - RoomMode::Battle {..} => 0x10, - RoomMode::Challenge {..} => 0x20, - _ => 0x0, - } - } } @@ -135,7 +127,10 @@ pub struct RoomState { // drop_table // items on ground // enemy info + pub difficulty: u8, pub players: u8, + pub episode: u8, + pub flags: u8, } impl RoomState { @@ -145,6 +140,34 @@ impl RoomState { } }*/ + pub fn get_flags(mode: &RoomMode, password: [u16; 16]) -> u8 { + let mut flags = 0u8; + + match mode { + RoomMode::Single {..} => {flags += 0x04} + RoomMode::Battle {..} => {flags += 0x10}, + RoomMode::Challenge {..} => {flags += 0x20}, + _ => {flags += 0x40}, + }; + + if password[0] > 0 { + flags += 0x02; + } + flags + } + + pub fn get_episode(mode: &RoomMode) -> u8 { + let mut episode = 0u8; + + episode = mode.episode().into(); + + match mode { + RoomMode::Single {..} => {episode += 0x10}, + _ => {episode += 0x40}, + } + episode + } + pub fn from_create_room(create_room: &libpso::packet::ship::CreateRoom) -> Result { if [create_room.battle, create_room.challenge, create_room.single_player].iter().sum::() > 1 { return Err(RoomCreationError::InvalidMode) @@ -174,14 +197,16 @@ impl RoomState { } }; - let ep = room_mode.episode(); Ok(RoomState { mode: room_mode, name: String::from_utf16_lossy(&create_room.name).trim_matches(char::from(0)).into(), password: create_room.password, //maps: [0; 0x20], - maps: Maps::new(ep), - players: 1, + maps: Maps::new(room_mode.episode()), + difficulty: create_room.difficulty + 0x22, + players: 0, + episode: RoomState::get_episode(&room_mode), + flags: RoomState::get_flags(&room_mode, create_room.password), }) } diff --git a/src/ship/ship.rs b/src/ship/ship.rs index fa09c3b..99313f4 100644 --- a/src/ship/ship.rs +++ b/src/ship/ship.rs @@ -9,6 +9,7 @@ use libpso::packet::messages::*; use libpso::{PacketParseError, PSOPacket}; use libpso::crypto::bb::PSOBBCipher; use libpso::character::character; +use libpso::packet::ship::{ROOM_MENU_ID}; use crate::common::cipherkeys::{ELSEWHERE_PRIVATE_KEY, ELSEWHERE_PARRAY}; use crate::common::serverstate::{SendServerPacket, RecvServerPacket, ServerState, OnConnect, ClientId}; @@ -443,22 +444,26 @@ impl ShipServerState { .filter_map(|(i, r)| { r.as_ref().map(|room| { RoomList { - menu_id: 1, + menu_id: ROOM_MENU_ID, item_id: i as u32, - difficulty: 0x22,//room.mode.difficulty().into(), + difficulty: room.difficulty, players: room.players, name: libpso::utf8_to_utf16_array!(room.name, 16), - episode: 0x40,//room.mode.episode().into(), - flags: room.mode.get_flags(), + episode: room.episode, + flags: room.flags, } }) }); + let baseroom: BaseRoom = BaseRoom { + menu_id: ROOM_MENU_ID, + title_id: ROOM_MENU_ID, + zero: 0x0000, + menu_title: libpso::utf8_to_utf16_array!("Room list menu", 17), + }; + 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() + baseroom, + rooms: active_room_list.collect() }))].into_iter()) } }