Module:SpecializationLink: Difference between revisions

From Against the Storm Official Wiki
(Updated with new specialization names; refactored the code; now using StyleUtils)
m (renaming first parameter to name for consistency with other link templates)
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
---Retrieves and validates data on the specified specialization, then sends data over to the view, another template.
---@module SpecializationLink
local SpecializationLink = {}
local SpecializationLink = {}


Line 5: Line 7:
--region Dependencies
--region Dependencies


local StyleUtils = require("Module:StyleUtils")
local SpecializationsData = mw.loadData("Module:SpecializationsData")
 
local VIEW_TEMPLATE = "Specialization_link/view"


--endregion
--endregion
Line 12: Line 16:


--region Private constants
--region Private constants
local SPEC_ALCH = "Alchemy"
local SPEC_BREW = "Brewing"
local SPEC_ENGI = "Engineering"
local SPEC_FARM = "Farming"
local SPEC_FORE = "Forest"
local SPEC_MEAT = "Meat production"
local SPEC_RAIN = "Rainwater"
local SPEC_TAIL = "Tailoring"
local SPEC_WARM = "Warmth"
local SPEC_WOOD = "Woodworking"
-- Icon filenames
local SPECIALIZATION_ICON_FILENAMES = {
    [SPEC_ALCH] = "Icon Spec Alchemy 64x64.png",
    [SPEC_BREW] = "Icon Spec Brewing 64x64.png",
    [SPEC_ENGI] = "Icon Spec Gear 64x64.png",
    [SPEC_FARM] = "Icon_Spec_Farm_64x64.png",
    [SPEC_FORE] = "Icon Spec Forest 64x64.png",
    [SPEC_MEAT] = "Icon_Spec_Meat_64x64.png",
    [SPEC_RAIN] = "Icon Spec RainWater 64x64.png",
    [SPEC_TAIL] = "Icon Spec Cloth 64x64.png",
    [SPEC_WARM] = "Icon_Spec_Fire_64x64.png",
    [SPEC_WOOD] = "Icon_Spec_Wood_64x64.png"
}
--endregion
--region Private member variables


--endregion
--endregion
--region Private methods
local function resolveIconSize(iconSizeRequested)
    -- default for these links is small
    local size = StyleUtils.IMG_S()
    if iconSizeRequested == "med" then
        size = StyleUtils.IMG_M()
    else
        if iconSizeRequested == "large" then
            size = StyleUtils.IMG_L()
        else
            if iconSizeRequested == "huge" then
                size = StyleUtils.IMG_H()
            end
        end
    end
    return size
end
local function makeIconPart(pageName, iconFilename, iconSize)
    -- Skip the icon if none requested. Continue if empty!
    if iconSize == "none" then
        return ""
    end
    local size = resolveIconSize(iconSize)
    return string.format("[[File:%s|%s|link=%s|alt=%s Specialization|%s Specialization]] ", iconFilename, size, pageName, pageName, pageName)
end
local function renderParts(specialization, iconFilename, iconSize)
    local iconPart = makeIconPart(specialization, iconFilename, iconSize)
    local linkPart = "[[" .. specialization .. " Specialization|" .. specialization .. "]]"
    return iconPart .. linkPart
end
--endregion




Line 102: Line 23:
--region Public methods
--region Public methods


--- Renders MediaWiki markup to display the name of a wiki page and an icon.
---@public
---Validates data then compiles arguments to send to the view template.
---
---
---@param frame table the MediaWiki calling context
---@param frame table the MediaWiki calling context
---@return string wiki markup with the results
---@return string wiki markup with the results
function SpecializationLink.renderLink(frame)
function SpecializationLink.main(frame)


     -- Extract the template parameters.
     --Extract the template parameters.
     local spec = frame.args.spec or frame.args[1]
     local specialization = frame.args.name or frame.args[1]
     local iconSize = frame.args.iconsize
     local iconSize = frame.args.size
    local showType = frame.args.display == "show_type"
    local noText = frame.args.display == "notext"


     if not spec or "" == spec then
     if not specialization or "" == specialization then
         return "The Specialization link template requires the name of a specialization."
         error("The Specialization link template requires the name of a specialization")
    end
 
    --Use the icon filename for validation check.
    local iconFilename = nil
    local type= "Type"
    local species = "Species"
 
    for _, specData in ipairs(SpecializationsData.specializations) do
        --Once match found, extract the data
        if string.lower(specData[SpecializationsData.NAME]) == string.lower(specialization) then
            iconFilename = specData[SpecializationsData.ICON_FILENAME]
            type = (specData[SpecializationsData.IS_COMFORTABLE] and "Comfort") or (specData[SpecializationsData.IS_PROFICIENCY] and "Proficiency") or "Type"
        end
     end
     end


    -- Use our local hardcoded data to validate the name provided.
    local iconFilename = SPECIALIZATION_ICON_FILENAMES[spec]
     if not iconFilename then
     if not iconFilename then
         return "No specialization found with that name: " .. spec .. "."
         error("No specialization found with that name: " .. specialization)
     end
     end


     return renderParts(spec, iconFilename, iconSize)
     local viewParameters = {
        ["name"] = specialization,
        ["iconfilename"] = iconFilename,
        ["iconsize"] = iconSize,
        ["type"] = showType and type or "",
        ["display"] = noText and "notext" or "",
    }


    return frame:expandTemplate{
        title = VIEW_TEMPLATE,
        args = viewParameters,
    }
end
end



Latest revision as of 21:53, 8 November 2024

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

---Retrieves and validates data on the specified specialization, then sends data over to the view, another template.
---@module SpecializationLink
local SpecializationLink = {}



--region Dependencies

local SpecializationsData = mw.loadData("Module:SpecializationsData")

local VIEW_TEMPLATE = "Specialization_link/view"

--endregion



--region Private constants

--endregion



--region Public methods

---@public
---Validates data then compiles arguments to send to the view template.
---
---@param frame table the MediaWiki calling context
---@return string wiki markup with the results
function SpecializationLink.main(frame)

    --Extract the template parameters.
    local specialization = frame.args.name or frame.args[1]
    local iconSize = frame.args.size
    local showType = frame.args.display == "show_type"
    local noText = frame.args.display == "notext"

    if not specialization or "" == specialization then
        error("The Specialization link template requires the name of a specialization")
    end

    --Use the icon filename for validation check.
    local iconFilename = nil
    local type= "Type"
    local species = "Species"

    for _, specData in ipairs(SpecializationsData.specializations) do
        --Once match found, extract the data
        if string.lower(specData[SpecializationsData.NAME]) == string.lower(specialization) then
            iconFilename = specData[SpecializationsData.ICON_FILENAME]
            type = (specData[SpecializationsData.IS_COMFORTABLE] and "Comfort") or (specData[SpecializationsData.IS_PROFICIENCY] and "Proficiency") or "Type"
        end
    end

    if not iconFilename then
        error("No specialization found with that name: " .. specialization)
    end

    local viewParameters = {
        ["name"] = specialization,
        ["iconfilename"] = iconFilename,
        ["iconsize"] = iconSize,
        ["type"] = showType and type or "",
        ["display"] = noText and "notext" or "",
    }

    return frame:expandTemplate{
        title = VIEW_TEMPLATE,
        args = viewParameters,
    }
end

--endregion

return SpecializationLink