Módulo:ItemDropsLine

De RuneScape Wiki
Ir para navegação Ir para pesquisar
Documentação do módulo
Esta documentação é transcluída de Módulo:ItemDropsLine/doc. [editar] [atualizar]
Módulo:ItemDropsLine requer Módulo:Paramtest.
Módulo:ItemDropsLine é solicitado por Módulo:Get drop info.

-- <pre>
local p = {}

local params = require('Módulo:Paramtest')
local lang = mw.language.getContentLanguage()
local commas = function (n) return lang:formatNum(tonumber(n)) end

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

--class, sort
local rarities = {
	sempre = { 'table-bg-blue', 1 },
	comum = { 'table-bg-green', 16 },
	incomum = { 'table-bg-yellow', 64 },
	raro = { 'table-bg-orange', 256 },
	['muito raro'] = { 'table-bg-red', 1024 },
	['aleatório'] = { 'table-bg-pink', 4096 },
	varia = { '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' }
}
-- supporting function for number => colour
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 expr(t)
	local err, val = pcall(mw.ext.ParserFunctions.expr, t)
	if err 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

local members_statuses = {
	yes = {t='[[Arquivo:P2P ícone.png|25px|link=]]', s=0, h='Monstro para membros', addclass='drops-members-yes', rowclass='drow-mem-yes'},
	no = {t='[[Arquivo:F2P ícone.png|25px|link=]]', s=2, h='Monstro de acesso gratuito', addclass='drops-members-no', rowclass='drow-mem-no'},
	both = {t='[[Arquivo:P2P ícone.png|25px|link=]]', s=1, h='Alguns variantes são de acesso gratuito outros para membros, veja o artigo', addclass='drops-members-both', rowclass='drow-mem-both'},
	['?'] = {t='N/A',s=10, addclass='table-na drops-members-unknown', rowclass='drow-mem-na'},
	['n/a'] = {t='N/a',s=10, addclass='table-na drops-members-unknown', rowclass='drow-mem-na'},
}

function p.main(frame)
	local args = frame:getParent().args
	-- Params and defaults
	local name,namenotes,
		combat,cbnotes,dtype,
		quantity,quantitynotes,
		rarity,raritynotes,
		members, membersnotes,
		raritynoteref = params.defaults{
				{args.monstro,'monstro'},
				{args.nomeNotas,''},
				{args.combate,nil},
				{args.cbnotes,''},
				{args.dtype,''},
				{args.quantidade,'Desconhecido'},
				{args.quantidadeNotas,''},
				{args.raridade,'Desconhecido'},
				{args.raridadeNotas,''},
				{args.membros,'N/A'},
				{args.membrosNotas,''},
				{args.raridadeNotasRef,false}
			}
	rarity = params.ucflc(rarity)
	quantity = quantity:lower()

	-- Table row
	return p._main{name,
		namenotes,
		combat,
		cbnotes,
		dtype,
		quantity,
		quantitynotes,
		rarity,
		raritynotes,
		members,
		membersnotes,
		raritynoteref
	}
end

