Ethics & Phenotype Endpoints + Phenotype Renaming

This commit is contained in:
Neshura 2023-09-08 22:55:17 +02:00
parent d91af52811
commit 097b6884b9
Signed by: Neshura
GPG key ID: B6983AAA6B9A7A6C
7 changed files with 163 additions and 41 deletions

View file

@ -25,6 +25,7 @@ build:
script: script:
- echo "Compiling the code..." - echo "Compiling the code..."
- echo DATABASE_URL=$DATABASE_URL >> .env - echo DATABASE_URL=$DATABASE_URL >> .env
- echo MACHINE_GROUP_ID=12 >> .env
- cargo build -r - cargo build -r
- echo "Compile complete." - echo "Compile complete."
after_script: after_script:

View file

@ -33,7 +33,7 @@ pub struct GameGroup {
pub struct Ethic { pub struct Ethic {
pub id: i32, pub id: i32,
pub name: String, pub name: String,
pub machine_ethic: bool, pub gestalt_ethic: bool,
} }
#[derive(Serialize, ToSchema, Debug, FromRow)] #[derive(Serialize, ToSchema, Debug, FromRow)]

View file

@ -148,7 +148,9 @@ async fn main() -> Result<()> {
v3::get_empire, v3::get_empire,
v3::create_empire, v3::create_empire,
v3::edit_empire, v3::edit_empire,
v3::delete_empire v3::delete_empire,
v3::get_ethics,
v3::get_phenotypes
), ),
components(schemas( components(schemas(
v3::schemas::AuthReturn, v3::schemas::AuthReturn,
@ -165,15 +167,17 @@ async fn main() -> Result<()> {
v3::schemas::DeleteEmpireParams, v3::schemas::DeleteEmpireParams,
v3::schemas::FullViewData, v3::schemas::FullViewData,
v3::schemas::Ethic, v3::schemas::Ethic,
v3::schemas::ChellarisEthics,
v3::schemas::EmpireEthic, v3::schemas::EmpireEthic,
v3::schemas::EmpireEthicLegacy, v3::schemas::EmpireEthicLegacy,
v3::schemas::ChellarisGameLegacy, v3::schemas::ChellarisGameLegacy,
v3::schemas::ChellarisGameFlat, v3::schemas::ChellarisGameFlat,
v3::schemas::ChellarisGame, v3::schemas::ChellarisGame,
v3::schemas::Species, v3::schemas::Phenotype,
v3::schemas::ChellarisPhenotypes,
v3::schemas::ChellarisGameGroupLegacy, v3::schemas::ChellarisGameGroupLegacy,
v3::schemas::ChellarisGroupFlat, v3::schemas::ChellarisGroupFlat,
v3::schemas::Portrait, v3::schemas::Species,
v3::schemas::ChellarisEmpireLegacy, v3::schemas::ChellarisEmpireLegacy,
v3::schemas::ChellarisEmpireFlat, v3::schemas::ChellarisEmpireFlat,
v3::schemas::ChellarisEmpire v3::schemas::ChellarisEmpire
@ -229,6 +233,8 @@ async fn main() -> Result<()> {
.service(v3::create_empire) .service(v3::create_empire)
.service(v3::edit_empire) .service(v3::edit_empire)
.service(v3::delete_empire) .service(v3::delete_empire)
.service(v3::get_ethics)
.service(v3::get_phenotypes)
// Swagger UI // Swagger UI
.service( .service(
SwaggerUi::new(concat!(api_base!(), "/swagger/{_:.*}")) SwaggerUi::new(concat!(api_base!(), "/swagger/{_:.*}"))

View file

@ -112,7 +112,7 @@ pub(crate) async fn ethics(
db_data.iter().for_each(|entry| { db_data.iter().for_each(|entry| {
let new_data = schemas::Ethic { let new_data = schemas::Ethic {
id: entry.id, id: entry.id,
machine_ethic: entry.machine_ethic, gestalt_ethic: entry.gestalt_ethic,
name: entry.name.clone() name: entry.name.clone()
}; };

View file

@ -38,7 +38,7 @@ pub struct GameGroup {
pub struct Ethic { pub struct Ethic {
pub id: i32, pub id: i32,
pub name: String, pub name: String,
pub machine_ethic: bool, pub gestalt_ethic: bool,
} }
#[derive(Serialize, ToSchema, Debug, FromRow)] #[derive(Serialize, ToSchema, Debug, FromRow)]

View file

@ -5,12 +5,9 @@ use actix_web::{
web::{self, Json}, web::{self, Json},
HttpRequest, HttpResponse, Responder, HttpRequest, HttpResponse, Responder,
}; };
use sqlx::{QueryBuilder}; use sqlx::QueryBuilder;
use crate::{ use crate::{db, AppState};
db::{self},
AppState,
};
pub(crate) mod schemas; pub(crate) mod schemas;
@ -110,37 +107,37 @@ pub(crate) async fn full_view_data(data: web::Data<AppState>) -> impl Responder
let mut parsed_data: schemas::FullViewData = schemas::FullViewData { let mut parsed_data: schemas::FullViewData = schemas::FullViewData {
games: HashMap::new(), games: HashMap::new(),
ethics: HashMap::new(), ethics: HashMap::new(),
species: HashMap::new(), phenotypes: HashMap::new(),
}; };
// Data processing // Data processing
// Species Vector // Species Vector
db_portrait_groups.iter().for_each(|species| { db_portrait_groups.iter().for_each(|species| {
let new_data = schemas::Species { let new_data = schemas::Phenotype {
id: species.id, id: species.id,
display: species.name.clone(), display: species.name.clone(),
portraits: HashMap::new(), species: HashMap::new(),
}; };
parsed_data parsed_data
.species .phenotypes
.entry(species.id) .entry(species.id)
.and_modify(|d| *d = new_data.clone()) .and_modify(|d| *d = new_data.clone())
.or_insert(new_data); .or_insert(new_data);
}); });
db_portraits.iter().for_each(|portrait| { db_portraits.iter().for_each(|portrait| {
let new_data = schemas::Portrait { let new_data = schemas::Species {
id: portrait.id, id: portrait.id,
hires: portrait.hires.clone(), hires: portrait.hires.clone(),
lores: Some(portrait.lores.clone()), lores: portrait.lores.clone(),
}; };
parsed_data parsed_data
.species .phenotypes
.get_mut(&portrait.group_id) .get_mut(&portrait.group_id)
.unwrap() .unwrap()
.portraits .species
.entry(portrait.id) .entry(portrait.id)
.and_modify(|d| *d = new_data.clone()) .and_modify(|d| *d = new_data.clone())
.or_insert(new_data); .or_insert(new_data);
@ -182,7 +179,7 @@ pub(crate) async fn full_view_data(data: web::Data<AppState>) -> impl Responder
let new_data = schemas::ChellarisEmpireLegacy { let new_data = schemas::ChellarisEmpireLegacy {
id: empire.id, id: empire.id,
gestalt: empire.gestalt, gestalt: empire.gestalt,
machine: false, machine: empire.portrait_group_id.to_string() == dotenv!("MACHINE_GROUP_ID"),
group: empire.group_id, group: empire.group_id,
portrait_id: empire.portrait_id, portrait_id: empire.portrait_id,
portrait_group_id: empire.portrait_group_id, portrait_group_id: empire.portrait_group_id,
@ -205,7 +202,7 @@ pub(crate) async fn full_view_data(data: web::Data<AppState>) -> impl Responder
let new_data = schemas::Ethic { let new_data = schemas::Ethic {
id: ethic.id, id: ethic.id,
display: ethic.name.clone(), display: ethic.name.clone(),
machine: ethic.machine_ethic, gestalt: ethic.gestalt_ethic,
}; };
parsed_data parsed_data
@ -222,11 +219,11 @@ pub(crate) async fn full_view_data(data: web::Data<AppState>) -> impl Responder
let new_data = schemas::EmpireEthicLegacy { let new_data = schemas::EmpireEthicLegacy {
ethic_id: empire_ethic.ethics_id, ethic_id: empire_ethic.ethics_id,
display: parsed_data.ethics[&empire_ethic.ethics_id].display.clone(), 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, fanatic: empire_ethic.fanatic,
}; };
if new_data.machine { if new_data.gestalt {
parsed_data parsed_data
.games .games
.get_mut(&game_id) .get_mut(&game_id)
@ -234,7 +231,7 @@ pub(crate) async fn full_view_data(data: web::Data<AppState>) -> impl Responder
.empires .empires
.get_mut(&id) .get_mut(&id)
.unwrap() .unwrap()
.machine = true; .gestalt = true;
} }
parsed_data parsed_data
@ -1235,9 +1232,113 @@ pub(crate) async fn delete_empire(
} }
} }
// Data Manipulation Endpoints // Ethics Endpoints
// Data Manipulation Admin Only
// Admin #[utoipa::path(
// Add/Update/Remove Portrait responses(
// Add/Update/Remove Species (status = 200, description = "OK", body = ChellarisEthics),
// Add/Update/Remove Ethics ),
)]
#[get("/api/v3/ethics")]
pub(crate) async fn get_ethics(data: web::Data<AppState>) -> impl Responder {
// SQL Queries
// TODO: Optimize by utilizing some SQL magic, for now this has to do
let mut db_ethics: HashMap<i32, schemas::Ethic> = HashMap::new();
let mut db_ethic_query = QueryBuilder::<sqlx::Postgres>::new("SELECT * FROM public.ethics");
match db_ethic_query
.build_query_as::<db::schemas::Ethic>()
.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<AppState>) -> impl Responder {
// SQL Queries
// TODO: Optimize by utilizing some SQL magic, for now this has to do
let mut db_phenotypes: HashMap<i32, schemas::Phenotype> = HashMap::new();
let mut db_phenotype_query = QueryBuilder::<sqlx::Postgres>::new("SELECT * FROM public.portrait_groups");
match db_phenotype_query
.build_query_as::<db::schemas::PortraitGroup>()
.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::<sqlx::Postgres>::new("SELECT * FROM public.portraits");
match db_species_query
.build_query_as::<db::schemas::Portrait>()
.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);
}

View file

@ -1,4 +1,4 @@
use std::{collections::HashMap}; use std::{collections::HashMap, hash::Hash};
use serde::{Serialize, Deserialize}; use serde::{Serialize, Deserialize};
use utoipa::{ToSchema, IntoParams}; use utoipa::{ToSchema, IntoParams};
@ -43,7 +43,7 @@ pub struct DeleteGameParam {
pub struct FullViewData { pub struct FullViewData {
pub games: HashMap<i32, ChellarisGameLegacy>, pub games: HashMap<i32, ChellarisGameLegacy>,
pub ethics: HashMap<i32, Ethic>, pub ethics: HashMap<i32, Ethic>,
pub species: HashMap<i32, Species>, pub phenotypes: HashMap<i32, Phenotype>,
} }
#[derive(Serialize, ToSchema, Debug, Clone)] #[derive(Serialize, ToSchema, Debug, Clone)]
@ -213,11 +213,13 @@ pub struct ChellarisEmpireLegacy {
pub ethics: HashMap<i32, EmpireEthicLegacy>, pub ethics: HashMap<i32, EmpireEthicLegacy>,
} }
// Ethics Structs
#[derive(Serialize, ToSchema, Debug, Clone)] #[derive(Serialize, ToSchema, Debug, Clone)]
pub struct Ethic { pub struct Ethic {
pub id: i32, pub id: i32,
pub display: String, pub display: String,
pub machine: bool, pub gestalt: bool,
} }
#[derive(Serialize, Deserialize, ToSchema, Debug, Clone)] #[derive(Serialize, Deserialize, ToSchema, Debug, Clone)]
@ -232,21 +234,33 @@ pub struct EmpireEthic {
pub struct EmpireEthicLegacy { pub struct EmpireEthicLegacy {
pub ethic_id: i32, pub ethic_id: i32,
pub display: String, pub display: String,
pub machine: bool, pub gestalt: bool,
pub fanatic: bool, pub fanatic: bool,
} }
#[derive(Serialize, ToSchema, Debug, Clone)]
pub struct ChellarisEthics {
pub ethics: HashMap<i32, Ethic>,
}
// Species Structs
#[derive(Serialize, ToSchema, Debug, Clone)]
pub struct Phenotype {
pub id: i32,
pub display: String,
pub species: HashMap<i32, Species>,
}
#[derive(Serialize, ToSchema, Debug, Clone)]
pub struct ChellarisPhenotypes {
pub phenotypes: HashMap<i32, Phenotype>
}
#[derive(Serialize, ToSchema, Debug, Clone)] #[derive(Serialize, ToSchema, Debug, Clone)]
pub struct Species { pub struct Species {
pub id: i32,
pub display: String,
pub portraits: HashMap<i32, Portrait>,
}
#[derive(Serialize, ToSchema, Debug, Clone)]
pub struct Portrait {
pub id: i32, pub id: i32,
pub hires: String, pub hires: String,
pub lores: Option<String>, pub lores: String,
} }