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.
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.

View File

@ -97,35 +97,35 @@ mod cofd {
"lol"
}
#[patch("/cofd/<owner>/<character_id>/attributes", data = "<attr_update>")]
#[post("/rpc/cofd/update_attribute", data = "<req>")]
pub(super) async fn update_attribute<'a>(
owner: String,
character_id: i32,
attr_update: Proto<Attribute>,
req: Proto<UpdateAttributeRequest>,
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")

View File

@ -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 => {