Module:DropsLineRoatz: Difference between revisions

From Roat Pkz
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
-- <nowiki>
local p = {}
local p = {}


Line 5: Line 6:
local coins_image = require('Module:Coins image')
local coins_image = require('Module:Coins image')
local curr_image = require('Module:Currency Image')
local curr_image = require('Module:Currency Image')
local exchange = require('Module:Exchange')  
local exchange = require('Module:Exchange')
local yesno = require('Module:Yesno')
local yesno = require('Module:Yesno')
 
local VariablesLua = mw.ext.VariablesLua
local var = mw.ext.VariablesLua


-- precalculated cached data
-- precalculated cached data
local geprices_data = mw.loadJsonData('Module:GEPrices/data.json')
local droppeditem_data = mw.loadJsonData('Module:DropsLine/itemData.json')


local geprice = exchange._price
local f_gealch = exchange._highalch
local ptitle = mw.title.getCurrentTitle()
local ptitle = mw.title.getCurrentTitle()
local ns = ptitle.nsText
local ns = ptitle.nsText
local title = ptitle.fullText
local title = ptitle.fullText
local pgTitle = ptitle.text
local pgTitle = ptitle.text
local members_note = '&nbsp;<sub title="Members only" style="cursor:help; margin-left:3px;">(m)</sub>'


local _noted = '&nbsp;<span class="dropsline-noted">(noted)</span>'
local _noted = '&nbsp;<span class="dropsline-noted">(noted)</span>'


local coins_priceString = "%s Pkp"
local _priceStrings = {
local other_priceString = "%s Pkp"
coins = "%s %s",
standard = "%s %s each",
alch_alt = "%s %s each; this item has a distinct value, even though it cannot be alchemised.",
ge_alch = "%s %s each; this is the high alchemy value as this item cannot be traded on the Grand Exchange.",
ge_alt = "%s %s each; this item has a distinct value, even though it cannot be traded on the Grand Exchange or be alchemised."
}


--bg, txt, sort; acceptable non-quantity rarity names
local _altval = '<span class="dropsline-altval" style="margin-left:0.3em;">[[File:AltValue.png|link=|frameless|20px]]</span>'
local valueImages = {
alch_alt = _altval,
ge_alt = _altval,
ge_alch = '<span class="dropsline-gealch" style="margin-left:0.3em;">[[File:High_Level_Alchemy_icon.png|link=High Level Alchemy|frameless|20px]]</span>'
}
 
function getSMWAlch(item)
    local smw = mw.smw.ask({
        '[['..item..']]',
        '?High Alchemy value'
    })
    if smw and smw[1] then
        return smw[1]['High Alchemy value']
    end
    return nil
end
 
--bg, txt, sort
local rarities = {
local rarities = {
    always = { 'table-bg-blue', 1 },
always = { 'table-bg-blue', 1 },
    common = { 'table-bg-green', 16 },
common = { 'table-bg-green', 16 },
    uncommon = { 'table-bg-yellow', 64 },
uncommon = { 'table-bg-yellow', 64 },
    rare = { 'table-bg-orange', 128 },
rare = { 'table-bg-orange', 256 },
    ['very rare'] = { 'table-bg-red', 1024 },
['very rare'] = { 'table-bg-red', 1024 },
    random = { 'table-bg-pink', 4096 },
random = { 'table-bg-pink', 4096 },
    varies = { 'table-bg-pink', 4096 },
varies = { 'table-bg-pink', 4096 },
    once = { 'table-bg-pink', 65536 },
_default = { 'table-bg-grey', 65536 }
    conditional = { 'table-bg-pink', 65536 },
}
    _default = { 'table-bg-grey', 65536 }
 
-- arbitrary numbers
local rarities2 = {
{ 1, 'table-bg-blue' },
{ 1/16, 'table-bg-green' },
{ 1/64, 'table-bg-yellow' },
{ 1/256, 'table-bg-orange' },
{ 1/1024, 'table-bg-red' }
}
}


-- colour-code
-- Treasure Hunter gem icons
local rarities_class = {
local th_gem_icons = {
    { 1, 'table-bg-blue' },
white = '[[File:THGem-common.png|link=|frameless|20px|Common]]',
    { 1/25, 'table-bg-green' },
yellow = '[[File:THGem-fairly-common.png|link=|frameless|20px|Fairly common]]',
    { 1/99.99, 'table-bg-yellow' },
orange = '[[File:THGem-uncommon.png|link=|frameless|20px|Uncommon]]',
    { 1/999.99, 'table-bg-orange' },
red = '[[File:THGem-rare.png|link=|frameless|20px|Rare]]',
    { 1/9999999, 'table-bg-red' }
purple = '[[File:THGem-very-rare.png|link=|frameless|20px|Very rare]]',
shadow = '[[File:THGem-ultra-rare.png|link=|frameless|20px|Shadow]]',
['ultra-rare'] = '[[File:THGem-ultra-rare2.png|link=|frameless|20px|Ultra-Rare]]',
no = 'N/A',
}
 
-- Treasure Hunter gem rarities
local th_gem_rarities = {
white = 1,
yellow = 1/2,
orange = 1/3,
red = 1/4,
purple = 1/5,
shadow = 1/6,
['ultra-rare'] = 1/7,
no = 0,
}
 
-- Squeal of Fortune slot icons
local sof_slot_icons = {
common = '[[File:SoF_slot_common.png|link=|frameless|x31px|Common]]',
uncommon = '[[File:SoF_slot_uncommon.png|link=|frameless|x31px|Uncommon]]',
rare = '[[File:SoF_slot_rare.png|link=|frameless|x31px|Rare]]',
['super rare'] = '[[File:SoF_slot_super_rare.png|link=|frameless|x31px|Super rare]]',
no = 'N/A',
}
 
-- Squeal of Fortune slot rarities
local sof_slot_rarities = {
common = 1,
uncommon = 1/2,
rare = 1/3,
['super rare'] = 1/4,
no = 0,
}
}


function get_rarity_class(val)
function get_rarity_class(val)
    for i,v in ipairs(rarities_class) do
for i,v in ipairs(rarities2) do
        curr = v
curr = v
        if val >= v[1] then
if val >= v[1] then
            break
break
        end
end
    end
end
    return curr[2]
return curr[2]
end
end


function commas(n)
function commas(n)
    if tonumber(n) then
if tonumber(n) and n ~= 1/0 then
        return lang:formatNum(tonumber(n))
return lang:formatNum(tonumber(n))
    else
else
        return n
return n
    end
end
end
end


function expr(t)
function expr(t)
    local noerr, val = pcall(mw.ext.ParserFunctions.expr, t)
local noerr, val = pcall(mw.ext.ParserFunctions.expr, t)
    if noerr then
if noerr then
        return tonumber(val)
return tonumber(val)
    else
else
        return false
return false
    end
end
end
end
function sigfig(n, f)
function sigfig(n, f)
    f = math.floor(f-1)
f = math.floor(f-1)
    if n == 0 then return 0 end
if n == 0 then return 0 end
    local m = math.floor(math.log10(n))
local m = math.floor(math.log10(n))
    f = math.max(m, f)
local v = n / (10^(m-f))
    local v = n / (10^(m-f))
-- floor(x + 0.5) is standard rounding to one decimal place
    v = math.floor(v + 0.5) * 10^(m-f)
v = math.floor(v+0.5) * 10^(m-f)
    -- floor(x + 0.5) is standard rounding to one decimal place
return v
    return v
end
end
p.sigfig = sigfig
p.sigfig = sigfig
p.commas = commas
function sigfigalt(n)
if n >= 100 then
return math.floor(n+0.5)
else
return sigfig(n, 3)
end
end


function p.main(frame)
function p.main(frame)
    local args = frame:getParent().args
local args = frame:getParent().args
    local frameArgs = frame.args
local frameArgs = frame.args
 
-- Params and defaults
    -- Params and defaults
local name,namenotes,
    local name,namenotes,
quantity,quantitynotes,
        quantity,quantitynotes,
rarity,alt_rarity,alt_rarity_endash,raritynotes,
        rarity,rarity1,alt_rarity,alt_rarity_endash,
thgem,sofslot,convert,convertcur,memsover,
        raritynotes,citation,monVers = params.defaults{
altcur,version,altSource = params.defaults{
                    {args.name or args.Name,'Item'},
{args.name or args.Name,'Item'},
                    {args.namenotes or args.Namenotes,''},
{args.namenotes or args.Namenotes,''},
                    {args.quantity or args.Quantity,'Unknown'},
{args.quantity or args.Quantity,'Unknown'},
                    {args.quantitynotes or args.Quantitynotes,''},
{args.quantitynotes or args.Quantitynotes,''},
                    {args.rarity or args.Rarity,'Unknown'},
{args.rarity or args.Rarity,'Unknown'},
{args.rarity1 or args.Rarity1,'Unknown'},
{args.altrarity,''},
{args.rarity2 or args.Rarity2,'Unknown'},
{args.altraritydash,''},
{args.rarity3 or args.Rarity3,'Unknown'},
{args.raritynotes or args.Raritynotes,''},
                    {args.altrarity or args.AltRarity,''},
{args.thgem,''},
                    {args.altraritydash or args.AltRarityDash,''},
{args.sofslot,''},
                    {args.raritynotes or args.Raritynotes,''},
{args.convert,''},
                    {args.citation,''},
{args.convertcurrency,''},
                    {args.version or args.Version,''},
{args.members or args.Members,''},
        }
{args.altcurrency or args.AltCurrency,''},
        raritynotes = raritynotes .. citation
{args.version or args.Version, ""},
    local rolls = tonumber(args.rolls or args.Rolls) or false
{args.altsource or args.Altsource,'GazBot'},
    local rollstext = ''
}
    if rolls then
if altSource ~= "GazBot" then
        rollstext = rolls .. ' × '
pgTitle = altSource
    end
end
    local approx = yesno(args.approx or args.Approx or 'no', false)
local rolls = tonumber(args.rolls) or false
    local isCoins = name:lower() == 'coins'
local rollstext = ''
    local isNothing = name:lower() == 'nothing'
if rolls then
    local altname = params.default_to(args.alt or args.Alt,name)
rollstext = rolls .. ' × '
    local gemwname = params.default_to(args.gemwname,name)
end
    local _smwname = params.default_to(args.smwname,gemwname)
local approx = yesno(args.approx or 'no', false)
 
local isCoins = name:lower() == 'coins'
    -- Remove version number from potions, enchanted jewellery, waterskins etc for smw
