Difference between revisions of "Module:Infobox Item"

From Idlescape Wiki
Jump to navigation Jump to search
m
m
Line 5: Line 5:
 
enchantment = 'Module:Enchantment/data'
 
enchantment = 'Module:Enchantment/data'
 
}
 
}
 +
 
local loaded_data_modules = {}
 
local loaded_data_modules = {}
  
function p.loadData (data_type)  
+
function p.loadData(data_type)  
 
local module_name = data_module_names[data_type]
 
local module_name = data_module_names[data_type]
 
if loaded_data_modules[module_name] == nil then
 
if loaded_data_modules[module_name] == nil then
Line 16: Line 17:
 
end
 
end
  
function p.findItem (name)
+
local function findItem(name)
 
for key, item in pairs(p.loadData("item")) do
 
for key, item in pairs(p.loadData("item")) do
 
if name == item['name'] then
 
if name == item['name'] then
Line 25: Line 26:
 
end
 
end
  
function p.getEnchantmentName (id)
+
local function getEnchantmentName(id)
 
return p.loadData("enchantment")[tostring(id)]['name']
 
return p.loadData("enchantment")[tostring(id)]['name']
 
end
 
end
  
function p.getItemName (id)
+
local function getItemName(id)
 
return p.loadData("item")[tostring(id)]['name']
 
return p.loadData("item")[tostring(id)]['name']
 
end
 
end
  
function p.addSeparator(num)
+
local function fullUrl(url)
return tostring(tonumber(num)):reverse():gsub("(%d%d%d)","%1,"):gsub(",(%-?)$","%1"):reverse()
+
local newUrl = url
end
+
if url:sub(1,5) == "https" then
 
+
return newUrl
function p.fullUrl(url)
 
local newUrl = ""
 
if url:sub(1,5) ~= "https" then
 
return url
 
 
end
 
end
 
 
 
if url:sub(1,1) ~= "/" then
 
if url:sub(1,1) ~= "/" then
newUrl = "/" .. url
+
newUrl = "/" .. newUrl
 
end
 
end
 
 
Line 51: Line 48:
 
end
 
end
  
function p.item(frame)
+
local function icon(name, url, word)
local args = frame:getParent().args
+
local s = fullUrl(url)
return p._item(args)
+
s = "<a href=/p/\"" .. name .. "\" title=\"" .. name .. "\"><img src=\"" .. s
 +
s = s .. "\" alt=\""  .. name .. "\" width=\"20\">"
 +
if word then
 +
s = s .. name
 +
end
 +
s = s .. "</a>"
 +
return s
 +
end
 +
 
 +
local function itemImage(id)
 +
local item = p.loadData("item")[tostring(id)]
 +
local url = ""
 +
if item['itemIcon'] then
 +
url = item['itemIcon']
 +
else
 +
url = item['itemImage']
 +
end
 +
return icon(item['name'], url)
 +
end
 +
 
 +
local function link(name)
 +
local s = ""
 +
s = "<a href=/p/\"" .. name .. "\" title=\"" .. name .. "\">" .. name .. "</a>"
 +
return s
 +
end
 +
 
 +
local function img(id)
 +
local url = ""
 +
if item['itemIcon'] then
 +
url = item['itemIcon']
 +
else
 +
url = item['itemImage']
 +
end
 +
return fullUrl(url)
 
end
 
end
  
function p._item(args)
+
local function addSeparator(num)
local name = args[1]
+
return tostring(tonumber(num)):reverse():gsub("(%d%d%d)","%1,"):gsub(",(%-?)$","%1"):reverse()
local item = 0
 
local infobox = ""
 
 
item = p.findItem (name)
 
infobox = p.createInfobox(item)
 
return infobox
 
 
end
 
end
  
function p.createInfobox(item)
+
local function createInfobox(item)
local s = "{{Infobox Item\n"
+
local args = {}
 
local url = ""
 
local url = ""
s = s .. "|title=" .. item['name'] .. "\n"
+
local s = ""
 +
args['title'] = item['name']
 
 
 
if item['itemIcon'] then
 
if item['itemIcon'] then
Line 76: Line 101:
 
url = item['itemImage']
 
url = item['itemImage']
 
end
 
