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 .
×
Our team is currently investigating rolling service outages. For more information, visit our most recent Forum Announcements post.
Create a free account

Sorting turnorder based on cards

I am writing a script to sort the Turn Order list every time I click the [Deal Cards to Turn Order Items] button. I know what needs to be done to make it happen, but I have a problem with the pr values having the suit icon in the value. How would one handle this? Ascii characters? Here are the values I've gotten, for an example: Thanks!
I'm close, it's just not sorting reverse suit in the case of ties... on('change:campaign:turnorder',function(obj){ var turnOrderRaw = obj.get('turnorder'); var turnOrder = []; if(turnOrderRaw != ''){ turnOrder = JSON.parse(turnOrderRaw); } turnOrder.sort(function(a,b){ return a.pr.localeCompare(b.pr); }).reverse(); Campaign().set('turnorder',JSON.stringify(turnOrder)); });
Ok, taken care of! on('change:campaign:turnorder',function(obj){ var turnOrderRaw = obj.get('turnorder'); var turnOrder = []; if(turnOrderRaw != ''){ turnOrder = JSON.parse(turnOrderRaw); } turnOrder.sort(function(a,b){ return a.pr.localeCompare(b.pr); }).reverse(); var prevVal = ''; var ord; for(i = 0; i < turnOrder.length; i++){ ord = turnOrder[i]; if(prevVal == ''){ prevVal = ord; } if(ord.pr.charAt(0) == prevVal.pr.charAt(0)){ turnOrder[i-1] = ord; turnOrder[i] = prevVal; } } Campaign().set('turnorder',JSON.stringify(turnOrder)); });
Still a couple of kinks to work out. King and Queen don't sort right, and sometimes the suits don't sort right. It's close though.
1392252197
Stephen S.
Pro
Marketplace Creator
Sheet Author
API Scripter
can you add another value for sorting and then just drop it for all other things?
My thinking right now is to handle RJo,BJo,A,K,Q,J cases within the sort and manipulate the returned value myself. More complex, but that's the best option I can think of currently since it isn't a straight forward asc/desc sort for face cards.
I'm unsure how the API actually works but is there anyway to have it activate the built in card sort functionality. If not maybe some of your kinks could be fixed by reading the card value, converting to a number between 1-54 and then sort using those values?
1392322598
Stephen S.
Pro
Marketplace Creator
Sheet Author
API Scripter
cursingbulldog said: I'm unsure how the API actually works but is there anyway to have it activate the built in card sort functionality. If not maybe some of your kinks could be fixed by reading the card value, converting to a number between 1-54 and then sort using those values? +1
I took cursingbulldog's advice, though didn't take it all the way up to 54. I converted face cards to values 11 - 15 (jokers are both 15), and figured out the char codes for the suit shapes and assigned them a number of 1 - 4. In the case of a tie, I add the suit number to the card number then subtract the first from the second to see if it should be before or after it in the list. I then reverse the list after the sort. on('change:campaign:turnorder',function(obj){ var swInitiativeManager = { compare: function (card1, card2) { var val1 = this.faceToNumber(card1.substring(0, card1.length - 1)); var val2 = this.faceToNumber(card2.substring(0, card2.length - 1)); var suit1 = this.codeToSuit(card1.charCodeAt(card1.length - 1)); var suit2 = this.codeToSuit(card2.charCodeAt(card2.length - 1)); if (val1 == val2) { val1 += suit1; val2 += suit2; } return val1 - val2; }, faceToNumber: function (value) { if (isNaN(value)) { switch (value.toUpperCase()) { case 'RJ': case 'BJ': return 15; case 'A': return 14; case 'K': return 13; case 'Q': return 12; case 'JA': return 11; } }else{ return value; } }, codeToSuit: function (value) { switch (value) { case 9827: // clubs return 1; case 9830: // diamonds return 2; case 9829: // hearts return 3; case 9824: // spades return 4; default: // joker return value; } } }; var turnOrderRaw = obj.get('turnorder'); var turnOrder = []; if(turnOrderRaw != ''){ turnOrder = JSON.parse(turnOrderRaw); } turnOrder.sort(function(a,b){ return swInitiativeManager.compare(a.pr,b.pr); }).reverse(); Campaign().set('turnorder',JSON.stringify(turnOrder)); });
1392463279
Alex L.
Pro
Sheet Author
Stephen S. said: cursingbulldog said: I'm unsure how the API actually works but is there anyway to have it activate the built in card sort functionality. If not maybe some of your kinks could be fixed by reading the card value, converting to a number between 1-54 and then sort using those values? +1 What card sort function?
The Turn Order can be set to sort Ascending or Descending, but for Savage Worlds Initiative it's sorted 2 different ways. It's sorted in Descending order by value and also in Descending order by Suit. The standard sort does not sort properly by Suit.