Module:Shopbox

From Against the Storm Official Wiki
Revision as of 02:12, 28 November 2023 by Aeredor (talk | contribs) (Adding quotes around the ID)

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

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



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



--region Private constants

local ARG_NAME = "name"

-- Subheading for all the buildings from this template.
local BUILDING_CATEGORY = "Production 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 workshopName 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, workshopName)

	-- Grab the data we'll use to populate this.
	local workshopID = WorkshopsData.getWorkshopID(workshopName)
	local workshopCategory = WorkshopsData.getWorkshopCategory(workshopName)
	local workshopSizeX, workshopSizeY = WorkshopsData.getWorkshopSize(workshopName)
	local workshopIsMovable = WorkshopsData.isWorkshopMovable(workshopName)
	local workshopIsEssential = WorkshopsData.isWorkshopInitiallyEssential(workshopName)
	local workshopStorageCap = WorkshopsData.getWorkshopStorage(workshopName)
	local workshopConstructionTime = WorkshopsData.getWorkshopConstructionTime(workshopName)
	local workshopWorkplaces = WorkshopsData.getWorkshopNumberOfWorkplaces(workshopName)
	local workshopRequiredGoods = WorkshopsData.getAllWorkshopRequiredGoods(workshopName)
	local workshopRecipesTable = WorkshopsData.getAllWorkshopRecipes(workshopName)

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

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

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

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

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

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

		Infobox.buildStandardRow(innerTable, Infobox.CSS_TR_BORDER_TOP,
				Infobox.TITLE_SIZE, workshopSizeX .. " × " .. workshopSizeY)
	end
	if workshopStorageCap then

		Infobox.buildStandardRow(innerTable, "",
				Infobox.TITLE_STORAGE_CAPACITY, workshopStorageCap)
	end
	if workshopWorkplaces then

		Infobox.buildStandardRow(innerTable, Infobox.CSS_TR_BORDER_BOT,
				Infobox.TITLE_WORKPLACES, workshopWorkplaces)
	end
	if workshopConstructionTime then

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

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

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

		innerTable:tag("tr"):css(Infobox.CSS_TR_BORDER_BOT)
	end
	if workshopID 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("\'" .. workshopID .. "\'"):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 Shopbox.renderShopbox(frame)
	
	-- Every workshop must have a name.
	local workshopName = frame.args[ARG_NAME]
	if not workshopName or workshopName == "" then
		return "You must specify the name of the workshop. See [[Template:Shopbox]] for examples."
	end

	-- Use this method here to check to see whether the provided name is valid.
	if not WorkshopsData.getAllDataForWorkshop(workshopName) then
		return "Shopbox can't find the specified workshop: " .. workshopName .. "."
	end

	-- Additional template parameters would go here.

	-- Get the data to display.
	local workshopDescription = WorkshopsData.getWorkshopDescription(workshopName)
	local workshopIconFilename = WorkshopsData.getWorkshopIcon(workshopName)

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

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

--endregion

return Shopbox