Roll20 uses cookies to improve your experience on our site. Cookies enable you to enjoy certain features, social sharing functionality, and tailor message and display ads to your interests on our site and others. They also help us understand how our site is being used. By continuing to use our site, you consent to our use of cookies. Update your cookie preferences .
×
Create a free account

Stuck on adapting complicated sheetworker to rolltemplate

I've been muddling along reasonably well on my own, but now completely stuck at the final hurdle so any help greatly appreciated!! I have a number of sheetworkers rolling dice and passing information to the rolltemplate very happily. Here's one that works exactly as required: const PCroll = [     'talking',     'physical',     'fighting',     'shooting' ]; PCroll.forEach(button => {   on(`clicked:${button}`, () => {     getAttrs(['[`${button}used`]', 'ones', 'twos', 'threes', 'fours', 'fives', 'sixes', 'sevens', 'eights', 'nines', 'tens'], v => {       startRoll(`&{template:PC} {{name=@{character_name}}} {{type=[[0]]}} {{action=rolls ${button}}} {{Roll=[[[[@{${button}}]]d6]]}} {{ones=[[0]]}} {{twos=[[0]]}} {{threes=[[0]]}} {{fours=[[0]]}} {{fives=[[0]]}} {{sixes=[[0]]}} {{sevens=[[0]]}} {{eights=[[0]]}} {{nines=[[0]]}} {{tens=[[0]]}}`, (PCroll) => {         const dice = PCroll.results.Roll.dice //This will be an array of the values rolled on all the dice         const roll1s = dice.filter(d => d === 1).length;         const roll2s = dice.filter(d => d === 2).length;         const roll3s = dice.filter(d => d === 3).length;         const roll4s = dice.filter(d => d === 4).length;         const roll5s = dice.filter(d => d === 5).length;         const roll6s = dice.filter(d => d === 6).length;         const roll7s = dice.filter(d => d === 7).length;         const roll8s = dice.filter(d => d === 8).length;         const roll9s = dice.filter(d => d === 9).length;         const roll10s = dice.filter(d => d === 10).length;         const setObj = {[`${button}used`]: 1};         setObj.dummy1 = dice.filter(d => d === 1).length;         setObj.dummy2 = dice.filter(d => d === 2).length;         setObj.dummy3 = dice.filter(d => d === 3).length;         setObj.dummy4 = dice.filter(d => d === 4).length;         setObj.dummy5 = dice.filter(d => d === 5).length;         setObj.dummy6 = dice.filter(d => d === 6).length;         setObj.dummy7 = dice.filter(d => d === 7).length;         setObj.dummy8 = dice.filter(d => d === 8).length;         setObj.dummy9 = dice.filter(d => d === 9).length;         setObj.dummy10 = dice.filter(d => d === 10).length;         finishRoll(           PCroll.rollId, // this is where you save the computed values into something that can be passed to rolltemplates.           {               ones:   roll1s + (+v.ones || 0),               twos:   roll2s + (+v.twos || 0),               threes: roll3s + (+v.threes || 0),               fours:  roll4s + (+v.fours || 0),               fives:  roll5s + (+v.fives || 0),               sixes:  roll6s + (+v.sixes || 0),               sevens: roll7s + (+v.sevens || 0),               eights: roll8s + (+v.eights || 0),               nines:  roll9s + (+v.nines || 0),               tens:   roll10s + (+v.tens || 0)           }         );         setAttrs(setObj);       });     });   }); }); What happens is the player clicks one of those four buttons (talking, physical, ...) to perform the roll. The worker rolls a number of d6 equal to the stat that corresponds to the button clicked. Then it records the number of each value rolled and adds these counts to attributes on the sheet (dummy1, dumy2, ...). It also adds each of these values to the count of those values previously rolled and stored on the sheet (ones, twos, ...) and sends them to the rolltemplate ({{ones=[[0]]}}, {{twos=[[0]]}}, ...). This is so that the rolltemplate's output can display the new totals for each value. E.g. If I have a 2 and a 4 in the bank and I roll 'talking' with a stat of 3 scoring two 1s and a 2 then the output will show I now have two 1s, two 2s and a 4. I now need to adapt another worker to this model, but the problem is that it was set up for repeating sections so it's structured rather differently (I had a LOT of help with it and don't fully understand it!). Here it is just using the default rolltemplate:   //Utility function from the K-scaffold. Parses out the section name, row id, and attribute name from an event's triggerName or sourceAttribute.   const parseTriggerName = function(string){     let match = string.replace(/^clicked:/,'').match(/(?:(repeating_[^_]+)_([^_]+)_)?(.+)/);     match.shift();     return match;   };   on('clicked:repeating_belongings:gearroll',(event)=>{     //Get our row information; we may not need all of this, but showing how you'd get all of it.     //section = `repeating_belongings`; rowID = the rowID; buttonName = gearroll     const [section,rowID,buttonName] = parseTriggerName(event.triggerName);     //The callback for this getAttrs is going to be async so that we can use the async/await pattern with startRoll     getAttrs([`repeating_belongings_${rowID}_gearquality`],async (attributes)=>{       //Convert the value to a number attributes[`repeating_belongings_${rowID}_gearquality`] = +attributes[`repeating_belongings_${rowID}_gearquality`];       //array to use to lookup how many dice of what size to roll       const diceLookup = [         {size:4,multiplier:1},         {size:6,multiplier:1},         {size:6,multiplier:2},         {size:8,multiplier:1},         {size:8,multiplier:2},       ];       //Extract the appropriate details       const dieDetails = diceLookup[attributes[`repeating_belongings_${rowID}_gearquality`] - 1];       if(!dieDetails) return; //If there are no details, then don't send a message       //Construct our message. The roll has to be in a roll template for CRP to work.       const message = `&{template:default} {{name=@{character_name}}} {{action=rolls @{repeating_belongings_${rowID}_gear}}} {{Roll=[[[[(1 - @{repeating_belongings_${rowID}_gearused}) * ${dieDetails.multiplier}]]d${dieDetails.size}]]}}`;       //Send the roll, and wait for the results to come back       const result = await startRoll(message);                 const dice = result.results.Roll.dice //This will be an array of the values rolled on all the dice                 const roll1s = dice.filter(d => d === 1).length;                 const roll2s = dice.filter(d => d === 2).length;                 const roll3s = dice.filter(d => d === 3).length;                 const roll4s = dice.filter(d => d === 4).length;                 const roll5s = dice.filter(d => d === 5).length;                 const roll6s = dice.filter(d => d === 6).length;                 const roll7s = dice.filter(d => d === 7).length;                 const roll8s = dice.filter(d => d === 8).length;                 const roll9s = dice.filter(d => d === 9).length;                 const roll10s = dice.filter(d => d === 10).length;       finishRoll(result.rollId);//We aren't doing any manipulation of the roll, so we can just finish it right away       setAttrs({//Set your attributes             [`repeating_belongings_${rowID}_gearused`]: 1,             dummy1: roll1s,             dummy2: roll2s,             dummy3: roll3s,             dummy4: roll4s,             dummy5: roll5s,             dummy6: roll6s,             dummy7: roll7s,             dummy8: roll8s,             dummy9: roll9s,             dummy10: roll10s       });     });   }); So this one rolls a stat corresponding to the button in the repeating section and records the count of each value rolled. What I need is for finishRoll to calculate the sums in the first worker (ones:   roll1s + (+v.ones || 0), etc.), but all attempts at mirroring the first worker have failed. Can anybody please advise?
Figured out a way! Probably not the most elegant, but works perfectly. I remembered that I could custom parse more than one roll at once, so just stuck in the old values of one, two, etc. as separate rolls and then set each one as a const. Looks like this:   on('clicked:repeating_belongings:gearroll',(event)=>{     const [section,rowID,buttonName] = parseTriggerName(event.triggerName);     getAttrs([`repeating_belongings_${rowID}_gearquality`],async (attributes)=>{ attributes[`repeating_belongings_${rowID}_gearquality`] = +attributes[`repeating_belongings_${rowID}_gearquality`];       const diceLookup = [         {size:4,multiplier:1},         {size:6,multiplier:1},         {size:6,multiplier:2},         {size:8,multiplier:1},         {size:8,multiplier:2},       ];       const dieDetails = diceLookup[attributes[`repeating_belongings_${rowID}_gearquality`] - 1];       if(!dieDetails) return;       const message = `&{template:PC} {{name=@{character_name}}} {{action=rolls @{repeating_belongings_${rowID}_gear}}} {{Roll=[[[[(1 - @{repeating_belongings_${rowID}_gearused}) * ${dieDetails.multiplier}]]d${dieDetails.size}]]}} {{Roll1=[[@{ones}]]}} {{ones=[[0]]}} {{Roll2=[[@{twos}]]}} {{twos=[[0]]}} {{Roll3=[[@{threes}]]}} {{threes=[[0]]}} {{Roll4=[[@{fours}]]}} {{fours=[[0]]}} {{Roll5=[[@{fives}]]}} {{fives=[[0]]}} {{Roll6=[[@{sixes}]]}} {{sixes=[[0]]}} {{Roll7=[[@{sevens}]]}} {{sevens=[[0]]}} {{Roll8=[[@{eights}]]}} {{eights=[[0]]}} {{Roll9=[[@{nines}]]}} {{nines=[[0]]}} {{Roll10=[[@{tens}]]}} {{tens=[[0]]}}`;       const result = await startRoll(message);                 const dice = result.results.Roll.dice                 const old1 = result.results.Roll1.result                 const old2 = result.results.Roll2.result                 const old3 = result.results.Roll3.result                 const old4 = result.results.Roll4.result                 const old5 = result.results.Roll5.result                 const old6 = result.results.Roll6.result                 const old7 = result.results.Roll7.result                 const old8 = result.results.Roll8.result                 const old9 = result.results.Roll9.result                 const old10 = result.results.Roll10.result                 const total1s = dice.filter(d => d === 1).length;                 const total2s = dice.filter(d => d === 2).length;                 const total3s = dice.filter(d => d === 3).length;                 const total4s = dice.filter(d => d === 4).length;                 const total5s = dice.filter(d => d === 5).length;                 const total6s = dice.filter(d => d === 6).length;                 const total7s = dice.filter(d => d === 7).length;                 const total8s = dice.filter(d => d === 8).length;                 const total9s = dice.filter(d => d === 9).length;                 const total10s = dice.filter(d => d === 10).length;       finishRoll(result.rollId,       {           ones: total1s + old1,           twos: total2s + old2,           threes: total3s + old3,           fours: total4s + old4,           fives: total5s + old5,           sixes: total6s + old6,           sevens: total7s + old7,           eights: total8s + old8,           nines: total9s + old9,           tens: total10s + old10       }       );       setAttrs({//Set your attributes             [`repeating_belongings_${rowID}_gearused`]: 1,             dummy1: total1s,             dummy2: total2s,             dummy3: total3s,             dummy4: total4s,             dummy5: total5s,             dummy6: total6s,             dummy7: total7s,             dummy8: total8s,             dummy9: total9s,             dummy10: total10s,       });     });   });