Infaerina / Forge and Fortune Automator

// ==UserScript==
// @name         Forge and Fortune Automator
// @namespace    http://tampermonkey.net/
// @version      0.3
// @license      MIT
// @description  Automate adventures and more!
// @author       Infaerina
// @match        https://forgeandfortune.github.io/*
// @require      https://code.jquery.com/jquery-latest.js
// @require		 https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js
// @require		 https://gist.github.com/raw/2625891/waitForKeyElements.js
// @downloadURL	 https://openuserjs.org/install/Infaerina/Forge_and_Fortune_Automator.user.js
// @updateURL	 https://openuserjs.org/install/Infaerina/Forge_and_Fortune_Automator.user.js
// @homepage 	 https://openuserjs.org/scripts/Infaerina/Forge_and_Fortune_Automator
// @grant        GM_getValue
// @grant		 GM_setValue
// @run-atan	 document-idle
// ==/UserScript==

// ==OpenUserJS==
// @author Infaerina
// ==/OpenUserJS==

$(document).ready(function() {

setTimeout(function() {
	var EXPAND_HEIGHT = '200px';
	var EXPAND_WIDTH = '180px';
	var CONTRACT_HEIGHT = '50px';
	var CONTRACT_WIDTH = '120px';
	$('body').append(
	'<div id="automatorEntireBody" class="draggable" style="display: block; font-family: Typewriter; font-size: 10px; border: 1px solid black; cursor: move; padding:5px; background: grey; user-select: none;">' +
		'<br><br><br>' +
		'<button id="automationbtn" class="automatorButtons">Automator</button>' +
		'<div id="automatorSubBody" style="display: block;">' +
			'<br>' +
			'<center><button class="automatorButtons" id="AutoAdventure1">Dungeon Slot 1</button></center>' +
			'<center><button class="automatorButtons" id="AutoAdventure2">Dungeon Slot 2</button></center>' +
			'<center><button class="automatorButtons" id="AutoAdventure3">Dungeon Slot 3</button></center>' +
			'<center><button class="automatorButtons" id="AutoSubmitOrders">Submit Orders</button></center>' +
			'<center><button class="automatorButtons" id="AutoDesynth">Desynth Items</button></center>' +
			'<center><button class="automatorButtons" id="AutoFusion">Fuse Items</button></center>' +
		'</div>' +
	'</div>');
	$('#automatorEntireBody').css({
		position: 'absolute',
		right: '5%',
		bottom: '15%',
		'z-index': '99',
		display: 'flex',
		'flex-flow': 'row wrap',
		'align-items': 'center',
		'justify-content': 'center',
		height: EXPAND_HEIGHT,
		width: EXPAND_WIDTH
	});
	$('.automatorButtons').css({
		background: 'lightgrey',
		'font-family': 'Typewriter',
		'font-size': '14px'
	});
	$('#automationbtn').css({
		'font-size': '20px',
		margin: 'auto',
		width: '100%'
	});
	//Constants
	var AUTOADVENTURE = 'Dungeon slot';
	//Set up buttons
	if(GM_getValue("AutoAdventure1Enabled") == '1'){
        $('#AutoAdventure1').css('background', 'green');
    } else {
        $('#AutoAdventure1').css('background', 'lightgrey');
    }
	$('#AutoAdventure1').click(function(){
		if (GM_getValue('AutoAdventure1Enabled', '0') != '1'){
			GM_setValue('AutoAdventure1Enabled', '1');
			$('#AutoAdventure1').css('background', 'green');
		} else {
			GM_setValue('AutoAdventure1Enabled', '0');
			$('#AutoAdventure1').css('background', 'lightgrey');
		}
	});

	if(GM_getValue("AutoAdventure2Enabled") == '1'){
        $('#AutoAdventure2').css('background', 'green');
    } else {
        $('#AutoAdventure2').css('background', 'lightgrey');
    }
	$('#AutoAdventure2').click(function(){
		if (GM_getValue('AutoAdventure2Enabled', '0') != '1'){
			GM_setValue('AutoAdventure2Enabled', '1');
			$('#AutoAdventure2').css('background', 'green');
		} else {
			GM_setValue('AutoAdventure2Enabled', '0');
			$('#AutoAdventure2').css('background', 'lightgrey');
		}
	});

	if(GM_getValue("AutoAdventure3Enabled") == '1'){
        $('#AutoAdventure3').css('background', 'green');
    } else {
        $('#AutoAdventure3').css('background', 'lightgrey');
    }
	$('#AutoAdventure3').click(function(){
		if (GM_getValue('AutoAdventure3Enabled', '0') != '1'){
			GM_setValue('AutoAdventure3Enabled', '1');
			$('#AutoAdventure3').css('background', 'green');
		} else {
			GM_setValue('AutoAdventure3Enabled', '0');
			$('#AutoAdventure3').css('background', 'lightgrey');
		}
	});

	if(GM_getValue("AutomatorShowButtons") == '1'){
        document.getElementById("automatorSubBody").style.display = "block";
    } else {
        document.getElementById("automatorSubBody").style.display = "none";
		$("#automatorEntireBody").css('height',CONTRACT_HEIGHT).css('width', CONTRACT_WIDTH);
    }
	$('#automationbtn').click(function(){
		if (GM_getValue('AutomatorShowButtons', '0') != '1'){
			GM_setValue('AutomatorShowButtons', '1');
			document.getElementById('automatorSubBody').style.display = 'block';
			$("#automatorEntireBody").css('height',EXPAND_HEIGHT).css('width', EXPAND_WIDTH);
			
		} else {
			GM_setValue('AutomatorShowButtons', '0');
			document.getElementById('automatorSubBody').style.display = 'none';
			$("#automatorEntireBody").css('height',CONTRACT_HEIGHT).css('width', CONTRACT_WIDTH);
		}
	});

	if(GM_getValue("AutoFusionEnabled") == '1'){
        $('#AutoFusion').css('background', 'green');
    } else {
        $('#AutoFusion').css('background', 'lightgrey');
    }
	$('#AutoFusion').click(function(){
		if (GM_getValue('AutoFusionEnabled', '0') != '1'){
			GM_setValue('AutoFusionEnabled', '1');
			$('#AutoFusion').css('background', 'green');
		} else {
			GM_setValue('AutoFusionEnabled', '0');
			$('#AutoFusion').css('background', 'lightgrey');
		}
	});
	if(GM_getValue("AutoDesynthEnabled") == '1'){
        $('#AutoDesynth').css('background', 'green');
    } else {
        $('#AutoDesynth').css('background', 'lightgrey');
    }
	$('#AutoDesynth').click(function(){
		if (GM_getValue('AutoDesynthEnabled', '0') != '1'){
			GM_setValue('AutoDesynthEnabled', '1');
			$('#AutoDesynth').css('background', 'green');
		} else {
			GM_setValue('AutoDesynthEnabled', '0');
			$('#AutoDesynth').css('background', 'lightgrey');
		}
	});
	if(GM_getValue("AutoSubmitOrdersEnabled") == '1'){
        $('#AutoSubmitOrders').css('background', 'green');
    } else {
        $('#AutoSubmitOrders').css('background', 'lightgrey');
    }
	$('#AutoSubmitOrders').click(function(){
		if (GM_getValue('AutoSubmitOrdersEnabled', '0') != '1'){
			GM_setValue('AutoSubmitOrdersEnabled', '1');
			$('#AutoSubmitOrders').css('background', 'green');
		} else {
			GM_setValue('AutoSubmitOrdersEnabled', '0');
			$('#AutoSubmitOrders').css('background', 'lightgrey');
		}
	});

	//Functions.
	function repeatDungeonSlot(dungeon){
		var currTab = $('.tablinks');
		for (let i = 0; i < currTab.length; i++){
			if (currTab[i].getAttribute("class").includes("tab-selected")){
				var tab = currTab[i];
			}
		}
		if (document.getElementById('dsbD00' + dungeon).innerText.includes('Complete')){
			document.getElementById('dsbD00' + dungeon).click();
			document.querySelector('div#dreRepeat').click();
			if (tab !== null){
				tab.click();
				tab.setAttribute('class', 'tablinks tab-selected');
			}
		}
	}
	function reAdventureSlot1(){
		if (GM_getValue("AutoAdventure1Enabled", '1') == '1'){
			repeatDungeonSlot(1);
		}
	}
	function reAdventureSlot2(){
		if (GM_getValue("AutoAdventure2Enabled", '1') == '1'){
			repeatDungeonSlot(2);
		}
	}
	function reAdventureSlot3(){
		if (GM_getValue("AutoAdventure3Enabled", '1') == '1'){
			repeatDungeonSlot(3);
		}
	}

	function autoFusion(){
		if (GM_getValue("AutoFusionEnabled", '1') == '1'){
			//Get number of fuses.
			var fused = document.querySelectorAll('div#fuseSlots div.fuseSlotStart');
			var collects = document.querySelectorAll('div#fuseSlots div.fuseSlotCollect');
			var toFuse = document.querySelectorAll('#fuseList > div.possibleFuseHolder > div > div.fuseStart');
			var maxFuses = document.querySelector('#fuseSlots').children.length;
			//attempt to collect fuses.
			if (collects !== null){
				for (let i = 0; i < collects.length; i++){
					if(collects[i].innerText.includes("Collect")){
						collects[i].click();
					}
				}
			}
			//attempt to add a fuses.
			if (toFuse !== null && maxFuses > collects.length){
				for (let i = 0; i < toFuse.length; i++){
					if(toFuse[i].innerText.includes("Fuse")){
						toFuse[i].click();
					}
				}
			}
			//attempt to start fuses.
			if (fused !== null) {
				for (let i = 0; i < fused.length; i++){
					if (fused[i].style.display !== "none"){
						fused[i].click();
					}
				}
			}
			
		}
	}
	
	function autoDesynth(){
		if (GM_getValue("AutoDesynthEnabled", '1') == '1'){
			var desynthed = $('#desynthSlot #desynthSlotStart');
			var toDesynth = $('.desynthGroup .desynthButton');
			var desynthSlots = $('#desynthSlot').length;
			var collects = $('#desynthSlot #desynthSlotCollect');
			//Collect desynths
			if (collects !== null){
				for (let i = 0; i < collects.length; i++){
					if(collects[i].innerText.includes('Collect')){
						collects[i].click();
					}
				}
			}
			//Add desynths
			if (toDesynth !== null && desynthSlots > collects.length){
				for (let i = 0; i < toDesynth.length; i++){
					if(toDesynth[i].innerText.includes('Desynth')){
						toDesynth[i].click();
					}
				}
			}
			if (desynthed !== null) {
				for (let i = 0; i < desynthed.length; i++){
					if (desynthed[i].style.display !== "none"){
						desynthed[i].click();
					}
				}
			}
		}
	}
	
	function submitGuildOrders(){
		//All guildOrders
		//var guildOrders = $('.orderCard');
		if (GM_getValue("AutoSubmitOrdersEnabled", '1') == '1'){
			//guildOrders not complete
			var guildOrders = $('.orderInv');
			var guildOrderElement = $('.guildOrder');
			var claimRewards = $('');
			for (let i = 0; i < guildOrders.length; i++){
				if (parseInt(guildOrders[i].innerText) > 0){
					guildOrders[i].click();
				}
			}
			for (let i = 0; i < guildOrderElement.length; i++){
				var orders = guildOrderElement[i].querySelectorAll('.orderCard').length;
				var orderCards = guildOrderElement[i].querySelectorAll('.orderComplete').length;
				if(orders !== 0 && orders == orderCards){
					guildOrderElement[i].querySelector('.guildOrderSubmit').click();
				}
			}
		}
	}
	
	//Frame functions.
	$('#automatorEntireBody').draggable({
	  containment: '.game-content',
	  stop: function( event, ui ) {
		$(this).css({
			right: parseInt($(this).css('right')) / ($('.game-content').width() / 100)+'%',
			bottom: parseInt($(this).css('bottom')) / ($('.game-content').height() / 100)+'%',
			top: 'auto',
			left: 'auto'
		});
	  }
	});


	//Main function
	var automatorLoop = setInterval(function(){
		reAdventureSlot1();
		reAdventureSlot2();
		reAdventureSlot3();
		autoFusion();
		autoDesynth();
		submitGuildOrders();
	}, 500);

}, 6000); //Two seconds will elapse and Code will execute.

});