Module:Calculator/ItemEconomy: Difference between revisions

From Melvor Idle
m (Fix nil handling of input parameter)
m (Chance extra item amounts to 1 by default instead of 0)
 
(17 intermediate revisions by the same user not shown)
Line 1: Line 1:
local p = {}
local p = {}


local shared = require('Module:Shared')
local number = require('Module:Number')
local eco = require("Module:ItemEconomy")
local eco = require("Module:ItemEconomy")


Line 24: Line 24:
-- Returns the estimated output items and TRUE/FALSE if the Ring of Wealth has a benefit.
-- Returns the estimated output items and TRUE/FALSE if the Ring of Wealth has a benefit.
--
--
local function calculateOutput(itemAmount, pChance, dChance, eChance)
local function calculateOutput(target, itemEconomy)
local estimatedOutput = eco.estimatedOutput(itemAmount, pChance, dChance, eChance)
local estimatedOutput = eco.estimatedOutput(target, itemEconomy)
local canUseRoW = eco.ringOfWealthHasEffect(pChance, dChance, eChance)
local useRoW = eco.ringOfWealthHasEffect(itemEconomy)
return estimatedOutput, canUseRoW
return math.floor(estimatedOutput), useRoW
end
end


Line 34: Line 34:
-- Returns the estimated input items and TRUE/FALSE if the Ring of Wealth has a benefit.
-- Returns the estimated input items and TRUE/FALSE if the Ring of Wealth has a benefit.
--
--
local function calculateInput(itemAmount, pChance, dChance, eChance)
local function calculateInput(target, itemEconomy)
local estimatedInput = eco.estimatedInput(itemAmount, pChance, dChance, eChance)
local estimatedInput = eco.estimatedInput(target, itemEconomy)
local useRoW = eco.ringOfWealthHasEffect(itemEconomy)
 
return math.ceil(estimatedInput), useRoW
end
 
local function addTableRow(tbl, c1, c2)
tbl:tag("tr")
:tag("th"):wikitext(c1)
:tag("td"):wikitext(c2)
local baseModifier = eco.estimatedInput(1, pChance, dChance, eChance)
return tbl
local rowModifier = eco.estimatedInput(1, pChance - 3, dChance + 7, eChance)
end


return estimatedOutput, rowModifier > baseModifier
local function createTable(target, calcAmount, useRoW, ecoType, multiplier)
local RoWYN = useRoW and "Yes" or "No"
local tbl = mw.html.create("table")
:addClass("wikitable sticky-header text-align-right align-left-1")
if ecoType == ECOIN then
addTableRow(tbl, "Desired Output", number.formatnum(target))
addTableRow(tbl, "Estimated Input", number.formatnum(calcAmount))
else
addTableRow(tbl, "Starting Items", number.formatnum(target))
addTableRow(tbl, "Estimated Output", number.formatnum(calcAmount))
end
addTableRow(tbl, "Economy Factor", string.format("x%.2f", multiplier))
addTableRow(tbl, "[[Ring of Wealth]] benefits", RoWYN)
return tbl
end
end


Line 49: Line 75:


function p._main(args)
function p._main(args)
local itemAmount = targetAmount
local target = number.toNumberOrDefault(args.targetAmount, 0)
local economyType = parseEconomy(args.economyType)
local economyType = parseEconomy(args.economyType)
 
local pChance = shared.toNumberOrDefault(args.preservationChance, 0)
-- Create and fill ItemEconomy object.
local dChance = shared.toNumberOrDefault(args.duplicationChance, 0)
local itemEconomy = eco.ItemEconomy:new()
local eChance = shared.toNumberOrDefault(args.extraItemChance, 0)
itemEconomy.inputsPerAction = number.toNumberOrDefault(args.inputsPerAction, 1)
itemEconomy.outputsPerAction = number.toNumberOrDefault(args.outputsPerAction, 1)
itemEconomy.preservationChance = number.toNumberOrDefault(args.preservationChance, 0)
itemEconomy.duplicationChance = number.toNumberOrDefault(args.duplicationChance, 0)
itemEconomy.extraItemChance = number.toNumberOrDefault(args.extraItemChance, 0)
itemEconomy.extraItemAmount = number.toNumberOrDefault(args.extraItemAmount, 1)
itemEconomy.flatExtraItems = number.toNumberOrDefault(args.flatExtraItems, 0)
itemEconomy.extraBaseItemChance = number.toNumberOrDefault(args.extraBaseItemChance, 0)
itemEconomy.extraBaseItems = number.toNumberOrDefault(args.extraBaseItems, 1)
 
