Module:SpeciesLink: Difference between revisions
From Against the Storm Official Wiki
m (Clear Frog icon filename since none is uploaded yet) |
(Rewrote to be more lightweight, externalize the view, and to include frogs) |
||
Line 1: | Line 1: | ||
- | --- @module SpeciesLink | ||
-- @module SpeciesLink | |||
local SpeciesLink = {} | local SpeciesLink = {} | ||
-- Dependencies | |||
--region Dependencies | |||
local SpeciesData = | |||
local SpeciesData = require("Module:SpeciesData") | |||
local VIEW_TEMPLATE = "Species_link/view" | |||
--endregion | |||
--region Private constants | |||
local VALID_SIZES = { | |||
["none"] = true, | |||
[" | ["small"] = true, | ||
["medium"] = true, | |||
["large"] = true, | |||
["huge"] = true, | |||
[" | |||
[" | |||
} | } | ||
-- | --endregion | ||
-- | --region Private methods | ||
local function validateSpeciesName(name) | |||
-- Try a quick match with the keys first. | |||
local speciesFound = SpeciesData.species[name] | |||
-- | if speciesFound then | ||
return speciesFound | |||
else | |||
-- Look into the data to see if the name matches any of the names in the data. | |||
for _, species in pairs(SpeciesData.species) do | |||
if name == species[SpeciesData.NAME] then | |||
return species | |||
end | |||
end | |||
end | |||
error("You specified an invalid species name. Please see the template documentation for how to use the parameters.") | |||
end | end | ||
-- return | --endregion | ||
--region Public methods | |||
function SpeciesLink.main(frame) | |||
local name = frame.args.species or frame.args[1] | |||
local iconSize = frame.args.iconsize or frame.args[2] | |||
-- Check that something at all was provided for the name before we can validate it. | |||
if not name or name == "" then | |||
error("You must specify the name of the species. Please see the template documentation for how to use the parameters.") | |||
end | |||
local validatedSpecies = validateSpeciesName(name) | |||
local speciesName = validatedSpecies[SpeciesData.NAME] | |||
local iconFilename = validatedSpecies[SpeciesData.ICON_FILENAME] .. ".png" | |||
-- Defer to the view to handle the iconSize. We don't need to validate it. | |||
local parameters = { | |||
["name"] = speciesName, | |||
["iconfilename"] = iconFilename, | |||
["size"] = iconSize, | |||
} | |||
return frame:expandTemplate{ | |||
title = VIEW_TEMPLATE, | |||
args = parameters, | |||
} | |||
end | |||
--endregion | |||
return SpeciesLink | return SpeciesLink |
Revision as of 03:38, 10 October 2024
Documentation for this module may be created at Module:SpeciesLink/doc
--- @module SpeciesLink local SpeciesLink = {} --region Dependencies local SpeciesData = require("Module:SpeciesData") local VIEW_TEMPLATE = "Species_link/view" --endregion --region Private constants local VALID_SIZES = { ["none"] = true, ["small"] = true, ["medium"] = true, ["large"] = true, ["huge"] = true, } --endregion --region Private methods local function validateSpeciesName(name) -- Try a quick match with the keys first. local speciesFound = SpeciesData.species[name] if speciesFound then return speciesFound else -- Look into the data to see if the name matches any of the names in the data. for _, species in pairs(SpeciesData.species) do if name == species[SpeciesData.NAME] then return species end end end error("You specified an invalid species name. Please see the template documentation for how to use the parameters.") end --endregion --region Public methods function SpeciesLink.main(frame) local name = frame.args.species or frame.args[1] local iconSize = frame.args.iconsize or frame.args[2] -- Check that something at all was provided for the name before we can validate it. if not name or name == "" then error("You must specify the name of the species. Please see the template documentation for how to use the parameters.") end local validatedSpecies = validateSpeciesName(name) local speciesName = validatedSpecies[SpeciesData.NAME] local iconFilename = validatedSpecies[SpeciesData.ICON_FILENAME] .. ".png" -- Defer to the view to handle the iconSize. We don't need to validate it. local parameters = { ["name"] = speciesName, ["iconfilename"] = iconFilename, ["size"] = iconSize, } return frame:expandTemplate{ title = VIEW_TEMPLATE, args = parameters, } end --endregion return SpeciesLink