Module:SpriteFinder: Difference between revisions

From Against the Storm Official Wiki
(Created to serve icons in perk and building descriptions)
 
m (adding grade 0 sprite)
 
(2 intermediate revisions by the same user not shown)
Line 20: Line 20:


local ICON_SIZE = mw.getCurrentFrame():expandTemplate{ title="ImgS" }
local ICON_SIZE = mw.getCurrentFrame():expandTemplate{ title="ImgS" }
local TEMPLATE_GRADE_ICON = {
    ["grade0"] = "0Star",
    ["grade1"] = "1Star",
    ["grade2"] = "2Star",
    ["grade3"] = "3Star"
}


--endregion
--endregion
Line 29: Line 36:


     return lowercaseString:gsub("(%a)([%w_']*)", function(first, rest)
     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
         return first:upper() .. rest:lower()
         return first:upper() .. rest:lower()
     end)
     end)
Line 41: Line 52:
function SpriteFinder.main(frame)
function SpriteFinder.main(frame)


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


     goodID = toTitleCase(goodID)
     -- Filter out the stars first.
 
    if TEMPLATE_GRADE_ICON[id] then
    local goodName = GoodsData.getGoodNameByID(goodID)
        return frame:expandTemplate{ title = TEMPLATE_GRADE_ICON[id] }
    if not goodName or "" == goodName then
    -- The rest are IDs for goods.
         return BLANK_RETURN
    else
    end
        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


    local goodIcon = GoodsData.getGoodIconByID(goodID)
        return "[[File:" .. goodIcon .. "|" .. ICON_SIZE .. "|alt=" .. goodName .. "|link=" .. goodName .. "]]"
    if not goodIcon or "" == goodIcon then
        return BLANK_RETURN
     end
     end
    return "[[File:" .. goodIcon .. "|" .. ICON_SIZE .. "|alt=" .. goodName .. "|link=" .. goodName .. "]]"
end
end



Latest revision as of 03:11, 9 October 2024

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
        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] }
    -- 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