Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add senses data (range, acuity, vision) #82

Merged
merged 4 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand All @@ -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"] }
Expand Down
2 changes: 1 addition & 1 deletion Makefile.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 3 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ 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:
pass

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):
Expand Down
6 changes: 4 additions & 2 deletions src/db/cr_core_initializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Sqlite>) -> 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?;
Expand Down
21 changes: 16 additions & 5 deletions src/db/data_providers/creature_fetcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -83,10 +83,10 @@ async fn fetch_creature_resistances(
.await?)
}

async fn fetch_creature_senses(conn: &Pool<Sqlite>, creature_id: i64) -> Result<Vec<RawSense>> {
async fn fetch_creature_senses(conn: &Pool<Sqlite>, creature_id: i64) -> Result<Vec<Sense>> {
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?)
}
Expand Down Expand Up @@ -188,6 +188,15 @@ async fn fetch_creature_perception(conn: &Pool<Sqlite>, creature_id: i64) -> Res
)
}

async fn fetch_creature_vision(conn: &Pool<Sqlite>, creature_id: i64) -> Result<bool> {
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<Sqlite>,
creature_id: i64,
Expand Down Expand Up @@ -577,14 +586,15 @@ 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 {
actions,
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))
Expand All @@ -595,6 +605,7 @@ pub async fn fetch_creature_extra_data(
language_detail,
perception,
perception_detail,
has_vision,
})
}

Expand Down
4 changes: 3 additions & 1 deletion src/models/creature/creature_component/creature_extra.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -30,7 +31,7 @@ pub struct CreatureExtraData {
pub skills: Vec<Skill>,
pub items: Vec<Item>,
pub languages: Vec<String>,
pub senses: Vec<String>,
pub senses: Vec<Sense>,
#[schema(example = json!({"fly": 100, "swim": 50, "Base": 25}))]
pub speeds: BTreeMap<String, i16>,
pub ability_scores: AbilityScores,
Expand All @@ -40,6 +41,7 @@ pub struct CreatureExtraData {
#[schema(example = 0)]
pub perception: i8,
pub perception_detail: Option<String>,
pub has_vision: bool,
}

impl CreatureExtraData {
Expand Down
2 changes: 1 addition & 1 deletion src/models/db/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
7 changes: 0 additions & 7 deletions src/models/db/raw_sense.rs

This file was deleted.

11 changes: 11 additions & 0 deletions src/models/db/sense.rs
Original file line number Diff line number Diff line change
@@ -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<i64>,
pub acuity: Option<String>,
}
2 changes: 2 additions & 0 deletions src/routes/bestiary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -87,6 +88,7 @@ pub fn init_docs(doc: &mut utoipa::openapi::OpenApi) {
CreatureExtraData,
CreatureCombatData,
CreatureSpellCasterData,
Sense,
Spell,
Shield,
Weapon,
Expand Down
Loading