SGWG Population

Rozmisteni populace

  1. // ==UserScript==
  2. // @name SGWG Population
  3. // @namespace ui
  4. // @version 0.3
  5. // @author Ondřej Jodas
  6. // @match http://sgwg.net/planety.php
  7. // @match http://stargate-dm.cz/planety.php
  8. // @grant none
  9. // @description Rozmisteni populace
  10. // ==/UserScript==
  11.  
  12. $.widget('ui.population', {
  13. _create: function(){
  14. var self = this,
  15. element = self.element,
  16. maxPopulation = self.options.maxPopulation
  17.  
  18. self._fillData();
  19. $('#rozdelit').click(function(){
  20. self._allocators();
  21. });
  22. },
  23.  
  24. _fillData : function() {
  25. var self = this,
  26. element = self.element,
  27. listTr = element.find('tr');
  28.  
  29. var lastTrAllTd = $(listTr.last()).find('td');
  30. self._freePopulation = parseFloat(lastTrAllTd[1].textContent.replace(/ /g, "").replace(",", "."));
  31.  
  32. self._totalPlanet = listTr.length-2;
  33.  
  34. self._populationToPlanet = Math.round(self._freePopulation*10/self._totalPlanet)/10;
  35.  
  36. var insertToTd = lastTrAllTd.last();
  37.  
  38. insertToTd.html($('<button>', {
  39. id: 'rozdelit',
  40. text: 'Rovnoměrně rozmístit'
  41. }));
  42. },
  43.  
  44. _allocators: function() {
  45. var self = this,
  46. element = self.element,
  47. tmpAllocate = [];
  48. tmpAllocate['fromPlanet'] = [];
  49. tmpAllocate['toPlanet'] = [];
  50. tmpAllocate['errorPlanet'] = [];
  51.  
  52. $.each($(element.find('tr')), function (number, actualTr){
  53. if (number != 0 && number <= self._totalPlanet) {
  54. var tds = $(actualTr).find('td');
  55. var population = parseFloat(tds[1].textContent.replace(/ /g, "").replace(",", "."));
  56. if (population-self._populationToPlanet < 0) {
  57. var count = Math.round((population-self._populationToPlanet)*10)/10*-1;
  58. var maxPop = parseFloat(tds[2].textContent.replace(/ /g, "").replace(",", "."));
  59. tmpAllocate['toPlanet'][tmpAllocate['toPlanet'].length] = {
  60. id: parseInt($(tds[0]).find('a').attr('href').replace("planety_detaily.php?id=", "")),
  61. count: count
  62. }
  63. if (count > maxPop) {
  64. tmpAllocate['errorPlanet'][tmpAllocate['errorPlanet'].length] = {
  65. name: tds[0].textContent,
  66. count: count-maxPop
  67. }
  68. }
  69. } else {
  70. tmpAllocate['fromPlanet'][tmpAllocate['fromPlanet'].length] = {
  71. id: parseInt($(tds[0]).find('a').attr('href').replace("planety_detaily.php?id=", "")),
  72. count: Math.round((population-self._populationToPlanet)*10)/10
  73. }
  74. }
  75. }
  76. });
  77. if (tmpAllocate['errorPlanet'].length > 0 ) {
  78. var errorPlanet = "Na nasledujicich planetach neni dostatek mista\n";
  79. $.each(tmpAllocate['errorPlanet'], function(key, value){
  80. errorPlanet = errorPlanet + value.name+" chybi "+ value.count + " mista\n";
  81. });
  82. alert(errorPlanet);
  83. } else {
  84. $.each(tmpAllocate['toPlanet'], function(toKey, toValue) {
  85. var missPop = toValue.count;
  86. $.each(tmpAllocate['fromPlanet'], function(fromKey, fromValue) {
  87. if (fromValue.count > 0 && missPop > 0) {
  88. if (fromValue.count > (missPop + 1)) {
  89. var move = missPop;
  90. tmpAllocate['fromPlanet'][fromKey].count = tmpAllocate['fromPlanet'][fromKey].count - move;
  91. } else {
  92. var move = fromValue.count;
  93. tmpAllocate['fromPlanet'][fromKey].count = 0;
  94. }
  95. missPop = missPop-move;
  96. var data = {
  97. zobraz: 3,
  98. antihack: self.options.antihack,
  99. z_pl: fromValue.id,
  100. na_pl: toValue.id,
  101. p_lidi: move
  102. };
  103. //http://sgwg.net/planety-ajax.php?zobraz=3&z_pl=652&na_pl=652&p_lidi=&antihack=5158415
  104. $.ajax({
  105. type: "GET",
  106. url: "planety-ajax.php",
  107. data: data
  108. });
  109. }
  110. });
  111. });
  112. }
  113. }
  114. });
  115.  
  116. $(function(){
  117. $('button').click(function() {
  118. var checkExist = setInterval(function () {
  119. if ($('#statistika_ajax').find('.full').length) {
  120. var antihack = $( "input[name='antihack']" ).val();
  121. $('#statistika_ajax').find('.full').population({antihack: antihack});
  122. clearInterval(checkExist);
  123. }
  124. }, 100);
  125. });
  126. });