Module:Calculator/ETA

From Melvor Idle
Revision as of 22:56, 29 February 2024 by Ricewind (talk | contribs) (First version)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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 / 3600000)
    local days = math.floor(timeInHundredths / 3600000 * 24)

    return string.format("%d-%02d:%02d:%02d", days, hrs % 24, min % 60, sec % 60)
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")

    tbl:tag("tr")
        :tag("th"):wikitext("Current Experience")
        :tag("th"):wikitext("Target Level")
        :tag("th"):wikitext("Target Experience")
        :tag("th"):wikitext("Experience Remaining")
        :tag("th"):wikitext("Actions Left")
        :tag("th"):wikitext("Time Left")

    tbl:tag("tr")
        :tag("td"):wikitext(shared.formatnum(currentExp))
        :tag("td"):wikitext(targetLvl)
        :tag("td"):wikitext(shared.formatnum(targetExp))
        :tag("td"):wikitext(shared.formatnum(expRemaining))
        :tag("td"):wikitext(shared.formatnum(actionsToTarget))
        :tag("td"):wikitext(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 = p.toNumberOrError(args.targetLvl)
	local actionExp = p.toNumberOrError(args.actionExp)
	local actionTime = p.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