Module:Npcinfo/idSets

From Terraria Wiki
Jump to navigation Jump to search

No documentation subpage exists yet for this module. Create one now.


local data = mw.loadData( 'Module:Npcinfo/idSets/data' ) -- loading data

local trim = mw.text.trim
-- cache
local currentFrame
local getArg = function(key)
	local value = currentFrame.args[key]
	if not value then
		return nil
	end
	value = trim(value)
	if value == '' then
		return nil
	else
		return value
	end
end

local function get(setname, npcid)
	if not data[setname] then
		return nil
	end

	if data[setname][npcid] == nil then
		return data[setname]['default']
	else
		return data[setname][npcid]
	end
end


return {
	-- {{#invoke:Npcinfo/idSets|getAllNames}} returns all valid set sets, seperated by comma.
	-- also can be called by other module:  require('Module:Npcinfo/idSets').getAllNames()
	getAllNames = function()
		local str
		for name,_ in pairs(data) do
			if str then
				str = str .. ', ' .. name
			else
				str = name
			end
		end
		return str
	end,

	-- {{#invoke:Npcinfo/idSets|value|<setname>|<npcid>}} returns the value for this npc in that set.
	-- return value may be:
	--     ""(Empty string): setname is invaild.
	--     "nil": only for nullable sets.
	--     "true" / "false": for bool values.
	--     other number string: int or float values.
	value = function(frame)
		currentFrame = frame -- cache
		local setname = getArg(1)
		local npcid = tonumber(getArg(2)) or 0
		return get(setname, npcid)
	end,

	-- for module.
	getValue = function(setname, npcid)
		npcid = tonumber(npcid) or 0
		return get(setname, npcid)
	end,

	-- for template
	getSet = function(frame)
		currentFrame = frame -- cache
		local setname = getArg(1)
		if not data[setname] then
			return
		end
		local str
		for k, v in pairs(data[setname]) do
			if str then 
				str = str .. ', ' .. k
			else
				str = k
			end
		end
		return str
	end,

	-- for module.
	getIdSet = function(setname)
		if not data[setname] then
			return nil
		end
		return data[setname] --readonly?
	end,

}