Module:Goodbox: Difference between revisions

From Against the Storm Official Wiki
m (replacing some empty styling with empty strings instead of nil)
(Next version; css-based styling instead of inline, new layout methods with divs instead of using tables for layout)
Line 16: Line 16:
local Infobox = require("Module:Infobox")
local Infobox = require("Module:Infobox")
local GoodsData = require("Module:GoodsData")
local GoodsData = require("Module:GoodsData")
local StyleUtils = require("Module:StyleUtils")




Line 21: Line 22:
--region  Private constants
--region  Private constants


local ARG_NAME = "name"
local ARGS = {
local ARG_PURPOSE = "purpose"
["name"] = "name",
local ARG_SPECIES_PREFERENCE = "species_preference"
["purpose"] = "purpose",
 
["species_preference"] = "species_preference",
local NBSP = " "
}


--endregion
--endregion
Line 32: Line 33:


--region Private methods
--region Private methods
---
--- Builds using the provided wikiInfobox a few header items.
---
---@param wikiInfobox table mw.html object into which we're building this
---@param goodName 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 makeHeaderContent(wikiInfobox, goodName)
-- Grab the data we'll use to populate this.
local goodDescription = GoodsData.getGoodDescription(goodName)
local goodIconFilename = GoodsData.getGoodIcon(goodName)
-- Start the header area
local header = Infobox.startNewHeader(wikiInfobox)
Infobox.makeTitle(header, goodName)
Infobox.makeFlavorText(header, goodDescription)
Infobox.makeTitleIcon(header, goodIconFilename)
return wikiInfobox
end


---
---
Line 39: Line 64:
---@param wikiInfobox table mw.html object into which we're building this
---@param wikiInfobox table mw.html object into which we're building this
---@param goodName string the name of the good the infobox is about
---@param goodName string the name of the good the infobox is about
---@param purpose string the parameter provided to the template
---@param params table of strings, the parameter provided to the template
---@param speciesPref string the parameter provided to the template
---@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, goodName, purpose, speciesPref)
local function makeInnerContent(wikiInfobox, goodName, params)


-- Grab the data we'll use to populate this.
-- Grab the data we'll use to populate this.
local goodID = GoodsData.getGoodID(goodName)
local goodCategory = GoodsData.getGoodCategory(goodName)
local goodCategory = GoodsData.getGoodCategory(goodName)
local goodIsEatable = GoodsData.isGoodEatable(goodName)
local goodIsEatable = GoodsData.isGoodEatable(goodName)
Line 51: Line 74:
local goodBurnTime = GoodsData.getGoodBurnTime(goodName)
local goodBurnTime = GoodsData.getGoodBurnTime(goodName)
local goodSellValue, goodBuyValue = GoodsData.getGoodValue(goodName)
local goodSellValue, goodBuyValue = GoodsData.getGoodValue(goodName)
local goodID = GoodsData.getGoodID(goodName)


-- 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()


-- Start with the things not provided by the data.
-- we'll reuse this to mark where separators are needed in the table
if purpose and purpose ~= "" then
local needsSeparator = false


Infobox.buildStandardRow(innerTable, "",
-- Start with the things from the parameters, if any
Infobox.toTitleCase(ARG_PURPOSE), purpose)
for paramTitle, paramValue in pairs(params or {}) do
needsSeparator = Infobox.makeInnerRow(innerTable, paramValue, needsSeparator,
Infobox.toTitleCase(paramTitle), paramValue)
end
end
if speciesPref and speciesPref ~= "" then


Infobox.buildStandardRow(innerTable, Infobox.CSS_TR_BORDER_BOT,
needsSeparator = true
Infobox.toTitleCase(ARG_SPECIES_PREFERENCE), speciesPref)
end


-- Storage category (this is the same as the subtitle for now)
needsSeparator = Infobox.makeInnerRow(innerTable, goodCategory, needsSeparator,
if goodCategory then
Infobox.TITLE_CATEGORY,
Infobox.makeCategoryIcon(goodCategory) .. StyleUtils.NBSP .. goodCategory)
needsSeparator = Infobox.makeInnerRow(innerTable, goodIsEatable, needsSeparator,
Infobox.TITLE_EATABLE, goodIsEatable and "Yes" or "No")
needsSeparator = Infobox.makeInnerRow(innerTable, goodIsBurnable, needsSeparator,
Infobox.TITLE_BURNABLE, goodIsBurnable and "Yes" or "No")
needsSeparator = Infobox.makeInnerRow(innerTable, goodIsBurnable and goodBurnTime,
needsSeparator, Infobox.TITLE_BURN_TIME, goodBurnTime)


