Module:Building infobox: Difference between revisions

From Beyond These Stars Official Wiki
added icon
Add resource icons and change layout of construction resources
Line 3: Line 3:




local BuildingDataModule = require("Module:Building_data")
local Building_Data_Module = require("Module:Building_data")
local Resource_Data_Module = require("Module:Resource_data")




Line 9: Line 10:
--#region html building helper functions
--#region html building helper functions


---@diagnostic disable-next-line: duplicate-doc-alias
---@alias HtmlNode table
---@alias HtmlNode table


Line 14: Line 16:
local HEADER_CLASS = INFOBOX_CLASS .. "-header"
local HEADER_CLASS = INFOBOX_CLASS .. "-header"
local HEADER_ICON_CLASS = HEADER_CLASS .. "-icon"
local HEADER_ICON_CLASS = HEADER_CLASS .. "-icon"
local DESCRIPTION_CLASS = INFOBOX_CLASS .. "-description"
local DESCRIPTION_CLASS = INFOBOX_CLASS .. "-description"
local CONSTRUCTION_CLASS = INFOBOX_CLASS .. "-construction"
local CONSTRUCTION_CLASS = INFOBOX_CLASS .. "-construction"
local CONSTRUCTION_TABLE_CLASS = CONSTRUCTION_CLASS .. "-table"
local CONSTRUCTION_TABLE_CLASS = CONSTRUCTION_CLASS .. "-table"
local RESOURCE_ICON_CLASS = "bts-resource-icon"
local SMALL_ICON_SIZE = "32px"
local TASKS_CLASS = INFOBOX_CLASS .. "-tasks"
local TASKS_CLASS = INFOBOX_CLASS .. "-tasks"


--- Renders wikitext for the building's icon.
--- Renders wikitext for the building's icon.
---@param building BuildingRecord
---@param building Building
---@return string wikitext
---@return string wikitext
local function render_icon_markup(building)
local function render_icon_markup(building)
   local icon_filename = "BuildingPortrait_" .. building.name .. ".png"
   local icon_filename = building.portrait_icon .. ".png"
   local image_markup = "[[File:" .. icon_filename .. "|256px]]"
   local image_markup = "[[File:" .. icon_filename .. "|256px]]"
   return image_markup
   return image_markup
Line 30: Line 41:
--- Adds the header of the infobox to the provided html node.
--- Adds the header of the infobox to the provided html node.
---@param div HtmlNode enclosing infobox div
---@param div HtmlNode enclosing infobox div
---@param building BuildingRecord
---@param building Building
local function build_header(div, building)
local function add_header(div, building)
   local header = div:tag("div"):addClass(HEADER_CLASS)
   local header = div:tag("div"):addClass(HEADER_CLASS)
   header:tag("h2"):wikitext(building.display_name):done()
   header:tag("h2"):wikitext(building.display_name):done()
Line 42: Line 53:
--- Adds the building's description to the provided html node.
--- Adds the building's description to the provided html node.
---@param div HtmlNode enclosing infobox div
---@param div HtmlNode enclosing infobox div
---@param building BuildingRecord
---@param building Building
local function build_description(div, building)
local function add_description(div, building)
   local description = div:tag("div"):addClass(DESCRIPTION_CLASS)
   local description = div:tag("div"):addClass(DESCRIPTION_CLASS)
   building.description = building.description:gsub("<hb>", ""):gsub("</hb>", "")
   building.description = building.description:gsub("<hb>", ""):gsub("</hb>", "")
Line 52: Line 63:




--- Adds the construction resources to the provided html table row.
--- Adds an icon for a resource to the provided html table row.
---@param tr HtmlNode table row
---@param tr HtmlNode tr
---@param building BuildingRecord
---@param resource ResourceRecord
local function add_construction_resource_row(tr, building)
local function add_resource_icon(tr, resource)
   for _, pair in ipairs(building.construction_resources) do
   local icon_markup = ""
     if pair.name and pair.name ~= "" and pair.amount and pair.amount ~= "" then
  if resource and resource.icon then
      tr:tag("td"):wikitext(pair.name):done()
    local file_name = "File:" .. resource.icon .. ".png"
    end
     local icon_size = "x" .. SMALL_ICON_SIZE
    local name = resource.display_name
    local link_attr = "link=" .. resource.display_name
    icon_markup = "[[" .. file_name .. "|" .. icon_size .. "|" .. name .. "|" .. link_attr .. "]]"
   end
   end
  tr:tag("td"):tag("span"):addClass(RESOURCE_ICON_CLASS):wikitext(icon_markup):done():done()
end
end


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


