Module:Infobox Item

From Idlescape Wiki
Revision as of 10:55, 11 September 2023 by Silent1 (talk | contribs)
Jump to navigation Jump to search

local p = {}

local data_module_names = {
	itemData = 'Module:Items/data'
}
local loaded_data_modules = {}

function p.loadItem (name, data_type) 
	local module_name = data_module_names[data_type]
	if loaded_data_modules[module_name] == nil then
		loaded_data_modules[module_name] = mw.loadData(module_name)
	end
	
	for key, item in pairs(loaded_data_modules[module_name]) do
		if name == item["name"] then
			return item
		end
	end
	
	return 0
end

function p.addSeparator(num)
	return tostring(num):reverse():gsub("(%d%d%d)","%1,"):gsub(",(%-?)$","%1"):reverse()
end

function p.fullUrl(url)
	local newUrl = ""
	if url:sub(1,5) ~= "https" then
		return url
	end
	
	if url:sub(1,1) ~= "/" then
		newUrl = "/" .. url
	end
	
	newUrl = "https://www.play.idlescape.com" .. newUrl
	return newUrl
end

function p.item(frame)
	local args = frame:getParent().args
	return p._item(args)
end

function p._item(args)
	local name = args[1]
	local item = 0
	local infobox = ""
	
	item = p.loadItem (name, 'itemData')
	infobox = p.createInfobox(item)
	return infobox
end

function p.createInfobox(item)
	local s = ""
	local url = ""
	s = s .. "|title = {{PAGENAME}}\n"
	
	if item["itemIcon"] then
		url = item["itemIcon"]
	else
		url = item["itemImage"]
	end
	s = s .. "|image =" .. p.fullUrl(url) .. "\n"
	
	if item["value"] then
		s = s .. "|gold =" .. p.addSeparator(tonumber(item["value"])) .. "\n"
	end
	
	if item["requiredLevel"] then
		for skill, level in pairs(item["requiredLevel"]) do
			s = s .. level .. " " .. skill
		end
	end
	
	return s
end

return p