Module:Average drop value/Sandbox
Documentation for this module may be created at Module:Average drop value/Sandbox/doc
local curr = require('Module:Currency')._amount
local dpl = require('Module:DPLlua')
local geprices = mw.loadJsonData('Module:GEPrices/data.json')
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, lowqty, highqty, rarity, rolls, alchprice, options)
local avgqty = (tonumber(lowqty) + tonumber(highqty)) / 2
local rar_good, price
-- parse price
local i_lo = item:lower()
if 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 = geprices[item] or alchprice or 0
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 * avgqty * rarity * rolls
mw.log(string.format('item %s: %s * %s * %s * %s = %s', item, price, lowqty, 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.getDropData(mobs)
--used in [[Module:Bestiary]] too
local query = {}
local smw = {}
for i,v in ipairs(mobs) do
mw.log("dropData", v)
query = {
'[[Dropped from::'..v..']]',
'?Drop JSON',
limit = 500
}
smw = mw.smw.ask(query)
mw.logObject(smw)
if smw and smw ~= {} and smw ~='' then
break
end
end
return smw
end
function p.totalvalfromdata(data,itemOptions,category, categoryFilter, exclude, excludeFilter)
--used in [[Module:Bestiary]] too
if (not data) or data == {} or data =='' then
return '?'
end
local totalval = 0
local options = itemOptions or {}
for i,v in ipairs(data) do
local j = mw.text.jsonDecode(v['Drop JSON'] or '{}')
if (not category) or _DPLcontains(categoryFilter,j['Dropped item']) then
if (not exclude) or (not _DPLcontains(excludeFilter,j['Dropped item'])) then
totalval = totalval + calcValue(j['Dropped item'], j['Quantity Low'], j['Quantity High'], j['Rarity'], j['Rolls'], j['Drop Value'], options)
end
end
end
return totalval
end
function p.totalval(mobs, itemOptions, category, categoryFilter, exclude, excludeFilter)
local data = p.getDropData(mobs)
return p.totalvalfromdata(data,itemOptions,category, categoryFilter, exclude, excludeFilter)
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 mobroot = ''
local moblevel = ''
local mob = ''
local mobs = {}
--mobs serves as a list of fallbacks to get drop data, e.g. maybe smw does not find data for barbarian#level 8 because they are the generic barbarian drop, but will display the data from just barbarian if mobs is {'Barbarian#level 8', 'Barbarian'}
if args.mob or args[1] then
mob = args.mob or args[1]
mobroot, moblevel = mob:match('([^#]*)#?(.*)')
table.insert(mobs, mob)
table.insert(mobs, mobroot)
else
mob = pageName
mobroot = pageName
table.insert(mobs, pageName)
end
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 itemOptions = {
brimstone = args.brimstone,
ecumenical = args.ecumenical
}
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
local totalval = p.totalval(mobs, itemOptions, args.category, categoryFilter, args.exclude, excludeFilter)
if args.round and totalval ~= '?' then
totalval = math.floor(totalval)
end
if totalval == '?' then
return ''
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