--- Lays out the construction resources in the provided html node.
--- Lays out the construction resources in the provided html node.
---@param div HtmlNode enclosing infobox div
---@param div HtmlNode enclosing infobox div
---@param building BuildingRecord
---@param building Building
local function build_construction(div, building)
local function add_construction(div, building)
   if not building.construction_resources or building.construction_resources == {} then return end
   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 tbl = div:tag("div"):addClass(CONSTRUCTION_CLASS):tag("table"):addClass(CONSTRUCTION_TABLE_CLASS)
   local tr_names = table:tag("tr")
   for _, pair in ipairs(building.construction_resources) do
  add_construction_resource_row(tr_names, building)
    local tr = tbl:tag("tr")
  tr_names:done()
    tr:tag("td"):wikitext(pair.amount):done()
  local tr_amounts = table:tag("tr")
    local resource = Resource_Data_Module.get_resource(pair.name)
  add_construction_amount(tr_amounts, building)
    add_resource_icon(tr, resource)
  tr_amounts:done()
    add_resource_link(tr, resource)
   return table
    tr:done()
  end
   return tbl
end
end


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


Line 102: Line 119:


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


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

Revision as of 21:22, 23 September 2026

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

local p = {}



local Building_Data_Module = require("Module:Building_data")
local Resource_Data_Module = require("Module:Resource_data")



--#region html building helper functions

---@diagnostic disable-next-line: duplicate-doc-alias
---@alias HtmlNode table

local INFOBOX_CLASS = "bts-infobox-building"
local HEADER_CLASS = INFOBOX_CLASS .. "-header"
local HEADER_ICON_CLASS = HEADER_CLASS .. "-icon"

local DESCRIPTION_CLASS = INFOBOX_CLASS .. "-description"

local CONSTRUCTION_CLASS = INFOBOX_CLASS .. "-construction"
local CONSTRUCTION_TABLE_CLASS = CONSTRUCTION_CLASS .. "-table"

local RESOURCE_ICON_CLASS = "bts-resource-icon"

local SMALL_ICON_SIZE = "32px"

local TASKS_CLASS = INFOBOX_CLASS .. "-tasks"



--- Renders wikitext for the building's icon.
---@param building Building
---@return string wikitext
local function render_icon_markup(building)
  local icon_filename = building.portrait_icon .. ".png"
  local image_markup = "[[File:" .. icon_filename .. "|256px]]"
  return image_markup
end

--- Adds the header of the infobox to the provided html node.
---@param div HtmlNode enclosing infobox div
---@param building Building
local function add_header(div, building)
  local header = div:tag("div"):addClass(HEADER_CLASS)
  header:tag("h2"):wikitext(building.display_name):done()
  header:tag("div"):addClass(HEADER_ICON_CLASS):wikitext(render_icon_markup(building))
  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 Building
local function add_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 an icon for a resource to the provided html table row.
---@param tr HtmlNode tr
---@param resource ResourceRecord
local function add_resource_icon(tr, resource)
  local icon_markup = ""
  if resource and resource.icon then
    local file_name = "File:" .. resource.icon .. ".png"
    local icon_size = "x" .. SMALL_ICON_SIZE
    local name = resource.display_name
    local link_attr = "link=" .. resource.display_name
    icon_markup = "[[" .. file_name .. "|" .. icon_size .. "|" .. name .. "|" .. link_attr .. "]]"
  end
  tr:tag("td"):tag("span"):addClass(RESOURCE_ICON_CLASS):wikitext(icon_markup):done():done()
end

--- Adds a link for a resource to the provided html table row.
---@param tr HtmlNode tr
---@param resource ResourceRecord
local function add_resource_link(tr, resource)
  local link_markup = ""
  if resource and resource.display_name then
    link_markup = "[[" .. resource.display_name .. "]]"
  end
  tr:tag("td"):wikitext(link_markup):done()
end

--- Lays out the construction resources in the provided html node.
---@param div HtmlNode enclosing infobox div
---@param building Building
local function add_construction(div, building)
  if not building.construction_resources or building.construction_resources == {} then return end
  local tbl = div:tag("div"):addClass(CONSTRUCTION_CLASS):tag("table"):addClass(CONSTRUCTION_TABLE_CLASS)
  for _, pair in ipairs(building.construction_resources) do
    local tr = tbl:tag("tr")
    tr:tag("td"):wikitext(pair.amount):done()
    local resource = Resource_Data_Module.get_resource(pair.name)
    add_resource_icon(tr, resource)
    add_resource_link(tr, resource)
    tr:done()
  end
  return tbl
end



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



--- Builds the infobox wikitext and html for the given building.
---@param building Building
---@return string wikitext
local function build_infobox(building)
  ---@type HtmlNode
  local div = mw.html.create("div")
  div:addClass(INFOBOX_CLASS)
  add_header(div, building)
  add_description(div, building)
  add_construction(div, building)
  add_tasks(div, building)
  return tostring(div)
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 wikitext Infobox code or empty string if no building 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 = Building_Data_Module.get_building(name)
  if not building then return "" end
  local wikitext = build_infobox(building)
  return wikitext
end



return p