Module:TestModule: Difference between revisions

From Against the Storm Official Wiki
m (Testing)
m (Testing)
 
(11 intermediate revisions by the same user not shown)
Line 1: Line 1:
--- @module BuildingLink
--- @module BuildingLink
local TestModule = {}
local BuildingLink = {}




Line 20: Line 20:


--region Private constants
--region Private constants
local VALID_SIZE = {
["none"] = true,
["small"] = true,
["medium"] = true,
["large"] = true,
["huge"] = true,
}
local VALID_PLURAL = {
["s"] = true,
["es"] = true,
}


--endregion
--endregion
Line 75: Line 88:
--- @param frame table the template's context, with arguments
--- @param frame table the template's context, with arguments
--- @return string wiki markup
--- @return string wiki markup
function TestModule.main(frame)
function BuildingLink.main(frame)
 
local frameargs = frame.args
framegetParentargs = frame:getParent().args
local ret = "frame.args = {<br>"
for k, v in pairs(frameargs) do
ret = ret .. "--[" .. k .. "] = " .. v .. ",<br>"
end
ret = ret .. "}<br>frame:getParent().args = {<br>"
for k, v in pairs(framegetParentargs) do
ret = ret .. "--[" .. k .. "] = " .. v .. ",<br>"
end
ret = ret .. "}<br>"


local name = frame.args.name
local name = frame.args.name
local iconSize = frame.args.size
local iconSize = frame.args.size
local needsPlural = frame.args.plural
local display = frame.args.display
local display = frame.args.display
local needsPlural


---- If the iconSize was not specified, but plural was "s", then these will match. We need to unset the iconSize to be default.
-- Initialize the array of unnamed parameters, up to 3, so several may be nil.
--if iconSize == needsPlural and (needsPlural == "s" or needsPlural == "es") then
local parent = frame:getParent()
-- iconSize = nil
if parent then
--end
local parameterStack = { parent.args[1], parent.args[2], parent.args[3], }
--
-- Then empty the stack and try to assign them as our variables.
---- It is valid by now; the above method already threw an error if not.
for _, nextParameter in ipairs(parameterStack) do
--validatedIcon = validateBuildingName(name)
ret = ret .. "nextParameter = " .. tostring(nextParameter) .. "<br>"
--
if nextParameter and nextParameter ~= "" then
---- The only valid value for mustBePlural is "s," so then make on check to see if name ends in a way that we need to add "es" at the end instead of just "s". For names that end in "y", we swap out the last character for "ies" instead. (This will require one-time setup for redirect pages.)
-- Prioritize setting the name first.
--if needsPlural == "s" or needsPlural == "es" then
if not name or name == "" then
-- if string.match(name, "y$") then
name = nextParameter
-- name = string.sub(name, 1, -2) .. "ies"
ret = ret .. "now name = " .. name .. "<br>"
-- needsPlural = nil -- unset, since it's incorporated into the name
else
-- else
-- These could happen in either order, because their valid values are non-overlapping
-- local needsSyllable = string.match(name, "[sxz]$") or string.match(name, "sh$") or string.match(name, "ch$")
ret = ret .. "checking VALID_SIZE[nextParameter] = " .. tostring(VALID_SIZE[nextParameter]) .. "<br>"
-- needsPlural = needsSyllable and "es" or "s"
if (not iconSize or iconSize == "") and VALID_SIZE[nextParameter] then
-- end
iconSize = nextParameter
--else
ret = ret .. "now iconSize = " .. iconSize .. "<br>"
-- needsPlural = nil
else
--end
ret = ret .. "checking VALID_PLURAL[nextParameter] = " .. tostring(VALID_PLURAL[nextParameter]) .. "<br>"
--
if not needsPlural and VALID_PLURAL[nextParameter] then
--viewParameters = {
needsPlural = nextParameter
-- ["name"] = name,
ret = ret .. "now needsPlural = " .. needsPlural .. "<br>"
-- ["plural"] = needsPlural,
end
-- ["iconfilename"] = validatedIcon,
end
-- ["iconsize"] = iconSize,
end
-- ["display"] = display,
end
--}
end
end


