Module:Average drop value

From Old School RuneScape Wiki
Jump to: navigation, search
Module documentation
This documentation is transcluded from Template:No documentation/doc. [edit] [purge]
This module does not have any documentation. Please consider adding documentation at Module:Average drop value/doc. [edit]
Module:Average drop value's function main is invoked by Template:Average drop value.
Module:Average drop value requires Module:Currency.
Module:Average drop value requires Module:DPLlua.
Module:Average drop value loads data from Module:GEPrices/data.
Module:Average drop value transcludes Template:GDTValue using frame:preprocess().
Module:Average drop value transcludes Template:KDTValue using frame:preprocess().
Module:Average drop value transcludes Template:RDTValue using frame:preprocess().

local geprices = mw.loadData('Module:GEPrices/data')
local curr = require('Module:Currency')._amount
local dpl = require('Module:DPLlua')

local p = {}

-- TODO move to helper function
local a_an_arr = {
	a = true,
	e = true,
	i = true,
	o = true,
	u = true
}
function a_an(x)
	local _x = mw.text.truncate(string.lower(x), 1, '')
	if a_an_arr[_x] then
		return 'an'
	end
	return 'a'
end

function calcValue(item, quantity, rarity, rolls, geprice, alchprice, options)
	local rar_good, price
	-- parse price
	local i_lo = item:lower()
	if i_lo == 'rare drop table' then
		price = tonumber(mw.getCurrentFrame():preprocess('{{RDTValue}}'))
	elseif i_lo == 'gem drop table' then
		price = tonumber(mw.getCurrentFrame():preprocess('{{GDTValue}}'))
	elseif i_lo == 'brimstone key' and options.brimstone ~= nil then
		price = tonumber(mw.getCurrentFrame():preprocess('{{KDTValue}}'))
	elseif i_lo == 'ecumenical key' then
		if options.ecumenical ~= nil then
			price = 61500
		else
			price = 0
		end
	else
		price = geprice or alchprice or 0
		quantity = 1
	end
	if not price then
		mw.log('0 price for '..item)
		return 0
	end
	-- parse rarity
	if rarity:lower() == 'always' then
		rarity = 1
	else
		rarity = rarity:gsub(',', '')
		rar_good, rarity= pcall(mw.ext.ParserFunctions.expr, rarity)
		if not rar_good then
		mw.log('0 rarity for '..item)
			return 0
		end
		rarity = tonumber(rarity)
		if not rarity then
		mw.log('0 rarity for '..item)
			return 0
		end
	end
	
	local val = price * quantity * rarity * rolls
	mw.log(string.format('item %s: %s * %s * %s * %s = %s', item, price, quantity, rarity, rolls, val))
	return val
end

function _DPLcontains(arr,s)
	for k, v in ipairs(arr) do
		if v == s then return true end
	end
	return false
end

function p._getData(mob)
	local query = {
		'[[Drop from::'..mob..']] OR [[Reward from::'..mob..']] OR [[Hunted from::'..mob..']] OR [[Loot from::'..mob..']]',
		'[[Dropped item::+]]',
		'?Dropped item#-',
		'?Drop Quantity',
		'?Rarity',
		'?Rolls',
		'?Average Price',
		'?Average High Alch Price',
		limit = 500
	}
	local smw = mw.smw.ask(query)
	return smw
end

function p.main(frame)
	return p._main(frame, frame:getParent().args)
end

function p._main(frame, args)
	local pageName = mw.title.getCurrentTitle().text
	local mob = args.mob or args[1] or pageName
	local mobroot, moblevel = mob:match('([^#]*)#?(.*)')
	local mobname = mob
	
	if args.mobname then
		mobname = args.mobname
	elseif moblevel ~= "" then
		mobname = string.format('%s ( %s )',mobroot, moblevel)
	end
	
	if mobroot ~= pageName then 
		mobname = string.format('[[%s|%s]]',mobroot,mobname) 
	end
	
	local killname = args.killname or 'kill'
	local data = p._getData(mob)
	local itemOptions = {
		brimstone = args.brimstone,
		ecumenical = args.ecumenical
	}
	local totalval = 0
	local categoryFilter = {}
	local excludeFilter = {}
	local catReportString = ""
	if args.category then
		categoryFilter = dpl.ask({
			namespace = '',
			ignorecase = 'true',
			category = args.category,
			allowcachedresults = 'true'
		})
		catReportString = 'counting only '..(args.category:lower())..' '
	end
	if args.exclude then
		excludeFilter = dpl.ask({
			namespace = '',
			ignorecase = 'true',
			category = args.exclude,
			allowcachedresults = 'true'
		})
		catReportString = catReportString..'excluding all '..(args.exclude:lower())..' '
	end
	
	for i,v in ipairs(data) do
		if (not args.category) or _DPLcontains(categoryFilter,v['Dropped item']) then
			if (not args.exclude) or (not _DPLcontains(excludeFilter,v['Dropped item'])) then
				totalval = totalval + calcValue(v['Dropped item'], v['Drop Quantity'], v['Rarity'], v['Rolls'], v['Average Price'], v['Average High Alch Price'], itemOptions)
			end
		end
	end
	
	if args.round then
		totalval = math.floor(totalval)
	end
	
	if args.raw then
		return totalval
	elseif args.brimstone ~= nil then
		return string.format('The average %s %s while on a [[Konar]] task %sis worth %s.', mobname, killname, catReportString, curr(totalval, 'coins'))
	elseif args.ecumenical ~= nil then
		return string.format('The average %s %s while in the [[Wilderness God Wars Dungeon]] %sis worth %s.', mobname, killname, catReportString, curr(totalval, 'coins'))
	else
		return string.format('The average %s %s %sis worth %s.', mobname, killname, catReportString, curr(totalval, 'coins'))
	end
end


return p