channox32 / Water-Crow-Harvest

// ==UserScript==
// @name Water-Crow-Harvest
// @namespace Script Runner Pro
// @match https://marketplace.plantvsundead.com/*
// @grant none
// @description Water-Crow-Harvest is a discord power tool to nofify user with PvU related tasks.
// @version 2.1
// @license MIT
// ==/UserScript==

(function () {
  const WATER_TYPE = 0;
  const CROW_TYPE = 1;
  const HARVEST_TYPE = 2;

  let loopCount = 0;
  let accountConfig = [{
    "account_name": "Vincent",
    "discord_id": "869025435273556049",
    "public_addresses": ["0xfa16543c44aee47d11e5b6e081ac0da098b249cb", "0x958549778b03fe5f5724f95ed2a939de0e7edb0e", "0x82fa8dc456d98756da0776d13c137bf7039d5116", "0x465e7b432d9f5a92bc7ad6134181bd5febfde671", "0x26afa0616b594732e6958e8627f6229b1cabf31e"]
  }, {
    "account_name": "Kirstine",
    "discord_id": "869025435273556049",
    "public_addresses": ["0x484ce2696bf2474fc6fef2219870a744e075c531"]
  }, {
    "account_name": "Spencer",
    "discord_id": "490551670074376212",
    "public_addresses": ["0xd8a0d6a91f1356bfc6b22ab97ca2c6e5e7304fa3", "0x633aaa6b86ece6af7960cd767d2992ea4ca4bd6f", "0x4fae9a5a7047a3af8c61c1ea04c4a3156c0a77b0", "0x4025161a85b33da5712f28e40fb777677cab718a", "0xa184b79b013b370794e6ac6e83eee9ac28f8e0ab"]
  }, {
    "account_name": "Christian",
    "discord_id": "321519973002510356",
    "public_addresses": ["0xc04c4b8e143ffec0b1bd28600250215eef84a392", "0xc774cc987ed47968ae95657ab2218ecc7640f1c4", "0xff9693bc7cc2725c40f7f5802003c3bf8f02eb01", "0x1a7104aba66a1a3f612cbaa326289999d6a4444c", "0x136e79a1182aa3499098e796418e4c0501b542b1"]
  }, {
    "account_name": "Marims",
    "discord_id": "884054391479484416",
    "public_addresses": ["0x76e0609011ed345085a9b738d14bdc988f476de2", "0xac133afc01168e99e3adbe163fc6e73f15c3323f", "0xeb389f0f63976838c8c9c2d955469f76d26cbdb3"]
  }];
  let notification_type = [{
      "type": "Water",
      "description": "Your plant needs water!",
      "textColor": 5814783
    },
    {
      "type": "Crow",
      "description": "Your plant has crow!",
      "textColor": 9051106
    },
    {
      "type": "Harvest",
      "description": "Congrats! Your account has a plant(s) to harvest!",
      "textColor": 2210060
    }
  ]

  if (!window.accountConfig) {
    window.accountConfig = accountConfig;
  }

  function checkForCrow() {
    let plantCrow = document.querySelectorAll('img.tw-absolute.crow-icon');
    let uwakCount = 0;
    let uwakLocation = '';
    plantCrow.forEach((kruk) => {
      if (kruk.style.display == "") {
        uwakCount += 1;
        uwakLocation = kruk;
      }
    });
    if (uwakCount > 0) {
      let account = getAccount();
      notifyDiscord(CROW_TYPE, account);
      return uwakLocation;
    }
    return uwakCount;
  }

  function notifyDiscord(notif_type, account) {
    var myHeaders = new Headers();
    myHeaders.append("Content-Type", "application/json");
    myHeaders.append("Cookie", "__cfruid=cc378d1218f130b10a3cc4b30c5dd6778a3c8898-1631196744; __dcfduid=f3c557da117711ec82c142010a0a0266; __sdcfduid=f3c557da117711ec82c142010a0a0266d95a4ab8497af0e5f0afaf78cf8f70c538bab997e56962317cf3d376e408c374");

    var raw = JSON.stringify({
      "content": `<@${account.discord_id}>`,
      "embeds": [{
        "title": `${notification_type[notif_type].type} Notification`,
        "color": notification_type[notif_type].textColor,
        "fields": [{
          "name": `${account.account_name} -> Account_${account.account_index}`,
          "value": `${notification_type[notif_type].description}`
        }]
      }]
    });

    var requestOptions = {
      method: 'POST',
      headers: myHeaders,
      body: raw,
      redirect: 'follow'
    };

    fetch("https://discord.com/api/webhooks/885527492889309274/Rtl_57V8l6DBCQEz556tfYIMPlhf_FdN52BuSAQRavBcMam04BRHdlTkdHnG1Q9GQV4t", requestOptions)
      .then(response => response.text())
      .then(result => console.log(result))
      .catch(error => console.log('error', error));
  }

  function getAccount() {
    let selectedAccount = {};
    accountConfig.some((account) => {
      let match = account.public_addresses.indexOf(getCurrentUser());
      if (match >= 0) {
        selectedAccount = account;
        selectedAccount["account_index"] = match + 1;
        return !!match;
      }
    });
    return selectedAccount;
  }

  function checkHarvest() {
    if (!document.querySelector("div.tw-flex.tw-flex-row.tw-flex-1.tw-gap-4.tw-justify-end > button").disabled) {
      let account = getAccount();
      notifyDiscord(HARVEST_TYPE, account);
    }
  }

  function notifyDryWater() {
    let plantWaterCounts = document.querySelectorAll("span.small");
    for (let plantWaterCount in plantWaterCounts) {
      if (plantWaterCounts[plantWaterCount].innerHTML == "0") {
        // call water audio
        let account = getAccount();
        notifyDiscord(WATER_TYPE, account);
      }
    }
  }

  function getCurrentUser() {
    if (!!window.ethereum) {
      return ethereum.selectedAddress;
    }
    else {
      console.log(`%c No Metamask Installed`);
      return;
    }
  }

  function checkMaintenance() {
    let maintenanceLabel = document.querySelector('.coming');
    if (!!maintenanceLabel && maintenanceLabel.innerText.toLowerCase() == 'farm maintenance') {
      return true;
    }
    return false;
  }

  setTimeout(() => {
    if (!checkMaintenance()) {
      let crowLoop = setInterval(() => {
        let useCrow = document.querySelector("#__layout > div > div:nth-child(2) > div > div > div.sidebar.tw-hidden.sm\\:tw-flex.sm\\:tw-flex-col.tw-flex-grow-0.tw-flex-shrink-0.tw-w-48.tw-pt-4 > div:nth-child(2) > div > div:nth-child(5) > div.tw-hidden.sm\\:tw-flex > button");
        let crowLocation = checkForCrow();
        notifyDryWater();
        checkHarvest();
        if (crowLocation !== 0) {
          console.log(`%c Naay uwak`, 'color:green');
          //bugawon
          crowLocation.click();
          setTimeout(() => {
            useCrow.click();
            clearInterval(crowLoop);
          }, 1000);
          setTimeout(() => {
            window.location.reload();
          }, 30000);
        }
        else {
          if (loopCount == 6) {
            window.location.reload();
          }
          console.log(`loop count: %c${loopCount}`, 'color:green');
        }
        loopCount++;
      }, 10000);
    }
    else {
      console.log(`%c Not in whitelist yet.`, 'color:red');
      setTimeout(() => {
        window.location.reload();
      }, 60000)
    }
  }, 5000);
})()