2020-12-27 21:03:10 +00:00
|
|
|
function makeAPI(root) {
|
2021-01-02 14:51:24 +00:00
|
|
|
//Protobuf types
|
2021-01-02 22:11:30 +00:00
|
|
|
const UpdateAttributeRequestType = 'models.proto.cofd.api.UpdateAttributeRequest';
|
|
|
|
const UpdateAttributeRequest = root.lookupType(UpdateAttributeRequestType);
|
|
|
|
|
2021-01-02 22:03:10 +00:00
|
|
|
const UpdateSkillValueRequestType = 'models.proto.cofd.api.UpdateSkillValueRequest';
|
|
|
|
const UpdateSkillValueRequest = root.lookupType(UpdateSkillValueRequestType);
|
2021-01-02 22:11:30 +00:00
|
|
|
|
|
|
|
//TODO rpc-ify
|
2021-01-02 14:51:24 +00:00
|
|
|
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:03:10 +00:00
|
|
|
|
2021-01-02 22:03:10 +00:00
|
|
|
function verifyAndCreate(protobufType, payload) {
|
|
|
|
let err = protobufType.verify(payload);
|
|
|
|
if (err) throw err;
|
|
|
|
return protobufType.create(payload);
|
|
|
|
}
|
|
|
|
|
2020-12-27 21:03:10 +00:00
|
|
|
async function updateAttribute(params) {
|
|
|
|
const { username, characterID, attribute, newValue } = params;
|
|
|
|
|
2021-01-02 22:11:30 +00:00
|
|
|
let req = verifyAndCreate(UpdateAttributeRequest, {
|
|
|
|
characterUsername: username,
|
|
|
|
characterId: parseInt(characterID),
|
|
|
|
attributeName: attribute,
|
|
|
|
attributeValue: parseInt(newValue)
|
2020-12-27 21:03:10 +00:00
|
|
|
});
|
|
|
|
|
2021-01-02 22:11:30 +00:00
|
|
|
let resp = await fetch('/api/rpc/cofd/update_attribute', {
|
|
|
|
method: 'POST',
|
|
|
|
headers: { ... protobufContentType(UpdateAttributeRequestType) },
|
|
|
|
body: UpdateAttributeRequest.encode(req).finish()
|
2020-12-27 21:03:10 +00:00
|
|
|
}).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;
|
|
|
|
|
2021-01-02 22:03:10 +00:00
|
|
|
let req = verifyAndCreate(UpdateSkillValueRequest, {
|
|
|
|
characterUsername: username,
|
|
|
|
characterId: parseInt(characterID),
|
|
|
|
skillName: skillName,
|
|
|
|
skillValue: parseInt(newValue)
|
2021-01-02 14:51:24 +00:00
|
|
|
});
|
|
|
|
|
2021-01-02 22:03:10 +00:00
|
|
|
let resp = await fetch('/api/rpc/cofd/update_skill_value', {
|
|
|
|
method: 'POST',
|
|
|
|
headers: { ... protobufContentType(UpdateSkillValueRequestType) },
|
|
|
|
body: UpdateSkillValueRequest.encode(req).finish()
|
2021-01-02 14:51:24 +00:00
|
|
|
}).then(async resp => {
|
|
|
|
console.log("resp is", await resp.text());
|
|
|
|
}).catch(async err => {
|
|
|
|
console.log("err is", err.text());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-12-27 21:03:10 +00:00
|
|
|
return {
|
2021-01-02 14:51:24 +00:00
|
|
|
updateAttribute,
|
|
|
|
updateSkillValue
|
2020-12-27 21:03:10 +00:00
|
|
|
};
|
|
|
|
}
|