end
s = s .. "|image=" .. p.fullUrl(url) .. "\n"
+
args['image'] = fullUrl(url)
 
 
 
if item['value'] then
 
if item['value'] then
s = s .. "|gold=" .. p.addSeparator(item['value']) .. "\n"
+
args['label1'] = itemImage(1)
 +
args['data1'] = addSeparator(item['value'])
 
end
 
end
 
 
if item['craftable'] then
+
local market = require("Module:Market")["_price"]({item['name'], 1, 1})
s = s .. "|source=[[Crafting]]\n"
+
if market then
 +
args['label2'] = icon('Market', "/images/ui/marketplace_icon.png")
 +
args['data2'] = addSeparator(item['value'])
 
end
 
end
 
 
 
if item['requiredLevel'] then
 
if item['requiredLevel'] then
s = s .. "|level-req ="
+
s = ""
 
for skill, level in pairs(item['requiredLevel']) do
 
for skill, level in pairs(item['requiredLevel']) do
 
s = s .. level .. " " .. skill .. "<br>"
 
s = s .. level .. " " .. skill .. "<br>"
 
end
 
end
s = s:sub(1,s:len()-4) .. "\n"
+
s = s:sub(1,s:len()-4)
 +
args['label3'] = "Level Required"
 +
args['data3'] = s
 
end
 
end
 +
 +
if item['craftable'] then
 +
args['label4'] = Source
 +
args['data4'] = link("Crafting")
 +
end
 +
 
 
 
if item['heat'] then
 
if item['heat'] then
s = s .. "|heat=" .. p.addSeparator(item['heat']) .. "\n"
+
args['label5'] = itemImage(2)
 +
args['data5'] = addSeparator(item['heat'])
 
end
 
end
 
 
 
if item['augmentationCost'] then
 
if item['augmentationCost'] then
s = s .. "|aug-bonus="
+
s = ""
 
for key, bonus in pairs(item['equipmentStats']['augmentationBonus']) do
 
for key, bonus in pairs(item['equipmentStats']['augmentationBonus']) do
 
s = s .. "+" .. bonus.value .. " "
 
s = s .. "+" .. bonus.value .. " "
 
s = s .. bonus.stat:sub(bonus.stat:find('%.')+1,bonus.stat:len()) .. "<br>"
 
s = s .. bonus.stat:sub(bonus.stat:find('%.')+1,bonus.stat:len()) .. "<br>"
 
end
 
end
 +
s = s:sub(1,s:len()-4)
 +
args['label7'] = "Aug Bonus"
 +
args['data7'] = s
 
 
s = s .. "|aug-cost="
+
s = ""
 
for key, cost in pairs(item['augmentationCost']) do
 
for key, cost in pairs(item['augmentationCost']) do
 
s = s .. cost .. " "
 
s = s .. cost .. " "
s = s .. "{{img|" .. p.getItemName(key) .."}}" .. "<br>"
+
s = s .. "{{img|" .. getItemName(key) .."|word=1}}" .. "<br>"
 
end
 
end
s = s:sub(1,s:len()-4) .. "\n"
+
s = s:sub(1,s:len()-4)
 +
args['label9'] = "Aug Cost"
 +
args['data9'] = s
 
end
 
end
 +
 +
args['header20'] = "Cooking"
 
 
 
if item['size'] then
 
if item['size'] then
s = s .. "|food-size=" .. item['size'] .. "\n"
+
args['label21'] = "Size"
 +
args['data21'] = item['size']
 +
end
 +
 +
if item['difficulty'] then
 +
args['label22'] = "Difficulty"
 +
args['data22'] = item['difficulty']
 
end
 
end
 
 
 
if item['ingredientTags'] then
 
if item['ingredientTags'] then
s = s .. "|food-cat="
+
s = ""
 
for key, tag in pairs(item['ingredientTags']) do
 
for key, tag in pairs(item['ingredientTags']) do
 
s = s .. tag .. "<br>"
 
s = s .. tag .. "<br>"
 
end
 
end
s = s:sub(1,s:len()-4) .. "\n"
+
s = s:sub(1,s:len()-4)
 +
args['label23'] = "Category"
 +
args['data23'] = item['size']
 
end
 
end
 
 
 
if item['cookingEnchantment'] then
 
