Difference between revisions of "Module:Heat"

From Idlescape Wiki
Jump to navigation Jump to search
(Heat wikitable)
 
m
Line 138: Line 138:
 
function p.heat(frame)
 
function p.heat(frame)
 
local args = frame:getParent().args
 
local args = frame:getParent().args
return p._item(args)
+
return p._heat(args)
 
end
 
end
  

Revision as of 01:52, 10 October 2023

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

local p = {}

local data_module_names = {
	item = 'Module:Items/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)
	local lname = name:lower()
	
	 --Remove leading and trailing spaces.
	lname = lname:gsub('^%s*(.-)%s*$', '%1')
	for key, item in pairs(p.loadData("item")) do
		if lname == item['name']:lower() then
			return item
		end
	end
	return 0
end

local function getItem(id)
	return p.loadData("item")[tostring(id)]
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 = "[[" .. name .. "|<img src=\"" .. s
	s = s .. "\" alt=\""  .. name .. "\" width=\"20\">"
	if word then
		s = s .. name
	end
	s = s .. "]]"
	return s
end

local function itemImage(id, word)
	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, word)
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 findSource(id)
	local s = ""
	s = s .. gatheringSource(id)
	s = s .. farmingSource(id)
	s = s .. scrollcraftingSource(id)
	s = s .. runecraftingSource(id)
	s = s .. smithingSource(id)
	s = s .. craftingSource(id)
	s = s .. cookingSource(id)
	if s:len() > 4 then 
		s = s:sub(1,s:len()-4)
	end
	return s	
end

local function createTable(item)
	local s = ""
	s = "{| class=\"wikitable sortable \"\n"
	s = s .. "!Image\n"
	s = s .. "!Heat\n"
	s = s .. "!Vendor\n"
	s = s .. "!Market\n"
	s = s .. "!Gold(Market)/Heat\n"
	s = s .. "|-"
	if item ~= 0 then
		
	else
		for key, item in pairs(p.loadData("items")) do
			if item['heat'] then
				s = s .. "|<img src=\"" .. fullUrl(item['itemImage']) .. "\" width=\"150\">"
				s = s .. "|" .. item['name'] .. "\n"
				s = s .. "|" .. addSeparator(item['heat']) .. "\n"
				s = s .. "|" .. addSeparator(item['value']) .. "\n"
				local market = require("Module:Market")["_price"]({item['name'], 1, 1})
				if market then
					s = s .. "|" .. addSeparator(market) .. "\n"
					s = s .. "|" .. addSeparator(math.floor(market/heat*100)/100) .. "\n"
				else
					s = s .. "|Yes\n"
					s = s .. "|\n"
				end
				
			end
		end
	end
	s = s .. "|}"
	return s
end

function p.heat(frame)
	local args = frame:getParent().args
	return p._heat(args)
end

function p._heat(args)
	local name = ""
	local item = 0
	local wikitable = ""
	
	if args[1] then
		name = args[1]
		item = findItem(name)
	end
	
	wikitable = createTable(item)
	return wikitable
end

return p