function p._main(...)
	local name,namenotes,
		combat,cbnotes,dtype,
		quantity,quantitynotes,
		rarity,raritynotes,
		members, membersnotes,
		raritynoteref = unpack(...)
	local members_data = members_statuses[string.lower(members)] or members_statuses['n/a']
	members_icon = members_data.t
	
	-- missing notes to empty string (and not nil)
	namenotes = namenotes or ''
	cbnotes = cbnotes or ''
	quantitynotes = quantitynotes or ''
	membersnotes = membersnotes or ''
	raritynotes = raritynotes or ''

	local rarity_value
	if rarities[string.lower(tostring(rarity))] then
		rarity = params.ucflc(rarity)
	else
		rarity_value = expr(rarity)
	end
	local rare_class, rare_sort = '', nil
	if rarity_value == undefined then
		rare_class, rare_sort = unpack(rarities[string.lower(tostring(rarity))] 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

	-- Clean up the lists
	quantity = qty(quantity)
	local qtysort = mw.text.split(quantity, '%D')[1]
	if qtysort == '' then
		qtysort = 0
	end
	local cmbclass = ''
	local cmbsort = combat
	if combat == 'N/A' then
		cmbclass = 'table-na'
		cmbsort = 0
	else
		combat, cmbsort = cmb(combat)
	end

	-- set row class based on members status
	local trclass = members_data.rowclass
	-- set row class and level sort by drop type
	if type(dtype) == 'string' then
		if dtype == 'thieving' then
			trclass = trclass..' drop-thieving'
		elseif dtype == 'loot' then
			trclass = trclass..' drop-thieving'
		elseif dtype == 'hunter' then
			trclass = trclass..' drop-hunter'
		elseif dtype == 'produce' then
			trclass = trclass..' drop-farming'
		elseif dtype == 'reward' then
			trclass = trclass..' drop-reward'
		else
			trclass = trclass..' drop-combat'
		end
	else
		trclass = trclass..' drop-combat'
	end

	-- Check if name is already formated
	if name:match('^%[%[') then
		name = name
	else
		name = '[['..name..']]'
	end
	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 #membersnotes > 5 then
		members_icon = members_icon..' '..membersnotes
	end
	-- Table row creation
	local ret = mw.html.create('tr')
	ret		:css('text-align','center')
			:addClass(trclass)
			:tag('td')
				:css('text-align','left')
				:wikitext(name)
			:done()
			:tag('td')
				:wikitext(combat)
				:addClass(cmbclass)
				:attr('data-sort-value', cmbsort)
			:done()
			:tag('td')
				:attr('data-sort-value', qtysort)
				:wikitext(quantity)
			:done()
	local rarity_cell = ret:tag('td')
	local rarity_span = rarity_cell:tag('span')
	rarity_span:wikitext(rarity)
	rarity_cell:attr('data-sort-value', rare_sort)
		:addClass(rare_class)
	:done()
	
	if type(rarity_value) == 'number' then
		rarity_span:attr({
			['title'] = string.format('%.3g%%', 100 * rarity_value),
			['data-drop-fraction'] = rarity,
			['data-drop-oneover'] = '1/' .. sigfig(1/rarity_value, 3),
			['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 > 5 then
		rarity_cell:wikitext(raritynotes)
	end
	if raritynoteref then
		--local refname = string.gsub(name, '%[%[', '')
		--refname = string.gsub(refname, '%|.-%]%]', '')
		--refname = string.gsub(refname, '%]%]', '')
		local reftext = 'Há mais informações sobre esse objeto. Veja a página associada para detalhes.'
		local ref = mw.getCurrentFrame():getParent():extensionTag( 'ref', reftext, {name = '_dropref_', group = 'dr'})
		rarity_cell:wikitext(ref)
	end
	
	ret:tag('td')
		:attr({['data-sort-value'] = members_data.s, title = members_data.h})
		:wikitext(members_icon)
		:addClass(members_data.addclass .. ' members-column')
	:done()
	return ret:done()
end

function qty(quantity)
	-- if no quantity is given, return unknown
	if not quantity or quantity == 'desconhecido' then
		return 'Desconhecido'
	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('%(notas%)','$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 and b then
				if a > b then
					a,b = b,a
				end
				addx = commas(a)..'–'..commas(b)
			else
				addx = splitvals[1]..'–'..splitvals[2]
			end
			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 'Desconhecido'
	end
end

function cmb(levels)
	-- if no level is given, return unknown
	if not levels then
		return 'Desconhecido', 0
	end

	-- split list into table
	-- recreate the list string to ensure consistent formatting
	local numstr = {}
	local isnums = true
	for v in mw.text.gsplit(levels, '[,;]') do
		v = mw.text.trim(v)
		if tonumber(v) then
			table.insert(numstr,tonumber(v))
		else
			table.insert(numstr, v)
			isnums = false
		end
	end

	local sortval = 0
	if isnums then
		table.sort(numstr)
		sortval = numstr[1] or 0
	end
	-- To prevent any possible confusion with formatted numbers
	-- elements should be separated with semicolons followed by a space
	return table.concat(numstr,'; '), sortval
end

return p