Module:Shopbox: Difference between revisions
From Against the Storm Official Wiki
(Recipe list now uses updated recipe template with list override) |
m (changed category icon maker) |
||
(4 intermediate revisions by the same user not shown) | |||
Line 15: | Line 15: | ||
local Infobox = require("Module:Infobox") | |||
local WorkshopsData = require("Module:WorkshopsData") | local WorkshopsData = require("Module:WorkshopsData") | ||
local | local StyleUtils = require("Module:StyleUtils") | ||
Line 22: | Line 23: | ||
--region Private constants | --region Private constants | ||
local ARGS = { | |||
["name"] = "name", | |||
local | |||
[" | |||
} | } | ||
Line 80: | Line 34: | ||
--- | --- | ||
--- | --- Shortcut method to expand a template with parameters | ||
--- | --- | ||
---@param | ---@param workshopName string the name of the workshop | ||
---@return string | ---@return string html markup returned by the template | ||
local function | local function expandRecipeList(workshopName) | ||
return | return mw.getCurrentFrame():expandTemplate{ title="Recipe", args={ | ||
building=workshopName, display="list" } } | |||
end | end | ||
Line 93: | Line 47: | ||
--- | --- | ||
--- | --- Builds using the provided wikiInfobox a few header items. | ||
--- | --- | ||
--- @param | ---@param wikiInfobox table mw.html object into which we're building this | ||
--- @return | ---@param workshopName string the name of the building the infobox is about | ||
local function | ---@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) | |||
local | |||
-- Start the header area | |||
local header = Infobox.startNewHeader(wikiInfobox) | |||
Infobox.makeTitle(header, workshopName) | |||
Infobox.makeFlavorText(header, workshopDescription) | |||
Infobox.makeTitleIcon(header, workshopIconFilename) | |||
return | return wikiInfobox | ||
end | end | ||
Line 156: | 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 | local function makeInnerContent(wikiInfobox, workshopName) | ||
-- Grab the data we'll use to populate this. | -- Grab the data we'll use to populate this. | ||
local workshopCategory = WorkshopsData.getWorkshopCategory(workshopName) | local workshopCategory = WorkshopsData.getWorkshopCategory(workshopName) | ||
local | 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 workshopRequiredGoods = WorkshopsData.getAllWorkshopRequiredGoods(workshopName) | local workshopRequiredGoods = WorkshopsData.getAllWorkshopRequiredGoods(workshopName) | ||
local | local workshopID = WorkshopsData.getWorkshopID(workshopName) | ||
-- Start the inner table | -- Start the inner table | ||
local innerTable = wikiInfobox | 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. | -- 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 | end | ||
--endregion | --endregion | ||
Line 265: | Line 146: | ||
--- | --- | ||
-- Creates an html | --- Creates an html and wiki markup to display the data in an infobox. | ||
-- | --- | ||
-- @param frame | --- @param frame table from template's calling context | ||
-- @return wiki markup | --- @return string of html and wiki markup | ||
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[ARGS.name] | ||
if not workshopName or workshopName == "" then | if not workshopName or workshopName == "" then | ||
return "You must specify the name of the | 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. | if not WorkshopsData.getWorkshopID(workshopName) then | ||
return "Shopbox can't find the specified | return "Shopbox can't find the specified building: " .. workshopName .. "." | ||
end | end | ||
-- Additional template parameters would go here. | -- Additional template parameters would go here. | ||
local wikiInfobox = Infobox.startNewInfobox() | |||
local | makeHeaderContent(wikiInfobox, workshopName) | ||
makeInnerContent(wikiInfobox, workshopName) | |||
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