local categoryIcon = "[[File:" .. Infobox.ICON_FILENAMES_CATEGORIES[goodCategory] .. "|" .. Infobox.ICONSIZE_MED .. "|alt=" .. goodCategory .. "]]"
needsSeparator = true


Infobox.buildStandardRow(innerTable, Infobox.CSS_TR_BORDER_TOP,
needsSeparator = Infobox.makeInnerRow(innerTable, goodSellValue, needsSeparator,
Infobox.TITLE_CATEGORY, categoryIcon .. NBSP .. goodCategory)
Infobox.TITLE_SELL_VALUE, Infobox.toTwoDecimalPlaces(goodSellValue))
end
needsSeparator = Infobox.makeInnerRow(innerTable, goodBuyValue, needsSeparator,
-- Since false is a valid value, have to directly check if it's nil.
Infobox.TITLE_BUY_VALUE, Infobox.toTwoDecimalPlaces(goodBuyValue))
if goodIsEatable ~= nil then
 
Infobox.buildStandardRow(innerTable, "",
Infobox.TITLE_EATABLE, goodIsEatable and "Yes" or "No")
end
if goodIsBurnable ~= nil then
 
Infobox.buildStandardRow(innerTable, "",
Infobox.TITLE_BURNABLE, goodIsBurnable and "Yes" or "No")
end
if goodIsBurnable and goodBurnTime then


Infobox.buildStandardRow(innerTable, Infobox.CSS_TR_BORDER_BOT,
needsSeparator = true
Infobox.TITLE_BURN_TIME, goodBurnTime)
end
-- Trade values.
if goodSellValue then


Infobox.buildStandardRow(innerTable, Infobox.CSS_TR_BORDER_TOP,
needsSeparator = Infobox.makeInnerRow(innerTable, goodID, needsSeparator,
Infobox.TITLE_SELL_VALUE, Infobox.toTwoDecimalPlaces(goodSellValue))
Infobox.TITLE_ID, StyleUtils.ITALIC .. goodID .. StyleUtils.ITALIC)
end
if goodBuyValue then


Infobox.buildStandardRow(innerTable, Infobox.CSS_TR_BORDER_BOT,
-- Close the inner table, and return it with the passed context
Infobox.TITLE_BUY_VALUE, Infobox.toTwoDecimalPlaces(goodBuyValue))
Infobox.endInnerTable(wikiInfobox, innerTable)
end
-- End with the ID.
if goodID then


Infobox.buildStandardRow(innerTable, Infobox.CSS_TR_BORDER_TOP,
return wikiInfobox
Infobox.TITLE_ID, "\'" .. goodID .. "\'")
end
 
-- Close the table.
innerTable:done():newline()
-- Close the table cell and row the inner table is in.
wikiInfobox:done():done():newline()
end
end


Line 130: Line 132:


-- Every good must have a name.
-- Every good must have a name.
local goodName = frame.args[ARG_NAME]
local goodName = frame.args[ARGS.name]
if not goodName or goodName == "" then
if not goodName or goodName == "" then
return "You must specify the name of the good. See [[Template:Goodbox]] for examples."
return "You must specify the name of the good. See [[Template:Goodbox]] for examples."
Line 141: Line 143:


-- Additional template parameters, may or may not be present.
-- Additional template parameters, may or may not be present.
local purpose = frame.args[ARG_PURPOSE]
local purpose = frame.args[ARGS.purpose]
local speciesPref = frame.args[ARG_SPECIES_PREFERENCE]
local speciesPref = frame.args[ARGS.species_preference]


-- Get the data.
local wikiInfobox = Infobox.startNewInfobox()
local goodDescription = GoodsData.getGoodDescription(goodName)
makeHeaderContent(wikiInfobox, goodName)
local goodCategory = GoodsData.getGoodCategory(goodName)
makeInnerContent(wikiInfobox, goodName,
local goodIconFilename = GoodsData.getGoodIcon(goodName)
{ [ARGS.purpose] = purpose, [ARGS.species_preference] = speciesPref })
 
-- Make the top of the infobox that every item has...
local wikiInfobox = mw.html.create("table"):css(Infobox.CSS_MAIN):newline()
-- with a title...
wikiInfobox:tag("tr")
:tag("th"):wikitext(goodName):done()
:done():newline()
-- and a subtitle, showing the category...
if goodCategory then
wikiInfobox:tag("tr")
:tag("td"):wikitext(goodCategory):done()
:done():newline()
end
-- and a large icon.
if goodIconFilename then
wikiInfobox:tag("tr")
:tag("td"):wikitext("[[File:" .. goodIconFilename .. "]]"):done()
:done():newline()
wikiInfobox:tag("tr"):css(Infobox.CSS_TR_BORDER_BOT)
:tag("td"):wikitext(goodName .. ", as seen in-game"):done()
:done():newline()
end
 
