Skip to content

Commit

Permalink
Add advantage mode store.
Browse files Browse the repository at this point in the history
  • Loading branch information
krbz999 committed Dec 28, 2024
1 parent 0d51509 commit 54f81e8
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 20 deletions.
76 changes: 56 additions & 20 deletions module/data/fields/advantage-mode-field.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -22,43 +22,79 @@ export default class AdvantageModeField extends foundry.data.fields.NumberField
/* -------------------------------------------- */

/**
* Number of advantage sources.
* @type {number}
* Advantage sources.
* @type {Map<string, Map<string, number>>}
*/
#advantage;
static #advantages = new Map();

/* -------------------------------------------- */

/**
* Number of disadvantage sources.
* @type {number}
* Disadvantage sources.
* @type {Map<string, Map<string, number>>}
*/
#disadvantage;
static #disadvantages = new Map();

/* -------------------------------------------- */

/** @inheritDoc */
initialize(value, model, options={}) {
this.#advantage = Number(value === 1);
this.#disadvantage = Number(value === -1);
return value;
/**
* Add one to the count of advantages or disadvantages.
* @param {Actor5e} actor The actor gaining a source of advantage or disadvantage.
* @param {string} path The path to the property affected.
* @param {boolean} [isAdvantage] Is this an instance of advantage or disadvantage?
*/
static addSource(actor, path, isAdvantage=true) {
const store = isAdvantage ? this.#advantages : this.#disadvantages;
let map = store.get(actor.uuid);
if ( !map ) {
store.set(actor.uuid, new Map());
map = store.get(actor.uuid);
}
map.set(path, (map.get(path) ?? 0) + 1);
}

/* -------------------------------------------- */

/**
* Clear out any tracked values for an actor.
* @param {Actor5e} actor The actor whose stored data to clear.
*/
static clearSources(actor) {
this.#advantages.delete(actor.uuid);
this.#disadvantages.delete(actor.uuid);
}

/* -------------------------------------------- */

/**
* Retrieve the advantage mode of a given property.
* @param {Actor5e} actor The actor whose mode to determine.
* @param {string} path The path to the property affected.
* @returns {number} The advantage mode.
*/
static getMode(actor, path) {
const advantages = this.#advantages.get(actor.uuid)?.get(path) ?? 0;
const disadvantages = this.#disadvantages.get(actor.uuid)?.get(path) ?? 0;
return Math.sign(advantages) - Math.sign(disadvantages);
}

/* -------------------------------------------- */

// /** @inheritDoc */
// initialize(value, model, options={}) {
// // Not functional due to no known fieldPath.
// if ( [-1, 1].includes(value) ) AdvantageModeField.addSource(model.parent, this.fieldPath, value === 1);
// return value;
// }

/* -------------------------------------------- */
/* Active Effect Integration */
/* -------------------------------------------- */

/** @override */
_applyChangeAdd(value, delta, model, change) {
switch ( delta ) {
case 1:
this.#advantage++;
break;
case -1:
this.#disadvantage++;
break;
}
return Math.sign(this.#advantage) - Math.sign(this.#disadvantage);
if ( [-1, 1].includes(delta) ) AdvantageModeField.addSource(model, change.key, delta === 1);
return AdvantageModeField.getMode(model, change.key);
}

/* -------------------------------------------- */
Expand Down
2 changes: 2 additions & 0 deletions module/documents/actor/actor.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import SystemDocumentMixin from "../mixins/document.mjs";
import Proficiency from "./proficiency.mjs";
import SelectChoices from "./select-choices.mjs";
import * as Trait from "./trait.mjs";
import AdvantageModeField from "../../data/fields/advantage-mode-field.mjs";

/**
* Extend the base Actor class to implement additional system-specific logic.
Expand Down Expand Up @@ -193,6 +194,7 @@ export default class Actor5e extends SystemDocumentMixin(Actor) {

/** @inheritDoc */
prepareData() {
AdvantageModeField.clearSources(this);
if ( this.system.modelProvider !== dnd5e ) return super.prepareData();
this._clearCachedValues();
this._preparationWarnings = [];
Expand Down

0 comments on commit 54f81e8

Please sign in to comment.