From 4dc1a9ff991ac9f7e434392ee6295ede66b40329 Mon Sep 17 00:00:00 2001 From: rakuja Date: Sat, 30 Nov 2024 23:39:15 +0100 Subject: [PATCH 1/4] chore: update libraries, bump MSRV, panic if update_cr_core is used more than once --- Cargo.toml | 7 ++++--- src/db/cr_core_initializer.rs | 6 ++++-- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a1b9091..d3bbd44 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,7 +5,7 @@ authors = ["RakuJa"] # Compiler info edition = "2021" -rust-version = "1.75.0" +rust-version = "1.79.0" description = "Beyond Your Bestiary Explorer (BYBE) is a web service that provides tools to help Pathfinder 2e Game Masters." readme = "README.md" @@ -39,8 +39,8 @@ sqlx = { version = "0.8.2", features = ["runtime-async-std", "sqlite"] } cached = { version = "0.54.0", features = ["async"] } anyhow = "1.0.93" -serde = { version = "1.0.214", features = ["derive"] } -serde_json = "1.0.132" +serde = { version = "1.0.215", features = ["derive"] } +serde_json = "1.0.133" strum = {version="0.26.3", features = ["derive"]} fastrand = "2.2.0" counter = "0.6.0" @@ -56,6 +56,7 @@ dotenvy = "0.15.7" env_logger = "0.11.5" log = "0.4.22" once_cell = "1.20.2" +once = "0.3.4" [build-dependencies] tokio = { version = "1", features = ["macros", "rt-multi-thread", "rt"] } diff --git a/src/db/cr_core_initializer.rs b/src/db/cr_core_initializer.rs index 4076d8e..a4c5c60 100644 --- a/src/db/cr_core_initializer.rs +++ b/src/db/cr_core_initializer.rs @@ -9,12 +9,14 @@ use crate::models::creature::creature_metadata::type_enum::CreatureTypeEnum; use crate::models::shared::rarity_enum::RarityEnum; use crate::models::shared::size_enum::SizeEnum; use anyhow::{bail, Result}; +use once::assert_has_not_been_called; use serde::{Deserialize, Serialize}; use sqlx::{FromRow, Pool, Sqlite}; -/// Handler for startup, first creature_core initialization. Then it shouldn't be used - pub async fn update_creature_core_table(conn: &Pool) -> Result<()> { + assert_has_not_been_called!( + "Handler for startup, first creature_core initialization. Then it shouldn't be used" + ); let scales = fetch_creature_scales(conn).await?; for cr in get_creatures_raw_essential_data(conn, 0, -1).await? { let traits = fetch_creature_traits(conn, cr.id).await?; From 16f19b8b236afeb951a22a4a255fd678353f468a Mon Sep 17 00:00:00 2001 From: rakuja Date: Sun, 1 Dec 2024 18:42:13 +0100 Subject: [PATCH 2/4] feat: add senses data and vision field --- src/db/data_providers/creature_fetcher.rs | 21 ++++++++++++++----- .../creature_component/creature_extra.rs | 4 +++- src/models/db/mod.rs | 2 +- src/models/db/raw_sense.rs | 7 ------- src/models/db/sense.rs | 11 ++++++++++ 5 files changed, 31 insertions(+), 14 deletions(-) delete mode 100644 src/models/db/raw_sense.rs create mode 100644 src/models/db/sense.rs diff --git a/src/db/data_providers/creature_fetcher.rs b/src/db/data_providers/creature_fetcher.rs index 322777c..e866929 100644 --- a/src/db/data_providers/creature_fetcher.rs +++ b/src/db/data_providers/creature_fetcher.rs @@ -23,9 +23,9 @@ use crate::models::creature::items::spell_caster_entry::SpellCasterEntry; use crate::models::db::raw_immunity::RawImmunity; use crate::models::db::raw_language::RawLanguage; use crate::models::db::raw_resistance::RawResistance; -use crate::models::db::raw_sense::RawSense; use crate::models::db::raw_speed::RawSpeed; use crate::models::db::raw_weakness::RawWeakness; +use crate::models::db::sense::Sense; use crate::models::item::armor_struct::Armor; use crate::models::item::item_struct::Item; use crate::models::item::shield_struct::Shield; @@ -83,10 +83,10 @@ async fn fetch_creature_resistances( .await?) } -async fn fetch_creature_senses(conn: &Pool, creature_id: i64) -> Result> { +async fn fetch_creature_senses(conn: &Pool, creature_id: i64) -> Result> { Ok(sqlx::query_as!( - RawSense, - "SELECT * FROM SENSE_TABLE INTERSECT SELECT sense_id FROM SENSE_CREATURE_ASSOCIATION_TABLE WHERE creature_id == ($1)", + Sense, + "SELECT * FROM SENSE_TABLE WHERE id IN (SELECT sense_id FROM SENSE_CREATURE_ASSOCIATION_TABLE WHERE creature_id == ($1))", creature_id ).fetch_all(conn).await?) } @@ -188,6 +188,15 @@ async fn fetch_creature_perception(conn: &Pool, creature_id: i64) -> Res ) } +async fn fetch_creature_vision(conn: &Pool, creature_id: i64) -> Result { + Ok( + sqlx::query_scalar("SELECT vision FROM CREATURE_TABLE WHERE id = $1 LIMIT 1") + .bind(creature_id) + .fetch_one(conn) + .await?, + ) +} + async fn fetch_creature_perception_detail( conn: &Pool, creature_id: i64, @@ -577,6 +586,7 @@ pub async fn fetch_creature_extra_data( let ac_detail = fetch_creature_ac_detail(conn, creature_id).await?; let language_detail = fetch_creature_language_detail(conn, creature_id).await?; let perception = fetch_creature_perception(conn, creature_id).await?; + let has_vision = fetch_creature_vision(conn, creature_id).await?; let perception_detail = fetch_creature_perception_detail(conn, creature_id).await?; Ok(CreatureExtraData { @@ -584,7 +594,7 @@ pub async fn fetch_creature_extra_data( skills, items, languages: languages.iter().map(|x| x.name.clone()).collect(), - senses: senses.iter().map(|x| x.name.clone()).collect(), + senses, speeds: speeds .iter() .map(|x| (x.name.clone(), x.value as i16)) @@ -595,6 +605,7 @@ pub async fn fetch_creature_extra_data( language_detail, perception, perception_detail, + has_vision, }) } diff --git a/src/models/creature/creature_component/creature_extra.rs b/src/models/creature/creature_component/creature_extra.rs index 6d57bd4..85d0349 100644 --- a/src/models/creature/creature_component/creature_extra.rs +++ b/src/models/creature/creature_component/creature_extra.rs @@ -1,6 +1,7 @@ use crate::models::creature::creature_metadata::variant_enum::CreatureVariant; use crate::models::creature::items::action::Action; use crate::models::creature::items::skill::Skill; +use crate::models::db::sense::Sense; use crate::models::item::item_struct::Item; use serde::{Deserialize, Serialize}; #[allow(unused_imports)] // it's actually used in the example schema @@ -30,7 +31,7 @@ pub struct CreatureExtraData { pub skills: Vec, pub items: Vec, pub languages: Vec, - pub senses: Vec, + pub senses: Vec, #[schema(example = json!({"fly": 100, "swim": 50, "Base": 25}))] pub speeds: BTreeMap, pub ability_scores: AbilityScores, @@ -40,6 +41,7 @@ pub struct CreatureExtraData { #[schema(example = 0)] pub perception: i8, pub perception_detail: Option, + pub has_vision: bool, } impl CreatureExtraData { diff --git a/src/models/db/mod.rs b/src/models/db/mod.rs index 4891214..471eb1c 100644 --- a/src/models/db/mod.rs +++ b/src/models/db/mod.rs @@ -1,6 +1,6 @@ pub mod raw_immunity; pub mod raw_language; pub mod raw_resistance; -pub mod raw_sense; pub mod raw_speed; pub mod raw_weakness; +pub mod sense; diff --git a/src/models/db/raw_sense.rs b/src/models/db/raw_sense.rs deleted file mode 100644 index 0ed414b..0000000 --- a/src/models/db/raw_sense.rs +++ /dev/null @@ -1,7 +0,0 @@ -use serde::{Deserialize, Serialize}; -use sqlx::FromRow; - -#[derive(Serialize, Deserialize, FromRow)] -pub struct RawSense { - pub name: String, -} diff --git a/src/models/db/sense.rs b/src/models/db/sense.rs new file mode 100644 index 0000000..8a24e74 --- /dev/null +++ b/src/models/db/sense.rs @@ -0,0 +1,11 @@ +use serde::{Deserialize, Serialize}; +use sqlx::FromRow; +use utoipa::ToSchema; + +#[derive(Serialize, Deserialize, FromRow, ToSchema, Clone, Eq, Hash, PartialEq)] +pub struct Sense { + pub id: i64, + pub name: String, + pub range: Option, + pub acuity: Option, +} From ab2ef96e4bfdc3e3e98bffe9879e1d8299e91ce6 Mon Sep 17 00:00:00 2001 From: rakuja Date: Sat, 7 Dec 2024 21:09:23 +0100 Subject: [PATCH 3/4] doc: update swagger --- src/routes/bestiary.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/routes/bestiary.rs b/src/routes/bestiary.rs index 9aa98a6..a341f01 100644 --- a/src/routes/bestiary.rs +++ b/src/routes/bestiary.rs @@ -29,6 +29,7 @@ use crate::models::item::weapon_struct::Weapon; use crate::models::bestiary_structs::CreatureSortEnum; use crate::models::bestiary_structs::{BestiaryPaginatedRequest, BestiarySortData}; +use crate::models::db::sense::Sense; use crate::models::routers_validator_structs::{CreatureFieldFilters, PaginatedRequest}; use crate::services::bestiary_service; use crate::services::bestiary_service::BestiaryResponse; @@ -87,6 +88,7 @@ pub fn init_docs(doc: &mut utoipa::openapi::OpenApi) { CreatureExtraData, CreatureCombatData, CreatureSpellCasterData, + Sense, Spell, Shield, Weapon, From d22497dc02f0eac44598d9662cdbe496e05ec39c Mon Sep 17 00:00:00 2001 From: rakuja Date: Sat, 7 Dec 2024 23:06:09 +0100 Subject: [PATCH 4/4] fix: update DB version to download --- Makefile.toml | 2 +- setup.py | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Makefile.toml b/Makefile.toml index 875268d..71c2a01 100644 --- a/Makefile.toml +++ b/Makefile.toml @@ -4,7 +4,7 @@ args = ["clean"] [tasks.prebuild] command = "python3" -args = ["setup.py", "--db_version", "2.3.0"] +args = ["setup.py", "--db_version", "2.4.0"] [tasks.format] install_crate = "rustfmt" diff --git a/setup.py b/setup.py index fc9d09e..ae863b6 100644 --- a/setup.py +++ b/setup.py @@ -25,7 +25,7 @@ def handle_command_line_arguments() -> Optional[str]: if currentArgument in ("-h", "--help"): print("This script downloads or creates necessary file to build BYBE. \n" "Should be executed the first time the project is built in the machine or when resetting the database \n" - "Pass the --db_version or -v argument to input a specific BYBE-DB version to download (>= 2.3.0)") + "Pass the --db_version or -d argument to input a specific BYBE-DB version to download (>= 2.3.0)") elif currentArgument in ("-d", "--db_version"): return currentValue except getopt.error: @@ -33,8 +33,8 @@ def handle_command_line_arguments() -> Optional[str]: def main(): # Check if the file already exists or needs downloading - db_version: str = handle_command_line_arguments() or "2.3.0" - print(db_version) + db_version: str = handle_command_line_arguments() + print(f"Using DB version: {db_version}") or "2.3.0" # Oldest DB version publicly available remote_url: str = f"https://github.com/RakuJa/BYBE-DB/releases/download/v{db_version}/database.db" destination_file: str = "database.db" if not os.path.exists(destination_file):