diff --git a/src/routes/api.rs b/src/routes/api.rs index 17ac5b8..7378e2c 100644 --- a/src/routes/api.rs +++ b/src/routes/api.rs @@ -9,7 +9,7 @@ pub(crate) fn routes() -> Vec { 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 = "")] - pub(super) async fn update_attribute<'a>( + #[post("/rpc/cofd/update_attribute_value", data = "")] + pub(super) async fn update_attribute_value<'a>( req: Proto, conn: TenebrousDbConn<'_>, logged_in_user: Option<&User>, diff --git a/static/scripts/src/api.ts b/static/scripts/src/api.ts index ce7a8e6..352e540 100644 --- a/static/scripts/src/api.ts +++ b/static/scripts/src/api.ts @@ -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(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); +} diff --git a/static/scripts/src/characters/edit.ts b/static/scripts/src/characters/edit.ts index 1b30065..f607ff6 100644 --- a/static/scripts/src/characters/edit.ts +++ b/static/scripts/src/characters/edit.ts @@ -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 | 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; + 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; + if (!input) return; + + console.log("updating skill"); const attribute = input.id; const newValue = parseInt(input.value);