Módulo:Infobox criar

De RuneScape Wiki
Ir para: navegação, pesquisa

A documentação para este módulo pode ser criada em Módulo:Infobox criar/doc

--<nowiki>

--[=[<pre>
-- Implementa [[Predefinição:Infobox fórmula]]
--]=]

local p = {}

local yesno = require('Módulo:Yesno')
local params = require('Módulo:Paramtest')
local commas = require('Módulo:Addcommas')
local geprice = require('Módulo:Mercado')._price
local skillpic = require('Módulo:Ícone')._main
local editbutton = require('Módulo:Edit button')
local onmain = require('Módulo:Mainonly').on_main
local JSONmod = require('Módulo:JSON')

function p.main(frame)
	local args = frame:getParent().args
	--[[
	Function to handle all the materials listed
	Creates a table to pass to _main() consisting of
	* Item name, as a wikilink
	* Item image, as a wikilink, includes link to page
	* Quantity
	* Formatted price for display
	* Clean price for calculations
	If the parameter mat# is defined and not blank
	Every parameter (mat#img, mat#qty, mat#price) will be looked for as well
	--]]
	local function mat_list()
		local ret_list = {}
		for i=1,10,1 do
			local mat = args['mat'..i]
			if mat and params.has_content(mat) then
				local name = mat
				-- 2 separate prices
				-- 1 for display
				-- 1 for calculation
				local price,price_raw
				local qty = params.default_to(args['mat'..i..'quantidade'],'1')
				local img = params.default_to(args['mat'..i..'imagem'],name..'.png'):gsub('Arquivo:','')
				local price_v = params.default_to(args['mat'..i..'preço'],'/')
				-- gemw = use exchange price
				if price_v:lower() == 'gemw' then
					price_raw = geprice(name)
					price = commas._add(price_raw)
				-- parseable as number = use that
				elseif tonumber(commas._strip(price_v),10) then
					price_raw = tonumber(commas._strip(price_v),10)
					price = commas._add(price_raw)
				-- Some untradeables have a distinct market price
				-- because of their materials
				-- The template {{GETotal}} will help here
				-- Look for template call
				elseif price_v:find('{{.*}}') then
					price_raw = frame:preprocess(price_v)
					price = commas._add(price_raw)
				-- everything else
				-- use N/A for display and 0 for calculations
				else
					price_raw = 0
					price = 'N/A'
				end

				table.insert(ret_list, {
					name = '[['..name..']]',
					name_raw = name,
					price = price,
					price_raw = price_raw,
					quantity = qty,
					image = '[[Arquivo:'..img..'|link='..name..']]',
					image_raw = img
				} )
			end
		end
		return ret_list
	end
	--[[
	Function to get a list of additional requirements, should there be any
	Will take non-blank parameters and simply paste them as is into a table
	Which will then be passed into the _main() function
	--]]
	local function get_other_reqs()
		local req_list = {}
		for i=1,5,1 do
			local req = args['misc'..i]
			if req and req:find('%S') then
				table.insert(req_list,req)
			end
		end
		return req_list
	end
	-- Fetching and formatting parameters starting here
	local item_name,item_image,
		skill,level,experience = params.defaults{
				{args.nome,mw.title.getCurrentTitle().text},
				{args.imagem,nil},
				{args.habilidade,nil},
				{args.nivel,editbutton('?')},
				{args.habilidade_exp,nil}
			}

	local skill2,level2,experience2,
		quest = params.defaults{
				{args.habilidade_sec,nil},
				{args.nivel_sec,nil},
				{args.habilidade_sec_exp,nil},
				{args.missao,nil}
			}
	-- Add [[File: and |link=...]] to image, if it's defined
	if item_image then
		item_image = '[[Arquivo:'..item_image:gsub('Arquivo:','')..'|link='..item_name..']]'
	-- anti-redlink
	elseif mw.title.getCurrentTitle().text == 'Infobox criar/preload'
			or mw.title.getCurrentTitle().text == 'PreloadPage/Orphanage' then
		item_image = ''
	else
		item_image = '[[Arquivo:'..item_name..'.png|link='..item_name..']]'
	end
	-- Allow amount to be removed if it's 1
	local item_amount = tonumber(args.quantidade,10) or 1

	-- Tables to pass; call above functions
	local materials = mat_list()
	local misc_reqs = get_other_reqs()

	-- Look for gemw in the parameter, otherwise nil
	local item_ge_v = string.lower(args.mercado or '')
	local item_gep
	if item_ge_v == 'gemw' then
		item_gep = geprice(item_name)
	elseif item_ge_v == 'não' then
		item_gep = 'não'
	else
		item_gep = nil
	end

	-- Only accept "yes" or "no"
	-- Anything else defaults to no
	-- Should this default to unknown instead?
	local membros = yesno(args.membros) and 'Sim' or 'Não'

	-- Pass parameters to _main
	return p._main{ item_name,
		item_image,
		item_amount,
		materials,
		misc_reqs,
		skill,
		level,
		experience,
		skill2,
		level2,
		experience2,
		item_gep,
		membros,
		quest
	}
end

--
-- Main
--
function p._main(...)
	-- Unpack parameters
	local item_name,item_image,item_amount,
		materials,misc_reqs,
		skill,level,experience,
		skill2,level2,experience2,item_gep,
		membros,quest = unpack(...)

	local ret = mw.html.create('table')
			:addClass('wikitable')
			:css({ width = '50%',
				['max-width'] = '450px' })
	-- Caption header
	local caption = item_image..' '..item_name
	if item_amount > 1 then
		caption = caption..' ×'..item_amount
	end
	ret:tag('caption'):wikitext(caption):done()

	-- Creation of row 1
	-- Columns 1 and 5
	local row1 = ret:tag('tr')
			:css('text-align','center')

	local row1a = row1:tag('th')
		:css('width','20%')
		:attr('colspan','2')

	if skill then
		if skill:lower() == 'no' then
			row1a:wikitext('-')
		else
			row1a:wikitext(skillpic(skill))
		end
	else
		row1a:wikitext('\'\'?\'\' '..editbutton())
	end

	local row1e = row1:tag('th')
		:css('width','20%')
		:attr('colspan','2')

	if skill2 and not (skill2:lower() == 'no') then
		row1e:wikitext(skillpic(skill2))
	else
		row1e:wikitext('-')
	end

	row1:tag('th')
		:attr('title','Preço médio do Mercado Geral')
		:css('width','20%')
		:wikitext('[[Arquivo:Criar-X MG ícone.png|link=]]')
		:done()

	-- Creation of row 2
	local row2 = ret:tag('tr')
		:css('text-align','center')
	local row2a = row2:tag('td')
			:attr('colspan','2')
	local row2b = row2:tag('td')
			:attr('colspan','2')
	local row2c = row2:tag('td')

	if skill and skill:lower() == 'no' then
		row2a:wikitext('-')
	elseif experience then
		row2a:wikitext(commas._add(experience)..' EXP')
	else
		row2a:wikitext('\'\'?\'\' '..editbutton())
	end

	if experience2 then
		row2b:wikitext(commas._add(experience2)..' EXP')
	else
		row2b:wikitext('-')
	end

	if item_gep then
		if item_gep == 'não' then
			row2c:attr('title','Não pode ser vendido no Mercado Geral')
				:wikitext('-')
		elseif item_amount > 1 then
			row2c:attr('title',commas._add(item_gep)..' moedas cada')
				:wikitext(commas._add(item_gep * item_amount))
		else
			row2c:wikitext(commas._add(item_gep))
		end
	else
		row2c:wikitext('\'\'?\'\' '..editbutton())
	end

	-- Add "Requirements" header
	ret:tag('tr')
			:tag('th')
				:attr('colspan','5')
				:wikitext('Requisitos')
			:done()
		:done()
	-- Skill requirement 1
	if skill and not (skill:lower() == 'não') then
		ret:tag('tr')
			:tag('td')
				:attr('colspan','4')
				:wikitext(skillpic(skill)..' Nível de '..params.ucflc(skill))
			:done()
			:tag('td')
				:css('text-align','center')
				:wikitext(level)
			:done()
		:done()
	end
	-- Skill requirement 2
	if skill2 and level2 then
		ret:tag('tr')
			:tag('td')
				:attr('colspan','4')
				:wikitext(skillpic(skill2)..' Nível de '..params.ucflc(skill2))
			:done()
			:tag('td')
				:css('text-align','center')
				:wikitext(level2)
			:done()
		:done()
	end
	
	-- Quest requirement
	if quest then
		ret:tag('tr')
			:tag('td')
				:attr('colspan','5')
				:wikitext('{{missão|'..quest..'}}')
			:done()
		:done()
	end

	-- Miscellaneous requirements
	for i, v in ipairs(misc_reqs) do
		ret:tag('tr')
			:tag('td')
				:attr('colspan','5')
				:wikitext(v)
			:done()
		:done()
	end

	-- Members requirement
	ret:tag('tr')
			:tag('td')
				:attr('colspan','4')
				:wikitext('[[Ficheiro:P2P ícone.png|20px|link=]] Apenas para membros')
			:done()
			:tag('td')
				:css('text-align','center')
				:wikitext(membros)
			:done()
		:done()

	-- Materials header
	ret:tag('tr')
		:tag('th')
			:attr('colspan','5')
			:wikitext('Materiais')
		:done()
	:done()
	:tag('tr')
		:tag('th')
			:attr('colspan','2')
			:wikitext('Objeto')
		:done()
		:tag('th')
			:attr('title','Quantidade')
			:wikitext('Quant.')
		:done()
		:tag('th')
			:wikitext('Preço')
		:done()
		:tag('th')
			:wikitext('Total')
		:done()
	:done()

	-- Materials
	local total_price = 0
	local valued_items = 0
	-- Iterates through the tables in the table of materials
	for i, v in ipairs(materials) do
		-- Fetch all the variables
		local mat_name = v.name
		local mat_price = v.price
		local mat_raw = v.price_raw
		local mat_ttl
		local mat_qty = v.quantity
		local mat_img = v.image
		-- If no price is given for item, delete total
		-- Otherwise quantity * price each
		if mat_raw == 0 then
			mat_ttl = '-'
		else
			mat_ttl = mat_qty * mat_raw
		end
		-- If item total is a number
		-- Add it to the overall total
		-- Indicate this by incrementing valued item count
		-- Done with total calculations, add commas to total for formatting
		if type(mat_ttl) == 'number' then
			total_price = total_price + mat_ttl
			mat_ttl = commas._add(mat_ttl)
			if mat_raw > 0 then
				valued_items = valued_items + 1
			end
		end
		ret:tag('tr')
			:tag('td')
				:css({ ['border-right'] = 'none',
					['text-align'] = 'center' })
				:wikitext(mat_img)
			:done()
			:tag('td')
				:css({ ['border-left'] = 'none',
					['border-right'] = 'none',
					['text-align'] = 'left' })
				:wikitext(mat_name)
			:done()
			:tag('td')
				:css({ ['border-left'] = 'none',
					['border-right'] = 'none',
					['text-align'] = 'right' })
				:wikitext(commas._add(mat_qty))
			:done()
			:tag('td')
				:css({ ['border-left'] = 'none',
					['text-align'] = 'right' })
				:wikitext(mat_price)
			:done()
			:tag('td')
				:css('text-align','right')
				:wikitext(mat_ttl)
			:done()
		:done()
	end

	-- If no materials found
	-- Indicate nothing listed, add an edit button for the page
	if #materials == 0 then
		ret:tag('tr')
				:tag('td')
					:attr('colspan','5')
					:css({ ['font-style'] = 'italic',
						['text-align'] = 'center' })
					:wikitext('Sem informação '..editbutton())
				:done()
			:done()
	end

	-- Total
	-- Hide if less than 2 items have a value
	if valued_items > 1 then
		ret:tag('tr')
			:tag('th')
				:attr('colspan','4')
				:css('text-align','right')
				:wikitext('Preço total')
			:done()
			:tag('td')
				:css('text-align','right')
				:wikitext(commas._add(total_price))
			:done()
		:done()
	end

	-- Profit
	if valued_items >= 1 then
		if item_gep and item_gep ~= 'não' then
			local profit = item_gep * item_amount - total_price
			local profclass = 'text-green'
			if profit < 0 then
				profclass = 'text-red'
			end
			ret:tag('tr')
				:tag('th')
					:attr('colspan','4')
					:css('text-align','right')
					:wikitext('Lucro')
				:done()
				:tag('td')
					:css('text-align','right')
					:addClass(profclass)
					:wikitext(commas._add(profit))
				:done()
			:done()
		end
	end

	if onmain then
		local smw_mats = {}
		local json_mats = {}
		for _, v in ipairs(materials) do
			table.insert(json_mats, {
				name = v.name_raw,
				quantity = tostring(v.quantity),
				image = v.image_raw
			})
			table.insert(smw_mats, v.name_raw)
		end

		local json = {
			product = item_name,
			image = item_image,
			mats = json_mats,
			skill = skill,
			level = tostring(tonumber(level) or '?'),
			experience = tostring(tonumber(experience) or '?')
		}
		if skill2 then
			json.skill2 = skill2
		end
		if tonumber(level2) then
			json.level2 = tostring(tonumber(level2))
		end
		if tonumber(experience2) then
			json.experience2 = tostring(tonumber(experience2))
		end
		
		json = mw.text.jsonEncode(json)

		json = JSONmod.wikiescapes(json)
		mw.smw.set({
			['Production JSON']=json,
			['Made from item']=smw_mats
		})
		--[=[
		table.insert(smw,'- - - - - - - - - -')
		table.insert(smw,'Production JSON:')
		table.insert(smw,string.format('[[Production JSON::%s]]',json))
		ret	:tag('div')
				:attr('smw-infobox-data')
				:addClass('hidden')
				:css('display','none')
				:tag('pre')
					:wikitext(table.concat(smw,'\n'))
				:done()
			:done()
			--]=]
	end

	return ret
end

return p