From ae6d5bcfa3259b685d0fcd6321af0578931845c4 Mon Sep 17 00:00:00 2001 From: jeff Date: Sun, 10 Jan 2021 20:30:25 +0000 Subject: [PATCH] Implement updating basic info for character name. --- src/db.rs | 21 +++++++++++ src/frontend/scripts/api.ts | 8 +++- src/frontend/scripts/characters/edit.ts | 37 ++++++++++++++++++- .../characters/edit_character.html.tera | 9 ++++- src/routes/api/cofd.rs | 5 ++- 5 files changed, 73 insertions(+), 7 deletions(-) diff --git a/src/db.rs b/src/db.rs index b765a2a..0bb1f58 100644 --- a/src/db.rs +++ b/src/db.rs @@ -39,6 +39,8 @@ pub(crate) trait Dao { async fn insert_character(&self, new_character: NewCharacter<'_>) -> sqlx::Result<()>; + async fn update_character<'a>(&self, character: &'a Character) -> sqlx::Result<()>; + async fn update_character_sheet<'a>(&self, character: &'a Character) -> sqlx::Result<()>; } @@ -124,6 +126,25 @@ impl Dao for SqlitePool { Ok(()) } + async fn update_character<'a>(&self, character: &'a Character) -> sqlx::Result<()> { + sqlx::query( + "UPDATE characters + set user_id = ?, viewable = ?, character_name = ?, + data_type = ?, data_version = ?, data = ? where id = ?", + ) + .bind(character.user_id) + .bind(character.viewable) + .bind(&character.character_name) + .bind(character.data_type) + .bind(character.data_version) + .bind(&character.data) + .bind(character.id) + .execute(self) + .await?; + + Ok(()) + } + async fn update_character_sheet<'a>(&self, character: &'a Character) -> sqlx::Result<()> { sqlx::query("UPDATE characters set data = ? where id = ?") .bind(&character.data) diff --git a/src/frontend/scripts/api.ts b/src/frontend/scripts/api.ts index d809210..9a170ff 100644 --- a/src/frontend/scripts/api.ts +++ b/src/frontend/scripts/api.ts @@ -1,5 +1,5 @@ import * as jspb from "google-protobuf"; -import { ApiResult, UpdateAttributeRequest, UpdateSkillValueRequest } from "../_proto/cofd_api_pb"; +import { ApiResult, UpdateBasicInfoRequest, UpdateAttributeRequest, UpdateSkillValueRequest } from "../_proto/cofd_api_pb"; const PROTOBUF_CONTENT_TYPE = { 'Content-Type': 'application/x-protobuf' }; @@ -7,7 +7,6 @@ function staticImplements() { return (constructor: U) => { constructor }; } - async function makeRequest(uri: string, params: T): Promise { let resp = await fetch(uri, { method: 'POST', @@ -28,3 +27,8 @@ export async function updateAttributeValue(params: UpdateAttributeRequest): Prom let data = await makeRequest('/api/rpc/cofd/update_attribute_value', params); return ApiResult.deserializeBinary(data); } + +export async function updateBasicInfo(params: UpdateBasicInfoRequest): Promise { + let data = await makeRequest('/api/rpc/cofd/update_basic_info', params); + return ApiResult.deserializeBinary(data); +} diff --git a/src/frontend/scripts/characters/edit.ts b/src/frontend/scripts/characters/edit.ts index c89a817..ebea8f9 100644 --- a/src/frontend/scripts/characters/edit.ts +++ b/src/frontend/scripts/characters/edit.ts @@ -1,4 +1,4 @@ -import { UpdateSkillValueRequest, UpdateAttributeRequest } from "../../_proto/cofd_api_pb"; +import { CharacterIdentifier, UpdateBasicInfoRequest, UpdateSkillValueRequest, UpdateAttributeRequest } from "../../_proto/cofd_api_pb"; import * as api from "../api"; // This is the scripting for the edit character page, which submits @@ -10,6 +10,13 @@ import * as api from "../api"; const [, , USERNAME, CHARACTER_ID] = window.location.pathname.split('/'); + function characterId(): CharacterIdentifier { + const id = new CharacterIdentifier(); + id.setCharacterId(parseInt(CHARACTER_ID)); + id.setOwner(USERNAME); + return id; + } + function setupAttributes() { const attributeInputs = document.querySelectorAll('#attributes input[type="number"]'); @@ -52,7 +59,6 @@ import * as api from "../api"; params.setSkillValue(newValue); let resp = await api.updateSkillValue(params); - console.log("got a response back", resp); } @@ -61,8 +67,35 @@ import * as api from "../api"; }); } + function setupBasicInfo() { + async function updateInfo() { + const params = new UpdateBasicInfoRequest(); + + const name = document.querySelector("#characterName")?.value ?? ""; + console.log("name is", name); + + params.setId(characterId()); + params.setName(name); + params.setAge(50); + params.setConcept("cool guy"); + params.setChronicle("the best one"); + params.setGender("apache attack helicopter"); + + let resp = await api.updateBasicInfo(params); + console.log("got a response back", resp); + } + + const inputs = document.querySelectorAll("#basicInfo input"); + + inputs.forEach(input => { + console.log('got an input', input); + input.addEventListener('blur', updateInfo); + }); + } + setupAttributes(); setupSkills(); + setupBasicInfo(); })().catch(e => { alert(e); }); diff --git a/src/frontend/templates/characters/edit_character.html.tera b/src/frontend/templates/characters/edit_character.html.tera index d5ab57e..753ebf7 100644 --- a/src/frontend/templates/characters/edit_character.html.tera +++ b/src/frontend/templates/characters/edit_character.html.tera @@ -89,8 +89,13 @@

Core Sheet

-

Name:

-

System: {{data_type}}

+
+

+ + +

+

System: {{data_type}}

+
{{ macros::attribute(name="Intelligence", value=sheet.intelligence) }} diff --git a/src/routes/api/cofd.rs b/src/routes/api/cofd.rs index e95a4d1..24b5dd6 100644 --- a/src/routes/api/cofd.rs +++ b/src/routes/api/cofd.rs @@ -48,12 +48,15 @@ pub(super) async fn update_basic_info<'a>( let mut character = load_character(&conn, logged_in_user, &id.owner, id.character_id).await?; let mut sheet: CofdSheet = character.try_deserialize()?; + println!("name will now be {}", req.name); + character.character_name = req.name.clone(); //Should probably remove name from the sheet? sheet.name = req.name.clone(); sheet.gender = req.gender.clone(); sheet.concept = req.concept.clone(); character.update_data(&sheet)?; - conn.update_character_sheet(&character).await?; + conn.update_character(&character).await?; + println!("Updated basic info"); Ok(Proto(ApiResult::success())) }