Module:Sandbox/User:Jakesterwars/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:Jakesterwars/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)
	local rar_good, price
	-- parse price
	local i_lo = item:lower()
	if i_lo == 'coins' then
		price = 1
	elseif 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 string.match(i_lo, '#') ~= nil then
		local replaced = item:gsub('#', '')
		price = geprices[replaced]
	else
		price = geprices[item]
	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
	quantity = quantity:gsub(' ?%(noted%)', '')
	quantity = quantity:gsub(',', '')
	local _quantity = tonumber(quantity)
	if not _quantity then
		local q_1, q_2 = string.match(quantity, '(%d+)–(%d+)')
		q_1 = tonumber(q_1)
		q_2 = tonumber(q_2)
		if q_1 and q_2 then
			_quantity = (q_1+q_2)/2
		else
			mw.log('0 quantity for '..item)
			return 0
		end
	end
	quantity = _quantity
	
	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)
	local query = {
		'[[Drop from::'..mob..']] OR [[Reward from::'..mob..']]',
		'[[Dropped item::+]]',
		'?Dropped item#-',
		'?Drop Quantity',
		'?Rarity',
		'?Rolls',
		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'])
	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