Module:Sandbox/User:4madness/GESmithedItemsSet

From WIDEVERSE Wiki
Jump to navigation Jump to search

Documentation for this module may be created at Module:Sandbox/User:4madness/GESmithedItemsSet/doc

-- 
-- Variation of [[Module:GEItem]] that queries a list of smithable items then shows the table.
-- <nowiki>
--

local p = {}

local yesno = require( 'Module:Yesno' )
local SmithedItems = require( 'Module:Sandbox/User:4madness/SmithedItems' )
local Exchange = require( 'Module:Exchange' )

-- Main function
function p.table(frame)
	local args = frame:getParent().args
	-- named args for JSCalculator support
	local group = args.Group or args.group or ''
	local material = args.Material or args.material or 'all'
	local maxRange = args.Max or args.max or nil
	local minRange = args.Min or args.min or nil
	local basics = yesno(args.Basics or args.basics or 'no')
	
	if (maxRange ~= nil) then
		local parts = mw.text.split( maxRange, '(', true )
		maxRange = tonumber(mw.text.trim(parts[0]))
	end
	if (minRange ~= nil) then
		local parts = mw.text.split( minRange, '(', true )
		minRange = tonumber(mw.text.trim(parts[0]))
	else
		minRange = 0
	end
	
	if material then
        material = mw.text.trim( material )
    else
        error( '"material" argument not specified', 0 )
    end
	
	local range = nil
	
	if (maxRange ~= nil) then
		range = { minRange, maxRange, alwaysShowUnupgradeable=basics }
	elseif (minRange > 0) then
		range = { minRange, 10, alwaysShowUnupgradeable=basics }
	end
	
	return p._table(material, group, range)
end

function p._table(material, group, range)
	material = string.lower(material)
	group = mw.text.trim(string.lower(group))
	
	local items = SmithedItems.getItems({
		material = material,
		group = group,
		range = range
	})
	
	-- 'items' should contain a table.
	--  - If it matched any items, it contains data in the format:
	--     > { <item>, <smithed quantity> }
	--  - If group was 'sets', the data will be in the format:
	--     > { <set name>, 1, { { <set item 1>, <quantity> }, { <set item 2>, <quantity> }, ... } }
	
	local buffer = ''
	for i=1,#items do
		local status, response = pcall(Exchange._table, items[i][1])
		if (status) then
			buffer = buffer .. '\n' .. response
		elseif (string.find(response, "not found")) then
			-- ignore untradeable item
		else
			error(response) -- rethrow
		end
	end
	
	if (buffer == '') then
		return '<tr><td colspan="10" style="text-align:center;">\'\'Error: No results matching that filter.\'\'</td></tr>'
	end
	
	return buffer
end

return p