Update skill value as rpc API (though not grpc)

This commit is contained in:
projectmoon 2021-01-02 22:03:10 +00:00
parent ee8f7e58e8
commit f2edd91a72
4 changed files with 45 additions and 30 deletions

View File

@ -53,9 +53,11 @@ message SkillUpdate {
//Partial update of a single skill dot amount. //Partial update of a single skill dot amount.
message SkillValueUpdate { message UpdateSkillValueRequest {
string name = 1; string character_username = 1;
int32 value = 2; int32 character_id = 2;
string skill_name = 3;
int32 skill_value = 4;
} }
//Partial update of only a skill's specializations. The //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. //Add a Condition to a Chronicles of Darkness character sheet.
message Condition { message Condition {
string name = 1; string name = 1;
}
service CofdApi {
rpc UpdateSkillValue(UpdateSkillValueRequest) returns (CofdSheet.Skill);
} }

View File

@ -51,6 +51,7 @@ where
} }
if message_type.as_ref().map(String::as_str) != Some(std::any::type_name::<T>()) { if message_type.as_ref().map(String::as_str) != Some(std::any::type_name::<T>()) {
println!("message type is {:?}", message_type);
return Outcome::Forward(data); return Outcome::Forward(data);
} }

View File

@ -27,7 +27,7 @@ pub(crate) fn routes() -> Vec<rocket::Route> {
async fn load_character( async fn load_character(
conn: &TenebrousDbConn<'_>, conn: &TenebrousDbConn<'_>,
logged_in_user: Option<&User>, logged_in_user: Option<&User>,
owner: String, owner: &str,
character_id: i32, character_id: i32,
) -> Result<Character, Error> { ) -> Result<Character, Error> {
let logged_in_user = logged_in_user.ok_or(Error::NotLoggedIn)?; let logged_in_user = logged_in_user.ok_or(Error::NotLoggedIn)?;
@ -37,7 +37,7 @@ async fn load_character(
.await? .await?
.ok_or(Error::NotFound)?; .ok_or(Error::NotFound)?;
if logged_in_user.username != owner { if &logged_in_user.username != owner {
return Err(Error::NoPermission); return Err(Error::NoPermission);
} }
@ -105,7 +105,7 @@ mod cofd {
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(&conn, logged_in_user, &owner, character_id).await?;
let mut sheet: CofdSheet = character.try_deserialize()?; let mut sheet: CofdSheet = character.try_deserialize()?;
match attr_update.name.to_lowercase().as_ref() { match attr_update.name.to_lowercase().as_ref() {
@ -143,7 +143,7 @@ mod cofd {
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(&conn, logged_in_user, &owner, character_id).await?;
let mut sheet: CofdSheet = character.try_deserialize()?; let mut sheet: CofdSheet = character.try_deserialize()?;
let updated_skill: &Skill = skill_update.skill.as_ref().ok_or(Error::InvalidInput)?; let updated_skill: &Skill = skill_update.skill.as_ref().ok_or(Error::InvalidInput)?;
let skill_entry = find_skill_entry(&mut sheet, &skill_update.name); let skill_entry = find_skill_entry(&mut sheet, &skill_update.name);
@ -162,24 +162,26 @@ mod cofd {
Ok("lol") Ok("lol")
} }
#[patch( #[post("/rpc/cofd/update_skill_value", data = "<request>")]
"/cofd/<owner>/<character_id>/skills",
data = "<value_update>",
rank = 2
)]
pub(super) async fn update_skill_value<'a>( pub(super) async fn update_skill_value<'a>(
owner: String, request: Proto<UpdateSkillValueRequest>,
character_id: i32,
value_update: Proto<SkillValueUpdate>,
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?; 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 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 skill
.map(|s| s.dots = value_update.value) .map(|s| s.dots = request.skill_value)
.ok_or(Error::InvalidInput)?; .ok_or(Error::InvalidInput)?;
println!("updated skill value",); println!("updated skill value",);

View File

@ -2,8 +2,8 @@ function makeAPI(root) {
//Protobuf types //Protobuf types
const AttributeType = 'models.proto.cofd.api.Attribute'; const AttributeType = 'models.proto.cofd.api.Attribute';
const Attribute = root.lookupType(AttributeType); const Attribute = root.lookupType(AttributeType);
const SkillValueUpdateType = 'models.proto.cofd.api.SkillValueUpdate'; const UpdateSkillValueRequestType = 'models.proto.cofd.api.UpdateSkillValueRequest';
const SkillValueUpdate = root.lookupType(SkillValueUpdateType); const UpdateSkillValueRequest = root.lookupType(UpdateSkillValueRequestType);
const SkillSpecializationUpdateType = 'models.proto.cofd.api.SkillSpecializationsUpdate'; const SkillSpecializationUpdateType = 'models.proto.cofd.api.SkillSpecializationsUpdate';
const SkillSpecializationsUpdate = root.lookupType(SkillSpecializationUpdateType); const SkillSpecializationsUpdate = root.lookupType(SkillSpecializationUpdateType);
@ -16,10 +16,16 @@ function makeAPI(root) {
const skillResource = (username, characterID, skillName) => const skillResource = (username, characterID, skillName) =>
'/api/cofd/' + username + '/' + characterID + '/skills'; '/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) { async function updateAttribute(params) {
const { username, characterID, attribute, newValue } = params; const { username, characterID, attribute, newValue } = params;
let req = Attribute.create({ let req = verifyAndCreate(Attribute, {
name: attribute, name: attribute,
value: parseInt(newValue) value: parseInt(newValue)
}); });
@ -40,17 +46,17 @@ function makeAPI(root) {
async function updateSkillValue(params) { async function updateSkillValue(params) {
const { username, characterID, skillName, newValue } = params; const { username, characterID, skillName, newValue } = params;
let req = SkillValueUpdate.create({ let req = verifyAndCreate(UpdateSkillValueRequest, {
name: skillName, characterUsername: username,
value: parseInt(newValue) characterId: parseInt(characterID),
skillName: skillName,
skillValue: parseInt(newValue)
}); });
const resource = skillResource(username, characterID); let resp = await fetch('/api/rpc/cofd/update_skill_value', {
method: 'POST',
let resp = await fetch(resource, { headers: { ... protobufContentType(UpdateSkillValueRequestType) },
method: 'PATCH', body: UpdateSkillValueRequest.encode(req).finish()
headers: { ... protobufContentType(SkillValueUpdateType) },
body: SkillValueUpdate.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 => {