Character Vault
Any Concept / Any System
Compendium
Your System Come To Life
Roll20 for Android
Streamlined for your Tablet
Roll20 for iPad
Streamlined for your Tablet

Personal tools

Difference between revisions of "RepeatingSum"

From Roll20 Wiki

Jump to: navigation, search
m (Changed parseInt to parseFloat in 2nd function)
m
 
(48 intermediate revisions by 2 users not shown)
Line 5: Line 5:
 
The sheet worker function below will do that for you. Some examples of how to use it are listed below.  
 
The sheet worker function below will do that for you. Some examples of how to use it are listed below.  
  
== repeatingSum Function ==
+
= repeatingSum Function =
Include the following function in the sheet worker script section of your character sheet. (A simpler version of the function is at the bottom of the page, for teaching purposes.)
+
Include the following function - '''without changes''' - in the sheet worker script section of your character sheet.  
<pre>
+
== The Code ==
 +
<pre data-language="javascript" style="overflow:auto; width:auto;">
 
/* ===== PARAMETERS ==========
 
/* ===== PARAMETERS ==========
destination = the name of the attribute that stores the total quantity
+
destinations = the name of the attribute that stores the total quantity
section = name of repeating fieldset, without the repeating_
+
        can be a single attribute, or an array: ['total_cost', 'total_weight']
quantity = the name of the field to be summed
+
        If more than one, the matching fields must be in the same order.
units (optional) = the name a field that multiplies quantity.
+
    section = name of repeating fieldset, without the repeating_
  Set as "false" or don't include this parameter, if ignored. If using a checkbox rather than a number input, set its value to 1.
+
    fields = the name of the attribute field to be summed
multiplytotal (optional) = a multiplier to every item in the fieldset. For instance, if summing coins, might want to multiply the final total by 0.02 (if each coin weighs 0.02).
+
          destination and fields both can be a single attribute: 'weight'
 +
          or an array of attributes: ['weight','number','equipped']
 
*/
 
*/
let repeatingSum = function (destination, section, quantity, units, multiplytotal){
+
const repeatingSum = (destinations, section, fields) => {
    let multiplier = true;
+
     if (!Array.isArray(destinations)) destinations = [destinations.replace(/\s/g, '').split(',')];
     if (units === undefined || units === false || units === 'false') multiplier = false;
+
     if (!Array.isArray(fields)) fields = [fields.replace(/\s/g, '').split(',')];
     if (multiplytotal === undefined) multiplytotal = 1;
+
     getSectionIDs(`repeating_${section}`, idArray => {
     getSectionIDs(`repeating_${section}`, (idArray) => {
+
         const attrArray = idArray.reduce((m, id) => [...m, ...(fields.map(field => `repeating_${section}_${id}_${field}`))], []);
         const quantityFields = idArray.map(id => `repeating_${section}_${id}_${quantity}`);
+
         getAttrs([...attrArray], v => {
         const unitFields = multiplier ? idArray.map(id => `repeating_${section}_${id}_${units}`) : [];
+
            const getValue = (section, id, field) => v[`repeating_${section}_${id}_${field}`] === 'on' ? 1 : parseFloat(v[`repeating_${section}_${id}_${field}`]) || 0;
        getAttrs(quantityFields.concat(unitFields), (v) => {
+
             const commonMultipliers = (fields.length <= destinations.length) ? [] : fields.splice(destinations.length, fields.length - destinations.length);
            console.log("===== values of v: "+ JSON.stringify(v) +" =====");
+
            const output = {};
            let sumTotal = 0;
+
            destinations.forEach((destination, index) => {
            const rowValues = quantityFields.map(attr => parseFloat(v[attr], 10) || 0);
+
                output[destination] = idArray.reduce((total, id) => total + getValue(section, id, fields[index]) * commonMultipliers.reduce((subtotal, mult) => subtotal * getValue(section, id, mult), 1), 0);
             if(multiplier) {
+
             });
                const rowMultiples = unitFields.map(attr => parseFloat(v[attr], 10) || 0);
+
             setAttrs(output);
                sumTotal = _.zip(rowValues, rowMultiples).map(([a, b]) => a*b).reduce((m, x) => m + x, 0);
+
         });  
            } else {
+
     });  
                sumTotal = rowValues.reduce((m, x) => m + x, 0);
+
};
             }
+
             setAttrs({[destination]: sumTotal * multiplytotal});  
+
         });
+
     });
+
}
+
 
</pre>
 
</pre>
== Using The Function ==
+
 
 +
== Simple Example ==
 