local altname = params.default_to(args.alt or args.Alt,name)
    local cleanedName
local gemwname = params.default_to(args.gemwname,name)
    local dropVers = ''
local smwname = gemwname
    if _smwname:match(' ?%(%d%)$') then
--local raritynotes = args.raritynotes or args.Raritynotes or ''
        cleanedName, dropVers = mw.ustring.match(gemwname, '^(.-) ?(%(%d%))$')
-- Remove version number from potions, enchanted jewellery, waterskins etc for smw
    elseif _smwname:match(' ?%(p%+*%)$') then
if smwname:match(' %(%d%)$') then
        cleanedName, dropVers = mw.ustring.match(_smwname, '^(.-) ?(%(p%+*%))$')
local cleanedName, itemVersion = mw.ustring.match(smwname, '^(.-) (%(%d%))$')
    elseif _smwname:match('%#') then
smwname = cleanedName..'#'..itemVersion
        cleanedName, dropVers = mw.ustring.match(_smwname, '^(.-)%#([%w%s%(%)]+)$')
    else
        cleanedName = mw.ustring.gsub(_smwname, ' %(%d%)$', '')
     end
     end
    cleanedName = mw.text.trim(cleanedName)
    dropVers = mw.text.trim(dropVers)
local rarity_value
   
if rarities[rarity:lower()] then
    local smwname = cleanedName
rarity = params.ucflc(rarity)
    if dropVers ~= '' then
else
        -- get subobject instead
        smwname = cleanedName..'#'..dropVers
    end
   
    local useSmw = true
    if params.has_content(args.smw) then
        useSmw = args.smw:lower() ~= 'no'
    end
    if params.has_content(frameArgs.smw) then
        useSmw = frameArgs.smw:lower() ~= 'no'
    end
   
    local rarity_value
    if rarities[rarity:lower()] then
        rarity = params.ucflc(rarity)
    else
         rarity_value = rarity:gsub(',','') --temp place to put this without overriding rarity
         rarity_value = rarity:gsub(',','') --temp place to put this without overriding rarity
         local rv1, rv2 = string.match(rarity_value, '([%d%.]+)/([%d%.]+)')
         local rv1, rv2 = string.match(rarity_value, '([%d%.]+)/([%d%.]+)')
Line 165: Line 214:
         end
         end
     end
     end
 
local rarity1_value
    if rarities[rarity1:lower()] then
        rarity1 = params.ucflc(rarity1)
    else
        rarity1_value = rarity1:gsub(',','') --temp place to put this without overriding rarity
        local rv1, rv2 = string.match(rarity1_value, '([%d%.]+)/([%d%.]+)')
        if rv1 and rv2 then
            rarity1 = commas(rv1) .. '/' .. commas(rv2)
            rarity1_value = rv1/rv2
        else
            rarity1_value = expr(rarity)
        end
    end
 
     local alt_rarity_value
     local alt_rarity_value
     if rarities[alt_rarity:lower()] then
     if rarities[alt_rarity:lower()] then
Line 193: Line 228:
         end
         end
     end
     end
thgem = mw.ustring.lower(args.thgem or '')
sofslot = mw.ustring.lower(args.sofslot or '')
quantity = mw.ustring.lower(quantity)
    local alchInfo = nil
local geInfo = nil
    if isCoins then
    alchInfo = {value = 1, type = 'coins'}
    geInfo = {value = 1, type = 'coins'}
else
if droppeditem_data[smwname] ~= nil then
if droppeditem_data[smwname] ~= false then -- not alchable
alchInfo = {value = droppeditem_data[smwname], type = 'standard'}
end
else
local smwret = getSMWAlch(smwname)
if smwret ~= nil then
alchInfo = {value = smwret, type = 'standard'}
end
end


    quantity = mw.ustring.lower(quantity)
if alchInfo == nil and (args.altvalue or args.AltValue) then
    local gemw = yesno(args.gemw or 'yes', false)
alchInfo = {value = tonumber(args.altvalue or args.AltValue), type = 'alch_alt', currency = altcur}
    local alch = yesno(args.alch or 'yes', false)
end


    -- Test for existance of alch value
local price = exchange._price_simple(gemwname)
    local hasmwalch, smwalchval
if price ~= nil then -- price exists
    local valueInfo = {
geInfo = {value = price, type = 'standard'}
        alch = {
elseif args.altvalue or args.AltValue then
            has = false,
geInfo = {value = tonumber(args.altvalue or args.AltValue), type = 'ge_alt', currency = altcur}
            value = 0
elseif alchInfo then
        },
geInfo = {value = alchInfo.value, type = 'ge_alch'}
        ge = {
end
            has = false,
end
            value = 0
        }
    }
   


-- Check members or F2P
    local members = yesno(memsover, false)
-- Use 'File:<name>.png' if no image param
-- Use 'File:<image>' if image param; image param will include extension
-- Special catch for coins
local image,image_n
if isCoins then
image_n = coins_image(quantity)
else
image_n = params.default_to(args.image or args.Image, name .. '.png')
image_n = mw.ustring.gsub(image_n, '#.+$', '.png')
end
if image_n:lower() == 'no' or params.is_empty(args.name or args.Name) then
image = ''
else
image = mw.ustring.format('[[File:%s|link=%s|alt=%s: RS3 %s drops %s with rarity %s%s in quantity %s]]', image_n, name, image_n, title, name, rollstext, rarity, quantity)
end
-- this only affects the JSON
local useSmw = true
if params.has_content(args.nosmw) then
useSmw = false
end
if params.has_content(frameArgs.nosmw) then
useSmw = false
end
      
      
    local geprice_frombulk = geprices_data[gemwname]
-- Level for Fishing, Archaeology, Mining, Woodcutting and Divination
    if not (type(geprice_frombulk) == 'number' and geprice_frombulk > 0) then
local level = 0
         geprice_frombulk = nil
if (args.level or args.Level) and tonumber(args.level or args.Level, 10) then
level = tonumber(args.level or args.Level, 10)
end
local rdt = string.lower(args.rdt or '') == 'yes'
local hasRowwideVersion = false
local versionKey = 'DEFAULT'
    if params.has_content(frameArgs.version or frameArgs.Version) then
         -- versions applied to the entire table
versionKey = frameArgs.version or frameArgs.Version
     end
     end
 
     if params.has_content(version) then
       
        -- versions applied to this row
     if isNothing then
        versionKey = version
        gemw = false
         hasRowwideVersion = true
    elseif isCoins then
        -- coins override
        valueInfo = {
            alch = {
                has = true,
                value = 1
            },
            ge = {
                has = true,
                value = 1
            }
        }
    else
        -- find alch price
        if alch then
            -- first check cache for alch value
            if cached_alch ~= nil then
                valueInfo.alch = {
                    has = true,
                    value = cached_alch
                }
            elseif gemw then
                -- then check gemw for alch value
                local hasgealch, gealchval = pcall(f_gealch,gemwname)
                if hasgealch then
                    if gealchval > -1 then
                        valueInfo.alch = {
                            has = true,
                            value = tonumber(gealchval)
                        }
                    end
                end
            end
            if not valueInfo.alch.has then
                -- failed to find alch in GEMW or is on the no-ge list/override
                -- lookup in SMW
 
                if smwret and smwret.alch ~= nil then
                    -- alch is defined, use it
                    valueInfo.alch = {
                        has = true,
                        value = smwret.alch
                    }
                else
                    alch = false
                end
            end
         end
        -- find ge price
        if gemw then
            if geprice_frombulk ~= nil then
                valueInfo.ge = {
                    has = true,
                    value = geprice_frombulk
                }
            else
                gemw = false
            end
        end
     end
     end
 
   
     -- Use 'File:<name>.png' if no image param
     local dropType = frameArgs.dtype or 'combat'
     -- Use 'File:<image>' if image param; image param will include extension
      
     -- Special catch for coins
     -- SoF/TH convert currency
    local image,image_n
local convertcurrencyKey = 'Oddments'
    if isCoins then
if dropType == 'sof' then
        image_n = coins_image(quantity)
convertcurrencyKey = 'Coins'
     else
end
        image_n = params.default_to(args.image or args.Image, name .. '.png')
     if params.has_content(frameArgs.convertcurrency) then
         image_n = mw.ustring.gsub(image_n, '#.+$', '.png')
         -- currency applied to the entire table
convertcurrencyKey = frameArgs.convertcurrency
     end
     end
     if image_n:lower() == 'no' or params.is_empty(args.name or args.Name) then
     if params.has_content(convertcurrency) then
         image = ''
         -- currency applied to this row
    elseif isNothing then
         convertcurrencyKey = convertcurrency
        image = '[[File:Bank filler.png|link=Nothing|alt=This does not exist.]]'
    else
         image = mw.ustring.format('[[File:%s|alt=%s: %s drops %s with rarity %s%s in quantity %s]]', image_n, name, image_n, title, name, rollstext, rarity, quantity)
     end
     end
     -- this only affects the JSON
      
    local rdt = string.lower(args.rdt or '') == 'yes'
-- Table row
       
local ret =  p._main(name,
    -- Table row
altname,namenotes,
    local ret =  p._main(name,
quantity,quantitynotes,
            altname,namenotes,
rarity,rarity_value,alt_rarity,alt_rarity_endash,alt_rarity_value,
            quantity,quantitynotes,
raritynotes,thgem,sofslot,
            rarity,rarity_value,alt_rarity,alt_rarity_endash,alt_rarity_value,
convert,convertcurrencyKey,image,members,
            raritynotes,image,
alchInfo,geInfo,
            rarity1,rarity1_value,alt_rarity,alt_rarity_endash,alt_rarity_value,
dropType,versionKey,hasRowwideVersion,
            raritynotes,image,
level,approx,
            rarity2,rarity_value,alt_rarity,alt_rarity_endash,alt_rarity_value,
smwname,useSmw,rdt,rolls)
            raritynotes,image,
            rarity3,rarity_value,alt_rarity,alt_rarity_endash,alt_rarity_value,
            raritynotes,image,
            valueInfo,gemw,alch,alt,
            isCoins,
            isNothing,
            frameArgs,monVers,
            cleanedName,dropVers,smwname,
            rdt,useSmw,
            approx,rolls)


    -- categories for mainspace
-- categories for mainspace
    local cats = ''
local cats = ''
    local onMain = ns == '' or ns == 'RuneScape'
    if onMain and useSmw then
if ns == '' then
        cats = categories{name,quantity,rarity,rarity1}
