Move cofd api endpoints into their own file
This commit is contained in:
parent
31091fcb8e
commit
9ca1f989c5
|
@ -1,9 +1,9 @@
|
||||||
use crate::db::{Dao, TenebrousDbConn};
|
use crate::db::{Dao, TenebrousDbConn};
|
||||||
use crate::errors::Error;
|
use crate::errors::Error;
|
||||||
use crate::models::characters::Character;
|
use crate::models::characters::Character;
|
||||||
use crate::models::proto::cofd::*;
|
|
||||||
use crate::models::users::User;
|
use crate::models::users::User;
|
||||||
use std::collections::btree_map::{Entry, OccupiedEntry};
|
|
||||||
|
mod cofd;
|
||||||
|
|
||||||
pub(crate) fn routes() -> Vec<rocket::Route> {
|
pub(crate) fn routes() -> Vec<rocket::Route> {
|
||||||
routes![
|
routes![
|
||||||
|
@ -40,177 +40,3 @@ async fn load_character(
|
||||||
|
|
||||||
Ok(character)
|
Ok(character)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Protobuf-based REST endpoints for editing a character.
|
|
||||||
mod cofd {
|
|
||||||
use super::*;
|
|
||||||
use crate::models::proto::cofd::cofd_sheet::Skill;
|
|
||||||
use crate::models::proto::{cofd::api::*, cofd::*, Proto};
|
|
||||||
|
|
||||||
fn find_skill_entry<'a>(
|
|
||||||
sheet: &'a mut CofdSheet,
|
|
||||||
skill_name: &'a str,
|
|
||||||
) -> Option<OccupiedEntry<'a, String, Skill>> {
|
|
||||||
let all_skills = vec![
|
|
||||||
&mut sheet.mental_skills,
|
|
||||||
&mut sheet.physical_skills,
|
|
||||||
&mut sheet.social_skills,
|
|
||||||
];
|
|
||||||
|
|
||||||
// Search all skill lists for this value using "workaround" to
|
|
||||||
// break value from for loops.
|
|
||||||
let skill: Option<OccupiedEntry<_, _>> = 'l: loop {
|
|
||||||
for skill_map in all_skills {
|
|
||||||
if let Entry::Occupied(entry) = skill_map.entry(skill_name.to_owned()) {
|
|
||||||
break 'l Some(entry);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
break None;
|
|
||||||
};
|
|
||||||
|
|
||||||
skill
|
|
||||||
}
|
|
||||||
|
|
||||||
fn find_skill<'a>(sheet: &'a mut CofdSheet, skill_name: &'a str) -> Option<&'a mut Skill> {
|
|
||||||
find_skill_entry(sheet, skill_name).map(|entry| entry.into_mut())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[post("/cofd/<owner>/<character_id>/basic-info", data = "<info>")]
|
|
||||||
pub(super) fn update_basic_info<'a>(
|
|
||||||
owner: String,
|
|
||||||
character_id: i32,
|
|
||||||
info: Proto<BasicInfo>,
|
|
||||||
) -> &'a str {
|
|
||||||
"lol"
|
|
||||||
}
|
|
||||||
|
|
||||||
#[post("/cofd/<owner>/<character_id>/attributes", data = "<info>")]
|
|
||||||
pub(super) fn update_attributes<'a>(
|
|
||||||
owner: String,
|
|
||||||
character_id: i32,
|
|
||||||
info: Proto<Attributes>,
|
|
||||||
) -> &'a str {
|
|
||||||
"lol"
|
|
||||||
}
|
|
||||||
|
|
||||||
#[post("/rpc/cofd/update_attribute_value", data = "<req>")]
|
|
||||||
pub(super) async fn update_attribute_value(
|
|
||||||
req: Proto<UpdateAttributeRequest>,
|
|
||||||
conn: TenebrousDbConn<'_>,
|
|
||||||
logged_in_user: Option<&User>,
|
|
||||||
) -> Result<Proto<ApiResult>, Error> {
|
|
||||||
let mut character = load_character(
|
|
||||||
&conn,
|
|
||||||
logged_in_user,
|
|
||||||
&req.character_username,
|
|
||||||
req.character_id,
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
let mut sheet: CofdSheet = character.try_deserialize()?;
|
|
||||||
let value = req.attribute_value;
|
|
||||||
match req.attribute_name.to_lowercase().as_ref() {
|
|
||||||
"strength" => Ok(sheet.strength = value),
|
|
||||||
"dexterity" => Ok(sheet.dexterity = value),
|
|
||||||
"stamina" => Ok(sheet.stamina = value),
|
|
||||||
"intelligence" => Ok(sheet.intelligence = value),
|
|
||||||
"wits" => Ok(sheet.wits = value),
|
|
||||||
"resolve" => Ok(sheet.resolve = value),
|
|
||||||
"presence" => Ok(sheet.presence = value),
|
|
||||||
"manipulation" => Ok(sheet.manipulation = value),
|
|
||||||
"composure" => Ok(sheet.composure = value),
|
|
||||||
_ => Err(Error::InvalidInput),
|
|
||||||
}?;
|
|
||||||
|
|
||||||
character.update_data(&sheet)?;
|
|
||||||
conn.update_character_sheet(&character).await?;
|
|
||||||
Ok(Proto(ApiResult {
|
|
||||||
success: true,
|
|
||||||
error: "".to_string(),
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
|
|
||||||
#[patch(
|
|
||||||
"/cofd/<owner>/<character_id>/skills",
|
|
||||||
data = "<skill_update>",
|
|
||||||
rank = 1
|
|
||||||
)]
|
|
||||||
pub(super) async fn update_skills<'a>(
|
|
||||||
owner: String,
|
|
||||||
character_id: i32,
|
|
||||||
skill_update: Proto<SkillUpdate>,
|
|
||||||
conn: TenebrousDbConn<'_>,
|
|
||||||
logged_in_user: Option<&User>,
|
|
||||||
) -> Result<&'a str, Error> {
|
|
||||||
let mut character = load_character(&conn, logged_in_user, &owner, character_id).await?;
|
|
||||||
let mut sheet: CofdSheet = character.try_deserialize()?;
|
|
||||||
let updated_skill: &Skill = skill_update.skill.as_ref().ok_or(Error::InvalidInput)?;
|
|
||||||
let skill_entry = find_skill_entry(&mut sheet, &skill_update.name);
|
|
||||||
|
|
||||||
skill_entry
|
|
||||||
.map(|mut entry| entry.insert(updated_skill.clone()))
|
|
||||||
.ok_or(Error::InvalidInput)?;
|
|
||||||
|
|
||||||
println!(
|
|
||||||
"updated skill {} with {:?}",
|
|
||||||
skill_update.name, skill_update.skill
|
|
||||||
);
|
|
||||||
|
|
||||||
character.update_data(&sheet)?;
|
|
||||||
conn.update_character_sheet(&character).await?;
|
|
||||||
Ok("lol")
|
|
||||||
}
|
|
||||||
|
|
||||||
#[post("/rpc/cofd/update_skill_value", data = "<request>")]
|
|
||||||
pub(super) async fn update_skill_value<'a>(
|
|
||||||
request: Proto<UpdateSkillValueRequest>,
|
|
||||||
conn: TenebrousDbConn<'_>,
|
|
||||||
logged_in_user: Option<&User>,
|
|
||||||
) -> Result<Proto<ApiResult>, Error> {
|
|
||||||
println!("{:#?}", request);
|
|
||||||
let mut character = load_character(
|
|
||||||
&conn,
|
|
||||||
logged_in_user,
|
|
||||||
&request.character_username,
|
|
||||||
request.character_id,
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
let mut sheet: CofdSheet = character.try_deserialize()?;
|
|
||||||
|
|
||||||
let mut skill: Option<&mut Skill> = find_skill(&mut sheet, &request.skill_name);
|
|
||||||
if let Some(ref mut s) = skill {
|
|
||||||
s.dots = request.skill_value;
|
|
||||||
}
|
|
||||||
|
|
||||||
let updated_skill: Skill = skill.map(|s| s.clone()).ok_or(Error::InvalidInput)?;
|
|
||||||
|
|
||||||
println!("updated skill value",);
|
|
||||||
|
|
||||||
character.update_data(&sheet)?;
|
|
||||||
conn.update_character_sheet(&character).await?;
|
|
||||||
Ok(Proto(ApiResult {
|
|
||||||
success: true,
|
|
||||||
error: "".to_string(),
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
|
|
||||||
#[put("/cofd/<owner>/<character_id>/conditions", data = "<info>")]
|
|
||||||
pub(super) fn add_condition<'a>(
|
|
||||||
owner: String,
|
|
||||||
character_id: i32,
|
|
||||||
info: Proto<Condition>,
|
|
||||||
) -> &'a str {
|
|
||||||
"lol"
|
|
||||||
}
|
|
||||||
|
|
||||||
#[delete("/cofd/<owner>/<character_id>/conditions", data = "<info>")]
|
|
||||||
pub(super) fn remove_condition<'a>(
|
|
||||||
owner: String,
|
|
||||||
character_id: i32,
|
|
||||||
info: Proto<Condition>,
|
|
||||||
) -> &'a str {
|
|
||||||
"lol"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -0,0 +1,176 @@
|
||||||
|
use super::load_character;
|
||||||
|
use crate::db::{Dao, TenebrousDbConn};
|
||||||
|
use crate::errors::Error;
|
||||||
|
use crate::models::characters::Character;
|
||||||
|
use crate::models::proto::cofd::cofd_sheet::Skill;
|
||||||
|
use crate::models::proto::cofd::*;
|
||||||
|
use crate::models::proto::{cofd::api::*, cofd::*, Proto};
|
||||||
|
use crate::models::users::User;
|
||||||
|
use std::collections::btree_map::{Entry, OccupiedEntry};
|
||||||
|
|
||||||
|
fn find_skill_entry<'a>(
|
||||||
|
sheet: &'a mut CofdSheet,
|
||||||
|
skill_name: &'a str,
|
||||||
|
) -> Option<OccupiedEntry<'a, String, Skill>> {
|
||||||
|
let all_skills = vec![
|
||||||
|
&mut sheet.mental_skills,
|
||||||
|
&mut sheet.physical_skills,
|
||||||
|
&mut sheet.social_skills,
|
||||||
|
];
|
||||||
|
|
||||||
|
// Search all skill lists for this value using "workaround" to
|
||||||
|
// break value from for loops.
|
||||||
|
let skill: Option<OccupiedEntry<_, _>> = 'l: loop {
|
||||||
|
for skill_map in all_skills {
|
||||||
|
if let Entry::Occupied(entry) = skill_map.entry(skill_name.to_owned()) {
|
||||||
|
break 'l Some(entry);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
break None;
|
||||||
|
};
|
||||||
|
|
||||||
|
skill
|
||||||
|
}
|
||||||
|
|
||||||
|
fn find_skill<'a>(sheet: &'a mut CofdSheet, skill_name: &'a str) -> Option<&'a mut Skill> {
|
||||||
|
find_skill_entry(sheet, skill_name).map(|entry| entry.into_mut())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[post("/cofd/<owner>/<character_id>/basic-info", data = "<info>")]
|
||||||
|
pub(super) fn update_basic_info<'a>(
|
||||||
|
owner: String,
|
||||||
|
character_id: i32,
|
||||||
|
info: Proto<BasicInfo>,
|
||||||
|
) -> &'a str {
|
||||||
|
"lol"
|
||||||
|
}
|
||||||
|
|
||||||
|
#[post("/cofd/<owner>/<character_id>/attributes", data = "<info>")]
|
||||||
|
pub(super) fn update_attributes<'a>(
|
||||||
|
owner: String,
|
||||||
|
character_id: i32,
|
||||||
|
info: Proto<Attributes>,
|
||||||
|
) -> &'a str {
|
||||||
|
"lol"
|
||||||
|
}
|
||||||
|
|
||||||
|
#[post("/rpc/cofd/update_attribute_value", data = "<req>")]
|
||||||
|
pub(super) async fn update_attribute_value(
|
||||||
|
req: Proto<UpdateAttributeRequest>,
|
||||||
|
conn: TenebrousDbConn<'_>,
|
||||||
|
logged_in_user: Option<&User>,
|
||||||
|
) -> Result<Proto<ApiResult>, Error> {
|
||||||
|
let mut character = load_character(
|
||||||
|
&conn,
|
||||||
|
logged_in_user,
|
||||||
|
&req.character_username,
|
||||||
|
req.character_id,
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
let mut sheet: CofdSheet = character.try_deserialize()?;
|
||||||
|
let value = req.attribute_value;
|
||||||
|
match req.attribute_name.to_lowercase().as_ref() {
|
||||||
|
"strength" => Ok(sheet.strength = value),
|
||||||
|
"dexterity" => Ok(sheet.dexterity = value),
|
||||||
|
"stamina" => Ok(sheet.stamina = value),
|
||||||
|
"intelligence" => Ok(sheet.intelligence = value),
|
||||||
|
"wits" => Ok(sheet.wits = value),
|
||||||
|
"resolve" => Ok(sheet.resolve = value),
|
||||||
|
"presence" => Ok(sheet.presence = value),
|
||||||
|
"manipulation" => Ok(sheet.manipulation = value),
|
||||||
|
"composure" => Ok(sheet.composure = value),
|
||||||
|
_ => Err(Error::InvalidInput),
|
||||||
|
}?;
|
||||||
|
|
||||||
|
character.update_data(&sheet)?;
|
||||||
|
conn.update_character_sheet(&character).await?;
|
||||||
|
Ok(Proto(ApiResult {
|
||||||
|
success: true,
|
||||||
|
error: "".to_string(),
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[patch(
|
||||||
|
"/cofd/<owner>/<character_id>/skills",
|
||||||
|
data = "<skill_update>",
|
||||||
|
rank = 1
|
||||||
|
)]
|
||||||
|
pub(super) async fn update_skills<'a>(
|
||||||
|
owner: String,
|
||||||
|
character_id: i32,
|
||||||
|
skill_update: Proto<SkillUpdate>,
|
||||||
|
conn: TenebrousDbConn<'_>,
|
||||||
|
logged_in_user: Option<&User>,
|
||||||
|
) -> Result<&'a str, Error> {
|
||||||
|
let mut character = load_character(&conn, logged_in_user, &owner, character_id).await?;
|
||||||
|
let mut sheet: CofdSheet = character.try_deserialize()?;
|
||||||
|
let updated_skill: &Skill = skill_update.skill.as_ref().ok_or(Error::InvalidInput)?;
|
||||||
|
let skill_entry = find_skill_entry(&mut sheet, &skill_update.name);
|
||||||
|
|
||||||
|
skill_entry
|
||||||
|
.map(|mut entry| entry.insert(updated_skill.clone()))
|
||||||
|
.ok_or(Error::InvalidInput)?;
|
||||||
|
|
||||||
|
println!(
|
||||||
|
"updated skill {} with {:?}",
|
||||||
|
skill_update.name, skill_update.skill
|
||||||
|
);
|
||||||
|
|
||||||
|
character.update_data(&sheet)?;
|
||||||
|
conn.update_character_sheet(&character).await?;
|
||||||
|
Ok("lol")
|
||||||
|
}
|
||||||
|
|
||||||
|
#[post("/rpc/cofd/update_skill_value", data = "<request>")]
|
||||||
|
pub(super) async fn update_skill_value<'a>(
|
||||||
|
request: Proto<UpdateSkillValueRequest>,
|
||||||
|
conn: TenebrousDbConn<'_>,
|
||||||
|
logged_in_user: Option<&User>,
|
||||||
|
) -> Result<Proto<ApiResult>, Error> {
|
||||||
|
println!("{:#?}", request);
|
||||||
|
let mut character = load_character(
|
||||||
|
&conn,
|
||||||
|
logged_in_user,
|
||||||
|
&request.character_username,
|
||||||
|
request.character_id,
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
let mut sheet: CofdSheet = character.try_deserialize()?;
|
||||||
|
|
||||||
|
let mut skill: Option<&mut Skill> = find_skill(&mut sheet, &request.skill_name);
|
||||||
|
if let Some(ref mut s) = skill {
|
||||||
|
s.dots = request.skill_value;
|
||||||
|
}
|
||||||
|
|
||||||
|
let updated_skill: Skill = skill.map(|s| s.clone()).ok_or(Error::InvalidInput)?;
|
||||||
|
|
||||||
|
println!("updated skill value",);
|
||||||
|
|
||||||
|
character.update_data(&sheet)?;
|
||||||
|
conn.update_character_sheet(&character).await?;
|
||||||
|
Ok(Proto(ApiResult {
|
||||||
|
success: true,
|
||||||
|
error: "".to_string(),
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[put("/cofd/<owner>/<character_id>/conditions", data = "<info>")]
|
||||||
|
pub(super) fn add_condition<'a>(
|
||||||
|
owner: String,
|
||||||
|
character_id: i32,
|
||||||
|
info: Proto<Condition>,
|
||||||
|
) -> &'a str {
|
||||||
|
"lol"
|
||||||
|
}
|
||||||
|
|
||||||
|
#[delete("/cofd/<owner>/<character_id>/conditions", data = "<info>")]
|
||||||
|
pub(super) fn remove_condition<'a>(
|
||||||
|
owner: String,
|
||||||
|
character_id: i32,
|
||||||
|
info: Proto<Condition>,
|
||||||
|
) -> &'a str {
|
||||||
|
"lol"
|
||||||
|
}
|
Loading…
Reference in New Issue