FANDOM


-- <pre>
local p = {}
 
local params = require('Module:Paramtest')
local lang = mw.language.getContentLanguage()
local commas = function (n) return lang:formatNum(tonumber(n)) end
 
--bg, txt, sort
local rarities = {
	always = {'#8FD7FA !important', '#083F66', 1},
	common = {'#98E553 !important', '#245200', 2},
	uncommon = {'#F5EB4E !important', '#524E00', 3},
	rare = {'#F6AB58 !important', '#582E00', 4},
	['very rare'] = {'#F86C67 !important', '#520800', 5},
	random = {'#F3BFF3 !important', '#801B80', 6},
	varies = {'#F3BFF3 !important', '#801B80', 6},
	discontinued = {'#CFCFCF !important', '#464646', 7}
}
 
function p.main(frame)
	local args = frame:getParent().args
	-- Params and defaults
	local name,namenotes,
		combat,cbnotes,
		quantity,quantitynotes,
		rarity,raritynotes = params.defaults{
				{args.Monster,'monster'},
				{args.Namenotes,''},
				{args.Combat,nil},
				{args.cbnotes,''},
				{args.Quantity,'unknown'},
				{args.Quantitynotes,''},
				{args.Rarity,'Unknown'},
				{args.Raritynotes,''}
			}
	rarity = params.ucflc(rarity)
	quantity = quantity:lower()
 
	-- Table row
	return p._main{name,
		namenotes,
		combat,
		cbnotes,
		quantity,
		quantitynotes,
		rarity,
		raritynotes}
end
 
function p._main(...)
	local name,namenotes,
		combat,cbnotes,
		quantity,quantitynotes,
		rarity,raritynotes = unpack(...)
	local rare_bg, rare_txt, rare_sort = unpack(rarities[rarity:lower()] or {'#FFFFFF !important', '#000000', 8})
 
	-- Clean up the lists
	quantity = qty(quantity)
	local qtysort = mw.text.split(quantity, '%D')[1]
	if qtysort == '' then
		qtysort = 0
	end
 
	combat = cmb(combat)
	name = '[['..name..']]'
	if #namenotes > 5 then
		name = name..' '..namenotes
	end
	if #cbnotes > 5 then
		combat = combat..' '..cbnotes
	end
	if #quantitynotes > 5 then
		quantity = quantity..' '..quantitynotes
	end
	if #raritynotes > 5 then
		rarity = rarity..' '..raritynotes
	end
	-- Table row creation
	local ret = mw.html.create('tr')
			:css('text-align','center')
			:tag('td')
				:css('text-align','left')
				:wikitext(name)
			:done()
			:tag('td')
				:wikitext(combat)
			:done()
			:tag('td')
				:attr('data-sort-value', qtysort)
				:wikitext(quantity)
			:done()
			:tag('td')
				:attr('data-sort-value',rare_sort)
				:css({background = rare_bg, color = rare_txt})
				:wikitext(rarity)
			:done()
	return ret:done()
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 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
			addx = commas(a)..'–'..commas(b)
			if v:find('$n') then
				addx = addx..' (noted)'
			end
			table.insert(numstr,addx)
		else
			local addx = tonumber(clean) ~= nil and commas(tonumber(clean)) or clean
			if v:find('$n') then
				addx = addx..' (noted)'
			end
			table.insert(numstr,addx)
		end
	end
	-- To prevent any possible confusion with formatted numbers
	-- elements should be separated with semicolons followed by a space
	numstr = table.concat(numstr,'; ')
	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
 
	-- 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
		table.insert(numstr,v)
	end
 
	table.sort(numstr,function (a,b)
			a = tonumber(a) or 0
			b = tonumber(b) or 0
			return a < b
		end)
	-- To prevent any possible confusion with formatted numbers
	-- elements should be separated with semicolons followed by a space
	return table.concat(numstr,'; ')
end
 
return p

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.