if item['cookingEnchantment'] then
s = s .. "|food-buff =" .. p.getEnchantmentName(item['cookingEnchantment']) .. "\n"
+
args['label24'] = "Buff"
 +
args['data24'] = getEnchantmentName(item['cookingEnchantment'])
 
end
 
end
 +
 +
args['header80'] = "Seeds"
 
 
 
local farming = item['farmingStats']
 
local farming = item['farmingStats']
 
if farming then
 
if farming then
s = s .. "|plot-size=" .. farming['height'] .. "x" .. farming['width'] .. "\n"
+
s = ""
s = s .. "|seed-exp=" .. farming['experience'] .. "\n"
 
s = s .. "|harvest-time=" .. farming['time'] .. " minutes\n"
 
s = s .. "|yield="
 
 
for key, yield in pairs(farming['yield']) do
 
for key, yield in pairs(farming['yield']) do
 
s = s .. yield['min'] .. "-" .. yield['max'] .. " "
 
s = s .. yield['min'] .. "-" .. yield['max'] .. " "
s = s .. p.getItemName(yield['id'])
+
s = s .. getItemName(yield['id'])
 
if yield['chance'] ~= 1 then
 
if yield['chance'] ~= 1 then
 
s = s .. " " .. tonumber(string.format('%.2f', yield['chance'] * 100)) .. "%"
 
s = s .. " " .. tonumber(string.format('%.2f', yield['chance'] * 100)) .. "%"
Line 143: Line 195:
 
s = s .. "<br>"
 
s = s .. "<br>"
 
end
 
end
s = s:sub(1,s:len()-4) .. "\n"
+
s = s:sub(1,s:len()-4)
 +
args['label81'] = "Level Required"
 +
args['data81'] = farming['requiredLevel']
 +
args['label82'] = "Experience"
 +
args['data82'] = farming['experience']
 +
args['label83'] = "Plot Size"
 +
args['data83'] = farming['height'] .. "x" .. farming['width']
 +
args['label84'] = "Harvest Time"
 +
args['data84'] = farming['time'] .. " minutes"
 +
args['label85'] = "Yield"
 +
args['data85'] = s
 
end
 
end
 
 
if item['extraTooltipInfo'] then
+
args['header100'] = "Tooltip"
s = s .. "|tooltip=" .. item['extraTooltipInfo'] .. "\n"
+
end
+
args['data101'] = "{{Infobox | subbox = yes"
 +
args['bodystyle'] = "padding: 0.5em; margin:auto; font-style:italic; font-size:110%; text-align: center"
 +
args['data1'] = item['extraTooltipInfo']
 +
 +
return require('Module:Infobox')['infobox'](args)
 +
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 = ""
 
 
s = s .. "}}"
+
item = findItem (name)
return s
+
infobox = createInfobox(item)
 +
return infobox
 
end
 
end
  
 
return p
 
return p

Revision as of 15:44, 11 September 2023


local p = {}

