Module:PriceCalculator: Difference between revisions
No edit summary |
No edit summary |
||
(12 intermediate revisions by the same user not shown) | |||
Line 14: | Line 14: | ||
-- Lookup conversion factors | -- Lookup conversion factors | ||
local percent = { | local percent = { | ||
sell = 0. | sell = 0.375, | ||
minor = 0. | minor = 0.250125, | ||
major = | major = 0.375 | ||
} | } | ||
Line 25: | Line 25: | ||
-- Calculate and return rounded result | -- Calculate and return rounded result | ||
local result = math.floor(price * factor + 0.0) | |||
return math.max(result, 1) | |||
end | end | ||
Line 34: | Line 35: | ||
local priceType = args.type or args[2] | local priceType = args.type or args[2] | ||
return p._calculate(price, priceType) | return p._calculate(price, priceType) | ||
end | |||
function p.renderPrice(frame) | |||
local args = frame.args | |||
local inputPrice = args.price or args[1] or "1" | |||
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 | |||
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 | end | ||
return p | return p |