local frameargs = frame.args
-- Validate the name before we continue any further.
framegetParentargs = frame:getParent().args
validatedIcon = validateBuildingName(name)


local ret = "frame.args = {<br>"
-- Handle default icon size.
if not iconSize or iconSize == "" or not VALID_SIZE[iconSize] then
iconSize = "none"
end


for k, v in pairs(frameargs) do
-- The only valid value for mustBePlural is "s," so then make on check to see if name ends in a way that we need to add "es" at the end instead of just "s". For names that end in "y", we swap out the last character for "ies" instead. (This will require one-time setup for redirect pages.)
ret = ret .. "--[" .. k .. "] = " .. v .. ",<br>"
if needsPlural == "s" or needsPlural == "es" then
if string.match(name,"y$") then
name = string.sub(name,1,-2) .. "ies"
needsPlural = nil -- unset now, since it's incorporated into the name itself
else
local needsSyllable = string.match(name,"[sxz]$") or string.match(name,"sh$") or string.match(name,"ch$")
needsPlural = needsSyllable and "es" or "s"
end
else
needsPlural = nil
end
end


ret = ret .. "}<br><br>frame:getParent().args = {<br>"
viewParameters = {
["name"] = name,
["plural"] = needsPlural,
["iconfilename"] = validatedIcon,
["iconsize"] = iconSize,
["display"] = display,
}


for k, v in pairs(framegetParentargs) do
ret = ret .. "<br>Finally...<br>name = " .. tostring(name) .. "<br>"
ret = ret .. "--[" .. k .. "] = " .. v .. ",<br>"
ret = ret .. "iconSize = " .. tostring(iconSize) .. "<br>"
end
ret = ret .. "needsPlural = " .. tostring(needsPlural) .. "<br>"
 
return ret


ret = ret .. "}"
--return frame:expandTemplate{
-- title = VIEW_TEMPLATE,
-- args = viewParameters,
--}


return ret
end
end


--endregion
--endregion


return TestModule
return BuildingLink

Latest revision as of 02:08, 18 October 2024

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

--- @module BuildingLink
local BuildingLink = {}



--region Dependencies

-- This module lazily loads dependencies, since they're expensive but only one is needed, we just don't know which in advance.
local WorkshopsData
local InstitutionsData
local FarmsData
local CampsData
local CollectorsData

local VIEW_TEMPLATE = "Building_link/view"

--endregion



--region Private constants

local VALID_SIZE = {
	["none"] = true,
	["small"] = true,
	["medium"] = true,
	["large"] = true,
	["huge"] = true,
}

local VALID_PLURAL = {
	["s"] = true,
	["es"] = true,
}

--endregion



--region Private methods

local function tryData(name, buildingInterface)

	local buildingID = buildingInterface.getID(name)

	return buildingInterface.getIcon(buildingID)
end



local function validateBuildingName(name)

	-- Lazy-load of external data, since it's expensive. Start with the most likely so that most pages load as fast as possible.
	WorkshopsData = require("Module:WorkshopsData")
	local validatedIcon = tryData(name, WorkshopsData)
	if not validatedIcon then
		InstitutionsData = require("Module:InstitutionsData")
		validatedIcon = tryData(name, InstitutionsData)
		if not validatedIcon then
			FarmsData = require("Module:FarmsData")
			validatedIcon = tryData(name, FarmsData)
			if not validatedIcon then
				CollectorsData = require("Module:CollectorsData")
				validatedIcon = tryData(name, CollectorsData)
				if not validatedIcon then
					CampsData = require("Module:CampsData")
					validatedIcon = tryData(name, CampsData)
					if not validatedIcon then
						error("No building found with name: " .. name .. ". Please see the template documentation for how to use the parameters")
					end
				end
			end
		end
	end

	return validatedIcon
