68 lines
2.3 KiB
JavaScript
68 lines
2.3 KiB
JavaScript
import { SYSTEM } from "../config/system.mjs"
|
|
import { CATEGORY } from "../config/skill.mjs"
|
|
export default class LethalFantasySkill extends foundry.abstract.TypeDataModel {
|
|
static defineSchema() {
|
|
const fields = foundry.data.fields
|
|
const schema = {}
|
|
const requiredInteger = { required: true, nullable: false, integer: true }
|
|
|
|
schema.description = new fields.HTMLField({ required: true, textSearch: true })
|
|
schema.category = new fields.StringField({ required: true, initial: "layperson", choices: SYSTEM.SKILL_CATEGORY })
|
|
schema.base = new fields.StringField({ required: true, initial: "WIS" })
|
|
schema.bonus = new fields.NumberField({ ...requiredInteger, required: true, initial: 0, min: 0 })
|
|
schema.cost = new fields.NumberField({ ...requiredInteger,required: true, initial: 0, min: 0 })
|
|
|
|
return schema
|
|
}
|
|
|
|
/** @override */
|
|
static LOCALIZATION_PREFIXES = ["LETHALFANTASY.Skill"]
|
|
|
|
get skillCategory() {
|
|
return game.i18n.localize(CATEGORY[this.category].label)
|
|
}
|
|
|
|
prepareDerivedData() {
|
|
super.prepareDerivedData();
|
|
this.skillTotal = this.computeBase();
|
|
}
|
|
|
|
computeBase() {
|
|
let actor = this.parent?.actor;
|
|
if (!actor) {
|
|
return `${this.base } + ${ String(this.bonus)}`;
|
|
}
|
|
|
|
// Split the base value per stat : WIS,DEX,STR,INT,CHA (example)
|
|
const base = this.base;
|
|
let baseSplit = base.split(",");
|
|
let baseSplitLength = baseSplit.length;
|
|
if ( baseSplitLength > 0) {
|
|
// Select the max stat value from the parent actor
|
|
let maxStat = 0;
|
|
for (let i = 0; i < baseSplitLength; i++) {
|
|
const stat = baseSplit[i];
|
|
const statValue = actor.system.characteristics[stat.toLowerCase()]?.value || 0;
|
|
if (statValue > maxStat) {
|
|
maxStat = statValue;
|
|
}
|
|
}
|
|
return maxStat;
|
|
} else {
|
|
// Split with + calculate the total
|
|
baseSplit = base.split("+");
|
|
baseSplitLength = baseSplit.length;
|
|
if ( baseSplitLength > 0) {
|
|
let total = 0;
|
|
for (let i = 0; i < baseSplitLength; i++) {
|
|
const stat = baseSplit[i];
|
|
const statValue = actor.system.characteristics[stat.toLowerCase()]?.value || 0;
|
|
total += statValue;
|
|
}
|
|
return total
|
|
}
|
|
}
|
|
return `${this.base } + ${ String(this.bonus)}`;
|
|
}
|
|
}
|