Reimplement attribute updtaes

This commit is contained in:
projectmoon 2021-01-05 19:32:03 +00:00
parent 63571d9711
commit 8f1ddf89dd
3 changed files with 39 additions and 28 deletions

View File

@ -9,7 +9,7 @@ pub(crate) fn routes() -> Vec<rocket::Route> {
routes![
cofd::update_basic_info,
cofd::update_attributes,
cofd::update_attribute,
cofd::update_attribute_value,
cofd::update_skills,
cofd::update_skill_value,
cofd::add_condition,
@ -94,8 +94,8 @@ mod cofd {
"lol"
}
#[post("/rpc/cofd/update_attribute", data = "<req>")]
pub(super) async fn update_attribute<'a>(
#[post("/rpc/cofd/update_attribute_value", data = "<req>")]
pub(super) async fn update_attribute_value<'a>(
req: Proto<UpdateAttributeRequest>,
conn: TenebrousDbConn<'_>,
logged_in_user: Option<&User>,

View File

@ -1,9 +1,10 @@
import { UpdateSkillValueRequest } from "../_proto/cofd_api_pb";
import * as jspb from "google-protobuf";
import { UpdateAttributeRequest, UpdateSkillValueRequest } from "../_proto/cofd_api_pb";
const PROTOBUF_CONTENT_TYPE = { 'Content-Type': 'application/x-protobuf' };
export async function updateSkillValue(params: UpdateSkillValueRequest) {
let resp = await fetch('/api/rpc/cofd/update_skill_value', {
async function makeRequest<T extends jspb.Message>(uri: string, params: T) {
let resp = await fetch(uri, {
method: 'POST',
headers: { ...PROTOBUF_CONTENT_TYPE },
body: params.serializeBinary()
@ -13,3 +14,11 @@ export async function updateSkillValue(params: UpdateSkillValueRequest) {
console.log("err is", err.text());
});
}
export async function updateSkillValue(params: UpdateSkillValueRequest) {
await makeRequest('/api/rpc/cofd/update_skill_value', params);
}
export async function updateAttributeValue(params: UpdateAttributeRequest) {
await makeRequest('/api/rpc/cofd/update_attribute_value', params);
}

View File

@ -1,7 +1,8 @@
import { UpdateSkillValueRequest } from "../../_proto/cofd_api_pb";
import { UpdateSkillValueRequest, UpdateAttributeRequest } from "../../_proto/cofd_api_pb";
import * as api from "../api";
(async () => {
type Option<T> = T | null | undefined;
//TODO start refactoring these into a separate script, and make API calls
//take all necessary info (e.g. username and character ID, plus other stuff)
//as object params.
@ -9,36 +10,37 @@ import * as api from "../api";
const [, , USERNAME, CHARACTER_ID] = window.location.pathname.split('/');
//const api = makeAPI(root);
//console.log("api is", api);
function setupAttributes() {
// const attributeInputs = document.querySelectorAll('#skills input[type="number"]');
const attributeInputs = document.querySelectorAll('#attributes input[type="number"]');
// async function skillValueHandler(event: Event) {
// const input = event.target as HTMLInputElement;
// console.log("updating attr");
// const attribute = input.id;
// const newValue = parseInt(input.value);
// const params = new UpdateSkillValueRequest();
// params.setCharacterUsername(USERNAME);
// params.setCharacterId(parseInt(CHARACTER_ID));
// params.setSkillName(attribute);
// params.setSkillValue(newValue);
// await api.updateSkillValue(params);
// }
async function attributeHandler(event: Event) {
const input = event.target as Option<HTMLInputElement>;
if (!input) return;
// Array.from(attributeInputs).forEach(input => {
// input.addEventListener('change', skillValueHandler);
// });
console.log("updating attr");
const attribute = input.id;
const newValue = parseInt(input.value);
const params = new UpdateAttributeRequest();
params.setCharacterUsername(USERNAME);
params.setCharacterId(parseInt(CHARACTER_ID));
params.setAttributeName(attribute);
params.setAttributeValue(newValue);
await api.updateAttributeValue(params);
}
Array.from(attributeInputs).forEach(input => {
input.addEventListener('change', attributeHandler);
});
}
function setupSkills() {
const skillInputs = document.querySelectorAll('#skills input[type="number"]');
async function skillValueHandler(event: Event) {
const input = event.target as HTMLInputElement;
console.log("updating attr");
const input = event.target as Option<HTMLInputElement>;
if (!input) return;
console.log("updating skill");
const attribute = input.id;
const newValue = parseInt(input.value);