Difference between revisions of "Module:Img"

From Idlescape Wiki
Jump to navigation Jump to search
m
Line 1: Line 1:
 
local p = {}
 
local p = {}
local origArgs = {}
+
local findId = require("Module:FindId")
local name
 
local width = 'auto'
 
local height = 'auto'
 
local add_word
 
local only_url
 
local no_link
 
local link
 
  
local data_module_names = {
+
local dataModuleNames = {
item = 'Module:Items/data',
+
item = "Module:Items/data",
monster = 'Module:Monsters/data',
+
enchantment = "Module:Enchantment/data",
location = 'Module:Location/data',
+
location = "Module:Location/data",
 +
monster = "Module:Items/data"
 
}
 
}
local loaded_data_modules = {}
 
  
function p.loadData (data_type)  
+
local idListType = {
local module_name = data_module_names[data_type]
+
[1] = {
if loaded_data_modules[module_name] == nil then
+
[1] = "item",
loaded_data_modules[module_name] = mw.loadData(module_name)
+
[2] = "itemImage"
 +
},
 +
[2] = {
 +
[1] = "monster",
 +
[2] = "image"
 +
},
 +
[3] = {
 +
[1] = "enchantment",
 +
[2] = "buffIcon"
 +
},
 +
[4] = {
 +
[1] = "location",
 +
[2] = "locationImage"
 +
}
 +
}
 +
 
 +
local loadedDataModules = {}
 +
 
 +
--
 +
-- Loads data modules
 +
--
 +
-- @param dataType {string}
 +
-- @return {data table}
 +
--
 +
function p.loadData (dataType)
 +
local moduleName = dataModuleNames[dataType]
 +
if loadedDataModules[moduleName] == nil then
 +
loadedDataModules[moduleName] = mw.loadData(moduleName)
 
end
 
end
+
return loadedDataModules[moduleName]
return loaded_data_modules[module_name]
 
 
end
 
end
  
--Search the item data for item name and return its id
+
--
local function findIdByName(name, type)
+
-- Helps capitalise the first letter of each word
local items = p.loadData(type)
+
--
local lname = string.lower(name)
+
-- @param first {string}
local id = nil
+
-- @param rest {string}
+
-- @return {strings}
for k,v in pairs(items) do
+
--
if lname == string.lower(v.name) then
+
local function tchelper(first, rest)
id = k
+
    return first:upper()..rest:lower()
return id
 
end
 
end
 
return id
 
 
end
 
end
  
--Find the image from the name
+
--
function p.findImage(name)
+
-- Generates a full url
local id
+
--
name = string.lower(name)
+
-- @param url {string}
local image = p.loadData('item')[name]
+
-- @return {string}
+
--
--If name is a number, pass in as an item ID
+
local function fullUrl(url)
if tonumber(name) then
+
local newUrl = url
id = name
+
if url:sub(1,5) == "https" then
else
+
return newUrl
id = p.findItemID(name)
 
 
end
 
end
+
 
--If id is valid
+
if url:sub(1,1) ~= "/" then
if id then
+
newUrl = "/" .. newUrl
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
 
end
+
 
return image
+
newUrl = "https://www.play.idlescape.com" .. newUrl
 +
return newUrl
 
end
 
end
  
--Find the image from the name
+
--  
local function findImage(name, origSource)
+
-- Finds input's id and input's type
local id = nil
+
--
local source = origSource
+
-- @param name {string}
if source == '' then
+
-- @return {number}
for k,v in pairs(data_module_names) do
+
-- @return {string}
id = findIdByName(name, k)
+
local function findInputId(name)
if id ~= nil then
+
local id
source = k
+
local inputType
break
+
for index, value in ipairs(idListType) do
end
+
id = findId(name, value[1])
 +
if type(id) == "number" then
 +
inputType = value[1]
 +
return id , inputType
 
end
 
end
else
 
id = findIdByName(name, source)
 
 
end
 
end
 +
return 0
 +
end
  
local sourceData = p.loadData(source)[id]
+
--
realname = sourceData.name
+
-- Finds arg's image url
if source == 'item' then
+
--
local item = sourceData
+
-- @param id {number}
if item.itemIcon then
+
-- @param inputType {string}
return item.itemIcon
+
-- @return {string}
else
+
--
return item.itemImage
+
local function findImageUrl(id, inputType)
 +
local imageUrl
 +
for index, value in ipairs(idListType) do
 +
if value[1] == inputType then
 +
imageUrl = fullUrl(p.loadData(inputType[1])[tostring(id)][value[2]])
 +
return imageUrl
 
end
 
end
elseif source == 'monster' then
 
return sourceData.image
 
elseif source == 'location' then
 
return sourceData.locationImage
 
 
end
 
end
return ''
+
return 0
 
end
 
end
  
--Create a string to output
+
--
local function formatImage(url)
+
-- Generates string with page link and <img> tag
local s = ''
+
--
local rs = nil
+
-- @param name {string}
+
-- @param url {string}
if url then
+
-- @param width {number}
+
-- @param height {number}
if url:sub(1,1) ~= '/' then url = '/' .. url end
+
-- @param word {number}
url = 'https://idlescape.com' .. url
+
-- @return {string}
s = url
+
--
+
local function image(name, url, width, height, word)
if only_url == '1' then rs = s end
+
local s = url
+
s = "src=\"".. s .. "\""
s = 'src="' .. s .. '"'
+
s = s .. " alt=\"" .. name .. "\""
s = s .. ' alt="' .. realname .. '"'
+
s = s .. " width=\"" .. width .. "\""
s = s .. ' width="' .. width .. '"'
+
s = s .. " height=\"" .. height .. "\""
s = s .. ' height="' .. height .. '"'
+
s = '<img ' .. s .. '>'
s = '<img ' .. s .. '>'
+
if word == '1' then
+
s = s .. "&nbsp;" .. name
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
 
