Ah, the classic developer's problem. What the heck does this other developer's code do? This however, is a variation on that theme, as I know what the code does. How the heck does it do it? I have this function that I believe GiGs wrote for me. It adds up sets of enabled values from a repeating section. It works great, but I want to expand its functionality. The problem is that it is fairly dense and full of syntax I'm not yet familiar with. I've been studying it and googling heavily, but I could use some clarification. I realize there's a bit of a learning curve for me here. If someone could just describe the data structures that are being used and how they are being loaded, that would be great. I understand concepts well. If this were C++ I could juggle data structures with the best of them, but this javascript syntax is still a bit foreign to me. It looks like arrays are being loaded with custom data structures. The purpose of this post is just to learn to read the worker as it is currently written. However, I thought it might be helpful if I describe what it is I'm trying to do. I'm not asking for this function to be rewritten for me, but if you did, I couldn't really object. What I really want though, is to learn what it does, so I can rewrite it myself. I mean, I know what it does. I just don't know how it does it. So repeating_effects is loaded with "effects". Each "effect" has a name, a checkbox called "toggle" that records whether the "effect" is enabled, and a set of 45 key value pairs, called "modifiers". Each "modifier" has a label and a numerical value. The 45 "modifier" labels are the same for each "effect", but their numerical values can be different. What I'd like to do is: Add another checkbox to each "effect" called "hidden" that records whether the "effect" is shown or hidden. Then I'd like to is go through all the "effects" and make a list of only those "effects" where the "toggle" checkbox is enabled, and the "hidden" checkbox is disabled, meaning that these effects are both enabled, and shown. Let's call these selected effects. Then I want to create an array of 45 empty arrays of strings, one for each "modifier" in an "effect". Then I want to go through each selected "effect", one "modifier" at a time. If a selected "effect" has a modifier value other than 0, I want to add the name of that "effect" in string form, to the array of strings associated with that "modifier". So say that the first of the 45 arrays of strings is the array associated with the "physicalstrength" modifier. Once this step is complete, that array would contain the names of each selected "effect" that has a non-zero value for the "physicalstrength" modifier. And all 45 would be filled in this way for their associated "modifiers". Now, inside the set of 45 modifiers, there are some modifiers that ultimately stand alone, and some that are grouped together. The next step would be to combine the arrays for those modifiers which are grouped together. This will take the number of arrays from 45 to 31. Then I want to remove any duplicates in an individual array, that were created by combining arrays. Then I'll take each of the remaining arrays, and concatenate it into a single space separated string. This way I'll go from 31 arrays of strings, to 31 strings. Then I'll run through the remaining array and store each of those 31 strings in its own attribute. This way, when each of the 31 rolls are made, I can include a list of all the "effects" that modified that roll. You know what? If someone could just go through the function line by line and write out what it is doing, like I did with the 8 steps I just listed, that might be all I need to learn to read this function. Also, I've spent months working on this character sheet, and I've asked a lot of questions. I've had a lot of help, but I keep wondering what if anything is expected from me in return for this help. Are there people who are paid by Roll20 to answer questions on these forums? Is that what I'm contributing, a monthly subscription fee? Do my documented questions and their answers help others? Or is there some social contract I'm unaware of and failing to fulfill? // add up effects modifiers
const modifiers = [
'physicalstrength', 'mentalstrength', 'mystictalent', 'meleedamage', 'rangeddamage',
'initiative', 'air', 'entropy', 'mental', 'physicalendurance',
'mentalendurance', 'mysticpower', 'meleeresistancepenalty', 'rangedresistancepenalty', 'mysticresistancepenalty',
'earth', 'bludgeoning', 'divine', 'agility', 'personality',
'defense', 'melee', 'ranged', 'mystic', 'fire',
'piercing', 'infernal', 'observation', 'abilitymagicpoints', 'defensemagicpoints',
'meleemagicpoints', 'rangedmagicpoints', 'spellmagicpoints', 'water', 'slashing',
'checks', 'actionpoint', 'abilityactionpoints', 'defenseactionpoints', 'meleeactionpoints',
'rangedactionpoints', 'spellactionpoints', 'animus', 'biological', 'skillrolls'
];
const section_field = (section, field, id = ':') => `repeating_${section}${id === ':' ? id : `_${id}_`}${field}`;
const buildChanges = modifiers.reduce((total,item) => `${total} change:${section_field('effects', item)}_modifier`,'');
on(`${buildChanges} change:repeating_effects:effect_checkbox remove:repeating_effects sheet:opened`, () => {
getSectionIDs('repeating_effects', idarray => {
const fieldnames = [];
idarray.forEach(id => {
modifiers.forEach(mod => fieldnames.push(section_field('effects', `${mod}_modifier`, id)));
fieldnames.push(section_field('effects', 'effect_checkbox', id));
});
getAttrs(fieldnames, v => {
const output = {};
modifiers.forEach(mod => output[`${mod}_modifier_total`] = 0);
idarray.forEach(id => {
const toggle = int(v[section_field('effects', 'effect_checkbox', id)]);
if(toggle) {
modifiers.forEach(mod => {
output[`${mod}_modifier_total`] = output[`${mod}_modifier_total`] + int(v[section_field('effects', `${mod}_modifier`, id)]);
});
}
});
setAttrs(output);
});
});
});