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: Template refactor #74

Merged
merged 13 commits into from
Oct 2, 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
4 changes: 4 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,7 @@ repos:
rev: v3.0.1
hooks:
- id: docker-compose-check
- repo: https://github.com/est31/cargo-udeps
rev: v0.1.47
hooks:
- id: udeps
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ actix-cors = "0.7.0"
validator = {version="0.18.1", features = ["derive"]}

utoipa = { version = "5.0.0-beta.0", features = ["actix_extras"] }
utoipa-swagger-ui = { version = "7.1.1-beta.0", features = ["actix-web"] }
utoipa-swagger-ui = { version = "7.1.1-beta.0", features = ["actix-web", "reqwest"] }

sqlx = { version = "0.8.2", features = ["runtime-async-std", "sqlite"] }
cached = { version = "0.53.1", features = ["async"] }
Expand Down Expand Up @@ -55,3 +55,6 @@ tokio = { version = "1", features = ["macros", "rt-multi-thread", "rt"] }
anyhow = "1.0.89"
sqlx = {version = "0.8.2", features = ["runtime-async-std", "sqlite"]}
dotenvy = "0.15.7"

[dev-dependencies]
rstest = "0.22.0"
2 changes: 0 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ RUN apk add build-base

RUN apk add musl-dev

RUN apk add curl

# Build the project with optimizations
RUN cargo build --target x86_64-unknown-linux-musl --release

Expand Down
2 changes: 1 addition & 1 deletion src/models/routers_validator_structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ impl Default for PaginatedRequest {

#[derive(Serialize, Deserialize, ToSchema, Validate, Eq, PartialEq, Hash, Clone)]
pub struct Dice {
#[validate(range(min = 1, max = 255))]
#[validate(range(min = 0, max = 255))]
pub n_of_dices: u8,
// 1 needs to be an option, to allow 100d1 => 100
#[validate(range(min = 1, max = 255))]
Expand Down
156 changes: 154 additions & 2 deletions src/models/shop_structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,52 @@ use utoipa::{IntoParams, ToSchema};
use validator::Validate;

#[derive(
Serialize, Deserialize, ToSchema, Default, EnumIter, Eq, PartialEq, Hash, Ord, PartialOrd, Clone,
Serialize, Deserialize, ToSchema, Default, Eq, PartialEq, Hash, Ord, PartialOrd, Clone,
)]
pub struct ShopTemplateData {
pub name: String,
pub description: String,
pub equipment_percentage: u8,
pub weapon_percentage: u8,
pub armor_percentage: u8,
pub shield_percentage: u8,
pub item_types: Vec<ItemTypeEnum>,
pub item_rarities: Vec<RarityEnum>,
pub item_traits_whitelist: Vec<String>,
pub item_traits_blacklist: Vec<String>,
}

impl From<ShopTemplateEnum> for ShopTemplateData {
fn from(template_enum: ShopTemplateEnum) -> Self {
let (e_p, w_p, a_p, s_p) = template_enum.get_equippable_percentages();
ShopTemplateData {
name: template_enum.to_string(),
description: template_enum.get_description(),
equipment_percentage: e_p,
weapon_percentage: w_p,
armor_percentage: a_p,
shield_percentage: s_p,
item_types: template_enum.get_allowed_item_types(),
item_rarities: template_enum.get_allowed_item_rarities(),
item_traits_whitelist: template_enum.get_traits_whitelist(),
item_traits_blacklist: template_enum.get_traits_blacklist(),
}
}
}

