Module:Shopbox

From Against the Storm Official Wiki
Revision as of 05:40, 24 December 2023 by Aeredor (talk | contribs) (Modified the layout to be ready for CSS classes; now not different from Service buildings, will converge.)

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")
local StyleUtils = require("Module:StyleUtils")



--region Private constants

local ARGS = {
	["name"] = "name",
}

--endregion



--region Private methods

---
--- Shortcut method to expand a template with parameters
---
---@param workshopName string the name of the workshop
---@return string html markup returned by the template
local function expandRecipeList(workshopName)

	return mw.getCurrentFrame():expandTemplate{ title="Recipe", args={
		building=workshopName, display="list"	} }
end



---
--- Builds using the provided wikiInfobox a few header items.
---
---@param wikiInfobox table mw.html object into which we're building this
---@param workshopName string the name of the building the infobox is about
---@return table the Mediawiki html object into which we've been adding things
local function makeHeaderContent(wikiInfobox, workshopName)

	-- Grab the data we'll use to populate this.
	local workshopDescription = WorkshopsData.getWorkshopDescription(workshopName)
	local workshopIconFilename = WorkshopsData.getWorkshopIcon(workshopName)

	-- Start the header area
	local header = Infobox.startNewHeader(wikiInfobox)

	Infobox.makeTitle(header, workshopName)
	Infobox.makeFlavorText(header, workshopDescription)
	Infobox.makeTitleIcon(header, workshopIconFilename)

	return wikiInfobox
end



---
--- 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 makeInnerContent(wikiInfobox, workshopName)

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

	-- Start the inner table
	local innerTable = Infobox.startNewInnerTable(wikiInfobox)

	-- we'll reuse this to mark where separators are needed in the table
	local needsSeparator = false

	-- Additional parameters would go here.

	needsSeparator = Infobox.makeInnerRow(innerTable, workshopCategory, needsSeparator,
			Infobox.TITLE_CATEGORY,
			Infobox.makeCategoryIcon(workshopCategory) .. StyleUtils.NBSP .. workshopCategory)
	needsSeparator = Infobox.makeInnerRow(innerTable, workshopRecipesTable and #workshopRecipesTable > 0,
			needsSeparator, Infobox.TITLE_RECIPES,
			expandRecipeList(workshopName))

	needsSeparator = true

	needsSeparator = Infobox.makeInnerRow(innerTable, workshopIsMovable, needsSeparator,
			Infobox.TITLE_MOVABLE, workshopIsMovable and "Yes" or "No")
	needsSeparator = Infobox.makeInnerRow(innerTable, workshopIsEssential, needsSeparator,
			Infobox.TITLE_ESSENTIAL, workshopIsEssential and "Yes" or "No")
	needsSeparator = Infobox.makeInnerRow(innerTable, workshopSizeX and workshopSizeY,
			needsSeparator, Infobox.TITLE_SIZE, workshopSizeX .. " × " .. workshopSizeY)
	needsSeparator = Infobox.makeInnerRow(innerTable, workshopStorageCap, needsSeparator,
			Infobox.TITLE_STORAGE_CAPACITY, workshopStorageCap)
	needsSeparator = Infobox.makeInnerRow(innerTable, workshopWorkplaces, needsSeparator,
			Infobox.TITLE_WORKPLACES, workshopWorkplaces)

	needsSeparator = true

	needsSeparator = Infobox.makeInnerRow(innerTable, workshopConstructionTime,
			needsSeparator, Infobox.TITLE_CONSTRUCTION_TIME,
			workshopConstructionTime)
	needsSeparator = Infobox.makeInnerRow(innerTable, workshopRequiredGoods, needsSeparator,
			Infobox.TITLE_CONSTRUCTION_COST,
			Infobox.writeRequiredGoodsRows(workshopRequiredGoods) )

	needsSeparator = true

	needsSeparator = Infobox.makeInnerRow(innerTable, workshopID, needsSeparator,
			Infobox.TITLE_ID, StyleUtils.ITALIC .. workshopID .. StyleUtils.ITALIC)

	-- Close the inner table, and return it with the passed context
	Infobox.endInnerTable(wikiInfobox, innerTable)

	return wikiInfobox
end

--endregion



--region Public methods

---
--- Creates an html and wiki markup to display the data in an infobox.
---
--- @param frame table from template's calling context
--- @return string of html and wiki markup
function Shopbox.renderShopbox(frame)
	
	-- Every workshop must have a name.
	local workshopName = frame.args[ARGS.name]
	if not workshopName or workshopName == "" then
		return "You must specify the name of the building. See [[Template:Shopbox]] for examples."
	end

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

	-- Additional template parameters would go here.

	local wikiInfobox = Infobox.startNewInfobox()
	makeHeaderContent(wikiInfobox, workshopName)
	makeInnerContent(wikiInfobox, workshopName)
	
	return wikiInfobox
end

--endregion

return Shopbox