Module:Servebox

From Against the Storm Official Wiki
Revision as of 01:05, 28 November 2023 by Aeredor (talk | contribs) (Updated to use utility module Infobox)

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

---
--- Module to create an infobox for a service building's wiki page.
---
--- Shows the facts from the data about the specified service building in an
--- easy-to-read table, with a large icon, information, categories, and the
--- flavor text.
---
--- This should be invoked from Template:Servebox with the parameter of the
--- building whose infobox should be shown. See the template documentation for
--- more information about parameters and errors and to see examples.
---
--- @module Servebox
local Servebox = {}


local Infobox = require("Module:Infobox")
local InstitutionsData = require("Module:InstitutionsData")



--region Private constants

local ARG_NAME = "name"

-- Subheading for all the buildings from this template.
local BUILDING_CATEGORY = "Service Building"

local NBSP = " "

--endregion



--region Private methods

---
--- Builds using the provided wikiInfobox a subtable to lay out headers and
--- fields.
---
---@param wikiInfobox table mw.html object into which we're building this
---@param institutionName string the name of the good the infobox is about
---@return table the Mediawiki html object into which we've been adding things
local function makeInnerTable(wikiInfobox, institutionName)

	-- Grab the data we'll use to populate this.
	local institutionID = InstitutionsData.getInstitutionID(institutionName)
	local institutionCategory = InstitutionsData.getInstitutionCategory(institutionName)
	local institutionSizeX, institutionSizeY = InstitutionsData.getInstitutionSize(institutionName)
	local institutionIsMovable = InstitutionsData.isInstitutionMovable(institutionName)
	local institutionIsEssential = InstitutionsData.isInstitutionInitiallyEssential(institutionName)
	local institutionConstructionTime = InstitutionsData.getInstitutionConstructionTime(institutionName)
	local institutionWorkplaces = InstitutionsData.getInstitutionNumberOfWorkplaces(institutionName)
	local institutionRequiredGoods = InstitutionsData.getAllInstitutionRequiredGoods(institutionName)
	local institutionRecipesTable = InstitutionsData.getAllInstitutionRecipes(institutionName)

	-- Start the inner table
	local innerTable = wikiInfobox:tag("tr"):tag("td"):newline():tag("table"):css(Infobox.CSS_INNER_TABLE)
	innerTable:newline()

	-- Additional parameters would go here.

	-- Construction toolbar category.
	if institutionCategory then

		local categoryIconFilename = Infobox.ICON_FILENAMES_CATEGORIES[institutionCategory]
		if categoryIconFilename ~= nil then
			local categoryIconPart = "[[File:" .. categoryIconFilename .. "|" .. Infobox.ICONSIZE_MED .. "|alt=".. institutionCategory .. "]]"

			Infobox.buildStandardRow(innerTable, Infobox.CSS_TR_BORDER_TOP,
					Infobox.TITLE_CATEGORY, categoryIconPart .. NBSP .. institutionCategory)
		end
	end
	-- Expand recipes into a small table
	if institutionRecipesTable and #institutionRecipesTable > 0 then

		local recipesRows = mw.getCurrentFrame():expandTemplate{ title="Recipe", args={
			building=institutionName, display="list"	} }

		Infobox.buildStandardRow(innerTable, Infobox.CSS_TR_BORDER_BOT,
				Infobox.TITLE_RECIPES, recipesRows)
	end
	-- Since false is a valid value, have to directly check if it's nil.
	if institutionIsMovable ~= nil then

		Infobox.buildStandardRow(innerTable, Infobox.CSS_TR_BORDER_TOP,
				Infobox.TITLE_MOVABLE, institutionIsMovable and "Yes" or "No")
	end
	if institutionIsEssential ~= nil then

		Infobox.buildStandardRow(innerTable, Infobox.CSS_TR_BORDER_BOT,
				Infobox.TITLE_ESSENTIAL, institutionIsEssential and "Yes" or "No" )
	end
	-- Combine sizes into one line
	if institutionSizeX ~= nil and institutionSizeY ~= nil then

		Infobox.buildStandardRow(innerTable, Infobox.CSS_TR_BORDER_TOP,
				Infobox.TITLE_SIZE, institutionSizeX .. " × " .. institutionSizeY)
	end
	if institutionWorkplaces then

		Infobox.buildStandardRow(innerTable, Infobox.CSS_TR_BORDER_BOT,
				Infobox.TITLE_WORKPLACES, institutionWorkplaces)
	end
	if institutionConstructionTime then

		Infobox.buildStandardRow(innerTable, Infobox.CSS_TR_BORDER_TOP,
				Infobox.TITLE_CONSTRUCTION_TIME, institutionConstructionTime)
	end
	-- Need to build a separate table to display building materials
	if institutionRequiredGoods then

		local requiredGoodsRows = tostring( Infobox.writeRequiredGoodsRows(institutionRequiredGoods) )

		Infobox.buildStandardRow(innerTable, Infobox.CSS_TR_BORDER_BOT,
				Infobox.TITLE_CONSTRUCTION_COST, requiredGoodsRows)
	end
	if institutionID then

		innerTable:tag("tr"):css(Infobox.CSS_TR_BORDER_TOP):css(Infobox.CSS_TR_BORDER_BOT)
				  :tag("th"):wikitext(Infobox.TITLE_ID):done()
				  :tag("td"):wikitext(institutionID):done()
				  :done():newline()
	end

	-- Close the table.
	innerTable:done():newline()
	-- Close the table cell and row the inner table is in.
	wikiInfobox:done():done():newline()
end

--endregion



--region Public methods

---
--- Creates an html table to display the data in a floating box.
---
--- @param frame table the template's calling context
--- @return string wiki markup for the box
function Servebox.renderServebox(frame)
	
	-- Every institution must have a name.
	local institutionName = frame.args[ARG_NAME]
	if not institutionName or institutionName == "" then
		return "You must specify the name of the service building. See [[Template:Servebox]] for examples."
	end

	-- Use this method here to check to see whether the provided name is valid.
	if not InstitutionsData.getAllDataForInstitution(institutionName) then
		return "Servebox can't find the specified service building: " .. institutionName .. "."
	end

	-- Additional template parameters would go here.

	-- Get the data to display.
	local institutionDescription = InstitutionsData.getInstitutionDescription(institutionName)
	local institutionIconFilename = InstitutionsData.getInstitutionIcon(institutionName)

	-- Make the top of the infobox that every service building has...
	local wikiInfobox = mw.html.create("table"):css(Infobox.CSS_MAIN):newline()
	-- with a title...
	wikiInfobox:tag("tr")
			   :tag("th"):wikitext(institutionName):done()
			   :done():newline()
	-- and a subtitle, showing the functional category...
	wikiInfobox:tag("tr")
			:tag("td"):wikitext(BUILDING_CATEGORY):done()
		:done():newline()
	-- and a large icon.
	if institutionIconFilename then
		wikiInfobox:tag("tr")
				   :tag("td"):wikitext("[[File:" .. institutionIconFilename .. "]]"):done()
				   :done():newline()
		wikiInfobox:tag("tr"):css(Infobox.CSS_TR_BORDER_BOT)
				   :tag("td"):wikitext(institutionName .. ", as seen in-game"):done()
				   :done():newline()
	end
	
	makeInnerTable(wikiInfobox, institutionName)

	-- Finish with the flavor text.
	if institutionDescription then
		wikiInfobox:tag("tr"):css(Infobox.CSS_TR_BORDER_TOP)
				   :tag("td"):wikitext(institutionDescription):done()
				   :done():newline()
	end
	
	return wikiInfobox
end

--endregion

return Servebox