channox32 / FarmersWorld Bot

// ==UserScript==
// @name FarmersWorld Bot
// @namespace Script Runner Pro
// @match https://play.farmersworld.io/
// @grant none
// @version 1.2
// @license MIT
// ==/UserScript==

(function () {
  function Mining() {
    this.isInitialized = false;
    this.toolCount = 0;
    this.tools = [];

    this.currentToolIndex = 0;


    this.init = () => {
        if (!this.isInitialized) {
            this.isInitialized = true;
            this.getNumberOfTools();
        }
        
    }

    this.getNumberOfTools = () => {
        let tools = document.querySelectorAll('img.carousel__img--item');

        if (!!tools && tools.length > 0) {
            this.toolCount = tools.length;
            this.tools = tools;
        }
        return;
    }

    this.farm = () => {
        //let timeoutCount = this.currentToolIndex == 0 ? this.currentToolIndex + 1 : this.currentToolIndex;
        let energyNeedRefill = false;
        let energyStats = document.querySelectorAll('div.resource-number')[3].innerText;
        energyStats = energyStats.match(/\d+/g);
      
        if (this.currentToolIndex != this.tools.length) {
            this.checkMine(this.tools[this.currentToolIndex]);
            this.currentToolIndex++;
        } else {
            this.currentToolIndex = 0;
        }
        
        if (!!energyStats && energyStats.length > 0) {
          let remaining = parseInt(energyStats[0], 10);
          let total = parseInt(energyStats[1], 10);
          if ((remaining/total) <= 0.5) {
            let refillable = total - remaining;
            this.refillEnergy(refillable);
            return;
          } else {
           console.log(`%c You still have energy left!`, 'color:yellow');
          }
        }
        setTimeout(this.farm, 10000);
        return;
    }
    
    this.refillEnergy = (energy) => {
      let plusIcon = document.querySelector('img.resource-energy--plus').click();
      setTimeout(() => {
        let modal = document.querySelector('div.modal.exchange-modal');
        if (!!modal) {
          let plusBtn = document.querySelectorAll('img.image-button')[2];
          for (let i = 5; i <= energy;) {
            setTimeout(() => {
              plusBtn.click();
            }, 500);
            i += 5;
          }

          let exchangeBtn = document.querySelector('div.plain-button.long');
          setTimeout(() => {
            exchangeBtn.click();
            this.farm();
          }, 1000);
        }
      }, 1000);
      return;
    }

    this.checkMine = (tool) => {
        tool.click();
        console.log(` %c Executing Tool Index: ${tool.getAttribute('alt')}`, 'color:gray');
    
      setTimeout(() => {
        let mineBtn = document.querySelectorAll('div.plain-button.semi-short')[0];
        let repairBtn = document.querySelectorAll('div.plain-button.semi-short')[1];
        let infoTime = document.querySelector('div.info-time');
          if (!!infoTime && infoTime.innerText == '00:00:00') {
            mineBtn.click();
            console.log(`%c Mined Successfully.`, 'color:green');
          } else {
            try {
              if (eval(document.querySelector("div.content").innerText) <= 0.25) {
                repairBtn.click();
                console.log(`%c Tool Repaired!!!`, 'color:blue');
              }
            } catch (err) {
              console.log(`%c Not valid for Repair`, 'color:orange');
            }
            console.log("not yet ready");
          }
      }, 500);
    }
}

function Farming() {
    this.isInitialized = false;
    this.seedCount = 0;
    this.seeds = [];

    this.currentSeedIndex = 0;


    this.init = () => {
        if (!this.isInitialized) {
            this.isInitialized = true;
            this.getNumberOfPlants();
        }
        
    }

    this.getNumberOfPlants = () => {
        let plants = document.querySelectorAll('img.carousel__img--item');

        if (!!plants && plants.length > 0) {
            this.seedCount = plants.length;
            this.seeds = plants;
        }
        return;
    }

    this.water = () => {
        //let timeoutCount = this.currentToolIndex == 0 ? this.currentToolIndex + 1 : this.currentToolIndex;
        if (this.currentSeedIndex != this.seeds.length) {
            this.checkMine(this.seeds[this.currentSeedIndex]);
            this.currentSeedIndex++;
        } else {
            this.currentSeedIndex = 0;
        }
        setTimeout(this.water, 10000);
        return;
    }

    this.checkMine = (seed) => {
        seed.click();
        console.log(` %c Executing Seed Index: ${seed.getAttribute('alt')}`, 'color:gray');
    
      setTimeout(() => {
        let waterBtn = document.querySelectorAll('div.plain-button.semi-short')[0];
        
        if (!!waterBtn && waterBtn.innerText.toLowerCase() == 'water') {
            waterBtn.click();
            console.log(`%c Plant Watered Successfully.`, 'color:green');
        } else {
         console.log("not yet ready");   
        }
      }, 500);
    }
}
  
function decisionMode(mode) {
    let farmModeComponent = document.querySelectorAll('img.rice');
    let miningComponent = document.querySelector('div.badge-container');
    
    let farmMode = new Farming();
    let miningMode = new Mining();

 
    if (mode == "1") {
      console.log(`%cMining activated`, 'color:green');
      if (!!miningComponent && miningComponent.classList.length > 0) {
        if (!miningMode.isInitialized) {
            miningMode.init();
        }
        miningMode.farm();
      }
    } else if (mode == "2") {
      console.log(`%cFarming activated`, 'color:green');
      if (!!farmModeComponent && farmModeComponent.length > 0) {
          if (!farmMode.isInitialized) {
              farmMode.init();
          }
          farmMode.water();
      }
    } else {
      alert("Invalid mode selected; exit code 1;");
      return;
    }
}

let checkerInt = setInterval(() => {
  let homePage = document.querySelector('section.home-container');
  if (!!homePage) {
    let decisionMaker = window.prompt("Choose Mode (1) mine | (2) farm", "1");
    decisionMode(decisionMaker);
    clearInterval(checkerInt);
  }
}, 5000);
}());