Módulo:Iteminfo query

Fonte: Terraria Wiki
Saltar para a navegação Saltar para a pesquisa

A documentação para este módulo pode ser criada na página Módulo:Iteminfo query/doc

--------------------------------------------------------------------------------
--
-- =============================================================================
--
-- Module:Iteminfo query
--
-- Fetch list of items by querying one of their "Iteminfo" attributes
--
-- =============================================================================
--
-- Code annotations:
-- This module is documented according to LuaCATS (Lua Comment and Type System).
-- LuaCATS comments are prefixed with three dashes (---) and use Markdown syntax.
-- For a full list of annotations, see the following link:
-- https://luals.github.io/wiki/annotations/
--
--------------------------------------------------------------------------------

local translate = require('Module:Tr').translate
local lang = require('Module:Lang')
local items = require('Module:Iteminfo')
local itemIds = items.info.IDs
local itemStat = items.getItemStat

---A cached version of the current frame, the interface to the parser.
local currentFrame

---Holds the arguments from the template call.
---@type table<string, string>
local inputArgs

---Return a trimmed version of the value of the template parameter with the specified `key`.
---Return `nil` if the parameter is empty or unset.
---@param key string|integer
---@return string?
local function getArg(key)
	local value = inputArgs[key]
	if not value then
		return nil
	end
	value = mw.text.trim(value)
	if value == '' then
		return nil
	end
	return value
end

--------------------------------------------------------------------------------
---Main return object
local p = {}

---Fetch list of items matching the parameters and return it as an array from
---Extension:ArrayFunctions.
---@param frame table Interface to the parser (`mw.frame`)
---@return string
p.go = function(frame)
	currentFrame = frame  -- global frame cache
	inputArgs = frame:getParent().args  -- global input args cache

	local statToLookFor = getArg('stat')
	local valueToLookFor = getArg('value')
	local valueNotToLookFor = getArg('valuenot')
	local getNames = getArg('get') ~= 'id'
	local sortBy = getArg('sort')

	-- validate the input parameters
	if not statToLookFor or not (valueToLookFor or valueNotToLookFor) then
		return frame:expandTemplate{ title = 'error', args = {
			'"stat" and "value"/"valuenot" must both be set in {{iteminfo query}}',
			from = 'Iteminfo query'
		}}
	end
	if valueToLookFor and valueNotToLookFor then
		return frame:expandTemplate{ title = 'error', args = {
			'Only one out of "value" and "valuenot" must be used in {{iteminfo query}}',
			from = 'Iteminfo query'
		}}
	end

	local foundItems = {}
	-- find all items that have the required stat and value and add their names
	-- to the `foundItems` list
	for itemId = itemIds.min, itemIds.max do
		local statValueOfThisItem = itemStat(itemId, statToLookFor)
		-- value must not be nil
		if statValueOfThisItem ~= nil then
			-- all comparisons must be string comparisons
			if (valueToLookFor and tostring(statValueOfThisItem) == valueToLookFor) or (valueNotToLookFor and tostring(statValueOfThisItem) ~= valueNotToLookFor) then
				-- this item has the required stat and value, so add it to the list
				if getNames then
					table.insert(foundItems, itemStat(itemId, 'name'))
				else
					table.insert(foundItems, itemId)
				end
			end
		end
	end

	if sortBy == 'name' then
		-- sort the names in the current wiki language
		local language = lang.get()
		table.sort(foundItems, function(item1, item2)
			if not getNames then
				-- `foundItems` is a list of item IDs, so `item1` and `item2` must
				-- be converted to names first
				item1 = npcStat(item1, 'name')
				item2 = npcStat(item2, 'name')
			end
			return translate(item1, language) < translate(item2, language)
		end)
	end

	-- output as an array from Extension:ArrayFunctions
	return mw.af.export(foundItems)
end

return p