2020-12-27 21:03:10 +00:00
|
|
|
use crate::db::{Dao, TenebrousDbConn};
|
|
|
|
use crate::errors::Error;
|
|
|
|
use crate::models::characters::{Character, CharacterDataType, DynCharacterData, Visibility};
|
2021-01-01 00:40:48 +00:00
|
|
|
use crate::models::proto::cofd::*;
|
2020-12-27 21:03:10 +00:00
|
|
|
use crate::models::users::User;
|
|
|
|
use rocket_contrib::templates::Template;
|
|
|
|
use serde::Serialize;
|
2020-12-29 14:01:07 +00:00
|
|
|
use std::borrow::Cow;
|
2021-01-01 00:40:48 +00:00
|
|
|
use std::collections::btree_map::{Entry, OccupiedEntry};
|
2020-12-27 21:03:10 +00:00
|
|
|
|
|
|
|
pub(crate) fn routes() -> Vec<rocket::Route> {
|
|
|
|
routes![
|
|
|
|
cofd::update_basic_info,
|
|
|
|
cofd::update_attributes,
|
|
|
|
cofd::update_attribute,
|
|
|
|
cofd::update_skills,
|
|
|
|
cofd::add_condition,
|
|
|
|
cofd::remove_condition
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
2020-12-29 14:01:07 +00:00
|
|
|
/// Load the character belonging to the given user, as long as they're
|
|
|
|
/// the owner of that character. Returns an error if user is not
|
|
|
|
/// logged in, the owner of the character is not found, or the logged
|
|
|
|
/// in user does not have the permission to access this character.
|
|
|
|
async fn load_character(
|
2020-12-29 23:06:41 +00:00
|
|
|
conn: &TenebrousDbConn<'_>,
|
2020-12-29 14:01:07 +00:00
|
|
|
logged_in_user: Option<&User>,
|
|
|
|
owner: String,
|
|
|
|
character_id: i32,
|
|
|
|
) -> Result<Character, Error> {
|
|
|
|
let logged_in_user = logged_in_user.ok_or(Error::NotLoggedIn)?;
|
2020-12-31 20:19:45 +00:00
|
|
|
|
2020-12-29 14:01:07 +00:00
|
|
|
let character: Character = conn
|
|
|
|
.load_character(character_id)
|
|
|
|
.await?
|
|
|
|
.ok_or(Error::NotFound)?;
|
|
|
|
|
2020-12-31 20:19:45 +00:00
|
|
|
if logged_in_user.username != owner {
|
2020-12-29 14:01:07 +00:00
|
|
|
return Err(Error::NoPermission);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(character)
|
|
|
|
}
|
|
|
|
|
2020-12-27 21:03:10 +00:00
|
|
|
/// Protobuf-based REST endpoints for editing a character.
|
|
|
|
mod cofd {
|
|
|
|
use super::*;
|
2021-01-01 00:40:48 +00:00
|
|
|
use crate::models::proto::cofd::cofd_sheet::Skill;
|
2020-12-27 21:03:10 +00:00
|
|
|
use crate::models::proto::{cofd::api::*, cofd::*, Proto};
|
|
|
|
|
|
|
|
#[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"
|
|
|
|
}
|
|
|
|
|
2020-12-27 21:49:08 +00:00
|
|
|
#[patch("/cofd/<owner>/<character_id>/attributes", data = "<attr_update>")]
|
|
|
|
pub(super) async fn update_attribute<'a>(
|
2020-12-27 21:03:10 +00:00
|
|
|
owner: String,
|
|
|
|
character_id: i32,
|
2020-12-27 21:49:08 +00:00
|
|
|
attr_update: Proto<Attribute>,
|
2020-12-29 23:06:41 +00:00
|
|
|
conn: TenebrousDbConn<'_>,
|
2020-12-27 21:49:08 +00:00
|
|
|
logged_in_user: Option<&User>,
|
|
|
|
) -> Result<&'a str, Error> {
|
2020-12-29 14:01:07 +00:00
|
|
|
let mut character = load_character(&conn, logged_in_user, owner, character_id).await?;
|
2020-12-27 21:49:08 +00:00
|
|
|
let mut sheet: CofdSheet = character.try_deserialize()?;
|
|
|
|
|
|
|
|
match attr_update.name.to_lowercase().as_ref() {
|
2020-12-29 14:01:07 +00:00
|
|
|
"strength" => Ok(sheet.strength = attr_update.value),
|
|
|
|
"dexterity" => Ok(sheet.dexterity = attr_update.value),
|
|
|
|
"stamina" => Ok(sheet.stamina = attr_update.value),
|
|
|
|
"intelligence" => Ok(sheet.intelligence = attr_update.value),
|
|
|
|
"wits" => Ok(sheet.wits = attr_update.value),
|
|
|
|
"resolve" => Ok(sheet.resolve = attr_update.value),
|
|
|
|
"presence" => Ok(sheet.presence = attr_update.value),
|
|
|
|
"manipulation" => Ok(sheet.manipulation = attr_update.value),
|
|
|
|
"composure" => Ok(sheet.composure = attr_update.value),
|
2020-12-27 21:49:08 +00:00
|
|
|
_ => Err(Error::InvalidInput),
|
|
|
|
}?;
|
|
|
|
|
|
|
|
println!(
|
|
|
|
"updated {} attribute {} to {}",
|
|
|
|
character.character_name, attr_update.name, attr_update.value
|
|
|
|
);
|
|
|
|
|
|
|
|
character.update_data(sheet)?;
|
2020-12-29 23:06:41 +00:00
|
|
|
conn.update_character_sheet(&character).await?;
|
2020-12-27 21:49:08 +00:00
|
|
|
Ok("lol")
|
2020-12-27 21:03:10 +00:00
|
|
|
}
|
|
|
|
|
2021-01-01 00:40:48 +00:00
|
|
|
#[patch("/cofd/<owner>/<character_id>/skills", data = "<skill_update>")]
|
|
|
|
pub(super) async fn update_skills<'a>(
|
2020-12-27 21:03:10 +00:00
|
|
|
owner: String,
|
|
|
|
character_id: i32,
|
2021-01-01 00:40:48 +00:00
|
|
|
skill_update: Proto<SkillUpdate>,
|
2020-12-29 23:06:41 +00:00
|
|
|
conn: TenebrousDbConn<'_>,
|
2021-01-01 00:40:48 +00:00
|
|
|
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 skill: &Skill = skill_update.skill.as_ref().ok_or(Error::InvalidInput)?;
|
|
|
|
|
|
|
|
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_entry: Option<OccupiedEntry<_, _>> = 'l: loop {
|
|
|
|
for skill_map in all_skills {
|
|
|
|
if let Entry::Occupied(entry) = skill_map.entry(skill_update.name.clone()) {
|
|
|
|
break 'l Some(entry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
break None;
|
|
|
|
};
|
|
|
|
|
|
|
|
skill_entry
|
|
|
|
.map(|mut entry| entry.insert(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")
|
2020-12-27 21:03:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[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"
|
|
|
|
}
|
|
|
|
}
|