Skip to content

Commit

Permalink
Auto-calculate derived XP from Experience History
Browse files Browse the repository at this point in the history
  • Loading branch information
Veilza committed Sep 1, 2024
1 parent 9d36191 commit 4eee665
Show file tree
Hide file tree
Showing 14 changed files with 82 additions and 3 deletions.
22 changes: 20 additions & 2 deletions display/shared/actors/parts/experience.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,31 @@
<div class="field-header">
{{localize "WOD5E.Experience.Remaining"}}
</div>
<input name="system.exp.value" value="{{actor.system.exp.value}}"/>
<div class="flexrow">
<div class="flexcol">
<input name="system.exp.value" value="{{actor.system.exp.value}}"/>
<div>{{localize "WOD5E.Experience.Initial"}}</div>
</div>
<div class="flexcol">
<input name="system.derivedXP.remainingXP" value="{{actor.system.derivedXP.remainingXP}}" disabled/>
<div>{{localize "WOD5E.Experience.Derived"}}</div>
</div>
</div>
</div>
<div class="tracker flexcol">
<div class="field-header">
{{localize "WOD5E.Experience.Total"}}
</div>
<input name="system.exp.max" value="{{actor.system.exp.max}}"/>
<div class="flexrow">
<div class="flexcol">
<input name="system.exp.max" value="{{actor.system.exp.max}}"/>
<div>{{localize "WOD5E.Experience.Initial"}}</div>
</div>
<div class="flexcol">
<input name="system.derivedXP.totalXP" value="{{actor.system.derivedXP.totalXP}}" disabled/>
<div>{{localize "WOD5E.Experience.Derived"}}</div>
</div>
</div>
</div>
</div>
<div class="flexcol">
Expand Down
2 changes: 2 additions & 0 deletions lang/de/core-de.json
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@
},
"Experience": {
"_AddExperience": "",
"_Derived": "",
"_History": "",
"_Initial": "",
"_NewExperience": "",
"_Remaining": "",
"_Total": ""
Expand Down
2 changes: 2 additions & 0 deletions lang/en/core-en.json
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@
},
"Experience": {
"AddExperience": "Add Experience",
"Derived": "Derived",
"History": "Experience History",
"Initial": "Initial",
"NewExperience": "New Experience",
"Remaining": "Remaining EXP",
"Total": "Total EXP"
Expand Down
2 changes: 2 additions & 0 deletions lang/es/core-es.json
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@
},
"Experience": {
"_AddExperience": "",
"_Derived": "",
"_History": "",
"_Initial": "",
"_NewExperience": "",
"_Remaining": "",
"_Total": ""
Expand Down
2 changes: 2 additions & 0 deletions lang/fr/core-fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@
},
"Experience": {
"_AddExperience": "",
"_Derived": "",
"_History": "",
"_Initial": "",
"_NewExperience": "",
"_Remaining": "",
"_Total": ""
Expand Down
2 changes: 2 additions & 0 deletions lang/it/core-it.json
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@
},
"Experience": {
"_AddExperience": "",
"_Derived": "",
"_History": "",
"_Initial": "",
"_NewExperience": "",
"_Remaining": "",
"_Total": ""
Expand Down
2 changes: 2 additions & 0 deletions lang/pl/core-pl.json
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@
},
"Experience": {
"_AddExperience": "",
"_Derived": "",
"_History": "",
"_Initial": "",
"_NewExperience": "",
"_Remaining": "",
"_Total": ""
Expand Down
2 changes: 2 additions & 0 deletions lang/pt-BR/core-pt-BR.json
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@
},
"Experience": {
"_AddExperience": "",
"_Derived": "",
"_History": "",
"_Initial": "",
"_NewExperience": "",
"_Remaining": "",
"_Total": ""
Expand Down
2 changes: 2 additions & 0 deletions lang/ru/core-ru.json
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@
},
"Experience": {
"_AddExperience": "",
"_Derived": "",
"_History": "",
"_Initial": "",
"_NewExperience": "",
"_Remaining": "",
"_Total": ""
Expand Down
2 changes: 2 additions & 0 deletions lang/template/core-template.json
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@
},
"Experience": {
"_AddExperience": "",
"_Derived": "",
"_History": "",
"_Initial": "",
"_NewExperience": "",
"_Remaining": "",
"_Total": ""
Expand Down
2 changes: 2 additions & 0 deletions lang/uk/core-uk.json
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@
},
"Experience": {
"_AddExperience": "",
"_Derived": "",
"_History": "",
"_Initial": "",
"_NewExperience": "",
"_Remaining": "",
"_Total": ""
Expand Down
32 changes: 32 additions & 0 deletions system/actor/scripts/experience.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
7 changes: 6 additions & 1 deletion system/actor/wod-v5-sheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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))
}

Expand Down
4 changes: 4 additions & 0 deletions template.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
"value": 0,
"max": 0
},
"derivedXP": {
"totalXP": 0,
"remainingXP": 0
},
"humanity": {
"value": 7,
"stains": 0
Expand Down

0 comments on commit 4eee665

Please sign in to comment.