Module:Campbox

From Against the Storm Official Wiki
Revision as of 02:10, 28 November 2023 by Aeredor (talk | contribs) (Adding quotes around the ID)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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

---
--- Module to create an infobox for displaying on a camp's wiki page.
---
--- Shows the facts from the data about the specified building in an easy-to-
--- read table, with a large icon, information, categories, and the flavor
--- text.
---
--- This should be invoked from Template:Campbox 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 Campbox
local Campbox = {}



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



--region Private constants

local ARG_NAME = "name"

-- Subheading for all the buildings from this template.
local BUILDING_CATEGORY = "Gathering Camp"

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 campName string the name of the camp the infobox is about
---@return table the Mediawiki html object into which we've been adding things
local function makeInnerTable(wikiInfobox, campName)

	-- Grab the data we'll use to populate this.
	local campID = CampsData.getCampID(campName)
	local campCategory = CampsData.getCampCategory(campName)
	local campSizeX, campSizeY = CampsData.getCampSize(campName)
	local campIsMovable = CampsData.isCampMovable(campName)
	local campIsEssential = CampsData.isCampInitiallyEssential(campName)
	local campStorageCap = CampsData.getCampStorage(campName)
	local campArea = CampsData.getCampArea(campName)
	local campConstructionTime = CampsData.getCampConstructionTime(campName)
	local campWorkplaces = CampsData.getCampNumberOfWorkplaces(campName)
	local campRequiredGoods = CampsData.getAllCampRequiredGoods(campName)
	local campRecipesTable = CampsData.getAllCampRecipes(campName)

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

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

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

		local recipesRows = mw.getCurrentFrame():expandTemplate{ title="Recipe", args={
			building= campName, 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 campIsMovable ~= nil then

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

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

		Infobox.buildStandardRow(innerTable, Infobox.CSS_TR_BORDER_TOP,
				Infobox.TITLE_SIZE, campSizeX .. " × " .. campSizeY)
	end
	if campArea then

		Infobox.buildStandardRow(innerTable, "",
				Infobox.TITLE_GATHERING_AREA, campArea)
	end
	if campStorageCap then

		Infobox.buildStandardRow(innerTable, "",
				Infobox.TITLE_STORAGE_CAPACITY, campStorageCap)
	end
	if campWorkplaces then

		Infobox.buildStandardRow(innerTable, Infobox.CSS_TR_BORDER_BOT,
				Infobox.TITLE_WORKPLACES, campWorkplaces)
	end
	if campConstructionTime then

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

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

		Infobox.buildStandardRow(innerTable, Infobox.CSS_TR_BORDER_BOT,
				Infobox.TITLE_CONSTRUCTION_COST, requiredGoodsRows)

		innerTable:tag("tr"):css(Infobox.CSS_TR_BORDER_BOT)
	end
	if campID 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("\'" .. campID .. "\'"):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 the template's calling context
-- @return wiki markup for the box
function Campbox.renderCampbox(frame)
	
	-- Every camp must have a name.
	local campName = frame.args[ARG_NAME]
	if not campName or campName == "" then
		return "You must specify the name of the camp. See [[Template:Campbox]] for examples."
	end

	-- Use this method here to check to see whether the provided name is valid.
	if not CampsData.getAllDataForCamp(campName) then
		return "Campbox can't find the specified camp: " .. campName .. "."
	end

	-- Additional template parameters would go here.

	-- Get the data to display.
	local campDescription = CampsData.getCampDescription(campName)
	local campIconFilename = CampsData.getCampIcon(campName)

	-- Make the top of the infobox that every one has...
	local wikiInfobox = mw.html.create("table"):css(Infobox.CSS_MAIN):newline()
	-- with a title...
	wikiInfobox:tag("tr")
			   :tag("th"):wikitext(campName):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 campIconFilename then
		wikiInfobox:tag("tr")
				   :tag("td"):wikitext("[[File:" .. campIconFilename .. "]]"):done()
				   :done():newline()
		wikiInfobox:tag("tr"):css(Infobox.CSS_TR_BORDER_BOT)
				   :tag("td"):wikitext(campName .. ", as seen in-game"):done()
				   :done():newline()
	end
	
	makeInnerTable(wikiInfobox, campName)

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

--endregion

return Campbox