Melvor TimeRemaining

Shows time remaining for completing a task with your current resources. Takes into account Mastery Levels and other bonuses.

目前為 2020-04-16 提交的版本,檢視 最新版本

// ==UserScript==
// @name         Melvor TimeRemaining
// @namespace    http://tampermonkey.net/
// @version      0.1.3
// @description  Shows time remaining for completing a task with your current resources. Takes into account Mastery Levels and other bonuses.
// @author       Breindahl#2660
// @match        https://*.melvoridle.com/*
// @grant        none
// ==/UserScript==

// Loading
console.log('Loading Breindahl TimeRemaining');

// Set variables
var currentSkill = null;

// Initialize audio for notification
function ding() {
	new Audio("https://www.myinstants.com/media/sounds/ding-sound-effect.mp3").play();
};

// Initialize message for notification
function notify(msg) {
	One.helpers('notify', {
		type: 'dark',
		from: 'bottom',
		align: 'center',
		message: msg
	});
};


// Create containers
document.getElementById("smith-item-have").outerHTML += "<br><small id =\"timeLeft\">"+""+"<small>";

//Funtion to get unformatted number for Qty
function getQtyUnformat(itemID) {
	let qty = 0;
	for (let i = 0; i < bank.length; i++) {
		if (bank[i].id === itemID) {
			qty += bank[i].qty;
		};
	};
	return qty;
};

// Convert seconds to hours/minutes/seconds
function secondsToHms(d) {
	d = Number(d);
	var h = Math.floor(d / 3600);
	var m = Math.floor(d % 3600 / 60);
	var s = Math.floor(d % 3600 % 60);
	var sDisplay = s > 0 ? s + (s == 1 ? " second" : " seconds") : "";
	var mDisplay = m > 0 ? m + (m == 1 ? " minute" : " minutes") + (s == "" ? "" : ", ") : "";
	var hDisplay = h > 0 ? h + (h == 1 ? " hour" : " hours") + ((s == "" && m == "") ? "" : ", ") : "";
	return hDisplay + mDisplay + sDisplay;
};

// Main function
function timeRemaining(item){

    // Reset variable
    var itemCraft = []
    var recordCraft = Infinity;

    // Get Item Requirements and Current Requirements
    for (let i = 0; i < items[item].smithReq.length; i++) {
        var itemReq;
        if (equippedItems[CONSTANTS.equipmentSlot.Cape] === CONSTANTS.item.Smithing_Skillcape && items[item].smithReq[i].id == 48) {
            itemReq = Math.floor(items[item].smithReq[i].qty / 2)
        } else {
            itemReq = items[item].smithReq[i].qty;
        }
        var itemQty = getQtyUnformat(items[item].smithReq[i].id);
        // Calculate max items you can craft for each itemReq
        itemCraft[i] = Math.floor(itemQty/itemReq);
        // Calculate limiting factor and set new record
        if(itemCraft[i] < recordCraft) {
            recordCraft = itemCraft[i];
        };

    };

    function masteryChance(masteryEXP){ //this function needs to return the chanceToKeep for any mastery EXP
        if (masteryEXP < 372) {return 0;}
        else if (372 <= masteryEXP && masteryEXP < 3102) {return 0.1}
        else if (3102 <= masteryEXP && masteryEXP < 22811) {return 0.2}
        else if (22811 <= masteryEXP && masteryEXP < 165505) {return 0.3}
        else if (165505 <= masteryEXP) {return 0.4}
    }

    var masteryLim = [372, 3102, 22811, 165505, Infinity] //this array should contain the thresholds at which a new chanceToKeep comes into effect

    function expectedTime(resources, resourcesPerAction, timePerAction, currentMastery, masteryLim){ //this function finds the required items to get expToLvl more exp, considering expPerItem and resource preservation due to mastery
        var expectedActions = Math.floor(resources/resourcesPerAction/(1-masteryChance(currentMastery))); //nr of actions if we do not reach new masteryLim
        var currentMastery_lim = masteryLim.find(element => element > currentMastery);  //this many actions can be taken until we reach a new mastery threshold
        var finalResult;
        var resourcesLeft
        if (expectedActions <= currentMastery_lim-currentMastery){
            finalResult = expectedActions
            resourcesLeft = 0
        } else {
            finalResult = (currentMastery_lim - currentMastery); //finalResult will be updated to keep track of actions
            resourcesLeft = (resources - finalResult*resourcesPerAction*(1-masteryChance(currentMastery))); //remaining resources after we reach new mastery level
        }
        currentMastery = currentMastery_lim; //prepare for loop
        while (Math.floor(resourcesLeft/resourcesPerAction) > 0) { //continue iterating through new chanceToKeep levels until we're out of resources
            currentMastery_lim = masteryLim.find(element => element > currentMastery); //nr of actions from next chanceToKeep
            expectedActions = resourcesLeft/resourcesPerAction/(1-masteryChance(currentMastery)); //how many actions we can do with the remaining resources at the new mastery level
            if (expectedActions <= currentMastery_lim - currentMastery) { //if this is the final mastery level, add the nr of actions to finalResult and stop iterating
                finalResult += expectedActions;
                break
            } else { //if we will reach a new mastery level, set the new remainder and add the items required to reach the new mastery level to final_result
                resourcesLeft -= (currentMastery_lim-currentMastery)*(1-masteryChance(currentMastery));
                finalResult += currentMastery_lim-currentMastery;
            }
            currentMastery = currentMastery_lim;
        }
        return finalResult*timePerAction
    }

    window.timeLeftLast = window.timeLeftGlobal;

    //Calculate time
    var timeLeft = Math.floor(expectedTime(recordCraft,1,smithInterval/1000, smithingMastery[smithingItems[selectedSmith].smithingID].masteryXP,masteryLim));
    //console.log("timeLeft "+timeLeft);
    window.timeLeftGlobal = timeLeft;

    //Inject HTML
    elementToChange = document.getElementById("timeLeft");
	if (timeLeft !== 0) {
		if(elementToChange !== null) {
			elementToChange.innerHTML = secondsToHms(timeLeft) + " remaining";
		};
	} else {
		if(elementToChange !== null) {
			elementToChange.innerHTML = secondsToHms(timeLeft) + "";
		};
	};
};

var startSmithingRef = startSmithing;
window.startSmithing = function() {
    startSmithingRef(true);
    timeRemaining(smithingItems[selectedSmith].itemID);
    if (window.timeLeftLast > 1 && window.timeLeftGlobal == 0) {
        notify("Task Done");
        console.log('task done');
        ding();
    };
    //console.log('timeLeftLast'+window.timeLeftLast);
    //console.log('timeLeftGlobal'+window.timeLeftGlobal);
};

var selectSmithRef = selectSmith;
window.selectSmith = function(smithingID) {
    selectSmithRef(smithingID);
    timeRemaining(smithingItems[selectedSmith].itemID);
};

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址