Skip to content

Commit

Permalink
#13
Browse files Browse the repository at this point in the history
- Add setting to show equation for numerical changes.
  • Loading branch information
Larkinabout committed May 8, 2024
1 parent b54b22f commit bb72916
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 4 deletions.
2 changes: 2 additions & 0 deletions languages/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
"changeLog.settings.playerPropertiesForm.name": "Changes for Player",
"changeLog.settings.playerPropertiesForm.label": "Changes for Player",
"changeLog.settings.playerPropertiesForm.hint": "Changes to log for the player",
"changeLog.settings.showEquation.name": "Show Equation",
"changeLog.settings.showEquation.hint": "For numerical changes, choose whether to show the equation on the chat card",
"changeLog.settings.showRecipients.name": "Show Recipients",
"changeLog.settings.showRecipients.hint": "Choose whether to show recipients on the chat card",
"changeLog.settings.showSender.name": "Show Sender",
Expand Down
36 changes: 32 additions & 4 deletions src/change-log.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ export class ChangeLog {
this.getGmActorTypes(),
this.getGmProperties(),
this.getPlayerProperties(),
this.getShowEquation(),
this.getShowRecipients(),
this.getShowSender()
])
Expand All @@ -124,6 +125,10 @@ export class ChangeLog {
this.playerProperties = await Utils.getSetting('playerProperties')?.split(DELIMITER) ?? []
}

async getShowEquation () {
this.showEquation = await Utils.getSetting('showEquation')
}

async getShowRecipients () {
this.showRecipients = await Utils.getSetting('showRecipients')
}
Expand Down Expand Up @@ -199,6 +204,10 @@ export class ChangeLog {

if (!isEveryone && !isGm && !isPlayer) continue

const oldValue = Utils.getValueByDotNotation(doc, key)
const newValue = Utils.getValueByDotNotation(data, key)
const { sign, adjustmentValue } = this.#getAdjustment(oldValue, newValue)

const templateData = {
tokenId: token?.id || null,
actorId: actor?.id || null,
Expand All @@ -208,8 +217,10 @@ export class ChangeLog {
documentType,
key,
modifiedByName,
oldValue: Utils.getValueByDotNotation(doc, key),
newValue: Utils.getValueByDotNotation(data, key)
oldValue,
newValue,
sign,
adjustmentValue
}

const whisperData = {
Expand Down Expand Up @@ -249,6 +260,8 @@ export class ChangeLog {

if (!isEveryone && !isGm && !isPlayer) continue

const { sign, adjustmentValue } = this.#getAdjustment(oldValue, newValue)

const templateData = {
tokenId: token?.id || null,
actorId: actor?.id || null,
Expand All @@ -259,7 +272,9 @@ export class ChangeLog {
key,
modifiedByName,
oldValue,
newValue
newValue,
sign,
adjustmentValue
}

const whisperData = {
Expand Down Expand Up @@ -309,6 +324,14 @@ export class ChangeLog {
this.#createChatMessage('delete', templateData, whisperData)
}

#getAdjustment (oldValue, newValue) {
if (!this.showEquation) return null
const difference = (Number.isInteger(oldValue) && Number.isInteger(newValue)) ? oldValue - newValue : null
const sign = (Number.isInteger(difference)) ? (difference < 0) ? 'fa-plus' : 'fa-minus' : null
const adjustmentValue = (Number.isInteger(difference)) ? Math.abs(difference) : null
return { sign, adjustmentValue }
}

#getAudience (documentType, actorType, key) {
return {
isEveryone: (this.everyoneActorTypes.includes(actorType) && this.everyoneProperties.includes(`${documentType}.${key}`)),
Expand All @@ -334,7 +357,10 @@ export class ChangeLog {
}

async #createChatMessage (changeType, templateData, whisperData) {
const { tokenId, actorId, documentId, document1Name, document2Name, documentType, key, oldValue, newValue, modifiedByName } = templateData
const {
tokenId, actorId, documentId, document1Name, document2Name, documentType,
key, oldValue, newValue, sign, adjustmentValue, modifiedByName
} = templateData
const { actor, isEveryone, isGm, isPlayer } = whisperData

if (!this.#isValidChange({ oldValue, newValue })) return
Expand All @@ -347,6 +373,8 @@ export class ChangeLog {
propertyName: Utils.getPropertyName(`${documentType}.${key}`),
oldValue: Utils.getPropertyValue(oldValue),
newValue: Utils.getPropertyValue(newValue),
sign: sign || 'fa-arrow-right',
adjustmentValue,
tooltip: `<div>${game.i18n.localize('changeLog.modifiedBy')}: ${modifiedByName}</div>`
}
)
Expand Down
10 changes: 10 additions & 0 deletions src/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ export const registerSettings = function () {
scope: 'world'
})

game.settings.register(MODULE.ID, 'showEquation', {
name: game.i18n.localize('changeLog.settings.showEquation.name'),
hint: game.i18n.localize('changeLog.settings.showEquation.hint'),
scope: 'world',
config: true,
type: Boolean,
default: false,
onChange: () => { game.changeLog.getShowEquation() }
})

game.settings.register(MODULE.ID, 'showSender', {
name: game.i18n.localize('changeLog.settings.showSender.name'),
hint: game.i18n.localize('changeLog.settings.showSender.hint'),
Expand Down
6 changes: 6 additions & 0 deletions styles/change-log.css
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@
justify-content: center;
}

.change-log-card-adjustment-value {
display: flex;
flex: 1;
justify-content: center;
}

.change-log-card-new-value {
display: flex;
flex: 1;
Expand Down
4 changes: 4 additions & 0 deletions templates/chat-card.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
<div class="change-log-card-values">
{{#if oldValue includeZero=true}}
<div class="change-log-card-old-value">{{{oldValue}}}</div>
<i class="fa {{sign}}"></i>
{{/if}}
{{#if adjustmentValue includeZero=true}}
<div class="change-log-card-adjustment-value">{{{adjustmentValue}}}</div>
<i class="fa fa-arrow-right"></i>
{{/if}}
{{#if newValue includeZero=true}}<div class="change-log-card-new-value">{{{newValue}}}</div>{{/if}}
Expand Down

0 comments on commit bb72916

Please sign in to comment.