Planets.nu - Sort Scoreboard

Allow sorting of the Scoreboard

  1. // ==UserScript==
  2. // @name Planets.nu - Sort Scoreboard
  3. // @description Allow sorting of the Scoreboard
  4. // @include http://planets.nu/home
  5. // @include http://play.planets.nu/*
  6. // @include http://planets.nu/*
  7. // @include http://planets.nu/games/*
  8. // @include http://test.planets.nu/*
  9. // @version 0.4
  10.  
  11. // @namespace https://greasyfork.org/users/2888
  12. // ==/UserScript==
  13. // 0.1 - Initial version: Enable sorting and add Slot number
  14. // 0.2 - Change default sort column to planet count, not Race name
  15. // 0.3 - Add small enhancement to Planets table: Mark planets building SB next turn with an "O"
  16. // 0.4 - Show Starbases also on resource view
  17.  
  18. function wrapper () { // wrapper for injection
  19. /* Overload the default showPlanets method to show
  20. * a "O" where a Starbase will be build next turn
  21. */
  22. /***** BEGIN Unchanged code *****/
  23. vgapDashboard.prototype.showPlanets = function(g) {
  24. vgap.playSound("button");
  25. vgap.closeSecond();
  26. this.content.empty();
  27. var c = "";
  28. if (!g) {
  29. g = 0
  30. }
  31. var b = $("<ul class='FilterMenu'></ul>").appendTo(this.content);
  32. $("<li " + (g == 0 ? "class='SelectedFilter'" : "") + ">Colony View</li>").tclick(function() {
  33. vgap.dash.showPlanets(0)
  34. }).appendTo(b);
  35. $("<li " + (g == 1 ? "class='SelectedFilter'" : "") + ">Resource View</li>").tclick(function() {
  36. vgap.dash.showPlanets(1)
  37. }).appendTo(b);
  38. c = "<div class='DashPane' style='height:" + ($("#DashboardContent").height() - 70) + "px;'>";
  39. c += "<table id='PlanetTable' align='left' class='CleanTable' border='0' width='100%' style='cursor:pointer;'><thead>";
  40. c += "<th></th><th align='left'>Id</th><th align='left'>Name</th>";
  41. if (g == 1) {
  42. c += "<th title='Starbase' align='left'>SB</th><th title='Megacredits' align='left'>MC</th><th title='Supplies' align='left'>S</th><th title='Neutronium' align='left'>N</th><th title='Duranium' align='left'>D</th><th title='Tritanium' align='left'>T</th><th title='Molybdenum' align='left'>M</th><th title='Ground Neutronium (unmined)' align='left'>GN</th><th title='Ground Duranium (unmined)' align='left'>GD</th><th title='Ground Tritanium (unmined)' align='left'>GT</th><th title='Ground Molybdenum (unmined)' align='left'>GM</th><th title='Neutronium Density' align='left'>DN</th><th title='Duranium Density' align='left'>DD</th><th title='Tritanium Density' align='left'>DT</th><th title='Molybdenum Density' align='left'>DM</th>"
  43. }
  44. if (g == 0) {
  45. c += "<th title='Starbase' align='left'>SB</th><th align='left'>FC</th><th title='Temperature' align='left'>T</th><th title='Colonists' align='left'>Cols</th><th title='Colonist Tax Rate' align='left'>Tx</th><th title='Colonist Happiness' align='left'>Hp</th><th title='Colonist Happiness Change' align='left'>+/-</th><th title='Natives' align='left'>Natives</th><th title='Native Government' align='left'>Gov</th><th title='Native Population' align='left'>Pop</th><th title='Native Tax Rate' align='left'>Tx</th><th title='Native Happiness' align='left'>Hp</th><th title='Native Happiness Change' align='left'>+/-</th><th title='Ready Checkbox Status' align='left'>R</th>"
  46. }
  47. c += "</thead><tbody id='PlanetRows'></tbody></table></div>";
  48. this.pane = $(c).appendTo(this.content);
  49. for (var d = 0; d < vgap.myplanets.length; d++) {
  50. var e = vgap.myplanets[d];
  51. /***** END Unchanged code *****/
  52. var a = vgap.getStarbase(e.id) != null ? "X" : ( e.buildingstarbase ? "O" : "");
  53. // Original code:
  54. // var a = vgap.getStarbase(e.id) != null ? "X" : "";
  55. /***** BEGIN Unchanged code *****/
  56. c = "<tr class='RowSelect'><td><img class='TinyIcon' src='" + e.img + "'/></td><td>" + e.id + "</td><td>" + e.name + "</td>";
  57. if (g == 1) {
  58. c += "<td>" + a + "</td><td>" + e.megacredits + "</td><td>" + e.supplies + "</td><td>" + e.neutronium + "</td><td>" + e.duranium + "</td><td>" + e.tritanium + "</td><td>" + e.molybdenum + "</td><td>" + e.groundneutronium + "</td><td>" + e.groundduranium + "</td><td>" + e.groundtritanium + "</td><td>" + e.groundmolybdenum + "</td><td>" + e.densityneutronium + "</td><td>" + e.densityduranium + "</td><td>" + e.densitytritanium + "</td><td>" + e.densitymolybdenum + "</td></tr>"
  59. }
  60. if (g == 0) {
  61. c += "<td>" + a + "</td><td>" + e.friendlycode + "</td><td>" + e.temp + "</td><td>" + e.clans * 100 + "</td><td>" + e.colonisttaxrate + "</td><td>" + e.colonisthappypoints + "</td><td" + (e.colhappychange < 0 ? " class='WarnText' " : "") + ">" + e.colhappychange + "</td>";
  62. if (e.nativeclans > 0) {
  63. c += "<td>" + e.nativeracename + "</td><td>" + e.nativegovernmentname + "</td><td>" + e.nativeclans * 100 + "</td><td>" + e.nativetaxrate + "</td><td>" + e.nativehappypoints + "</td><td" + (e.nativehappychange < 0 ? " class='WarnText' " : "") + ">" + e.nativehappychange + "</td>"
  64. } else {
  65. c += "<td></td><td></td><td></td><td></td><td></td><td></td>"
  66. }
  67. c += "<td>" + (e.readystatus > 0 ? (e.readystatus == 1 ? "-" : "+") : "") + "</td></tr>"
  68. }
  69. var f = function(h) {
  70. return function() {
  71. vgap.map.selectPlanet(h)
  72. }
  73. };
  74. $(c).click(f(e.id)).appendTo("#PlanetRows")
  75. }
  76. $("#PlanetTable").tablesorter();
  77. this.pane.jScrollPane();
  78. vgap.CurrentView = "showPlanets";
  79. vgap.showPlanetsViewed = 1
  80. }
  81. /***** END Unchanged code *****/
  82. /* add custom data parser to overcome the issue of wrong text sorting
  83. * The normal text parser sorts e.g. 137 as lower than 95. And we can't
  84. * simply use numeric sorting because we want to see the diffs compared
  85. * to the turn before: 136 (+4)
  86. * which makes the table entry a String
  87. */
  88. $.tablesorter.addParser({
  89. // set a unique id
  90. id: 'data',
  91. is: function(s) {
  92. // return false so this parser is not auto detected
  93. return false;
  94. },
  95. format: function(s, table, cell, cellIndex) {
  96. var $cell = $(cell);
  97.  
  98. // return column specific data value
  99. return parseInt($cell.attr('data-value'),10) || 0;
  100. },
  101. // set type to numeric
  102. type: 'numeric'
  103. });
  104. // Overload the default showScores method
  105. vgapDashboard.prototype.showScores = function (view) {
  106. vgap.playSound("button");
  107. vgap.closeSecond();
  108. this.content.empty();
  109. /* tbl_hd is the head row of the default Scoreboard
  110. * we add an additional column for the slot number here
  111. * can be used to figure out gs freindly code
  112. */
  113. var tbl_hd = "<table id='ScoreTable' width='100%' class='CleanTable' style='cursor:pointer;'><thead>";
  114. tbl_hd += "<tr><th title='Use for gs Friendly code'>Slot#</th><th>Race (player)</th><th>Planets</th><th>Starbases</th><th>War Ships</th><th>Freighters</th><th>Military</th><th>Score</th><th>Priority Points</th></tr></thead>";
  115. var sum_planets = 0;
  116. var sum_sb = 0;
  117. var sum_warships = 0;
  118. var sum_freight = 0;
  119. // per player content rows
  120. var main_tbl = "<tbody>";
  121. for (var i = 0; i < vgap.scores.length; i++) {
  122. var scores = vgap.scores[i];
  123. var slot = "";
  124. sum_planets += scores.planets;
  125. sum_sb += scores.starbases;
  126. sum_warships += scores.capitalships;
  127. sum_freight += scores.freighters;
  128. main_tbl += "<tr>";
  129. if (scores.ownerid >= 10) {
  130. slot = String.fromCharCode(scores.ownerid + 55);
  131. } else {
  132. slot = scores.ownerid + '';
  133. }
  134. main_tbl += "<td data-value='" + scores.ownerid + "'>" + slot + "</td>";
  135. if (vgap.game.gametype == 5 && vgap.game.turn <= 1 && scores.ownerid != vgap.player.id) {
  136. main_tbl += "<td>Unknown</td>";
  137. } else {
  138. main_tbl += "<td>" + vgap.raceName(scores.ownerid) + "</td>";
  139. }
  140. main_tbl += "<td data-value='" + scores.planets + "'>" + scores.planets + this.scoreChange(scores.planetchange) + "</td>";
  141. main_tbl += "<td data-value='" + scores.starbases + "'>" + scores.starbases + this.scoreChange(scores.starbasechange) + "</td>";
  142. main_tbl += "<td data-value='" + scores.capitalships + "'>" + scores.capitalships + this.scoreChange(scores.shipchange) + "</td>";
  143. main_tbl += "<td data-value='" + scores.freighters + "'>" + scores.freighters + this.scoreChange(scores.freighterchange) + "</td>";
  144. main_tbl += "<td data-value='" + scores.militaryscore + "'>" + scores.militaryscore + this.scoreChange(scores.militarychange) + "</td>";
  145. main_tbl += "<td data-value='" + scores.inventoryscore + "'>" + scores.inventoryscore + this.scoreChange(scores.inventorychange) + "</td>";
  146. main_tbl += "<td data-value='" + scores.prioritypoints + "'>" + scores.prioritypoints + this.scoreChange(scores.prioritypointchange) + "</td>";
  147. main_tbl += "</tr>";
  148. }
  149. // Add footer with sums
  150. tbl_hd += "<tfoot><tr><th></th><th>Totals:</th><th>" + sum_planets + "</th><th>" + sum_sb + "</th><th>" + sum_warships + "</th><th>" + sum_freight + "</th></tr></tfoot>";
  151. // Write out the whole pane now
  152. var a = "<div class='DashPane'><div id='MessageInbox'>";
  153. a += "<table id='TotalScoreTable' width='40%' class='CleanTable' style='cursor:pointer;'><tbody>";
  154. // I think it is helpful to see the Totals as a summary at the beginning
  155. a += "<tr><td>Total Ships:</td><td>" + (sum_warships + sum_freight) + "</td><td>Total Planets:</td><td>" + sum_planets + "</td></tr>";
  156. a += "</tbody></table>";
  157. a += tbl_hd + main_tbl + "</tbody></table>";
  158. a += "<br/>Win Condition: <a href='http://planets.nu/documentation/win-conditions' target='_blank'>";
  159. if (vgap.game.wincondition == 3) {
  160. a += "Military Score";
  161. } else {
  162. if (vgap.game.wincondition == 1) {
  163. a += "Diplomatic Planets";
  164. }
  165. }
  166. a += "</a><br/>";
  167. $("#DashboardLeft").hide();
  168. if (!vgap.ie8 && !vgap.ie7) {
  169. this.content.width($("#PlanetsDashboard").width() - 44);
  170. }
  171. this.pane = $(a).appendTo(this.content);
  172. $("<a class='MoreBack'>Back</a>").tclick(function() {
  173. vgap.dash.closeScoreboard();
  174. return false;
  175. }).appendTo(this.pane);
  176. vgap.CurrentView = "showScores";
  177. vgap.showScoreViewed = 1;
  178. // Add the tablesorter
  179. $("#ScoreTable").tablesorter({
  180. sortInitialOrder: 'desc',
  181. sortList: [[2,1]],
  182. headers: {
  183. 0 : { sorter: 'data' },
  184. 2 : { sorter: 'data' },
  185. 3 : { sorter: 'data' },
  186. 4 : { sorter: 'data' },
  187. 5 : { sorter: 'data' },
  188. 6 : { sorter: 'data' },
  189. 7 : { sorter: 'data' },
  190. 8 : { sorter: 'data' }
  191. }});
  192. this.pane.jScrollPane();
  193. };
  194. } //wrapper for injection
  195.  
  196. var script = document.createElement("script");
  197. script.type = "application/javascript";
  198. script.textContent = "(" + wrapper + ")();";
  199.  
  200. document.body.appendChild(script);