From 4eee6656d730f19959bd212a48d47fdce6dea675 Mon Sep 17 00:00:00 2001 From: Veilza Date: Sun, 1 Sep 2024 18:46:09 -0500 Subject: [PATCH] Auto-calculate derived XP from Experience History --- display/shared/actors/parts/experience.hbs | 22 +++++++++++++-- lang/de/core-de.json | 2 ++ lang/en/core-en.json | 2 ++ lang/es/core-es.json | 2 ++ lang/fr/core-fr.json | 2 ++ lang/it/core-it.json | 2 ++ lang/pl/core-pl.json | 2 ++ lang/pt-BR/core-pt-BR.json | 2 ++ lang/ru/core-ru.json | 2 ++ lang/template/core-template.json | 2 ++ lang/uk/core-uk.json | 2 ++ system/actor/scripts/experience.js | 32 ++++++++++++++++++++++ system/actor/wod-v5-sheet.js | 7 ++++- template.json | 4 +++ 14 files changed, 82 insertions(+), 3 deletions(-) diff --git a/display/shared/actors/parts/experience.hbs b/display/shared/actors/parts/experience.hbs index da25f5a4..728368f7 100644 --- a/display/shared/actors/parts/experience.hbs +++ b/display/shared/actors/parts/experience.hbs @@ -5,13 +5,31 @@
{{localize "WOD5E.Experience.Remaining"}}
- +
+
+ +
{{localize "WOD5E.Experience.Initial"}}
+
+
+ +
{{localize "WOD5E.Experience.Derived"}}
+
+
{{localize "WOD5E.Experience.Total"}}
- +
+
+ +
{{localize "WOD5E.Experience.Initial"}}
+
+
+ +
{{localize "WOD5E.Experience.Derived"}}
+
+
diff --git a/lang/de/core-de.json b/lang/de/core-de.json index ae6d557c..b3eb4432 100644 --- a/lang/de/core-de.json +++ b/lang/de/core-de.json @@ -123,7 +123,9 @@ }, "Experience": { "_AddExperience": "", + "_Derived": "", "_History": "", + "_Initial": "", "_NewExperience": "", "_Remaining": "", "_Total": "" diff --git a/lang/en/core-en.json b/lang/en/core-en.json index d1e17cf4..d5aa0f7f 100644 --- a/lang/en/core-en.json +++ b/lang/en/core-en.json @@ -123,7 +123,9 @@ }, "Experience": { "AddExperience": "Add Experience", + "Derived": "Derived", "History": "Experience History", + "Initial": "Initial", "NewExperience": "New Experience", "Remaining": "Remaining EXP", "Total": "Total EXP" diff --git a/lang/es/core-es.json b/lang/es/core-es.json index a9f7d444..6b8cc442 100644 --- a/lang/es/core-es.json +++ b/lang/es/core-es.json @@ -123,7 +123,9 @@ }, "Experience": { "_AddExperience": "", + "_Derived": "", "_History": "", + "_Initial": "", "_NewExperience": "", "_Remaining": "", "_Total": "" diff --git a/lang/fr/core-fr.json b/lang/fr/core-fr.json index f1357120..f773bcc1 100644 --- a/lang/fr/core-fr.json +++ b/lang/fr/core-fr.json @@ -123,7 +123,9 @@ }, "Experience": { "_AddExperience": "", + "_Derived": "", "_History": "", + "_Initial": "", "_NewExperience": "", "_Remaining": "", "_Total": "" diff --git a/lang/it/core-it.json b/lang/it/core-it.json index 6391118c..c2a3c08f 100644 --- a/lang/it/core-it.json +++ b/lang/it/core-it.json @@ -123,7 +123,9 @@ }, "Experience": { "_AddExperience": "", + "_Derived": "", "_History": "", + "_Initial": "", "_NewExperience": "", "_Remaining": "", "_Total": "" diff --git a/lang/pl/core-pl.json b/lang/pl/core-pl.json index ede18782..24518833 100644 --- a/lang/pl/core-pl.json +++ b/lang/pl/core-pl.json @@ -123,7 +123,9 @@ }, "Experience": { "_AddExperience": "", + "_Derived": "", "_History": "", + "_Initial": "", "_NewExperience": "", "_Remaining": "", "_Total": "" diff --git a/lang/pt-BR/core-pt-BR.json b/lang/pt-BR/core-pt-BR.json index 17656a52..a6e67490 100644 --- a/lang/pt-BR/core-pt-BR.json +++ b/lang/pt-BR/core-pt-BR.json @@ -123,7 +123,9 @@ }, "Experience": { "_AddExperience": "", + "_Derived": "", "_History": "", + "_Initial": "", "_NewExperience": "", "_Remaining": "", "_Total": "" diff --git a/lang/ru/core-ru.json b/lang/ru/core-ru.json index b7357c68..f4e18052 100644 --- a/lang/ru/core-ru.json +++ b/lang/ru/core-ru.json @@ -123,7 +123,9 @@ }, "Experience": { "_AddExperience": "", + "_Derived": "", "_History": "", + "_Initial": "", "_NewExperience": "", "_Remaining": "", "_Total": "" diff --git a/lang/template/core-template.json b/lang/template/core-template.json index 85ba3762..e03b9cbc 100644 --- a/lang/template/core-template.json +++ b/lang/template/core-template.json @@ -123,7 +123,9 @@ }, "Experience": { "_AddExperience": "", + "_Derived": "", "_History": "", + "_Initial": "", "_NewExperience": "", "_Remaining": "", "_Total": "" diff --git a/lang/uk/core-uk.json b/lang/uk/core-uk.json index d9fd814d..fc72fe4b 100644 --- a/lang/uk/core-uk.json +++ b/lang/uk/core-uk.json @@ -123,7 +123,9 @@ }, "Experience": { "_AddExperience": "", + "_Derived": "", "_History": "", + "_Initial": "", "_NewExperience": "", "_Remaining": "", "_Total": "" diff --git a/system/actor/scripts/experience.js b/system/actor/scripts/experience.js index b70229da..ae06b295 100644 --- a/system/actor/scripts/experience.js +++ b/system/actor/scripts/experience.js @@ -64,3 +64,35 @@ export const _onEditExperience = async function (event, actor) { } */ + +export const _onCalculateDerivedExperience = async function (actor) { + const exp = actor.system.exp + const experiences = actor.system.experiences + + // If there's no experiences to calculate from, just end the statement early + if (!experiences) return + + const { totalXP, spentXP } = experiences.reduce((acc, exp) => { + const value = parseInt(exp.value) + + // If the value is greater than or equal to 0, add it under total XP + if (value >= 0) { + acc.totalXP += value + } else { // Otherwise, track it as spent XP + acc.spentXP += value + } + + return acc + }, { + totalXP: parseInt(exp.max), + spentXP: parseInt(-exp.value) + }) + + const remainingXP = totalXP + spentXP + + // Return the derivedXP values + return { + totalXP, + remainingXP + } +} diff --git a/system/actor/wod-v5-sheet.js b/system/actor/wod-v5-sheet.js index a1918d99..7be7bd83 100644 --- a/system/actor/wod-v5-sheet.js +++ b/system/actor/wod-v5-sheet.js @@ -15,7 +15,7 @@ import { _onResourceChange, _setupDotCounters, _setupSquareCounters, _onDotCount import { _onAddBonus, _onDeleteBonus, _onEditBonus } from './scripts/specialty-bonuses.js' // Various button functions import { _onRollItem } from './scripts/item-roll.js' -import { _onAddExperience } from './scripts/experience.js' +import { _onAddExperience, _onCalculateDerivedExperience } from './scripts/experience.js' /** * Extend the base ActorSheet document and put all our base functionality here @@ -67,6 +67,10 @@ export class WoDActor extends ActorSheet { await _onWillpowerChange(actor) } + if (this.object.type !== 'group' && this.object.type !== 'spc') { + actorData.derivedXP = await _onCalculateDerivedExperience(actor) + } + // Handle attribute preparation const attributesPrep = await prepareAttributes(actor) actorData.attributes = attributesPrep.attributes @@ -298,6 +302,7 @@ export class WoDActor extends ActorSheet { // Willpower Rolls html.find('.willpower-roll').click(this._onWillpowerRoll.bind(this)) + // Toggle whether a field will be shown in Limited view or not html.find('.toggle-limited').click(this._onToggleLimited.bind(this)) } diff --git a/template.json b/template.json index 0c286d8f..91da79c4 100644 --- a/template.json +++ b/template.json @@ -31,6 +31,10 @@ "value": 0, "max": 0 }, + "derivedXP": { + "totalXP": 0, + "remainingXP": 0 + }, "humanity": { "value": 7, "stains": 0