Module:Shop calc

This is the current revision of this page, as edited by Alex (talk | contribs) at 00:13, 17 October 2024 (Created page with "local p = {} local commas = require('Module:Addcommas')._add function expr(x) x = tostring(x) local expr_good, expr_val = pcall(mw.ext.ParserFunctions.expr, x) if expr_good then return tonumber(expr_val) end return nil end function p.main(frame) local input = frame:getParent().args local shopIncPercent = (input.shop_inc ~= nil and input.shop_inc ~= '') and tonumber(input.shop_inc) * .01 or 0 local calcTotal = p.calc(input.base_price, shopIncPercent, input.nu..."). The present address (URL) is a permanent link to this version.

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Documentation for this module may be created at Module:Shop calc/doc

local p = {}

local commas = require('Module:Addcommas')._add

function expr(x)
	x = tostring(x)
	local expr_good, expr_val = pcall(mw.ext.ParserFunctions.expr, x)
	if expr_good then
		return tonumber(expr_val)
	end
	return nil
end

function p.main(frame)
	local input = frame:getParent().args
	local shopIncPercent = (input.shop_inc ~= nil and input.shop_inc ~= '') and tonumber(input.shop_inc) * .01 or 0
	local calcTotal = p.calc(input.base_price, shopIncPercent, input.number_bought, tonumber(input.base_stock), tonumber(input.cur_stock))
	
	local costPerHop = commas(calcTotal)
	local averageCost = commas(calcTotal / input.number_bought)
	
	if (tonumber(input.total_purchased) > 0)
	then
		local totalCost = commas((calcTotal / input.number_bought) * input.total_purchased)
		return string.format('\n Total cost per hop: %s \n Average cost per item is: %s per item \n Total cost: %s', costPerHop, averageCost, totalCost)
	else
		return string.format('\n Total cost per hop: %s \n Average cost per item is: %s per item', costPerHop, averageCost)
	end
end

function p.calc(basePrice, shopIncrease, numberBought, baseStock, currentStock)
	local total = 0
	
	-- These two values are optional
	if (baseStock > 0 and currentStock > 0)
	then
		local first = baseStock - currentStock
		local last = numberBought + (baseStock - currentStock) - 1
		
		for i = first, last do
			--start at 1 and subtract 1 in function so it actually does 0 - (numberBought)
			total = total + basePrice + math.floor((shopIncrease * basePrice) * i)
		end
	else
		for i = 1, numberBought do
			--start at 1 and subtract 1 in function so it actually does 0 - (numberBought - 1)
			total = total + basePrice + math.floor((shopIncrease*basePrice)*(i - 1))
		end
	end
	
	return total
end

return p