Module:Construction

From Against the Storm Official Wiki
Revision as of 01:47, 30 October 2024 by Aeredor (talk | contribs) (typo in ID of pipes. it's singular.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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 Dependencies
-- none!
--endregion


--region Private constants

local RL_WOOD = "Wood"
local RL_PLANKS = "Planks"
local RL_BRICKS = "Bricks"
local RL_FABRIC = "Fabric"
local RL_PARTS = "Parts"
local RL_PIPES = "Pipes"
local RL_WILDFIRE = "Wildfire Essence"

local ID_LOOKUP_RL = {
    ["[Mat Raw] Wood"] = RL_WOOD,
    ["[Mat Processed] Planks"] = RL_PLANKS,
    ["[Mat Processed] Bricks"] = RL_BRICKS,
    ["[Mat Processed] Fabric"] = RL_FABRIC,
    ["[Mat Processed] Parts"] = RL_PARTS,
    ["[Mat Processed] Pipe"] = RL_PIPES,
    ["Hearth Parts"] = RL_WILDFIRE,
}

local ORDER_LOOKUP = {
    ["[Mat Raw] Wood"] = 1,
    ["[Mat Processed] Planks"] = 2,
    ["[Mat Processed] Bricks"] = 3,
    ["[Mat Processed] Fabric"] = 4,
    ["[Mat Processed] Parts"] = 5,
    ["[Mat Processed] Pipe"] = 6,
    ["Hearth Parts"] = 7,
}

local INDEX_CONSTRUCTION_GOODS_AMOUNT = "amount"
local INDEX_CONSTRUCTION_GOODS_ID = "name"

local INDEX_LINK = "resource_link"

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 link = mw.getCurrentFrame():expandTemplate{ title="rl", args = {
        name = resourceLink, size = "medium" } }

    local row = htmlTable:tag("tr")
    row:tag("td"):wikitext(BOLD .. amount .. BOLD):done()
    row:tag("td"):wikitext(link):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 wildfire = frame.args.wildfire


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

    if wood and wood ~= "" and tonumber(wood) > 0 then
        makeRow(htmlTable, wood, RL_WOOD)
    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
    if pipes and pipes ~= "" and tonumber(pipes) > 0 then
        makeRow(htmlTable, pipes, RL_PIPES)
    end
    if wildfire and wildfire ~= "" and tonumber(wildfire) > 0 then
        makeRow(htmlTable, wildfire, RL_WILDFIRE)
    end

    htmlTable:done()

    return htmlTable
end

function Construction.makeTableFromData(constructionGoods)

    if not constructionGoods or not next(constructionGoods) or #constructionGoods < 1 then
        return "None"
    end

    local orderedConstruction = {
        [1] = { [INDEX_CONSTRUCTION_GOODS_AMOUNT] = 0,
                [INDEX_LINK] = RL_WOOD, },
        [2] = { [INDEX_CONSTRUCTION_GOODS_AMOUNT] = 0,
                [INDEX_LINK] = RL_PLANKS, },
        [3] = { [INDEX_CONSTRUCTION_GOODS_AMOUNT] = 0,
                [INDEX_LINK] = RL_BRICKS, },
        [4] = { [INDEX_CONSTRUCTION_GOODS_AMOUNT] = 0,
                [INDEX_LINK] = RL_FABRIC, },
        [5] = { [INDEX_CONSTRUCTION_GOODS_AMOUNT] = 0,
                [INDEX_LINK] = RL_PARTS, },
        [6] = { [INDEX_CONSTRUCTION_GOODS_AMOUNT] = 0,
                [INDEX_LINK] = RL_PIPES, },
        [7] = { [INDEX_CONSTRUCTION_GOODS_AMOUNT] = 0,
                [INDEX_LINK] = RL_WILDFIRE, },
    }

    for _, stack in ipairs(constructionGoods) do
        local id = stack[INDEX_CONSTRUCTION_GOODS_ID]
        local order = ORDER_LOOKUP[id]
        orderedConstruction[order][INDEX_CONSTRUCTION_GOODS_AMOUNT] = stack[INDEX_CONSTRUCTION_GOODS_AMOUNT]
        orderedConstruction[order][INDEX_LINK] = ID_LOOKUP_RL[id]
    end

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

    for _, stack in ipairs(orderedConstruction) do
        if stack and stack[INDEX_CONSTRUCTION_GOODS_AMOUNT] > 1 then
            makeRow(htmlTable, stack[INDEX_CONSTRUCTION_GOODS_AMOUNT], stack[INDEX_LINK])
        end
    end

    htmlTable:allDone()

    return tostring(htmlTable)
end

--endregion

return Construction