Difference between revisions of "Module:Silent1/sandbox"

From Idlescape Wiki
Jump to navigation Jump to search
m
m
Line 43: Line 43:
 
end
 
end
 
 
local id = p.findItemID(origArgs[1])
+
local id = tostring(p.findItemID(origArgs[1]))
 
return p.getURL(id)
 
return p.getURL(id)
 
end
 
end
  
 
return p
 
return p

Revision as of 10:38, 14 February 2022

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

local p = {}
local origArgs = {}

local data_module_names = {
	item = 'Module:Silent1/sandbox/data'
}
local loaded_data_modules = {}

function p.loadData (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
	
	return loaded_data_modules[module_name]
end

function p.findItemID(name)
	local items = p.loadData('item')
	local lname = string.lower(name)
	local id = 0
	
	for k,v in pairs(items) do
		if lname == string.lower(v[name]) then 
			id = k
			return id
		end
	end
	return id
end

function p.getURL(id)
	return 'https://idlescape.com' .. p.loadData('item')[id][itemImage]
end

-- If called via #invoke, use the args passed into the invoking template.
-- Otherwise, for testing purposes, assume args are being passed directly in.
function p.image(frame)
	if frame == mw.getCurrentFrame() then
		origArgs = frame:getParent().args
	else
		origArgs = frame
	end
	
	local id = tostring(p.findItemID(origArgs[1]))
	return p.getURL(id)
end

return p