Module:DropsLine/sandbox: Difference between revisions

From WIDEVERSE Wiki
Jump to navigation Jump to search
mNo edit summary
 
m (1 revision imported)
 
(No difference)

Latest revision as of 21:22, 4 November 2021

Documentation for this module may be created at Module:DropsLine/sandbox/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.loadData('Module:DropsLine/sandbox/itemData')

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

local _membs = '&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%s",
	standard = "%s %s%s each",
	alch_alt = "%s %s%s each; this item has a distinct value, even though it cannot be alchemised.",
	ge_alch = "%s %s%s each; this is the high alchemy value as this item cannot be traded on the Grand Exchange.",
	ge_alt = "%s %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>'
}

local smwData = nil
function getSMWInfo(item)
    if smwData ~= nil then
        return smwData
    end
    local smw = mw.smw.ask({
        '[['..item..']]',
        '?High Alchemy value'
    })
    if smw and smw[1] then
        smwData = {
            alch = smw[1]['High Alchemy value']
        }
    else
        smwData = false
    end
    return smwData
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' }
}

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) then
		return lang:formatNum(tonumber(n))
	else
		return n
	end
end

-- list of items to automatically not use gemw
-- this should only be used for items dropped by many monsters
local nogemw = {
	'clue scroll ', 'starved ancient effigy', 'rare drop table', 'wilderness shared loot table', 'warpriest', 'court summons'
}
-- list of items to automatically not use high alch
-- this should only be used for items dropped by many monsters
local noalch = {
	'clue scroll ', 'starved ancient effigy', 'rare drop table', 'wilderness shared loot table', 'warpriest', 'court summons', 'stone spirit'
}

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,raritynotes,memsover,
		altcur,monVers,altSource = params.defaults{
					{args.Name,'Item'},
					{args.Namenotes,''},
					{args.Quantity,'Unknown'},
					{args.Quantitynotes,''},
					{args.Rarity,'Unknown'},
					{args.Raritynotes,''},
					{args.Members,''},
					{args.AltCurrency,''},
					{args.Version,''},
					{args.Altsource,'GazBot'},
				}
	if altSource ~= "GazBot" then
		pgTitle = altSource
	end
	local isCoins = name:lower() == 'coins'
	local altname = params.default_to(args.Alt,name)
	local gemwname = params.default_to(args.gemwname,name)
	local _smwname = params.default_to(args.smwname,gemwname)
	--local raritynotes = args.Raritynotes or ''
	-- Remove version number from potions, enchanted jewellery, waterskins etc for smw
	local cleanedName
	local dropVers = ''
	if _smwname:match(' %(%d%)$') then
		cleanedName, dropVers = mw.ustring.match(_smwname, '^(.-) (%(%d%))$')
    elseif _smwname:match('%#') then
        cleanedName, dropVers = mw.ustring.match(_smwname, '^(.-)%#([%w%s%(%)_]+)$')
    else
        cleanedName = mw.ustring.gsub(_smwname, ' %(%d%)$', '')
    end
    cleanedName = mw.text.trim(cleanedName)
    dropVers = mw.text.trim(dropVers)
    
    local smwname = cleanedName 
    if dropVers ~= '' then
        -- get subobject instead
        smwname = cleanedName..'#'..dropVers
    end
	
	local rarity_value
	if rarities[rarity:lower()] then
		rarity = params.ucflc(rarity)
	else
		rarity_value = expr(rarity)
	end
	

	quantity = mw.ustring.lower(quantity)
	local gemw = yesno(args.gemw or 'yes', false)
	local alchable = yesno(args.alch or 'yes', false)

	-- automatic no gemw
    for _, v in ipairs(nogemw) do
        -- can't use a plain table lookup because this uses match
		if mw.ustring.match(mw.ustring.lower(name or ''), v) then
			gemw = false
			break
		end
	end
	-- automatic no alch
	for _, v in ipairs(noalch) do
        -- can't use a plain table lookup because this uses match
		if mw.ustring.match(mw.ustring.lower(name or ''), v) then
			alchable = false
			break
		end
	end
    
    local alchInfo = nil
	local geInfo = nil
	
    if isCoins then
    	alchInfo = {value = 1, type = 'coins'}
    	geInfo = {value = 1, type = 'coins'}
    end
    
    if alchable then
	    if droppeditem_data[smwname] ~= nil then
	    	alchInfo = {value = droppeditem_data[smwname], type = 'standard'}
	    else
            local smwret = getSMWInfo(smwname)
            if smwret and smwret.alch ~= nil then
                alchInfo = {value = smwret.alch, type = 'standard'}
            end
	    end
	end
	
	if alchInfo == nil and args.AltValue then
		alchInfo = {value = tonumber(args.AltValue), type = 'alch_alt', currency = altcur}
	end
	
	if gemw then
		geInfo = {value = exchange._price(gemwname), type = 'standard'}
	elseif args.AltValue then
		geInfo = {value = tonumber(args.AltValue), type = 'ge_alt', currency = altcur}
	elseif alchInfo then
		geInfo = {value = alchInfo.value, type = 'ge_alch'}
	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, name .. '.png')
		image_n = mw.ustring.gsub(image_n, '#.+$', '.png')
	end
	if image_n:lower() == 'no' or params.is_empty(args.Name) then
		image = ''
	else
		image = mw.ustring.format('[[File:%s|link=%s|alt=%s: RS3 %s drops %s with rarity %s in quantity %s]]', image_n, name, image_n, title, name, rarity, quantity)
	end
	-- this only affects the JSON
	local useSmw = true
	if params.has_content(args.nosmw) then
		useSmw = false
	end
	
	-- Level for Fishing, Archaeology, Mining, Woodcutting and Divination
	local level = 0
	if args.Level and tonumber(args.Level, 10) then
		level = tonumber(args.Level, 10)
	end
	
	-- Row class currently only used by [[Module:DropsLineMulti]]
	local rowClass = ''
	
	-- Prefix for rarity
	local rarePref = ''
	if args.approx and yesno(args.approx) then
		rarePref = '<span title="Approximate">~</span> '
	end
	
	local rdt = string.lower(args.rdt or '') == 'yes'
	
	-- Table row
	local ret =  p._main(name,
			altname,namenotes,
			quantity,quantitynotes,rarePref,
			rarity,rarity_value,raritynotes,
			image,members,
			alchInfo,geInfo,
			isCoins,
			frameArgs,monVers,
			cleanedName,dropVers,level,rowClass,
			smwname,useSmw,rdt)

	-- categories for mainspace
	local cats = ''
	
	if ns == '' or ns == 'RuneScape' 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,rarePref,
		rarity,rarity_value,raritynotes,
		image,members,
		alchInfo,geInfo,
		isCoins,
		frameArgs,monVers,
		cleanedName,dropVers,level,rowClass,
		smwname,useSmw,rdt)
	
    -- GE value, alch value, quantity cell contents
	local total, alchtotal, vsort, vasort, _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
    
    -- value sorts
	if type(vsort) ~= 'number' then
		vsort = 0
	end
	if type(vasort) ~= 'number' then
		vasort = 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 _r = rarity

    local imgmembs = ''
	-- Add to name of item if members item
	if members == true then
		imgmembs = _membs
	end
	
	-- Table row creation
    local ret = mw.html.create('tr')
            -- row-wide things
			:addClass(rowClass)
            :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 '',imgmembs))
            :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')
	rarity_cell:wikitext(rarePref)
	local rarity_span = rarity_cell:tag('span')
	rarity_span:wikitext(rarity)
	rarity_cell:attr('data-sort-value',rare_sort)
				:addClass(rare_class)
	if type(rarity_value) == 'number' then
		rarity_cell:attr('title', string.format('%.3g%%', 100 * rarity_value))
		rarity_span:attr({
			['data-drop-fraction'] = commas(mw.text.split(rarity, '/')[1]) .. '/' .. commas(mw.text.split(rarity, '/')[2]),
			['data-drop-oneover'] = '1/' .. commas(sigfigalt(1/rarity_value)),
			['data-drop-percent'] = sigfig(100 * rarity_value, 3),
			['data-drop-permil'] = sigfig(1000 * rarity_value, 3),
			['data-drop-permyriad'] = sigfig(10000 * rarity_value, 3),
		})
	end
	if #raritynotes > 3 then
		rarity_cell:wikitext(raritynotes)
	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',vsort)
            :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 = 'coin'
    	local currency_img = nil
    	if geInfo.currency then
    		currency_img, currency = currency(geInfo.currency, geInfo.value, vsort)
    	end
    	ge_td_title = mw.ustring.format(_priceStrings[geInfo.type], commas(geInfo.value) or '', currency, lang:plural(geInfo.value or 0, '', 's'))
    	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 = 'coin'
    	local currency_img = ''
    	if alchInfo.currency then
    		currency_img, currency = currency(alchInfo.currency, alchInfo.value, vsort)
    	end
    	alch_td_title = mw.ustring.format(_priceStrings[alchInfo.type], commas(alchInfo.value) or '', currency, lang:plural(alchInfo.value or 0, '', 's'))
    	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)

	-- SMW
	if (ns == '' or ns == 'RuneScape') and useSmw then
	    local versionKey = 'DEFAULT'
	    if params.has_content(frameArgs.version) then
	        -- versions applied to the entire table
	       versionKey = frameArgs.version
	    end
	    if params.has_content(monVers) then
	        -- versions applied to this row
	        versionKey = monVers
	    end
	    dropFrom = pgTitle
        if versionKey ~= 'DEFAULT' then
        	dropFrom = pgTitle .. '#' .. versionKey
        end
        
		local smw_sub = {}
		local smw_done = false
		-- check if applies to all or only a version
		--add function to reduce image to File:Some name.png
		local smwImage = mw.text.encode(image)
		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 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 dropType = frameArgs.dtype or 'combat'
		
		local droppedFromName = 'Dropped from'
		local droppedItemName = 'Dropped item'
		if rdt == true then
			droppedFromName = 'RDT Dropped from'
			droppedItemName = 'Dropped item from RDT'
		end
		smw_sub = {
			['Drop type'] = dropType,
			[droppedFromName] = dropFrom,
			['Drop level'] = mw.ext.VariablesLua.var(string.format("DropLevel_%s_%s", dropType, versionKey)),
			[droppedItemName] = smwname,
			['Dropped item image']=smwImage,
			['Dropped item text']=altname,
			['Name Notes']=smwNameNote,
			['Drop Quantity']=smwQuantity,
			['Quantity High']=_h,
			['Quantity Low']=_l,
			Rarity=rarity,
			['Rarity sort']=100/(tonumber(rare_sort) or 1),
			['Rarity Notes']=smwRarityNote
		}
		if string.match(raritynotes, 'UNIQ%-%-ref') then
			smw_sub['RarityNote Ref'] = true
		end
		if string.match(quantity, 'UNIQ%-%-ref') then
			smw_sub['QuantityNote Ref'] = true
		end
		if smwValue > 0 then
			smw_sub['Drop Value'] = smwValue
		end
		if frameArgs.dtype == 'archaeology' then
			if VariablesLua.varexists('dropsline_is_arch_soil') and VariablesLua.var('dropsline_is_arch_soil', 'false') == 'true' then
				smw_sub['Is soil screening'] = 'true'
			else
				smw_sub['Is soil screening'] = 'false'
			end
		end
		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 = 2147483648
	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 tonumber(clean) and tonumber(clean) ~= 1 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 not rarities[rarity:lower()] 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>