Skip to content

Commit

Permalink
Refactor enrichers for performance and monsters
Browse files Browse the repository at this point in the history
  • Loading branch information
MrPrimate committed Oct 30, 2024
1 parent dbae20d commit 0e2ebd3
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 38 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
- Find Familiar would not add summons if Legacy naming suffix in use.
- Fix Maneuvering Attack parsing.
- Override items would retain activities from the DDB Importer item.
- Monster Parser improvements for upcoming changes.
- Ranger Parsing improvements.
- Improvement to spell and item parsing notifications.

# 6.0.22

Expand Down
112 changes: 86 additions & 26 deletions src/parser/enrichers/DDBBaseEnricher.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@ export default class DDBBaseEnricher {

DOCUMENT_STUB = {};

EXTERNAL_ENRICHERS = {};
ENRICHERS = {};

static _loadDataStub(stub) {
return utils.isFunction(stub) ? stub() : stub;
}

_loadEnricherData(name) {
if (this.EXTERNAL_ENRICHERS?.[name]) {
const ExternalEnricher = DDBBaseEnricher._loadDataStub(this.EXTERNAL_ENRICHERS?.[name]);
if (this.ENRICHERS?.[name]) {
const ExternalEnricher = DDBBaseEnricher._loadDataStub(this.ENRICHERS?.[name]);
return new ExternalEnricher({
ddbEnricher: this,
});
Expand All @@ -86,16 +86,18 @@ export default class DDBBaseEnricher {

_getEnricherMatchesV2() {

const loadedMatch = this._loadEnricherData(this.hintName);
const loadedEnricher = this._loadEnricherData(this.hintName);

if (!loadedMatch) return null;
if (!loadedEnricher) return;

this.loadedEnricher = loadedEnricher;

this.activity = loadedMatch.activity;
this.effect = loadedMatch.effect;
this.override = loadedMatch.override;
this.additionalActivities = loadedMatch.additionalActivities;
this.documentStub = loadedMatch.documentStub;
return true;
// this.activity = loadedMatch.activity;
// this.effect = loadedMatch.effect;
// this.override = loadedMatch.override;
// this.additionalActivities = loadedMatch.additionalActivities;
// this.documentStub = loadedMatch.documentStub;
// return true;
}

_findEnricherMatch(type) {
Expand All @@ -120,22 +122,69 @@ export default class DDBBaseEnricher {
// lookupName
}

_getEnricherMatchesV1() {
this.activity = this._findEnricherMatch("ACTIVITY_HINTS");
this.effect = this._findEnricherMatch("EFFECT_HINTS");
this.override = this._findEnricherMatch("DOCUMENT_OVERRIDES");
this.additionalActivities = this._findEnricherMatch("ADDITIONAL_ACTIVITIES");
this.documentStub = this._findEnricherMatch("DOCUMENT_STUB");
}
// _getEnricherMatchesV1() {
// this.activity = this._findEnricherMatch("ACTIVITY_HINTS");
// this.effect = this._findEnricherMatch("EFFECT_HINTS");
// this.override = this._findEnricherMatch("DOCUMENT_OVERRIDES");
// this.additionalActivities = this._findEnricherMatch("ADDITIONAL_ACTIVITIES");
// this.documentStub = this._findEnricherMatch("DOCUMENT_STUB");
// }

_prepare() {
if (this.isCustomAction) return;
this.hintName = (this.is2014 ? this.DND_2014.NAME_HINTS[this.name] : null)
?? this.NAME_HINTS[this.name]
?? this.name;

const loadV2 = this._getEnricherMatchesV2();
if (!loadV2) this._getEnricherMatchesV1();
this._getEnricherMatchesV2();
}

get type() {
if (this.loadedEnricher) {
return this.loadedEnricher.type;
} else {
return null;
}
}

get activity() {
if (this.loadedEnricher) {
return this.loadedEnricher.activity;
} else {
return this._findEnricherMatch("ACTIVITY_HINTS");
}
}

get effect() {
if (this.loadedEnricher) {
return this.loadedEnricher.effect;
} else {
return this._findEnricherMatch("EFFECT_HINTS");
}
}

get override() {
if (this.loadedEnricher) {
return this.loadedEnricher.override;
} else {
return this._findEnricherMatch("DOCUMENT_OVERRIDES");
}
}

get additionalActivities() {
if (this.loadedEnricher) {
return this.loadedEnricher.additionalActivities;
} else {
return this._findEnricherMatch("ADDITIONAL_ACTIVITIES");
}
}

get documentStub() {
if (this.loadedEnricher) {
return this.loadedEnricher.documentStub;
} else {
return this._findEnricherMatch("DOCUMENT_STUB");
}
}

constructor() {
Expand All @@ -150,6 +199,8 @@ export default class DDBBaseEnricher {
this.effectType = "basic";
this.enricherType = "general";
this.manager = null;
this.loadedEnricher = null;
this._originalActivity = null;
}

load({ ddbParser, document, name = null, is2014 = null } = {}) {
Expand Down Expand Up @@ -177,6 +228,14 @@ export default class DDBBaseEnricher {
else if (this.document) this.document = data;
}

get originalActivity() {
return this._originalActivity;
}

set originalActivity(activity) {
this._originalActivity = activity;
}

// eslint-disable-next-line complexity
_applyActivityDataOverride(activity, overrideData) {
if (overrideData.name) activity.name = overrideData.name;
Expand Down Expand Up @@ -355,14 +414,15 @@ export default class DDBBaseEnricher {
return activity;
}

applyActivityOverride(activity) {
if (!this.activity) return activity;
applyActivityOverride(activityData) {
this.originalActivity = activityData;
if (!this.activity) return activityData;

let activityHint = utils.isFunction(this.activity) ? this.activity() : this.activity;

if (!activityHint) return activity;
if (!activityHint) return activityData;

return this._applyActivityDataOverride(activity, activityHint);
return this._applyActivityDataOverride(activityData, activityHint);
}

// eslint-disable-next-line complexity
Expand Down Expand Up @@ -562,9 +622,8 @@ export default class DDBBaseEnricher {
activities: {},
effects: [],
};
if (!this.ddbParser?.ddbData?.ddbCharacter) return result;
if (!this.ddbParser?.ddbCharacter) return result;
const actions = this.ddbParser.ddbCharacter._characterFeatureFactory.getActions({ name, type });
// console.warn("actions", { name, type, actions, this: this });
if (actions.length === 0) return result;
const actionFeatures = actions.map((action) => {
return this.ddbParser.ddbCharacter._characterFeatureFactory.getFeatureFromAction({
Expand Down Expand Up @@ -625,6 +684,7 @@ export default class DDBBaseEnricher {
// console.warn("Activity", activity);

if (activityHint.overrides) {
this.originalActivity = activity;
activity = this._applyActivityDataOverride(activity, activityHint.overrides);
}

Expand Down
21 changes: 12 additions & 9 deletions src/parser/enrichers/DDBEnricherMixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ export default class DDBEnricherMixin {
return uses;
}

_buildDamagePartsWithBase() {
const original = this.ddbEnricher.originalActivity;

const base = foundry.utils.deepClone(this.data.system.damage.base);
const parts = foundry.utils.deepClone(original?.damage.parts ?? []);
return [base, ...parts];
}

static effectModules = effectModules;

static generateATLChange = generateATLChange;
Expand Down Expand Up @@ -89,6 +97,10 @@ export default class DDBEnricherMixin {
this.manager = ddbEnricher.manager;
}

get type() {
return null;
}

get data() {
return this.ddbEnricher.ddbParser.data;
}
Expand All @@ -114,12 +126,3 @@ export default class DDBEnricherMixin {
}

}

// DDBEnricherMixin.prototype.effectModules = effectModules;
// DDBEnricherMixin.prototype.generateATLChange = generateATLChange;
// DDBEnricherMixin.prototype.generateCustomChange = generateCustomChange;
// DDBEnricherMixin.prototype.generateDowngradeChange = generateDowngradeChange;
// DDBEnricherMixin.prototype.generateOverrideChange = generateOverrideChange;
// DDBEnricherMixin.prototype.generateSignedAddChange = generateSignedAddChange;
// DDBEnricherMixin.prototype.generateUnsignedAddChange = generateUnsignedAddChange;
// DDBEnricherMixin.prototype.generateUpgradeChange = generateUpgradeChange;
6 changes: 5 additions & 1 deletion src/parser/enrichers/DDBFeatureEnricher.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ import SoulOfVengeance from "./feature/SoulOfVengeance.js";
import PrimalCompanion from "./feature/PrimalCompanion.js";
import PrimalCompanionSummon from "./feature/PrimalCompanionSummon.js";
import PrimalCompanionRestoreBeast from "./feature/PrimalCompanionRestoreBeast.js";
import Tireless from "./feature/Tireless.js";
import TemporaryHitPoints from "./feature/TemporaryHitPoints.js";

export default class DDBFeatureEnricher extends DDBBaseEnricher {
constructor() {
Expand All @@ -49,7 +51,7 @@ export default class DDBFeatureEnricher extends DDBBaseEnricher {
this._prepare();
}

EXTERNAL_ENRICHERS = {
ENRICHERS = {
"Abjure Foes": () => AbjureFoes,
"Arcane Recovery": () => ArcaneRecovery,
"Archdruid": () => Archdruid,
Expand Down Expand Up @@ -81,6 +83,8 @@ export default class DDBFeatureEnricher extends DDBBaseEnricher {
"Primal Companion": () => PrimalCompanion,
"Primal Companion: Summon": () => PrimalCompanionSummon,
"Primal Companion: Restore Beast": () => PrimalCompanionRestoreBeast,
"Tireless": () => Tireless,
"Temporary Hit Points": () => TemporaryHitPoints,
};

DND_2014 = {
Expand Down
6 changes: 5 additions & 1 deletion src/parser/enrichers/general/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ import DDBEnricherMixin from "../DDBEnricherMixin.js";

export default class Example extends DDBEnricherMixin {

get type() {
return "utility"; // activity type - if type is none, activity hit will be generally undefined
}

get activity() {
return {
// THESE ARE ACTIvITY ADJUSTMENTS RAN EVERYWHERE
name: "Activity Name", // if not type default"
type: "utility", // activity type - if type is none, activity hit will be generally undefined
type: "utility", // deprecated, use type() activity type - if type is none, activity hit will be generally undefined
parent: "name", // name of lookup parent if only applies to certain types, e.g. spells attached to items, this would be the item name
noConsumeTargets: true, // remove any auto generated consumption targets
addItemConsume: true, // add item consume
Expand Down
2 changes: 2 additions & 0 deletions src/parser/features/DDBAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ export default class DDBAction extends DDBBaseFeature {
"Hunter's Mark",
"Primal Companion: Restore Beast",
"Primal Companion: Summon",
"Hunter's Mark: Precise Hunter",
"Temporary Hit Points",
];

static SKIPPED_ACTIONS_STARTSWITH = [
Expand Down
2 changes: 1 addition & 1 deletion src/parser/features/DDBBaseFeature.js
Original file line number Diff line number Diff line change
Expand Up @@ -1160,7 +1160,7 @@ export default class DDBBaseFeature {
);

const activity = this.getActivity({
typeOverride: typeOverride ?? this.enricher.activity?.type ?? this.activityType,
typeOverride: typeOverride ?? this.enricher.type ?? this.enricher.activity?.type ?? this.activityType,
name,
nameIdPostfix,
}, options);
Expand Down

0 comments on commit 0e2ebd3

Please sign in to comment.