Module:RarityHandler: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
Line 15: | Line 15: | ||
function p.getSortValue(frame) | function p.getSortValue(frame) | ||
local | local raw = frame.args[1] | ||
if not | if not raw or type(raw) ~= "string" then | ||
return 999999999 | return 999999999 | ||
end | end | ||
local rarity = clean(raw) | |||
if not rarity then return 999999999 end | |||
if rarity | |||
local lowered = rarity:lower() | local lowered = rarity:lower() | ||
Line 38: | Line 34: | ||
end | end | ||
-- Match and convert fractional chance like "1/2.5", "1/ | -- Match and convert fractional chance like "1/2.5", "1/10000" | ||
local num = lowered:match("^1/(%d+%.?%d*)$") | local num = lowered:match("^1/(%d+%.?%d*)$") | ||
if num then | if num then | ||
local n = tonumber(num) | local n = tonumber(num) | ||
if n then | if n then | ||
return n | return math.floor(n) | ||
end | end | ||
end | end | ||
return 999999999 | return 999999999 | ||
end | end |
Revision as of 13:54, 19 May 2025
Documentation for this module may be created at Module:RarityHandler/doc
local p = {}
-- Clean: trim and strip leading ~
local function clean(rarity)
if not rarity or type(rarity) ~= "string" then return nil end
rarity = mw.text.trim(rarity)
-- Strip leading ~
if rarity:sub(1, 1) == "~" then
rarity = rarity:sub(2)
end
-- Strip commas
rarity = rarity:gsub(",", "")
return rarity
end
function p.getSortValue(frame)
local raw = frame.args[1]
if not raw or type(raw) ~= "string" then
return 999999999
end
local rarity = clean(raw)
if not rarity then return 999999999 end
local lowered = rarity:lower()
-- Handle special values
if lowered == "1" or lowered == "always" then
return 1
elseif lowered == "rare" then
return 999999998
elseif lowered == "never" then
return 0
end
-- Match and convert fractional chance like "1/2.5", "1/10000"
local num = lowered:match("^1/(%d+%.?%d*)$")
if num then
local n = tonumber(num)
if n then
return math.floor(n)
end
end
return 999999999
end
-- For class logic: cleaned string, lowercase
function p.getCleanValue(frame)
local rarity = clean(frame.args[1])
if not rarity then return "Unknown" end
if rarity == "1" then
return "Always"
end
return rarity
end
-- For display
function p.normalizeDisplay(frame)
local original = frame.args[1]
if not original or type(original) ~= "string" then return "Unknown" end
local trimmed = mw.text.trim(original)
local hasTilde = trimmed:sub(1, 1) == "~"
local cleaned = hasTilde and trimmed:sub(2) or trimmed
if cleaned == "1" then
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
return hasTilde and ("~" .. cleaned) or cleaned
end
return p