RPC-ify update attribute API.

This commit is contained in:
projectmoon 2021-01-02 22:11:30 +00:00
parent f2edd91a72
commit b042a32f82
3 changed files with 40 additions and 35 deletions

View File

@ -32,9 +32,11 @@ message Attributes {
} }
//Update an attribute's dot amount. TODO rename to AttributesUpdate. //Update an attribute's dot amount. TODO rename to AttributesUpdate.
message Attribute { message UpdateAttributeRequest {
string name = 1; string character_username = 1;
int32 value = 2; int32 character_id = 2;
string attribute_name = 3;
int32 attribute_value = 4;
} }
//Update skill entries in a Chronicles of Darkness character sheet. //Update skill entries in a Chronicles of Darkness character sheet.

View File

@ -97,35 +97,35 @@ mod cofd {
"lol" "lol"
} }
#[patch("/cofd/<owner>/<character_id>/attributes", data = "<attr_update>")] #[post("/rpc/cofd/update_attribute", data = "<req>")]
pub(super) async fn update_attribute<'a>( pub(super) async fn update_attribute<'a>(
owner: String, req: Proto<UpdateAttributeRequest>,
character_id: i32,
attr_update: Proto<Attribute>,
conn: TenebrousDbConn<'_>, conn: TenebrousDbConn<'_>,
logged_in_user: Option<&User>, logged_in_user: Option<&User>,
) -> Result<&'a str, Error> { ) -> Result<&'a str, Error> {
let mut character = load_character(&conn, logged_in_user, &owner, character_id).await?; let mut character = load_character(
let mut sheet: CofdSheet = character.try_deserialize()?; &conn,
logged_in_user,
&req.character_username,
req.character_id,
)
.await?;
match attr_update.name.to_lowercase().as_ref() { let mut sheet: CofdSheet = character.try_deserialize()?;
"strength" => Ok(sheet.strength = attr_update.value), let value = req.attribute_value;
"dexterity" => Ok(sheet.dexterity = attr_update.value), match req.attribute_name.to_lowercase().as_ref() {
"stamina" => Ok(sheet.stamina = attr_update.value), "strength" => Ok(sheet.strength = value),
"intelligence" => Ok(sheet.intelligence = attr_update.value), "dexterity" => Ok(sheet.dexterity = value),
"wits" => Ok(sheet.wits = attr_update.value), "stamina" => Ok(sheet.stamina = value),
"resolve" => Ok(sheet.resolve = attr_update.value), "intelligence" => Ok(sheet.intelligence = value),
"presence" => Ok(sheet.presence = attr_update.value), "wits" => Ok(sheet.wits = value),
"manipulation" => Ok(sheet.manipulation = attr_update.value), "resolve" => Ok(sheet.resolve = value),
"composure" => Ok(sheet.composure = attr_update.value), "presence" => Ok(sheet.presence = value),
"manipulation" => Ok(sheet.manipulation = value),
"composure" => Ok(sheet.composure = value),
_ => Err(Error::InvalidInput), _ => Err(Error::InvalidInput),
}?; }?;
println!(
"updated {} attribute {} to {}",
character.character_name, attr_update.name, attr_update.value
);
character.update_data(sheet)?; character.update_data(sheet)?;
conn.update_character_sheet(&character).await?; conn.update_character_sheet(&character).await?;
Ok("lol") Ok("lol")

View File

@ -1,9 +1,12 @@
function makeAPI(root) { function makeAPI(root) {
//Protobuf types //Protobuf types
const AttributeType = 'models.proto.cofd.api.Attribute'; const UpdateAttributeRequestType = 'models.proto.cofd.api.UpdateAttributeRequest';
const Attribute = root.lookupType(AttributeType); const UpdateAttributeRequest = root.lookupType(UpdateAttributeRequestType);
const UpdateSkillValueRequestType = 'models.proto.cofd.api.UpdateSkillValueRequest'; const UpdateSkillValueRequestType = 'models.proto.cofd.api.UpdateSkillValueRequest';
const UpdateSkillValueRequest = root.lookupType(UpdateSkillValueRequestType); const UpdateSkillValueRequest = root.lookupType(UpdateSkillValueRequestType);
//TODO rpc-ify
const SkillSpecializationUpdateType = 'models.proto.cofd.api.SkillSpecializationsUpdate'; const SkillSpecializationUpdateType = 'models.proto.cofd.api.SkillSpecializationsUpdate';
const SkillSpecializationsUpdate = root.lookupType(SkillSpecializationUpdateType); const SkillSpecializationsUpdate = root.lookupType(SkillSpecializationUpdateType);
@ -25,17 +28,17 @@ function makeAPI(root) {
async function updateAttribute(params) { async function updateAttribute(params) {
const { username, characterID, attribute, newValue } = params; const { username, characterID, attribute, newValue } = params;
let req = verifyAndCreate(Attribute, { let req = verifyAndCreate(UpdateAttributeRequest, {
name: attribute, characterUsername: username,
value: parseInt(newValue) characterId: parseInt(characterID),
attributeName: attribute,
attributeValue: parseInt(newValue)
}); });
const resource = attributesResource(username, characterID); let resp = await fetch('/api/rpc/cofd/update_attribute', {
method: 'POST',
let resp = await fetch(resource, { headers: { ... protobufContentType(UpdateAttributeRequestType) },
method: 'PATCH', body: UpdateAttributeRequest.encode(req).finish()
headers: { ... protobufContentType(AttributeType) },
body: Attribute.encode(req).finish()
}).then(async resp => { }).then(async resp => {
console.log("resp is", await resp.text()); console.log("resp is", await resp.text());
}).catch(async err => { }).catch(async err => {