Let's say you have an fieldset called '''repeating_inventory''', and in that set you have fields '''item_name''' and '''item_weight'''. You want to sum all the weights, and show that in an attribute outside the fieldset named '''encumbrance_total'''.
 
Let's say you have an fieldset called '''repeating_inventory''', and in that set you have fields '''item_name''' and '''item_weight'''. You want to sum all the weights, and show that in an attribute outside the fieldset named '''encumbrance_total'''.
  
You'd add the above function, and the following one:
+
You'd add the above function, and the following worker:
<pre>
+
<pre data-language="javascript" style="overflow:auto; width:auto;">
on('change:repeating_inventory remove:repeating_inventory', function() {
+
on('change:repeating_inventory:item_weight remove:repeating_inventory', function() {
 
repeatingSum("encumbrance_total","inventory","item_weight");
 
repeatingSum("encumbrance_total","inventory","item_weight");
 
});
 
});
 
</pre>
 
</pre>
  
 +
== Weight * Number ==
 
Most inventory lists are a little more complicated. You might have an extra field named '''item_number'''. For instance, your equipment list might include:
 
Most inventory lists are a little more complicated. You might have an extra field named '''item_number'''. For instance, your equipment list might include:
<pre>
+
<pre data-language="javascript" style="overflow:auto; width:auto;">
 
item_name: bow, item_weight: 3, item_number: 1
 
item_name: bow, item_weight: 3, item_number: 1
 
item_name: arrows: item_weight: 0.1, item_number: 20
 
item_name: arrows: item_weight: 0.1, item_number: 20
 
</pre>
 
</pre>
 
and so on. In this case, you'd use the following function:
 
and so on. In this case, you'd use the following function:
<pre>
+
<pre data-language="javascript" style="overflow:auto; width:auto;">
 
on('change:repeating_inventory remove:repeating_inventory', function() {
 
on('change:repeating_inventory remove:repeating_inventory', function() {
repeatingSum("encumbrance_total","inventory","item_weight","item_number");
+
repeatingSum("encumbrance_total","inventory",["item_weight","item_number"]);
 
});
 
});
 
</pre>
 
</pre>
 +
When using multiple inputs multiplied together, set them up as an array of attribute names, like: ['weight','number'], instead of 'weight'.
  
You could use it for conditional items. Let's say you have a '''repeating_armour''' fieldset, with field names, '''armour_piece''' and '''armour_worn'''. Armour_worn is a checkbox, with a value of 1. So, you can list a variety of armours, and decide which ones you are wearing by ticking the checkbox. The following function would total the worn armour pieces and add it to an '''armour_weight''' attribute, and ignore the armour not being worn.
+
== Conditional Sums (e.g. using a Checkbox) ==
<pre>
+
You could use it for conditional items. Let's say you have a '''repeating_armour''' fieldset, with field names, '''armour_piece''' and '''armour_worn'''. Armour_worn is a checkbox. So, you can list a variety of armours, and decide which ones you are wearing by ticking the checkbox. The following function would total the worn armour pieces and add it to an '''armour_weight''' attribute, and ignore the armour not being worn.
 +
<pre data-language="javascript" style="overflow:auto; width:auto;">
 
on('change:repeating_armour remove:repeating_armour', function() {
 
on('change:repeating_armour remove:repeating_armour', function() {
repeatingSum("armour_weight", "armour","armour_piece","armour_worn");
+
repeatingSum("armour_weight", "armour",["armour_piece","armour_worn"]);
 
});
 
});
 
</pre>
 
</pre>
 +
Note: this works because the a checked Checkbox has a default value of 'on' when checked, and '0' when unchecked.  This script treats any text value as a number value of 1, so even if you don't set a value for the checkbox in the html, this will work.
  
For a final example, lets say you have a '''repeating_coinage''' fieldset, with fields '''coin_name''', '''coin_value''', and '''coin_number'''. In this set, names and values are fixed (copper, 1, silver, 10, gold, 100), and players enter the number of each they have. You want to report (a) the total value, and (b) the total weight of the coins.
+
== Multiplying More Than One Column ==
 +
Sometimes you want to total up more than one thing in a repeating section, like weight and cost.
 +
Let's say you have a a set of powers, potions, and abilities that can each buff your stats. Each one might buff different combinations of stats. You want to track what the bonus for each is. You can do that a function like this:
  
<pre>
+
<pre data-language="javascript" style="overflow:auto; width:auto;">
on('change:repeating_coinage remove:repeating_coinage', function() {
+
on('change:repeating_buffs remove:repeating_buffs', function() {
repeatingSum("total_coin_value", "coinage", "coin_number", "coin_value");
+
    repeatingSum(
repeatingSum("total_coin_weight", "coinage", "coin_number", "coin_weight", '0.02');
+
        ['total_str_mod', 'total_dex_mod', 'total_con_mod', 'total_int_mod', 'total_wiz_mod' 'total_cha_mod'],  
 +
        "buffs",  
 +
        ['str_mod', 'dex_mod', 'con_mod', 'int_mod', 'wiz_mod' 'cha_mod', 'buff_active']);
 
});
 
});
 
</pre>
 
</pre>
The first repeatingSum above multiples the number gold coins by god value, adds that to the number of silver coins multiplied by silver value, and adds to that the number of copper coins multiplied by copper value.
+
I've put the destination, section, and fields parameters each on different rows, so you can see them more easily.  
The second repeatingSum does the same for weights, but then multiples the total weight by 0.02 (so you get 50 coins per pound). This allows you to list coin weights as relative weights - lets say copper and silver coins both weigh then same, so the coin_weight is 1 for those. But gold coins are heavier and have a coin_weight of 2.  
+
  
== Simpler Version of the Function ==
+
Notice the six destinations are in an array (enclosed by square brackets [ ]).
If you're creating your own character sheet, you might want to understand how the repeatingSum function works. The function below has exactly the same functionality, but is written using expressions that might be more familiar to those without much javascript experience. It'll likely still take some studying to figure out, but I present it as a self-teaching tool. I leave the studying of it as an exercise to the reader!
+
 
<pre>
+
Then notice in the fields, there are 6 corresponding attributes, that each match one of the destinations, in the same order, followed by an extra attribute:
function repeatingSum (destination, section, quantity, units, multiplytotal){
+
 
var repSection = "repeating_" + section;
+
buff_active is a checkbox, which lets you switch the buffs on or off. So if you have a buff that gave +2 to DEX and CON, and another buff that gave +2 to STR and DEX, with both active you'd get +2 STR, +4 DEX, +2 CON. But you can check or uncheck those boxes so only one, both, or neither buffs are active.
     var multiplier = true;
+
 
     // check if then optional parameters exist, and handle if they don't.
+
The attributes listed in fields must be in the same order as the destination fields they match. Any extra fields (like buff_active) are applied to all of the destinations.
if (units === undefined || units === false || units === 'false') multiplier = false;
+
 
if (multiplytotal === undefined) multiplytotal = 1;
+
= repeatingSum Version 2 =
getSectionIDs(repSection, function(idArray) {
+
The above function works for most people. But there are some features people often want, that can't be done directly. The massively expanded function below provides several expansions:
//Construct the Arrays of fields to get attributes for...
+
 
var quantityFields = [];
+
* Sum any number of columns in a repeating section. (This has been backported into the above function.)
var unitFields = [];
+
* Add attributes from outside the repeating section.
for (var a=0; a< idArray.length; a++) {
+
* Subtract the repeating section total from an outside attribute.
            // each row in a repeating section has its own id.
+
* Round the final total to a specific number of decimal places.
            // idArray is an array of those ids.
+
* multiply the total of a repeating section
            // using them you can build a n array of the full attribute names for quantity and units fields.
+
* calculate several different columns in the section, and apply different modifiers to each.
quantityFields[quantityFields.length] = repSection + "_" + idArray[a] + "_" + quantity;
+
The code for this version is below, and the new features will be described beneath it.
if(multiplier) unitFields[unitFields.length] = repSection + "_" + idArray[a] + "_" + units;
+
== the Code ==
         }
+
<pre data-language="javascript" style="overflow:auto; width:auto;">
         // combine the arrays so that getAttrs works properly
+
const repeatingSum = (destinations, section, fields, ...extras) => {
var fields = quantityFields.concat(unitFields);
+
    const isNumber = value => parseFloat(value).toString() === value.toString();
getAttrs(fields, function(v) {
+
     const isOption = value => [...checks.valid, ...checks.roundtypes].includes(value);
console.log("%%% values of v: "+ JSON.stringify(v) +" %%%");
+
     const isRounding = value => checks.roundtypes.includes(value);
var sumTotal = 0;
+
    const isFraction = value => value.includes('/') && !(value.includes(',') || value.includes('|'));
             var tempSum = 0;
+
    const getTrimmed = value => value.toLowerCase().replace(/\s/g, '');
           
+
    const getRounded = (type, value, pow) => (Math[type](value * Math.pow(10, pow)) / Math.pow(10, pow)).toFixed(Math.max(0, pow));
for(a=0; a<idArray.length; a++){
+
    const getFraction = (value) => /*{ console.log(`value: ${value}`); */
                 // for each row in the repeating section, get the quantity and unit attributes, and sum them up.
+
        parseInt(value.split('/')[0]) / parseInt(value.split('/')[1]);
    tempSum = parseFloat(v[quantityFields[a]]) || 0;
+
    const getMultiplier = (value, rounding = 1) => 'undefined' === typeof value ? (rounding ? 0: 1) : (
    if(multiplier) {
+
        isNumber(value) ? parseFloat(value) : (isFraction(value) ? getFraction(value) : value));
        tempSum = tempSum * parseFloat(v[unitFields[a]]) || 0;
+
    if (!Array.isArray(destinations)) destinations = [getTrimmed(destinations)];
    }
+
    if (!Array.isArray(fields)) fields = [getTrimmed(fields)];
sumTotal += tempSum;
+
    const fields_trimmed = fields.map(field => getTrimmed(field).split(':')[0]);
console.log("$$$ sumTotal: " + sumTotal + " $$$");
+
    const subfields = fields_trimmed.slice(0,destinations.length);
}
+
    const checks = { valid: ['multiplier'], roundtypes: ['ceil', 'round', 'floor'] };
var setSumValue = {};
+
    let properties = {attributes: {}, options: {}};
setSumValue[destination]=sumTotal * multiplytotal;
+
    extras.forEach(extra => {
setAttrs(setSumValue);
+
        const [prop, v] = getTrimmed(extra).split(':');
});
+
        const multiplier_maybe = getMultiplier(v, isRounding(prop));
});
+
        const obj = isNumber(multiplier_maybe) ? subfields.reduce((obj,field) => {
}
+
            obj[field] = multiplier_maybe;
 +
            return obj;
 +
        },{}) : multiplier_maybe.split(',').reduce((obj, item) => {
 +
            const [stat, value] = item.split('|');
 +
            const multiplier = getMultiplier(value, isRounding(prop));
 +
            obj[stat] = multiplier;
 +
            return obj;
 +
         }, {});
 +
         properties[isOption(prop) ? 'options' : 'attributes'][prop] = obj;
 +
    });
 +
    getSectionIDs(`repeating_${section}`, idArray => {
 +
        const attrArray = idArray.reduce((m, id) => [...m, ...(fields_trimmed.map(field => `repeating_${section}_${id}_${field}`))], []);
 +
        getAttrs([...attrArray, ...Object.keys(properties.attributes)], v => {
 +
            const getValue = (section, id, field) => v[`repeating_${section}_${id}_${field}`] === 'on' ? 1 : parseFloat(v[`repeating_${section}_${id}_${field}`]) || 0;
 +
             const commonMultipliers = (fields.length <= destinations.length) ? [] : fields.splice(destinations.length, fields.length - destinations.length);
 +
            const output = destinations.reduce((obj, destination, index) => {
 +
                 let sumTotal = idArray.reduce((total, id) => total + getValue(section, id, fields_trimmed[index]) * commonMultipliers.reduce((subtotal, mult) => subtotal * ((!mult.includes(':') || mult.split(':')[1].split(',').includes(fields_trimmed[index])) ? getValue(section, id, mult.split(':')[0]) : 1), 1), 0);
 +
                sumTotal *= (properties.options.hasOwnProperty('multiplier') && Object.keys(properties.options.multiplier).includes(fields_trimmed[index])) ? (parseFloat(properties.options.multiplier[fields_trimmed[index]]) || 0): 1;
 +
                sumTotal += Object.entries(properties.attributes).reduce((total, [key, value]) =>
 +
                    total += (value.hasOwnProperty(fields_trimmed[index]) ? parseFloat(v[key] || 0) * (parseFloat(value[fields_trimmed[index]]) || 1): 0) , 0);
 +
                checks.roundtypes.forEach(type => {
 +
                    if (properties.options.hasOwnProperty(type)) {
 +
                        if (Object.keys(properties.options[type]).includes(fields_trimmed[index])) {
 +
                            sumTotal = getRounded(type, sumTotal, (+properties.options[type][fields_trimmed[index]] || 0));
 +
                        } else if (properties.options[type] == '0' || !isNaN(+properties.options[type] || 'x') ) {
 +
                            sumTotal = getRounded(type, sumTotal, +properties.options[type]);
 +
                        }  
 +
                    }  
 +
                });
 +
                obj[destination] = sumTotal;
 +
                return obj;
 +
            }, {});
 +
            setAttrs(output);
 +
        });  
 +
    });  
 +
};
 
</pre>
 
</pre>
'''Author:''' [https://app.roll20.net/users/157788 GiGs](G-G-G on github).
+
 
 +
== Adding Attributes From Outside the Section ==
 +
Let's say you have an equipment total, and a coinage total, and you want to add in the weight of the coins to your encumbrance. You need a total_encumbrance and attributes for gp, sp, and cp. Then a separate total_weight attribute that adds these altogether.
 +
But with this function you can add them all at the same time.
 +
<pre data-language="javascript" style="overflow:auto; width:auto;">
 +
on('change:repeating_encumbrance:item_weight remove:repeating_armour change:gp, change:sp, change:cp', function() {
 +
repeatingSum('total_weight', "encumbrance",'item_weight','gp', 'sp', 'cp');
 +
});
 +
</pre>
 +
This example will total up the weight of all items carried, and all the coinage too and save to one destination, the total_weight attribute.
 +
The additional attributes are just listed after the fields section, separated by commas.
 +
 
 +
However, your coins probably aren't listed in pounds, so it would be handy to multiply them by a weight factor.
 +
 
 +
== Multipliers and Subtractions ==
 +
=== Multiplying Bonus Attributes ===
 +
You can multiply bonus attributes using either decimals or fractions. Imagine in your game, you get 20 gp to a pound, and 30 sp, and 50 cp respectively.
 +
<pre data-language="javascript" style="overflow:auto; width:auto;">
 +
on('change:repeating_encumbrance:item_weight remove:repeating_armour change:gp, change:sp, change:cp', function() {
 +
repeatingSum('total_weight', "encumbrance",'item_weight', 'gp:1/20', 'sp:1/30', 'cp:1/50');
 +
});
 +
</pre>
 +
Note: When using fractions, never use the form 1 1/2 for 1.5; use 3/2 instead.
 +
 
 +
=== Multiplying Specific Rows ===
 +
What if you were totalling cost and weight, it doesnt make sense to add coinage to the cost column, So you can declare that an attribute applies to just specific fields, like this:
 +
 
 +
<pre data-language="javascript" style="overflow:auto; width:auto;">
 +
on('change:repeating_encumbrance:item_weight change:repeating_encumbrance:item_cost remove:repeating_armour change:gp, change:sp, change:cp', function() {
 +
repeatingSum(['total_weight', 'total_cost'], "encumbrance",['item_weight','item_cost'], 'gp:item_weight|1/20', 'sp:item_weight|1/30', 'cp:item_weight|1/50');
 +
});
 +
</pre>
 +
 
 +
In this example, we are adding up both cost and weight. But we can see that GP, SP, and CP are totalled up with item_weight, and are ignored for the item_cost calculation.
 +
 
 +
Notice that when using multiple fields from the repeating section, they have to be put inside array brackets [ ], but the extra attributes and any additional properties are just added after the fields, separated by commas. They can be in any order.
 +
 
 +
=== Adding Specific Rows ===
 +
If the player had a separate coinage weight attribute that already totalled up the coinage weight, that section would be simpler:
 +
<pre data-language="javascript" style="overflow:auto; width:auto;">
 +
on('change:repeating_encumbrance:item_weight change:repeating_encumbrance:item_cost remove:repeating_armour change:coinage_weight', function() {
 +
repeatingSum(['total_weight', 'total_cost'], "encumbrance",['item_weight','item_cost'], 'coinage_weight:item_weight');
 +
});
 +
</pre>
 +
Here we have a single external attribute, that is added just to the weight column.
 +
 
 +
=== Multiplying the Repeating Section ===
 +
The previous operations have been applied to bonus attributes, those that are added to the repeating section. But you can multiply the repeating section total itself. You do this just by adding the multiply property.
 +
<pre data-language="javascript" style="overflow:auto; width:auto;">
 +
on('change:repeating_encumbrance:item_weight change:repeating_encumbrance:item_cost remove:repeating_armour', function() {
 +
repeatingSum(['total_weight', 'total_cost'], "encumbrance",['item_weight','item_cost'], 'multiply: 2');
 +
});
 +
</pre>
 +
The above will total up item weight and cost, and then double the final total. You can apply the multiplier to specific columns.
 +
<pre data-language="javascript" style="overflow:auto; width:auto;">
 +
on('change:repeating_encumbrance:item_weight change:repeating_encumbrance:item_cost remove:repeating_armour', function() {
 +
repeatingSum(['total_weight', 'total_cost'], "encumbrance",['item_weight','item_cost'], 'multiply: total_weight|2/3');
 +
});
 +
</pre>
 +
This multiplies the weight column by 2/3, and leaves the cost column unchanged.
 +
 
 +
=== Subtractions ===
 +
Sometimes its handy to be able to express a repeating section as a subtraction
 +
In Shadowrun, for example, characters buy cyberware, and each reduces the Essence attribute. The more cyberware you get, the lower your essence becomes.
 +
By using a multiplier of -1, and adding an external attribute, Essence, we can manage that:
 +
<pre data-language="javascript" style="overflow:auto; width:auto;">
 +
on('change:repeating_cyberware:essence_cost change:repeating_cyberware:item_cost remove:repeating_cyberware', function() {
 +
repeatingSum(['essence_score', 'total_cost'], "cyberware",['essence_cost','item_cost'], 'essence_base: essence_cost', multiply:essence_cost|-1);
 +
});
 +
</pre>
 +
Here we have a repeating section that totals up the essence_cost and item_cost columns.
 +
 
 +
The essence_cost column is totalled up, multiplied by -1 and added to a bonus attribute: essence_base. This is saved to the characters essence_score attribute.
 +
 
 +
The item cost is totalled up and saved to total_cost, so you can see just how much money you can get by killing this character and pillaging their cyberware...
 +
 
 +
== Rounding Totals ==
 +
Being able to round totals is very handy. You can apply round, ceil, or floor to the final totals, in whatever digits you want, applying different rounding to each column.
 +
Here's a worker to calculate the mass, volume, power requirements, and cost of various parts of a starship. Mass and volume are both rounded to thousandths, power to whole numbers, and cost to whole millions.
 +
<pre data-language="javascript" style="overflow:auto; width:auto;">
 +
on('change:repeating_starship:mass change:repeating_starship:volume change:repeating_starship:power change:repeating_starship:cost  remove:repeating_starship', function() {
 +
repeatingSum(['starship_mass', 'starship_volume', 'starship_power', 'starship_cost'],
 +
'starship', ['mass:round|3','volume:round|3', 'power:round', 'cost:round|-6']);
 +
});
 +
</pre>
 +
If you just wanted round off the final total to thousandths, it's a lot simpler:
 +
<pre data-language="javascript" style="overflow:auto; width:auto;">
 +
on('change:repeating_starship:mass change:repeating_starship:volume change:repeating_starship:power change:repeating_starship:cost  remove:repeating_starship', function() {
 +
repeatingSum(['starship_mass', 'starship_volume', 'starship_power', 'starship_cost'],
 +
'starship',['mass','volume', 'power', 'cost6'], 'round: 3');
 +
});
 +
</pre>
 +
You can apply round, ceil, and floor functions to individual columns, or to the whole section.
 +
 
 +
Dont combine them: if you apply rounding to a specific column, you cant use the non-specific rounding modifier, . In that case, any rounding you want to apply must be applied individually to each column.
 +
 
 +
== Syntax Summary==
 +
Here's a summary of the function's syntax, in the same format as the code at the top of the page.
 +
<pre data-language="javascript" style="overflow:auto; width:auto;">
 +
/* ===== PARAMETERS ==========
 +
destinations = the name of the attribute that stores the total quantity
 +
section = name of repeating fieldset, without the repeating_
 +
fields = the name of the attribute field to be summed
 +
      can be a single attribute: 'weight'
 +
      or an array of attributes: ['weight','number','equipped']
 +
extras: everything after the fields parameter is optional and can be in any order:
 +
    'ceil'
 +
    'round'
 +
    'floor'
 +
    'ceil: 3'
 +
    'round: -2'
 +
    'round: equipment_weight, equipment_cost|2
 +
        you want to round the final total.
 +
        If you supply a field name, it will round just that total. You can supply multiple fields, separated by commas.
 +
        If you supply a number, it will round to that many digits.
 +
        round:1 rounds to tenths; floor:-3 rounds down to thousands, so 3567 would be shown as 3000.
 +
        If you dont supply a number, it assumes 0, and returns an integer (whole numbers).
 +
        IMPORTANT: if you list ANY field, then ALL fields to be rounded must be specifically stated.
 +
        Don't do this: floor:equipment_weight|2, round,
 +
   
 +
    'multiplier: 2'
 +
    'multiplier:equipment_weight|2'
 +
    'multiplier: equipment_weight|2, equipment_cost|3'
 +
        Multiplier will apply a multiple to the final total. You can multiple all fields, or specific fields.
 +
        It doesnt apply to attributes being added from outside the repeating section.
 +
        Multiplier can be negative, representing a subtraction.
 +
 
 +
    'an_attribute'
 +
    'an_attribute:-1'
 +
    'an_attribute:0.5'
 +
    'an_attribute:equipment_cost'
 +
    'an_attribute:equipment_cost|-1'
 +
    'an_attribute:equipment_cost|-1,equipment_weight|2'
 +
        You can also list attributes from outside the repeating section. Don't try to add attributes from other repeating sections.
 +
        by default, the listed attribute will be added to all fields.
 +
        You can list one or more fields, and it will only be added to those fields.
 +
        You can list a number: the attribute will be multiplied by that ammount. So -1 subtracts the attribute.
 +
    */
 +
</pre>
 +
'''Author:''' [https://app.roll20.net/users/157788 GiGs](G-G-G on github), with help from [https://app.roll20.net/users/104025 The Aaron], inspired by a [https://app.roll20.net/forum/permalink/6857889/ script created by Finderski].
 +
 
 +
=Related Pages=
 +
* [[Sheetworker_examples_for_Non-programmers|Sheetworker Examples for Non-programmers]]
 +
* [[Sheet_Worker_Snippets|Sheetworker Snippets]] - short, practical examples of sheetworker use
 +
* [[UniversalSheetWorkers|Universal Sheet Workers]] - How to create one function that can handle a bunch of similar sheet workers
 +
 
 +
==See Also==
 +
* [https://app.roll20.net/forum/post/8033979/advice-api-sheet-workers-and-performance/?pageforid=8034567#post-8034567 Sheet Worker Optimization] by Scott C.
 +
* [https://app.roll20.net/forum/post/6963354/build-lookup-table-into-a-character-sheet/?pageforid=6964447#post-6964447 How to integrate table of stats into a sheet] -Forum post
 +
* [https://developer.mozilla.org/en-US/docs/Learn/Accessibility/CSS_and_JavaScript#JavaScript JavaScript Best Practices] - MDN web docs
 +
* [https://developer.mozilla.org/en-US/docs/Learn/JavaScript/First_steps Introduction to JavaScript] - MDN web docs
 +
 
 +
<br>
 +
<br>
 +
 
 
__FORCETOC__
 
__FORCETOC__
[[Category:Tips]]
+
 
[[Category:User content]]
+
[[Category:Sheetworker]]
 +
[[Category:Character Sheet Creation]]

Latest revision as of 14:36, 19 June 2020

One request that crops up on the roll20 forums over and over again, is how can you add up all the items in a repeating section?

Say you have an inventory section, listing the items you are carrying, and you need their total weight. Or you have section listing all the coins of different types, and you want their values. Or a skill or power section, and you want the total character points used to buy them.

The sheet worker function below will do that for you. Some examples of how to use it are listed below.

Contents

[edit] repeatingSum Function

Include the following function - without changes - in the sheet worker script section of your character sheet.

[edit] The Code

/* ===== PARAMETERS ==========
destinations = the name of the attribute that stores the total quantity
        can be a single attribute, or an array: ['total_cost', 'total_weight']
        If more than one, the matching fields must be in the same order. 
    section = name of repeating fieldset, without the repeating_
    fields = the name of the attribute field to be summed
          destination and fields both can be a single attribute: 'weight'
          or an array of attributes: ['weight','number','equipped']
*/
const repeatingSum = (destinations, section, fields) => {
    if (!Array.isArray(destinations)) destinations = [destinations.replace(/\s/g, '').split(',')];
    if (!Array.isArray(fields)) fields = [fields.replace(/\s/g, '').split(',')];
    getSectionIDs(`repeating_${section}`, idArray => {
        const attrArray = idArray.reduce((m, id) => [...m, ...(fields.map(field => `repeating_${section}_${id}_${field}`))], []);
        getAttrs([...attrArray], v => {
            const getValue = (section, id, field) => v[`repeating_${section}_${id}_${field}`] === 'on' ? 1 : parseFloat(v[`repeating_${section}_${id}_${field}`]) || 0;
            const commonMultipliers = (fields.length <= destinations.length) ? [] : fields.splice(destinations.length, fields.length - destinations.length);
            const output = {};
            destinations.forEach((destination, index) => {
                output[destination] = idArray.reduce((total, id) => total + getValue(section, id, fields[index]) * commonMultipliers.reduce((subtotal, mult) => subtotal * getValue(section, id, mult), 1), 0);
            });
            setAttrs(output);
        }); 
    }); 
};

[edit] Simple Example

Let's say you have an fieldset called repeating_inventory, and in that set you have fields item_name and item_weight. You want to sum all the weights, and show that in an attribute outside the fieldset named encumbrance_total.

You'd add the above function, and the following worker:

on('change:repeating_inventory:item_weight remove:repeating_inventory', function() {
	repeatingSum("encumbrance_total","inventory","item_weight");
});

[edit] Weight * Number

Most inventory lists are a little more complicated. You might have an extra field named item_number. For instance, your equipment list might include:

item_name: bow, item_weight: 3, item_number: 1
item_name: arrows: item_weight: 0.1, item_number: 20

and so on. In this case, you'd use the following function:

on('change:repeating_inventory remove:repeating_inventory', function() {
	repeatingSum("encumbrance_total","inventory",["item_weight","item_number"]);
});

When using multiple inputs multiplied together, set them up as an array of attribute names, like: ['weight','number'], instead of 'weight'.

[edit] Conditional Sums (e.g. using a Checkbox)

You could use it for conditional items. Let's say you have a repeating_armour fieldset, with field names, armour_piece and armour_worn. Armour_worn is a checkbox. So, you can list a variety of armours, and decide which ones you are wearing by ticking the checkbox. The following function would total the worn armour pieces and add it to an armour_weight attribute, and ignore the armour not being worn.

on('change:repeating_armour remove:repeating_armour', function() {
	repeatingSum("armour_weight", "armour",["armour_piece","armour_worn"]);
});

Note: this works because the a checked Checkbox has a default value of 'on' when checked, and '0' when unchecked. This script treats any text value as a number value of 1, so even if you don't set a value for the checkbox in the html, this will work.

[edit] Multiplying More Than One Column

Sometimes you want to total up more than one thing in a repeating section, like weight and cost. Let's say you have a a set of powers, potions, and abilities that can each buff your stats. Each one might buff different combinations of stats. You want to track what the bonus for each is. You can do that a function like this:

on('change:repeating_buffs remove:repeating_buffs', function() {
    repeatingSum(
        ['total_str_mod', 'total_dex_mod', 'total_con_mod', 'total_int_mod', 'total_wiz_mod' 'total_cha_mod'], 
        "buffs", 
        ['str_mod', 'dex_mod', 'con_mod', 'int_mod', 'wiz_mod' 'cha_mod', 'buff_active']);
});

I've put the destination, section, and fields parameters each on different rows, so you can see them more easily.

Notice the six destinations are in an array (enclosed by square brackets [ ]).

Then notice in the fields, there are 6 corresponding attributes, that each match one of the destinations, in the same order, followed by an extra attribute:

buff_active is a checkbox, which lets you switch the buffs on or off. So if you have a buff that gave +2 to DEX and CON, and another buff that gave +2 to STR and DEX, with both active you'd get +2 STR, +4 DEX, +2 CON. But you can check or uncheck those boxes so only one, both, or neither buffs are active.

The attributes listed in fields must be in the same order as the destination fields they match. Any extra fields (like buff_active) are applied to all of the destinations.

[edit] repeatingSum Version 2

The above function works for most people. But there are some features people often want, that can't be done directly. The massively expanded function below provides several expansions:

  • Sum any number of columns in a repeating section. (This has been backported into the above function.)
  • Add attributes from outside the repeating section.
  • Subtract the repeating section total from an outside attribute.
  • Round the final total to a specific number of decimal places.
  • multiply the total of a repeating section
  • calculate several different columns in the section, and apply different modifiers to each.

The code for this version is below, and the new features will be described beneath it.

[edit] the Code

const repeatingSum = (destinations, section, fields, ...extras) => {
    const isNumber = value => parseFloat(value).toString() === value.toString();
    const isOption = value => [...checks.valid, ...checks.roundtypes].includes(value);
    const isRounding = value => checks.roundtypes.includes(value);
    const isFraction = value => value.includes('/') && !(value.includes(',') || value.includes('|'));
    const getTrimmed = value => value.toLowerCase().replace(/\s/g, '');
    const getRounded = (type, value, pow) => (Math[type](value * Math.pow(10, pow)) / Math.pow(10, pow)).toFixed(Math.max(0, pow));
    const getFraction = (value) => /*{ console.log(`value: ${value}`); */
        parseInt(value.split('/')[0]) / parseInt(value.split('/')[1]);
    const getMultiplier = (value, rounding = 1) => 'undefined' === typeof value ? (rounding ? 0: 1) : (
        isNumber(value) ? parseFloat(value) : (isFraction(value) ? getFraction(value) : value));
    if (!Array.isArray(destinations)) destinations = [getTrimmed(destinations)];
    if (!Array.isArray(fields)) fields = [getTrimmed(fields)];
    const fields_trimmed = fields.map(field => getTrimmed(field).split(':')[0]);
    const subfields = fields_trimmed.slice(0,destinations.length);
    const checks = { valid: ['multiplier'], roundtypes: ['ceil', 'round', 'floor'] };
    let properties = {attributes: {}, options: {}};
    extras.forEach(extra => {
        const [prop, v] = getTrimmed(extra).split(':');
        const multiplier_maybe = getMultiplier(v, isRounding(prop));
        const obj = isNumber(multiplier_maybe) ? subfields.reduce((obj,field) => {
            obj[field] = multiplier_maybe;
            return obj;
        },{}) : multiplier_maybe.split(',').reduce((obj, item) => {
            const [stat, value] = item.split('|');
            const multiplier = getMultiplier(value, isRounding(prop));
            obj[stat] = multiplier;
            return obj;
        }, {});
        properties[isOption(prop) ? 'options' : 'attributes'][prop] = obj;
    });
    getSectionIDs(`repeating_${section}`, idArray => {
        const attrArray = idArray.reduce((m, id) => [...m, ...(fields_trimmed.map(field => `repeating_${section}_${id}_${field}`))], []);
        getAttrs([...attrArray, ...Object.keys(properties.attributes)], v => {
            const getValue = (section, id, field) => v[`repeating_${section}_${id}_${field}`] === 'on' ? 1 : parseFloat(v[`repeating_${section}_${id}_${field}`]) || 0;
            const commonMultipliers = (fields.length <= destinations.length) ? [] : fields.splice(destinations.length, fields.length - destinations.length);
            const output = destinations.reduce((obj, destination, index) => {
                let sumTotal = idArray.reduce((total, id) => total + getValue(section, id, fields_trimmed[index]) * commonMultipliers.reduce((subtotal, mult) => subtotal * ((!mult.includes(':') || mult.split(':')[1].split(',').includes(fields_trimmed[index])) ? getValue(section, id, mult.split(':')[0]) : 1), 1), 0);
                sumTotal *= (properties.options.hasOwnProperty('multiplier') && Object.keys(properties.options.multiplier).includes(fields_trimmed[index])) ? (parseFloat(properties.options.multiplier[fields_trimmed[index]]) || 0): 1;
                sumTotal += Object.entries(properties.attributes).reduce((total, [key, value]) => 
                    total += (value.hasOwnProperty(fields_trimmed[index]) ? parseFloat(v[key] || 0) * (parseFloat(value[fields_trimmed[index]]) || 1): 0) , 0);
                checks.roundtypes.forEach(type => {
                    if (properties.options.hasOwnProperty(type)) {
                        if (Object.keys(properties.options[type]).includes(fields_trimmed[index])) {
                            sumTotal = getRounded(type, sumTotal, (+properties.options[type][fields_trimmed[index]] || 0));
                        } else if (properties.options[type] == '0' || !isNaN(+properties.options[type] || 'x') ) {
                            sumTotal = getRounded(type, sumTotal, +properties.options[type]);
                        } 
                    } 
                });
                obj[destination] = sumTotal;
                return obj;
            }, {});
            setAttrs(output);
        }); 
    }); 
};

[edit] Adding Attributes From Outside the Section

Let's say you have an equipment total, and a coinage total, and you want to add in the weight of the coins to your encumbrance. You need a total_encumbrance and attributes for gp, sp, and cp. Then a separate total_weight attribute that adds these altogether. But with this function you can add them all at the same time.

on('change:repeating_encumbrance:item_weight remove:repeating_armour change:gp, change:sp, change:cp', function() {
	repeatingSum('total_weight', "encumbrance",'item_weight','gp', 'sp', 'cp');
});

This example will total up the weight of all items carried, and all the coinage too and save to one destination, the total_weight attribute. The additional attributes are just listed after the fields section, separated by commas.

However, your coins probably aren't listed in pounds, so it would be handy to multiply them by a weight factor.

[edit] Multipliers and Subtractions

[edit] Multiplying Bonus Attributes

You can multiply bonus attributes using either decimals or fractions. Imagine in your game, you get 20 gp to a pound, and 30 sp, and 50 cp respectively.

on('change:repeating_encumbrance:item_weight remove:repeating_armour change:gp, change:sp, change:cp', function() {
	repeatingSum('total_weight', "encumbrance",'item_weight', 'gp:1/20', 'sp:1/30', 'cp:1/50');
});

Note: When using fractions, never use the form 1 1/2 for 1.5; use 3/2 instead.

[edit] Multiplying Specific Rows

What if you were totalling cost and weight, it doesnt make sense to add coinage to the cost column, So you can declare that an attribute applies to just specific fields, like this:

on('change:repeating_encumbrance:item_weight change:repeating_encumbrance:item_cost remove:repeating_armour change:gp, change:sp, change:cp', function() {
	repeatingSum(['total_weight', 'total_cost'], "encumbrance",['item_weight','item_cost'], 'gp:item_weight|1/20', 'sp:item_weight|1/30', 'cp:item_weight|1/50');
});

In this example, we are adding up both cost and weight. But we can see that GP, SP, and CP are totalled up with item_weight, and are ignored for the item_cost calculation.

Notice that when using multiple fields from the repeating section, they have to be put inside array brackets [ ], but the extra attributes and any additional properties are just added after the fields, separated by commas. They can be in any order.

[edit] Adding Specific Rows

If the player had a separate coinage weight attribute that already totalled up the coinage weight, that section would be simpler:

on('change:repeating_encumbrance:item_weight change:repeating_encumbrance:item_cost remove:repeating_armour change:coinage_weight', function() {
	repeatingSum(['total_weight', 'total_cost'], "encumbrance",['item_weight','item_cost'], 'coinage_weight:item_weight');
});

Here we have a single external attribute, that is added just to the weight column.

[edit] Multiplying the Repeating Section

The previous operations have been applied to bonus attributes, those that are added to the repeating section. But you can multiply the repeating section total itself. You do this just by adding the multiply property.

on('change:repeating_encumbrance:item_weight change:repeating_encumbrance:item_cost remove:repeating_armour', function() {
	repeatingSum(['total_weight', 'total_cost'], "encumbrance",['item_weight','item_cost'], 'multiply: 2');
});

The above will total up item weight and cost, and then double the final total. You can apply the multiplier to specific columns.

on('change:repeating_encumbrance:item_weight change:repeating_encumbrance:item_cost remove:repeating_armour', function() {
	repeatingSum(['total_weight', 'total_cost'], "encumbrance",['item_weight','item_cost'], 'multiply: total_weight|2/3');
});

This multiplies the weight column by 2/3, and leaves the cost column unchanged.

[edit] Subtractions

Sometimes its handy to be able to express a repeating section as a subtraction In Shadowrun, for example, characters buy cyberware, and each reduces the Essence attribute. The more cyberware you get, the lower your essence becomes. By using a multiplier of -1, and adding an external attribute, Essence, we can manage that:

on('change:repeating_cyberware:essence_cost change:repeating_cyberware:item_cost remove:repeating_cyberware', function() {
	repeatingSum(['essence_score', 'total_cost'], "cyberware",['essence_cost','item_cost'], 'essence_base: essence_cost', multiply:essence_cost|-1);
});

Here we have a repeating section that totals up the essence_cost and item_cost columns.

The essence_cost column is totalled up, multiplied by -1 and added to a bonus attribute: essence_base. This is saved to the characters essence_score attribute.

The item cost is totalled up and saved to total_cost, so you can see just how much money you can get by killing this character and pillaging their cyberware...

[edit] Rounding Totals

Being able to round totals is very handy. You can apply round, ceil, or floor to the final totals, in whatever digits you want, applying different rounding to each column. Here's a worker to calculate the mass, volume, power requirements, and cost of various parts of a starship. Mass and volume are both rounded to thousandths, power to whole numbers, and cost to whole millions.

on('change:repeating_starship:mass change:repeating_starship:volume change:repeating_starship:power change:repeating_starship:cost  remove:repeating_starship', function() {
	repeatingSum(['starship_mass', 'starship_volume', 'starship_power', 'starship_cost'], 
'starship', ['mass:round|3','volume:round|3', 'power:round', 'cost:round|-6']);
});

If you just wanted round off the final total to thousandths, it's a lot simpler:

on('change:repeating_starship:mass change:repeating_starship:volume change:repeating_starship:power change:repeating_starship:cost  remove:repeating_starship', function() {
	repeatingSum(['starship_mass', 'starship_volume', 'starship_power', 'starship_cost'], 
'starship',['mass','volume', 'power', 'cost6'], 'round: 3');
});

You can apply round, ceil, and floor functions to individual columns, or to the whole section.

Dont combine them: if you apply rounding to a specific column, you cant use the non-specific rounding modifier, . In that case, any rounding you want to apply must be applied individually to each column.

[edit] Syntax Summary

Here's a summary of the function's syntax, in the same format as the code at the top of the page.

/* ===== PARAMETERS ==========
destinations = the name of the attribute that stores the total quantity
section = name of repeating fieldset, without the repeating_
fields = the name of the attribute field to be summed
      can be a single attribute: 'weight'
      or an array of attributes: ['weight','number','equipped']
extras: everything after the fields parameter is optional and can be in any order:
    'ceil'
    'round'
    'floor'
    'ceil: 3'
    'round: -2'
    'round: equipment_weight, equipment_cost|2
        you want to round the final total. 
        If you supply a field name, it will round just that total. You can supply multiple fields, separated by commas.
        If you supply a number, it will round to that many digits. 
        round:1 rounds to tenths; floor:-3 rounds down to thousands, so 3567 would be shown as 3000.
        If you dont supply a number, it assumes 0, and returns an integer (whole numbers).
        IMPORTANT: if you list ANY field, then ALL fields to be rounded must be specifically stated.
        Don't do this: floor:equipment_weight|2, round,
    
    'multiplier: 2'
    'multiplier:equipment_weight|2'
    'multiplier: equipment_weight|2, equipment_cost|3'
        Multiplier will apply a multiple to the final total. You can multiple all fields, or specific fields.
        It doesnt apply to attributes being added from outside the repeating section.
        Multiplier can be negative, representing a subtraction.

    'an_attribute'
    'an_attribute:-1'
    'an_attribute:0.5'
    'an_attribute:equipment_cost'
    'an_attribute:equipment_cost|-1'
    'an_attribute:equipment_cost|-1,equipment_weight|2'
        You can also list attributes from outside the repeating section. Don't try to add attributes from other repeating sections.
        by default, the listed attribute will be added to all fields.
        You can list one or more fields, and it will only be added to those fields.
        You can list a number: the attribute will be multiplied by that ammount. So -1 subtracts the attribute.
    */

Author: GiGs(G-G-G on github), with help from The Aaron, inspired by a script created by Finderski.

[edit] Related Pages

[edit] See Also