From dacbf1eea384ecddeaa1cb21b89c908ac3730131 Mon Sep 17 00:00:00 2001 From: andy Date: Sun, 23 May 2021 18:18:36 +0000 Subject: [PATCH] add level requirements for rooms and update tests --- src/ship/packet/handler/room.rs | 68 ++++++++++++++++++++++++++++++--- src/ship/room.rs | 5 ++- src/ship/ship.rs | 2 +- tests/test_shops.rs | 6 +-- 4 files changed, 69 insertions(+), 12 deletions(-) diff --git a/src/ship/packet/handler/room.rs b/src/ship/packet/handler/room.rs index c3be9dc..a058f9a 100644 --- a/src/ship/packet/handler/room.rs +++ b/src/ship/packet/handler/room.rs @@ -7,20 +7,47 @@ use crate::ship::location::{ClientLocation, RoomId, RoomLobby, ClientLocationErr use crate::ship::packet::builder; use crate::ship::room; use crate::ship::items::ItemManager; +use std::convert::{TryFrom}; pub fn create_room(id: ClientId, create_room: &CreateRoom, client_location: &mut ClientLocation, clients: &mut Clients, item_manager: &mut ItemManager, + level_table: &CharacterLevelTable, rooms: &mut Rooms) - -> Result + Send>, ShipError> { + -> Result + Send>, anyhow::Error> { + let client = clients.get(&id).ok_or(ShipError::ClientNotFound(id))?; + let level = level_table.get_level_from_exp(client.character.char_class, client.character.exp); + match room::Difficulty::try_from(create_room.difficulty)? { + room::Difficulty::Ultimate => { + if level < 80 { + return Ok(Box::new(vec![(id, SendShipPacket::SmallDialog(SmallDialog::new("You must be at least level 80 \nto create Ultimate rooms.".into())))].into_iter())) + } + }, + room::Difficulty::VeryHard => { + if level < 40 { + return Ok(Box::new(vec![(id, SendShipPacket::SmallDialog(SmallDialog::new("You must be at least level 40 \nto create Very Hard rooms.".into())))].into_iter())) + } + }, + room::Difficulty::Hard => { + if level < 20 { + return Ok(Box::new(vec![(id, SendShipPacket::SmallDialog(SmallDialog::new("You must be at least level 20 \nto create Hard rooms.".into())))].into_iter())) + } + }, + room::Difficulty::Normal => { + if level < 1 { + return Ok(Box::new(vec![(id, SendShipPacket::SmallDialog(SmallDialog::new("You must be at least level 1 \nto create Normal rooms.".into())))].into_iter())) + } + }, + // i can't believe you've done this + _ => {unreachable!()}, + }; + let area = client_location.get_area(id).unwrap(); let area_client = client_location.get_local_client(id).unwrap(); let lobby_neighbors = client_location.get_client_neighbors(id).unwrap(); let room_id = client_location.create_new_room(id).unwrap(); - - let client = clients.get_mut(&id).unwrap();//.ok_or(ShipError::ClientNotFound(id)).unwrap(); let mut room = room::RoomState::from_create_room(create_room, client.character.section_id).unwrap(); room.bursting = true; @@ -63,11 +90,40 @@ pub fn join_room(id: ClientId, level_table: &CharacterLevelTable, rooms: &mut Rooms) -> Result + Send>, ShipError> { + let client = clients.get(&id).ok_or(ShipError::ClientNotFound(id))?; + let level = level_table.get_level_from_exp(client.character.char_class, client.character.exp); + let room = rooms.get(pkt.item as usize) + .ok_or_else(|| ShipError::InvalidRoom(pkt.item))?.as_ref() + .ok_or_else(|| ShipError::InvalidRoom(pkt.item))?; + + match room.mode.difficulty() { + room::Difficulty::Ultimate => { + if level < 80 { + return Ok(Box::new(vec![(id, SendShipPacket::SmallDialog(SmallDialog::new("You must be at least level 80 \nto join Ultimate rooms.".into())))].into_iter())) + } + }, + room::Difficulty::VeryHard => { + if level < 40 { + return Ok(Box::new(vec![(id, SendShipPacket::SmallDialog(SmallDialog::new("You must be at least level 40 \nto join Very Hard rooms.".into())))].into_iter())) + } + }, + room::Difficulty::Hard => { + if level < 20 { + return Ok(Box::new(vec![(id, SendShipPacket::SmallDialog(SmallDialog::new("You must be at least level 20 \nto join Hard rooms.".into())))].into_iter())) + } + }, + room::Difficulty::Normal => { + if level < 1 { + return Ok(Box::new(vec![(id, SendShipPacket::SmallDialog(SmallDialog::new("You must be at least level 1 \nto join Normal rooms.".into())))].into_iter())) + } + }, + }; + let original_area = client_location.get_area(id).unwrap(); let original_neighbors = client_location.get_client_neighbors(id).unwrap(); - let room = rooms.get(pkt.item as usize) - .ok_or(ShipError::InvalidRoom(pkt.item))?.as_ref() - .ok_or(ShipError::InvalidRoom(pkt.item))?; + // let room = rooms.get(pkt.item as usize) + // .ok_or_else(|| ShipError::InvalidRoom(pkt.item))?.as_ref() + // .ok_or_else(|| ShipError::InvalidRoom(pkt.item))?; if room.bursting { return Ok(Box::new(vec![(id, SendShipPacket::SmallDialog(SmallDialog::new("player is bursting\nplease wait".into())))].into_iter())) } diff --git a/src/ship/room.rs b/src/ship/room.rs index 3aae5da..991de54 100644 --- a/src/ship/room.rs +++ b/src/ship/room.rs @@ -7,14 +7,15 @@ use crate::ship::drops::DropTable; use crate::entity::character::SectionID; use crate::ship::monster::{load_monster_stats_table, MonsterType, MonsterStats}; use crate::ship::map::area::MapAreaLookup; +use thiserror::Error; -#[derive(Debug)] +#[derive(Debug, Error)] +#[error("")] pub enum RoomCreationError { InvalidMode, InvalidEpisode(u8), InvalidDifficulty(u8), CouldNotLoadMonsterStats(RoomMode), - } #[derive(Debug, Copy, Clone, derive_more::Display)] diff --git a/src/ship/ship.rs b/src/ship/ship.rs index ae9255d..7e79552 100644 --- a/src/ship/ship.rs +++ b/src/ship/ship.rs @@ -635,7 +635,7 @@ impl ServerState for ShipServerState { }, RecvShipPacket::CreateRoom(create_room) => { let block = self.blocks.with_client(id, &self.clients)?; - handler::room::create_room(id, create_room, &mut block.client_location, &mut self.clients, &mut self.item_manager, &mut block.rooms)? + handler::room::create_room(id, create_room, &mut block.client_location, &mut self.clients, &mut self.item_manager, &self.level_table, &mut block.rooms)? }, RecvShipPacket::RoomNameRequest(_req) => { let block = self.blocks.with_client(id, &self.clients)?; diff --git a/tests/test_shops.rs b/tests/test_shops.rs index eb1098f..5c414f1 100644 --- a/tests/test_shops.rs +++ b/tests/test_shops.rs @@ -323,7 +323,7 @@ async fn test_other_clients_see_purchase() { log_in_char(&mut ship, ClientId(2), "a2", "a").await; join_lobby(&mut ship, ClientId(1)).await; join_lobby(&mut ship, ClientId(2)).await; - create_room_with_difficulty(&mut ship, ClientId(1), "room", "", Difficulty::Ultimate).await; + create_room_with_difficulty(&mut ship, ClientId(1), "room", "", Difficulty::Normal).await; join_room(&mut ship, ClientId(2), 0).await; ship.handle(ClientId(1), &RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::ShopRequest(ShopRequest { @@ -377,7 +377,7 @@ async fn test_other_clients_see_stacked_purchase() { log_in_char(&mut ship, ClientId(2), "a2", "a").await; join_lobby(&mut ship, ClientId(1)).await; join_lobby(&mut ship, ClientId(2)).await; - create_room_with_difficulty(&mut ship, ClientId(1), "room", "", Difficulty::Ultimate).await; + create_room_with_difficulty(&mut ship, ClientId(1), "room", "", Difficulty::Normal).await; join_room(&mut ship, ClientId(2), 0).await; ship.handle(ClientId(1), &RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::ShopRequest(ShopRequest { @@ -414,7 +414,7 @@ async fn test_buying_item_without_enough_mseseta() { .build()); log_in_char(&mut ship, ClientId(1), "a1", "a").await; join_lobby(&mut ship, ClientId(1)).await; - create_room_with_difficulty(&mut ship, ClientId(1), "room", "", Difficulty::Ultimate).await; + create_room_with_difficulty(&mut ship, ClientId(1), "room", "", Difficulty::Normal).await; ship.handle(ClientId(1), &RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::ShopRequest(ShopRequest { client: 255,