local result = 0
local result = 0
local row = false
local row = false
if economyType == ECOOUT then
if economyType == ECOOUT then
result, row = calculateOutput(itemAmount, pChance, dChance, eChance)
result, row = calculateOutput(target, itemEconomy)
else
else
result, row = calculateInput(itemAmount, pChance, dChance, eChance)
result, row = calculateInput(target, itemEconomy)
end
end
 
return result, row
local multiplier = eco.estimatedOutputMultiplier(itemEconomy)
local tbl = createTable(target, result, row, economyType, multiplier)
       
    return tostring(tbl)
end
end


return p
return p

Latest revision as of 00:07, 19 March 2024

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

local p = {}

local number = require('Module:Number')
local eco = require("Module:ItemEconomy")

ECOIN = "INPUT"
ECOOUT = "OUTPUT"

local function parseEconomy(eco)
	if eco == nil then
		return ECOOUT
	end
	
	if eco:upper() == ECOIN then
        return ECOIN
    elseif eco:upper() == ECOOUT then
        return ECOOUT
    end
    
    return ECOOUT
end

--
-- Returns the estimated output items and TRUE/FALSE if the Ring of Wealth has a benefit.
--
local function calculateOutput(target, itemEconomy)
	local estimatedOutput = eco.estimatedOutput(target, itemEconomy)
	local useRoW = eco.ringOfWealthHasEffect(itemEconomy)
	
	return math.floor(estimatedOutput), useRoW
end

--
-- Returns the estimated input items and TRUE/FALSE if the Ring of Wealth has a benefit.
--
local function calculateInput(target, itemEconomy)
	local estimatedInput = eco.estimatedInput(target, itemEconomy)
	local useRoW = eco.ringOfWealthHasEffect(itemEconomy)

	return math.ceil(estimatedInput), useRoW
end

local function addTableRow(tbl, c1, c2)
	tbl:tag("tr")
		:tag("th"):wikitext(c1)
		:tag("td"):wikitext(c2)
	
	return tbl
end

local function createTable(target, calcAmount, useRoW, ecoType, multiplier)
	local RoWYN = useRoW and "Yes" or "No"
	
	local tbl = mw.html.create("table")
		:addClass("wikitable sticky-header text-align-right align-left-1")
		
	if ecoType == ECOIN then
		addTableRow(tbl, "Desired Output", number.formatnum(target))
		addTableRow(tbl, "Estimated Input", number.formatnum(calcAmount))
	else
		addTableRow(tbl, "Starting Items", number.formatnum(target))
		addTableRow(tbl, "Estimated Output", number.formatnum(calcAmount))
	end
	
	addTableRow(tbl, "Economy Factor", string.format("x%.2f", multiplier))
	addTableRow(tbl, "[[Ring of Wealth]] benefits", RoWYN)
	
	return tbl
end

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

function p._main(args)
	local target = number.toNumberOrDefault(args.targetAmount, 0)
	local economyType = parseEconomy(args.economyType)

	-- Create and fill ItemEconomy object.
	local itemEconomy = eco.ItemEconomy:new()
	itemEconomy.inputsPerAction =		number.toNumberOrDefault(args.inputsPerAction, 1)
	itemEconomy.outputsPerAction =		number.toNumberOrDefault(args.outputsPerAction, 1)
	itemEconomy.preservationChance =	number.toNumberOrDefault(args.preservationChance, 0)
	itemEconomy.duplicationChance = 	number.toNumberOrDefault(args.duplicationChance, 0)
	itemEconomy.extraItemChance =		number.toNumberOrDefault(args.extraItemChance, 0)
	itemEconomy.extraItemAmount =		number.toNumberOrDefault(args.extraItemAmount, 1)
	itemEconomy.flatExtraItems =		number.toNumberOrDefault(args.flatExtraItems, 0)
	itemEconomy.extraBaseItemChance =	number.toNumberOrDefault(args.extraBaseItemChance, 0)
	itemEconomy.extraBaseItems =		number.toNumberOrDefault(args.extraBaseItems, 1)

	local result = 0
	local row = false
	
	if economyType == ECOOUT then
		result, row = calculateOutput(target, itemEconomy)
	else
		result, row = calculateInput(target, itemEconomy)
	end

	local multiplier = eco.estimatedOutputMultiplier(itemEconomy)
	local tbl = createTable(target, result, row, economyType, multiplier)
        
    return tostring(tbl)
end

return p