RuneScape Wiki
mNo edit summary
Tag: sourceedit
No edit summary
Line 5: Line 5:
 
function p.main(frame)
 
function p.main(frame)
 
-- This template is fairly expensive to use, so it should be limited
 
-- This template is fairly expensive to use, so it should be limited
mw.incrementExpensiveFunctionCount()
+
--mw.incrementExpensiveFunctionCount()
   
 
local args = frame:getParent().args
 
local args = frame:getParent().args

Revision as of 06:13, 8 March 2019

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

-- <pre>
local p = {}
local JSONS = require('Dev:Json')

function p.main(frame)
	-- This template is fairly expensive to use, so it should be limited
	--mw.incrementExpensiveFunctionCount()

	local args = frame:getParent().args

	local monster = string.gsub(args['?monster'] or args.monster or '','[][]','')
	monster = mw.text.decode(monster)

	local drop = string.lower(args.userparam or args.drop or '')
	drop = mw.text.decode(drop)

	-- escape lua characters
	local _drop = drop:gsub('([%^%$%(%)%%%.%[%]%*%+%-%?])','%%%1')

	local smwdata = frame:preprocess('{{'..
					'{{{|safesubst:}}}#show:'..
					monster..
				[[
					|?Drop JSON
					|?All Combat level=&&SPLITPOINT&&Combat level:
					|headers=plain
					|format=list
					}}
				]])

	smwdata = mw.text.decode(tostring(smwdata))
	local _found = false

	local json, cmb = unpack( mw.text.split(smwdata,'%(&&SPLITPOINT&&') )

	local json_good
	json_good, json = pcall(JSONS.decode,string.format('[ %s ]',json))

	if not json_good then
		return ''
	end

	cmb = string.format( '(%s',cmb or '?' )

	local ret = {}

	for _, v in pairs(json) do
		if drop == string.lower(v.name) then
			table.insert(ret,v)
			_found = true
		end
	end

	if not _found then
		return ''
	end

	local combat = mw.ustring.match(cmb:lower(),'%(combat level: (.-)%)') or 'N/A'
	combat = mw.ustring.gsub(combat,'(%d),(%d%d%d)','%1%2')
	local retString = {}

	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
			
		table.insert(retString,
				string.format('{{ItemDropsLine|Monster=%s|Combat=%s|Quantity=%s|Rarity=%s}}',
				monster,
				combat,
				quantity,
				v.rarity)
			)
	end

	return frame:preprocess(table.concat(retString,'\n'))
end

return p