Skip to content

Commit

Permalink
Recode of the definition files for better maintenance in the future
Browse files Browse the repository at this point in the history
  • Loading branch information
Veilza committed Aug 29, 2024
1 parent a123442 commit 29a473b
Show file tree
Hide file tree
Showing 24 changed files with 141 additions and 345 deletions.
2 changes: 1 addition & 1 deletion system/actor/htr/scripts/edges.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const _onAddEdge = async function (event) {

// Secondary variables
const selectLabel = game.i18n.localize('WOD5E.HTR.SelectEdge')
const itemOptions = WOD5E.Edges.getList()
const itemOptions = WOD5E.Edges.getList({})

// Variables yet to be defined
let options = []
Expand Down
2 changes: 1 addition & 1 deletion system/actor/scripts/experience.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

export const _onAddExperience = async function (actor) {
// Define the actor's gamesystem, defaulting to "mortal" if it's not in the systems list
const system = actor.system.gamesystem in WOD5E.Systems.getList() ? actor.system.gamesystem : 'mortal'
const system = actor.system.gamesystem in WOD5E.Systems.getList({}) ? actor.system.gamesystem : 'mortal'

// Render the template
const experienceTemplate = 'systems/vtm5e/display/shared/actors/parts/experience-display.hbs'
Expand Down
2 changes: 1 addition & 1 deletion system/actor/scripts/item-roll.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export const _onRollItem = async function (event) {
advancedDice = disableAdvancedDice ? 0 : await WOD5E.api.getAdvancedDice({ actor })

// Define the actor's gamesystem, defaulting to "mortal" if it's not in the systems list
const system = actorData.gamesystem in WOD5E.Systems.getList() ? actorData.gamesystem : 'mortal'
const system = actorData.gamesystem in WOD5E.Systems.getList({}) ? actorData.gamesystem : 'mortal'

// Some quick modifications to vampire and werewolf rolls
// in order to properly display the dice in the dialog window
Expand Down
2 changes: 1 addition & 1 deletion system/actor/scripts/roll.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export const _onConfirmRoll = async function (dataset, actor) {
}

// Define the actor's gamesystem, defaulting to "mortal" if it's not in the systems list
const system = actor.system.gamesystem in WOD5E.Systems.getList() ? actor.system.gamesystem : 'mortal'
const system = actor.system.gamesystem in WOD5E.Systems.getList({}) ? actor.system.gamesystem : 'mortal'

// Some quick modifications to vampire and werewolf rolls
// in order to properly display the dice in the dialog window
Expand Down
6 changes: 3 additions & 3 deletions system/actor/scripts/specialty-bonuses.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const _onAddBonus = async function (event, actor, data, SkillEditDialog)
const bonusPath = header.dataset.bonusPath

// Define the actor's gamesystem, defaulting to "mortal" if it's not in the systems list
const system = actor.system.gamesystem in WOD5E.Systems.getList() ? actor.system.gamesystem : 'mortal'
const system = actor.system.gamesystem in WOD5E.Systems.getList({}) ? actor.system.gamesystem : 'mortal'

// Default values for a new specialty
const bonusData = {
Expand Down Expand Up @@ -98,7 +98,7 @@ export const _onDeleteBonus = async function (event, actor, data, SkillEditDialo
const bonusPath = header.dataset.bonusPath

// Define the actor's gamesystem, defaulting to "mortal" if it's not in the systems list
const system = actor.system.gamesystem in WOD5E.Systems.getList() ? actor.system.gamesystem : 'mortal'
const system = actor.system.gamesystem in WOD5E.Systems.getList({}) ? actor.system.gamesystem : 'mortal'

// Define the existing list of bonuses
const bonusKeys = bonusPath.split('.')
Expand Down Expand Up @@ -133,7 +133,7 @@ export const _onEditBonus = async function (event, actor, data, SkillEditDialog)
}

// Define the actor's gamesystem, defaulting to "mortal" if it's not in the systems list
const system = actor.system.gamesystem in WOD5E.Systems.getList() ? actor.system.gamesystem : 'mortal'
const system = actor.system.gamesystem in WOD5E.Systems.getList({}) ? actor.system.gamesystem : 'mortal'

// Render the template
const bonusTemplate = 'systems/vtm5e/display/shared/actors/parts/specialty-display.hbs'
Expand Down
8 changes: 4 additions & 4 deletions system/actor/spc-actor-sheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ export class SPCActorSheet extends WoDActor {
}

// Define the actor's gamesystem, defaulting to "mortal" if it's not in the systems list
const system = actor.system.gamesystem in WOD5E.Systems.getList() ? actor.system.gamesystem : 'mortal'
const system = actor.system.gamesystem in WOD5E.Systems.getList({}) ? actor.system.gamesystem : 'mortal'

// Display the dialog
new Dialog({
Expand Down Expand Up @@ -287,15 +287,15 @@ export class SPCActorSheet extends WoDActor {
titleLabel = game.i18n.localize('WOD5E.VTM.AddDiscipline')
label = game.i18n.localize('WOD5E.VTM.SelectDiscipline')
} else if (powerType === 'gift') {
const giftsList = WOD5E.Gifts.getList()
const giftsList = WOD5E.Gifts.getList({})
for (const [key, value] of Object.entries(giftsList)) {
options = options.concat(`<option value="${key}">${value.displayName}</option>`)
}

titleLabel = game.i18n.localize('WOD5E.WTA.AddGift')
label = game.i18n.localize('WOD5E.WTA.SelectGift')
} else if (powerType === 'edge') {
const edgesList = WOD5E.Edges.getList()
const edgesList = WOD5E.Edges.getList({})
for (const [key, value] of Object.entries(edgesList)) {
options = options.concat(`<option value="${key}">${value.displayName}</option>`)
}
Expand Down Expand Up @@ -333,7 +333,7 @@ export class SPCActorSheet extends WoDActor {
}

// Define the actor's gamesystem, defaulting to "mortal" if it's not in the systems list
const system = actor.system.gamesystem in WOD5E.Systems.getList() ? actor.system.gamesystem : 'mortal'
const system = actor.system.gamesystem in WOD5E.Systems.getList({}) ? actor.system.gamesystem : 'mortal'

// Display the dialog
new Dialog({
Expand Down
16 changes: 8 additions & 8 deletions system/actor/wod-v5-sheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ export class WoDActor extends ActorSheet {
equipment[i.system.equipmentType].push(i)
} else if (i.type === 'feature') {
// Check the featuretype field and set a default
const featuretype = i.system.featuretype in WOD5E.Features.getList() ? i.system.featuretype : 'background'
const featuretype = i.system.featuretype in WOD5E.Features.getList({}) ? i.system.featuretype : 'background'

// Append to features
features[featuretype].push(i)
Expand Down Expand Up @@ -237,7 +237,7 @@ export class WoDActor extends ActorSheet {
const item = actor.getEmbeddedDocument('Item', li.data('itemId'))

// Define the actor's gamesystem, defaulting to "mortal" if it's not in the systems list
const system = actor.system.gamesystem in WOD5E.Systems.getList() ? actor.system.gamesystem : 'mortal'
const system = actor.system.gamesystem in WOD5E.Systems.getList({}) ? actor.system.gamesystem : 'mortal'

// Variables yet to be defined
let buttons = {}
Expand Down Expand Up @@ -336,7 +336,7 @@ export class WoDActor extends ActorSheet {
const skill = header.dataset.skill

// Define the actor's gamesystem, defaulting to "mortal" if it's not in the systems list
const system = actor.system.gamesystem in WOD5E.Systems.getList() ? actor.system.gamesystem : 'mortal'
const system = actor.system.gamesystem in WOD5E.Systems.getList({}) ? actor.system.gamesystem : 'mortal'

// Render selecting a skill/attribute to roll
const skillTemplate = 'systems/vtm5e/display/shared/actors/parts/skill-dialog.hbs'
Expand Down Expand Up @@ -464,7 +464,7 @@ export class WoDActor extends ActorSheet {
// Top-level variables
const actor = this.actor
const dataset = event.currentTarget.dataset
const itemsList = WOD5E.ItemTypes.getList()
const itemsList = WOD5E.ItemTypes.getList({})
const type = dataset.type

// Variables to be defined later
Expand All @@ -476,7 +476,7 @@ export class WoDActor extends ActorSheet {
let options = ''

// Define the actor's gamesystem, defaulting to "mortal" if it's not in the systems list
const system = actor.system.gamesystem in WOD5E.Systems.getList() ? actor.system.gamesystem : 'mortal'
const system = actor.system.gamesystem in WOD5E.Systems.getList({}) ? actor.system.gamesystem : 'mortal'

// Generate the item name
itemName = subtype ? WOD5E.api.generateLabelAndLocalize({ string: subtype, type }) : itemsList[type].label
Expand All @@ -490,12 +490,12 @@ export class WoDActor extends ActorSheet {
break
case 'perk':
selectLabel = game.i18n.localize('WOD5E.HTR.SelectEdge')
itemOptions = WOD5E.Edges.getList()
itemOptions = WOD5E.Edges.getList({})
itemName = game.i18n.format('WOD5E.HTR.NewStringPerk', { string: itemName })
break
case 'gift':
selectLabel = game.i18n.localize('WOD5E.WTA.SelectGift')
itemOptions = WOD5E.Gifts.getList()
itemOptions = WOD5E.Gifts.getList({})

if (subtype && subtype === 'rite') {
itemName = game.i18n.format('WOD5E.NewString', { string: itemName })
Expand All @@ -508,7 +508,7 @@ export class WoDActor extends ActorSheet {
break
case 'feature':
selectLabel = game.i18n.localize('WOD5E.ItemsList.SelectFeature')
itemOptions = WOD5E.Features.getList()
itemOptions = WOD5E.Features.getList({})
itemName = game.i18n.format('WOD5E.NewString', { string: itemName })
break
default:
Expand Down
2 changes: 1 addition & 1 deletion system/actor/wta/scripts/gifts.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const _onAddGift = async function (event) {

// Secondary variables
const selectLabel = game.i18n.localize('WOD5E.WTA.SelectGift')
const itemOptions = WOD5E.Gifts.getList()
const itemOptions = WOD5E.Gifts.getList({})

// Variables yet to be defined
let options = []
Expand Down
32 changes: 3 additions & 29 deletions system/api/def/actortypes.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* global game, Hooks */
/* global Hooks */

// Mortal
import { MortalActorSheet } from '../../actor/mortal-actor-sheet.js'
Expand All @@ -12,35 +12,9 @@ import { WerewolfActorSheet } from '../../actor/wta/werewolf-actor-sheet.js'
// All systems
import { SPCActorSheet } from '../../actor/spc-actor-sheet.js'
import { GroupActorSheet } from '../../actor/group-actor-sheet.js'
import { BaseDefinitionClass } from './base-definition-class.js'

export class ActorTypes {
// Function to help with quickly grabbing all the listed values;
// Will only retrieve objects (definitions)
static getList () {
return Object.entries(this)
.filter(([, value]) => typeof value === 'object' && value !== null && !Array.isArray(value))
.reduce((accumulator, [key, value]) => {
accumulator[key] = value
return accumulator
}, {})
}

// Localize the labels
static initializeLabels () {
for (const [, value] of Object.entries(this)) {
if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
value.label = game.i18n.localize(value.label)
}

// Handle which label to display
if (value.rename) {
value.displayName = value.rename
} else {
value.displayName = value.label
}
}
}

export class ActorTypes extends BaseDefinitionClass {
// Run any necessary compilation on ready
static onReady () {
ActorTypes.initializeLabels()
Expand Down
64 changes: 4 additions & 60 deletions system/api/def/attributes.js
Original file line number Diff line number Diff line change
@@ -1,66 +1,10 @@
/* global game, Hooks */

export class Attributes {
// Function to help with quickly grabbing all the listed values;
// Will only retrieve objects (definitions)
// Optional string can be provided to filter by type
static getList ({
type = '',
custom = false
}) {
return Object.entries(this)
// Filter out any entries with improper formats
.filter(([, value]) => typeof value === 'object' && value !== null && !Array.isArray(value) &&
// Filter based on given filters provided with the function, if any
(!type || value.type === type) && (!custom || value.custom === custom))
// Reduce into a format the system can work with
.reduce((accumulator, [key, value]) => {
accumulator[key] = value
return accumulator
}, {})
}

// Method to add extra attributes
static addCustom (customAttributes) {
for (const [, value] of Object.entries(customAttributes)) {
if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
// Note this feature as being a custom feature
value.custom = true

this[value.id] = value
}
}
}

// Localize the labels
static initializeLabels () {
const modifications = game.settings.get('vtm5e', 'modifiedAttributes')
import { BaseDefinitionClass } from './base-definition-class.js'

for (const [key, value] of Object.entries(this)) {
if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
const checkModification = modifications.filter(attribute => attribute.id === key)

value.label = game.i18n.localize(value.label)

// If there are modifications, update the attribute
if (checkModification.length > 0) {
value.rename = checkModification[0].rename
value.hidden = checkModification[0].hidden
} else {
// If there are no modifications, use default values
value.rename = ''
value.hidden = false
}
}

// Handle which label to display
if (value.rename) {
value.displayName = value.rename
} else {
value.displayName = value.label
}
}
}
export class Attributes extends BaseDefinitionClass {
static modsEnabled = true
static defCategory = 'Attributes'

// Run any necessary compilation on ready
static onReady () {
Expand Down
77 changes: 77 additions & 0 deletions system/api/def/base-definition-class.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/* global game */

export class BaseDefinitionClass {
static modsEnabled = false
static defCategory = ''

// Function to help with quickly grabbing all the listed values
// Will only retrieve objects (definitions)
static getList ({
type = '',
custom = false
}) {
return Object.entries(this)
// Filter out any entries with improper formats
.filter(([, value]) => typeof value === 'object' && value !== null && !Array.isArray(value) &&
// Filter based on given filters provided with the function, if any
(!type || value.type === type) && (!custom || value.custom === custom))
// Reduce into a format the system can work with
.reduce((accumulator, [key, value]) => {
accumulator[key] = value
return accumulator
}, {})
}

// Localize the labels
static initializeLabels () {
let modifications = []

// Check if modifications are enabled
if (this.modsEnabled && this.defCategory) {
modifications = game.settings.get('vtm5e', `modified${this.defCategory}`)
}

// Cycle through each entry in the definition file to initialize the labels on each
// Quickly filter out any non-object, non-null, non-array values
const definitionEntries = Object.entries(this).filter(([, value]) => typeof value === 'object' && value !== null && !Array.isArray(value))
for (const [key, value] of definitionEntries) {
if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
// If there are no modifications, use default values
value.rename = ''
value.hidden = false

// If mods are enabled, check for a modification to the definition
if (this.modsEnabled) {
const checkModification = modifications.filter(definition => definition.id === key)

// If there are modifications, update the value's properties
if (checkModification.length > 0) {
value.rename = checkModification[0].rename
value.hidden = checkModification[0].hidden
}
}

value.label = game.i18n.localize(value.label)
}

// Handle which label to display
if (value.rename) {
value.displayName = value.rename
} else {
value.displayName = value.label
}
}
}

// Method to add extra definitions to a category
static addCustom (customDefinitions) {
for (const [, value] of Object.entries(customDefinitions)) {
if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
// Note this definition as being custom
value.custom = true

this[value.id] = value
}
}
}
}
Loading

0 comments on commit 29a473b

Please sign in to comment.