Checking S0mething 2

Find S0mething in s0mewhere

当前为 2023-08-20 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Checking S0mething 2
  3. // @version 1.666.0
  4. // @description Find S0mething in s0mewhere
  5. // @author S0me1
  6. // @match https://www.erepublik.com/*
  7. // @grant GM_setValue
  8. // @grant GM_getValue
  9. // @grant GM_addStyle
  10. // @namespace https://greasyfork.org/users/1144622
  11. // ==/UserScript==
  12.  
  13. (function () {
  14. // Function to construct the payload from variables
  15. function constructPayload(battleId, zoneId, round, division, battleZoneId, _token) {
  16. const action = "battleStatistics";
  17. const type = "damage";
  18. const leftPage = 1;
  19. const rightPage = 1;
  20.  
  21. return {
  22. battleId,
  23. zoneId,
  24. action,
  25. round,
  26. division,
  27. battleZoneId,
  28. type,
  29. leftPage,
  30. rightPage,
  31. _token
  32. };
  33. }
  34.  
  35. // Function to send the payload using POST request
  36. async function sendPayload(payload) {
  37. const url = "https://www.erepublik.com/en/military/battle-console";
  38.  
  39. try {
  40. const response = await fetch(url, {
  41. method: "POST",
  42. headers: {
  43. "Content-Type": "application/x-www-form-urlencoded"
  44. },
  45. body: Object.keys(payload)
  46. .map(key => `${encodeURIComponent(key)}=${encodeURIComponent(payload[key])}`)
  47. .join('&')
  48. });
  49.  
  50. const responseData = await response.json();
  51. return responseData;
  52. } catch (error) {
  53. console.error("Error:", error);
  54. return null;
  55. }
  56. }
  57.  
  58. // Function to construct the payload for inventory
  59. function constructPayloadForInventory(battleId, sideCountryId, battleZoneId, _token) {
  60. return {
  61. battleId,
  62. sideCountryId,
  63. battleZoneId,
  64. _token
  65. };
  66. }
  67.  
  68. // Function to send the payload using POST request (modified for inventory)
  69. async function sendPayloadForInventory(payload) {
  70. const url = "https://www.erepublik.com/en/military/fightDeploy-getInventory";
  71.  
  72. try {
  73. const response = await fetch(url, {
  74. method: "POST",
  75. headers: {
  76. "Content-Type": "application/x-www-form-urlencoded"
  77. },
  78. body: Object.keys(payload)
  79. .map(key => `${encodeURIComponent(key)}=${encodeURIComponent(payload[key])}`)
  80. .join('&')
  81. });
  82.  
  83. const responseData = await response.json();
  84. return responseData;
  85. } catch (error) {
  86. console.error("Error:", error);
  87. return null;
  88. }
  89. }
  90.  
  91. // Function to construct the payload for fight deploy
  92. function constructPayloadForFightDeploy(battleId, battleZoneId, sideCountryId, weaponQuality, totalEnergy, skinId, _token) {
  93. return {
  94. battleId,
  95. battleZoneId,
  96. sideCountryId,
  97. weaponQuality,
  98. totalEnergy,
  99. skinId,
  100. _token
  101. };
  102. }
  103.  
  104. // Function to send a POST request for fight deploy
  105. async function sendPostRequestForFightDeploy(payload) {
  106. const url = 'https://www.erepublik.com/en/military/fightDeploy-startDeploy';
  107.  
  108. try {
  109. const response = await fetch(url, {
  110. method: 'POST',
  111. headers: {
  112. 'Content-Type': 'application/x-www-form-urlencoded'
  113. },
  114. body: Object.keys(payload)
  115. .map(key => `${encodeURIComponent(key)}=${encodeURIComponent(payload[key])}`)
  116. .join('&')
  117. });
  118.  
  119. const responseData = await response.json();
  120. return responseData;
  121. } catch (error) {
  122. console.error('Error:', error);
  123. return null;
  124. }
  125. }
  126.  
  127. // Function to check if country.fighterData is empty and push to new array if empty
  128. function checkFighterDataEmpty(responseData, battleId, countryLocationId, emptyBattleIds) {
  129. if (responseData && responseData[countryLocationId] && responseData[countryLocationId]["fighterData"]) {
  130. const fighterData = responseData[countryLocationId]["fighterData"];
  131. const isFighterDataEmpty = Object.keys(fighterData).length === 0;
  132. console.log(`Is ${countryLocationId}.fighterData empty?`, isFighterDataEmpty);
  133.  
  134. if (isFighterDataEmpty) {
  135. emptyBattleIds.push(battleId);
  136. }
  137. } else {
  138. console.log(`Could not find ${countryLocationId}.fighterData in the response.`);
  139. }
  140. }
  141.  
  142. // Function to process battles and update data arrays
  143. async function processBattles(countryList, division, countryLocationId, _token) {
  144. // Fetch data from the first URL
  145. const response1 = await fetch("https://www.erepublik.com/en/military/campaignsJson/list");
  146. const data1 = await response1.json();
  147.  
  148. let dataBattle = {}; // Store battle data by ID
  149. let idbattle = []; // Store battle IDs
  150.  
  151. // Get battles object from the first JSON file
  152. var battles1 = data1.battles;
  153.  
  154. for (var battleId in battles1) {
  155. if (battles1.hasOwnProperty(battleId)) {
  156. var battle = battles1[battleId];
  157. var dw = battle.war_type;
  158.  
  159. if (dw == "direct"){
  160. var inv = battle.inv.id;
  161. var def = battle.def.id;
  162. var inva = battle.inv.allies;
  163. var defa = battle.def.allies;
  164.  
  165. if (countryList.includes(inv) && countryList.includes(def)) {
  166. if (inv === countryLocationId || def === countryLocationId) {
  167. dataBattle[battle.id] = battle;
  168. idbattle.push(battle.id);
  169. }
  170.  
  171. if (inva.includes(countryLocationId) || defa.includes(countryLocationId)){
  172. dataBattle[battle.id] = battle;
  173. idbattle.push(battle.id);
  174. }
  175. }
  176. }
  177. }
  178. }
  179.  
  180. await delay(2000); // 1000 milliseconds = 1 second
  181. // Fetch data from the second URL
  182. const response2 = await fetch("https://www.erepublik.com/en/military/campaignsJson/citizen");
  183. const data2 = await response2.json();
  184.  
  185. // Get battles object from the second JSON file
  186. var battles2 = data2.battles;
  187.  
  188. // Prepare dataDivision object
  189. let dataDivision = {};
  190.  
  191. for (let i = 0; i < idbattle.length; i++) {
  192. let battleId = idbattle[i];
  193.  
  194. // Check if the battleId exists in the battles object from the second JSON file
  195. if (battles2.hasOwnProperty(battleId)) {
  196. // Store the relevant battle data in the dataDivision object using battle ID as the index
  197. dataDivision[battleId] = battles2[battleId].selectedBattleZoneId;
  198. }
  199. }
  200.  
  201. return { dataBattle, idbattle, dataDivision };
  202. }
  203.  
  204. function delay(ms) {
  205. return new Promise(resolve => setTimeout(resolve, ms));
  206. }
  207.  
  208. // Main function
  209. async function main() {
  210. let division = erepublik.citizen.division; // Using citizen's division
  211. let _token = csrfToken; // Replace with actual _token
  212.  
  213. // Assuming erepublik is defined and citizen.countryLocationId is accessible
  214. let countryLocationId = erepublik.citizen.countryLocationId;
  215.  
  216. // Declare the countryList array for later use
  217. var countryList = [9, 42, 23, 14, 70, 39, 48, 49, 54, 56, 71, 72, 66, 57, 67, 35, 53, 81, 1, 68, 30, 40, 24];
  218.  
  219. // Process battles and update data arrays
  220. const { dataBattle, idbattle, dataDivision } = await processBattles(countryList, division, countryLocationId, _token);
  221.  
  222. console.log("dataBattle:", dataBattle); // Print dataBattle to console
  223. console.log("idbattle:", idbattle); // Print idbattle to console
  224. console.log("dataDivision:", dataDivision); // Print dataDivision to console
  225.  
  226. // Create an array to store empty battle IDs
  227. let emptyBattleIds = [];
  228.  
  229. // Iterate through each battle ID and perform a task
  230. for (let i = 0; i < idbattle.length; i++) {
  231. let currentBattleId = idbattle[i];
  232. let selectedBattleZoneId = dataDivision[currentBattleId];
  233. let cb = dataBattle[currentBattleId];
  234. let zoneId = cb.zone_id; // Using zone_id from dataBattle
  235. let round = zoneId; // Using round from dataBattle
  236.  
  237. // Perform your task here for each battle ID
  238. console.log(`Task for battle ID ${currentBattleId}: selectedBattleZoneId = ${selectedBattleZoneId}`);
  239.  
  240. // Example: You can use this data to construct a payload and send it using sendPayload function
  241. const payload = constructPayload(currentBattleId, zoneId, round, division, selectedBattleZoneId, _token);
  242. const responseData = await sendPayload(payload);
  243.  
  244. // Call the function and pass currentBattleId, countryLocationId, and emptyBattleIds
  245. checkFighterDataEmpty(responseData, currentBattleId, countryLocationId, emptyBattleIds);
  246. await delay(1000); // 1000 milliseconds = 1 second
  247. }
  248.  
  249. // Print the array of empty battle IDs
  250. console.log("Empty Battle IDs:", emptyBattleIds);
  251.  
  252. // Continue with other functions and logic here
  253.  
  254. // Iterate through each emptyBattleIds and perform a task
  255. for (let j = 0; j < emptyBattleIds.length; j++) {
  256. let target = emptyBattleIds[j];
  257. let zonetarget = dataDivision[target];
  258.  
  259. const payload2 = constructPayloadForInventory(target, countryLocationId, zonetarget, _token);
  260. const inventory = await sendPayloadForInventory(payload2);
  261. const listweapon = inventory.weapons;
  262. const energya = inventory.poolEnergy;
  263. const vehicles = inventory.vehicles;
  264. const weaponQuality = 10; //change with weapon that you want to use
  265. const energyuse = 15; //change with energy to deploy
  266.  
  267. // Find the object with quality 10 in the weapons response array
  268. const objectWithQuality = listweapon.find(item => item.quality === weaponQuality);
  269.  
  270. // Get the value of the 'amount' property if the object is found
  271. let weaponAmount = null;
  272. if (objectWithQuality) {
  273. weaponAmount = objectWithQuality.amount;
  274. }
  275.  
  276. // Find the object with vehicle isRecommended:true
  277. const skinRecommended = vehicles.find(skin => skin.isRecommended === true);
  278.  
  279. // Get the value of the 'amount' property if the object is found
  280. let skinId = null;
  281. if (skinRecommended) {
  282. skinId = skinRecommended.id;
  283. }
  284.  
  285.  
  286. // Log the amount value
  287. console.log(inventory);
  288. console.log("Amount with Quality 10:", weaponAmount);
  289. console.log("IDrecommended:", skinId);
  290.  
  291. if(energya > energyuse && weaponAmount > 2){
  292. const fightDeployPayload = constructPayloadForFightDeploy(target, zonetarget, countryLocationId, weaponQuality, energyuse, skinId, _token);
  293. const fightDeployResponse = await sendPostRequestForFightDeploy(fightDeployPayload);
  294. console.log(fightDeployResponse);
  295. await delay(10000); // 1000 milliseconds = 1 second
  296. }
  297.  
  298. await delay(1000); // 1000 milliseconds = 1 second
  299. }
  300. }
  301.  
  302. // Add an event listener for page load
  303. window.addEventListener('load', function() {
  304. // Call the main function when the page is fully loaded
  305. main();
  306. });
  307. })();