makeInnerTable(wikiInfobox, goodName, purpose, speciesPref)
 
-- Finish with the flavor text.
if goodDescription then
wikiInfobox:tag("tr"):css(Infobox.CSS_TR_BORDER_TOP)
:tag("td"):wikitext(goodDescription):done()
:done():newline()
end


return wikiInfobox
return wikiInfobox

Revision as of 23:02, 22 December 2023

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

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



local Infobox = require("Module:Infobox")
local GoodsData = require("Module:GoodsData")
local StyleUtils = require("Module:StyleUtils")



--region  Private constants

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

--endregion



--region Private methods

---
--- Builds using the provided wikiInfobox a few header items.
---
---@param wikiInfobox table mw.html object into which we're building this
---@param goodName 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 makeHeaderContent(wikiInfobox, goodName)

	-- Grab the data we'll use to populate this.
	local goodDescription = GoodsData.getGoodDescription(goodName)
	local goodIconFilename = GoodsData.getGoodIcon(goodName)

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

	Infobox.makeTitle(header, goodName)
	Infobox.makeFlavorText(header, goodDescription)
	Infobox.makeTitleIcon(header, goodIconFilename)

	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 goodName string the name of the good the infobox is about
---@param params table of strings, the parameter provided to the template
---@return table the Mediawiki html object into which we've been adding things
 local function makeInnerContent(wikiInfobox, goodName, params)

	-- Grab the data we'll use to populate this.
	local goodCategory = GoodsData.getGoodCategory(goodName)
	local goodIsEatable = GoodsData.isGoodEatable(goodName)
	local goodIsBurnable = GoodsData.isGoodBurnable(goodName)
	local goodBurnTime = GoodsData.getGoodBurnTime(goodName)
	local goodSellValue, goodBuyValue = GoodsData.getGoodValue(goodName)
	local goodID = GoodsData.getGoodID(goodName)

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

	-- Start with the things from the parameters, if any
	for paramTitle, paramValue in pairs(params or {}) do
		needsSeparator = Infobox.makeInnerRow(innerTable, paramValue, needsSeparator,
				Infobox.toTitleCase(paramTitle), paramValue)
	end

	needsSeparator = true

	needsSeparator = Infobox.makeInnerRow(innerTable, goodCategory, needsSeparator,
			Infobox.TITLE_CATEGORY,
			Infobox.makeCategoryIcon(goodCategory) .. StyleUtils.NBSP .. goodCategory)
	needsSeparator = Infobox.makeInnerRow(innerTable, goodIsEatable, needsSeparator,
			Infobox.TITLE_EATABLE, goodIsEatable and "Yes" or "No")
	needsSeparator = Infobox.makeInnerRow(innerTable, goodIsBurnable, needsSeparator,
			Infobox.TITLE_BURNABLE, goodIsBurnable and "Yes" or "No")
	needsSeparator = Infobox.makeInnerRow(innerTable, goodIsBurnable and goodBurnTime,
			needsSeparator, Infobox.TITLE_BURN_TIME, goodBurnTime)

	needsSeparator = true

	needsSeparator = Infobox.makeInnerRow(innerTable, goodSellValue, needsSeparator,
			Infobox.TITLE_SELL_VALUE, Infobox.toTwoDecimalPlaces(goodSellValue))
	needsSeparator = Infobox.makeInnerRow(innerTable, goodBuyValue, needsSeparator,
			Infobox.TITLE_BUY_VALUE, Infobox.toTwoDecimalPlaces(goodBuyValue))

	needsSeparator = true

	needsSeparator = Infobox.makeInnerRow(innerTable, goodID, needsSeparator,
			Infobox.TITLE_ID, StyleUtils.ITALIC .. goodID .. 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 Goodbox.renderGoodbox(frame)

	-- Every good must have a name.
	local goodName = frame.args[ARGS.name]
	if not goodName or goodName == "" then
		return "You must specify the name of the good. See [[Template:Goodbox]] for examples."
	end

	-- Use this method here to check to see whether the provided name is valid.
	if not GoodsData.getGoodID(goodName) then
		return "Goodbox can't find the specified good: " .. goodName .. "."
	end

	-- Additional template parameters, may or may not be present.
	local purpose = frame.args[ARGS.purpose]
	local speciesPref = frame.args[ARGS.species_preference]

	local wikiInfobox = Infobox.startNewInfobox()
	makeHeaderContent(wikiInfobox, goodName)
	makeInnerContent(wikiInfobox, goodName,
			{ [ARGS.purpose] = purpose, [ARGS.species_preference] = speciesPref })

	return wikiInfobox
end

--endregion

return Goodbox