Module:Average drop value/Sandbox: Difference between revisions
Jump to navigation
Jump to search
Content added Content deleted
No edit summary Tag: Reverted |
No edit summary Tag: Manual revert |
||
Line 1: | Line 1: | ||
local |
local curr = require('Module:Currency')._amount |
||
local dpl = require('Module:DPLlua') |
|||
local geprices = mw.loadJsonData('Module:GEPrices/data.json') |
|||
local p = {} |
local p = {} |
||
-- TODO move to helper function |
|||
function p.main(frame) |
|||
local a_an_arr = { |
|||
a = true, |
|||
local orRegions = args['or'] or 'No' |
|||
e = true, |
|||
i = true, |
|||
local regions = {'misthalin', 'karamja', 'asgarnia', 'desert', 'fremennik', 'kandarin', 'morytania', 'tirannwn', 'wilderness', 'kourend'} |
|||
o = true, |
|||
u = true |
|||
local ret = mw.html.create('tr') |
|||
} |
|||
mw.log(args[1]) |
|||
function a_an(x) |
|||
if args['incompletable'] == 'Yes' then |
|||
local _x = mw.text.truncate(string.lower(x), 1, '') |
|||
ret:tag('th'):wikitext(args[1]):addClass('table-bg-red') |
|||
if a_an_arr[_x] then |
|||
else ret:tag('th'):wikitext(args[1]) |
|||
return 'an' |
|||
end |
end |
||
return 'a' |
|||
mw.log(tostring(ret)) |
|||
end |
|||
local required = '' |
|||
local auto = '' |
|||
function calcValue(item, lowqty, highqty, rarity, rolls, alchprice, options) |
|||
for _, region in ipairs(regions) do |
|||
local avgqty = (tonumber(lowqty) + tonumber(highqty)) / 2 |
|||
if args[region] ~= nil then |
|||
local rar_good, price |
|||
if args[region] == 'Yes' then |
|||
-- parse price |
|||
if #required ~= 0 then |
|||
local i_lo = item:lower() |
|||
required = required .. '&' |
|||
if i_lo == 'brimstone key' and options.brimstone ~= nil then |
|||
end |
|||
price = tonumber(mw.getCurrentFrame():preprocess('{{KDTValue}}')) |
|||
required = required .. region |
|||
elseif i_lo == 'ecumenical key' then |
|||
if options.ecumenical ~= nil then |
|||
auto = auto .. tb(region) |
|||
price = 61500 |
|||
else |
|||
else |
|||
ret:tag('td'):wikitext(tb(region) .. ' ' .. args[region]) |
|||
price = 0 |
|||
end |
end |
||
end |
|||
if args['incompletable'] == 'Yes' then |
|||
ret:tag('td'):addClass('table-bg-red') |
|||
ret:tag('td'):addClass('table-bg-red') |
|||
else |
else |
||
price = geprices[item] or alchprice or 0 |
|||
ret:tag('td'):wikitext(auto) |
|||
ret:tag('td'):wikitext(tb(required)) |
|||
end |
end |
||
if |
if not price then |
||
mw.log('0 price for '..item) |
|||
if args['incompletable'] == 'Yes' then |
|||
return 0 |
|||
ret:tag('td'):wikitext(string.format('[[File:Yes check.svg|20px|link=|alt=Quest may be played without autocompletion]] Partially playable')):addClass('table-bg-red') |
|||
end |
|||
else ret:tag('td'):wikitext(string.format('[[File:Yes check.svg|20px|link=|alt=Quest may be played without autocompletion]] Fully playable')) |
|||
-- 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 |
end |
||
rarity = tonumber(rarity) |
|||
elseif args['playable'] == 'No' then |
|||
if |
if not rarity then |
||
mw.log('0 rarity for '..item) |
|||
ret:tag('td'):wikitext(string.format('[[File:X mark.svg|20px|link=|alt=Quest may not be played through]] Not completeable')):addClass('table-bg-red') |
|||
return 0 |
|||
else ret:tag('td'):wikitext(string.format('[[File:X mark.svg|20px|link=|alt=Quest may not be played through]] Autocomplete only')) |
|||
end |
end |
||
end |
|||
else |
|||
if args['incompletable'] == 'Yes' then |
|||
local val = price * avgqty * rarity * rolls |
|||
ret:tag('td'):wikitext(''):addClass('table-bg-red') |
|||
mw.log(string.format('item %s: %s * %s * %s * %s = %s', item, price, lowqty, rarity, rolls, val)) |
|||
else ret:tag('td'):wikitext('') |
|||
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 |
||
end |
end |
||
return smw |
|||
if args['incompletable'] == 'Yes' then |
|||
end |
|||
ret:tag('td'):wikitext(args['notes'] or ''):addClass('table-bg-red') |
|||
else ret:tag('td'):wikitext(args['notes'] or '') |
|||
function p.totalvalfromdata(data,itemOptions,category, categoryFilter, exclude, excludeFilter) |
|||
--used in [[Module:Bestiary]] too |
|||
if (not data) or data == {} or data =='' then |
|||
return '?' |
|||
end |
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) |
|||
return tostring(ret) |
|||
local data = p.getDropData(mobs) |
|||
return p.totalvalfromdata(data,itemOptions,category, categoryFilter, exclude, excludeFilter) |
|||
end |
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 |
return p |
Latest revision as of 11:22, 17 October 2024
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