From 59f35d0366a6d2e774a3b39b3e9d24b468c60a4c Mon Sep 17 00:00:00 2001 From: projectmoon Date: Tue, 19 Jan 2021 22:03:07 +0000 Subject: [PATCH] Working, albeit very ugly, inefficient, and sometimes wonky merits. --- src/frontend/scripts/api.ts | 2 +- src/frontend/scripts/characters/edit.ts | 73 ++++++++++++++++--- .../characters/edit_character.html.tera | 13 ++++ .../edit_character_macros.html.tera | 24 ++++-- src/routes/api/cofd.rs | 2 +- 5 files changed, 92 insertions(+), 22 deletions(-) diff --git a/src/frontend/scripts/api.ts b/src/frontend/scripts/api.ts index f76692d..2b09154 100644 --- a/src/frontend/scripts/api.ts +++ b/src/frontend/scripts/api.ts @@ -29,7 +29,7 @@ export async function updateBasicInfo(params: UpdateBasicInfoRequest): Promise { +export async function updateMerits(params: UpdateMeritsRequest): Promise { let data = await makeRequest('/api/rpc/cofd/update_merits', params); return ApiResult.deserializeBinary(data); } diff --git a/src/frontend/scripts/characters/edit.ts b/src/frontend/scripts/characters/edit.ts index 31f52a2..9c7b86c 100644 --- a/src/frontend/scripts/characters/edit.ts +++ b/src/frontend/scripts/characters/edit.ts @@ -97,13 +97,17 @@ import * as api from "../api"; function setupMerits() { async function updateMerits() { + let meritElements = document.querySelectorAll('#merits input[class="merit-name"]'); let merits: CofdSheet.Merit[] = - Array.from(document.querySelectorAll('#merits input[class="merit"]')).map(input => { + Array.from(meritElements).map(input => { const dotsInput = input.parentElement?.querySelector('input[class="merit-dots"]'); + let dotsAmount = dotsInput?.value ?? "0"; + if (dotsAmount.length == 0) { dotsAmount = "0"; } + const merit = new CofdSheet.Merit(); merit.setName(input.value); - merit.setDots(parseInt(dotsInput?.value ?? "0")); + merit.setDots(parseInt(dotsAmount)); return merit; }); @@ -116,18 +120,63 @@ import * as api from "../api"; console.log("got a response back", resp); } - const meritNames = document.querySelectorAll('#merits input[class="merit"]'); - const meritDots = document.querySelectorAll('#merits input[class="merit-dots"]'); + async function removeMerit(event: Event) { + const button = event.target as Option; + const meritLine = button?.parentElement; + if (meritLine) { + document.querySelector("#merit-list")?.removeChild(meritLine); + await updateMerits(); + } + } - meritNames.forEach(input => { - console.log('got an input', input); - input.addEventListener('blur', updateMerits); - }); + function addMeritLine() { + const meritName = document.createElement('input'); + meritName.type = 'text'; + meritName.value = ''; + meritName.classList.add('merit-name'); - meritDots.forEach(input => { - console.log('got an input', input); - input.addEventListener('change', updateMerits); - }); + const meritDots = document.createElement('input'); + meritDots.type = 'number'; + meritDots.min = '0'; + meritDots.value = '0'; + meritDots.classList.add('merit-dots'); + + const removeButton = document.createElement('button'); + removeButton.innerText = "X"; + removeButton.setAttribute('data-event-type', 'remove-merit'); + + const dots = document.createElement('span'); + dots.innerText = 'Dots'; + + const merit = document.createElement('div'); + merit.classList.add('merit'); + merit.appendChild(meritName); + merit.appendChild(meritDots); + merit.appendChild(dots); + merit.appendChild(removeButton); + + document.querySelector("#merit-list")?.appendChild(merit); + } + + async function processControlEvent(event: Event) { + console.log('processing merit control event'); + const element = event.target as Element; + const eventType = element.getAttribute('data-event-type') ?? ''; + + if (eventType == 'remove-merit') { + await removeMerit(event); + } else if (eventType == 'add-merit') { + addMeritLine(); + } + } + + const meritControls = document.querySelector("#merit-controls"); + meritControls?.addEventListener('click', processControlEvent); + + const meritList = document.querySelector("#merit-list"); + meritList?.addEventListener('click', processControlEvent); + meritList?.addEventListener('blur', updateMerits); + meritList?.addEventListener('change', updateMerits); } setupAttributes(); diff --git a/src/frontend/templates/characters/edit_character.html.tera b/src/frontend/templates/characters/edit_character.html.tera index 2a69054..d772545 100644 --- a/src/frontend/templates/characters/edit_character.html.tera +++ b/src/frontend/templates/characters/edit_character.html.tera @@ -156,5 +156,18 @@ {% endfor %} + +
+

Merits

+
+ {% for merit in sheet.merits %} + {{ macros::merit(name=merit.name, value=merit.dots) }} + {% endfor %} +
+ +
+ +
+
{% endblock content %} diff --git a/src/frontend/templates/characters/edit_character_macros.html.tera b/src/frontend/templates/characters/edit_character_macros.html.tera index 903af74..e85b5de 100644 --- a/src/frontend/templates/characters/edit_character_macros.html.tera +++ b/src/frontend/templates/characters/edit_character_macros.html.tera @@ -1,13 +1,21 @@ {% macro attribute(name, value) %} -
- - -
+
+ + +
{% endmacro attribute %} {% macro skill(name, value) %} -
- - -
+
+ + +
{% endmacro skill %} + +{% macro merit(name, value) %} +
+ + Dots + +
+{% endmacro merit %} diff --git a/src/routes/api/cofd.rs b/src/routes/api/cofd.rs index a122527..cd3eed5 100644 --- a/src/routes/api/cofd.rs +++ b/src/routes/api/cofd.rs @@ -163,7 +163,7 @@ pub(super) async fn update_skill_value<'a>( })) } -#[post("/rpc/cofd/update_skill_value", data = "")] +#[post("/rpc/cofd/update_merits", data = "")] pub(super) async fn update_merits<'a>( req: Proto, conn: TenebrousDbConn<'_>,