Module:SpriteFinder: Difference between revisions

From Against the Storm Official Wiki
m no title case for the word "of"
No edit summary
 
(6 intermediate revisions by 2 users not shown)
Line 22: Line 22:


local TEMPLATE_GRADE_ICON = {
local TEMPLATE_GRADE_ICON = {
    ["grade0"] = "0Star",
     ["grade1"] = "1Star",
     ["grade1"] = "1Star",
     ["grade2"] = "2Star",
     ["grade2"] = "2Star",
Line 37: Line 38:
         -- Skip the word "of" in packs of goods
         -- Skip the word "of" in packs of goods
         if first == "o" and rest == "f" then
         if first == "o" and rest == "f" then
            return first .. rest
        end
        -- Skip the string "[WE]" to avoid incorrect lowercase on Enriched Fuel id
        if first == "W" and rest == "E" then
             return first .. rest
             return first .. rest
         end
         end
Line 59: Line 64:
     if TEMPLATE_GRADE_ICON[id] then
     if TEMPLATE_GRADE_ICON[id] then
         return frame:expandTemplate{ title = TEMPLATE_GRADE_ICON[id] }
         return frame:expandTemplate{ title = TEMPLATE_GRADE_ICON[id] }
    -- Filter out Glade icons.
    elseif id == "dangerous" or id == "forbidden" then
    id = toTitleCase(id)
    return "[[File:Icon Order " .. id .. "Discovery.png|" .. ICON_SIZE .. "|alt=" .. id .. "|link=" .. "Glades" .. "]]"
    -- Filter out Glade Event solve type icons.
    elseif id == "dec_loyalty" then
    return "[[File:FlatIcon Loy.png|" .. ICON_SIZE .. "|alt=" .. id .. "|link=" .. "Glade Events" .. "]]"
    elseif id == "dec_empathy" then
    return "[[File:FlatIcon Emp.png|" .. ICON_SIZE .. "|alt=" .. id .. "|link=" .. "Glade Events" .. "]]"
    elseif id == "dec_corruption" then
    return "[[File:FlatIcon Corr.png|" .. ICON_SIZE .. "|alt=" .. id .. "|link=" .. "Glade Events" .. "]]"
     -- The rest are IDs for goods.
     -- The rest are IDs for goods.
     else
     else

Latest revision as of 20:51, 26 October 2025

Documentation for this module may be created at Module:SpriteFinder/doc

---
--- Replaces sprite XML tags in descriptions with small icons.
---
---@module SpriteFinder
SpriteFinder = {}

--region Dependencies

local GoodsData = require("Module:GoodsData")

--endregion



--region Private constants

local ARG_ID = "id"

local BLANK_RETURN = "?"

local ICON_SIZE = mw.getCurrentFrame():expandTemplate{ title="ImgS" }

local TEMPLATE_GRADE_ICON = {
    ["grade0"] = "0Star",
    ["grade1"] = "1Star",
    ["grade2"] = "2Star",
    ["grade3"] = "3Star"
}

--endregion


--region Private methods

local function toTitleCase(lowercaseString)

    return lowercaseString:gsub("(%a)([%w_']*)", function(first, rest)
        -- Skip the word "of" in packs of goods
        if first == "o" and rest == "f" then
            return first .. rest
        end
        -- Skip the string "[WE]" to avoid incorrect lowercase on Enriched Fuel id
        if first == "W" and rest == "E" then
            return first .. rest
        end
        return first:upper() .. rest:lower()
    end)
end

--endregion



--region public methods

function SpriteFinder.main(frame)

    local id = frame.args[ARG_ID]
    if not id or "" == id then
        return BLANK_RETURN
    end

    -- Filter out the stars first.
    if TEMPLATE_GRADE_ICON[id] then
        return frame:expandTemplate{ title = TEMPLATE_GRADE_ICON[id] }
    -- Filter out Glade icons.
    elseif id == "dangerous" or id == "forbidden" then
    	id = toTitleCase(id)
    	return "[[File:Icon Order " .. id .. "Discovery.png|" .. ICON_SIZE .. "|alt=" .. id .. "|link=" .. "Glades" .. "]]"
    -- Filter out Glade Event solve type icons.
    elseif id == "dec_loyalty" then
    	return "[[File:FlatIcon Loy.png|" .. ICON_SIZE .. "|alt=" .. id .. "|link=" .. "Glade Events" .. "]]"
    elseif id == "dec_empathy" then
    	return "[[File:FlatIcon Emp.png|" .. ICON_SIZE .. "|alt=" .. id .. "|link=" .. "Glade Events" .. "]]"
    elseif id == "dec_corruption" then
    	return "[[File:FlatIcon Corr.png|" .. ICON_SIZE .. "|alt=" .. id .. "|link=" .. "Glade Events" .. "]]"
    -- The rest are IDs for goods.
    else
        id = toTitleCase(id)
        local goodName = GoodsData.getGoodNameByID(id)
        if not goodName or "" == goodName then
            return BLANK_RETURN
        end
        local goodIcon = GoodsData.getGoodIconByID(id)
        if not goodIcon or "" == goodIcon then
            return BLANK_RETURN
        end

        return "[[File:" .. goodIcon .. "|" .. ICON_SIZE .. "|alt=" .. goodName .. "|link=" .. goodName .. "]]"
    end
end

--endregion

return SpriteFinder