Module:PerkLink: Difference between revisions

From Against the Storm Official Wiki
(created based on ResourceLink)
 
(Updated to use the real PerksData)
Line 1: Line 1:
-------------------------------------------------------------------------------
local PerkLink = {}
-- Renders the {{Perk_link}} template
 
--
 
-- Takes an argument, the name of the perk. Optionally, accepts a second
 
-- argument, "med" or "large" to render the icon as larger than the standard
--region Dependencies
-- size of text, {{ImgS}}
-------------------------------------------------------------------------------


local PerkLink = {}
local PerksData = require("Module:PerksData")
local StyleUtils = require("Module:StyleUtils")
 
--endregion


local PerkData = require("Module:PerkData") -- lookup table for perks






-------------------------------------------------------------------------------
--region Private member variables
-- Constants
-------------------------------------------------------------------------------
local TEMPLATE_IMGSMALL = "ImgS"
local TEMPLATE_IMGMED = "ImgM"
local TEMPLATE_IMGLARGE = "ImgL"
local TEMPLATE_IMGHUGE = "ImgH"


local REPLACEMENT_FILENAME = "Question_mark.png"
local REPLACEMENT_FILENAME = "Question_mark.png"


--endregion




-------------------------------------------------------------------------------
 
-- Main rendering function
--region Private methods
-- uses PerkData lookup function, parses the result and handles errors
 
-- with default values, then assembles a final string to return to the wiki
local function resolveIconSize(iconSizeRequested)
-------------------------------------------------------------------------------
 
function PerkLink.renderLink(frame)
-- default for perk links is small
local argPerkName = frame.args.perk
local size = StyleUtils.IMG_S()
local argPerkIconSize = frame.args.iconsize
 
if iconSizeRequested == "med" then
if not argPerkName then
size = StyleUtils.IMG_M()
return "Perk_Link Error: no perk given"
else
if iconSizeRequested == "large" then
size = StyleUtils.IMG_L()
else
if iconSizeRequested == "huge" then
size = StyleUtils.IMG_H()
end
end
end
end
 
 
-- get the data about the perk and then adopt the provided name
return size
-- in case we need to look it up again
end
local tableData = PerkData.getData(argPerkName)
 
if not tableData then
 
return "Resource_Link Error: " .. argPerkName .." not found"
 
local function makeIconPart(perkID, pageName, iconSize)
 
if not iconSize or iconSize == "" or iconSize == "none" then
return ""
end
end
local strPageName = PerkData.getPagenameFromData(tableData)
 
-- if looking up the provided perk name returned no page, then render an error
local filename = PerksData.getIconByID(perkID) or REPLACEMENT_FILENAME
if not strPageName then
local size = resolveIconSize(iconSize)
return "Resource_Link Error: " .. argPerkName .." not found"
 
return string.format("[[File:%s|%s|link=%s|alt=%s|%s]]", filename, size, pageName, pageName, pageName)
end
 
 
 
local function renderWithID(perkID, iconSize)
 
local pageName = PerksData.getNameByID(perkID)
local iconPart = makeIconPart(perkID, pageName, iconSize)
 
local pagePart = "[[" .. pageName .. "]]"
 
return iconPart .. " " .. pagePart
end
 
 
 
local function renderWithName(name, iconSize)
 
local listOfIDs = PerksData.getAllPerkIDsByName(name)
local perkID = listOfIDs[1]
 
return renderWithID(perkID, iconSize)
end
 
--endregion
 
 
 
--region Public methods
 
--- Main rendering method invoked from the template.
---
---@param frame table the MediaWiki calling context
---@return string wiki markup with the results
function PerkLink.renderLink(frame)
 
-- Extract the template parameters.
local perkID = frame.args.id or frame.args[1]
local name = frame.args.name
local iconSize = frame.args.iconsize
 
if perkID and perkID ~= "" then
return renderWithID(perkID, iconSize)
else
if name and name ~= "" then
return renderWithName(name, iconSize)
else
return "The Perk_link template requires the ID or name of a perk."
end
end
end
 
-- if the icon filename didn't exist, then show a default but subtle question mark icon instead
-- this will help people with troubleshooting, instead of just showing no icon then editors
-- wonder what went wrong
local strIconFilename = PerkData.getIconFilenameFromData(tableData) or REPLACEMENT_FILENAME
-- use the established templates for image sizes, with the default being
-- small, or in-line size, for perks
local switchIconSize = {
["med"] = frame:expandTemplate{title=TEMPLATE_IMGMED},
["large"] = frame:expandTemplate{title=TEMPLATE_IMGLARGE},
["huge"] = frame:expandTemplate{title=TEMPLATE_IMGHUGE}
}
-- if it's not in the switch, then use the default, small size
local strIconSize = switchIconSize[argPerkIconSize] or frame:expandTemplate{title=TEMPLATE_IMGSMALL}
-- combine the string parts to return to the page
local strFilePart = string.format("[[File:%s|%s|link=%s|alt=%s|%s]]", strIconFilename, strIconSize, strPageName, strPageName, strPageName)
-- combine the file part with the link part
return strFilePart .. " [[" .. strPageName .. "]]"
end
end


--endregion


-------------------------------------------------------------------------------
-- Return when required into another Module.
-------------------------------------------------------------------------------
return PerkLink
return PerkLink

Revision as of 23:06, 28 March 2024

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

local PerkLink = {}



--region Dependencies

local PerksData = require("Module:PerksData")
local StyleUtils = require("Module:StyleUtils")

--endregion




--region Private member variables

local REPLACEMENT_FILENAME = "Question_mark.png"

--endregion



--region Private methods

local function resolveIconSize(iconSizeRequested)

	-- default for perk links is small
	local size = StyleUtils.IMG_S()

	if iconSizeRequested == "med" then
		size = StyleUtils.IMG_M()
	else
		if iconSizeRequested == "large" then
			size = StyleUtils.IMG_L()
		else
			if iconSizeRequested == "huge" then
				size = StyleUtils.IMG_H()
			end
		end
	end

	return size
end



local function makeIconPart(perkID, pageName, iconSize)

	if not iconSize or iconSize == "" or iconSize == "none" then
		return ""
	end

	local filename = PerksData.getIconByID(perkID) or REPLACEMENT_FILENAME
	local size = resolveIconSize(iconSize)

	return string.format("[[File:%s|%s|link=%s|alt=%s|%s]]", filename, size, pageName, pageName, pageName)
end



local function renderWithID(perkID, iconSize)

	local pageName = PerksData.getNameByID(perkID)
	local iconPart = makeIconPart(perkID, pageName, iconSize)

	local pagePart = "[[" .. pageName .. "]]"

	return iconPart .. " " .. pagePart
end



local function renderWithName(name, iconSize)

	local listOfIDs = PerksData.getAllPerkIDsByName(name)
	local perkID = listOfIDs[1]

	return renderWithID(perkID, iconSize)
end

--endregion



--region Public methods

--- Main rendering method invoked from the template.
---
---@param frame table the MediaWiki calling context
---@return string wiki markup with the results
function PerkLink.renderLink(frame)

	-- Extract the template parameters.
	local perkID = frame.args.id or frame.args[1]
	local name = frame.args.name
	local iconSize = frame.args.iconsize

	if perkID and perkID ~= "" then
		return renderWithID(perkID, iconSize)
	else
		if name and name ~= "" then
			return renderWithName(name, iconSize)
		else
			return "The Perk_link template requires the ID or name of a perk."
		end
	end

end

--endregion

return PerkLink