end

--endregion



--region Public methods

---main
--- Extracts parameters, validates the building by getting its icon, and then sends the data to the view template for rendering.
---
--- @param frame table the template's context, with arguments
--- @return string wiki markup
function BuildingLink.main(frame)

local frameargs = frame.args
framegetParentargs = frame:getParent().args
local ret = "frame.args = {<br>"
for k, v in pairs(frameargs) do
	ret = ret .. "--[" .. k .. "] = " .. v .. ",<br>"
end
ret = ret .. "}<br>frame:getParent().args = {<br>"
for k, v in pairs(framegetParentargs) do
	ret = ret .. "--[" .. k .. "] = " .. v .. ",<br>"
end
ret = ret .. "}<br>"

	local name = frame.args.name
	local iconSize = frame.args.size
	local display = frame.args.display
	local needsPlural

	-- Initialize the array of unnamed parameters, up to 3, so several may be nil.
	local parent = frame:getParent()
	if parent then
		local parameterStack = { parent.args[1], parent.args[2], parent.args[3], }
		-- Then empty the stack and try to assign them as our variables.
		for _, nextParameter in ipairs(parameterStack) do
			ret = ret .. "nextParameter = " .. tostring(nextParameter) .. "<br>"
			if nextParameter and nextParameter ~= "" then
				-- Prioritize setting the name first.
				if not name or name == "" then
					name = nextParameter
					ret = ret .. "now name = " .. name .. "<br>"
				else
					-- These could happen in either order, because their valid values are non-overlapping
					ret = ret .. "checking VALID_SIZE[nextParameter] = " .. tostring(VALID_SIZE[nextParameter]) .. "<br>"
					if (not iconSize or iconSize == "") and VALID_SIZE[nextParameter] then
						iconSize = nextParameter
						ret = ret .. "now iconSize = " .. iconSize .. "<br>"
					else
						ret = ret .. "checking VALID_PLURAL[nextParameter] = " .. tostring(VALID_PLURAL[nextParameter]) .. "<br>"
						if not needsPlural and VALID_PLURAL[nextParameter] then
							needsPlural = nextParameter
							ret = ret .. "now needsPlural = " .. needsPlural .. "<br>"
						end
					end
				end
			end
		end
	end

	-- Validate the name before we continue any further.
	validatedIcon = validateBuildingName(name)

	-- Handle default icon size.
	if not iconSize or iconSize == "" or not VALID_SIZE[iconSize] then
		iconSize = "none"
	end

	-- The only valid value for mustBePlural is "s," so then make on check to see if name ends in a way that we need to add "es" at the end instead of just "s". For names that end in "y", we swap out the last character for "ies" instead. (This will require one-time setup for redirect pages.)
	if needsPlural == "s" or needsPlural == "es" then
		if string.match(name,"y$") then
			name = string.sub(name,1,-2) .. "ies"
			needsPlural = nil -- unset now, since it's incorporated into the name itself
		else
			local needsSyllable = string.match(name,"[sxz]$") or string.match(name,"sh$") or string.match(name,"ch$")
			needsPlural = needsSyllable and "es" or "s"
		end
	else
		needsPlural = nil
	end

	viewParameters = {
		["name"] = name,
		["plural"] = needsPlural,
		["iconfilename"] = validatedIcon,
		["iconsize"] = iconSize,
		["display"] = display,
	}

	ret = ret .. "<br>Finally...<br>name = " .. tostring(name) .. "<br>"
	ret = ret .. "iconSize = " .. tostring(iconSize) .. "<br>"
	ret = ret .. "needsPlural = " .. tostring(needsPlural) .. "<br>"

	return ret

	--return frame:expandTemplate{
	--	title = VIEW_TEMPLATE,
	--	args = viewParameters,
	--}

end

--endregion

return BuildingLink