Module:RarityHandler: Difference between revisions

No edit summary
Undo revision 4483 by Ryan (talk)
Tag: Undo
 
(7 intermediate revisions by the same user not shown)
Line 1: Line 1:
local p = {}
local p = {}


-- Clean: trim and strip leading ~
-- Clean up the rarity string: strip ~, commas, trim
local function clean(rarity)
local function clean(rarity)
    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)
    if rarity:sub(1, 1) == "~" then
rarity = rarity:gsub("^~", "") -- remove ~
        rarity = rarity:sub(2)
rarity = rarity:gsub(",", "") -- remove commas
    end
return rarity
    return rarity
end
end


function p.getSortValue(frame)
function p.getSortValue(frame)
    local raw = frame.args[1]
local raw = frame.args[1]
    if not raw or type(raw) ~= "string" then return 999999999 end
if not raw or type(raw) ~= "string" then return 999999999 end


    local trimmed = mw.text.trim(raw)
local rarity = clean(raw)
    if trimmed:sub(1, 1) == "~" then
if not rarity then return 999999999 end
        trimmed = trimmed:sub(2)
    end


    local lowered = string.lower(trimmed)
local lowered = rarity:lower()


    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 999999998
return 999999998
    elseif lowered == "never" then
elseif lowered == "never" then
        return 0
return 0
    end
end


    local chance = lowered:match("^1/(%d+%.?%d*)$")
-- Match 1/X, return raw number (not string)
    if chance then
local numStr = lowered:match("^1/(%d+%.?%d*)$")
        return tonumber(chance) or 999999999
if numStr then
    end
local num = tonumber(numStr)
if num then return math.floor(num) end
end


    return 999999999
return 999999999
end
end


-- For class logic: cleaned string, lowercase
-- For logic styling
function p.getCleanValue(frame)
function p.getCleanValue(frame)
    local rarity = clean(frame.args[1])
local rarity = clean(frame.args[1])
    if not rarity then return "Unknown" end
if not rarity then return "Unknown" end
 
if rarity == "1" then return "Always" end
    if rarity == "1" then
return rarity
        return "Always"
    end
 
    return rarity
end
end


-- For display
-- For display
function p.normalizeDisplay(frame)
function p.normalizeDisplay(frame)
    local original = frame.args[1]
local original = frame.args[1]
    if not original or type(original) ~= "string" then return "Unknown" end
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


    local trimmed = mw.text.trim(original)
if cleaned == "1" then return "Always" end
    local hasTilde = trimmed:sub(1, 1) == "~"
    local cleaned = hasTilde and trimmed:sub(2) or trimmed


    if cleaned == "1" then
local prefix, num = cleaned:match("^(1/)(%d+%.?%d*)$")
        return "Always"
if prefix and num then
    end
local withCommas = mw.language.getContentLanguage():formatNum(tonumber(num))
return (hasTilde and "~" or "") .. prefix .. withCommas
end


    return hasTilde and ("~" .. cleaned) or cleaned
return hasTilde and "~" .. cleaned or cleaned
end
end


return p
return p