Difference between revisions of "Module:Infobox Item"

From Idlescape Wiki
Jump to navigation Jump to search
(test)
 
m
Line 50: Line 50:
 
 
 
item = p.loadItem (name, 'itemData')
 
item = p.loadItem (name, 'itemData')
infobox = createInfobox(item)
+
infobox = p.createInfobox(item)
 
return infobox
 
return infobox
 
end
 
end
  
function createInfobox(item)
+
function p.createInfobox(item)
 
local s = ""
 
local s = ""
 
local url = ""
 
local url = ""
Line 63: Line 63:
 
url = item["itemImage"]
 
url = item["itemImage"]
 
end
 
end
s = s .. "|image ={{{image|}}}|<img src=\"" .. fullUrl(url) .. "\" height=\"auto\" width=\"150\">|{{img|{{PAGENAME}}|150}}}}\n"
+
s = s .. "|image ={{{image|}}}|<img src=\"" .. p.fullUrl(url) .. "\" height=\"auto\" width=\"150\">|{{img|{{PAGENAME}}|150}}}}\n"
 
return s
 
return s
 
end
 
end
  
 
return p
 
return p

Revision as of 10:42, 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 ={{{title|{{PAGENAME}}}}}\n"
	if item["itemIcon"] then
		url = item["itemIcon"]
	else
		url = item["itemImage"]
	end
	s = s .. "|image ={{{image|}}}|<img src=\"" .. p.fullUrl(url) .. "\" height=\"auto\" width=\"150\">|{{img|{{PAGENAME}}|150}}}}\n"
	return s
end

return p