chi / 地下城英雄挂机脚本

// ==UserScript==
// @author       chi
// @copyright    2024, chi (https://openuserjs.org/users/chi)
// @name         地下城英雄挂机脚本
// @namespace    https://github.com
// @version      0.0.1
// @description  地下城英雄挂机脚本
// @include      *
// @updateURL    
// @license MIT
// @note	 更新
// ==/UserScript==

let lastClickedButtonText = null;

function clickButton(buttonText) {
  let buttons = document.querySelectorAll("button");
  for (let button of buttons) {
    if (button.innerText.includes(buttonText)) {
      button.click();
      lastClickedButtonText = buttonText;
      return { success: true, clickedButton: buttonText };
    }
  }
  return { success: false, clickedButton: null };
}

function clickButtonBasedOnText() {
  const buttons = [
    document.getElementById("lvlSlot0"),
    document.getElementById("lvlSlot1"),
    document.getElementById("lvlSlot2"),
  ];
  const priorities = [
    // 按顺序进行属性加点,调整先后顺序即可改变加点优先级
    { keyword: "生命值", button: null },
    { keyword: "吸血", button: null },
    { keyword: "攻速", button: null },
    { keyword: "攻击力", button: null },
    { keyword: "防御力", button: null },
    { keyword: "暴击", button: null },
  ];
  buttons.forEach((button) => {
    if (button && button.textContent) {
      priorities.forEach((priority) => {
        if (button.textContent.includes(priority.keyword) && !priority.button) {
          priority.button = button;
        }
      });
    }
  });
  for (const priority of priorities) {
    if (priority.button) {
      priority.button.click();
      break;
    }
  }
}

function getLastTagContent() {
  const dungeonLogDiv = document.getElementById("dungeonLog");
  if (dungeonLogDiv) {
    const pTags = dungeonLogDiv.getElementsByTagName("p");
    return pTags[pTags.length - 1].innerText;
  } else {
    return null;
  }
}

class TimerTask {
  constructor(delay, task) {
    this.delay = delay;
    this.task = task;
  }

  execute() {
    return new Promise((resolve) => {
      const executeTask = () => {
        const result = this.task();
        if (result === true) {
          resolve();
        } else {
          setTimeout(executeTask, this.delay);
        }
      };
      executeTask();
    });
  }
}

class TimerChainManager {
  constructor() {
    this.tasks = [];
  }

  addTask(delay, task) {
    this.tasks.push(new TimerTask(delay, task));
  }

  executeChain() {
    let sequence = Promise.resolve();
    for (let task of this.tasks) {
      sequence = sequence.then(() => task.execute());
    }
    sequence.then(() => {
      this.executeChain();
    });
    return sequence;
  }
}

const timerChain = new TimerChainManager();

timerChain.addTask(100, () => {
  const battleButtons = document.querySelectorAll("#battleButton");
  if (battleButtons.length > 0) {
    battleButtons.forEach((button) => button.click());
  }
  const lvlupSelectDiv = document.getElementById("lvlupSelect");
  if (lvlupSelectDiv) {
    clickButtonBasedOnText();
  }

  let lastText = getLastTagContent();
  if (lastText === "你发现了一道通往boss房间的门。") {
    let enterResult = clickButton("进入");
    // let enterResult = clickButton("忽视");
    if (enterResult.success) {
      return true;
    }
  }

  if (lastText === "你发现了一个被诅咒") {
    let enterResult = clickButton("忽视");
    if (enterResult.success) {
      return true;
    }
  }

  if (lastText.includes("你发现了一个祝福雕像")) {
    let enterResult = clickButton("忽视");
    if (enterResult.success) {
      return true;
    }
  }

  if (lastText.includes("你遇到了")) {
    let enterResult = clickButton("战斗");
    if (enterResult.success) {
      return true;
    }
  }

  if (lastText.includes("你发现了一个宝藏室")) {
    let enterResult = clickButton("打开箱子");
    if (enterResult.success) {
      return true;
    }
  }

  if (lastText.includes("你发现了一个神秘的房间")) {
    let enterResult = clickButton("进入");
    // let enterResult = clickButton("忽视");
    if (enterResult.success) {
      return true;
    }
  }

  if (lastText.includes("你发现了一道门")) {
    let enterResult = clickButton("进入");
    // let enterResult = clickButton("忽视");
    if (enterResult.success) {
      return true;
    }
  }
});

setTimeout(() => {
    timerChain.executeChain().then(() => {
        console.log("任务链执行完毕");
    });
  }, 5000);