cats = categories{name,quantity,rarity}
    end
end
    return ret..cats
return ret..cats
end
end


-- main function to generate the row
-- main function to generate the row
function p._main(name,
function p._main(name,altname,namenotes,
        altname,namenotes,
quantity,quantitynotes,
        quantity,quantitynotes,
rarity,rarity_value,alt_rarity,alt_rarity_endash,alt_rarity_value,
        rarity,rarity_value,alt_rarity,alt_rarity_endash,alt_rarity_value,
raritynotes,thgem,sofslot,
        raritynotes,image,
convert,convertcurrencyKey,image,members,
        rarity1,rarity1_value,alt_rarity,alt_rarity_endash,alt_rarity_value,
alchInfo,geInfo,
        raritynotes,image,
dropType,versionKey,hasRowwideVersion,
        rarity2,rarity_value,alt_rarity,alt_rarity_endash,alt_rarity_value,
level,approx,
        raritynotes,image,
smwname,useSmw,rdt,rolls)
        rarity3,rarity_value,alt_rarity,alt_rarity_endash,alt_rarity_value,
        raritynotes,image,
    -- GE value, alch value, quantity cell contents
        valueInfo,gemw,alch,alt,
local total, alchtotal, converttotal, vsort, vasort, vcsort, _h, _l
        isCoins,
    quantity, _h, _l = qty(quantity)
        isNothing,
    if geInfo then
        frameArgs,monVers,
    total, vsort = get_total(geInfo.value,_h,_l)
        cleanedName,dropVers,smwname,
     end
        rdt,useSmw,
     if alchInfo then
        approx, rolls)
    alchtotal, vasort = get_total(alchInfo.value,_h,_l)
 
     -- value sorts
     if type(vsort) ~= 'number' then
        vsort = 0
     end
     end
     if type(vasort) ~= 'number' then
local convertstr = 'each'
        vasort = 0
     if convert then
    converttotal, vcsort = get_total(convert,_h,_l)
    -- If there is no quantity range, assume the convert value is the total
    if _h == _l then
    converttotal, vcsort = get_total(convert,1,1)
    convertstr = 'total'
    end
     end
     end
   
    -- value sorts
if type(vsort) ~= 'number' then
vsort = 0
end
if type(vasort) ~= 'number' then
vasort = 0
end
if type(vcsort) ~= 'number' then
vcsort = 0
end


     -- quantity notes
     -- quantity notes
    if #quantitynotes > 3 then
if #quantitynotes > 3 then
        quantity = quantity..quantitynotes
quantity = quantity..quantitynotes
     end
     end
      
      
     -- rarity cell contents
     -- rarity cell contents
    local rare_class, rare_sort
local rare_class, rare_sort
    if rarity_value == undefined then
if rarity_value == undefined then
        rare_class, rare_sort = unpack(rarities[rarity:lower()] or rarities._default)
rare_class, rare_sort = unpack(rarities[rarity:lower()] or rarities._default)
    elseif rarity_value == false then
elseif rarity_value == false then
        rare_class, rare_sort = unpack(rarities._default)
rare_class, rare_sort = unpack(rarities._default)
    else
else
        rare_sort = 1/rarity_value
rare_sort = 1/rarity_value
        rare_class = get_rarity_class(rarity_value)
rare_class = get_rarity_class(rarity_value)
    end
end
    local rollstext = ''
local rollstext = ''
    local tilde = ''
if rolls then
    if approx then
rollstext = rolls .. ' × '
        tilde = '~'
if rarity_value ~= false then
    end
rare_sort = rare_sort / rolls
    local _r = rarity
rare_class = get_rarity_class(math.min(1/rare_sort,0.99))
end
end
local tilde = ''
if approx then
tilde = '~'
end
local _r = rarity
      
      
     -- monster versions
     -- monster versions
     local hasRowwideVersion = false
     if hasRowwideVersion then
    local tblVers = frameArgs.version
        -- setup reference for this row
    local versionKey = 'DEFAULT'
        -- reference name
    if params.has_content(tblVers) then
local cleanref = mw.ustring.gsub(
         -- versions applied to the entire table
mw.ustring.gsub(
      versionKey = tblVers
mw.ustring.lower(versionKey),
-- replace spaces with hyphens
"%s", "-"
),
-- remove all non-word characters
"[^%w%-]", ""
);
local refname = "autod-" .. cleanref;
 
         -- create and append the reference
raritynotes = raritynotes .. mw.getCurrentFrame():extensionTag{ name='ref', content = mw.ustring.format('Only dropped by %s version.', versionKey), args = { group='d', name = refname } };
     end
     end
     if params.has_content(monVers) then
      
        -- versions applied to this row
-- Table row creation
        versionKey = monVers
        hasRowwideVersion = true
    end
 
    local quantityClassOverride = isNothing and 'table-na' or nil
    -- Table row creation
     local ret = mw.html.create('tr')
     local ret = mw.html.create('tr')
             -- row-wide things
             -- row-wide things
             :css('text-align','center')
             :css('text-align','center')
             -- inventory image
             -- inventory image
            :tag('td')
:tag('td')
                :addClass('inventory-image')
:addClass('inventory-image')
                :wikitext(image)
:attr('data-sort-value',name)
:wikitext(image)
             :done()
             :done()
             -- item name
             -- item name
            :tag('td')
:tag('td')
                :css('text-align','left')
:css('text-align','left')
                :addClass('item-col')
