Module:Calculator/ETA

From Melvor Idle
Revision as of 23:24, 29 February 2024 by Ricewind (talk | contribs) (Fix erroneous calculation of time remainder)

Documentation for this module may be created at Module:Calculator/ETA/doc

local p = {}

local MEXP = require('Module:Experience')
local shared = require('Module:Shared')

function p._formatTime(timeInHundredths)
    local sec = math.floor(timeInHundredths / 100)
    local min = math.floor(timeInHundredths / 60000)
    local hrs = math.floor(timeInHundredths / 360000)
    local days = math.floor(timeInHundredths / (360000 * 24))

	if days == 0 then
		return string.format("%02dh %02dm %02ds", hrs % 24, min % 60, sec % 60)
	else
		return string.format("%d days %02dh %02dm %02ds", days, hrs % 24, min % 60, sec % 60)
	end
end

function p._addTableRow(tbl, c1, c2)
	tbl:tag("tr")
		:tag("th"):wikitext(c1)
		:tag("td"):wikitext(c2)
	
	return tbl
end

function p._calc(currentExp, targetLvl, expPerAction, actionTime)
	-- ActionTime is represented in hundreds of a second.
	-- 1.6 seconds = 160
	local actionsPerHour = math.floor(360000 / actionTime)
	local targetExp = MEXP.expForLevel(targetLvl)
	local expRemaining = math.max(targetExp - currentExp, 0)
	
	local actionsToTarget = math.ceil(expRemaining / expPerAction)
	local timeToTarget = actionsToTarget * actionTime
	
	local tbl = mw.html.create("table")
        :addClass("wikitable sticky-header text-align-right align-left-1")

	p._addTableRow(tbl, "Current Experience", shared.formatnum(currentExp))
	p._addTableRow(tbl, "Target Level", targetLvl)
	p._addTableRow(tbl, "Target Experience", shared.formatnum(targetExp))
	p._addTableRow(tbl, "Experience Remaining", shared.formatnum(expRemaining))
	p._addTableRow(tbl, "Actions Left", shared.formatnum(actionsToTarget))
	p._addTableRow(tbl, "Time Left", p._formatTime(timeToTarget))

    return tostring(tbl)
end

function p.main(frame)
	return p._main(frame)
end

function p._main(frame)
	local args = frame:getParent().args
	
	local currentExp = shared.toNumberOrDefault(args.currentExp, 0)
	local currentLvl = shared.toNumberOrDefault(args.currentLvl, 0)

	local targetLvl = shared.toNumberOrError(args.targetLvl)
	local actionExp = shared.toNumberOrError(args.actionExp)
	local actionTime = shared.toNumberOrError(args.actionTime)

	-- Check Exp param for validity first, then Lvl
	if(currentExp == 0) then
		currentExp = MEXP.expForLevel(currentLvl)
	end
	
	return p._calc(currentExp, targetLvl, actionExp, actionTime)
end

return p