Old School RuneScape Wiki
Advertisement

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

-- <pre>
local p = {}

local yesno = require('Module:Yesno')
local uclc = require('Module:Paramtest').ucflc
local commas = require('Module:Addcommas')

local raritybg = {
	['always'] = '#AFEEEE',
	['common'] = '#56E156',
	['uncommon'] = '#FFED4C',
	['rare'] = '#FF863C',
	['very rare'] = '#FF6262',
	['random'] = '#FFA3FF',
	['varies'] = '#FFA3FF',
	['discontinued'] = '#DBFF4C'
}

local raritysort = {
	['always'] = '1',
	['common'] = '2',
	['uncommon'] = '3',
	['rare'] = '4',
	['very rare'] = '5',
	['varies'] = '6',
	['random'] = '6',
	['discontinued'] = '7'
}

function p._main(frame)
	local args = frame:getParent().args
	-- Params and defaults
	local name = string.upper( params.default_to(args.Name,'item') )
	local namenotes = args.Namenotes or args.namenotes or ''
	local quantity = params.default_to(args.Quantity,'Unknown')
	local quantitynotes = args.Quantitynotes or args.quantitynotes or ''
	local rarity = params.default_to(args.Rarity,'Unknown')
	local raritynotes = args.Raritynotes or args.raritynotes or ''
	rarity = params.ucflc(rarity)
	quantity = quantity:lower()
	local gemw = args.gemw or 'yes'
	gemw = yesno(gemw)
	-- Use 'File:<name>.png' if no image param
	-- User 'File:<image>' if image param; image param will include extension
	local image = 'File:' .. params.default_to(args.Image,name .. '.png')

	-- Table row
	local ret =  main(name,namenotes,quantity,quantitynotes,rarity,raritynotes,image,gemw)

	-- categories for mainspace
	local cats = ''
	local ns = mw.title.getCurrentTitle().nsText
	if ns == '' then
		cats = categories(name,quantity,rarity)
	end
	return ret..cats
end

function main(name,namenotes,quantity,quantitynotes,rarity,raritynotes,image,gemw)
	local rare_bg = raritybg[rarity:lower()] or '#FFFFFF'
	local rare_sort = raritysort[rarity:lower()] or '8'
	quantity = qty(quantity)
	-- Table row creation
	local ret = '\n|- style="text-align:center;"' .. 
		'\n| class="hidenoimage" | [[' .. image .. ']]' ..
		'\n| style="text-align:left;" | [[' .. name .. ']] ' .. namenotes ..
		'\n| ' .. quantity .. ' ' .. quantitynotes ..
		'\n| style="background:' .. rare_bg .. ';"' ..
			-- display none is used to create a sort key to let
			-- rarity sorting work properly
			' | <span style="display:none;">' .. rare_sort ..
			'; </span>' .. rarity .. ' ' .. raritynotes
	return ret
end


function qty(quantity)
	-- if no quantity is given, return unknown
	if not quantity 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 ret = {}
	-- 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 contain 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 noted = v:find('$n')
			local splitvals = mw.text.split(clean,'–')
			local a = tonumber(splitvals[1])
			local b = tonumber(splitvals[2])
			local smaller,larger
			if a > b then
				smaller = b
				larger = a
			else
				smaller = a
				larger = b
			end
			numstr = numstr .. commas._add(smaller) .. '–' .. commas._add(larger)
			if noted then
				numstr = numstr .. ' (noted)'
			end
		else
			local a = tonumber(clean)
			numstr = numstr .. commas._add(a)
			if v:find('$n') then
				numstr = numstr .. ' (noted)'
			end
		end
		-- To prevent any possible confusion with formatted numbers
		-- elements should be separated with semicolons followed by a space
		numstr = numstr .. '; '
	end
	-- removes the final separator, because it's redundant
	numstr = numstr:sub(1,-3)

	if numstr:find('%d') then
		return numstr
	else
		return 'Unknown'
	end
end

-- adding categories to mainspace
function categories(name,quantity,rarity)
	local ret = ''
	name = name:lower()
	quantity = quantity:lower()
	if name:find('clue scroll') then
		ret = ret .. '[[Category:Clue Drop Monsters]]'
	end
	if not raritysort[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
Advertisement