Module:ControllerUtilities: Difference between revisions

From Against the Storm Official Wiki
m (Fixed some error validation)
(Added method to replace sprite tags with appropriate wiki markup)
Line 12: Line 12:


--region Private Constants
--region Private Constants
local TEMPLATE_SPRITE_FINDER = "Sprite"


local PATTERN_EVERYTHING_BETWEEN_COMMAS_AND_SPACES = "[^%s,][^,]+[^%s,]"
local PATTERN_EVERYTHING_BETWEEN_COMMAS_AND_SPACES = "[^%s,][^,]+[^%s,]"
local PATTERN_SPRITE_NAME_CAPTURE_VALUE = '<sprite name="?([^>^"]+)"?>'


--endregion
--endregion
Line 44: Line 47:
end
end


function ControllerUtilities.findAndReplaceSpriteTagsWithFiles(description, currentFrame)
    description = description:gsub(PATTERN_SPRITE_NAME_CAPTURE_VALUE, function(name)
        return currentFrame:expandTemplate{
            title = TEMPLATE_SPRITE_FINDER,
            args = { ["id"] = name }
        }
    end)
    return description
end
--endregion
--endregion


return ControllerUtilities
return ControllerUtilities

Revision as of 23:38, 5 May 2024

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

---
--- Centralizes some common functionality across controllers
---
---@module ControllerUtilities
ControllerUtilities = {}

--region Public Constants

--endregion



--region Private Constants

local TEMPLATE_SPRITE_FINDER = "Sprite"

local PATTERN_EVERYTHING_BETWEEN_COMMAS_AND_SPACES = "[^%s,][^,]+[^%s,]"
local PATTERN_SPRITE_NAME_CAPTURE_VALUE = '<sprite name="?([^>^"]+)"?>'

--endregion



--region Public methods

--- Converts a string that represents a list of items into an array of those items. This is useful when a template parameter is used as a list and not a single value.
---
---@param commaSeparatedStringOfIDs string the list of IDs, separated by commas
---@param needsKeyValueFlags boolean true if the list should be made of keys (IDs) and values of `true`, false if the list should be an array of IDs
---@return table the IDs now separated into elements in a table
function ControllerUtilities.expandCommaSeparatedStringsIntoTable(commaSeparatedStringOfIDs, needsKeyValueFlags)

    if not commaSeparatedStringOfIDs or "" == commaSeparatedStringOfIDs then
        return {}
    end

    local list = {}
    for eachID in string.gmatch(commaSeparatedStringOfIDs, PATTERN_EVERYTHING_BETWEEN_COMMAS_AND_SPACES) do
        if needsKeyValueFlags then
            list[eachID] = true
        else
            table.insert(list, eachID)
        end
    end

    return list
end

function ControllerUtilities.findAndReplaceSpriteTagsWithFiles(description, currentFrame)

    description = description:gsub(PATTERN_SPRITE_NAME_CAPTURE_VALUE, function(name)
        return currentFrame:expandTemplate{
            title = TEMPLATE_SPRITE_FINDER,
            args = { ["id"] = name }
         }
    end)

    return description
end
--endregion

return ControllerUtilities