Difference between revisions of "Module:Img"

From Idlescape Wiki
Jump to navigation Jump to search
(The module is slower than using a switch statement in a template. Will leave it uninvoked for now.)
 
m
Line 11: Line 11:
 
local data_module_names = {
 
local data_module_names = {
 
item = 'Module:Items/data',
 
item = 'Module:Items/data',
--item = 'Module:Silent1/sandbox/data',
+
monster = 'Module:Monsters/data',
other = 'Module:OtherImages/data'
+
location = 'Module:Locations/data',
 
}
 
}
 
local loaded_data_modules = {}
 
local loaded_data_modules = {}
Line 26: Line 26:
  
 
--Search the item data for item name and return its id
 
--Search the item data for item name and return its id
function p.findItemID(name)
+
local function findIdByName(name, type)
local items = p.loadData('item')
+
local items = p.loadData(type)
 
local lname = string.lower(name)
 
local lname = string.lower(name)
 
local id = nil
 
local id = nil
 
 
 
for k,v in pairs(items) do
 
for k,v in pairs(items) do
if lname == string.lower(v['name']) then  
+
if lname == string.lower(v.name) then  
 
id = k
 
id = k
 
return id
 
return id
Line 44: Line 44:
 
local id
 
local id
 
name = string.lower(name)
 
name = string.lower(name)
local image = p.loadData('other')[name]
+
local image = p.loadData('item')[name]
 
 
 
--If name is a number, pass in as an item ID
 
--If name is a number, pass in as an item ID
Line 56: Line 56:
 
if id then
 
if id then
 
local item = p.loadData('item')[id]
 
local item = p.loadData('item')[id]
local url = item['itemImage']
+
local url = item.itemImage
local icon = item['itemIcon']
+
local icon = item.itemIcon
local realname = item['name']
+
local realname = item.name
 
 
 
if icon then url = icon end
 
if icon then url = icon end
 
image = {name = realname, image = url}
 
image = {name = realname, image = url}
 
 
if not (image['name'] and image['image']) then
+
if not (image.name and image.image) then
 
image = nil
 
image = nil
 
end
 
end
Line 71: Line 71:
 
end
 
end
  
-- function p.findImage(name)
+
--Find the image from the name
-- local id
+
local function findImage(name, origSource)
-- name = string.lower(name)
+
local id = nil
-- local image = p.loadData('other')[name]
+
local source = origSource
+
if source == '' then
-- if not image then
+
for k,v in pairs(data_module_names) do
-- image = p.loadData('item')[name]
+
id = findIdByName(name, k)
-- end
+
if id ~= nil then
-- return image
+
source = k
-- end
+
break
 +
end
 +
end
 +
else
 +
id = findIdByName(name, source)
 +
end
 +
 
 +
local sourceData = p.loadData(source)[id]
 +
realname = sourceData.name
 +
if source == 'item' then
 +
local item = sourceData
 +
if item.itemIcon then
 +
return item.itemIcon
 +
else
 +
return item.itemImage
 +
end
 +
elseif source == 'monster' then
 +
return sourceData.image
 +
elseif source == 'location' then
 +
return sourceData.locationImage
 +
end
 +
return ''
 +
end
  
 
--Create a string to output
 
--Create a string to output
local function _image()
+
local function formatImage(url)
local image = p.findImage(name)
 
 
local s = ''
 
local s = ''
 
local rs = nil
 
local rs = nil
 
 
if image then  
+
if url then  
local realname = image['name']
 
local url = image['image']
 
 
 
 
if url:sub(1,1) ~= '/' then url = '/' .. url end
 
if url:sub(1,1) ~= '/' then url = '/' .. url end
Line 120: Line 139:
  
 
function p.image(frame)
 
function p.image(frame)
return p._image(frame:getParent())
+
return p._image(frame:getParent().args)
 
end
 
end
  
function p._image(frame)
+
function p._image(args)
origArgs = frame.args
+
local name = args[1]
name = origArgs[1]
+
width = 20
arg = origArgs[2]
+
height = 20
if not arg then  
+
add_word = false
width = 20
+
realname = ''
else arg:match('%S')
+
local source = ''
width = arg
+
if args[2] then
 +
