diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index b92dca6..4580c75 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -25,6 +25,7 @@ build: script: - echo "Compiling the code..." - echo DATABASE_URL=$DATABASE_URL >> .env + - echo MACHINE_GROUP_ID=12 >> .env - cargo build -r - echo "Compile complete." after_script: diff --git a/src/db/schemas.rs b/src/db/schemas.rs index 88525ad..0978e56 100644 --- a/src/db/schemas.rs +++ b/src/db/schemas.rs @@ -33,7 +33,7 @@ pub struct GameGroup { pub struct Ethic { pub id: i32, pub name: String, - pub machine_ethic: bool, + pub gestalt_ethic: bool, } #[derive(Serialize, ToSchema, Debug, FromRow)] diff --git a/src/main.rs b/src/main.rs index 8cc7c09..28b5ef2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -148,7 +148,9 @@ async fn main() -> Result<()> { v3::get_empire, v3::create_empire, v3::edit_empire, - v3::delete_empire + v3::delete_empire, + v3::get_ethics, + v3::get_phenotypes ), components(schemas( v3::schemas::AuthReturn, @@ -165,15 +167,17 @@ async fn main() -> Result<()> { v3::schemas::DeleteEmpireParams, v3::schemas::FullViewData, v3::schemas::Ethic, + v3::schemas::ChellarisEthics, v3::schemas::EmpireEthic, v3::schemas::EmpireEthicLegacy, v3::schemas::ChellarisGameLegacy, v3::schemas::ChellarisGameFlat, v3::schemas::ChellarisGame, - v3::schemas::Species, + v3::schemas::Phenotype, + v3::schemas::ChellarisPhenotypes, v3::schemas::ChellarisGameGroupLegacy, v3::schemas::ChellarisGroupFlat, - v3::schemas::Portrait, + v3::schemas::Species, v3::schemas::ChellarisEmpireLegacy, v3::schemas::ChellarisEmpireFlat, v3::schemas::ChellarisEmpire @@ -229,6 +233,8 @@ async fn main() -> Result<()> { .service(v3::create_empire) .service(v3::edit_empire) .service(v3::delete_empire) + .service(v3::get_ethics) + .service(v3::get_phenotypes) // Swagger UI .service( SwaggerUi::new(concat!(api_base!(), "/swagger/{_:.*}")) diff --git a/src/v2/mod.rs b/src/v2/mod.rs index 9f37731..8cff9c5 100644 --- a/src/v2/mod.rs +++ b/src/v2/mod.rs @@ -112,7 +112,7 @@ pub(crate) async fn ethics( db_data.iter().for_each(|entry| { let new_data = schemas::Ethic { id: entry.id, - machine_ethic: entry.machine_ethic, + gestalt_ethic: entry.gestalt_ethic, name: entry.name.clone() }; diff --git a/src/v2/schemas.rs b/src/v2/schemas.rs index e575885..69897e0 100644 --- a/src/v2/schemas.rs +++ b/src/v2/schemas.rs @@ -38,7 +38,7 @@ pub struct GameGroup { pub struct Ethic { pub id: i32, pub name: String, - pub machine_ethic: bool, + pub gestalt_ethic: bool, } #[derive(Serialize, ToSchema, Debug, FromRow)] diff --git a/src/v3/mod.rs b/src/v3/mod.rs index b95ab27..9d56bce 100644 --- a/src/v3/mod.rs +++ b/src/v3/mod.rs @@ -5,12 +5,9 @@ use actix_web::{ web::{self, Json}, HttpRequest, HttpResponse, Responder, }; -use sqlx::{QueryBuilder}; +use sqlx::QueryBuilder; -use crate::{ - db::{self}, - AppState, -}; +use crate::{db, AppState}; pub(crate) mod schemas; @@ -110,37 +107,37 @@ pub(crate) async fn full_view_data(data: web::Data) -> impl Responder let mut parsed_data: schemas::FullViewData = schemas::FullViewData { games: HashMap::new(), ethics: HashMap::new(), - species: HashMap::new(), + phenotypes: HashMap::new(), }; // Data processing // Species Vector db_portrait_groups.iter().for_each(|species| { - let new_data = schemas::Species { + let new_data = schemas::Phenotype { id: species.id, display: species.name.clone(), - portraits: HashMap::new(), + species: HashMap::new(), }; parsed_data - .species + .phenotypes .entry(species.id) .and_modify(|d| *d = new_data.clone()) .or_insert(new_data); }); db_portraits.iter().for_each(|portrait| { - let new_data = schemas::Portrait { + let new_data = schemas::Species { id: portrait.id, hires: portrait.hires.clone(), - lores: Some(portrait.lores.clone()), + lores: portrait.lores.clone(), }; parsed_data - .species + .phenotypes .get_mut(&portrait.group_id) .unwrap() - .portraits + .species .entry(portrait.id) .and_modify(|d| *d = new_data.clone()) .or_insert(new_data); @@ -182,7 +179,7 @@ pub(crate) async fn full_view_data(data: web::Data) -> impl Responder let new_data = schemas::ChellarisEmpireLegacy { id: empire.id, gestalt: empire.gestalt, - machine: false, + machine: empire.portrait_group_id.to_string() == dotenv!("MACHINE_GROUP_ID"), group: empire.group_id, portrait_id: empire.portrait_id, portrait_group_id: empire.portrait_group_id, @@ -205,7 +202,7 @@ pub(crate) async fn full_view_data(data: web::Data) -> impl Responder let new_data = schemas::Ethic { id: ethic.id, display: ethic.name.clone(), - machine: ethic.machine_ethic, + gestalt: ethic.gestalt_ethic, }; parsed_data @@ -222,11 +219,11 @@ pub(crate) async fn full_view_data(data: web::Data) -> impl Responder let new_data = schemas::EmpireEthicLegacy { ethic_id: empire_ethic.ethics_id, display: parsed_data.ethics[&empire_ethic.ethics_id].display.clone(), - machine: parsed_data.ethics[&empire_ethic.ethics_id].machine, + gestalt: parsed_data.ethics[&empire_ethic.ethics_id].gestalt, fanatic: empire_ethic.fanatic, }; - if new_data.machine { + if new_data.gestalt { parsed_data .games .get_mut(&game_id) @@ -234,7 +231,7 @@ pub(crate) async fn full_view_data(data: web::Data) -> impl Responder .empires .get_mut(&id) .unwrap() - .machine = true; + .gestalt = true; } parsed_data @@ -1235,9 +1232,113 @@ pub(crate) async fn delete_empire( } } -// Data Manipulation Endpoints +// Ethics Endpoints +// Data Manipulation Admin Only -// Admin -// Add/Update/Remove Portrait -// Add/Update/Remove Species -// Add/Update/Remove Ethics +#[utoipa::path( + responses( + (status = 200, description = "OK", body = ChellarisEthics), + ), +)] +#[get("/api/v3/ethics")] +pub(crate) async fn get_ethics(data: web::Data) -> impl Responder { + // SQL Queries + // TODO: Optimize by utilizing some SQL magic, for now this has to do + let mut db_ethics: HashMap = HashMap::new(); + + let mut db_ethic_query = QueryBuilder::::new("SELECT * FROM public.ethics"); + + match db_ethic_query + .build_query_as::() + .fetch_all(&data.db) + .await + { + Ok(data) => { + for ethic in data { + db_ethics.insert( + ethic.id, + schemas::Ethic { + id: ethic.id, + gestalt: ethic.gestalt_ethic, + display: ethic.name, + }, + ); + } + } + Err(e) => return HttpResponse::UnprocessableEntity().body(format!("{:#?}", e.to_string())), + }; + + // Empire Ethic Creation + let parsed_data: schemas::ChellarisEthics = schemas::ChellarisEthics { ethics: db_ethics }; + + return HttpResponse::Ok().json(parsed_data); +} + +// Species & Portrait Endpoints +// Data Manipulation Admin Only + +#[utoipa::path( + responses( + (status = 200, description = "OK", body = ChellarisPhenotypes), + ), + security( + ("api_key" = []) + ), +)] +#[get("/api/v3/phenotypes")] +pub(crate) async fn get_phenotypes(data: web::Data) -> impl Responder { + // SQL Queries + // TODO: Optimize by utilizing some SQL magic, for now this has to do + let mut db_phenotypes: HashMap = HashMap::new(); + + let mut db_phenotype_query = QueryBuilder::::new("SELECT * FROM public.portrait_groups"); + + match db_phenotype_query + .build_query_as::() + .fetch_all(&data.db) + .await + { + Ok(data) => { + for phenotype in data { + db_phenotypes.insert( + phenotype.id, + schemas::Phenotype { + id: phenotype.id, + display: phenotype.name, + species: HashMap::new(), + }, + ); + } + } + Err(e) => return HttpResponse::UnprocessableEntity().body(format!("{:#?}", e.to_string())), + }; + + let mut db_species_query = QueryBuilder::::new("SELECT * FROM public.portraits"); + + match db_species_query + .build_query_as::() + .fetch_all(&data.db) + .await + { + Ok(data) => { + for species in data { + if let Some(phenotype) = db_phenotypes.get_mut(&species.group_id) { + phenotype.species.insert( + species.id, + schemas::Species { + id: species.id, + hires: species.hires, + lores: species.lores + } + ); + } + } + } + Err(e) => return HttpResponse::UnprocessableEntity().body(format!("{:#?}", e.to_string())), + }; + + // Empire Ethic Creation + let parsed_data: schemas::ChellarisPhenotypes = schemas::ChellarisPhenotypes { phenotypes: db_phenotypes }; + + return HttpResponse::Ok().json(parsed_data); +} diff --git a/src/v3/schemas.rs b/src/v3/schemas.rs index 8ff7e60..7320358 100644 --- a/src/v3/schemas.rs +++ b/src/v3/schemas.rs @@ -1,4 +1,4 @@ -use std::{collections::HashMap}; +use std::{collections::HashMap, hash::Hash}; use serde::{Serialize, Deserialize}; use utoipa::{ToSchema, IntoParams}; @@ -43,7 +43,7 @@ pub struct DeleteGameParam { pub struct FullViewData { pub games: HashMap, pub ethics: HashMap, - pub species: HashMap, + pub phenotypes: HashMap, } #[derive(Serialize, ToSchema, Debug, Clone)] @@ -213,11 +213,13 @@ pub struct ChellarisEmpireLegacy { pub ethics: HashMap, } +// Ethics Structs + #[derive(Serialize, ToSchema, Debug, Clone)] pub struct Ethic { pub id: i32, pub display: String, - pub machine: bool, + pub gestalt: bool, } #[derive(Serialize, Deserialize, ToSchema, Debug, Clone)] @@ -232,21 +234,33 @@ pub struct EmpireEthic { pub struct EmpireEthicLegacy { pub ethic_id: i32, pub display: String, - pub machine: bool, + pub gestalt: bool, pub fanatic: bool, } +#[derive(Serialize, ToSchema, Debug, Clone)] +pub struct ChellarisEthics { + pub ethics: HashMap, +} + +// Species Structs + +#[derive(Serialize, ToSchema, Debug, Clone)] +pub struct Phenotype { + pub id: i32, + pub display: String, + pub species: HashMap, +} + +#[derive(Serialize, ToSchema, Debug, Clone)] +pub struct ChellarisPhenotypes { + pub phenotypes: HashMap +} + #[derive(Serialize, ToSchema, Debug, Clone)] pub struct Species { - pub id: i32, - pub display: String, - pub portraits: HashMap, -} - -#[derive(Serialize, ToSchema, Debug, Clone)] -pub struct Portrait { pub id: i32, pub hires: String, - pub lores: Option, + pub lores: String, }