Module:Sandbox/User:Gau Cho/Average drop value: Difference between revisions

From Illerai
Jump to navigation Jump to search
illerai>CookBot
m bot: change to mw.loadJsonData
 
m 1 revision imported
 
(No difference)

Latest revision as of 22:26, 2 November 2024

Documentation for this module may be created at Module:Sandbox/User:Gau Cho/Average drop value/doc

local geprices = mw.loadJsonData('Module:GEPrices/data.json')
local curr = require('Module:Currency')._amount

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)
	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}}'))
	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 p._getData(mob)
	mw.log('Testfghj')
	local query = {
		'[[Drop from::'..mob..']] OR [[Reward 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:getParent().args)
end

function p._main(args)
	local mob = args.mob or args[1] or mw.title.getCurrentTitle().text
	local mobname = mob
	if args.mobname then
		mobname = args.mobname
	elseif mob:find('#') then
		mobname = string.format('%s (%s)', mob:match('(.*)#(.*)'))
	end
	local killname = args.killname or 'kill'
	local data = p._getData(mob)
	local totalval = 0
	for i,v in ipairs(data) do
		totalval = totalval + calcValue(v['Dropped item'], v['Drop Quantity'], v['Rarity'], v['Rolls'], v['Average Price'], v['Average High Alch Price'])
	end
	if args.raw then
		return totalval
	end
	return string.format('The average %s %s is worth %s.', mobname, killname, curr(totalval, 'coins'))
end


return p