Module:SpriteFinder

From Against the Storm Official Wiki
Revision as of 21:10, 5 May 2024 by Aeredor (talk | contribs) (Created to serve icons in perk and building descriptions)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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

--endregion


--region Private methods

local function toTitleCase(lowercaseString)

    return lowercaseString:gsub("(%a)([%w_']*)", function(first, rest)
        return first:upper() .. rest:lower()
    end)
end

--endregion



--region public methods

function SpriteFinder.main(frame)

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

    goodID = toTitleCase(goodID)

    local goodName = GoodsData.getGoodNameByID(goodID)
    if not goodName or "" == goodName then
        return BLANK_RETURN
    end

    local goodIcon = GoodsData.getGoodIconByID(goodID)
    if not goodIcon or "" == goodIcon then
        return BLANK_RETURN
    end

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

--endregion

return SpriteFinder