#[derive(
Serialize,
Deserialize,
ToSchema,
Default,
EnumIter,
Eq,
PartialEq,
Hash,
Ord,
PartialOrd,
Clone,
Display,
)]
pub enum ShopTemplateEnum {
Blacksmith,
Expand All @@ -18,6 +63,103 @@ pub enum ShopTemplateEnum {
General,
}

impl ShopTemplateEnum {
/// Returns percentage of equipment, weapons, armor, shield for the given template
pub fn get_equippable_percentages(&self) -> (u8, u8, u8, u8) {
match self {
ShopTemplateEnum::Blacksmith => (10, 40, 25, 25),
ShopTemplateEnum::Alchemist => (100, 0, 0, 0),
ShopTemplateEnum::General => (70, 10, 10, 10),
}
}

pub fn get_allowed_item_types(&self) -> Vec<ItemTypeEnum> {
match self {
ShopTemplateEnum::Blacksmith => {
vec![
ItemTypeEnum::Armor,
ItemTypeEnum::Shield,
ItemTypeEnum::Weapon,
ItemTypeEnum::Consumable,
ItemTypeEnum::Equipment,
]
}
ShopTemplateEnum::Alchemist => {
vec![ItemTypeEnum::Consumable, ItemTypeEnum::Equipment]
}
ShopTemplateEnum::General => {
vec![
ItemTypeEnum::Armor,
ItemTypeEnum::Shield,
ItemTypeEnum::Weapon,
ItemTypeEnum::Consumable,
ItemTypeEnum::Equipment,
]
}
}
}

pub fn get_allowed_item_rarities(&self) -> Vec<RarityEnum> {
match self {
ShopTemplateEnum::Blacksmith => {
vec![RarityEnum::Common, RarityEnum::Uncommon, RarityEnum::Rare]
}
ShopTemplateEnum::Alchemist => {
vec![RarityEnum::Common, RarityEnum::Uncommon, RarityEnum::Rare]
}
ShopTemplateEnum::General => {
vec![RarityEnum::Common, RarityEnum::Uncommon, RarityEnum::Rare]
}
}
}

pub fn get_traits_whitelist(&self) -> Vec<String> {
// For future-proof, right now contains 0 logic
match self {
ShopTemplateEnum::Blacksmith => {
vec![]
}
ShopTemplateEnum::Alchemist => {
vec![
"Alchemical".to_string(),
"Bomb".to_string(),
"Splash".to_string(),
"Potion".to_string(),
]
}
ShopTemplateEnum::General => {
vec![]
}
}
}

pub fn get_traits_blacklist(&self) -> Vec<String> {
match self {
ShopTemplateEnum::Blacksmith => {
vec![]
}
ShopTemplateEnum::Alchemist => {
vec![]
}
ShopTemplateEnum::General => {
vec![]
}
}
}

pub fn get_description(&self) -> String {
String::from(match self {
ShopTemplateEnum::Blacksmith => {
"Mainly weapons, armors and shields, sometimes equipment and consumables"
}
ShopTemplateEnum::Alchemist => {
"Only equipment and consumables, no weapons, armors or shields"
}
ShopTemplateEnum::General => "All kinds of items",
})
}
}

#[derive(Serialize, Deserialize, ToSchema, Validate, Clone)]
pub struct RandomShopData {
pub category_filter: Option<Vec<String>>,
Expand All @@ -33,9 +175,19 @@ pub struct RandomShopData {
#[validate(range(max = 30))]
pub max_level: Option<u8>,
#[validate(length(min = 1))]
pub equipment_dices: Vec<Dice>,
pub equippable_dices: Vec<Dice>,
#[validate(length(min = 1))]
pub consumable_dices: Vec<Dice>,

#[validate(range(min = 0, max = 100))]
pub equipment_percentage: Option<u8>,
#[validate(range(min = 0, max = 100))]
pub weapon_percentage: Option<u8>,
#[validate(range(min = 0, max = 100))]
pub armor_percentage: Option<u8>,
#[validate(range(min = 0, max = 100))]
pub shield_percentage: Option<u8>,

pub shop_template: Option<ShopTemplateEnum>,
pub pathfinder_version: Option<PathfinderVersionEnum>,
}
Expand Down
21 changes: 21 additions & 0 deletions src/routes/shop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::models::item::weapon_struct::WeaponData;
use crate::models::response_data::ResponseItem;
use crate::models::routers_validator_structs::ItemFieldFilters;
use crate::models::routers_validator_structs::{Dice, PaginatedRequest};
use crate::models::shop_structs::ShopTemplateData;
use crate::models::shop_structs::ShopTemplateEnum;
use crate::models::shop_structs::{ItemSortEnum, ShopPaginatedRequest};
use crate::models::shop_structs::{RandomShopData, ShopSortData};
Expand All @@ -24,6 +25,7 @@ pub fn init_endpoints(cfg: &mut web::ServiceConfig) {
.service(get_item)
.service(get_shop_listing)
.service(get_items_traits_list)
.service(get_templates_data)
.service(get_items_sources_list)
.service(get_random_shop_listing),
);
Expand All @@ -37,6 +39,7 @@ pub fn init_docs(doc: &mut utoipa::openapi::OpenApi) {
get_item,
get_random_shop_listing,
get_items_traits_list,
get_templates_data,
get_items_sources_list
),
components(schemas(
Expand All @@ -47,6 +50,7 @@ pub fn init_docs(doc: &mut utoipa::openapi::OpenApi) {
RandomShopData,
Dice,
ShopTemplateEnum,
ShopTemplateData,
ItemFieldFilters,
ItemSortEnum,
DamageData,
Expand Down Expand Up @@ -181,6 +185,23 @@ pub async fn get_items_traits_list(data: web::Data<AppState>) -> actix_web::Resu
Ok(web::Json(shop_service::get_traits_list(&data).await))
}

#[utoipa::path(
get,
path = "/shop/templates_data",
tag = "shop",
params(

),
responses(
(status=200, description = "Successful Response", body = [ShopTemplateData]),
(status=400, description = "Bad request.")
),
)]
#[get("/templates_data")]
pub async fn get_templates_data() -> actix_web::Result<impl Responder> {
Ok(web::Json(shop_service::get_shop_templates_data().await))
}

fn sanitize_id(creature_id: &str) -> actix_web::Result<i64> {
let id = creature_id.parse::<i64>();
match id {
Expand Down
Loading
Loading