Module:PriceCalculator: Difference between revisions
No edit summary |
No edit summary |
||
(16 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
local p = {} | local p = {} | ||
-- Internal function to calculate price | |||
function p._calculate(price, priceType) | function p._calculate(price, priceType) | ||
-- Sanitize input: convert to string, remove commas | |||
price = tostring(price):gsub(",", "") | |||
price = tonumber(price) | price = tonumber(price) | ||
-- Error handling | |||
if not price or price < 0 then | if not price or price < 0 then | ||
return "Invalid price" | return "Invalid price: " .. tostring(price) | ||
end | end | ||
-- Lookup conversion factors | |||
local percent = { | local percent = { | ||
sell = 0. | sell = 0.375, | ||
minor = 0. | minor = 0.250125, | ||
major = | major = 0.375 | ||
} | } | ||
local factor = percent[priceType] | local factor = percent[priceType] | ||
if not factor then | if not factor then | ||
return "Invalid type" | return "Invalid type: " .. tostring(priceType) | ||
end | end | ||
return math.floor(price * factor + 0. | -- Calculate and return rounded result | ||
local result = math.floor(price * factor + 0.0) | |||
return math.max(result, 1) | |||
end | end | ||
-- Entry point for #invoke | |||
function p.calculate(frame) | function p.calculate(frame) | ||
local args = frame | local args = frame.args -- Use direct args from #invoke | ||
local price = args.price or args[1] | local price = args.price or args[1] | ||
local priceType = args.type or args[2] | local priceType = args.type or args[2] | ||
Line 28: | Line 37: | ||
end | end | ||
-- | function p.renderPrice(frame) | ||
local args = frame.args | |||
local inputPrice = args.price or args[1] or "1" | |||
return | 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 max = tonumber(args.max or args.MaximumQuantity or "") -- optional | |||
local rawPrice = p._calculate(inputPrice, priceType) | |||
if type(rawPrice) ~= "number" then | |||
return "Error: " .. tostring(rawPrice) | |||
end | end | ||
}) | local price = rawPrice | ||
local function format(num) | |||
return mw.getContentLanguage():formatNum(num) | |||
end | |||
local coinIcon = frame:preprocess('{{CoinIcon|size=16px}}') | |||
if max and max ~= min then | |||
if price > 1 then | |||
return string.format('<abbr title="%d coins each">%s–%s</abbr> %s', price, format(price * min), format(price * max), coinIcon) | |||
else | |||
return string.format('%s–%s %s', format(price * min), format(price * max), coinIcon) | |||
end | |||
elseif min == 1 then | |||
return string.format('%s %s', format(price), coinIcon) | |||
else | |||
if price > 1 then | |||
return string.format('<abbr title="%d coins each">%s</abbr> %s', price, format(price * min), coinIcon) | |||
else | |||
return string.format('%s %s', format(price * min), coinIcon) | |||
end | |||
end | |||
end | |||
return p |