Module:RarityHandler: Difference between revisions
No edit summary |
No edit summary |
||
Line 12: | Line 12: | ||
function p.getSortValue(frame) | function p.getSortValue(frame) | ||
local | local rarity = frame.args[1] | ||
if not | if not rarity or type(rarity) ~= "string" then | ||
return 999999999 | |||
end | |||
-- Trim whitespace and strip leading ~ | |||
if | rarity = mw.text.trim(rarity) | ||
if rarity:sub(1, 1) == "~" then | |||
rarity = rarity:sub(2) | |||
end | end | ||
local lowered = | -- Normalize lowercase | ||
local lowered = rarity:lower() | |||
-- Handle special values | |||
if lowered == "1" or lowered == "always" then | if lowered == "1" or lowered == "always" then | ||
return 1 | return 1 | ||
Line 30: | Line 35: | ||
end | end | ||
local | -- Match and convert fractional chance like "1/2.5", "1/128" | ||
if | local num = lowered:match("^1/(%d+%.?%d*)$") | ||
if num then | |||
local n = tonumber(num) | |||
if n then | |||
return n | |||
end | |||
end | end | ||
-- Unknown or badly formatted | |||
return 999999999 | return 999999999 | ||
end | end |