Clearer function names in new character flow.

This commit is contained in:
jeff 2020-12-09 08:58:49 +00:00
parent 0751a783dc
commit e59e9a5ebf
3 changed files with 34 additions and 35 deletions

View File

@ -39,7 +39,7 @@ pub(crate) trait Visibility {
} }
} }
#[derive(DbEnum, Debug, Serialize, PartialEq)] #[derive(DbEnum, Debug, Serialize, PartialEq, Clone, Copy)]
pub enum CharacterDataType { pub enum CharacterDataType {
ChroniclesOfDarknessV1, ChroniclesOfDarknessV1,
ChangelingV1, ChangelingV1,

View File

@ -10,8 +10,8 @@ mod new;
pub(crate) fn routes() -> Vec<rocket::Route> { pub(crate) fn routes() -> Vec<rocket::Route> {
routes![ routes![
view_character, view_character,
new::new_character, new::new_character_page,
new::create_new_character, new::new_character_submit,
new::new_character_not_logged_in, new::new_character_not_logged_in,
edit_character edit_character
] ]

View File

@ -37,36 +37,20 @@ impl<'v> FromFormValue<'v> for CharacterDataType {
} }
} }
#[get("/new")] fn create_new_sheet(system: &CharacterDataType) -> Result<BytesMut, Error> {
pub(super) fn new_character( use CharacterDataType::*;
logged_in_user: &User, let sheet: BytesMut = match system {
conn: TenebrousDbConn, ChroniclesOfDarknessV1 => {
) -> Result<Template, Error> { let sheet = CofdSheet::default();
let mut context = HashMap::new(); let mut buf = BytesMut::with_capacity(std::mem::size_of_val(&sheet));
let form = NewCharacterForm { sheet.encode(&mut buf)?;
name: "".to_string(),
system: CharacterDataType::ChroniclesOfDarknessV1,
};
context.insert("form", form);
Ok(Template::render("characters/new_character", context))
}
fn new_sheet(system: &CharacterDataType) -> Result<BytesMut, Error> {
let sheet = match system {
CharacterDataType::ChroniclesOfDarknessV1 => {
let mut new_character = CofdSheet::default();
new_character.strength = 100;
let mut buf = BytesMut::with_capacity(std::mem::size_of_val(&new_character));
new_character.encode(&mut buf)?;
buf buf
} }
CharacterDataType::ChangelingV1 => { ChangelingV1 => {
let mut new_character = ChangelingSheet::default(); let mut sheet = ChangelingSheet::default();
new_character.base = Some(CofdSheet::default()); sheet.base = Some(CofdSheet::default());
new_character.base.as_mut().unwrap().strength = 100; let mut buf = BytesMut::with_capacity(std::mem::size_of_val(&sheet));
let mut buf = BytesMut::with_capacity(std::mem::size_of_val(&new_character)); sheet.encode(&mut buf)?;
new_character.encode(&mut buf)?;
buf buf
} }
}; };
@ -74,12 +58,12 @@ fn new_sheet(system: &CharacterDataType) -> Result<BytesMut, Error> {
Ok(sheet) Ok(sheet)
} }
fn do_new_character( fn create_new_character(
form: Form<NewCharacterForm>, form: Form<NewCharacterForm>,
user_id: i32, user_id: i32,
conn: TenebrousDbConn, conn: TenebrousDbConn,
) -> Result<(), Error> { ) -> Result<(), Error> {
let sheet = new_sheet(&form.system)?; let sheet = create_new_sheet(&form.system)?;
let insert = NewCharacter { let insert = NewCharacter {
user_id: user_id, user_id: user_id,
@ -94,8 +78,23 @@ fn do_new_character(
Ok(()) Ok(())
} }
#[get("/new")]
pub(super) fn new_character_page(
logged_in_user: &User,
conn: TenebrousDbConn,
) -> Result<Template, Error> {
let mut context = HashMap::new();
let form = NewCharacterForm {
name: "".to_string(),
system: CharacterDataType::ChroniclesOfDarknessV1,
};
context.insert("form", form);
Ok(Template::render("characters/new_character", context))
}
#[post("/new", data = "<form>")] #[post("/new", data = "<form>")]
pub(super) fn create_new_character( pub(super) fn new_character_submit(
form: Result<Form<NewCharacterForm>, FormError>, form: Result<Form<NewCharacterForm>, FormError>,
logged_in_user: &User, logged_in_user: &User,
conn: TenebrousDbConn, conn: TenebrousDbConn,
@ -113,7 +112,7 @@ pub(super) fn create_new_character(
return Err(Template::render("characters/new_character", context)); return Err(Template::render("characters/new_character", context));
} }
match do_new_character(form.unwrap(), logged_in_user.id, conn) { match create_new_character(form.unwrap(), logged_in_user.id, conn) {
Ok(_) => Ok(crate::routes::common::redirect_to_index()), Ok(_) => Ok(crate::routes::common::redirect_to_index()),
Err(e) => { Err(e) => {
let context = HashMap::<String, String>::new(); let context = HashMap::<String, String>::new();