Difference between revisions of "Module:Infobox Item"

From Idlescape Wiki
Jump to navigation Jump to search
m
m
Line 57: Line 57:
 
local s = ""
 
local s = ""
 
local url = ""
 
local url = ""
s = s .. "|title ={{{title|{{PAGENAME}}}}}\n"
+
s = s .. "|title = {{PAGENAME}}\n"
 +
 
if item["itemIcon"] then
 
if item["itemIcon"] then
 
url = item["itemIcon"]
 
url = item["itemIcon"]
Line 63: Line 64:
 
url = item["itemImage"]
 
url = item["itemImage"]
 
end
 
end
s = s .. "|image ={{{image|}}}|<img src=\"" .. p.fullUrl(url) .. "\" height=\"auto\" width=\"150\">|{{img|{{PAGENAME}}|150}}}}\n"
+
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
 
return s
 
end
 
end
  
 
return p
 
return p

Revision as of 10:55, 11 September 2023


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