Module:Construction

From Against the Storm Official Wiki
Revision as of 02:28, 7 December 2023 by Aeredor (talk | contribs) (Added support for pipes)

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

---
--- Generates a small, inline-style table to show building materials. I tried
--- to do this with parser functions, but couldn't control the layout enough.
---
---@module Construction
local Construction = {}



--region Private constants

local RL_PLANKS = mw.getCurrentFrame():expandTemplate{ title="rl", args = {
    resource = "Planks", iconsize = "med" } }
local RL_BRICKS = mw.getCurrentFrame():expandTemplate{ title="rl", args = {
    resource = "Bricks", iconsize = "med" } }
local RL_FABRIC = mw.getCurrentFrame():expandTemplate{ title="rl", args = {
    resource = "Fabric", iconsize = "med" } }
local RL_PARTS = mw.getCurrentFrame():expandTemplate{ title="rl", args = {
    resource = "Parts", iconsize = "med" } }
local RL_WOOD = mw.getCurrentFrame():expandTemplate{ title="rl", args = {
    resource = "Wood", iconsize = "med" } }
local RL_PIPES = mw.getCurrentFrame():expandTemplate{ title="rl", args = {
    resource = "Pipes", iconsize = "med" } }

local BOLD = "'''"

--endregion



--region Private methods

---
--- Adds a simple table row with data cells to the provided HTML table entity.
---
---@param htmlTable table an HTML table entity
---@param amount number of building material
---@param resourceLink string one of the constants representing a resource link template invocation
local function makeRow(htmlTable, amount, resourceLink)

    local row = htmlTable:tag("tr")
    row:tag("td"):wikitext(BOLD .. amount .. BOLD):done()
    row:tag("td"):wikitext(resourceLink):done()
    row:done():newline()

    return row
end

--endregion



--region Public methods

---
--- Writes a simple HTML table with the specified template parameters
---
---@param frame table MediaWiki context
function Construction.renderTable(frame)

    local wood = frame.args.wood
    local planks = frame.args.planks
    local bricks = frame.args.bricks
    local fabric = frame.args.fabric
    local parts = frame.args.parts
    local pipes = frame.args.pipes

    local htmlTable = mw.html.create("table")

    if wood and wood ~= "" and tonumber(wood) > 0 then
        makeRow(htmlTable, wood, RL_WOOD)
    end
    if pipes and pipes ~= "" and tonumber(pipes) > 0 then
        makeRow(htmlTable, pipes, RL_PIPES)
    end
    if planks and planks ~= "" and tonumber(planks) > 0 then
        makeRow(htmlTable, planks, RL_PLANKS)
    end
    if bricks and bricks ~= "" and tonumber(bricks) > 0 then
        makeRow(htmlTable, bricks, RL_BRICKS)
    end
    if fabric and fabric ~= "" and tonumber(fabric) > 0 then
        makeRow(htmlTable, fabric, RL_FABRIC)
    end
    if parts and parts ~= "" and tonumber(parts) > 0 then
        makeRow(htmlTable, parts, RL_PARTS)
    end

    return htmlTable
end

--endregion

return Construction