end
if not rs then rs = s end
+
s = '[[' .. name:gsub("(%a)([%w_']*)", tchelper) .. '|' .. s .. ']]'
return rs
+
return s
 
end
 
end
  
function p.image(frame)
+
--
return p._image(frame:getParent().args)
+
-- Main img method accessed through #invoke
 +
--
 +
-- @param frame {table}
 +
-- @return {string}
 +
--
 +
function p.img(frame)
 +
return p._img(frame:getParent())
 
end
 
end
  
function p._image(args)
+
--
local name = args[1]
+
-- img method to allow it to be called by other modules
width = 20
+
--
height = 20
+
-- @param _args {table}
add_word = false
+
-- @return {string}
realname = ''
+
--
local source = ''
+
function p._img(_args)
if args[2] then
+
local width = _args[2]
width = args[2]
+
local height = _args[3]
 +
local id
 +
local inputType
 +
local url
 +
local s
 +
if not width then
 +
width = 20
 
end
 
end
if args[3] then
+
if not height then
height = args[3]
+
height = 20
 
end
 
end
if args[4] then
+
id, inputType = findInputId(_args[1])
add_word = args[4]
+
if not id then
 +
return "couldn't find id. Check spelling or update id lists"
 
end
 
end
if args[5] then
+
url = findImageUrl(id, inputType)
source = args[5]
+
if not url then
 +
return "couldn't find image url."
 
end
 
end
+
s = image(_args[1], url, width, height, _args["word"])
if not name then return '' end
+
return s
local image = findImage(name, source)
 
return formatImage(image)
 
 
end
 
end
  
 
return p
 
return p

Revision as of 12:35, 1 June 2024


local p = {}
local findId = require("Module:FindId")

local dataModuleNames = {
	item = "Module:Items/data",
	enchantment = "Module:Enchantment/data",
	location = "Module:Location/data",
	monster = "Module:Items/data"
}

local idListType = {
	[1] = {
		[1] = "item",
		[2] = "itemImage"
	},
	[2] = {
		[1] = "monster",
		[2] = "image"
	},
	[3] = {
		[1] = "enchantment",
		[2] = "buffIcon"
	},
	[4] = {
		[1] = "location",
		[2] = "locationImage"
	}
}

local loadedDataModules = {}

--
-- Loads data modules
--
-- @param dataType {string}
-- @return {data table}
--
function p.loadData (dataType)
	local moduleName = dataModuleNames[dataType]
	if loadedDataModules[moduleName] == nil then
		loadedDataModules[moduleName] = mw.loadData(moduleName)
	end
	return loadedDataModules[moduleName]
end

--
-- Helps capitalise the first letter of each word
--
-- @param first {string}
-- @param rest {string}
-- @return {strings}
--
local function tchelper(first, rest)
    return first:upper()..rest:lower()
end

--
-- Generates a full url
--
-- @param url {string}
-- @return {string}
--
local function fullUrl(url)
	local newUrl = url
	if url:sub(1,5) == "https" then
		return newUrl
	end

	if url:sub(1,1) ~= "/" then
		newUrl = "/" .. newUrl
	end

	newUrl = "https://www.play.idlescape.com" .. newUrl
	return newUrl
end

-- 
-- Finds input's id and input's type
--
-- @param name {string}
-- @return {number}
-- @return {string}
local function findInputId(name)
	local id
	local inputType
	for index, value in ipairs(idListType) do
		id = findId(name, value[1])
		if type(id) == "number" then
			inputType = value[1]
			return id , inputType
		end
	end
	return 0
end

--
-- Finds arg's image url
--
-- @param id {number}
-- @param inputType {string}
-- @return {string}
--
local function findImageUrl(id, inputType)
	local imageUrl
	for index, value in ipairs(idListType) do
		if value[1] == inputType then
			imageUrl = fullUrl(p.loadData(inputType[1])[tostring(id)][value[2]])
			return imageUrl
		end
	end
	return 0
end

--
-- Generates string with page link and <img> tag
--
-- @param name {string}
-- @param url {string}
-- @param width {number}
-- @param height {number}
-- @param word {number}
-- @return {string}
--
local function image(name, url, width, height, word)
	local s = url
	s = "src=\"".. s .. "\""
	s = s .. " alt=\"" .. name .. "\""
	s = s .. " width=\"" .. width .. "\""
	s = s .. " height=\"" .. height .. "\""
	s = '<img ' .. s .. '>'
	if word == '1' then
		s = s .. "&nbsp;" .. name
	end
	s = '[[' .. name:gsub("(%a)([%w_']*)", tchelper) .. '|' .. s .. ']]'
	return s
end

--
-- Main img method accessed through #invoke
--
-- @param frame {table}
-- @return {string}
--
function p.img(frame)
	return p._img(frame:getParent())
end

--
-- img method to allow it to be called by other modules
--
-- @param _args {table}
-- @return {string}
--
function p._img(_args)
	local width = _args[2]
	local height = _args[3]
	local id
	local inputType
	local url
	local s
	if not width then
		width = 20
	end
	if not height then
		height = 20
	end
	id, inputType = findInputId(_args[1])
	if not id then
		return "couldn't find id. Check spelling or update id lists"
	end
	url = findImageUrl(id, inputType)
	if not url then
		return "couldn't find image url."
	end
	s = image(_args[1], url, width, height, _args["word"])
	return s
end

return p