MoD mode

try to manage the world!

  1. // ==UserScript==
  2. // @name MoD mode
  3. // @namespace http://tampermonkey.net/
  4. // @version 2024-05-25
  5. // @description try to manage the world!
  6. // @author You
  7. // @match https://www.erepublik.com/en
  8. // @icon https://www.google.com/s2/favicons?sz=64&domain=erepublik.com
  9. // @grant none
  10. // ==/UserScript==
  11. (function() {
  12. 'use strict';
  13.  
  14. // Define the list
  15. const _token = csrfToken;
  16. const dateTime = SERVER_DATA.serverTime.dateTime;
  17. let message = `*Philippines TW* \n`;
  18. var list = [{
  19. war_id: 205525,
  20. id: 639847,
  21. country_id: 49,
  22. region: 465
  23. }, {
  24. war_id: 205903,
  25. id: 639992,
  26. country_id: 81,
  27. region: 464
  28. }, {
  29. war_id: 206105,
  30. id: 640137,
  31. country_id: 10,
  32. region: 270
  33. }, {
  34. war_id: 209808,
  35. id: 639948,
  36. country_id: 59,
  37. region: 510
  38. }, {
  39. war_id: 211305,
  40. id: 639829,
  41. country_id: 24,
  42. region: 50
  43. }, {
  44. war_id: 211391,
  45. id: 639907,
  46. country_id: 31,
  47. region: 704
  48. }, {
  49. war_id: 212392,
  50. id: 640151,
  51. country_id: 15,
  52. region: 171
  53. }, {
  54. war_id: 213683,
  55. id: 639924,
  56. country_id: 168,
  57. region: 750
  58. }, {
  59. war_id: 213982,
  60. id: 639720,
  61. country_id: 54,
  62. region: 271
  63. }, {
  64. war_id: 213988,
  65. id: 639668,
  66. country_id: 164,
  67. region: 728
  68. }, {
  69. war_id: 214058,
  70. id: 640024,
  71. country_id: 82,
  72. region: 724
  73. }, {
  74. war_id: 214111,
  75. id: 640346,
  76. country_id: 77,
  77. region: 641
  78. }, {
  79. war_id: 214188,
  80. id: 641830,
  81. country_id: 169,
  82. region: 178
  83. }, {
  84. war_id: 214656,
  85. id: 652852,
  86. country_id: 165,
  87. region: 749
  88. }];
  89.  
  90. mainFunction();
  91.  
  92. async function mainFunction() {
  93. createDivElement('scan', 'sidebar', 'SCANNING ALL BATTLE');
  94. createDivElement('list', 'sidebar', '');
  95. const fetchTime = getCurrentUnixTimestamp();
  96. const fetchlist = await fetchData(`https://www.erepublik.com/en/military/campaignsJson/list?${fetchTime}`);
  97.  
  98. for (let item of list) {
  99. let country = fetchlist.countries[item.country_id].name;
  100.  
  101. // Call the function to check if war ID exists
  102. let result = await isWarIdExist(fetchlist, item.war_id); // Ensure to await the result
  103. let exists = result.exists;
  104. let battleData = result.battle;
  105. console.log(result);
  106. if (exists) {
  107. const region = battleData.region.name;
  108. const defC = fetchlist.countries[battleData.def.id].name;
  109. const defP = battleData.def.points;
  110. const invC = fetchlist.countries[battleData.inv.id].name;
  111. const invP = battleData.inv.points;
  112. console.log(region);
  113. message += `\n*${region}*\n🛡: ${defC} : ${defP} Points \n🗡: ${invC} : ${invP} Points\n`;
  114. } else {
  115. const battleId = item.id;
  116. const payload = payloadList(battleId, _token);
  117. const url = `https://www.erepublik.com/en/military/battle-console`
  118. const list = await PostRequest(payload, url);
  119. console.log(list.list[0].result);
  120. const result = list.list[0].result;
  121. const outcome = result.outcome;
  122. const winner = result.winner;
  123.  
  124. if (outcome === "defense" && winner === "Philippines") {
  125. const ready = await checkRegionExistence(fetchlist, item.region);
  126. let rta = `*not ready*`;
  127. if(ready){
  128. rta = `*ready*`;
  129. }
  130. const endTime = result.end;
  131. var times = compareTime(dateTime, endTime);
  132. console.log("Difference between dateTime and resultEnd:", times);
  133. message += `\nAttack *${country}* - auto in ${times}\n [${item.war_id}](https://www.erepublik.com/en/wars/show/${item.war_id}) ${rta}\n`;
  134. const linkText = `Attack ${country} - auto in ${times}`;
  135. const linkUrl = `https://www.erepublik.com/en/wars/show/${item.war_id}`;
  136. displayLinkInHtml('list', linkText, linkUrl);
  137.  
  138. } else {
  139. message += `\nWaiting attack from *${country}*\n`;
  140.  
  141. }
  142. await delay(3000);
  143. console.log("War ID", item.war_id, "does not exist in battles.");
  144. }
  145.  
  146. console.log("------------------------");
  147. }
  148. sendMessage(message);
  149. createDivElement('send', 'sidebar', 'DATA SENT TO CHANNEL');
  150. }
  151.  
  152.  
  153. // Function to check if a war_id exists in fetchlist.battles
  154. function isWarIdExist(fetchlist, warId) {
  155. for (let battleId in fetchlist.battles) {
  156. if (fetchlist.battles.hasOwnProperty(battleId)) {
  157. if (fetchlist.battles[battleId].war_id === warId) {
  158. return {
  159. exists: true,
  160. battle: fetchlist.battles[battleId]
  161. };
  162. }
  163. }
  164. }
  165. return {
  166. exists: false,
  167. battle: null
  168. };
  169. }
  170.  
  171. function checkRegionExistence(object, regionId) {
  172. // Iterate over battles object
  173. for (const battleId in object.battles) {
  174. const battle = object.battles[battleId];
  175. // Check if region id matches
  176. if (battle.region && battle.region.id === regionId) {
  177. // If region id exists, return false
  178. return false;
  179. }
  180. }
  181. // If region id doesn't exist, return true
  182. return true;
  183. }
  184.  
  185. // Function to get current UNIX timestamp
  186. function getCurrentUnixTimestamp() {
  187. const currentTime = new Date();
  188. const unixTimestamp = Math.floor(currentTime.getTime() / 1000); // Convert milliseconds to seconds
  189. return unixTimestamp;
  190. }
  191.  
  192. // Function to introduce a delay
  193. function delay(ms) {
  194. return new Promise(resolve => setTimeout(resolve, ms));
  195. }
  196.  
  197. function createDivElement(divId, parentId, textContent) {
  198. const parentElement = document.querySelector(`.${parentId}`);
  199. if (parentElement) {
  200. const newDiv = document.createElement('div');
  201. newDiv.id = divId;
  202. newDiv.textContent = textContent;
  203. parentElement.appendChild(newDiv);
  204. } else {
  205. console.error(`Parent element with class '${parentId}' not found.`);
  206. }
  207. }
  208.  
  209. // Function to display any value in HTML
  210. function displayValueInHtml(elementId, value) {
  211. const element = document.getElementById(elementId);
  212. if (element) {
  213. element.textContent = `${element.textContent} ${value}`;
  214. } else {
  215. console.error(`Element with ID '${elementId}' not found.`);
  216. }
  217. }
  218.  
  219. function displayLinkInHtml(containerId, linkText, linkUrl) {
  220. const containerElement = document.getElementById(containerId);
  221. if (containerElement) {
  222. const linkElement = document.createElement('a');
  223. linkElement.href = linkUrl;
  224. linkElement.target = '_blank';
  225. linkElement.textContent = linkText;
  226. containerElement.appendChild(linkElement);
  227. containerElement.appendChild(document.createElement('br'));
  228. } else {
  229. console.error(`Container element with ID '${containerId}' not found.`);
  230. }
  231. }
  232.  
  233. // Function to fetch data from a URL
  234. async function fetchData(url) {
  235. try {
  236. const response = await fetch(url);
  237. if (!response.ok) {
  238. throw new Error(`HTTP error! Status: ${response.status}`);
  239. }
  240. const data = await response.json();
  241. return data;
  242. } catch (error) {
  243. throw new Error(`Failed to fetch data from ${url}: ${error.message}`);
  244. }
  245. }
  246.  
  247.  
  248. async function PostRequest(payload, url) {
  249. try {
  250. const response = await fetch(url, {
  251. method: "POST",
  252. headers: {
  253. "Content-Type": "application/x-www-form-urlencoded"
  254. },
  255. body: Object.keys(payload)
  256. .map(key => `${encodeURIComponent(key)}=${encodeURIComponent(payload[key])}`)
  257. .join('&')
  258. });
  259.  
  260. const responseData = await response.json();
  261. return responseData;
  262. } catch (error) {
  263. console.error("Error:", error);
  264. return null;
  265. }
  266. }
  267.  
  268. // Function to construct the payload from variables
  269. function payloadList(battleId, _token) {
  270. const action = "warList";
  271. const Page = 1;
  272.  
  273.  
  274. return {
  275. battleId,
  276. action,
  277. Page,
  278. _token
  279. };
  280. }
  281.  
  282.  
  283. function compareTime(dateTime, resultEnd) {
  284. // Convert dateTime and resultEnd to Unix timestamps
  285. var dateTimeUnix = (new Date(dateTime)).getTime() / 1000;
  286. var resultEndUnix = (new Date(resultEnd)).getTime() / 1000;
  287.  
  288. // If resultEnd is earlier than dateTime, add 24 hours to resultEnd
  289. if (resultEndUnix < dateTimeUnix) {
  290. resultEndUnix += 24 * 3600; // 24 hours in seconds
  291. }
  292.  
  293. // Calculate the difference in seconds
  294. var differenceInSeconds = Math.abs(dateTimeUnix - resultEndUnix);
  295.  
  296. // Convert difference to HH:MM:SS format
  297. var hours = Math.floor(differenceInSeconds / 3600);
  298. var minutes = Math.floor((differenceInSeconds % 3600) / 60);
  299. var seconds = Math.floor(differenceInSeconds % 60);
  300.  
  301. // Format the difference as HH:MM:SS
  302. var formattedDifference = hours.toString().padStart(2, '0') + ":" +
  303. minutes.toString().padStart(2, '0') + ":" +
  304. seconds.toString().padStart(2, '0');
  305.  
  306. return formattedDifference;
  307. }
  308.  
  309. function sendMessage(message) {
  310. var botToken = '6423448975:AAGmYbAXaC0rTuIDH-2SoNXhjPLdjayX35c';
  311. var chatId = '-1002078934269';
  312.  
  313. var apiUrl = 'https://api.telegram.org/bot' + botToken + '/sendMessage?chat_id=' + chatId + '&text=' + encodeURIComponent(message) + '&parse_mode=markdown&disable_web_page_preview=true';
  314.  
  315. // Make the HTTP request
  316. fetch(apiUrl)
  317. .then(response => response.json())
  318. .then(data => console.log(data))
  319. .catch(error => console.error('Error:', error));
  320. }
  321.  
  322.  
  323.  
  324.  
  325. })();