Module:Items

From Coromon Wiki
Jump to navigation Jump to search

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

local p = { }

local util = require("Module:Util")
local parser = require("Module:Parse")
local itemList = mw.loadData("Module:Items/List");

local errorMsg = "N/A"

function p.isValid (frame)
	
	local name_or_id = frame.args[1] or frame.args["item"]
	local item = p.getItem(name_or_id)
	
	return item==nil and "false" or "true"
end

function p.getID (frame)
	
	local name_id = frame.args[1] or frame.args["item"]
	
	for id, item in pairs(itemList) do
		
		if item["name"] ~= nil and parser.toDisplayText(item["name"]):lower() == name_id:lower() or id == name_id then
			--mw.logObject(item)
			return id
		end
	end
	
	return nil
end

function p.getItem (name_id)
	
	for id, item in pairs(itemList) do
		if item["name"] ~= nil and parser.toDisplayText(item["name"]):lower() == name_id:lower() or id == name_id then
			--mw.logObject(item)
			return item
		end
	end
	
	return nil
end

function p.getItemsWith (name_id)
	
	local matches = {}
	for id, item in pairs(itemList) do
		if item["name"] ~= nil and util.startsWith(item["name"],name_id) or util.startsWith(id,name_id) then
			--mw.logObject(item)
			table.insert(matches, item)
		end
	end
	
	return matches
end

function p.getParam (frame)
	
	local item = p.getItem(frame.args[1] or frame.args["item"])
	local param = frame.args[2] or frame.args["param"]
	
	return item[param]
end

function p.getIcon (frame)
	
	local _id = frame.args[1] or frame.args['item']
	
	if _id:lower()=='gold' then
		return 'GoldIcon'
	end
	
	local id = p.getID(frame)
	
	if id==nil then
		return ''
	end

	if  id:match("GAUNTLET_COUPON")~=nil then
		return 'GAUNTLET_COUPON'
	elseif  id:match("SHOP_COUPON")~=nil then
		return 'SHOP_COUPON'
	elseif util.startsWith(id, 'SKILL_FLASH') then
		return 'SKILL_FLASH'
	end
	
	return id
end

return p