Module:Calculator/Summoning Interval Efficiency

From Melvor Idle
Revision as of 14:21, 13 March 2024 by Ricewind (talk | contribs) (Create module to calculate time efficiency when using summoning synergies that decrease action interval)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Documentation for this module may be created at Module:Calculator/Summoning Interval Efficiency/doc

local number = require('Module:Number')
local economy = require('Module:ItemEconomy')

local p = {}

local function calculateTimeEfficiency(playerStats)
	local result = {
		isInfinite = false,
		twoTabletTimeSave = -1,
		threeTabletTimeSave = -1,
		effectiveTablets = -1
	}
	
	-- It is possible to get 100% tablet save chance. The rest of the calculation is irrelevant
	-- since the amount of time saved is infinite.
	if playerStats.tabletSaveChance >= 100 then
		result.isInfinite = true
		return result
	end
	
	-- Calculate total amount of tablets made, including double chance
	local averageTabletsMade = economy.estimatedOutput(
		playerStats.tabletsMade, 
		0, 
		playerStats.tabletDoubleChance)

	-- Calculate the effective amount of tablets the player has from one craft, including tablet save chance
	local tabletSaveChance = number.clamp(playerStats.tabletSaveChance, 0, 99)
	local effectiveTablets = averageTabletsMade / (1 - tabletSaveChance / 100)
	
	-- The effective time per tablet.
	-- two if the player was using tablets anyway
	-- three if the player is using tablets specifically for the time save synergy
	local twoTabletTime = (playerStats.actionTimeSaved / 2) * effectiveTablets
	local threeTabletTime = (playerStats.actionTimeSaved / 3) * effectiveTablets
	
	-- Subtract the total time saved by the time it takes to create the tablets.
	local twoTabletTimeSave = twoTabletTime - playerStats.summoningInterval
	local threeTabletTimeSave = threeTabletTime - playerStats.summoningInterval
	
	result.effectiveTablets = effectiveTablets
	result.twoTabletTimeSave = twoTabletTimeSave
	result.threeTabletTimeSave = threeTabletTimeSave
	return result
end

-- Add a row to the table
-- If the value parameter is a negative number, colour the cell red.
local function addTableRow(tbl, header, value)
    local row = tbl:tag("tr")
    local cell = row:tag("td")
    
    row:tag("th"):wikitext(header)

    if type(value) == "number" and value < 0 then
        cell:css('color', 'red'):wikitext(value)
    else
        cell:wikitext(value)
    end
    return tbl
end

local function createTable(calcResult)
	local tableData = {
    	effectiveTablets,
    	twoTabletTimeSave,
    	threeTabletTimeSave
    }

	local tbl = mw.html.create("table")
        :addClass("wikitable sticky-header text-align-right align-left-1")

	if res.isInfinite then
		tableData.effectiveTablets = 'Infinite'
		tableData.twoTabletTimeSave = 'Infinite'
		tableData.threeTabletTimeSave = 'Infinite'
	else
		-- Calculate time saved in seconds for two and three tablet synergy
		local twoTab = number.actionTimeToTimeSpan(res.twoTabletTimeSave)
    	local threeTab = number.actionTimeToTimeSpan(res.threeTabletTimeSave)
    	tableData.effectiveTablets = calcResult.effectiveTablets
		tableData.twoTabletTimeSave = twoTab.totalSeconds
		tableData.threeTabletTimeSave = threeTab.totalSeconds
	end

	addTableRow(tbl, "Effective tablets", tableData.effectiveTablets)
	addTableRow(tbl, "Two tablet time save", tableData.twoTabletTimeSave)
	addTableRow(tbl, "Three tablet time save", tableData.threeTabletTimeSave)
	return tbl
end

function p.main(frame)
	local args = frame:getParent().args
	return p._main(args)
end

function p._main(args)
	-- Parse inputs and turn intervals into integers (hundreds of a second)
	local summoningInterval = number.toNumberOrError(args.summoningInterval) * 100
	local actionTimeSaved = number.toNumberOrError(args.actionTimeSaved) * 100
	local tabletsMade = number.toNumberOrError(args.tabletsMade)
	local tabletDoubleChance = number.toNumberOrDefault(args.tabletDoubleChance, 0)
	local tabletSaveChance = number.toNumberOrDefault(args.tabletSaveChance, 0)

	local playerStats = {
		summoningInterval = summoningInterval,
		tabletsMade = tabletsMade,
		tabletDoubleChance = tabletDoubleChance,
		tabletSaveChance = tabletSaveChance,
		actionTimeSaved = actionTimeSaved
	}
	
	local calcResult = calculateTimeEfficiency(playerStats)
	local tbl = createTable(calcResult)
	
	return tostring(tbl)
end

return p