Module:BuildingList

From Against the Storm Official Wiki
Revision as of 14:48, 28 November 2023 by Aeredor (talk | contribs) (Working on a new thing...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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

---
--- Generates a large table of buildings.
---
--- @module BuildingList
local BuildingList = {}



local WorkshopsData = require("Module:WorkshopsData")
local InstitutionsData = require("Module:InstitutionsData")
local FarmsData = require("Module:FarmsData")
local CampsData = require("Module:CampsData")



--region Private constants

local DEFAULT_SIZE_SMALL = "small"

local ICON_SIZES = {
	[DEFAULT_SIZE_SMALL] = mw.getCurrentFrame():expandTemplate{ title = "ImgS" },
	["med"] = mw.getCurrentFrame():expandTemplate{ title = "ImgM" },
	["large"] = mw.getCurrentFrame():expandTemplate{ title = "ImgL" },
	["huge"] = mw.getCurrentFrame():expandTemplate{ title = "ImgH" }
}

local NBSP = " "

--endregion



--region Private methods

---
--- Loops through the data to find matches to the provided category.
---
---@param category string the name of the category
local function loadBuildingList(category)

	local buildingList = {}

	for i = 1, WorkshopsData.getNumberOfWorkshopsInDatabase() do
		local buildingName = WorkshopsData.getWorkshopNameFromDatabase(i)
		if not category or category == WorkshopsData.getWorkshopCategory(buildingName) then
			table.insert(buildingList, buildingName)
		end
	end
	for i = 1, InstitutionsData.getNumberOfInstitutionsInDatabase() do
		local buildingName = InstitutionsData.getInstitutionNameFromDatabase(i)
		if not category or category == InstitutionsData.getInstitutionCategory(buildingName) then
			table.insert(buildingList, buildingName)
		end
	end
	for i = 1, FarmsData.getNumberOfFarmsInDatabase() do
		local buildingName = FarmsData.getFarmNameFromDatabase(i)
		if not category or category == FarmsData.getFarmCategory(buildingName) then
			table.insert(buildingList, buildingName)
		end
	end
	for i = 1, CampsData.getNumberOfCampsInDatabase() do
		local buildingName = CampsData.getCampNameFromDatabase(i)
		if not category or category == CampsData.getCampCategory(buildingName) then
			table.insert(buildingList, buildingName)
		end
	end

	return buildingList
end

--endregion



--region Public methods

---
--- Renders an icon and the name of a building linked to a wiki page
--- corresponding to the provided building.
---
--- Uses MediaWiki markup to display the name of a wiki page and an icon that
--- has been uploaded to the wiki. Both must be known by the data model,
--- or the template's behavior may be different than expected.
---
--- @param frame table the template's calling context, with parameters
--- @return string wiki markup
function BuildingList.renderList(frame)

	-- Extract the template parameter, if any.
	local argCategory = frame.args.category

	local buildingList = loadBuildingList(argCategory)

	return buildingList
end

--endregion

return BuildingList