Module:Sandbox/User:Radio53kip/dropslist

Documentation for this module may be created at Module:Sandbox/User:Radio53kip/dropslist/doc

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

local _comimg = '<span class="drops-combat" style="margin-left:0.3em;">[[File:Multicombat.png|link=Combat level|frameless|20px]]</span>'
local _thvimg = '<span class="drops-thieving" style="margin-left:0.3em;">[[File:Thieving-icon.png|link=Thieving|frameless|20px]]</span>'
local _hunimg = '<span class="drops-hunter" style="margin-left:0.3em;">[[File:Hunter-icon.png|link=Hunter|frameless|20px]]</span>'
local _farimg = '<span class="drops-farming" style="margin-left:0.3em;">[[File:Farming-icon.png|link=|frameless|20px]]</span>'
local _rewimg = '<span class="drops-reward" style="margin-left:0.3em;">[[File:Casket.png|link=|frameless|20px|Reward]]</span>'
local _arcimg = '<span class="drops-archaeology" style="margin-left:0.3em;">[[File:Archaeology-icon.png|link=|frameless|20px]]</span>'
local _fisimg = '<span class="drops-fishing" style="margin-left:0.3em;">[[File:Fishing-icon.png|link=|frameless|20px]]</span>'
local _minimg = '<span class="drops-mining" style="margin-left:0.3em;">[[File:Mining-icon.png|link=|frameless|20px]]</span>'
local _wdcimg = '<span class="drops-woodcutting" style="margin-left:0.3em;">[[File:Woodcutting-icon.png|link=|frameless|20px]]</span>'
local _divimg = '<span class="drops-divination" style="margin-left:0.3em;">[[File:Divination-icon.png|link=|frameless|20px]]</span>'

local rewimg = '<span class="drops-reward" style="margin-left:0.3em;">[[File:%s|link=|frameless|20px|%s]]</span>'
local rewsrcs = mw.loadData('Module:get drop info/rewards')

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.args)
end
function p.test(i)
	return p._main({item = i})
end
function p._main(args)
	local item = args.item or args[1]
	local smwitem
	if item then
		local cleanedName = item
		local dropVers = ''
		if item:match(' %(%d%)$') then
			cleanedName, dropVers = mw.ustring.match(item, '^(.-) (%(%d%))$')
		elseif item:match('%#') then
			cleanedName, dropVers = mw.ustring.match(item, '^(.-)%#([%w%s%(%)]+)$')
		end
		if dropVers ~= nil and dropVers ~= '' then
			smwitem = cleanedName .. '#' .. dropVers
		end
	else
		item = mw.title.getCurrentTitle().text
	end
	if not smwitem then
		smwitem = item
	end
	
	local difrdt = '[[Dropped item from RDT::'..smwitem..']]'
	local q = {
		'[[Dropped item::'..smwitem..']]',
		'?Dropped item text',
		'?Drop from#-',
		'?Loot from#-',
		'?Hunted from#-',
		'?Produce from#-',
		'?Excavated from#-',
		'?Fished from#-',
		'?Mined from#-',
		'?Cut from#-',
		'?Harvested from#-',
		'?Reward from#-',
		'?Name Notes',
		'?Drop Quantity',
		'?Quantity High',
		'?Quantity Low',
		'?Rarity',
		'?Rarity Notes',
		'?RarityNote Ref',
		'?Members',
		'?Drop from.All Is members only=mob All Is members only',
		'?Drop from.Is members only=mob Is members only',
		'?Drop from.Combat level=mob Combat level',
		'?Drop from.All Combat level=mob All Combat level',
		'?Loot from.Thieving level=mob Thieving level',
		'?Loot from.All Thieving level=mob All Thieving level',
		'?Hunted from.Hunter level=mob Hunter level',
		'?Hunted from.All Hunter level=mob All Hunter level',
		'?Produce from.Farming level=mob Farming level',
		'?Produce from.All Farming level=mob All Farming level',
		'?Archaeology level',
		'?Excavated from.Archaeology level=mob Archaeology level',
		'?Excavated from.All Archaeology level=mob All Archaeology level',
		'?Fished from.Fishing level=mob Fishing level',
		'?Fished from.All Fishing level=mob All Fishing level',
		'?Mined from.Mining level=mob Mining level',
		'?Mined from.All Mining level=mob All Mining level',
		'?Cut from.Woodcutting level=mob Woodcutting level',
		'?Cut from.All Woodcutting level=mob All Woodcutting level',
		'?Harvested from.Divination level=mob Divination level',
		'?Harvested from.All Divination level=mob All Divination level',
		limit = args.limit or 100,
		sort = args.sort,
		order = args.order
	}

	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')
				..".''[[Category:Empty drop lists]]"
	end
	
	local ret = {}
	local hasRefs = false
	if smwdata then
		for i,v in ipairs(smwdata) do
			if v['RarityNote Ref'] then
				hasRefs = true
			end
			for j,u in ipairs(makeLines(item, v) or {}) do
				table.insert(ret, u)
			end
		end
	end
	
	local t = mw.html.create('table')
	t	:addClass('wikitable sortable sticky-header item-drops autosort=4,a')
		:css('text-align', 'center')
		:tag('tr')
			:tag('th'):wikitext('Source'):done()
			:tag('th'):wikitext('Level'):done()
			:tag('th'):wikitext('Quantity'):done()
			:tag('th'):wikitext('Rarity'):addClass('drops-rarity-header'):done()
			:tag('th'):wikitext('Members'):done()
	for i,v in ipairs(ret) do
		t:node(v)
	end
	
	local text = {
		"<div class=\"seealso\">This list was created dynamically. For help, see [[Template:Dropping monsters list/FAQ|the FAQ]].</div>\n",
		"<div class=\"seealso\">To force an update of this list, click ", purge('dml-'..mw.uri.anchorEncode(item), 'here', 'span'), ".</div>\n",
		"<div class=\"seealso\">For an exhaustive list of all known sources for this item, see <span class='plainlinks'>[", tostring(mw.uri.fullUrl('Special:Ask', {q=q[1], po = '?Drop Quantity\n?Rarity', p = { mainlabel = 'Monster', format = 'table'}, limit = 500})), " here]</span> (<span class='plainlinks'>[", tostring(mw.uri.fullUrl('Special:Ask', {q=q[1]..' OR '..difrdt, po = '?Drop Quantity\n?Rarity', p = { mainlabel = 'Monster', format = 'table'}, limit = 500})), " include RDT]</span>).</div>\n",
		tostring(t)
	}
	
	if hasRefs then
		local refList = mw.getCurrentFrame():getParent():extensionTag( 'references', '', {group='dr'} )
		table.insert(text, refList)
	end
	
	return table.concat(text, '')

