Auto Work at night

try to take over the world!

  1. // ==UserScript==
  2. // @name Auto Work at night
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.2
  5. // @description try to take over the world!
  6. // @author You
  7. // @match https://www.erepublik.com/en/main/messages-inbox
  8. // @grant GM.setValue
  9. // @grant GM.getValue
  10. // ==/UserScript==
  11.  
  12. var isEatingHungry = true; // true - eat as soon as possible; false - eat when buffer is full
  13. var remainingEnergyBeforeStartEat = 1020; // valid only when isEatingHungry = false
  14. var countCompaniesToWork = 20; // valid only when isEatingHungry = false
  15. var minEnergyToWork = 800; // valid only when isEatingHungry = true
  16.  
  17. var timeBeforeRefreshMin = 1;
  18. var timeMaxTimeWithoutRefreshMin = 60;
  19. var alwaysAutoEat = true;
  20.  
  21. var session_key_last_work_day = "last_work_day";
  22.  
  23. async function getSession(key){
  24. return await GM.getValue(key);
  25. }
  26. async function setSession(key, value){
  27. await GM.setValue(key, value);
  28. }
  29.  
  30. /*
  31. (function() {
  32. $j(document).ready(function(){
  33. debugger
  34. // () => async {
  35. // let x = await GM.getValue(session_key_last_work_day);
  36. // alert(x);
  37. // await setSession(session_key_last_work_day, "123");
  38. // }
  39. setInterval(main, 1 * 60 * 1000); // 1 min
  40. //main();
  41. });
  42. })();
  43. */
  44. window.addEventListener("load", function(){
  45. //alert(123);
  46. // make initial eat of food if possible
  47. var isHome = isHomePage();
  48. if(alwaysAutoEat && isHome && canEatFood()){
  49. eatFood();
  50. }
  51.  
  52. setInterval(main, 1 * 60 * 1000);
  53. logInfo("page loaded");
  54. });
  55.  
  56.  
  57.  
  58. function canEatFood(){
  59. var foodRemaining = parseInt($j("big.tooltip_health_limit")[0].innerText);
  60. logInfo("remaining food = " + foodRemaining);
  61. //alert(foodRemaining);
  62. var canEat = foodRemaining > 10 && erepublik.citizen.energyToRecover - erepublik.citizen.energy >= 4;
  63. //alert(canEat);
  64. return canEat;
  65. }
  66. function eatFood(){
  67. logInfo("eating food");
  68. $j(".eat_food_wide").click();
  69. }
  70. function getAvailableEnergy(){
  71. var foodRemaining = parseInt($j("big.tooltip_health_limit")[0].innerText);
  72. return erepublik.citizen.energy + foodRemaining; //erepublik.citizen.energyFromFoodRemaining;
  73. }
  74. function getCountCompaniesCanworkWithEnergy(){
  75. var number = Math.floor (getAvailableEnergy() / 10 );
  76. if(!isEatingHungry){
  77. number = Math.min(number, countCompaniesToWork);
  78. }
  79. //alert("work in = "+number);
  80. return number;
  81. }
  82.  
  83. function isTimeToWork()
  84. {
  85. var totalCapacity = erepublik.citizen.energyToRecover * 2;
  86. var foodRemaining = parseInt($j("big.tooltip_health_limit")[0].innerText);
  87. var currentEnergy = erepublik.citizen.energy + foodRemaining; //erepublik.citizen.energyFromFoodRemaining;
  88.  
  89. var hasReachedMaxLimit = (currentEnergy + remainingEnergyBeforeStartEat) >= totalCapacity;
  90. var hasReachedMinLimit = currentEnergy >= minEnergyToWork;
  91. var result = (isEatingHungry && hasReachedMinLimit)
  92. || (!isEatingHungry && hasReachedMaxLimit);
  93. // alert("isTimeToWork = "+ result);
  94. return result;
  95. }
  96.  
  97. function isHomePage()
  98. {
  99. var isHome = $j("#hpTopNews").length == 1;
  100. return isHome;
  101. }
  102.  
  103. function gotoHomePage()
  104. {
  105. logInfo("going to home page");
  106. location.href = "https://www.erepublik.com/en";
  107. }
  108.  
  109. function gotoCompanies()
  110. {
  111. logInfo("going to companies");
  112. location.href = "https://www.erepublik.com/en/economy/myCompanies" + "?mine=mine-script";
  113. }
  114.  
  115. var isActivatedTracking;
  116. var loadTime;
  117. function tryToRedirectHome() {
  118. if(!loadTime){
  119. loadTime = new Date();
  120. }
  121. var now = new Date();
  122. var isTooMuch = now.getTime() - loadTime.getTime() > timeBeforeRefreshMin*60*1000;
  123. var needForceRefresh = now.getTime() - loadTime.getTime() > timeMaxTimeWithoutRefreshMin*60*1000;
  124. if(isTooMuch || erepublik.citizen.energy == 0){
  125. gotoHomePage();
  126. }
  127. if(needForceRefresh){
  128. window.location.reload();
  129. }
  130. else{
  131. if(!isActivatedTracking){
  132. isActivatedTracking = true;
  133. setInterval(tryToRedirectHome, 10000);
  134. }
  135. }
  136. }
  137.  
  138. function workNow()
  139. {
  140. logInfo("workNow() called");
  141. var counter = 0;
  142.  
  143. // remove all checkboxes
  144. $j("div.list_group .listing:not(div.disabled) a.owner_work").removeClass("active");
  145.  
  146. var countCompaniesCanworkWithEnergy = getCountCompaniesCanworkWithEnergy();
  147. $j("div.list_group .listing:not(div.disabled) a.owner_work")
  148. .each(function(idx, item)
  149. {
  150. if(idx >= countCompaniesCanworkWithEnergy)
  151. return;
  152. //if(idx >= countCompaniesToWork)
  153. // return;
  154. //alert(counter);
  155. $j(item).click();
  156. counter++;
  157. });
  158.  
  159. $j("#start_production").click();
  160. }
  161.  
  162.  
  163.  
  164.  
  165. function main()
  166. {
  167. logInfo("enter main...");
  168. var isTimeToWrk = isTimeToWork();
  169. var isHome = isHomePage();
  170.  
  171. var isMyCompaniesScreen = window.location.href.includes("erepublik.com/en/economy/myCompanie");
  172. var isMineLink = window.location.search.includes("mine-script");
  173.  
  174. if(alwaysAutoEat && isHome && canEatFood()){
  175. // eat only in home to avoid wrong values of food remaining
  176. eatFood();
  177. }
  178.  
  179. if(isHome){
  180. if(isTimeToWrk){
  181. gotoCompanies();
  182. }
  183. }
  184. else
  185. {
  186. if(isMineLink)
  187. {
  188. if(isMyCompaniesScreen)
  189. {
  190. workNow();
  191. }
  192. }
  193. // todo - try to redirect home
  194. tryToRedirectHome();
  195. }
  196. logInfo("...exit main");
  197. }
  198.  
  199. function logInfo(info)
  200. {
  201. console.info(info + " " + new Date());
  202. }