:addClass('item-col')
                :wikitext(string.format('%s',name,altname,#namenotes > 3 and namenotes or ''))
:wikitext(string.format('[[%s|%s]]%s%s',name,altname,#namenotes > 3 and namenotes or '',members and members_note or ''))
             :done()
             :done()
            -- level
            if level>0 then
ret:tag('td')
:attr('data-sort-value',level)
:wikitext(level)
            ret:done()
            end
             -- quantity
             -- quantity
            :tag('td')
ret:tag('td')
                :addClass(quantityClassOverride)
:attr('data-sort-value',_h)
                :attr('data-sort-value',_h)
:wikitext(quantity)
                :wikitext(quantity)
             ret:done()
             :done()
      
      
     -- rarity
     -- rarity
    local rarity_cell = ret:tag('td')
local rarity_cell = ret:tag('td')
    local rarity_span = rarity_cell:tag('span')
local rarity_span = rarity_cell:tag('span')
    rarity_span:wikitext(rollstext .. tilde .. rarity)
rarity_span:wikitext(rollstext .. tilde .. rarity)
    rarity_cell:attr('data-sort-value',rare_sort)
rarity_cell:attr('data-sort-value',rare_sort)
                :addClass(rare_class)
:addClass(rare_class)
    if type(rarity_value) == 'number' then
local rarity_cell_title
        rarity_cell:attr('title', rollstext .. tilde .. string.format('%.3g%%', 100 * rarity_value))
if type(rarity_value) == 'number' then
        rarity_span:attr({
rarity_cell_title = rollstext .. tilde .. string.format('%.3g%%', 100 * rarity_value)
            ['data-drop-fraction'] = rollstext .. tilde .. rarity,
rarity_span:attr({
            ['data-drop-oneover'] = rollstext .. tilde .. '1/' .. commas(sigfig(1/rarity_value, 4)),
['data-drop-fraction'] = rollstext .. tilde .. commas(mw.text.split(rarity, '/')[1]) .. '/' .. commas(mw.text.split(rarity, '/')[2]),
            ['data-drop-percent'] = rollstext .. tilde .. sigfig(100 * rarity_value, 3),
['data-drop-oneover'] = rollstext .. tilde .. '1/' .. commas(sigfigalt(1/rarity_value)),
            ['data-drop-permil'] = rollstext .. tilde .. sigfig(1000 * rarity_value, 3),
['data-drop-percent'] = rollstext .. tilde .. sigfig(100 * rarity_value, 3),
            ['data-drop-permyriad'] = rollstext .. tilde .. sigfig(10000 * rarity_value, 3),
['data-drop-permil'] = rollstext .. tilde .. sigfig(1000 * rarity_value, 3),
        })
['data-drop-permyriad'] = rollstext .. tilde .. sigfig(10000 * rarity_value, 3),
    end
})
 
end
    -- rarity1
    local rarity_cell = ret:tag('td')
    local rarity_span = rarity_cell:tag('span')
    rarity_span:wikitext(rollstext .. tilde .. rarity)
    rarity_cell:attr('data-sort-value',rare_sort)
                :addClass(rare_class)
    if type(rarity_value) == 'number' then
        rarity_cell:attr('title', rollstext .. tilde .. string.format('%.3g%%', 100 * rarity_value))
        rarity_span:attr({
            ['data-drop-fraction'] = rollstext .. tilde .. rarity1,
            ['data-drop-oneover'] = rollstext .. tilde .. '1/' .. commas(sigfig(1/rarity1_value, 4)),
            ['data-drop-percent'] = rollstext .. tilde .. sigfig(100 * rarity1_value, 3),
            ['data-drop-permil'] = rollstext .. tilde .. sigfig(1000 * rarity1_value, 3),
            ['data-drop-permyriad'] = rollstext .. tilde .. sigfig(10000 * rarity1_value, 3),
        })
    end
 
   
     if alt_rarity ~= '' then
     if alt_rarity ~= '' then
local rarity_cell_sep
         if alt_rarity_endash  ~= '' then
         if alt_rarity_endash  ~= '' then
             rarity_cell:tag('span'):wikitext('–')
             rarity_cell_sep = '–';
         else
         else
             rarity_cell:tag('span'):wikitext('; ')
             rarity_cell_sep = '; ';
         end
         end
rarity_cell:tag('span'):wikitext(rarity_cell_sep);
         local alt_rarity_span = rarity_cell:tag('span')
         local alt_rarity_span = rarity_cell:tag('span')
         alt_rarity_span:wikitext(alt_rarity)
         alt_rarity_span:wikitext(alt_rarity)
         if type(alt_rarity_value) == 'number' then
         if type(alt_rarity_value) == 'number' then
rarity_cell_title = rarity_cell_title .. rarity_cell_sep .. string.format('%.3g%%', 100 * alt_rarity_value)
             alt_rarity_span:attr({
             alt_rarity_span:attr({
                 ['data-drop-fraction'] = alt_rarity,
                 ['data-drop-fraction'] = alt_rarity,
Line 471: Line 507:
         end
         end
     end
     end
rarity_cell:attr("title", rarity_cell_title);


    if #raritynotes > 3 then
if #raritynotes > 3 then
        rarity_cell:wikitext(raritynotes)
rarity_cell:wikitext(raritynotes)
     end
end
 
     -- Treasure Hunter gem
    if dropType == 'th' then
local th_gem_cell = ret:tag('td')
if thgem == 'no' then
th_gem_cell:addClass('table-na')
end
th_gem_cell:wikitext(th_gem_icons[thgem] or '')
th_gem_cell:attr('data-sort-value',th_gem_rarities[thgem] or 2)
-- Squeal of Fortune slot
elseif dropType == 'sof' then
local sof_slot_cell = ret:tag('td')
if sofslot == 'no' then
sof_slot_cell:addClass('table-na')
end
sof_slot_cell:wikitext(sof_slot_icons[sofslot] or '')
sof_slot_cell:attr('data-sort-value',sof_slot_rarities[sofslot] or 2)
end


     -- setup GE and alch cells
     -- setup GE and alch cells
     local ge_td = ret:tag('td')
     local ge_td = ret:tag('td')
      
     local alch_td = ret:tag('td')


     -- common attributes
     -- common attributes
     ge_td  :attr('data-sort-value',vsort)
     ge_td  :attr('data-sort-value',vsort)
    :addClass('ge-column')
    :addClass('ge-column')
    :css({
    :css({
        ['text-align'] = 'right',
        ['text-align'] = 'right',
        cursor = 'help'
        cursor = 'help'
    })
    })
 
    alch_td :attr('data-sort-value',vasort)
            :addClass('alch-column')
            :css({
                ['text-align'] = 'right',
                cursor = 'help'
            })


     local ge_td_title, ge_td_content, alch_td_title, alch_td_content
     local ge_td_title, ge_td_content, alch_td_title, alch_td_content
      --Cases for the GE, alch values, and isNothing handling
    --Cases for the GE and alch values
     if isNothing then
    local smwValue = 0
        ge_td_content = 'N/A'
     if geInfo then
        ge_td_title = 'This does not exist.'
    local currency_name = lang:plural(geInfo.value or 0, 'coin', 'coins')
        ge_td:addClass('table-na'):css('text-decoration', 'underline dotted')
    local currency_img = nil
        alch_td_content = 'N/A'
    if geInfo.currency then
        alch_td_title = 'This does not exist.'
    currency_img, currency_name = currency(geInfo.currency, geInfo.value, vsort)
        alch_td:addClass('table-na'):css('text-decoration', 'underline dotted')
    end
    elseif isCoins then
    ge_td_title = mw.ustring.format(_priceStrings[geInfo.type], commas(geInfo.value) or '', currency_name)
        local coinsStr = lang:plural(vsort, '', 's')
    ge_td_content = total .. (currency_img or valueImages[geInfo.type] or '')
        ge_td_title = mw.ustring.format(coins_priceString, total, coinsStr)
   
        ge_td_content = total
         if geInfo.type == 'ge_alt' or geInfo.type == 'ge_alch' then
         alch_td_title = mw.ustring.format(coins_priceString, total, coinsStr)
        smwValue = geInfo.value
         alch_td_content = total
         end
     else
     else
         if valueInfo.ge.has then
    ge_td_content = 'Not sold'
            ge_td_title = mw.ustring.format(other_priceString, commas(valueInfo.ge.value), lang:plural(valueInfo.ge.value, '', 's'))
         ge_td_title = 'This item cannot be traded on the Grand Exchange nor alchemised and has no applicable value to display.'
            ge_td_content = total
        ge_td:css('color', '#999')
        end
    end
        if valueInfo.alch.has then
   
            alch_td_title = mw.ustring.format(other_priceString, commas(valueInfo.alch.value), lang:plural(valueInfo.alch.value, '', 's'))
    if alchInfo then
            alch_td_content = alchtotal
    local currency_name = lang:plural(alchInfo.value or 0, 'coin', 'coins')
        end
    local currency_img = ''
          
    if alchInfo.currency then
        if ge_td_content == nil then
    currency_img, currency_name = currency(alchInfo.currency, alchInfo.value, vasort)
            ge_td_content = 'Not sold'
    end
            ge_td_title = 'This item cannot be traded on the Trading Post.'
    alch_td_title = mw.ustring.format(_priceStrings[alchInfo.type], commas(alchInfo.value) or '', currency_name)
            ge_td:addClass('table-na'):css('text-decoration', 'underline dotted')
    alch_td_content = alchtotal .. (currency_img or valueImages[alchInfo.type] or '')
        end
    else
 
         alch_td_content = 'Not alchemisable'
        alch_td_title = 'This item cannot be alchemised and has no applicable value to display.'
        alch_td:css('color', '#999')
     end
     end
     ge_td:wikitext(ge_td_content):attr('title', ge_td_title)
     ge_td:wikitext(ge_td_content):attr('title', ge_td_title)
    alch_td:wikitext(alch_td_content):attr('title', alch_td_title)


     -- SMW
     -- SoF/TH convert value
     local onMain = ns == '' or ns == 'RuneScape'
     if dropType == 'sof' or dropType == 'th' then
    local unrecognizedDropVersionCategory = ''
local convert_td = ret:tag('td')
    if onMain and useSmw and isNothing ~= true then
    convert_td:attr('data-sort-value',vcsort)
        local smw_sub = {}
            :addClass('convert-column')
        -- check if applies to all or only a version
            :css({
                ['text-align'] = 'right',
                cursor = 'help'
            })
    if params.has_content(convert) then
    if convert ~= 'no' then
    local convert_currency_name = 'coin'
    local convert_currency_img = ''
    if convertcurrencyKey then
    convert_currency_img, convert_currency_name = currency(convertcurrencyKey, convert, vcsort)
    end
    convert_td_title = mw.ustring.format("%s %s %s", commas(convert) or '', convert_currency_name, convertstr or '')
    convert_td_content = converttotal .. (convert_currency_img or '')
    else
        convert_td_content = 'Cannot be converted'
        convert_td_title = 'This item cannot be converted and has no applicable value to display.'
        convert_td:css('color', '#999')
    end
    convert_td:wikitext(convert_td_content):attr('title', convert_td_title)
    end
convert_td:attr('data-sort-value',convert or -1)
end


     
-- SMW
        dropFrom = pgTitle
if (ns == '') and useSmw then
    dropFrom = pgTitle
         if versionKey ~= 'DEFAULT' then
         if versionKey ~= 'DEFAULT' then
            dropFrom = pgTitle .. '#' .. versionKey
        dropFrom = pgTitle .. '#' .. versionKey
        end
        local droppedItemName = 'Dropped item'
        if rdt == true then
            droppedItemName = 'Dropped item from RDT'
         end
         end
          
          
-- check if applies to all or only a version
--add function to reduce image to File:Some name.png
local smwNameNote = mw.text.killMarkers(namenotes)
local smwQuantity = mw.text.killMarkers(quantity)
smwQuantity = smwQuantity:gsub('<span class="dropsline%-noted">', '')
smwQuantity = smwQuantity:gsub('</span>', '')
smwQuantity = smwQuantity:gsub(',', '')
smwQuantity = smwQuantity:gsub('&nbsp;', ' ')
smwQuantity = smwQuantity:gsub(';', ',')
local smwRarityNote = mw.text.killMarkers(raritynotes)
local smwRolls = rolls or 1
local subcount = 1
if VariablesLua.varexists( 'dropcount' ) then
subcount = VariablesLua.var( 'dropcount', 1 )
subcount = subcount + 1
VariablesLua.vardefine( 'dropcount', subcount)
else
VariablesLua.vardefine( 'dropcount', 1)
end
local subname = 'DROP_'..smwname..'_'..smwQuantity..'_'..rarity..'_'..subcount


        local smw_json = {
local droppedItemName = 'Dropped item'
            ['Dropped item']=smwname,
if rdt == true then
            ['Name Notes']=smwNameNote,
droppedItemName = 'Dropped item from RDT'
            ['Drop Quantity']=smwQuantity,
end
            ['Quantity High']=_h,
if level == 0 then
            ['Quantity Low']=_l,
level = mw.ext.VariablesLua.var(string.format("DropLevel_%s_%s", dropType, versionKey))
            Rarity=rarity,
end
            Rarity1=rarity1,
local smw_json = {
            ['Alt Rarity']=alt_rarity,
['Drop type'] = dropType,
            ['Alt Rarity Dash'] = alt_rarity_endash,
['Dropped from'] = dropFrom,
            ['Rarity Notes']=smwRarityNote,
[droppedItemName] = smwname,
            ['Rolls']=smwRolls,
['Drop level'] = level,
            ['Dropped from'] = dropFrom,
['Name Notes']=smwNameNote,
            ['Drop level'] = dropLevel,
['Drop Quantity']=smwQuantity,
            ['Drop type'] = dropType
['Quantity High']=_h,
        }
['Quantity Low']=_l,
        local smw_sub = {
Rarity=rarity,
            [droppedItemName] = smwname,
['Alt Rarity']=alt_rarity,
            ['Dropped from'] = dropFrom,
['Alt Rarity Dash'] = alt_rarity_endash,
            ["Drop JSON"] = mw.text.jsonEncode(smw_json)
['Rarity Notes']=smwRarityNote,
        }
['Rolls']=smwRolls,
    end
['Approx']=approx
   
}
    return tostring(ret) .. unrecognizedDropVersionCategory
if string.match(raritynotes, 'UNIQ%-%-ref') then
smw_json['RarityNote Ref'] = true
end
if string.match(quantity, 'UNIQ%-%-ref') then
smw_json['QuantityNote Ref'] = true
end
if smwValue > 0 then
smw_json['Drop Value'] = smwValue
end
if dropType == 'archaeology' then
if VariablesLua.var('dropsline_is_arch_soil') == 'true' then
smw_json['Is soil screening'] = 'true'
else
smw_json['Is soil screening'] = 'false'
end
end
local smw_sub = {
[droppedItemName] = smwname,
['Dropped from'] = dropFrom,
["Drop JSON"] = mw.text.jsonEncode(smw_json)
}
mw.smw.subobject(smw_sub, subname) -- drop is subobject of page
end
return tostring(ret)
end
end


function qty(quantity, isNothing)
function qty(quantity)
    -- if no quantity is given, return unknown
-- if no quantity is given, return unknown
    if string.lower(quantity) == 'varies' then
if string.lower(quantity) == 'varies' then
        return 'Varies'
return 'Varies'
    elseif isNothing then
end
        return 'N/A'
if not quantity or string.lower(quantity) == 'unknown' then
    elseif not quantity or string.lower(quantity) == 'unknown' then
return 'Unknown'
        return 'Unknown'
end
    end
-- en dashes are the proper dash for number ranges
    -- en dashes are the proper dash for number ranges
-- replace all hyphens and em dashes with en
    -- replace all hyphens and em dashes with en
-- strip *all* whitespace
    -- strip *all* whitespace
-- change '(noted)' to '$n' for parsing
    -- change '(noted)' to '$n' for parsing
quantity = mw.ustring.gsub(quantity,'[-—]','–')
    quantity = mw.ustring.gsub(quantity,'[-—]','–')
:gsub('%s','')
        :gsub('%s','')
:gsub('%(noted%)','$n')
        :gsub('%(noted%)','$n')
-- split list into table
    -- split list into table
local vals = mw.text.split(quantity,'[,;]')
    local vals = mw.text.split(quantity,'[,;]')
local low = 2147483648
    local low = 2147483648
local high = 0
    local high = 0
-- recreate the quantity string to ensure consistent formatting
    -- recreate the quantity string to ensure consistent formatting
local numstr = {}
    local numstr = {}
for i, v in ipairs(vals) do
    for i, v in ipairs(vals) do
local clean = v:gsub('$n','')
        local clean = v:gsub('$n','')
-- if list element contains an en dash (indicating range)
        -- if list element contains an en dash (indicating range)
-- Find the smaller/larger number (just in case)
        -- Find the smaller/larger number (just in case)
-- Compare them to the current min/max
        -- Compare them to the current min/max
-- put them in order with desired format
        -- put them in order with desired format
if mw.ustring.find(v,'–') then
        if mw.ustring.find(v,'–') then
local splitvals = mw.text.split(clean,'–')
            local splitvals = mw.text.split(clean,'–')
-- assume a is smaller, b is larger
            -- assume a is smaller, b is larger
local a = tonumber(splitvals[1])
            local a = tonumber(splitvals[1])
local b = tonumber(splitvals[2])
            local b = tonumber(splitvals[2])
-- Just in case
            -- Just in case
if a > b then
            if a > b then
a,b = b,a
                a,b = b,a
end
            end
if a < low then
            if a < low then
low = a
                low = a
end
            end
if b > high then
            if b > high then
high = b
                high = b
end
            end
local addx = commas(a)..'–'..commas(b)
            local addx = commas(a)..'–'..commas(b)
if v:find('$n') then
            if v:find('$n') then
addx = addx.._noted
                addx = addx.._noted
end
            end
table.insert(numstr,addx)
            table.insert(numstr,addx)
else
        else
local a = tonumber(clean)
            local a = tonumber(clean)
if a < low then
            if a < low then
low = a
                low = a
end
            end
if a > high then  
            if a > high then  
high = a
                high = a
end
            end
local addx = commas(a)
            local addx = commas(a)
if v:find('$n') then
            if v:find('$n') then
addx = addx.._noted
                addx = addx.._noted
end
            end
table.insert(numstr,addx)
            table.insert(numstr,addx)
end
        end
end
    end
-- Add a line break if there are too many elements
    -- Add a line break if there are too many elements
-- To keep the tables thin
    -- To keep the tables thin
if #numstr > 11 then
    if #numstr > 11 then
local mid = math.floor(#numstr/2)
        local mid = math.floor(#numstr/2)
numstr[mid] = '<br/>'..numstr[mid]
        numstr[mid] = '<br/>'..numstr[mid]
end
    end
-- To prevent any possible confusion with formatted numbers
    -- To prevent any possible confusion with formatted numbers
-- elements should be separated with semicolons followed by a space
    -- elements should be separated with semicolons followed by a space
numstr = table.concat(numstr,'; ')
    numstr = table.concat(numstr,'; ')
-- If no numbers are found in the string, return unknown
    -- If no numbers are found in the string, return unknown
if not numstr:find('%d') then
    if not numstr:find('%d') then
return 'Unknown', price
        return 'Unknown', price
end
    end


    return numstr, high, low
return numstr, high, low
end
end


function get_total(value,qhigh,qlow)
function get_total(value,qhigh,qlow)
    -- if no alch value is given, return unknown
-- if no alch value is given, return unknown
    if not value or string.lower(value) == 'unknown' then
if not value or string.lower(value) == 'unknown' then
        return value
return value
    end
end
    -- if value is negative (from smw/ge) it cannot be alched
-- if value is negative (from smw/ge) it cannot be alched
    if tonumber(value) and tonumber(value) < 0 then
if tonumber(value) and tonumber(value) < 0 then
        return false
return false
    end
end
    -- if no numbers return not alchemisable
-- if no numbers return not alchemisable
    if not tonumber(value) and not value:find('%d') then
if not tonumber(value) and not value:find('%d') then
        return false
return false
    end
end
 
-- en dashes are the proper dash for number ranges
-- replace all hyphens and em dashes with en
-- strip *all* whitespace
value = mw.ustring.gsub(value,'[-—]','–')
:gsub('%s','')
-- split list into table
local vals = mw.text.split(value,'[,;]')
-- All value ranges will be a range
-- e.g. if items valued at 100 coins are dropped in quantities of 1, 3, 5
-- the value returned will be 100–500 rather than 100; 300; 500
-- If low and high vars are the same in the end, only 1 value is displayed
local low = 2147483649147483648
local high = 0
-- recreate the alchval string to ensure consistent formatting
for i, v in ipairs(vals) do
local clean = v:gsub('$n','')
-- if list element contains an en dash (indicating range)
-- Find the smaller/larger number (just in case)
-- Compare them to the current min/max
-- put them in order with desired format
if mw.ustring.find(v,'–') then
local splitvals = mw.text.split(clean,'–')
-- assume a is smaller, b is larger
local a = tonumber(splitvals[1])
local b = tonumber(splitvals[2])
-- Just in case
if a > b then
a,b = b,a
end
if a < low then
low = a
end
if b > high then
high = b
end
else
local a = tonumber(clean)
if a < low then
low = a
end
if a > high then
high = a
end
end
end


    -- en dashes are the proper dash for number ranges
local valret, sort
    -- replace all hyphens and em dashes with en
if not qhigh or not qlow then
    -- strip *all* whitespace
sort = high
    value = mw.ustring.gsub(value,'[-—]','–')
valret = commas(high)
        :gsub('%s','')
else
    -- split list into table
local lower = qlow * low
    local vals = mw.text.split(value,'[,;]')
local higher = qhigh * high
    -- All value ranges will be a range
if higher == lower then
    -- e.g. if items valued at 100 coins are dropped in quantities of 1, 3, 5
valret = commas(higher)
    -- the value returned will be 100–500 rather than 100; 300; 500
else
    -- If low and high vars are the same in the end, only 1 value is displayed
valret = commas(lower)..'–'..commas(higher)
    local low = 2147483648
end
    local high = 0
sort = higher
    -- recreate the alchval string to ensure consistent formatting
end
    for i, v in ipairs(vals) do
        local clean = v:gsub('$n','')
        -- if list element contains an en dash (indicating range)
        -- Find the smaller/larger number (just in case)
        -- Compare them to the current min/max
        -- put them in order with desired format
        if mw.ustring.find(v,'–') then
            local splitvals = mw.text.split(clean,'–')
            -- assume a is smaller, b is larger
            local a = tonumber(splitvals[1])
            local b = tonumber(splitvals[2])
            -- Just in case
            if a > b then
                a,b = b,a
            end
            if a < low then
                low = a
            end
            if b > high then
                high = b
            end
        else
            local a = tonumber(clean)
            if a < low then
                low = a
            end
            if a > high then
                high = a
            end
        end
    end


    local valret, sort, avg
return valret, sort
    if not qhigh or not qlow then
end
        sort = high
        avg = high
        valret = commas(high)
    else
        local lower = qlow * low
        local higher = qhigh * high
        if higher == lower then
            valret = commas(higher)
            avg = higher
        else
            valret = commas(lower)..'–'..commas(higher)
            avg = (lower+higher)/2
        end
        sort = higher
    end


    return valret, sort, avg
-- function to get the currency image and name (singular vs plural)
function currency(altcur,price,total)
price = tostring(price)
total = tostring(total)
-- body
local lowcur = string.lower(altcur)
local clean = price:gsub('%W','')
local retcur, img
if lang:plural(tonumber(clean) or 0, 'f', 't') == 't' then
retcur = altcur
else
if lowcur == 'zemomark' or lowcur == 'tokkul' or lowcur == 'teci' then
retcur = altcur
elseif lowcur == 'pieces of eight' then
retcur = 'piece of eight'
else
retcur = string.sub(altcur,1,(string.len(altcur)-1))
end
end
img = curr_image(altcur,total) or 'AltValue.png'
img = '<span class="dropsline-altval style="margin-left:0.3em;">[[File:'..img..'|link=|frameless|20px]]</span>'
return img, retcur
end
end


-- adding categories to mainspace
-- adding categories to mainspace
function categories(...)
function categories(...)
    local name,quantity,rarity = unpack(...)
local name,quantity,rarity = unpack(...)
    local ret = ''
local ret = ''
    name = name:lower()
name = name:lower()
    quantity = quantity:lower()
quantity = quantity:lower()
    if name:find('clue scroll') then
if name:find('effigy') then
        ret = ret .. '[[Category:Monsters that drop clues]]'
ret = ret .. '[[Category:Effigy dropping monsters]]'
    end
elseif name:find('clue scroll ') then
    if rarity == nil or rarity == '' or rarity:lower() == 'unknown' then
ret = ret .. '[[Category:Clue scroll dropping monsters]]'
        ret = ret .. '[[Category:Needs drop rarity added]]'
elseif name:find('rare drop table') then
    end
ret = ret .. '[[Category:Monsters with access to the rare drop table]]'
    if quantity:find('Unknown') then
elseif name:find('wilderness shared loot table') then
        ret = ret .. '[[Category:Needs drop quantity added]]'
ret = ret .. '[[Category:Monsters with access to the Wilderness shared loot table]]'
    end
end
    return ret
if rarity == nil or rarity == '' or rarity:lower() == 'unknown' then
ret = ret .. '[[Category:Needs drop rarity added]]'
end
if quantity:find('Unknown') then
ret = ret .. '[[Category:Needs drop quantity added]]'
end
return ret
end
end


return p
return p
-- </nowiki>

Revision as of 22:05, 2 April 2024

Documentation for this module may be created at Module:DropsLineRoatz/doc

-- <nowiki>
local p = {}

local params = require('Module:Paramtest')
local lang = mw.language.getContentLanguage()
local coins_image = require('Module:Coins image')
local curr_image = require('Module:Currency Image')
local exchange = require('Module:Exchange')
local yesno = require('Module:Yesno')
local VariablesLua = mw.ext.VariablesLua

-- precalculated cached data
local droppeditem_data = mw.loadJsonData('Module:DropsLine/itemData.json')

local ptitle = mw.title.getCurrentTitle()
local ns = ptitle.nsText
local title = ptitle.fullText
local pgTitle = ptitle.text

local members_note = '&nbsp;<sub title="Members only" style="cursor:help; margin-left:3px;">(m)</sub>'

local _noted = '&nbsp;<span class="dropsline-noted">(noted)</span>'

local _priceStrings = {
	coins = "%s %s",
	standard = "%s %s each",
	alch_alt = "%s %s each; this item has a distinct value, even though it cannot be alchemised.",
	ge_alch = "%s %s each; this is the high alchemy value as this item cannot be traded on the Grand Exchange.",
	ge_alt = "%s %s each; this item has a distinct value, even though it cannot be traded on the Grand Exchange or be alchemised."
}

local _altval = '<span class="dropsline-altval" style="margin-left:0.3em;">[[File:AltValue.png|link=|frameless|20px]]</span>'
local valueImages = {
	alch_alt = _altval,
	ge_alt = _altval,
	ge_alch = '<span class="dropsline-gealch" style="margin-left:0.3em;">[[File:High_Level_Alchemy_icon.png|link=High Level Alchemy|frameless|20px]]</span>'
}

function getSMWAlch(item)
    local smw = mw.smw.ask({
        '[['..item..']]',
        '?High Alchemy value'
    })
    if smw and smw[1] then
        return smw[1]['High Alchemy value']
    end
    return nil
end

--bg, txt, sort
local rarities = {
	always = { 'table-bg-blue', 1 },
	common = { 'table-bg-green', 16 },
	uncommon = { 'table-bg-yellow', 64 },
	rare = { 'table-bg-orange', 256 },
	['very rare'] = { 'table-bg-red', 1024 },
	random = { 'table-bg-pink', 4096 },
	varies = { 'table-bg-pink', 4096 },
	_default = { 'table-bg-grey', 65536 }
}

-- arbitrary numbers
local rarities2 = {
	{ 1, 'table-bg-blue' },
	{ 1/16, 'table-bg-green' },
	{ 1/64, 'table-bg-yellow' },
	{ 1/256, 'table-bg-orange' },
	{ 1/1024, 'table-bg-red' }
}

-- Treasure Hunter gem icons
local th_gem_icons = {
	white = '[[File:THGem-common.png|link=|frameless|20px|Common]]',
	yellow = '[[File:THGem-fairly-common.png|link=|frameless|20px|Fairly common]]',
	orange = '[[File:THGem-uncommon.png|link=|frameless|20px|Uncommon]]',
	red = '[[File:THGem-rare.png|link=|frameless|20px|Rare]]',
	purple = '[[File:THGem-very-rare.png|link=|frameless|20px|Very rare]]',
	shadow = '[[File:THGem-ultra-rare.png|link=|frameless|20px|Shadow]]',
	['ultra-rare'] = '[[File:THGem-ultra-rare2.png|link=|frameless|20px|Ultra-Rare]]',
	no = 'N/A',
}

-- Treasure Hunter gem rarities
local th_gem_rarities = {
	white = 1,
	yellow = 1/2,
	orange = 1/3,
	red = 1/4,
	purple = 1/5,
	shadow = 1/6,
	['ultra-rare'] = 1/7,
	no = 0,
}

-- Squeal of Fortune slot icons
local sof_slot_icons = {
	common = '[[File:SoF_slot_common.png|link=|frameless|x31px|Common]]',
	uncommon = '[[File:SoF_slot_uncommon.png|link=|frameless|x31px|Uncommon]]',
	rare = '[[File:SoF_slot_rare.png|link=|frameless|x31px|Rare]]',
	['super rare'] = '[[File:SoF_slot_super_rare.png|link=|frameless|x31px|Super rare]]',
	no = 'N/A',
}

-- Squeal of Fortune slot rarities
local sof_slot_rarities = {
	common = 1,
	uncommon = 1/2,
	rare = 1/3,
	['super rare'] = 1/4,
	no = 0,
}

function get_rarity_class(val)
	for i,v in ipairs(rarities2) do
		curr = v
		if val >= v[1] then
			break
		end
	end
	return curr[2]
end

function commas(n)
	if tonumber(n) and n ~= 1/0 then
		return lang:formatNum(tonumber(n))
	else
		return n
	end
end

function expr(t)
	local noerr, val = pcall(mw.ext.ParserFunctions.expr, t)
	if noerr then
		return tonumber(val)
	else
		return false
	end
end
function sigfig(n, f)
	f = math.floor(f-1)
	if n == 0 then return 0 end
	local m = math.floor(math.log10(n))
	local v = n / (10^(m-f))
	-- floor(x + 0.5) is standard rounding to one decimal place
	v = math.floor(v+0.5) * 10^(m-f)
	return v
end
p.sigfig = sigfig
function sigfigalt(n)
	if n >= 100 then
		return math.floor(n+0.5)
	else
		return sigfig(n, 3)
	end
end

function p.main(frame)
	local args = frame:getParent().args
	local frameArgs = frame.args
	-- Params and defaults
	local name,namenotes,
		quantity,quantitynotes,
		rarity,alt_rarity,alt_rarity_endash,raritynotes,
		thgem,sofslot,convert,convertcur,memsover,
		altcur,version,altSource = params.defaults{
					{args.name or args.Name,'Item'},
					{args.namenotes or args.Namenotes,''},
					{args.quantity or args.Quantity,'Unknown'},
					{args.quantitynotes or args.Quantitynotes,''},
					{args.rarity or args.Rarity,'Unknown'},
					{args.altrarity,''},
					{args.altraritydash,''},
					{args.raritynotes or args.Raritynotes,''},
					{args.thgem,''},
					{args.sofslot,''},
					{args.convert,''},
					{args.convertcurrency,''},
					{args.members or args.Members,''},
					{args.altcurrency or args.AltCurrency,''},
					{args.version or args.Version, ""},
					{args.altsource or args.Altsource,'GazBot'},
				}
	if altSource ~= "GazBot" then
		pgTitle = altSource
	end
	local rolls = tonumber(args.rolls) or false
	local rollstext = ''
	if rolls then
		rollstext = rolls .. ' × '
	end
	local approx = yesno(args.approx or 'no', false)
	local isCoins = name:lower() == 'coins'
	local altname = params.default_to(args.alt or args.Alt,name)
	local gemwname = params.default_to(args.gemwname,name)
	local smwname = gemwname
	--local raritynotes = args.raritynotes or args.Raritynotes or ''
	-- Remove version number from potions, enchanted jewellery, waterskins etc for smw
	if smwname:match(' %(%d%)$') then
		local cleanedName, itemVersion = mw.ustring.match(smwname, '^(.-) (%(%d%))$')
		smwname = cleanedName..'#'..itemVersion
    end
	
	local rarity_value
	if rarities[rarity:lower()] then
		rarity = params.ucflc(rarity)
	else
        rarity_value = rarity:gsub(',','') --temp place to put this without overriding rarity
        local rv1, rv2 = string.match(rarity_value, '([%d%.]+)/([%d%.]+)')
        if rv1 and rv2 then
            rarity = commas(rv1) .. '/' .. commas(rv2)
            rarity_value = rv1/rv2
        else
            rarity_value = expr(rarity)
        end
    end
	
    local alt_rarity_value
    if rarities[alt_rarity:lower()] then
        alt_rarity = params.ucflc(alt_rarity)
    else
        alt_rarity_value = alt_rarity:gsub(',','') --temp place to put this without overriding rarity
        local rv1, rv2 = string.match(alt_rarity_value, '([%d%.]+)/([%d%.]+)')
        if rv1 and rv2 then
            alt_rarity = commas(rv1) .. '/' .. commas(rv2)
            alt_rarity_value = rv1/rv2
        else
            alt_rarity_value = expr(alt_rarity)
        end
    end
	
	thgem = mw.ustring.lower(args.thgem or '')
	sofslot = mw.ustring.lower(args.sofslot or '')

	quantity = mw.ustring.lower(quantity)

    local alchInfo = nil
	local geInfo = nil
	
    if isCoins then
    	alchInfo = {value = 1, type = 'coins'}
    	geInfo = {value = 1, type = 'coins'}
	else
		if droppeditem_data[smwname] ~= nil then
			if droppeditem_data[smwname] ~= false then -- not alchable
				alchInfo = {value = droppeditem_data[smwname], type = 'standard'}
			end
		else
			local smwret = getSMWAlch(smwname)
			if smwret ~= nil then
				alchInfo = {value = smwret, type = 'standard'}
			end
		end

		if alchInfo == nil and (args.altvalue or args.AltValue) then
			alchInfo = {value = tonumber(args.altvalue or args.AltValue), type = 'alch_alt', currency = altcur}
		end

		local price = exchange._price_simple(gemwname)
		if price ~= nil then -- price exists
			geInfo = {value = price, type = 'standard'}
		elseif args.altvalue or args.AltValue then
			geInfo = {value = tonumber(args.altvalue or args.AltValue), type = 'ge_alt', currency = altcur}
		elseif alchInfo then
			geInfo = {value = alchInfo.value, type = 'ge_alch'}
		end
	end

	-- Check members or F2P
    local members = yesno(memsover, false)
	-- Use 'File:<name>.png' if no image param
	-- Use 'File:<image>' if image param; image param will include extension
	-- Special catch for coins
	local image,image_n
	if isCoins then
		image_n = coins_image(quantity)
	else
		image_n = params.default_to(args.image or args.Image, name .. '.png')
		image_n = mw.ustring.gsub(image_n, '#.+$', '.png')
	end
	if image_n:lower() == 'no' or params.is_empty(args.name or args.Name) then
		image = ''
	else
		image = mw.ustring.format('[[File:%s|link=%s|alt=%s: RS3 %s drops %s with rarity %s%s in quantity %s]]', image_n, name, image_n, title, name, rollstext, rarity, quantity)
	end
	-- this only affects the JSON
	local useSmw = true
	if params.has_content(args.nosmw) then
		useSmw = false
	end
	if params.has_content(frameArgs.nosmw) then
		useSmw = false
	end
    
	-- Level for Fishing, Archaeology, Mining, Woodcutting and Divination
	local level = 0
	if (args.level or args.Level) and tonumber(args.level or args.Level, 10) then
		level = tonumber(args.level or args.Level, 10)
	end
	
	local rdt = string.lower(args.rdt or '') == 'yes'
	
	local hasRowwideVersion = false
	local versionKey = 'DEFAULT'
    if params.has_content(frameArgs.version or frameArgs.Version) then
        -- versions applied to the entire table
		versionKey = frameArgs.version or frameArgs.Version
    end
    if params.has_content(version) then
        -- versions applied to this row
        versionKey = version
        hasRowwideVersion = true
    end
    
    local dropType = frameArgs.dtype or 'combat'
    
    -- SoF/TH convert currency
	local convertcurrencyKey = 'Oddments'
	if dropType == 'sof' then
		convertcurrencyKey = 'Coins'
	end
    if params.has_content(frameArgs.convertcurrency) then
        -- currency applied to the entire table
		convertcurrencyKey = frameArgs.convertcurrency
    end
    if params.has_content(convertcurrency) then
        -- currency applied to this row
        convertcurrencyKey = convertcurrency
    end
    
	-- Table row
	local ret =  p._main(name,
			altname,namenotes,
			quantity,quantitynotes,
			rarity,rarity_value,alt_rarity,alt_rarity_endash,alt_rarity_value,
			raritynotes,thgem,sofslot,
			convert,convertcurrencyKey,image,members,
			alchInfo,geInfo,
			dropType,versionKey,hasRowwideVersion,
			level,approx,
			smwname,useSmw,rdt,rolls)

	-- categories for mainspace
	local cats = ''
	
	if ns == '' then
		cats = categories{name,quantity,rarity}
	end
	return ret..cats
end

-- main function to generate the row
function p._main(name,altname,namenotes,
		quantity,quantitynotes,
		rarity,rarity_value,alt_rarity,alt_rarity_endash,alt_rarity_value,
		raritynotes,thgem,sofslot,
		convert,convertcurrencyKey,image,members,
		alchInfo,geInfo,
		dropType,versionKey,hasRowwideVersion,
		level,approx,
		smwname,useSmw,rdt,rolls)
	
    -- GE value, alch value, quantity cell contents
	local total, alchtotal, converttotal, vsort, vasort, vcsort, _h, _l
    quantity, _h, _l = qty(quantity)
    if geInfo then
	    total, vsort = get_total(geInfo.value,_h,_l)
    end
    if alchInfo then
	    alchtotal, vasort = get_total(alchInfo.value,_h,_l)
    end
	local convertstr = 'each'
    if convert then
	    converttotal, vcsort = get_total(convert,_h,_l)
	    -- If there is no quantity range, assume the convert value is the total
	    if _h == _l then
	    	converttotal, vcsort = get_total(convert,1,1)
	    	convertstr = 'total'
	    end
    end
    
    -- value sorts
	if type(vsort) ~= 'number' then
		vsort = 0
	end
	if type(vasort) ~= 'number' then
		vasort = 0
	end
	if type(vcsort) ~= 'number' then
		vcsort = 0
	end

    -- quantity notes
	if #quantitynotes > 3 then
		quantity = quantity..quantitynotes
    end
    
    -- rarity cell contents
	local rare_class, rare_sort
	if rarity_value == undefined then
		rare_class, rare_sort = unpack(rarities[rarity:lower()] or rarities._default)
	elseif rarity_value == false then
		rare_class, rare_sort = unpack(rarities._default)
	else
		rare_sort = 1/rarity_value
		rare_class = get_rarity_class(rarity_value)
	end
	local rollstext = ''
	if rolls then
		rollstext = rolls .. ' × '
		if rarity_value ~= false then
			rare_sort = rare_sort / rolls
			rare_class = get_rarity_class(math.min(1/rare_sort,0.99))
		end
	end
	local tilde = ''
	if approx then
		tilde = '~'
	end
	local _r = rarity
    
    -- monster versions
    if hasRowwideVersion then
        -- setup reference for this row
        -- reference name
		local cleanref = mw.ustring.gsub(
			mw.ustring.gsub(
				mw.ustring.lower(versionKey),
				-- replace spaces with hyphens
				"%s", "-"
			),
			-- remove all non-word characters
			"[^%w%-]", ""
		);
		local refname = "autod-" .. cleanref;

        -- create and append the reference
		raritynotes = raritynotes .. mw.getCurrentFrame():extensionTag{ name='ref', content = mw.ustring.format('Only dropped by %s version.', versionKey), args = { group='d', name = refname } };
    end
    
	-- Table row creation
    local ret = mw.html.create('tr')
            -- row-wide things
            :css('text-align','center')
            -- inventory image
			:tag('td')
				:addClass('inventory-image')
				:attr('data-sort-value',name)
				:wikitext(image)
            :done()
            -- item name
			:tag('td')
				:css('text-align','left')
				:addClass('item-col')
				:wikitext(string.format('[[%s|%s]]%s%s',name,altname,#namenotes > 3 and namenotes or '',members and members_note or ''))
            :done()
            -- level
            if level>0 then
				ret:tag('td')
					:attr('data-sort-value',level)
					:wikitext(level)
	            ret:done()
            end
            -- quantity
			ret:tag('td')
				:attr('data-sort-value',_h)
				:wikitext(quantity)
            ret:done()
    
    -- rarity
	local rarity_cell = ret:tag('td')
	local rarity_span = rarity_cell:tag('span')
	rarity_span:wikitext(rollstext .. tilde .. rarity)
	rarity_cell:attr('data-sort-value',rare_sort)
				:addClass(rare_class)
	local rarity_cell_title
	if type(rarity_value) == 'number' then
		rarity_cell_title = rollstext .. tilde .. string.format('%.3g%%', 100 * rarity_value)
		rarity_span:attr({
			['data-drop-fraction'] = rollstext .. tilde .. commas(mw.text.split(rarity, '/')[1]) .. '/' .. commas(mw.text.split(rarity, '/')[2]),
			['data-drop-oneover'] = rollstext .. tilde .. '1/' .. commas(sigfigalt(1/rarity_value)),
			['data-drop-percent'] = rollstext .. tilde .. sigfig(100 * rarity_value, 3),
			['data-drop-permil'] = rollstext .. tilde .. sigfig(1000 * rarity_value, 3),
			['data-drop-permyriad'] = rollstext .. tilde .. sigfig(10000 * rarity_value, 3),
		})
	end
	
    if alt_rarity ~= '' then
		local rarity_cell_sep
        if alt_rarity_endash  ~= '' then
            rarity_cell_sep = '–';
        else
            rarity_cell_sep = '; ';
        end
		rarity_cell:tag('span'):wikitext(rarity_cell_sep);

        local alt_rarity_span = rarity_cell:tag('span')
        alt_rarity_span:wikitext(alt_rarity)
        if type(alt_rarity_value) == 'number' then
			rarity_cell_title = rarity_cell_title .. rarity_cell_sep .. string.format('%.3g%%', 100 * alt_rarity_value)
            alt_rarity_span:attr({
                ['data-drop-fraction'] = alt_rarity,
                ['data-drop-oneover'] = '1/' .. commas(sigfig(1/alt_rarity_value, 3)),
                ['data-drop-percent'] = sigfig(100 * alt_rarity_value, 3),
                ['data-drop-permil'] = sigfig(1000 * alt_rarity_value, 3),
                ['data-drop-permyriad'] = sigfig(10000 * alt_rarity_value, 3),
            })
        end
    end
	rarity_cell:attr("title", rarity_cell_title);

	if #raritynotes > 3 then
		rarity_cell:wikitext(raritynotes)
	end

    -- Treasure Hunter gem
    if dropType == 'th' then
		local th_gem_cell = ret:tag('td')
		if thgem == 'no' then
			th_gem_cell:addClass('table-na')
		end
		th_gem_cell:wikitext(th_gem_icons[thgem] or '')
		th_gem_cell:attr('data-sort-value',th_gem_rarities[thgem] or 2)
	-- Squeal of Fortune slot
	elseif dropType == 'sof' then
		local sof_slot_cell = ret:tag('td')
		if sofslot == 'no' then
			sof_slot_cell:addClass('table-na')
		end
		sof_slot_cell:wikitext(sof_slot_icons[sofslot] or '')
		sof_slot_cell:attr('data-sort-value',sof_slot_rarities[sofslot] or 2)
	end

    -- setup GE and alch cells
    local ge_td = ret:tag('td')
    local alch_td = ret:tag('td')

    -- common attributes
    ge_td   :attr('data-sort-value',vsort)
    :addClass('ge-column')
    :css({
        ['text-align'] = 'right',
        cursor = 'help'
    })
    alch_td :attr('data-sort-value',vasort)
            :addClass('alch-column')
            :css({
                ['text-align'] = 'right',
                cursor = 'help'
            })

    local ge_td_title, ge_td_content, alch_td_title, alch_td_content
    --Cases for the GE and alch values
    local smwValue = 0
    if geInfo then
    	local currency_name = lang:plural(geInfo.value or 0, 'coin', 'coins')
    	local currency_img = nil
    	if geInfo.currency then
    		currency_img, currency_name = currency(geInfo.currency, geInfo.value, vsort)
    	end
    	ge_td_title = mw.ustring.format(_priceStrings[geInfo.type], commas(geInfo.value) or '', currency_name)
    	ge_td_content = total .. (currency_img or valueImages[geInfo.type] or '')
    	
        if geInfo.type == 'ge_alt' or geInfo.type == 'ge_alch' then
        	smwValue = geInfo.value
        end
    else
    	ge_td_content = 'Not sold'
        ge_td_title = 'This item cannot be traded on the Grand Exchange nor alchemised and has no applicable value to display.'
        ge_td:css('color', '#999')
    end
    
    if alchInfo then
    	local currency_name = lang:plural(alchInfo.value or 0, 'coin', 'coins')
    	local currency_img = ''
    	if alchInfo.currency then
    		currency_img, currency_name = currency(alchInfo.currency, alchInfo.value, vasort)
    	end
    	alch_td_title = mw.ustring.format(_priceStrings[alchInfo.type], commas(alchInfo.value) or '', currency_name)
    	alch_td_content = alchtotal .. (currency_img or valueImages[alchInfo.type] or '')
    else
        alch_td_content = 'Not alchemisable'
        alch_td_title = 'This item cannot be alchemised and has no applicable value to display.'
        alch_td:css('color', '#999')
    end
    ge_td:wikitext(ge_td_content):attr('title', ge_td_title)
    alch_td:wikitext(alch_td_content):attr('title', alch_td_title)

    -- SoF/TH convert value
    if dropType == 'sof' or dropType == 'th' then
		local convert_td = ret:tag('td')
	    convert_td:attr('data-sort-value',vcsort)
	            :addClass('convert-column')
	            :css({
	                ['text-align'] = 'right',
	                cursor = 'help'
	            })
	    if params.has_content(convert) then
	    	if convert ~= 'no' then
		    	local convert_currency_name = 'coin'
		    	local convert_currency_img = ''
		    	if convertcurrencyKey then
		    		convert_currency_img, convert_currency_name = currency(convertcurrencyKey, convert, vcsort)
		    	end
		    	convert_td_title = mw.ustring.format("%s %s %s", commas(convert) or '', convert_currency_name, convertstr or '')
		    	convert_td_content = converttotal .. (convert_currency_img or '')
		    else
		        convert_td_content = 'Cannot be converted'
		        convert_td_title = 'This item cannot be converted and has no applicable value to display.'
		        convert_td:css('color', '#999')
		    end
	    	convert_td:wikitext(convert_td_content):attr('title', convert_td_title)
	    end
		convert_td:attr('data-sort-value',convert or -1)
	end

	-- SMW
	if (ns == '') and useSmw then
	    dropFrom = pgTitle
        if versionKey ~= 'DEFAULT' then
        	dropFrom = pgTitle .. '#' .. versionKey
        end
        
		-- check if applies to all or only a version
		--add function to reduce image to File:Some name.png
		local smwNameNote = mw.text.killMarkers(namenotes)
		local smwQuantity = mw.text.killMarkers(quantity)
		smwQuantity = smwQuantity:gsub('<span class="dropsline%-noted">', '')
		smwQuantity = smwQuantity:gsub('</span>', '')
		smwQuantity = smwQuantity:gsub(',', '')
		smwQuantity = smwQuantity:gsub('&nbsp;', ' ')
		smwQuantity = smwQuantity:gsub(';', ',')
		local smwRarityNote = mw.text.killMarkers(raritynotes)
		local smwRolls = rolls or 1
		local subcount = 1
		if VariablesLua.varexists( 'dropcount' ) then
			subcount =	VariablesLua.var( 'dropcount', 1 )
			subcount = subcount + 1
			VariablesLua.vardefine( 'dropcount', subcount)
		else
			VariablesLua.vardefine( 'dropcount', 1)
		end
		local subname = 'DROP_'..smwname..'_'..smwQuantity..'_'..rarity..'_'..subcount

		local droppedItemName = 'Dropped item'
		if rdt == true then
			droppedItemName = 'Dropped item from RDT'
		end
		if level == 0 then
			level = mw.ext.VariablesLua.var(string.format("DropLevel_%s_%s", dropType, versionKey))
		end
		local smw_json = {
			['Drop type'] = dropType,
			['Dropped from'] = dropFrom,
			[droppedItemName] = smwname,
			['Drop level'] = level,
			['Name Notes']=smwNameNote,
			['Drop Quantity']=smwQuantity,
			['Quantity High']=_h,
			['Quantity Low']=_l,
			Rarity=rarity,
			['Alt Rarity']=alt_rarity,
			['Alt Rarity Dash'] = alt_rarity_endash,
			['Rarity Notes']=smwRarityNote,
			['Rolls']=smwRolls,
			['Approx']=approx
		}
		if string.match(raritynotes, 'UNIQ%-%-ref') then
			smw_json['RarityNote Ref'] = true
		end
		if string.match(quantity, 'UNIQ%-%-ref') then
			smw_json['QuantityNote Ref'] = true
		end
		if smwValue > 0 then
			smw_json['Drop Value'] = smwValue
		end
		if dropType == 'archaeology' then
			if VariablesLua.var('dropsline_is_arch_soil') == 'true' then
				smw_json['Is soil screening'] = 'true'
			else
				smw_json['Is soil screening'] = 'false'
			end
		end
		local smw_sub = {
			[droppedItemName] = smwname,
			['Dropped from'] = dropFrom,
			["Drop JSON"] = mw.text.jsonEncode(smw_json)
		}
		mw.smw.subobject(smw_sub, subname) -- drop is subobject of page
	end
	
	return tostring(ret)
end

function qty(quantity)
	-- if no quantity is given, return unknown
	if string.lower(quantity) == 'varies' then
		return 'Varies'
	end
	if not quantity or string.lower(quantity) == 'unknown' then
		return 'Unknown'
	end
	-- en dashes are the proper dash for number ranges
	-- replace all hyphens and em dashes with en
	-- strip *all* whitespace
	-- change '(noted)' to '$n' for parsing
	quantity = mw.ustring.gsub(quantity,'[-—]','–')
		:gsub('%s','')
		:gsub('%(noted%)','$n')
	-- split list into table
	local vals = mw.text.split(quantity,'[,;]')
	local low = 2147483648
	local high = 0
	-- recreate the quantity string to ensure consistent formatting
	local numstr = {}
	for i, v in ipairs(vals) do
		local clean = v:gsub('$n','')
		-- if list element contains an en dash (indicating range)
		-- Find the smaller/larger number (just in case)
		-- Compare them to the current min/max
		-- put them in order with desired format
		if mw.ustring.find(v,'–') then
			local splitvals = mw.text.split(clean,'–')
			-- assume a is smaller, b is larger
			local a = tonumber(splitvals[1])
			local b = tonumber(splitvals[2])
			-- Just in case
			if a > b then
				a,b = b,a
			end
			if a < low then
				low = a
			end
			if b > high then
				high = b
			end
			local addx = commas(a)..'–'..commas(b)
			if v:find('$n') then
				addx = addx.._noted
			end
			table.insert(numstr,addx)
		else
			local a = tonumber(clean)
			if a < low then
				low = a
			end
			if a > high then 
				high = a
			end
			local addx = commas(a)
			if v:find('$n') then
				addx = addx.._noted
			end
			table.insert(numstr,addx)
		end
	end
	-- Add a line break if there are too many elements
	-- To keep the tables thin
	if #numstr > 11 then
		local mid = math.floor(#numstr/2)
		numstr[mid] = '<br/>'..numstr[mid]
	end
	-- To prevent any possible confusion with formatted numbers
	-- elements should be separated with semicolons followed by a space
	numstr = table.concat(numstr,'; ')
	-- If no numbers are found in the string, return unknown
	if not numstr:find('%d') then
		return 'Unknown', price
	end

	return numstr, high, low
end

function get_total(value,qhigh,qlow)
	-- if no alch value is given, return unknown
	if not value or string.lower(value) == 'unknown' then
		return value
	end
	-- if value is negative (from smw/ge) it cannot be alched
	if tonumber(value) and tonumber(value) < 0 then
		return false
	end
	-- if no numbers return not alchemisable
	if not tonumber(value) and not value:find('%d') then
		return false
	end

	-- en dashes are the proper dash for number ranges
	-- replace all hyphens and em dashes with en
	-- strip *all* whitespace
	value = mw.ustring.gsub(value,'[-—]','–')
		:gsub('%s','')
	-- split list into table
	local vals = mw.text.split(value,'[,;]')
	-- All value ranges will be a range
	-- e.g. if items valued at 100 coins are dropped in quantities of 1, 3, 5
	-- the value returned will be 100–500 rather than 100; 300; 500
	-- If low and high vars are the same in the end, only 1 value is displayed
	local low = 2147483649147483648
	local high = 0
	-- recreate the alchval string to ensure consistent formatting
	for i, v in ipairs(vals) do
		local clean = v:gsub('$n','')
		-- if list element contains an en dash (indicating range)
		-- Find the smaller/larger number (just in case)
		-- Compare them to the current min/max
		-- put them in order with desired format
		if mw.ustring.find(v,'–') then
			local splitvals = mw.text.split(clean,'–')
			-- assume a is smaller, b is larger
			local a = tonumber(splitvals[1])
			local b = tonumber(splitvals[2])
			-- Just in case
			if a > b then
				a,b = b,a
			end
			if a < low then
				low = a
			end
			if b > high then
				high = b
			end
		else
			local a = tonumber(clean)
			if a < low then
				low = a
			end
			if a > high then 
				high = a
			end
		end
	end

	local valret, sort
	if not qhigh or not qlow then
		sort = high
		valret = commas(high)
	else
		local lower = qlow * low
		local higher = qhigh * high
		if higher == lower then
			valret = commas(higher)
		else
			valret = commas(lower)..'–'..commas(higher)
		end
		sort = higher
	end

	return valret, sort
end

-- function to get the currency image and name (singular vs plural)
function currency(altcur,price,total)
	price = tostring(price)
	total = tostring(total)
	-- body
	local lowcur = string.lower(altcur)
	local clean = price:gsub('%W','')
	local retcur, img
	if lang:plural(tonumber(clean) or 0, 'f', 't') == 't' then
		retcur = altcur
	else
		if lowcur == 'zemomark' or lowcur == 'tokkul' or lowcur == 'teci' then
			retcur = altcur
		elseif lowcur == 'pieces of eight' then
			retcur = 'piece of eight'
		else
			retcur = string.sub(altcur,1,(string.len(altcur)-1))
		end
	end
	img = curr_image(altcur,total) or 'AltValue.png'
	img = '<span class="dropsline-altval style="margin-left:0.3em;">[[File:'..img..'|link=|frameless|20px]]</span>'
	return img, retcur
end

-- adding categories to mainspace
function categories(...)
	local name,quantity,rarity = unpack(...)
	local ret = ''
	name = name:lower()
	quantity = quantity:lower()
	if name:find('effigy') then
		ret = ret .. '[[Category:Effigy dropping monsters]]'
	elseif name:find('clue scroll ') then
		ret = ret .. '[[Category:Clue scroll dropping monsters]]'
	elseif name:find('rare drop table') then
		ret = ret .. '[[Category:Monsters with access to the rare drop table]]'
	elseif name:find('wilderness shared loot table') then
		ret = ret .. '[[Category:Monsters with access to the Wilderness shared loot table]]'
	end
	if rarity == nil or rarity == '' or rarity:lower() == 'unknown' then
		ret = ret .. '[[Category:Needs drop rarity added]]'
	end
	if quantity:find('Unknown') then
		ret = ret .. '[[Category:Needs drop quantity added]]'
	end
	return ret
end

return p
-- </nowiki>