Work on custom checks

This commit is contained in:
Johannes Loher 2021-04-25 18:35:29 +02:00
parent 3d999c4cf9
commit 26e22ed3cf
5 changed files with 42 additions and 1 deletions

View file

@ -23,6 +23,7 @@ interface DS4ActorPreparedDataDataBase {
combatValues: DS4ActorPreparedDataDataCombatValues;
rolling: DS4ActorPreparedDataDataRolling;
checks: DS4ActorPreparedDataDataChecks;
customChecks: DS4ActorPreparedDataDataCustomChecks;
}
interface DS4ActorPreparedDataDataAttributes {
@ -66,6 +67,8 @@ type DS4ActorPreparedDataDataChecks = {
[key in Check]: number;
};
type DS4ActorPreparedDataDataCustomChecks = Record<string, number>;
// types
interface DS4CharacterPreparedDataData extends DS4ActorPreparedDataDataBase {

View file

@ -110,6 +110,7 @@ export class DS4Actor extends Actor<DS4ActorData, DS4Item, DS4ActorPreparedData>
prepareDerivedData(): void {
this._prepareCombatValues();
this._prepareChecks();
this._prepareCustomChecks();
}
/**
@ -122,7 +123,10 @@ export class DS4Actor extends Actor<DS4ActorData, DS4Item, DS4ActorPreparedData>
const checkProperties = Object.keys(DS4.i18n.checks)
.filter((check) => check !== "defend")
.map((check) => `data.checks.${check}`);
return combatValueProperties.concat(checkProperties);
const customCheckProperties = Object.keys(game.settings.get("ds4", "customChecks")).map(
(check) => `data.customChecks.${check}`,
);
return combatValueProperties.concat(checkProperties).concat(customCheckProperties);
}
/**
@ -251,6 +255,25 @@ export class DS4Actor extends Actor<DS4ActorData, DS4Item, DS4ActorPreparedData>
};
}
protected _prepareCustomChecks(): void {
const customChecksSettings = game.settings.get("ds4", "customChecks");
const data = this.data.data;
data.customChecks = {};
Object.entries(customChecksSettings).forEach(([identifier, check]) => {
const replacedFormula = this._replaceFormulaData(check.formula);
console.log(replacedFormula);
data.customChecks[identifier] = Roll.MATH_PROXY.safeEval(replacedFormula);
});
}
protected _replaceFormulaData(formula: string): string {
const dataRgx = new RegExp(/@([a-z.0-9_\-]+)/gi);
return formula.replace(dataRgx, (match, term) => {
const value = getProperty(this.data, term);
return value !== undefined ? String(value).trim() : match;
});
}
/**
* Handle how changes to a Token attribute bar are applied to the Actor.
* This only differs from the base implementation by also allowing negative values.

View file

@ -2,5 +2,6 @@ declare namespace ClientSettings {
interface Values {
"ds4.systemMigrationVersion": number;
"ds4.useSlayingDiceForAutomatedChecks": boolean;
"ds4.customChecks": Record<string, { label: string; formula: string }>;
}
}

View file

@ -18,4 +18,14 @@ export function registerSystemSettings(): void {
type: Boolean,
default: false,
});
game.settings.register("ds4", "customChecks", {
name: "DS4.SettingsCustomChecks",
scope: "world",
config: false,
// eslint-disable-next-line
//@ts-ignore
type: Object,
default: {},
});
}

View file

@ -3,4 +3,8 @@
{{> systems/ds4/templates/sheets/actor/components/check.hbs check-key=check-key check-target-number=(lookup
../data.checks check-key) check-label=check-label}}
{{/each}}
{{#each data.customChecks as |check-target-number check-key|}}
{{> systems/ds4/templates/sheets/actor/components/check.hbs check-key=check-key
check-target-number=check-target-number check-label=check-key}}
{{/each}}
</div>