Module:ItemDropsLineUpdated

From RuneRealm Wiki
Jump to navigation Jump to search
Module documentation
This documentation is transcluded from Template:No documentation/doc. [edit] [history] [purge]
This module does not have any documentation. Please consider adding documentation at Module:ItemDropsLineUpdated/doc. [edit]
Module:ItemDropsLineUpdated requires Module:Addcommas.
Module:ItemDropsLineUpdated requires Module:NPCQuery.
Module:ItemDropsLineUpdated requires Module:Paramtest.
Module:ItemDropsLineUpdated requires Module:Yesno.

-- <pre>
local p = {}

local yesno = require('Module:Yesno')
local params = require('Module:Paramtest')
local commas = require('Module:Addcommas')
local npcs = require('Module:NPCQuery')

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'
}
local membersort = {
	['yes'] = '1',
	['no'] = '2',
	['no?'] = '2',
	['yes?'] = '1',
	[''] = '3'
}

function p.main(frame)
	local args = frame:getParent().args
	-- Params and defaults
	local name = params.default_to(args.Monster,'monster')
	local namenotes = args.Namenotes or ''
	local combat = params.default_to(args.Combat,nil)
	local cbnotes = args.cbnotes or ''
	local quantity = params.default_to(args.Quantity,'Unknown')
	local quantitynotes = args.Quantitynotes or ''
	local member = args.Member or npcs.NPCisMember(name)
	local rarity = params.default_to(args.Rarity,'Unknown')
	local raritynotes = (args.Raritynotes or args.raritynotes) or ''
	rarity = params.ucflc(rarity)
	quantity = quantity:lower()

	-- Table row
	return p._main(name,namenotes,combat,cbnotes,quantity,quantitynotes,rarity,raritynotes,member)
end

function p._main(name,namenotes,combat,cbnotes,quantity,quantitynotes,rarity,raritynotes,member)
	local rare_bg = raritybg[rarity:lower()] or '#FFFFFF'
	local rare_sort = raritysort[rarity:lower()] or '8'
	    local member_sort = membersort[member:lower()] or '8'
	-- Clean up the lists
	quantity = qty(quantity)
	combat = cmb(combat)
	-- Table row creation
	local ret = '\n|- style="text-align:center;white-space:nowrap;"' .. 
		'\n| style="text-align:left;white-space:nowrap;" | [[' .. name .. ']] ' .. namenotes ..
		'\n| ' .. combat .. ' ' .. cbnotes ..
		'\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;white-space:nowrap;">' .. rare_sort ..
			'; </span>' .. rarity .. ' ' .. raritynotes ..
			'\n| <span style="display:none;">' .. member_sort ..
			'; </span>' .. member .. '\n|-\n'
	return ret
end

function qty(quantity)
	-- if no quantity is given, return unknown
	if not quantity or 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,',')
	-- recreate the quantity string to ensure consistent formatting
	local numstr = ''
	for i, v in ipairs(vals) do
		local clean = v:gsub('$n','')
		if mw.ustring.find(v,'–') then
			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 v:find('$n') then
				numstr = numstr .. ' (noted)'
			end
		else
			local a = tonumber(clean)
			if a then
				numstr = numstr .. commas._add(a)
			end
			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

function cmb(levels)
	-- if no level is given, return unknown
	if not levels then
		return 'Unknown'
	end
	-- en dashes are the proper dash for number ranges
	-- replace all hyphens and em dashes with en
	-- strip *all* whitespace
	levels = mw.ustring.gsub(levels,'[-—]','–')
		:gsub('%s','')
		:gsub('%(',' (')
	-- split list into table
	local vals = mw.text.split(levels,',')
	-- recreate the list string to ensure consistent formatting
	local numstr = ''
	for i, v in ipairs(vals) do
		-- To prevent any possible confusion with formatted numbers
		-- elements should be separated with semicolons followed by a space
		numstr = numstr .. v .. '; '
	end
	-- removes the final separator, because it's redundant
	numstr = numstr:sub(1,-3)
	return numstr
end

return p