Module:Shopbox: Difference between revisions
From Against the Storm Official Wiki
(Recipe list now uses updated recipe template with list override) |
(Updated to use utility module Infobox) |
||
Line 15: | Line 15: | ||
local Infobox = require("Module:Infobox") | |||
local WorkshopsData = require("Module:WorkshopsData") | local WorkshopsData = require("Module:WorkshopsData") | ||
Line 22: | Line 22: | ||
--region Private constants | --region Private constants | ||
local ARG_NAME = "name" | |||
local | |||
-- Subheading for all the buildings from this template. | -- Subheading for all the buildings from this template. | ||
local BUILDING_CATEGORY = "Production Building" | local BUILDING_CATEGORY = "Production Building" | ||
local NBSP = " " | local NBSP = " " | ||
--endregion | --endregion | ||
Line 78: | Line 34: | ||
--region Private methods | --region Private methods | ||
--- | --- | ||
Line 171: | Line 57: | ||
-- Start the inner table | -- Start the inner table | ||
local innerTable = wikiInfobox:tag("tr"):tag("td"):newline():tag("table"):css( | local innerTable = wikiInfobox:tag("tr"):tag("td"):newline():tag("table"):css(Infobox.CSS_INNER_TABLE) | ||
innerTable:newline() | innerTable:newline() | ||
Line 178: | Line 64: | ||
-- Construction toolbar category. | -- Construction toolbar category. | ||
if workshopCategory then | if workshopCategory then | ||
local categoryIconFilename = ICON_FILENAMES_CATEGORIES[workshopCategory] | |||
local categoryIconFilename = Infobox.ICON_FILENAMES_CATEGORIES[workshopCategory] | |||
if categoryIconFilename ~= nil then | if categoryIconFilename ~= nil then | ||
local categoryIconPart = "[[File:" .. categoryIconFilename .. | local categoryIconPart = "[[File:" .. categoryIconFilename .. "|" .. Infobox.ICONSIZE_MED .. "|alt=" .. workshopCategory .. "]]" | ||
innerTable | |||
Infobox.buildStandardRow(innerTable, Infobox.CSS_TR_BORDER_TOP, | |||
Infobox.TITLE_CATEGORY, categoryIconPart .. NBSP .. workshopCategory) | |||
end | end | ||
end | end | ||
-- Expand recipes into a small table | -- Expand recipes into a small table | ||
if workshopRecipesTable and #workshopRecipesTable > 0 then | if workshopRecipesTable and #workshopRecipesTable > 0 then | ||
local recipesRows = mw.getCurrentFrame():expandTemplate{ title="Recipe", args={ | local recipesRows = mw.getCurrentFrame():expandTemplate{ title="Recipe", args={ | ||
building=workshopName, display="list" } } | building=workshopName, display="list" } } | ||
innerTable | |||
Infobox.buildStandardRow(innerTable, Infobox.CSS_TR_BORDER_BOT, | |||
Infobox.TITLE_RECIPES, recipesRows) | |||
end | end | ||
-- Since false is a valid value, have to directly check if it's nil. | -- Since false is a valid value, have to directly check if it's nil. | ||
if workshopIsMovable ~= nil then | if workshopIsMovable ~= nil then | ||
innerTable | |||
Infobox.buildStandardRow(innerTable, Infobox.CSS_TR_BORDER_TOP, | |||
Infobox.TITLE_MOVABLE, workshopIsMovable and "Yes" or "No") | |||
end | end | ||
if workshopIsEssential ~= nil then | if workshopIsEssential ~= nil then | ||
innerTable | |||
Infobox.buildStandardRow(innerTable, Infobox.CSS_TR_BORDER_BOT, | |||
Infobox.TITLE_ESSENTIAL, workshopIsEssential and "Yes" or "No") | |||
end | end | ||
-- Combine sizes into one line | -- Combine sizes into one line | ||
if workshopSizeX ~= nil and workshopSizeY ~= nil then | if workshopSizeX ~= nil and workshopSizeY ~= nil then | ||
innerTable | |||
Infobox.buildStandardRow(innerTable, Infobox.CSS_TR_BORDER_TOP, | |||
Infobox.TITLE_SIZE, workshopSizeX .. " × " .. workshopSizeY) | |||
end | end | ||
if workshopStorageCap then | if workshopStorageCap then | ||
innerTable | |||
Infobox.buildStandardRow(innerTable, nil, | |||
Infobox.TITLE_STORAGE_CAPACITY, workshopStorageCap) | |||
end | end | ||
if workshopWorkplaces then | if workshopWorkplaces then | ||
innerTable | |||
Infobox.buildStandardRow(innerTable, Infobox.CSS_TR_BORDER_BOT, | |||
Infobox.TITLE_WORKPLACES, workshopWorkplaces) | |||
end | end | ||
if workshopConstructionTime then | if workshopConstructionTime then | ||
innerTable | |||
Infobox.buildStandardRow(innerTable, Infobox.CSS_TR_BORDER_TOP, | |||
Infobox.TITLE_CONSTRUCTION_TIME, workshopConstructionTime) | |||
end | end | ||
-- Need to build a separate table to display building materials | -- Need to build a separate table to display building materials | ||
if workshopRequiredGoods then | if workshopRequiredGoods then | ||
local requiredGoodsRows = tostring( writeRequiredGoodsRows(workshopRequiredGoods) ) | |||
innerTable | 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 | end | ||
if workshopID then | if workshopID then | ||
innerTable:tag("tr"):css( | innerTable:tag("tr"):css(Infobox.CSS_TR_BORDER_TOP):css(Infobox.CSS_TR_BORDER_BOT) | ||
:tag("th"):wikitext(TITLE_ID):done() | :tag("th"):wikitext(Infobox.TITLE_ID):done() | ||
:tag("td"):wikitext(workshopID):done() | :tag("td"):wikitext(workshopID):done() | ||
:done():newline() | :done():newline() | ||
Line 269: | Line 149: | ||
-- @param frame the template's calling context | -- @param frame the template's calling context | ||
-- @return wiki markup for the box | -- @return wiki markup for the box | ||
function Shopbox. | function Shopbox.renderShopbox(frame) | ||
-- Every workshop must have a name. | -- Every workshop must have a name. | ||
local workshopName = frame.args[ | local workshopName = frame.args[ARG_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 workshop. See [[Template:Shopbox]] for examples." | ||
Line 289: | Line 169: | ||
-- Make the top of the infobox that every workshop has... | -- Make the top of the infobox that every workshop has... | ||
local wikiInfobox = mw.html.create("table"):css( | local wikiInfobox = mw.html.create("table"):css(Infobox.CSS_MAIN):newline() | ||
-- with a title... | -- with a title... | ||
wikiInfobox:tag("tr") | wikiInfobox:tag("tr") | ||
Line 303: | Line 183: | ||
:tag("td"):wikitext("[[File:" .. workshopIconFilename .. "]]"):done() | :tag("td"):wikitext("[[File:" .. workshopIconFilename .. "]]"):done() | ||
:done():newline() | :done():newline() | ||
wikiInfobox:tag("tr"):css( | wikiInfobox:tag("tr"):css(Infobox.CSS_TR_BORDER_BOT) | ||
:tag("td"):wikitext(workshopName .. ", as seen in-game"):done() | :tag("td"):wikitext(workshopName .. ", as seen in-game"):done() | ||
:done():newline() | :done():newline() | ||
Line 312: | Line 192: | ||
-- Finish with the flavor text. | -- Finish with the flavor text. | ||
if workshopDescription then | if workshopDescription then | ||
wikiInfobox:tag("tr"):css( | wikiInfobox:tag("tr"):css(Infobox.CSS_TR_BORDER_TOP) | ||
:tag("td"):wikitext(workshopDescription):done() | :tag("td"):wikitext(workshopDescription):done() | ||
:done():newline() | :done():newline() |
Revision as of 01:04, 28 November 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") --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, nil, 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