Ikariam Automation

Attempts to automate all the routine tasks in ikariam, like transporting wine

当前为 2017-04-10 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Ikariam Automation
  3. // @namespace Danielv123
  4. // @version 1.1
  5. // @description Attempts to automate all the routine tasks in ikariam, like transporting wine
  6. // @author Danielv123
  7. // @match http*://*.ikariam.gameforge.com/*
  8. // @grant unsafeWindow
  9. // ==/UserScript==
  10.  
  11. function main () {
  12. // Ikariam user tools
  13. // SEND CTs ---------------------------------------------------------
  14. // check if the CT option exists in message dialog
  15. // document.querySelector("option[value='77']").click();
  16. // Send the message
  17. // document.querySelector("#js_messageSubmitButton").click()
  18. // SEND RESOURCES ---------------------------------------------------
  19. window.sendAlot = function(townNumber, townFromNumber, resource, amount) {
  20. localStorage.resource = resource;
  21. localStorage.amount = amount;
  22. localStorage.destination = townNumber;
  23. localStorage.origin = townFromNumber;
  24. asdadas();
  25. //sendResources(townNumber, resource, window.asdadas);
  26. };
  27. window.asdadas = function(){
  28. if(localStorage.amount < 0) {
  29. transporterStatus = "Nothing to do, all resources sent";
  30. }
  31. if(document.querySelector("#js_GlobalMenu_freeTransporters").innerHTML < 2) {
  32. transporterStatus = "Waiting for free cargoships";
  33. }
  34. if(localStorage.paused == "true"){
  35. transporterStatus = "Script is paused";
  36. }
  37. if(localStorage.resource && localStorage.amount > 0) {
  38. //localStorage.amount -= 5000;
  39. sendResources(localStorage.destination, localStorage.origin, localStorage.resource);
  40. }
  41. };
  42. // send 5000 resources from one town to another
  43. window.sendResources = function(townNumber, fromTownNumber, resource, callback) {
  44. if(localStorage.paused == "true" /*|| Number(document.querySelector("#js_GlobalMenu_maxActionPoints").innerHTML) < 1*/ || document.querySelector("#js_GlobalMenu_freeTransporters").innerHTML < 2/* || document.querySelector("#js_CityPosition1PortCountdownText").innerHTML*/) {
  45. // if we have no action points OR the city is currently loading some ships, wait and do nothing.
  46. //setTimeout(function(){window.sendResources(townNumber,fromTownNumber,resource,callback);}, 10000);
  47. // NO, STOP IT, DON'T!
  48. // we already have a loop that keeps retrying. Relax.
  49. } else {
  50. // if sending to the same town, STOP IT. ITS NOT FUNNY.
  51. if(townNumber == fromTownNumber){
  52. throw "ERROR sending to same town? Not on my watch!";
  53. }
  54. // correct for the fact that the dock GUI does not show the currently selected town
  55. if(townNumber > fromTownNumber){
  56. townNumber--;
  57. }
  58. transporterStatus = "Going to town " + fromTownNumber + " to send resources";
  59. gotoTown(fromTownNumber, function(){
  60. if(Number(document.querySelector("#js_GlobalMenu_maxActionPoints").innerHTML) > 0){
  61. // click dock on the left (gotta have dock there, no shipyardy stuff)
  62. document.querySelector("#js_CityPosition1Link").click();
  63. setTimeout(function(){
  64. // get list if town sending targets from dock and click one
  65. document.querySelectorAll(".cities.clearfix > li > a")[townNumber].click();
  66. setTimeout(function(){
  67. // either pick totalShips/actionPoints or 20, whatever is smaller.
  68. let numberOfShips = Math.min(Math.floor(document.querySelector("#js_GlobalMenu_freeTransporters").innerHTML / Number(document.querySelector("#js_GlobalMenu_maxActionPoints").innerHTML)), 20);
  69. if(document.querySelector("#js_GlobalMenu_freeTransporters").innerHTML >= numberOfShips){
  70. // find the right resource selector slider and set it to 5k, then click the send button
  71. let resourceID = "#textfield_" + resource;
  72. // Set how much we send. Pick whatever is smaller of localStorage and what we can send right now.
  73. document.querySelector(resourceID).value = Math.min(500*numberOfShips, localStorage.amount);
  74. let sentAmount = Math.min(500*numberOfShips, localStorage.amount);
  75. document.querySelector("#submit").click();
  76. setTimeout(function(){
  77. // close window when stuff is sent
  78. transporterStatus = "Sent " + sentAmount + " " + resource;
  79. localStorage.amount -= sentAmount;
  80. if(callback && typeof callback == "function"){
  81. setTimeout(callback, 1000);
  82. }
  83. document.querySelector("div.close").click();
  84. }, 2000);
  85. } else {
  86. transporterStatus = "Not enough ships";
  87. }
  88. },2000);
  89. },2000);
  90. } else {
  91. transporterStatus = "No action points left";
  92. }
  93. }); // end gototown callback
  94. }
  95. };
  96. window.gotoTown = function(townNumber, callback) {
  97. console.log("going to town " +townNumber);
  98. document.querySelector("#js_citySelectContainer > span").click();
  99. setTimeout(function(){
  100. try{
  101. document.querySelectorAll("#dropDown_js_citySelectContainer > div.bg > ul > li > a")[townNumber].click();
  102. } catch (e){}
  103. setTimeout(callback, 1000);
  104. },1000);
  105. };
  106. window.getTownResources = function(townNumber, callback) {
  107. var checkResources = function() {
  108. console.log(typeof callback);
  109. var resources = {};
  110. resources.wood = stringToNumber(document.querySelector("#resources_wood > span").innerHTML);
  111. resources.wine = stringToNumber(document.querySelector("#resources_wine > span").innerHTML);
  112. resources.marble = stringToNumber(document.querySelector("#resources_marble > span").innerHTML);
  113. resources.glass = stringToNumber(document.querySelector("#resources_glass > span").innerHTML);
  114. resources.sulfur = stringToNumber(document.querySelector("#resources_sulfur > span").innerHTML);
  115. callback(resources);
  116. };
  117. gotoTown(townNumber, checkResources);
  118. };
  119. window.stringToNumber = function(str) {
  120. return parseFloat(str.replace(',','').replace(' ',''));
  121. };
  122. setInterval(asdadas, 10000);
  123. // update userscript status box
  124. var transporterStatus;
  125. setInterval(function(){
  126. $("#transporterMaterial")[0].innerHTML = "Material: " + localStorage.resource;
  127. $("#transporterAmount")[0].innerHTML = "Amount: " + localStorage.amount;
  128. // transporterStatus is a global variable that is assigned throughout the functions to give an approximate as to what the script is doing
  129. $("#transporterStatus")[0].innerHTML = transporterStatus;
  130. },1000);
  131. // create form to send resources
  132. window.createForm = function (){
  133. let HTML = '<div><span>From: </span><select id="transporterSendFromTown">';
  134. let townList = document.querySelector("#dropDown_js_citySelectContainer > div.bg > ul").childNodes;
  135. for(let i = 0; i < townList.length; i++){
  136. HTML += '<option value="'+i+'">'+townList[i].childNodes[0].innerHTML+'</option>';
  137. }
  138. HTML += '</select></div>';
  139. HTML += '<div><span>Destination: </span><select id="transporterSendDestination">';
  140. for(let i = 0; i < townList.length; i++){
  141. HTML += '<option value="'+i+'">'+townList[i].childNodes[0].innerHTML+'</option>';
  142. }
  143. HTML += '</select></div>';
  144. HTML += '<div><span>Resource: </span><select id="transporterSendResource">';
  145. HTML += '<option value="wood">Wood</option>';
  146. HTML += '<option value="wine">Wine</option>';
  147. HTML += '<option value="marble">Marble</option>';
  148. HTML += '<option value="glass">Crystal</option>';
  149. HTML += '<option value="sulfur">Sulfur</option>';
  150. HTML += '</select></div>';
  151. HTML += '<div><span>Amount: </span><input id="transporterSendAmount" type="number"></div>';
  152. HTML += '<button onclick="sendResourcesFromForm();">Send resources</button>';
  153. return HTML;
  154. };
  155. window.sendResourcesDialog = function () {
  156. // use ikariams built in fancy dialog box for our dialog for extra fancyness
  157. ikariam.createPopup("ikaMationTransporterDialog","Mass transport resources",createForm(),"???","class");
  158. };
  159. window.sendResourcesFromForm = function () {
  160. let destination = document.querySelector("#transporterSendDestination").value;
  161. let fromTown = document.querySelector("#transporterSendFromTown").value;
  162. let resource = document.querySelector("#transporterSendResource").value;
  163. let amount = document.querySelector("#transporterSendAmount").value;
  164. sendAlot(destination, fromTown, resource, amount);
  165. document.querySelector("#ikaMationTransporterDialog").outerHTML = "";
  166. };
  167. }
  168. $('body').append("<div id='userscript' style='position:fixed;background-color:white;z-index:100000000;bottom:0px;right:0px;height:150px;width:200px;'></div>");
  169. $("#userscript").append("<h2 style='font-size:20px;font-weight:bold;'>IkaMation</h2><button onclick='localStorage.paused = localStorage.paused == \"false\"'>Pause/resume script</button><p id='transporterMaterial'>Material: </p><p id='transporterAmount'>Amount:</p><p id='transporterStatus'></p>");
  170.  
  171. $("#userscript").append('<button onclick="sendResourcesDialog();">Mass send resources</button>');
  172.  
  173. var script = document.createElement('script');
  174. script.appendChild(document.createTextNode('('+ main +')();'));
  175. (document.body || document.head || document.documentElement).appendChild(script);
  176. /*
  177. let infoBox = document.createElement("div");
  178. infoBox.appendChild(document.createTextNode("<div id='userscript' style='position:fixed;background-color:white;z-index:100000000;bottom:0px;right:0px;height:100px;width:200px;'></div>"));
  179. (document.body|| document.documentElement).appendChild(infoBox);
  180. */