Module:Construction: Difference between revisions

From Against the Storm Official Wiki
(making sure we're not comparing empty strings)
m (updated the resource link parameter to medium)
 
(5 intermediate revisions by the same user not shown)
Line 6: Line 6:
local Construction = {}
local Construction = {}


--region Dependencies
-- none!
--endregion




--region Private constants
--region Private constants


local RL_PLANKS = mw.getCurrentFrame():expandTemplate{ title="rl", args = {
local RL_WOOD = "Wood"
    resource = "Planks", iconsize = "med" } }
local RL_PLANKS = "Planks"
local RL_BRICKS = mw.getCurrentFrame():expandTemplate{ title="rl", args = {
local RL_BRICKS = "Bricks"
     resource = "Bricks", iconsize = "med" } }
local RL_FABRIC = "Fabric"
local RL_FABRIC = mw.getCurrentFrame():expandTemplate{ title="rl", args = {
local RL_PARTS = "Parts"
     resource = "Fabric", iconsize = "med" } }
local RL_PIPES = "Pipes"
local RL_PARTS = mw.getCurrentFrame():expandTemplate{ title="rl", args = {
 
    resource = "Parts", iconsize = "med" } }
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,
}
 
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,
}
 
local INDEX_CONSTRUCTION_GOODS_AMOUNT = "amount"
local INDEX_CONSTRUCTION_GOODS_ID = "name"
 
local INDEX_LINK = "resource_link"


local BOLD = "'''"
local BOLD = "'''"
Line 34: Line 59:
---@param resourceLink string one of the constants representing a resource link template invocation
---@param resourceLink string one of the constants representing a resource link template invocation
local function makeRow(htmlTable, amount, resourceLink)
local function makeRow(htmlTable, amount, resourceLink)
    local link = mw.getCurrentFrame():expandTemplate{ title="rl", args = {
        name = resourceLink, size = "medium" } }


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


Line 55: Line 83:
function Construction.renderTable(frame)
function Construction.renderTable(frame)


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


     local htmlTable = mw.html.create("table")
     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
     if planks and planks ~= "" and tonumber(planks) > 0 then
         makeRow(htmlTable, planks, RL_PLANKS)
         makeRow(htmlTable, planks, RL_PLANKS)
Line 74: Line 107:
         makeRow(htmlTable, parts, RL_PARTS)
         makeRow(htmlTable, parts, RL_PARTS)
     end
     end
    if pipes and pipes ~= "" and tonumber(pipes) > 0 then
        makeRow(htmlTable, pipes, RL_PIPES)
    end
    htmlTable:done()
    return htmlTable
end
function Construction.makeTableFromData(constructionGoods)
    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, },
    }
    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:done()


     return htmlTable
     return htmlTable

Latest revision as of 17:10, 20 October 2024

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

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

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

    htmlTable:done()

    return htmlTable
end

function Construction.makeTableFromData(constructionGoods)

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

    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:done()

    return htmlTable
end

--endregion

return Construction