From 76c67425b18857c912b657e64a8757b537b06cc5 Mon Sep 17 00:00:00 2001 From: jeff Date: Sat, 2 Jan 2021 22:11:30 +0000 Subject: [PATCH] RPC-ify update attribute API. --- proto/cofd_api.proto | 8 +++++--- src/routes/api.rs | 42 +++++++++++++++++++++--------------------- static/scripts/api.js | 25 ++++++++++++++----------- 3 files changed, 40 insertions(+), 35 deletions(-) diff --git a/proto/cofd_api.proto b/proto/cofd_api.proto index 95c87b7..5b7ab58 100644 --- a/proto/cofd_api.proto +++ b/proto/cofd_api.proto @@ -32,9 +32,11 @@ message Attributes { } //Update an attribute's dot amount. TODO rename to AttributesUpdate. -message Attribute { - string name = 1; - int32 value = 2; +message UpdateAttributeRequest { + string character_username = 1; + int32 character_id = 2; + string attribute_name = 3; + int32 attribute_value = 4; } //Update skill entries in a Chronicles of Darkness character sheet. diff --git a/src/routes/api.rs b/src/routes/api.rs index 1e6317a..8c632c8 100644 --- a/src/routes/api.rs +++ b/src/routes/api.rs @@ -97,35 +97,35 @@ mod cofd { "lol" } - #[patch("/cofd///attributes", data = "")] + #[post("/rpc/cofd/update_attribute", data = "")] pub(super) async fn update_attribute<'a>( - owner: String, - character_id: i32, - attr_update: Proto, + req: Proto, 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 mut character = load_character( + &conn, + logged_in_user, + &req.character_username, + req.character_id, + ) + .await?; - match attr_update.name.to_lowercase().as_ref() { - "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), + 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), }?; - println!( - "updated {} attribute {} to {}", - character.character_name, attr_update.name, attr_update.value - ); - character.update_data(sheet)?; conn.update_character_sheet(&character).await?; Ok("lol") diff --git a/static/scripts/api.js b/static/scripts/api.js index 545841f..c0785eb 100644 --- a/static/scripts/api.js +++ b/static/scripts/api.js @@ -1,9 +1,12 @@ function makeAPI(root) { //Protobuf types - const AttributeType = 'models.proto.cofd.api.Attribute'; - const Attribute = root.lookupType(AttributeType); + const UpdateAttributeRequestType = 'models.proto.cofd.api.UpdateAttributeRequest'; + const UpdateAttributeRequest = root.lookupType(UpdateAttributeRequestType); + const UpdateSkillValueRequestType = 'models.proto.cofd.api.UpdateSkillValueRequest'; const UpdateSkillValueRequest = root.lookupType(UpdateSkillValueRequestType); + + //TODO rpc-ify const SkillSpecializationUpdateType = 'models.proto.cofd.api.SkillSpecializationsUpdate'; const SkillSpecializationsUpdate = root.lookupType(SkillSpecializationUpdateType); @@ -25,17 +28,17 @@ function makeAPI(root) { async function updateAttribute(params) { const { username, characterID, attribute, newValue } = params; - let req = verifyAndCreate(Attribute, { - name: attribute, - value: parseInt(newValue) + let req = verifyAndCreate(UpdateAttributeRequest, { + characterUsername: username, + characterId: parseInt(characterID), + attributeName: attribute, + attributeValue: parseInt(newValue) }); - const resource = attributesResource(username, characterID); - - let resp = await fetch(resource, { - method: 'PATCH', - headers: { ... protobufContentType(AttributeType) }, - body: Attribute.encode(req).finish() + let resp = await fetch('/api/rpc/cofd/update_attribute', { + method: 'POST', + headers: { ... protobufContentType(UpdateAttributeRequestType) }, + body: UpdateAttributeRequest.encode(req).finish() }).then(async resp => { console.log("resp is", await resp.text()); }).catch(async err => {