模块:Iteminfo

来自Terraria Wiki
跳转至: 导航搜索

此模块的文档可以在模块:Iteminfo/doc创建

local data = mw.loadData( 'Module:iteminfo/data' ) -- loading data table.

local trim = mw.text.trim

-- cache
local currentFrame

-- helper function
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 p = {
	-- for other module, get all stats, return as a table.
	getItemInfo = function(itemid)
		if itemid >= 3930 or itemid <= 0 then
			itemid = 0
		end
		local d = data[itemid]
		local result = {['type'] = itemid}
		for k,v in pairs(data[0]) do
			if d[k] ~= nil and d[k] ~= v then
				result[k] = d[k]
			else
				result[k] = v
			end
		end
		return result
	end,

	-- for template, get all stats, setting in dplvars.
	getInfo = function(frame)
		currentFrame = frame -- cache

		local itemid = tonumber(getArg('id')) or tonumber(getArg(1)) or 0
		local prefix = getArg('prefix') or getArg(2) or '_iteminfo_'

		if itemid >= 3930 or itemid <= 0 then
			itemid = 0
		end
		
		local result = data[itemid]
		local args = { prefix .. 'type', itemid }
		for k,v in pairs(data[0]) do
			table.insert(args, prefix .. k)
			if result[k] ~= nil and result[k] ~= v then
				table.insert(args, result[k])
			else
				table.insert(args, v)
			end
		end

		frame:callParserFunction{ name = '#dplvar:set', args = args }
	end,

	-- for other module, get single stat, return this stat directly
	getItemStat = function(itemid, stat)
		if itemid >= 3930 or itemid <= 0 then
			itemid = 0
		end
		if stat == 'type' then
			return itemid
		end
		return data[itemid][stat] or data[0][stat]
	end,

	-- for template, get single stat, return directly
	getStat = function(frame)
		currentFrame = frame -- cache

		local itemid = tonumber(getArg('id')) or tonumber(getArg(1)) or 0
		local stat = getArg('stat') or getArg(2)

		if not stat then
			return
		end

		if itemid >= 3930 or itemid <= 0 then
			itemid = 0
		end
		if stat == 'type' then
			return itemid
		end
		
		return data[itemid][stat] or data[0][stat] or nil -- "or nil" will convert "false" to "nil"
		
	end,
}

--alias
p.getinfo = p.getInfo
p.GetInfo = p.getInfo
p.getstat = p.getStat
p.GetStat = p.getStat

return p