Module:PriceCalculator: Difference between revisions

No edit summary
No edit summary
 
(7 intermediate revisions by the same user not shown)
Line 25: Line 25:


-- Calculate and return rounded result
-- Calculate and return rounded result
return math.floor(price * factor + 0.0)
    local result = math.floor(price * factor + 0.0)
return math.max(result, 1)
end
end


Line 39: Line 40:
local args = frame.args
local args = frame.args
local inputPrice = args.price or args[1] or "1"
local inputPrice = args.price or args[1] or "1"
local priceType = args.type or args[2] or "sell"
local priceType = mw.text.trim(args.type or args[2] or "sell")
local min = tonumber(args.min or args.MinimumQuantity or "1") or 1
local min = tonumber(args.min or args.MinimumQuantity or "1") or 1
local max = tonumber(args.max or args.MaximumQuantity or "") -- optional
local max = tonumber(args.max or args.MaximumQuantity or "") -- optional


-- Calculate actual price using your _calculate function
local rawPrice = p._calculate(inputPrice, priceType)
local price = tonumber(p._calculate(inputPrice, priceType))
if type(rawPrice) ~= "number" then
if not price then
return "Error: " .. tostring(rawPrice)
return "Error: invalid calculated price" .. tostring(price)
end
end
local price = rawPrice


-- Helper to format numbers
local function format(num)
local function format(num)
return mw.getContentLanguage():formatNum(num)
return mw.getContentLanguage():formatNum(num)
end
end


-- Coin icon HTML (customize as needed)
local coinIcon = frame:preprocess('{{CoinIcon|size=16px}}')
local coinIcon = '[[File:Coins.png|22px|link=Coins]]'


-- Logic for display
if max and max ~= min then
if max and max ~= min then
if price > 1 then
if price > 1 then
return string.format(
return string.format('<abbr title="%d coins each">%s–%s</abbr> %s', price, format(price * min), format(price * max), coinIcon)
'<abbr title="%d coins each">%s–%s</abbr>%s',
price,
format(price * min),
format(price * max),
coinIcon
)
else
else
return string.format('%s–%s%s', format(price * min), format(price * max), coinIcon)
return string.format('%s–%s %s', format(price * min), format(price * max), coinIcon)
end
end
elseif min == 1 then
elseif min == 1 then
return string.format('%s%s', format(price), coinIcon)
return string.format('%s %s', format(price), coinIcon)
else
else
if price > 1 then
if price > 1 then
return string.format(
return string.format('<abbr title="%d coins each">%s</abbr> %s', price, format(price * min), coinIcon)
'<abbr title="%d coins each">%s</abbr>%s',
price,
format(price * min),
coinIcon
)
else
else
return string.format('%s%s', format(price * min), coinIcon)
return string.format('%s %s', format(price * min), coinIcon)
end
end
end
end
end
end


return p
return p