Needing help with coding... d12 success pool w/ a twist.

1506395858
OK, first I apologize if this post is in the wrong spot or a duplication of someone else posting... Going forward with the request. What I am ultimately trying to do is make a macro for a custom game that I am developing. D12 success pool much like any other success pool api out there, but here is the twist. 1 = failures and take away successes. 8 thru 12 = successes 12 = is exponential; meaning any 12's rolled in a given roll will take on the value of all 12's rolled. example 1: 5 dice (d12) are rolled: 3,5,6,7,12 = 1 successes example 2: 6 dice are rolled: 1,5,8,9,10,12 = 3 successes example 3: 4 dice are rolled: 4,6,12,12 = 4 successes example 4: 5 dice are rolled: 3,5,10,12,12 = 5 successes Here is the code that i have so far. (btw: something is not right here... it doesn't work with roll20 yet.) Please help "help me, obi won code-nobi. you are our only hope" <code> Number of Dice: <input type="text" name="roll_count" id="roll_count" size="3"> <button value="Roll" onclick="alert(roll(document.getElementById('roll_count').value));"> Roll </button> function roll(number_of_dice) { var successes = 0; var twelve_count = 0; var twelve_successes = 0; var roll = []; for (i = 0; i < number_of_dice; i++) { roll[i] = Math.floor(Math.random() * 12) + 1; } alert("You rolled: " + roll); for (i = 0; i < roll.length; i++) { //alert(i); if (roll[i] > 8) { if (roll[i] == 12) { twelve_count++; } else { successes++; } twelve_successes = Math.pow(twelve_count, 2); successes = successes + twelve_successes; } else { successes--; } } return ("Wins:" + successes); }
1506407357
The Aaron
Pro
API Scripter
Should example 2 be 4 successes instead of 3? The Roll20 API is not like writing JavaScript in a webpage, it has much more in common with a node module. I'd suggest looking at some example scripts and the wiki docs to get an idea. 
The Aaron said: Should example 2 be 4 successes instead of 3? example 2: 6 dice are rolled: 1,5,8,9,10,12 = 3 successes If I'm interpreting Wallace's rules correctly: one 12 means "12"s are worth one success 8, 9, 10 = three successes 1 = one anti-success (failure) (1*1)+(1+1+1)+(-1) = three successes
1506409466
Loki
Pro
Sheet Author
I guess, the "1" in example 2 negates one of the four succeses (8,9,10,12), leaving three successes.
1506430748

Edited 1506433050
The Aaron
Pro
API Scripter
Ah, I see.  That will teach me to respond in the middle of the night! =D Here's a minimal script that does implements these rules: on('ready',()=>{     const roll = (dice) => {         const d12 = () => randomInteger(12),             val = (n) => n===12 ? 'd' : n===1 ? 'f' : _.contains([8,9,10,11],n) ? 'p' : 'z';          let res = {             rolls: _.sortBy(_.times(dice, d12),_.identity),             successes: 0,         };         let pts = _.countBy(res.rolls,val);         res.successes = ((pts.f||0)*-1) + (pts.p||0) + ((pts.d||0)*(pts.d||0));         return res;     };     on('chat:message',(msg)=>{         if(msg.type==='api' && msg.content.match(/^!roll/)){             let args = msg.content.split(/\s+/);             let dice = parseInt(args[1],10)||0;             if(dice>0){                 let res = roll(dice);                 sendChat('roller',`Result: <b>${res.successes}</b>, rolled: ${res.rolls}`);             } else {                 let who=(getObj('player',msg.playerid)||{get:()=>'API'}).get('_displayname');                 sendChat('roller',`/w "${who}" <div><code>!roll NUMBER</code> to roll the dice.</div>`);             }         }     }); }); Call it with: !roll 5 in the chat. Some example rolls:
1506442231
Thank you very much! I will try this out after work today!
1506442673
The Aaron
Pro
API Scripter
No problem!  It's kind of tersely written, because that's how I write things, but I'm happy to explain any parts of it.  It uses a bunch of Javascript ES6 syntax (fat arrow functions: "(arg)=>body", const, let, ` ` template literals, etc) and underscore.js (_.times, _.countby, _.contains, etc), and Roll20's API interface functions ( on() for registering event handlers, randomInteger() for getting die rolls, sendChat() for output to the chat panel). Let me know how I can help!