width = args[2]
 +
end
 +
if args[3] then
 +
height = args[3]
 +
end
 +
if args[4] then
 +
add_word = args[4]
 +
end
 +
if args[5] then
 +
source = args[5]
 
end
 
end
arg = origArgs[3]
 
if arg and arg:match('%S') then height = arg end
 
add_word = origArgs['word']
 
only_url = origArgs['url']
 
no_link = origArgs['nolink']
 
link = origArgs['link']
 
 
 
 
if not name then return '' end
 
if not name then return '' end
return _image()
+
local image = findImage(name, source)
 +
return formatImage(image)
 
end
 
end
  
 
return p
 
return p

Revision as of 14:55, 28 May 2024


local p = {}
local origArgs = {}
local name
local width = 'auto'
local height = 'auto'
local add_word
local only_url
local no_link
local link

local data_module_names = {
	item = 'Module:Items/data',
	monster = 'Module:Monsters/data',
	location = 'Module:Locations/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

--Search the item data for item name and return its id
local function findIdByName(name, type)
	local items = p.loadData(type)
	local lname = string.lower(name)
	local id = nil
	
	for k,v in pairs(items) do
		if lname == string.lower(v.name) then 
			id = k
			return id
		end
	end
	return id
end

--Find the image from the name
function p.findImage(name)
	local id
	name = string.lower(name)
	local image = p.loadData('item')[name]
	
	--If name is a number, pass in as an item ID
	if tonumber(name) then
		id = name
	else 
		id = p.findItemID(name)
	end
		
	--If id is valid
	if id then
		local item = p.loadData('item')[id]
		local url = item.itemImage
		local icon = item.itemIcon
		local realname = item.name
		
		if icon then url = icon end
		image = {name = realname, image = url}
		
		if not (image.name and image.image) then
			image = nil
		end
	end
	
	return image
end

--Find the image from the name
local function findImage(name, origSource)
	local id = nil
	local source = origSource
	if source == '' then
		for k,v in pairs(data_module_names) do
			id = findIdByName(name, k)
			if id ~= nil then
				source = k
				break
			end
		end
	else 
		id = findIdByName(name, source)
	end

	local sourceData = p.loadData(source)[id]
	realname = sourceData.name
	if source == 'item' then
		local item = sourceData
		if item.itemIcon then
			return item.itemIcon
		else 
			return item.itemImage
		end
	elseif source == 'monster' then
		return sourceData.image
	elseif source == 'location' then
		return sourceData.locationImage
	end
	return ''
end

--Create a string to output
local function formatImage(url)
	local s = ''
	local rs = nil
	
	if url then 
		
		if url:sub(1,1) ~= '/' then url = '/' .. url end
		url = 'https://idlescape.com' .. url
		s = url
		
		if only_url == '1' then rs = s end
		
		s = 'src="' .. s .. '"'
		s = s .. ' alt="' .. realname .. '"'
		s = s .. ' width="' .. width .. '"'
		s = s .. ' height="' .. height .. '"'
		s = '<img ' .. s .. '>'
		
		if add_word == '1' then 
			s = s .. ' ' .. realname
		end
		
		if no_link == '1' then rs = s end
		
		if not (link and link:match('%S')) then link = realname end
		s = '[[' .. link .. '|' .. s .. ']]'
	else
		s = '[[' .. name .. ']]' .. '{{?}}'
	end
	if not rs then rs = s end 
	return rs
end

function p.image(frame)
	return p._image(frame:getParent().args)
end

function p._image(args)
	local name = args[1]
	width = 20
	height = 20
	add_word = false
	realname = ''
	local source = ''
	if args[2] then
		width = args[2]
	end
	if args[3] then
		height = args[3]
	end
	if args[4] then
		add_word = args[4]
	end
	if args[5] then
		source = args[5]
	end
	
	if not name then return '' end
	local image = findImage(name, source)
	return formatImage(image)
end

return p