end

function makeLines(item, data)
	-- iterate through different drop from parameters, get variant names
	local srcs = {
		drop=data['Drop from'],loot=data['Loot from'],hunter=data['Hunted from'],
		produce=data['Produce from'],reward=data['Reward from'],archaeology=data['Excavated from'],
		fishing=data['Fished from'],mining=data['Mined from'],woodcutting=data['Cut from'],divination=data['Harvested from']
	}
	local srcsTable = {}
	for i,v in pairs(srcs) do
		if type(v) == 'string' then
			table.insert(srcsTable, {dtype=i,src=v})
		elseif type(v) == 'table' then
			for _,k in pairs(v) do
				table.insert(srcsTable, {dtype=i,src=k})
			end
		end
	end

	-- iterate through found variants, get stats for each
	local srcsData, srcsDataGr = {}, {}
	local _found = false
	for _,v in ipairs(srcsTable) do
		local members, level, levelsort, img
		members = data['Members'] or data['mob Is members only'] or data['mob All Is members only'] or '?'
		
		if v['dtype'] == 'reward' then
			level = 'N/A'
			levelsort = -1000
			img = _rewimg
			if rewsrcs[v['src']] then
				level = rewsrcs[v['src']][1]
				img = string.format(rewimg, rewsrcs[v['src']][2], v['src'] )
			end
		else
			local level_property
			if v['dtype'] == 'drop' then
				level_property = 'Combat level'
				img = _comimg
			elseif v['dtype'] == 'loot' then
				level_property = 'Thieving level'
				img = _thvimg
			elseif v['dtype'] == 'hunter' then
				level_property = 'Hunter level'
				img = _hunimg
			elseif v['dtype'] == 'produce' then
				level_property = 'Farming level'
				img = _farimg
			elseif v['dtype'] == 'archaeology' then
				level_property = 'Archaeology level'
				img = _arcimg
			elseif v['dtype'] == 'fishing' then
				level_property = 'Fishing level'
				img = _fisimg
			elseif v['dtype'] == 'mining' then
				level_property = 'Mining level'
				img = _minimg
			elseif v['dtype'] == 'woodcutting' then
				level_property = 'Woodcutting level'
				img = _wdcimg
			elseif v['dtype'] == 'divination' then
				level_property = 'Divination level'
				img = _divimg
			end
			
			level = data[level_property] or data['mob '..level_property] or data['mob All '..level_property] or item_propv(item, level_property) or '?'
			levelsort = data[level_property] or data['mob '..level_property] or data['mob All '..level_property] or item_propv(item, level_property) or -1
		end
		
		if type(level) == 'table' then
			levelsort = level[1]
			level = table.concat(level, ', ')
		end
		if v['dtype'] == 'loot' then
			levelsort = 100 - levelsort
		end
		if type(members) == 'table' then
			members = memberstring.both
		else
			members = memberstring[members] or memberstring['?']
		end
		local noteref = data['RarityNote Ref'] or false
		local cleanSrc = v['src']
		local splitSrc = mw.text.split(v['src'], '%#')
		if #splitSrc == 2 then
			splitSrc[2] = splitSrc[2]:gsub('_', ' ')
			cleanSrc = string.format('%s|%s <span class="beast-version">%s</span>', v['src'], splitSrc[1], splitSrc[2])
		end
		
		table.insert(srcsData, {src=cleanSrc,dtype=v['dtype'],mems=members,lvl=level,lvlsort=levelsort,lvlimg=img,ref=noteref})
	end

	local rows = {}
	-- iterate through found sources, create line for each
	for _, v in pairs(srcsData) do
		-- name,namenotes,combat,cbnotes,dtype,quantity,quantitynotes,rarity,raritynotes,members,membersnotes,raritynoteref
		local dropline = line({v['src'], data['Name Notes'], v['lvl'], v['lvlimg'], v['dtype'], data['Drop Quantity'], '', data['Rarity'], data['Rarity Notes'], v['mems'], '', v['ref']})
		table.insert(rows, dropline)
	end
	-- iterate through found groups, create line for each
	--[=[
	for i,v in pairs(srcsDataGr) do
		-- i = monster-dtype-members, v = table of version data tables
		if #v > 1 then
			-- more than 1 in group
			local vers, lvl_l, lvl_h, gmems = {}, 9999, 0, v[1]['mems']
			for _,k in ipairs(v) do
				local versLink = '[['..k['bmon']..k['link']..'|'..k['vers']..']]'
				vers[k['vers']] = versLink
				if tonumber(k['lvl']) then
					if k['lvl'] < lvl_l then lvl_l = k['lvl'] end
					if k['lvl'] > lvl_h then lvl_h = k['lvl'] end
				end
			end
			-- Create level display
			local glevel
			if lvl_h ~= 0 then
				if lvl_l == lvl_h then
					glevel = lvl_l
				else
					glevel = lvl_l..'-'..lvl_h
				end
			else
				glevel = v[1]['lvl']
			end
			-- Sort versions
			local keys, versSorted = {}, {}
			for  j in pairs(vers) do table.insert(keys, tonumber(j)) end
			table.sort(keys)
			for _,l in ipairs(keys) do table.insert(versSorted, vers[tostring(l)]) end
			-- Create name/types string
			local gname
			if string.match(v[1]['link'], '[lL]evel') then
				gname = '[['..v[1]['bmon']..']]'..' <span class="beast-version">Level: '..mw.text.listToText(versSorted, ', ', ', ')..'</span>'
			else
				gname = '[['..v[1]['bmon']..']]'..' <span class="beast-version">Version: '..mw.text.listToText(versSorted, ', ', ', ')..'</span>'
			end
			-- Generate line
			local dropline = line({gname, data['Name Notes'], glevel, v[1]['lvlimg'], v[1]['lvlsort'], data['Drop Quantity'], '', data['Rarity'], data['Rarity Notes'], v[1]['mems'], ''})
			table.insert(rows, dropline)
		else
			--only 1 in group
			local dropline = line({v[1]['src'], data['Name Notes'], v[1]['lvl'], v[1]['lvlimg'], v[1]['lvlsort'], data['Drop Quantity'], '', data['Rarity'], data['Rarity Notes'], v[1]['mems'], ''})

		end
	end
	--]=]

	return rows
end

function item_propv(name, prop)
	local q = {
		'[['..name..']]',
		'?'..prop
	}
	local smwdata = mw.smw.ask(q)
	if type(smwdata) == 'table' then
		for _,v in ipairs(smwdata) do
			if v[prop] then
				return v[prop]
			end
		end
	end
end

return p
-- </nowiki>