Module:RarityHandler: Difference between revisions
No edit summary |
No edit summary |
||
(6 intermediate revisions by the same user not shown) | |||
Line 5: | Line 5: | ||
if not rarity or type(rarity) ~= "string" then return nil end | if not rarity or type(rarity) ~= "string" then return nil end | ||
rarity = mw.text.trim(rarity) | rarity = mw.text.trim(rarity) | ||
-- Strip leading ~ | |||
if rarity:sub(1, 1) == "~" then | if rarity:sub(1, 1) == "~" then | ||
rarity = rarity:sub(2) | rarity = rarity:sub(2) | ||
end | end | ||
-- Strip commas | |||
rarity = rarity:gsub(",", "") | |||
return rarity | return rarity | ||
end | end | ||
function p.getSortValue(frame) | function p.getSortValue(frame) | ||
local rarity = | local rarity = frame.args[1] | ||
if not rarity then return | if not rarity or type(rarity) ~= "string" then | ||
return 999999999 | |||
end | |||
-- Trim whitespace and strip leading ~ | |||
rarity = mw.text.trim(rarity) | |||
if rarity:sub(1, 1) == "~" then | |||
rarity = rarity:sub(2) | |||
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 | ||
elseif lowered == "rare" then | elseif lowered == "rare" then | ||
return | return 999999998 | ||
elseif lowered == "never" then | elseif lowered == "never" then | ||
return | return 0 | ||
end | |||
return | |||
-- Match and convert fractional chance like "1/2.5", "1/128" | |||
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 | |||
end | end | ||
Line 33: | Line 54: | ||
function p.getCleanValue(frame) | function p.getCleanValue(frame) | ||
local rarity = clean(frame.args[1]) | local rarity = clean(frame.args[1]) | ||
return rarity | if not rarity then return "Unknown" end | ||
if rarity == "1" then | |||
return "Always" | |||
end | |||
return rarity | |||
end | end | ||
Line 47: | Line 74: | ||
if cleaned == "1" then | if cleaned == "1" then | ||
return "Always" | return "Always" | ||
end | |||
-- Format numbers like 1/10000 → 1/10,000 | |||
local prefix, num = cleaned:match("^(1/)(%d+%.?%d*)$") | |||
if prefix and num then | |||
local withCommas = mw.language.getContentLanguage():formatNum(tonumber(num)) | |||
return (hasTilde and "~" or "") .. prefix .. withCommas | |||
end | end | ||