Module:Shopbox: Difference between revisions

From Against the Storm Official Wiki
m (replacing some empty styling with empty strings instead of nil)
m (changed category icon maker)
 
(2 intermediate revisions by the same user not shown)
Line 17: Line 17:
local Infobox = require("Module:Infobox")
local Infobox = require("Module:Infobox")
local WorkshopsData = require("Module:WorkshopsData")
local WorkshopsData = require("Module:WorkshopsData")
local StyleUtils = require("Module:StyleUtils")




Line 22: Line 23:
--region Private constants
--region Private constants


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


-- Subheading for all the buildings from this template.
--endregion
local BUILDING_CATEGORY = "Production Building"
 
 
 
--region Private methods


local NBSP = " "
---
--- 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)


--endregion
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




--region Private methods


---
---
Line 42: Line 77:
---@param workshopName string the name of the good the infobox is about
---@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
---@return table the Mediawiki html object into which we've been adding things
local function makeInnerTable(wikiInfobox, workshopName)
local function makeInnerContent(wikiInfobox, workshopName)


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


-- Start the inner table
-- Start the inner table
local innerTable = wikiInfobox:tag("tr"):tag("td"):newline():tag("table"):css(Infobox.CSS_INNER_TABLE)
local innerTable = Infobox.startNewInnerTable(wikiInfobox)
innerTable:newline()
 
-- we'll reuse this to mark where separators are needed in the table
local needsSeparator = false


-- Additional parameters would go here.
-- Additional parameters would go here.


-- Construction toolbar category.
needsSeparator = Infobox.makeInnerRow(innerTable, workshopCategory, needsSeparator,
if workshopCategory then
Infobox.TITLE_CATEGORY,
Infobox.makeCategoryIcon(workshopCategory, false) .. StyleUtils.NBSP .. workshopCategory)
needsSeparator = Infobox.makeInnerRow(innerTable, workshopRecipesTable and #workshopRecipesTable > 0,
needsSeparator, Infobox.TITLE_RECIPES,
expandRecipeList(workshopName))


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


Infobox.buildStandardRow(innerTable, Infobox.CSS_TR_BORDER_TOP,
needsSeparator = Infobox.makeInnerRow(innerTable, workshopIsMovable, needsSeparator,
Infobox.TITLE_CATEGORY, categoryIconPart .. NBSP .. workshopCategory)
Infobox.TITLE_MOVABLE, workshopIsMovable and "Yes" or "No")
end
needsSeparator = Infobox.makeInnerRow(innerTable, workshopIsEssential, needsSeparator,
end
Infobox.TITLE_ESSENTIAL, workshopIsEssential and "Yes" or "No")
-- Expand recipes into a small table
needsSeparator = Infobox.makeInnerRow(innerTable, workshopSizeX and workshopSizeY,
if workshopRecipesTable and #workshopRecipesTable > 0 then
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)


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


Infobox.buildStandardRow(innerTable, Infobox.CSS_TR_BORDER_BOT,
needsSeparator = Infobox.makeInnerRow(innerTable, workshopConstructionTime,
Infobox.TITLE_RECIPES, recipesRows)
needsSeparator, Infobox.TITLE_CONSTRUCTION_TIME,
end
workshopConstructionTime)
-- Since false is a valid value, have to directly check if it's nil.
needsSeparator = Infobox.makeInnerRow(innerTable, workshopRequiredGoods, needsSeparator,
if workshopIsMovable ~= nil then
Infobox.TITLE_CONSTRUCTION_COST,
Infobox.writeRequiredGoodsRows(workshopRequiredGoods) )


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


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


Infobox.buildStandardRow(innerTable, Infobox.CSS_TR_BORDER_TOP,
-- Close the inner table, and return it with the passed context
Infobox.TITLE_SIZE, workshopSizeX .. " × " .. workshopSizeY)
Infobox.endInnerTable(wikiInfobox, innerTable)
end
if workshopStorageCap then
 
Infobox.buildStandardRow(innerTable, "",
Infobox.TITLE_STORAGE_CAPACITY, workshopStorageCap)
end
if workshopWorkplaces then


Infobox.buildStandardRow(innerTable, Infobox.CSS_TR_BORDER_BOT,
return wikiInfobox
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
end


--endregion
--endregion
Line 145: Line 146:


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


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


-- Additional template parameters would go here.
-- Additional template parameters would go here.


-- Get the data to display.
local wikiInfobox = Infobox.startNewInfobox()
local workshopDescription = WorkshopsData.getWorkshopDescription(workshopName)
makeHeaderContent(wikiInfobox, workshopName)
local workshopIconFilename = WorkshopsData.getWorkshopIcon(workshopName)
makeInnerContent(wikiInfobox, 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
return wikiInfobox

Latest revision as of 05:46, 24 December 2023

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, false) .. 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