Advertisement
shubhamgoyal

Untitled

Dec 30th, 2020
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.24 KB | None | 0 0
  1. // ==UserScript==
  2. // @name Auto MG for Real
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.1.3
  5. // @description Continuously do MG when autobattle is available or help with travelling and clicking
  6. // @author nobody
  7. // @downloadURL https://gist.github.com/delgado3d/eb83808d0fe1d6e5a598a5d6fc79ba7b/raw/auto-mg.user.js
  8. // @updateURL https://gist.github.com/delgado3d/eb83808d0fe1d6e5a598a5d6fc79ba7b/raw/auto-mg.user.js
  9. // @match https://www.lordswm.com/mercenary_guild.php*
  10. // @grant none
  11. // ==/UserScript==
  12.  
  13. // ER - 2
  14. // PC - 6
  15. // FT - 16
  16. // FV - 21
  17. const MG_OFFICE = 2;
  18.  
  19. // -1 means reject
  20. // 0 and positive number means accept upto that level
  21. // eg: 13 at invaders means accept all levels until 13 (1,2,3....12,13) not 14 and above
  22. // set very high number to accept all levels; for eg 9999
  23. const QUEST_LIMIT = [
  24. 9999,
  25. 9999,
  26. 13, // invaders
  27. -1, // brigand
  28. 5, // monster
  29. 6, // raid
  30. 9999,
  31. 12, // vanguard
  32. 9999,
  33. 15, // army
  34. 13, // conspi
  35. 9999
  36. ];
  37.  
  38. // dont change anything below this
  39.  
  40. const MG_OFFICE_LOCATIONS = [2, 6, 16, 21];
  41.  
  42. const KEY = "autoMGEnabled";
  43.  
  44. const isEnabled = () => localStorage.getItem(KEY) === "1" || false;
  45. const enableScript = () => localStorage.setItem(KEY, "1");
  46. const disableScript = () => localStorage.setItem(KEY, "0");
  47.  
  48. let secondsRemaining;
  49.  
  50. const printOnScreen = text => {
  51. const pTag = document.createElement("p");
  52. const textTag = document.createTextNode(text);
  53. pTag.appendChild(textTag);
  54. document.body.appendChild(pTag);
  55. };
  56.  
  57. const QUEST_NAME = [
  58. 0,
  59. 0,
  60. "invaders",
  61. "brigands",
  62. "monster",
  63. "raid",
  64. 0,
  65. "vanguard",
  66. 0,
  67. "army",
  68. "conspi"
  69. ];
  70.  
  71. QUEST_LIMIT.forEach((accept, i) => {
  72. if (accept === -1 && [10, 7, 4, 5, 9, 2, 3].includes(i)) {
  73. printOnScreen(`Note: ${QUEST_NAME[i]} being rejected`);
  74. }
  75. });
  76.  
  77. const getResponseText = url =>
  78. new Promise(resolve => {
  79. const req = new XMLHttpRequest();
  80. req.open("GET", url, true);
  81. req.send();
  82. req.onreadystatechange = () => {
  83. if (req.readyState === 4 && req.status === 200) {
  84. return resolve(req.responseText);
  85. }
  86. return null;
  87. };
  88. });
  89.  
  90. const timeRemainsForQuest = () => {
  91. if (
  92. document
  93. .getElementsByTagName("body")[0]
  94. .innerText.match(/Come back in (\d+) minut/)
  95. ) {
  96. printOnScreen("Next quest not yet available.\n");
  97. const minutes = Number(RegExp.$1);
  98. secondsRemaining = minutes * 60;
  99. if (minutes === 1) secondsRemaining = 40;
  100. return true;
  101. }
  102. return false;
  103. };
  104.  
  105. const reloadPage = () => window.location.reload();
  106.  
  107. const waitAndReload = seconds => {
  108. printOnScreen(`Refreshing in ${seconds} seconds.`);
  109. setTimeout(reloadPage, seconds * 1000);
  110. };
  111.  
  112. const inDifferentLocation = () => {
  113. if (
  114. document
  115. .getElementsByTagName("body")[0]
  116. .innerText.match(/status: You are in/)
  117. ) {
  118. printOnScreen("Not at MG office.");
  119. return true;
  120. }
  121. return false;
  122. };
  123.  
  124. const getDelta = text => {
  125. if (text.match(/Delta=(\d+)/)) return RegExp.$1;
  126. return -1;
  127. };
  128.  
  129. const walkToMGOffice = () => {
  130. let destination = "https://www.lordswm.com/move_sector.php?id=2";
  131. if (MG_OFFICE_LOCATIONS.includes(MG_OFFICE))
  132. destination = `https://www.lordswm.com/move_sector.php?id=${MG_OFFICE}`;
  133. const req = new XMLHttpRequest();
  134. req.open("GET", destination, true);
  135. req.send();
  136. req.onreadystatechange = () => {
  137. if (req.readyState === 4 && req.status === 200) {
  138. const seconds = getDelta(req.responseText);
  139. printOnScreen("Walking to MG office.");
  140. waitAndReload(seconds);
  141. }
  142. };
  143. };
  144.  
  145. const questAvailable = () => {
  146. if (document.getElementsByTagName("body")[0].innerText.match(/Accept/i))
  147. return true;
  148. return false;
  149. };
  150.  
  151. const getQuestType = () => {
  152. const quests = document
  153. .getElementsByTagName("body")[0]
  154. .innerHTML.match(/'<b>[a-zA-Z0-9,\-'" ]+\{\d+\}<\/b>'/g);
  155. const questTypes = [];
  156. let questID;
  157. quests.forEach(quest => {
  158. if (quest.indexOf("onspirators") !== -1) questID = 10;
  159. else if (quest.indexOf("anguard") !== -1) questID = 7;
  160. else if (quest.indexOf("onster") !== -1) questID = 4;
  161. else if (quest.indexOf("raid") !== -1) questID = 5;
  162. else if (quest.indexOf("rmy of") !== -1) questID = 9;
  163. else if (quest.indexOf("nvaders") !== -1) questID = 2;
  164. else if (quest.indexOf("rigand") !== -1) questID = 3;
  165.  
  166. questTypes.push({
  167. id: questID,
  168. level: parseInt(quest.match(/{(\d+)}/)[1], 10)
  169. });
  170. });
  171. return questTypes;
  172.  
  173. // var bTag = document.getElementsByTagName("b");
  174. // var quest = bTag[bTag.length - 2].innerText;
  175. // if (!isNaN(quest)) quest = bTag[bTag.length - 3].innerText;
  176. // var refBtn = document
  177. // .getElementsByClassName("wbwhite")[0]
  178. // .getElementsByTagName("a")[1];
  179. // var quest_id = -1;
  180. // if (quest.indexOf("onspirators") != -1) quest_id = 10;
  181. // else if (quest.indexOf("anguard") != -1) quest_id = 7;
  182. // else if (quest.indexOf("onster") != -1) quest_id = 4;
  183. // else if (quest.indexOf("raid") != -1) quest_id = 5;
  184. // else if (quest.indexOf("rmy of") != -1) quest_id = 9;
  185. // else if (quest.indexOf("nvaders") != -1) quest_id = 2;
  186. // else if (quest.indexOf("rigand") != -1) quest_id = 3;
  187. // return quest_id;
  188. };
  189.  
  190. const acceptOrRejectQuest = () => {
  191. const aTag = document.getElementsByTagName("a");
  192. const accepts = [];
  193. let decline = "";
  194. const awards = [];
  195. const quests = getQuestType();
  196. const selected = {
  197. accept: "",
  198. award: 9999999
  199. };
  200. for (let i = 0; i < aTag.length; i += 1) {
  201. if (aTag[i].innerText.match(/Accept/)) accepts.push(aTag[i].href);
  202. if (aTag[i].innerText.match(/Decline/)) decline = aTag[i].href;
  203. }
  204. const golds = document
  205. .getElementsByTagName("body")[0]
  206. .innerText.match(/Award: (\d+) gold/g);
  207. golds.forEach(c => awards.push(c.match(/\d+/g).toString()));
  208. quests.forEach(({ id, level }, index) => {
  209. if (QUEST_LIMIT[id] === -1) return;
  210. if (level > QUEST_LIMIT[id]) return;
  211. if (awards[index] < selected.award) {
  212. selected.award = awards[index];
  213. selected.accept = accepts[index];
  214. }
  215. });
  216. if (selected.accept) window.location.href = selected.accept;
  217. else window.location.href = decline;
  218.  
  219. // var bTag = document.getElementsByTagName("b");
  220. // var acceptLink = bTag[bTag.length - 1].parentNode.href;
  221. // var rejectLink = bTag[bTag.length - 1].parentNode.nextElementSibling.href;
  222. // if (!ACCEPT_BRIGANDS_QUEST && getQuestType() === 3)
  223. // window.location.href = rejectLink;
  224. // else if (!ACCEPT_VANGUARD_QUEST && getQuestType() === 7)
  225. // window.location.href = rejectLink;
  226. // else window.location.href = acceptLink;
  227. };
  228.  
  229. const waitForXSeconds = seconds =>
  230. new Promise(resolve => {
  231. printOnScreen(`Waiting for ${seconds} seconds`);
  232. setTimeout(resolve, seconds * 1000);
  233. });
  234.  
  235. const checkForAutoBattle = async () => {
  236. const text = await getResponseText("https://www.lordswm.com/map.php");
  237. if (text && text.includes("Autobattle") && text.match(/href='([a-z0-9&?=_.]+)';/g)) {
  238. printOnScreen("Found autobattle");
  239. return RegExp.$1;
  240. }
  241. return null;
  242. };
  243.  
  244. async function atQuestLocation() {
  245. if (!isEnabled()) return;
  246. const path = await checkForAutoBattle();
  247. if (path) {
  248. const location = `https://www.lordswm.com/${path}`;
  249. let text = await getResponseText(location);
  250. if (text && getDelta(text) > -1) {
  251. do {
  252. await waitForXSeconds(getDelta(text));
  253. text = await getResponseText(
  254. "https://www.lordswm.com/waiting_for_results.php"
  255. );
  256. } while (
  257. !(
  258. (text && text.match(/Back to guild/i)) ||
  259. (text && text.match(/Go to map/i))
  260. )
  261. );
  262. }
  263. if (text && text.match(/Back to guild/i)) {
  264. // will be instant travel
  265. window.location.href =
  266. "https://www.lordswm.com/waiting_for_results.php?exit=1&gps=1";
  267. } else if (text && text.match(/Go to map/i)) {
  268. // click link to go to map and click autobattle button
  269. text = await getResponseText(
  270. "https://www.lordswm.com/waiting_for_results.php?exit=2"
  271. );
  272. atQuestLocation();
  273. }
  274. } else {
  275. const [{ id }] = getQuestType();
  276. if (id === 3) {
  277. // handle brigands differently
  278. await getResponseText(
  279. `https://www.lordswm.com/map.php?action=accept_merc_task${id}`
  280. );
  281. await waitForXSeconds(5);
  282. reloadPage();
  283. } else if (id !== -1) {
  284. window.location.href = `https://www.lordswm.com/map.php?action=accept_merc_task${id}`;
  285. } else {
  286. printOnScreen("Could not understand quest");
  287. }
  288. }
  289. }
  290.  
  291. const processQuest = async () => {
  292. printOnScreen("Assuming quest was accepted.");
  293. // wait to heal
  294. if (heart !== 100) {
  295. console.log(heart);
  296. await waitForXSeconds(59);
  297. reloadPage();
  298. return;
  299. }
  300. const imgTags = document.getElementsByTagName("img");
  301. const moveOut = imgTags[imgTags.length - 1].parentNode.href;
  302. const req = new XMLHttpRequest();
  303. req.open("GET", moveOut, true);
  304. req.send();
  305. req.onreadystatechange = () => {
  306. if (req.readyState === 4 && req.status === 200) {
  307. const travelTime = getDelta(req.responseText);
  308. printOnScreen(`Travelling for ${travelTime} seconds.`);
  309. if (travelTime > 0) {
  310. printOnScreen(
  311. `Combat page will automatically open in ${travelTime} seconds.`
  312. );
  313. window.setTimeout(atQuestLocation, travelTime * 1000);
  314. } else {
  315. // assume quest at same location
  316. // alert("Could not get travel time");
  317. atQuestLocation();
  318. }
  319. }
  320. };
  321. };
  322.  
  323. const insertToggleElement = () => {
  324. const bTags = document.getElementsByTagName("b");
  325. const enabled = isEnabled();
  326. const color = enabled ? "green" : "red";
  327. const txt = enabled ? "&" : "@";
  328. const html = `<span><b id="auto-mg-toggle" style="color:${color}; cursor:pointer" title="${
  329. enabled ? "Disable" : "Enable"
  330. } Auto MG${enabled ? "" : ". Page will refresh."}">${txt}</b></span> `;
  331. let locationTag = null;
  332. for (let i = 0; i < bTags.length; i += 1) {
  333. if (bTags[i].innerText === "Mercenaries' guild") {
  334. locationTag = bTags[i];
  335. }
  336. }
  337. if (locationTag) {
  338. locationTag.insertAdjacentHTML("beforeBegin", html);
  339. }
  340. };
  341.  
  342. const toggleScirpt = e => {
  343. if (isEnabled()) {
  344. disableScript();
  345. e.target.style.color = "red";
  346. e.target.innerText = "@";
  347. e.target.title = "Enable Auto MG. Page will refresh.";
  348. } else {
  349. enableScript();
  350. e.target.style.color = "green";
  351. e.target.innerText = "&";
  352. e.target.title = "Disable Auto MG";
  353. reloadPage();
  354. }
  355. };
  356.  
  357. (async () => {
  358. // disable script on first run
  359. if (localStorage.getItem(KEY) === null) disableScript();
  360.  
  361. insertToggleElement();
  362. document.getElementById("auto-mg-toggle").onclick = toggleScirpt;
  363.  
  364. if (!isEnabled()) {
  365. printOnScreen("Script is disabled!");
  366. return;
  367. }
  368.  
  369. if (timeRemainsForQuest()) {
  370. waitAndReload(secondsRemaining);
  371. } else if (inDifferentLocation()) {
  372. walkToMGOffice();
  373. } else if (questAvailable()) {
  374. await waitForXSeconds(1);
  375. acceptOrRejectQuest();
  376. } else if (isEnabled()) {
  377. // page after accepting... hopefully...
  378. processQuest();
  379. }
  380. })();
  381.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement