let6 / DP blaze

// ==UserScript==
// @name         DP blaze
// @include      http://www.drapro-nutaku.dmmgames.com/blaze*
// @include      https://www.drapro-nutaku.dmmgames.com/blaze*
// @run-at       document-idle
// @license      MIT
// ==/UserScript==

var minFullAPPotions = 10000;
var min50APPotions = 100;
var min30APPotions = 3;
var timeOutRefresh=30*60*1000;// wait between refresh when cannot use potion and have low AP

function beforeStart() {
    if ('undefined' == typeof jQuery){
        setTimeout(beforeStart,500);
        return;
    }
    if (location.pathname.startsWith("/blaze_battle/battle/")){
        setTimeout(doBattle,3000);
        return;
    }
    if (location.pathname==="/blaze" || location.pathname==="/blaze/index") {
		setTimeout(blazeEvent,500);
        return;
	}
	if (location.pathname.startsWith("/blaze_battle/result/")){
		gotoHome();
	}
	if (location.pathname==="/blaze/result_full" || location.pathname.startsWith("/blaze/quest_result")){
		if ($("#itemNum1:visible").length){
			usePotion();
		} else {
			gotoHome();
		}
	}
	setTimeout(gotoHome,timeOutRefresh); //return home after x seconds
}

function blazeEvent() {
    var maxAP=0,nowAP=0;
    if ($("#nowApText").length){
        maxAP = parseInt($("#maxApText").get(0).innerText);
        nowAP = parseInt($("#nowApText").get(0).innerText);
    }
    // Check scrolls and start easy battle if sufficient
    if ($('#iconBook6Num').length && document.getElementById('iconBook6Num').innerHTML>=2 && $('#blazeSummonBtn69').length) {
        if (nowAP>=10) {
            $('#blazeSummonBtn69').click();
        } else {
            $('#blazeSummonBtn69').click();
            setTimeout(usePotion,500);
        }
    } else if ($('#iconBook6Num').length && $(".btnMR.emphasis").length){
        $(".btnMR.emphasis").get(0).click();
    }

    // Select rear battle start
    if ($(".selectPositionBtn.front").length && !$('#blazeSummonBtn69').get(0).hidden) {
        $(".selectPositionBtn.front").get(0).click();
    }
}

function doBattle(){
	if ($(".tapScreen.bossDead").length) {
		$(".tapScreen.bossDead").trigger('tap');
		setTimeout(doBattle,500);
	} else if ($("#skillSkipBtn:visible").length){
		if (!$("#skillSkipBtn:visible.off").length){
			$("#skillSkipBtn:visible").get(0).click();
		}
		setTimeout(doBattle,500);
	} else if ($("#skillBtn").length && !$("#skillBtn.off").length) {
		$("#skillBtn").get(0).click();
		setTimeout(selectAutoSkills,500);
	} else {
		setTimeout(doBattle,500);
	}
}

function selectAutoSkills(){
	if ($("#skillAutoSetBtn:visible").length){
		$("#skillAutoSetBtn").trigger('tap');
		setTimeout(activateSkill,500);
	} else if ($("#skillExecuteBtn").length && !$("#skillExecuteBtn.off").length){
		setTimeout(activateSkill,500);
	} else {
		selectAutoSkills();
	}
}

function activateSkill(){
	if ($("#skillExecuteBtn").length && !$("#skillExecuteBtn.off").length){
		$("#skillExecuteBtn").trigger('click');
		setTimeout(doBattle,500);
		setTimeout(gotoHome,500);
	}
}

function usePotion(){
	if ($("#itemNum3").length && $("#itemNum3").get(0).innerText.replace("x ","")>min50APPotions){
		if ($("#itemUseBtn3_plural") && ($("#itemUseBtn3_plural").get(0) !== undefined)){
			$("#itemUseBtn3_plural").get(0).click();
			setTimeout(gotoHome,500);
		} else if ($("#itemUseBtn3")){
			$("#itemUseBtn3").get(0).click();
			$("#itemUseBtn3").get(0).click();
			setTimeout(gotoHome,500);
        }
	} else if ($("#itemNum2").length && $("#itemNum2").get(0).innerText.replace("x ","")>min30APPotions){
		if ($("#itemUseBtn2_plural")){
			$("#itemUseBtn2_plural").get(0).click();
			setTimeout(gotoHome,500);
		}
	} else if ($("#itemNum1").length && $("#itemNum1").get(0).innerText.replace("x ","")>minFullAPPotions){
		if ($("#itemUseBtn1")){
			$("#itemUseBtn1").get(0).click();
			setTimeout(gotoHome,500);
		}
	}
}

function gotoHome(){
	location.href='https://www.drapro-nutaku.dmmgames.com/blaze';
}

setTimeout(beforeStart,500);