From 9968cc4a6e3c59e30a8ea39d0e862d446b866232 Mon Sep 17 00:00:00 2001 From: jeff Date: Sat, 2 Jan 2021 22:03:10 +0000 Subject: [PATCH] Update skill value as rpc API (though not grpc) --- proto/cofd_api.proto | 12 +++++++++--- src/models/proto.rs | 1 + src/routes/api.rs | 32 +++++++++++++++++--------------- static/scripts/api.js | 30 ++++++++++++++++++------------ 4 files changed, 45 insertions(+), 30 deletions(-) diff --git a/proto/cofd_api.proto b/proto/cofd_api.proto index 4718740..95c87b7 100644 --- a/proto/cofd_api.proto +++ b/proto/cofd_api.proto @@ -53,9 +53,11 @@ message SkillUpdate { //Partial update of a single skill dot amount. -message SkillValueUpdate { - string name = 1; - int32 value = 2; +message UpdateSkillValueRequest { + string character_username = 1; + int32 character_id = 2; + string skill_name = 3; + int32 skill_value = 4; } //Partial update of only a skill's specializations. The @@ -68,4 +70,8 @@ message SkillSpecializationsUpdate { //Add a Condition to a Chronicles of Darkness character sheet. message Condition { string name = 1; +} + +service CofdApi { + rpc UpdateSkillValue(UpdateSkillValueRequest) returns (CofdSheet.Skill); } \ No newline at end of file diff --git a/src/models/proto.rs b/src/models/proto.rs index 0a995a4..9e32492 100644 --- a/src/models/proto.rs +++ b/src/models/proto.rs @@ -51,6 +51,7 @@ where } if message_type.as_ref().map(String::as_str) != Some(std::any::type_name::()) { + println!("message type is {:?}", message_type); return Outcome::Forward(data); } diff --git a/src/routes/api.rs b/src/routes/api.rs index 6ca7186..1e6317a 100644 --- a/src/routes/api.rs +++ b/src/routes/api.rs @@ -27,7 +27,7 @@ pub(crate) fn routes() -> Vec { async fn load_character( conn: &TenebrousDbConn<'_>, logged_in_user: Option<&User>, - owner: String, + owner: &str, character_id: i32, ) -> Result { let logged_in_user = logged_in_user.ok_or(Error::NotLoggedIn)?; @@ -37,7 +37,7 @@ async fn load_character( .await? .ok_or(Error::NotFound)?; - if logged_in_user.username != owner { + if &logged_in_user.username != owner { return Err(Error::NoPermission); } @@ -105,7 +105,7 @@ mod cofd { 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 character = load_character(&conn, logged_in_user, &owner, character_id).await?; let mut sheet: CofdSheet = character.try_deserialize()?; match attr_update.name.to_lowercase().as_ref() { @@ -143,7 +143,7 @@ mod cofd { 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 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); @@ -162,24 +162,26 @@ mod cofd { Ok("lol") } - #[patch( - "/cofd///skills", - data = "", - rank = 2 - )] + #[post("/rpc/cofd/update_skill_value", data = "")] pub(super) async fn update_skill_value<'a>( - owner: String, - character_id: i32, - value_update: Proto, + request: 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?; + 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 skill: Option<&mut Skill> = find_skill(&mut sheet, &value_update.name); + let skill: Option<&mut Skill> = find_skill(&mut sheet, &request.skill_name); skill - .map(|s| s.dots = value_update.value) + .map(|s| s.dots = request.skill_value) .ok_or(Error::InvalidInput)?; println!("updated skill value",); diff --git a/static/scripts/api.js b/static/scripts/api.js index d655e3c..545841f 100644 --- a/static/scripts/api.js +++ b/static/scripts/api.js @@ -2,8 +2,8 @@ function makeAPI(root) { //Protobuf types const AttributeType = 'models.proto.cofd.api.Attribute'; const Attribute = root.lookupType(AttributeType); - const SkillValueUpdateType = 'models.proto.cofd.api.SkillValueUpdate'; - const SkillValueUpdate = root.lookupType(SkillValueUpdateType); + const UpdateSkillValueRequestType = 'models.proto.cofd.api.UpdateSkillValueRequest'; + const UpdateSkillValueRequest = root.lookupType(UpdateSkillValueRequestType); const SkillSpecializationUpdateType = 'models.proto.cofd.api.SkillSpecializationsUpdate'; const SkillSpecializationsUpdate = root.lookupType(SkillSpecializationUpdateType); @@ -16,10 +16,16 @@ function makeAPI(root) { const skillResource = (username, characterID, skillName) => '/api/cofd/' + username + '/' + characterID + '/skills'; + function verifyAndCreate(protobufType, payload) { + let err = protobufType.verify(payload); + if (err) throw err; + return protobufType.create(payload); + } + async function updateAttribute(params) { const { username, characterID, attribute, newValue } = params; - let req = Attribute.create({ + let req = verifyAndCreate(Attribute, { name: attribute, value: parseInt(newValue) }); @@ -40,17 +46,17 @@ function makeAPI(root) { async function updateSkillValue(params) { const { username, characterID, skillName, newValue } = params; - let req = SkillValueUpdate.create({ - name: skillName, - value: parseInt(newValue) + let req = verifyAndCreate(UpdateSkillValueRequest, { + characterUsername: username, + characterId: parseInt(characterID), + skillName: skillName, + skillValue: parseInt(newValue) }); - const resource = skillResource(username, characterID); - - let resp = await fetch(resource, { - method: 'PATCH', - headers: { ... protobufContentType(SkillValueUpdateType) }, - body: SkillValueUpdate.encode(req).finish() + let resp = await fetch('/api/rpc/cofd/update_skill_value', { + method: 'POST', + headers: { ... protobufContentType(UpdateSkillValueRequestType) }, + body: UpdateSkillValueRequest.encode(req).finish() }).then(async resp => { console.log("resp is", await resp.text()); }).catch(async err => {