tenebrous-sheets/static/scripts/api.js

66 lines
2.3 KiB
JavaScript
Raw Normal View History

function makeAPI(root) {
2021-01-02 14:51:24 +00:00
//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 SkillSpecializationUpdateType = 'models.proto.cofd.api.SkillSpecializationsUpdate';
const SkillSpecializationsUpdate = root.lookupType(SkillSpecializationUpdateType);
const protobufContentType = (messageType) =>
({ 'Content-Type': 'application/x-protobuf; messageType="' + messageType + '"' });
2020-12-27 21:49:08 +00:00
const attributesResource = (username, characterID) =>
'/api/cofd/' + username + '/' + characterID + '/attributes';
2021-01-02 14:51:24 +00:00
const skillResource = (username, characterID, skillName) =>
'/api/cofd/' + username + '/' + characterID + '/skills';
async function updateAttribute(params) {
const { username, characterID, attribute, newValue } = params;
let req = Attribute.create({
name: attribute,
value: parseInt(newValue)
});
2020-12-27 21:49:08 +00:00
const resource = attributesResource(username, characterID);
let resp = await fetch(resource, {
2020-12-27 21:49:08 +00:00
method: 'PATCH',
2021-01-02 14:51:24 +00:00
headers: { ... protobufContentType(AttributeType) },
body: Attribute.encode(req).finish()
}).then(async resp => {
console.log("resp is", await resp.text());
}).catch(async err => {
console.log("err is", err.text());
});
}
2021-01-02 14:51:24 +00:00
async function updateSkillValue(params) {
const { username, characterID, skillName, newValue } = params;
let req = SkillValueUpdate.create({
name: skillName,
value: parseInt(newValue)
});
const resource = skillResource(username, characterID);
let resp = await fetch(resource, {
method: 'PATCH',
headers: { ... protobufContentType(SkillValueUpdateType) },
body: SkillValueUpdate.encode(req).finish()
}).then(async resp => {
console.log("resp is", await resp.text());
}).catch(async err => {
console.log("err is", err.text());
});
}
return {
2021-01-02 14:51:24 +00:00
updateAttribute,
updateSkillValue
};
}