local data_module_names = {
	item = 'Module:Items/data',
	enchantment = 'Module:Enchantment/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

local function findItem(name)
	for key, item in pairs(p.loadData("item")) do
		if name == item['name'] then
			return item
		end
	end
	return 0
end

local function getEnchantmentName(id)
	return p.loadData("enchantment")[tostring(id)]['name']
end

local function getItemName(id)
	return p.loadData("item")[tostring(id)]['name']
end

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

local function icon(name, url, word)
	local s = fullUrl(url)
	s = "<a href=/p/\"" .. name .. "\" title=\"" .. name .. "\"><img src=\"" .. s
	s = s .. "\" alt=\""  .. name .. "\" width=\"20\">"
	if word then
		s = s .. name
	end
	s = s .. "</a>"
	return s
end

local function itemImage(id)
	local item = p.loadData("item")[tostring(id)]
	local url = ""
	if item['itemIcon'] then
		url = item['itemIcon']
	else
		url = item['itemImage']
	end
	return icon(item['name'], url)
end

local function link(name)
	local s = ""
	s = "<a href=/p/\"" .. name .. "\" title=\"" .. name .. "\">" .. name .. "</a>"
	return s
end

local function img(id)
	local url = ""
	if item['itemIcon'] then
		url = item['itemIcon']
	else
		url = item['itemImage']
	end
	return fullUrl(url)
end

local function addSeparator(num)
	return tostring(tonumber(num)):reverse():gsub("(%d%d%d)","%1,"):gsub(",(%-?)$","%1"):reverse()
end

local function createInfobox(item)
	local args = {}	
	local url = ""
	local s = ""
	args['title'] = item['name']
	
	if item['itemIcon'] then
		url = item['itemIcon']
	else
		url = item['itemImage']
	end
	args['image'] = fullUrl(url)
	
	if item['value'] then
		args['label1'] = itemImage(1)
		args['data1'] = addSeparator(item['value'])
	end
	
	local market = require("Module:Market")["_price"]({item['name'], 1, 1})
	if market then
		args['label2'] = icon('Market', "/images/ui/marketplace_icon.png")
		args['data2'] = addSeparator(item['value'])
	end
	
	if item['requiredLevel'] then
		s = ""
		for skill, level in pairs(item['requiredLevel']) do
			s = s .. level .. " " .. skill .. "<br>"
		end
		s = s:sub(1,s:len()-4)
		args['label3'] = "Level Required"
		args['data3'] = s
	end
	
	if item['craftable'] then
		args['label4'] = Source
		args['data4'] = link("Crafting")
	end
	
	
	if item['heat'] then
		args['label5'] = itemImage(2)
		args['data5'] = addSeparator(item['heat'])
	end
	
	if item['augmentationCost'] then
		s = ""
		for key, bonus in pairs(item['equipmentStats']['augmentationBonus']) do
			s = s .. "+" .. bonus.value .. " "
			s = s .. bonus.stat:sub(bonus.stat:find('%.')+1,bonus.stat:len()) .. "<br>"
		end
		s = s:sub(1,s:len()-4)
		args['label7'] = "Aug Bonus"
		args['data7'] = s
		
		s = ""
		for key, cost in pairs(item['augmentationCost']) do
			s = s .. cost .. " "
			s = s .. "{{img|" .. getItemName(key) .."|word=1}}" .. "<br>"
		end
		s = s:sub(1,s:len()-4)
		args['label9'] = "Aug Cost"
		args['data9'] = s
	end
	
	args['header20'] = "Cooking"
	
	if item['size'] then
		args['label21'] = "Size"
		args['data21'] = item['size']
	end
	
	if item['difficulty'] then
		args['label22'] = "Difficulty"
		args['data22'] = item['difficulty']
	end
	
	if item['ingredientTags'] then
		s = ""
		for key, tag in pairs(item['ingredientTags']) do
			s = s .. tag .. "<br>"
		end
		s = s:sub(1,s:len()-4)
		args['label23'] = "Category"
		args['data23'] = item['size']
	end	
	
	if item['cookingEnchantment'] then
		args['label24'] = "Buff"
		args['data24'] = getEnchantmentName(item['cookingEnchantment'])
	end
	
	args['header80'] = "Seeds"
	
	local farming = item['farmingStats']
	if farming then
		s = ""
		for key, yield in pairs(farming['yield']) do
			s = s .. yield['min'] .. "-" .. yield['max'] .. " "
			s = s .. getItemName(yield['id'])
			if yield['chance'] ~= 1 then
				s = s .. " " .. tonumber(string.format('%.2f', yield['chance'] * 100)) .. "%"
			end
			s = s .. "<br>"
		end
		s = s:sub(1,s:len()-4)
		args['label81'] = "Level Required"
		args['data81'] = farming['requiredLevel']
		args['label82'] = "Experience"
		args['data82'] = farming['experience']
		args['label83'] = "Plot Size"
		args['data83'] = farming['height'] .. "x" .. farming['width']
		args['label84'] = "Harvest Time"
		args['data84'] = farming['time'] .. " minutes"
		args['label85'] = "Yield"
		args['data85'] = s
	end
	
	args['header100'] = "Tooltip"
	
	args['data101'] = "{{Infobox | subbox = yes"
	args['bodystyle'] = "padding: 0.5em; margin:auto; font-style:italic; font-size:110%; text-align: center"
	args['data1'] = item['extraTooltipInfo']
	
	return require('Module:Infobox')['infobox'](args)
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 = findItem (name)
	infobox = createInfobox(item)
	return infobox
end

return p