Greasy Fork is available in English.

Ikariam Automation

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

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