Module:Sandbox/User:Csp713/Get drop info

From WIDEVERSE Wiki
Jump to navigation Jump to search

Documentation for this module may be created at Module:Sandbox/User:Csp713/Get drop info/doc

-- <nowiki>
local line = require('Module:ItemDropsLine')._main
local yesno = require('Module:Yesno')
local purge = require('Module:Purge')._purge
local p = {}

local memberstring = {
	yes = 'yes',
	[true] = 'yes',
	['true'] = 'yes',
	no = 'no',
	[false] = 'no',
	['false'] = 'no',
	both = 'both',
	['?'] = '?',
	['n/a'] = 'n/a'
}

function p.main(frame)
	return p._main(frame:getParent().args)
end
function p.test(i)
	return p._main({item = i})
end
function p._main(args)
	local item = args.item or args[1] or mw.title.getCurrentTitle().text
	local difrdt = '[[Drops item from RDT::'..item..']]'
	local q = {
		'[[Drops item::'..item..']]',
		'?Drop JSON',
		'?All Combat level',
		'?All Is members only',
		limit = args.limit or 50,
		sort = args.sort,
		order = args.order
	}
	if item:find('[%(]') then
		q[1] = '[[Drops item variant::'..item..']]'
		difrdt = '[[Drops item variant from RDT::'..item..']]'
	end
	local smwdata = mw.smw.ask(q)
	
	if not smwdata then
		return ":''No drop sources found. To force an update, click "
				..purge('dml-'..mw.uri.anchorEncode(item), 'here', 'span')
				..".''"
				-- Comment out for sandbox
				--.."[[Category:Empty drop lists]]"
	end
	
	local ret = {}
	for i,v in ipairs(smwdata) do
		for j,u in ipairs(makeLine(item, v) or {}) do
			table.insert(ret, u)
		end
	end
	
	local t = mw.html.create('table')
	t	:addClass('wikitable sortable item-drops autosort=4,a')
		:css('text-align', 'center')
		:tag('tr')
			:tag('th'):wikitext('Source'):done()
			:tag('th'):wikitext('Combat level'):done()
			:tag('th'):wikitext('Quantity'):done()
			:tag('th'):wikitext('Rarity'):done()
			:tag('th'):wikitext('Members-only<br />monster?'):done()
	for i,v in ipairs(ret) do
		t:node(v)
	end
	
	local text = {
		":''This list was created dynamically. For help, see [[Template:Dropping monsters list/FAQ|the FAQ]].''\n",
		":''To force an update of this list, click ", purge('dml-'..mw.uri.anchorEncode(item), 'here', 'span'), ".''\n",
		":''For an exhaustive list of all known sources for this item, see <span class='plainlinks'>[", tostring(mw.uri.fullUrl('Special:Ask', {q=q[1]..' OR '..difrdt, limit=500})), " here]</span>.''\n",
		tostring(t)
	}
	
	return table.concat(text, '')
end

function makeLine(item, data)
	-- This template is fairly expensive to use, so it should be limited
	mw.incrementExpensiveFunctionCount()

	local monster = data[1]:gsub('%[%[:(.*)|.*%]%]', '%1')

	item = mw.text.decode(string.lower(item))
	local _found = false
	local ret = {}
	
	local dropJson = data['Drop JSON']
	if(type(dropJson) == 'string') then
		dropJson = {dropJson}
	end
	for i,v in ipairs(dropJson or {}) do
		local json_good, json = pcall(mw.text.jsonDecode, mw.text.decode(v))
		if json_good then
			if string.lower(json.name) == item then
				table.insert(ret, json)
				_found = true
			end
		end
	end
	
	if not _found then
		return nil
	end
	local combat = data['All Combat level'] or 'N/A'
	if type(combat) == 'table' then
		combat = table.concat(combat, ', ')
	end
	local members = data['All Is members only'] or '?'
	if type(members) == 'table' then
		members = memberstring.both
	else
		members = memberstring[members] or memberstring['?']
	end
	
	local rows = {}

	for _, v in pairs(ret) do
		local _min,_max = v.quantity[1],v.quantity[2]
		local quantity

		if tonumber(_min) == tonumber(_max) then
			quantity = _min
		else
			quantity = string.format('%s-%s',_min,_max)
		end
		
		local dropline = line({monster, '', combat, '', quantity, '', v.rarity, '', members, ''})
		
		table.insert(rows, dropline)
	end

	return rows
end

return p
-- </nowiki>