Module:Infobox Item

From Idlescape Wiki
Revision as of 12:49, 11 September 2023 by Silent1 (talk | contribs)
Jump to navigation Jump to search

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

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

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

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

function p.addSeparator(num)
	return tostring(tonumber(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.findItem (name)
	infobox = p.createInfobox(item)
	return infobox
end

function p.createInfobox(item)
	local s = "{{Infobox Item\n"
	local url = ""
	s = s .. "|title=" .. item['name'] .. "\n"
	
	if item['itemIcon'] then
		url = item['itemIcon']
	else
		url = item['itemImage']
	end
	s = s .. "|image=" .. p.fullUrl(url) .. "\n"
	
	if item['value'] then
		s = s .. "|gold=" .. p.addSeparator(item['value']) .. "\n"
	end
	
	if item['craftable'] then
		s = s .. "|source=[[Crafting]]\n"
	end
	
	if item['requiredLevel'] then
		s = s .. "|level-req ="
		for skill, level in pairs(item['requiredLevel']) do
			s = s .. level .. " " .. skill .. "<br>"
		end
		s = s:sub(1,s:len()-4) .. "\n"
	end
	
	if item['heat'] then
		s = s .. "|heat=" .. p.addSeparator(item['heat']) .. "\n"
	end
	
	if item['augmentationCost'] then
		s = s .. "|aug-bonus="
		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 .. "|aug-cost="
		for key, cost in pairs(item['augmentationCost']) do
			s = s .. cost .. " "
			s = s .. "{{img|" .. p.getItemName(key) .."}}" .. "<br>"
		end
		s = s:sub(1,s:len()-4) .. "\n"
	end
	
	if item['size'] then
		s = s .. "|food-size=" .. item['size'] .. "\n"
	end
	
	if item['ingredientTags'] then
		s = s .. "|food-cat="
		for key, tag in pairs(item['ingredientTags']) do
			s = s .. tag .. "<br>"
		end
		s = s:sub(1,s:len()-4) .. "\n"
	end	
	
	if item['cookingEnchantment'] then
		s = s .. "|food-buff =" .. p.getEnchantmentName(item['cookingEnchantment']) .. "\n"
	end
	
	local farming = item['farmingStats']
	if farming then
		s = s .. "|plot-size=" .. farming['height'] .. "x" .. farming['width'] .. "\n"
		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
			s = s .. yield['min'] .. "-" .. yield['max'] .. " "
			s = s .. p.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) .. "\n"
	end
	
	if item['extraTooltipInfo'] then
		s = s .. "|tooltip=" .. item['extraTooltipInfo'] .. "\n"
	end
	
	s = s .. "}}"
	return s
end

return p