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

Changing turn order with drag changes pr from int to sting?

1410552862

Edited 1410553675
DXWarlock
Sheet Author
API Scripter
I have a script that sorts turn order by high to low, announces who goes and what order they go in which works fine long as I dont drag someone in the turn order to a new place. The issue is it should still sort regardless and re order them as Im getting the PR of each entry sorting high to low and pushing back to the turn order..but once you drag a person to a new place. the 'pr' turns to a string. For example I set initiative for 2 people and log turn order I get: [{"id":"-JWAEQmyMkuBCLa_d_lb","pr":19},{"id":"-JWAEQ8sNSHHbWvWgurJ","pr":3}] BUT when I drag the one guy to the top and log turn order it returns this [{"id":"-JWAEQ8sNSHHbWvWgurJ","pr":"3","custom":""},{"id":"-JWAEQmyMkuBCLa_d_lb","pr":"19","custom":""}] Where the pr is now a string. Only thing I did to cause the change is drag a turn to a new place. Its easy to check with this code: on('chat:message', function (msg) { if (msg.type == 'api' && msg.content.indexOf('!check') !== -1) { order = JSON.parse(Campaign().get("turnorder")); log(order); } }); set 2 peoples initiative with the auto add &{tracker} do !check drag the bottom guy to the top and do !check Long as you're not editing the pr should it not stay the same type of variable?
1410560526

Edited 1410560632
Lithl
Pro
Sheet Author
API Scripter
In most cases, it shouldn't matter. Javascript freely converts between strings and numbers. As for why it's happening, most likely because the VTT has to resync things with Firebase after you make the manual change, and the VTT isn't going to assume that you're using just numbers in case you're playing a game that uses playing cards for the turn ordering. If the value being a string is a problem, you can simply convert the string to a number. parseInt(pr) is the most versatile and robust solution, while pr | 0 is the fastest.
1410565487

Edited 1410565814
DXWarlock
Sheet Author
API Scripter
Well this issue I have is I'm doing this in my script to reorder the list before using it. Which works great sorting them everytime unless I drag and pr becomes a string from a int. function sort(order) { var nOrd = order.sortByProp('pr'); Campaign().set("turnorder", JSON.stringify(nOrd)); } Array.prototype.sortByProp = function (p) { return this.sort(function (a, b) { return(a[p] < b[p]) ? 1 : (a[p] > b[p]) ? -1 : 0; }); }; Which fails on the sorting, as the pr string aren't sortable by greater or lesser than. My problem is I got the sortbyprop from either you or Aaron long ago, and no matter how I log the info I can't find the pr being passed to parseInt them. (or if it is being passed, that whole Array.prototype.sortByProp part far as I know is 'javascript gnomes" doing their magic it worked when I added it, but not sure how...haha. If I tried to duplicate what it does, Id have 400 If/elseif/else to do the same thing.)
1410570390
The Aaron
Roll20 Production Team
API Scripter
function sort(order) { var nOrd = order.sortByProp('pr'); Campaign().set("turnorder", JSON.stringify(nOrd)); } Array.prototype.sortByProp = function (p) { return this.sort(function (a, b) { return( parseInt (a[p],10) < parseInt (b[p],10) ) ? 1 : ( parseInt (a[p],10) > parseInt (b[p],10)) ? -1 : 0; }); }; should fix it...
1410595668

Edited 1410596223
Lithl
Pro
Sheet Author
API Scripter
Aaron, I recommend typechecking the value before parsing it. It may be desirable to have sortByProp sort lexicographically. var aIsNumeric = _.isFinite(a[p]); var bIsNumeric = _.isFinite(b[p]); if (aIsNumeric && bIsNumeric) { // Compare after parsing } else { // Compare without parsing } Also, thinking about it, I recommend using parseFloat over parseInt , since some people do use fractional values in their turn orders!
1410621046
The Aaron
Roll20 Production Team
API Scripter
Good points Brian! :)
1410627612
DXWarlock
Sheet Author
API Scripter
AH thats Aaron and brian! I was going mad figure out where in that thing the pr actually was reading :)