Module:Building infobox: Difference between revisions

From Beyond These Stars Official Wiki
WIP building infobox
m WIP building infobox
Line 3: Line 3:




local BuildingDataModule = require("Module-Building_data")
local BuildingDataModule = require("Module:Building_data")





Revision as of 13:58, 13 September 2026

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

local p = {}



local BuildingDataModule = require("Module:Building_data")



--#region html building helper functions

---@alias HtmlNode table

local INFOBOX_CLASS = "bts-infobox-building"
local HEADER_CLASS = INFOBOX_CLASS .. "-header"
local DESCRIPTION_CLASS = INFOBOX_CLASS .. "-description"
local CONSTRUCTION_CLASS = INFOBOX_CLASS .. "-construction"
local CONSTRUCTION_TABLE_CLASS = CONSTRUCTION_CLASS .. "-table"
local TASKS_CLASS = INFOBOX_CLASS .. "-tasks"

--- Adds the header of the infobox to the provided html node.
---@param div HtmlNode enclosing infobox div
---@param building BuildingRecord
local function build_header(div, building)
  local header = div:tag("div"):addClass(HEADER_CLASS)
  header:tag("h2"):wikitext(building.display_name):done()
  header:tag("h3"):wikitext(building.category):done()
end

--- Adds the building's description to the provided html node.
---@param div HtmlNode enclosing infobox div
---@param building BuildingRecord
local function build_description(div, building)
  local description = div:tag("div"):addClass(DESCRIPTION_CLASS)
  building.description = building.description:gsub("<hb>", ""):gsub("</hb>", "")
  description:tag("p"):wikitext(building.description):done()
  return description
end

--- Adds the construction resources to the provided html table row.
---@param tr HtmlNode table row
---@param building BuildingRecord
local function add_construction_resource_row(tr, building)
  for _, pair in ipairs(building.construction_resources) do
    if pair.name and pair.name ~= "" and pair.amount and pair.amount ~= "" then
      tr:tag("td"):wikitext(pair.name):done()
    end
  end
end

--- Adds the construction amount to the provided html table row.
--- @param tr HtmlNode table row
--- @param building BuildingRecord
local function add_construction_amount(tr, building)
  for _, pair in ipairs(building.construction_resources) do
    if pair.name and pair.name ~= "" and pair.amount and pair.amount ~= "" then
      tr:tag("td"):wikitext(pair.amount):done()
    end
  end
end

--- Lays out the construction resources in the provided html node.
---@param div HtmlNode enclosing infobox div
---@param building BuildingRecord
local function build_construction(div, building)
  if not building.construction_resources or building.construction_resources == {} then return end
  local table = div:tag("div"):addClass(CONSTRUCTION_CLASS):tag("table"):addClass(CONSTRUCTION_TABLE_CLASS)
  local tr_names = table:tag("tr")
  add_construction_resource_row(tr_names, building)
  tr_names:done()
  local tr_amounts = table:tag("tr")
  add_construction_amount(tr_amounts, building)
  tr_amounts:done()
  return table
end

--- Lays out the building's tasks in the provided html node.
---@param div HtmlNode enclosing infobox div
---@param building BuildingRecord
local function build_tasks(div, building)
  if not building.tasks or building.tasks == {} then return end
  local section = div:tag("div"):addClass(TASKS_CLASS)
end

--- Builds the infobox wikitext and html for the given building.
---@param building BuildingRecord
---@return string wikitext
local function build_infobox(building)
  ---@type HtmlNode
  local html = mw.html.create("div")
  html:addClass(INFOBOX_CLASS)
  build_header(html, building)
  build_description(html, building)
  build_construction(html, building)
  build_tasks(html, building)
  return tostring(html)
end

--#endregion



--- `#invoke` entry point. Reads `page_name` from invoke args (not the parent 
--- template); falls back to the current page title.
---@param frame table Scribunto frame from `#invoke`.
---@return string|nil Infobox wikitext, or nil if no cargo row exists.
function p.render(frame)
  local args = frame.args or {}
  ---@type string
  local name = args[1] or args.page_name or mw.title.getCurrentTitle().fullText
  local building = BuildingDataModule.get_building(name)
  if not building then return nil end
  local wikitext = build_infobox(building)
  return wikitext
end



return p