Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name Auto MG for Real
- // @namespace http://tampermonkey.net/
- // @version 1.1.3
- // @description Continuously do MG when autobattle is available or help with travelling and clicking
- // @author nobody
- // @downloadURL https://gist.github.com/delgado3d/eb83808d0fe1d6e5a598a5d6fc79ba7b/raw/auto-mg.user.js
- // @updateURL https://gist.github.com/delgado3d/eb83808d0fe1d6e5a598a5d6fc79ba7b/raw/auto-mg.user.js
- // @match https://www.lordswm.com/mercenary_guild.php*
- // @grant none
- // ==/UserScript==
- // ER - 2
- // PC - 6
- // FT - 16
- // FV - 21
- const MG_OFFICE = 2;
- // -1 means reject
- // 0 and positive number means accept upto that level
- // eg: 13 at invaders means accept all levels until 13 (1,2,3....12,13) not 14 and above
- // set very high number to accept all levels; for eg 9999
- const QUEST_LIMIT = [
- 9999,
- 9999,
- 13, // invaders
- -1, // brigand
- 5, // monster
- 6, // raid
- 9999,
- 12, // vanguard
- 9999,
- 15, // army
- 13, // conspi
- 9999
- ];
- // dont change anything below this
- const MG_OFFICE_LOCATIONS = [2, 6, 16, 21];
- const KEY = "autoMGEnabled";
- const isEnabled = () => localStorage.getItem(KEY) === "1" || false;
- const enableScript = () => localStorage.setItem(KEY, "1");
- const disableScript = () => localStorage.setItem(KEY, "0");
- let secondsRemaining;
- const printOnScreen = text => {
- const pTag = document.createElement("p");
- const textTag = document.createTextNode(text);
- pTag.appendChild(textTag);
- document.body.appendChild(pTag);
- };
- const QUEST_NAME = [
- 0,
- 0,
- "invaders",
- "brigands",
- "monster",
- "raid",
- 0,
- "vanguard",
- 0,
- "army",
- "conspi"
- ];
- QUEST_LIMIT.forEach((accept, i) => {
- if (accept === -1 && [10, 7, 4, 5, 9, 2, 3].includes(i)) {
- printOnScreen(`Note: ${QUEST_NAME[i]} being rejected`);
- }
- });
- const getResponseText = url =>
- new Promise(resolve => {
- const req = new XMLHttpRequest();
- req.open("GET", url, true);
- req.send();
- req.onreadystatechange = () => {
- if (req.readyState === 4 && req.status === 200) {
- return resolve(req.responseText);
- }
- return null;
- };
- });
- const timeRemainsForQuest = () => {
- if (
- document
- .getElementsByTagName("body")[0]
- .innerText.match(/Come back in (\d+) minut/)
- ) {
- printOnScreen("Next quest not yet available.\n");
- const minutes = Number(RegExp.$1);
- secondsRemaining = minutes * 60;
- if (minutes === 1) secondsRemaining = 40;
- return true;
- }
- return false;
- };
- const reloadPage = () => window.location.reload();
- const waitAndReload = seconds => {
- printOnScreen(`Refreshing in ${seconds} seconds.`);
- setTimeout(reloadPage, seconds * 1000);
- };
- const inDifferentLocation = () => {
- if (
- document
- .getElementsByTagName("body")[0]
- .innerText.match(/status: You are in/)
- ) {
- printOnScreen("Not at MG office.");
- return true;
- }
- return false;
- };
- const getDelta = text => {
- if (text.match(/Delta=(\d+)/)) return RegExp.$1;
- return -1;
- };
- const walkToMGOffice = () => {
- let destination = "https://www.lordswm.com/move_sector.php?id=2";
- if (MG_OFFICE_LOCATIONS.includes(MG_OFFICE))
- destination = `https://www.lordswm.com/move_sector.php?id=${MG_OFFICE}`;
- const req = new XMLHttpRequest();
- req.open("GET", destination, true);
- req.send();
- req.onreadystatechange = () => {
- if (req.readyState === 4 && req.status === 200) {
- const seconds = getDelta(req.responseText);
- printOnScreen("Walking to MG office.");
- waitAndReload(seconds);
- }
- };
- };
- const questAvailable = () => {
- if (document.getElementsByTagName("body")[0].innerText.match(/Accept/i))
- return true;
- return false;
- };
- const getQuestType = () => {
- const quests = document
- .getElementsByTagName("body")[0]
- .innerHTML.match(/'<b>[a-zA-Z0-9,\-'" ]+\{\d+\}<\/b>'/g);
- const questTypes = [];
- let questID;
- quests.forEach(quest => {
- if (quest.indexOf("onspirators") !== -1) questID = 10;
- else if (quest.indexOf("anguard") !== -1) questID = 7;
- else if (quest.indexOf("onster") !== -1) questID = 4;
- else if (quest.indexOf("raid") !== -1) questID = 5;
- else if (quest.indexOf("rmy of") !== -1) questID = 9;
- else if (quest.indexOf("nvaders") !== -1) questID = 2;
- else if (quest.indexOf("rigand") !== -1) questID = 3;
- questTypes.push({
- id: questID,
- level: parseInt(quest.match(/{(\d+)}/)[1], 10)
- });
- });
- return questTypes;
- // var bTag = document.getElementsByTagName("b");
- // var quest = bTag[bTag.length - 2].innerText;
- // if (!isNaN(quest)) quest = bTag[bTag.length - 3].innerText;
- // var refBtn = document
- // .getElementsByClassName("wbwhite")[0]
- // .getElementsByTagName("a")[1];
- // var quest_id = -1;
- // if (quest.indexOf("onspirators") != -1) quest_id = 10;
- // else if (quest.indexOf("anguard") != -1) quest_id = 7;
- // else if (quest.indexOf("onster") != -1) quest_id = 4;
- // else if (quest.indexOf("raid") != -1) quest_id = 5;
- // else if (quest.indexOf("rmy of") != -1) quest_id = 9;
- // else if (quest.indexOf("nvaders") != -1) quest_id = 2;
- // else if (quest.indexOf("rigand") != -1) quest_id = 3;
- // return quest_id;
- };
- const acceptOrRejectQuest = () => {
- const aTag = document.getElementsByTagName("a");
- const accepts = [];
- let decline = "";
- const awards = [];
- const quests = getQuestType();
- const selected = {
- accept: "",
- award: 9999999
- };
- for (let i = 0; i < aTag.length; i += 1) {
- if (aTag[i].innerText.match(/Accept/)) accepts.push(aTag[i].href);
- if (aTag[i].innerText.match(/Decline/)) decline = aTag[i].href;
- }
- const golds = document
- .getElementsByTagName("body")[0]
- .innerText.match(/Award: (\d+) gold/g);
- golds.forEach(c => awards.push(c.match(/\d+/g).toString()));
- quests.forEach(({ id, level }, index) => {
- if (QUEST_LIMIT[id] === -1) return;
- if (level > QUEST_LIMIT[id]) return;
- if (awards[index] < selected.award) {
- selected.award = awards[index];
- selected.accept = accepts[index];
- }
- });
- if (selected.accept) window.location.href = selected.accept;
- else window.location.href = decline;
- // var bTag = document.getElementsByTagName("b");
- // var acceptLink = bTag[bTag.length - 1].parentNode.href;
- // var rejectLink = bTag[bTag.length - 1].parentNode.nextElementSibling.href;
- // if (!ACCEPT_BRIGANDS_QUEST && getQuestType() === 3)
- // window.location.href = rejectLink;
- // else if (!ACCEPT_VANGUARD_QUEST && getQuestType() === 7)
- // window.location.href = rejectLink;
- // else window.location.href = acceptLink;
- };
- const waitForXSeconds = seconds =>
- new Promise(resolve => {
- printOnScreen(`Waiting for ${seconds} seconds`);
- setTimeout(resolve, seconds * 1000);
- });
- const checkForAutoBattle = async () => {
- const text = await getResponseText("https://www.lordswm.com/map.php");
- if (text && text.includes("Autobattle") && text.match(/href='([a-z0-9&?=_.]+)';/g)) {
- printOnScreen("Found autobattle");
- return RegExp.$1;
- }
- return null;
- };
- async function atQuestLocation() {
- if (!isEnabled()) return;
- const path = await checkForAutoBattle();
- if (path) {
- const location = `https://www.lordswm.com/${path}`;
- let text = await getResponseText(location);
- if (text && getDelta(text) > -1) {
- do {
- await waitForXSeconds(getDelta(text));
- text = await getResponseText(
- "https://www.lordswm.com/waiting_for_results.php"
- );
- } while (
- !(
- (text && text.match(/Back to guild/i)) ||
- (text && text.match(/Go to map/i))
- )
- );
- }
- if (text && text.match(/Back to guild/i)) {
- // will be instant travel
- window.location.href =
- "https://www.lordswm.com/waiting_for_results.php?exit=1&gps=1";
- } else if (text && text.match(/Go to map/i)) {
- // click link to go to map and click autobattle button
- text = await getResponseText(
- "https://www.lordswm.com/waiting_for_results.php?exit=2"
- );
- atQuestLocation();
- }
- } else {
- const [{ id }] = getQuestType();
- if (id === 3) {
- // handle brigands differently
- await getResponseText(
- `https://www.lordswm.com/map.php?action=accept_merc_task${id}`
- );
- await waitForXSeconds(5);
- reloadPage();
- } else if (id !== -1) {
- window.location.href = `https://www.lordswm.com/map.php?action=accept_merc_task${id}`;
- } else {
- printOnScreen("Could not understand quest");
- }
- }
- }
- const processQuest = async () => {
- printOnScreen("Assuming quest was accepted.");
- // wait to heal
- if (heart !== 100) {
- console.log(heart);
- await waitForXSeconds(59);
- reloadPage();
- return;
- }
- const imgTags = document.getElementsByTagName("img");
- const moveOut = imgTags[imgTags.length - 1].parentNode.href;
- const req = new XMLHttpRequest();
- req.open("GET", moveOut, true);
- req.send();
- req.onreadystatechange = () => {
- if (req.readyState === 4 && req.status === 200) {
- const travelTime = getDelta(req.responseText);
- printOnScreen(`Travelling for ${travelTime} seconds.`);
- if (travelTime > 0) {
- printOnScreen(
- `Combat page will automatically open in ${travelTime} seconds.`
- );
- window.setTimeout(atQuestLocation, travelTime * 1000);
- } else {
- // assume quest at same location
- // alert("Could not get travel time");
- atQuestLocation();
- }
- }
- };
- };
- const insertToggleElement = () => {
- const bTags = document.getElementsByTagName("b");
- const enabled = isEnabled();
- const color = enabled ? "green" : "red";
- const txt = enabled ? "&" : "@";
- const html = `<span><b id="auto-mg-toggle" style="color:${color}; cursor:pointer" title="${
- enabled ? "Disable" : "Enable"
- } Auto MG${enabled ? "" : ". Page will refresh."}">${txt}</b></span> `;
- let locationTag = null;
- for (let i = 0; i < bTags.length; i += 1) {
- if (bTags[i].innerText === "Mercenaries' guild") {
- locationTag = bTags[i];
- }
- }
- if (locationTag) {
- locationTag.insertAdjacentHTML("beforeBegin", html);
- }
- };
- const toggleScirpt = e => {
- if (isEnabled()) {
- disableScript();
- e.target.style.color = "red";
- e.target.innerText = "@";
- e.target.title = "Enable Auto MG. Page will refresh.";
- } else {
- enableScript();
- e.target.style.color = "green";
- e.target.innerText = "&";
- e.target.title = "Disable Auto MG";
- reloadPage();
- }
- };
- (async () => {
- // disable script on first run
- if (localStorage.getItem(KEY) === null) disableScript();
- insertToggleElement();
- document.getElementById("auto-mg-toggle").onclick = toggleScirpt;
- if (!isEnabled()) {
- printOnScreen("Script is disabled!");
- return;
- }
- if (timeRemainsForQuest()) {
- waitAndReload(secondsRemaining);
- } else if (inDifferentLocation()) {
- walkToMGOffice();
- } else if (questAvailable()) {
- await waitForXSeconds(1);
- acceptOrRejectQuest();
- } else if (isEnabled()) {
- // page after accepting... hopefully...
- processQuest();
- }
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement