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

List all Objects and methods

Sorry if I use the wrong terminology here. I believe that the API WIKI doesn't have all the Objects properties and methods listed. Of course I may well be wrong on that :-( If it want to list all the properties and methods of an Object, how can I do that? What i'm looking for is the method that sorts the turn tracker, but knowing how to find that information will be invaluable.
1525017324
The Aaron
Pro
API Scripter
There isn't a method built in for doing that.  The Turn Tracker is just a JSON encoded array of objects on the Campaign object.  To sort it, you must decode it into a Javascript array, apply a sort to it, then re-encode it as JSON and set it on the Campaign object.  Here's an example of sorting descending: const sortTurnOrderDesc = () => { // get turn order as array of objects let turns = JSON.parse(Campaign().get('turnorder')||'[]')); // sort descending by priority entry turns.sort( (a, b) => (parseInt(b._pr)||0) - (parseInt(a._pr)||0) ); // set the turnorder back on campaign as a JSON string Campaign().set('turnorder', JSON.stringify(turns)); };
OK, thank you. Just with having the sort into ascending and descending, though there might be something to call .
1525021850
GiGs
Pro
Sheet Author
API Scripter
There's a Hero system script that adds characters to the turn tracker, multiple times. each character has a speed, which is how many times they act per turn. So, the turn tracker might look like Speedy 3.77 Ajax 3.85 Dynamo 4.8 Speedy 5.77 Ajax 6.85 Speedy 8.77 Dynamo 8.8 Ajax 9.85 Speedy 10.77 Speedy 12.77 Dynamo 12.8 Ajax 12.85 The number after the decimal is the tiebreaker, the number before the decimal is the segment they all act. The tricky thing is, combats start at segment 12, then go 1, 2, 3, etc. The first turn is always just a single segment at turn 12. So sorting by ascending works, except that everything at turn 12 then needs to be put to the top, so it would look like: Speedy 12.77 Dynamo 12.8 Ajax 12.85 Speedy 3.77 Ajax 3.85 Dynamo 4.8 Speedy 5.77 Ajax 6.85 Speedy 8.77 Dynamo 8.8 Ajax 9.85 Speedy 10.77 Is there an easy way to do that sort?
ok, gave that a whirl, in this format on("chat:message", function(msg) { if (msg.type === "api" && msg.content.indexOf("!tor") !== -1) { const sortTurnOrderDesc = () => { // get turn order as array of objects let turns = JSON.parse(Campaign().get('turnorder')||'[]'); // sort descending by priority entry turns.sort( (a, b) => (parseInt(b._pr)||0) - (parseInt(a._pr)||0) ); // set the turnorder back on campaign as a JSON string Campaign().set('turnorder', JSON.stringify(turns)); }; }; } ); The turn Order remained the same
1525023458

Edited 1525023491
The Aaron
Pro
API Scripter
This would do that Hero System sort: const sortHeroSystem = (a,b) => { const aInt = (parseInt(a._pr)||0); const aFloat = (parseFloat(a._pr)||0); const bInt = (parseInt(b._pr)||0); const bFloat = (parseFloat(b._pr)||0); if( 12 === aInt && 12 !== bInt){ return -1; } if( 12 !== aInt && 12 === bInt){ return 1; } return (aFloat - bFloat); };
1525023604
The Aaron
Pro
API Scripter
MarkL said: ok, gave that a whirl, in this format You just need to call that function: on("chat:message", function(msg) {   if (msg.type === "api" && msg.content.indexOf("!tor") !== -1) {     const sortTurnOrderDesc = () => {       // get turn order as array of objects       let turns = JSON.parse(Campaign().get('turnorder')||'[]');       // sort descending by priority entry         turns.sort( (a, b) => (parseInt(b._pr)||0) - (parseInt(a._pr)||0) );       // set the turnorder back on campaign as a JSON string       Campaign().set('turnorder', JSON.stringify(turns));     };     // call sort function     sortTurnOrderDesc();   }; });
1525024099

Edited 1525024203
GiGs
Pro
Sheet Author
API Scripter
Thanks, you're great as always, Aaron. By the way, what is ._pr doing in that function?
Thanks Aaron. I'll go away and work out exactly what it is doing.... Wait a moment, I see.  I really should do some reading, rather than thrashing around blindly :-(
1525028054
GiGs
Pro
Sheet Author
API Scripter
I'm stumped, Aaron. How do I integrate this: const sortHeroSystem = (a,b) => { const aInt = (parseInt(a._pr)||0); const aFloat = (parseFloat(a._pr)||0); const bInt = (parseInt(b._pr)||0); const bFloat = (parseFloat(b._pr)||0); if( 12 === aInt && 12 !== bInt){ return -1; } if( 12 !== aInt && 12 === bInt){ return 1; } return (aFloat - bFloat); }; with this: const sortTurnOrderDesc = () => { // get turn order as array of objects let turns = JSON.parse(Campaign().get('turnorder')||'[]')); // sort descending by priority entry turns.sort( (a, b) => (parseInt(b._pr)||0) - (parseInt(a._pr)||0) ); // set the turnorder back on campaign as a JSON string Campaign().set('turnorder', JSON.stringify(turns)); }; ??
1525032247
The Aaron
Pro
API Scripter
Oh!  Sorry, was typing that from (faulty!) memory.  Should be pr, not _pr: const sortHeroSystem = (a,b) => { const aInt = (parseInt(a.pr)||0); const aFloat = (parseFloat(a.pr)||0); const bInt = (parseInt(b.pr)||0); const bFloat = (parseFloat(b.pr)||0); if( 12 === aInt && 12 !== bInt){ return -1; } if( 12 !== aInt && 12 === bInt){ return 1; } return (aFloat - bFloat); }; Likewise, the above script should be: on("chat:message", function(msg) {   if (msg.type === "api" && msg.content.indexOf("!tor") !== -1) {     const sortTurnOrderDesc = () => {       // get turn order as array of objects       let turns = JSON.parse(Campaign().get('turnorder')||'[]');       // sort descending by priority entry         turns.sort( (a, b) => (parseInt(b.pr)||0) - (parseInt(a.pr)||0) );       // set the turnorder back on campaign as a JSON string       Campaign().set('turnorder', JSON.stringify(turns));     };     // call sort function     sortTurnOrderDesc();   }; }); And finally, integrating that sort function into the API command !sorths : on('ready', () => {   const sortHeroSystem = (a,b) => {     const aInt = (parseInt(a.pr)||0);     const aFloat = (parseFloat(a.pr)||0);     const bInt = (parseInt(b.pr)||0);     const bFloat = (parseFloat(b.pr)||0);     if( 12 === aInt && 12 !== bInt){       return -1;     }     if( 12 !== aInt && 12 === bInt){       return 1;     }     return (aFloat - bFloat);   };   const sortTurnOrderDesc = () => {     let turns = JSON.parse(Campaign().get('turnorder')||'[]');     turns.sort( sortHeroSystem );     Campaign().set('turnorder', JSON.stringify(turns));   };   on("chat:message", function(msg) {     if (msg.type === "api" && /^!sorths\b/i.test(msg.content) && playerIsGM(msg.playerid) ) {       sortTurnOrderDesc();     };   }); });
1525035216
GiGs
Pro
Sheet Author
API Scripter
Thanks, it works perfectly!