Module:Item

Jump to navigation Jump to search

Documentation

Designed to be part of a template like so:

{{#invoke:Item|main|{{{1}}}|{{{2}}}}}

Parameter 1 is the game abbreviation and parameter 2 is the item name. This module will output the item icon and name together. If parameter 2 is an emdash, it outputs an emdash.

See also: Template:Item/Documentation


local getArgs = require('Module:Arguments').getArgs
local p = {}

function p.main(frame)
	local args = getArgs(frame)
	local game = args[1]:lower()
	local item = ""
	if args[2] == nil then
		return "—"
	else
		item = mw.ustring.lower(args[2])
	end
	if item == "—" then
		return item
	end
	local itemData = mw.loadData('Module:Data/Item/' .. game).item
	local iconPage = mw.title.makeTitle('Module', 'Data/Icon/' .. game)
	local equipPage = mw.title.makeTitle('Module', 'Data/Equipment/' .. game)
	local link = ''
	if equipPage.exists then 
		local equipData = mw.loadData('Module:Data/Equipment/' .. game).equipment
		if not itemData[item] and not equipData[item] then
			error("Item or equipment " .. item .. " not defined for " .. game)
		end
		local iconData = {}
		if iconPage.exists then
			iconData = mw.loadData('Module:Data/Icon/' .. game)["icon"]
		end
		local itemIcon = ""
		local gap = " "
		if game == "desum" then
			gap = "<small> </small>"
		end
		
		if itemData[item] and itemData[item]["icon"] then
			itemIcon = itemData[item]["icon"]
		elseif equipData[item] and equipData[item]["icon"] then
			itemIcon = equipData[item]["icon"]
		elseif equipData[item] and equipData[item]["equiptype"] then
			itemIcon = equipData[item]["equiptype"]
		end
		
		if iconData[itemIcon] then
			link = iconData[itemIcon] .. gap
		elseif equipData[itemIcon] then
			link = iconData[itemIcon] .. gap
		end
		
		if itemData[item] and itemData[item]["link"] then
			link = link .. "[[" .. itemData[item]["link"] .. "|" .. itemData[item]["name"] .. "]]"
		elseif equipData[item] and equipData[item]["link"] then
			link = link .. "[[" .. equipData[item]["link"] .. "|" .. equipData[item]["name"] .. "]]"
		elseif itemData[item] then
			link = link .. "[[" .. itemData[item]["name"] .. "]]"
		else
			link = link .. "[[" .. equipData[item]["name"] .. "]]"
		end
	else -- if equip page doesn't exist
		if not itemData[item] then
			error("Item " .. item .. " not defined for " .. game)
		end
		local iconData = {}
		if iconPage.exists then
			iconData = require('Module:Data/Icon/' .. game)["icon"]
		end
		local itemIcon = ""
		local gap = " "
		if game == "desum" then
			gap = "<small> </small>"
		end
		if itemData[item]["icon"] then
			itemIcon = itemData[item]["icon"]
		end
		if iconData[itemIcon] then
			link = iconData[itemIcon] .. gap
		end
		if itemData[item]["link"] then
			link = link .. "[[" .. itemData[item]["link"] .. "|" .. itemData[item]["name"] .. "]]"
		else
			link = link .. "[[" .. itemData[item]["name"] .. "]]"
		end
	end
	return link
end

return p