Module:Construction: Difference between revisions

From Against the Storm Official Wiki
m (Moved pipes down in the table)
m (typo in ID of pipes. it's singular.)
Tag: Manual revert
 
(9 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 = {
local RL_WILDFIRE = "Wildfire Essence"
     resource = "Parts", iconsize = "med" } }
 
local RL_WOOD = mw.getCurrentFrame():expandTemplate{ title="rl", args = {
local ID_LOOKUP_RL = {
     resource = "Wood", iconsize = "med" } }
    ["[Mat Raw] Wood"] = RL_WOOD,
local RL_PIPES = mw.getCurrentFrame():expandTemplate{ title="rl", args = {
    ["[Mat Processed] Planks"] = RL_PLANKS,
    resource = "Pipes", iconsize = "med" } }
    ["[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 = "'''"
local BOLD = "'''"
Line 38: Line 62:
---@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 65: Line 92:
     local parts = frame.args.parts
     local parts = frame.args.parts
     local pipes = frame.args.pipes
     local pipes = frame.args.pipes
    local wildfire = frame.args.wildfire


     local htmlTable = mw.html.create("table")
     local htmlTable = mw.html.create("table")
Line 79: Line 108:
     if fabric and fabric ~= "" and tonumber(fabric) > 0 then
     if fabric and fabric ~= "" and tonumber(fabric) > 0 then
         makeRow(htmlTable, fabric, RL_FABRIC)
         makeRow(htmlTable, fabric, RL_FABRIC)
    end
    if parts and parts ~= "" and tonumber(parts) > 0 then
        makeRow(htmlTable, parts, RL_PARTS)
     end
     end
     if pipes and pipes ~= "" and tonumber(pipes) > 0 then
     if pipes and pipes ~= "" and tonumber(pipes) > 0 then
         makeRow(htmlTable, pipes, RL_PIPES)
         makeRow(htmlTable, pipes, RL_PIPES)
     end
     end
     if parts and parts ~= "" and tonumber(parts) > 0 then
     if wildfire and wildfire ~= "" and tonumber(wildfire) > 0 then
         makeRow(htmlTable, parts, RL_PARTS)
         makeRow(htmlTable, wildfire, RL_WILDFIRE)
     end
     end
    htmlTable:done()


     return htmlTable
     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
end



Latest revision as of 01:47, 30 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 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