• Share this text:
Report Abuse
Untitled - posted by guest on 11th December 2020 06:10:15 PM

{

  const moduleName = 'LazyOkapiGardenHelper';


  const capitalize = (word) => word.charAt(0).toUpperCase() + word.slice(1);

  const uncapitalize = (word) => word.charAt(0).toLowerCase() + word.slice(1);

  const clone = (x) => JSON.parse(JSON.stringify(x));

  const doc = {

    elId: document.getElementById.bind(document),

    qSel: document.querySelector.bind(document),

    qSelAll: document.querySelectorAll.bind(document),

  }


  class Config {

    static get default() {

      return {

        autoFreeze,

      };

    }


    static get storageKey() { return moduleName; }


    static load() {

      let config = window.localStorage.getItem(this.storageKey);

      if (!config) {

        this.save(this.default);

        return this.default;

      }

      return Object.assign(this.default, JSON.parse(config));

    }


    static save(config) {

      window.localStorage.setItem(this.storageKey, JSON.stringify(config));

    }

  }


  class Garden {

    static get minigame() { return Game.Objects['Farm'].minigame; }

    static get isActive() { return this.minigame !== undefined; }


    static get CpSMult() {

      var res = 1

      for (let key in Game.buffs) {

        if (typeof Game.buffs[key].multCpS != 'undefined') {

          res *= Game.buffs[key].multCpS;

        }

      }

      return res;

    }


    static get secondsBeforeNextTick() {

      return (this.minigame.nextStep - Date.now()) / 1000;

    }


    static get selectedSeed() { return this.minigame.seedSelected; }

    static set selectedSeed(seedId) { this.minigame.seedSelected = seedId; }


    static getPlant(id) { return this.minigame.plantsById[id - 1]; }

    static getTile(x, y) {

      let tile = this.minigame.getTile(x, y);

      return { seedId: tile[0], age: tile[1] };

    }


    static getPlantStage(tile) {

      let plant = this.getPlant(tile.seedId);

      if (tile.age < plant.mature) {

        return 'young';

      } else {

        if ((tile.age + Math.ceil(plant.ageTick + plant.ageTickR)) < 100) {

          return 'mature';

        } else {

          return 'dying';

        }

      }

    }


    static tileIsEmpty(x, y) { return this.getTile(x, y).seedId == 0; }


    static plantSeed(seedId, x, y) {

      let plant = this.getPlant(seedId + 1);

      if (plant.plantable) {

        this.minigame.useTool(seedId, x, y);

      }

    }


    static forEachTile(callback) {

      for (let x = 0; x < 6; x++) {

        for (let y = 0; y < 6; y++) {

          if (this.minigame.isTileUnlocked(x, y)) {

            callback(x, y);

          }

        }

      }

    }


    static harvest(x, y) { this.minigame.harvest(x, y); }


    static fillGardenWithSelectedSeed() {

      if (this.selectedSeed > -1) {

        this.forEachTile((x, y) => {

          if (this.tileIsEmpty(x, y)) {

            this.plantSeed(this.selectedSeed, x, y);

          }

        });

      }

    }


    static freezeIfAllMature() {

      if (config.autoFreeze) {

        let allMature = true;

        for (let x = 0; x < 6; x++) {

          for (let y = 0; y < 6; y++) {

            if (this.minigame.isTileUnlocked(x, y)) {

              let tile = this.getTile(x, y);

              let stage = this.getPlantStage(tile);

              if (stage != 'mature') {

                allMature = false;

                break;

              }

            }

          }

        }


        if (allMature) {

          this.freezePlants();

        }

      }

    }


    //Straight up copied from CC Source, there seems to not have been a method

    static freezePlants() {

      //if (!M.freeze && M.nextFreeze>Date.now()) return false;

      PlaySound('snd/toneTick.mp3');

      this.minigame.freeze = (this.minigame.freeze ? 0 : 1);

      if (this.minigame.freeze) {

        this.minigame.computeEffs();

        PlaySound('snd/freezeGarden.mp3');

        this.classList.add('on');

        l('gardenContent').classList.add('gardenFrozen');


        for (var y = 0; y < 6; y++) {

          for (var x = 0; x < 6; x++) {

            var tile = this.minigame.plot[y][x];

            if (tile[0] > 0) {

              var me = this.minigame.plantsById[tile[0] - 1];

              var age = tile[1];

              if (me.key == 'cheapcap' && Math.random() < 0.15) {

                this.minigame.plot[y][x] = [0, 0];

                if (me.onKill) me.onKill(x, y, age);

                this.minigame.toRebuild = true;

              }

            }

          }

        }

      }

      else {

        //M.nextFreeze=Date.now()+(Game.Has('Turbo-charged soil')?1:(1000*60*10));

        this.minigame.computeEffs();

        this.classList.remove('on');

        l('gardenContent').classList.remove('gardenFrozen');

      }

    }


    static run(config) {

      if (config.autoFreeze) {

        this.freezeIfAllMature()

      }

    }

  }


  class UI {

    static build(config) {

      // Add options menu

      let menu = document.getElementById("gardenTools");


      // Gradient Button

      let buttonGradient = document.createElement("a");

      buttonGradient.className = "option";

      buttonGradient.innerText = "Switch Gradient";

      buttonGradient.id = "buttonDealGradient";


      menu.append(buttonGradient, menu.childNodes[0]);

    }

  }


  class Main {

    static init() {

      this.timerInterval = 1000;

      this.config = Config.load();

      UI.build(this.config);


      this.start();

    }


    static start() {

      this.timerId = window.setInterval(

        () => Garden.run(this.config),

        this.timerInterval

      );

    }


    static stop() { window.clearInterval(this.timerId); }


    static save() { Config.save(this.config); }


    static handleToggle(key) {

      this.config[key] = !this.config[key];

      this.save();

      UI.toggleButton(key);

    }


    static handleClick(key) {

      if (key == 'fillGardenWithSelectedSeed') {

        Garden.fillGardenWithSelectedSeed();

      }

      this.save();

    }

  }


  if (Garden.isActive) {

    Main.init();

  } else {

    let msg = `You don't have a garden yet. This mod won't work without it!`;

    console.log(msg);

    UI.createWarning(msg);

  }



}

Report Abuse

Login or Register to edit or copy and save this text. It's free.