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.
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
@ -69,3 +71,7 @@ message SkillSpecializationsUpdate {
message Condition {
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>()) {
println!("message type is {:?}", message_type);
return Outcome::Forward(data);
}

View File

@ -27,7 +27,7 @@ pub(crate) fn routes() -> Vec<rocket::Route> {
async fn load_character(
conn: &TenebrousDbConn<'_>,
logged_in_user: Option<&User>,
owner: String,
owner: &str,
character_id: i32,
) -> Result<Character, Error> {
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/<owner>/<character_id>/skills",
data = "<value_update>",
rank = 2
)]
#[post("/rpc/cofd/update_skill_value", data = "<request>")]
pub(super) async fn update_skill_value<'a>(
owner: String,
character_id: i32,
value_update: Proto<SkillValueUpdate>,
request: Proto<UpdateSkillValueRequest>,
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",);

View File

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