Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
- #133: Add api functions for counters.
  • Loading branch information
Larkinabout committed Oct 1, 2024
1 parent 0d89860 commit d45ffa7
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 8 deletions.
79 changes: 73 additions & 6 deletions scripts/counters.js
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,17 @@ export function uncheckCheckbox (entity, counterKey) {
entity.setFlag(MODULE.ID, counterKey, false)
}

/**
* Toggle checkbox counter
* @param {object} entity The entity: actor or item
* @param {string} counterKey The counter key
*/
export function toggleCheckbox (entity, counterKey) {
counterKey = (counterKey.startsWith('counters.')) ? `${counterKey}.value` : counterKey
const flag = entity.getFlag(MODULE.ID, counterKey)
entity.setFlag(MODULE.ID, counterKey, !flag)
}

/**
* Decrease fraction counter
* @param {object} entity The entity: actor or item
Expand All @@ -446,13 +457,26 @@ export function decreaseFraction (entity, counterKey, actionValue = 1) {
* @param {number} actionValue The action value
*/
export function increaseFraction (entity, counterKey, actionValue = 1) {
modifyFraction(entity, counterKey, actionValue)
}

/**
* Modify fraction counter
* @param {object} entity The entity: actor or item
* @param {string} counterKey The counter key
* @param {number} actionValue The action value
*/
export function modifyFraction (entity, counterKey, actionValue = 1) {
const oldValue = entity.getFlag(MODULE.ID, `${counterKey}.value`) ?? 0
const maxValue = getMax(entity, counterKey) ?? entity.getFlag(MODULE.ID, `${counterKey}.max`)
const newValue = oldValue + actionValue

if (!maxValue || newValue <= maxValue) {
if (newValue >= 0 && (!maxValue || newValue <= maxValue)) {
entity.setFlag(MODULE.ID, `${counterKey}.value`, newValue)
} else {
if (newValue >= 0 && oldValue < maxValue) {
entity.setFlag(MODULE.ID, `${counterKey}.value`, maxValue)
}
ui.notifications.info(game.i18n.localize('CUSTOM_DND5E.reachedCounterLimit'))
}
}
Expand All @@ -478,15 +502,28 @@ export function decreaseNumber (entity, counterKey, actionValue = 1) {
* @param {number} actionValue The action value
*/
export function increaseNumber (entity, counterKey, actionValue = 1) {
modifyNumber(entity, counterKey, actionValue)
}

/**
* Modify number counter
* @param {object} entity The entity: actor or item
* @param {string} counterKey The counter key
* @param {number} actionValue The action value
*/
export function modifyNumber (entity, counterKey, actionValue = 1) {
const originalKey = counterKey
counterKey = (counterKey.startsWith('counters.')) ? `${counterKey}.value` : counterKey
const oldValue = entity.getFlag(MODULE.ID, counterKey) ?? 0
const maxValue = getMax(entity, counterKey) ?? entity.getFlag(MODULE.ID, `${originalKey}.max`)
const newValue = oldValue + actionValue

if (!maxValue || newValue <= maxValue) {
if (newValue >= 0 && (!maxValue || newValue <= maxValue)) {
entity.setFlag(MODULE.ID, counterKey, newValue)
} else {
if (newValue >= 0 && oldValue < maxValue) {
entity.setFlag(MODULE.ID, counterKey, maxValue)
}
ui.notifications.info(game.i18n.localize('CUSTOM_DND5E.reachedCounterLimit'))
}
}
Expand All @@ -512,17 +549,32 @@ export function decreaseSuccess (entity, counterKey, actionValue = 1) {
* @param {number} actionValue The action value
*/
export function increaseSuccess (entity, counterKey, actionValue = 1) {
modifySuccess(entity, counterKey, actionValue)
}

/**
* Modify success on success/failure counter
* @param {object} entity The entity: actor or item
* @param {string} counterKey The counter key
* @param {number} actionValue The action value
*/
export function modifySuccess (entity, counterKey, actionValue = 1) {
const oldValue = entity.getFlag(MODULE.ID, `${counterKey}.success`) ?? 0
const maxValue = getMax(entity, counterKey) ?? entity.getFlag(MODULE.ID, `${counterKey}.max`)
const newValue = (maxValue) ? Math.min(oldValue + actionValue, maxValue) : oldValue + actionValue

if (!maxValue || newValue <= maxValue) {
if (newValue >= 0 && (!maxValue || newValue <= maxValue)) {
entity.setFlag(MODULE.ID, `${counterKey}.success`, newValue)
} else {
if (newValue >= 0 && oldValue < maxValue) {
entity.setFlag(MODULE.ID, `${counterKey}.success`, maxValue)
}
ui.notifications.info(game.i18n.localize('CUSTOM_DND5E.reachedCounterLimit'))
}
}

/**
* Decrease failure on successFailure counter
* Decrease failure on success/failure counter
* @param {object} entity The entity: actor or item
* @param {string} counterKey The counter key
* @param {number} actionValue The action value
Expand All @@ -536,18 +588,33 @@ export function decreaseFailure (entity, counterKey, actionValue = 1) {
}

/**
* Increase failure on successFailure counter
* Increase failure on success/failure counter
* @param {object} entity The entity: actor or item
* @param {string} counterKey The counter key
* @param {number} actionValue The action value
*/
export function increaseFailure (entity, counterKey, actionValue = 1) {
modifyFailure(entity, counterKey, actionValue)
}

/**
* Modify failure on success/failure counter
* @param {object} entity The entity: actor or item
* @param {string} counterKey The counter key
* @param {number} actionValue The action value
*/
export function modifyFailure (entity, counterKey, actionValue = 1) {
const oldValue = entity.getFlag(MODULE.ID, `${counterKey}.failure`) ?? 0
const maxValue = getMax(entity, counterKey) || entity.getFlag(MODULE.ID, `${counterKey}.max`)
const newValue = (maxValue) ? Math.min(oldValue + actionValue, maxValue) : oldValue + actionValue

if (!maxValue || newValue <= maxValue) {
if (newValue >= 0 && (!maxValue || newValue <= maxValue)) {
entity.setFlag(MODULE.ID, `${counterKey}.failure`, newValue)
} else {
if (newValue >= 0 && oldValue < maxValue) {
entity.setFlag(MODULE.ID, `${counterKey}.failure`, maxValue)
}
ui.notifications.info(game.i18n.localize('CUSTOM_DND5E.reachedCounterLimit'))
}
}

Expand Down
42 changes: 40 additions & 2 deletions scripts/module.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CONSTANTS } from './constants.js'
import { CONSTANTS, MODULE } from './constants.js'
import { Logger, getSetting, registerSetting } from './utils.js'
import { register as registerHouseRules, registerNegativeHp } from './house-rules.js'
import { register as registerAbilities, setConfig as setAbilities } from './abilities.js'
Expand All @@ -10,7 +10,24 @@ import { register as registerArmorProficiencies, setConfig as setArmorProficienc
import { register as registerCampSupplies } from './camp-supplies.js'
import { register as registerConditions, setConfig as setConditions } from './conditions.js'
import { register as registerConsumableTypes, setConfig as setConsumableTypes } from './consumable-types.js'
import { register as registerCounters } from './counters.js'
import {
register as registerCounters,
checkCheckbox,
uncheckCheckbox,
toggleCheckbox,
increaseFraction,
decreaseFraction,
modifyFraction,
increaseNumber,
decreaseNumber,
modifyNumber,
increaseSuccess,
decreaseSuccess,
modifySuccess,
increaseFailure,
decreaseFailure,
modifyFailure
} from './counters.js'
import { register as registerCurrency, setConfig as setCurrency } from './currency.js'
import { register as registerDamageTypes, setConfig as setDamageTypes } from './damage-types.js'
import { register as registerDebug } from './debug.js'
Expand Down Expand Up @@ -43,6 +60,27 @@ Hooks.on('init', async () => {
CONFIG.CUSTOM_DND5E = foundry.utils.deepClone(CONFIG.DND5E)
CONFIG.CUSTOM_DND5E.coreStatusEffects = foundry.utils.deepClone(CONFIG.statusEffects)

const module = game.modules.get(MODULE.ID)
module.api = {
counters: {
checkCheckbox,
uncheckCheckbox,
toggleCheckbox,
increaseFraction,
decreaseFraction,
modifyFraction,
increaseNumber,
decreaseNumber,
modifyNumber,
increaseSuccess,
decreaseSuccess,
modifySuccess,
increaseFailure,
decreaseFailure,
modifyFailure
}
}

registerSetting(
CONSTANTS.DEBUG.SETTING.KEY,
{
Expand Down

0 comments on commit d45ffa7

Please sign in to comment.