Módulo:Infobox cooking

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

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

--<nowiki>

--[=[<pre>
-- Implements [[Template:Infobox cooking]]
--]=]

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 mapicons = require('Módulo:Map_clickpic')
local editbutton = require('Módulo:Edit button')
local onmain = require('Módulo:Mainonly').on_main

local cookimg = '[[Arquivo:Culinária ícone.png|21x21px|link=Culinária|alt=Culinária]]'

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)
				-- price disabled
				-- use N/A for display and 0 for calculations
				elseif yesno(price_v, true) == false then
					price_raw = 0
					price = 'N/D'
				-- everything else
				else
					price_raw = geprice(name)
					price = commas._add(price_raw)
				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
	
	-- Fetching and formatting parameters starting here
	local item_name,item_image,
		ticks,level,
		burnlevel,gauntlets,
		fireburn,firegaunt,
		cookmatic,
		experience,xpfire,
		tools,method,
		quest,
		servings,lphealed,
		instructions,totalvar = params.defaults{
				{args.nome,mw.title.getCurrentTitle().text},
				{args.imagem,nil},
				{args.ticks,editbutton('?')},
				{args['nível'],editbutton('?')},
				{args['nívelsemqueimar'],nil},
				{args.manoplas,nil},
				{args.queimarfogueira,nil},
				{args.manoplasfogueira,nil},
				{args['cozinhomático'],nil},
				{args['exp'],editbutton('?')},
				{args['expfogueira'],nil},
				{args.ferramentas,nil},
				{args['método'],nil},
				{args['missão'],nil},
				{args['porções'],1},
				{args.cura,nil},
				{args['instruções'],nil},
				{args.totalvar,nil}
			}
	local item_link = mw.title.getCurrentTitle().text
	local members = yesno(args.membros) and '[[Arquivo:P2P ícone.png|21px|link=]] Somente para membros' or '[[Arquivo:F2P ícone.png|21px|link=]] Gratuito para jogar'
	
	-- Add [[File: and |link=...]] to image, if it's defined
	if item_image then
		item_image = '[[Arquivo:'..item_image:gsub('Arquivo:','')..'|link='..item_link..']]'
	-- 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
	
	-- Heat source
	local heat = yesno(args.heat,true)
	if heat == nil then
		heat = true
	end
	-- Requires range or fire
	if yesno(args['fogão'],false) == true then
		heat = 'fogão'
	elseif yesno(args['fogão'],false) == false then
		heat = 'fogueira'
	end
	-- Brewing
	local brewing = false
	if yesno(args.brewing,false) then
		brewing = true
		heat = false
	end
	-- Water source
	local water = false
	if yesno(args['água'],false) then
		water = true
	end

	-- Tables to pass; call above functions
	local materials = mat_list()
	
	-- Check servings  and lp are numbers
	if tonumber(servings,10) then
		servings = tonumber(servings,10)
	else
		servings = 1
	end
	if lphealed and tonumber(lphealed,10) then
		lphealed = tonumber(lphealed,10)
	else
		lphealed = nil
	end
	
	-- Burn level
	if burnlevel then
		if tonumber(burnlevel,10) then
			burnlevel = tonumber(burnlevel,10)
		elseif string.lower(burnlevel) == 'nenhum' or yesno(burnlevel) == false then
			burnlevel = nil
		end
	else
		burnlevel = nil
	end
	
	-- Look for GE price unless disabled, nil if disabled
	local item_ge_v = string.lower(args.geValue or 'sim')
	local item_gep
	if item_ge_v == 'gemw' then
		item_gep = geprice(item_name)
	elseif tonumber(item_ge_v,10) then
		item_gep = tonumber(item_ge_v,10)
	elseif yesno(item_ge_v) == false then
		item_gep = nil
	else
		item_gep = geprice(item_name)
	end
	
	return p._main{ frame,
		item_name,
		item_image,
		item_link,
		item_gep,
		level,
		burnlevel,
		gauntlets,
		fireburn,
		firegaunt,
		cookmatic,
		experience,
		xpfire,
		tools,
		brewing,
		water,
		heat,
		quest,
		members,
		ticks,
		method,
		materials,
		instructions,
		servings,
		lphealed,
		totalvar
	}
end

function p._main(...)
	-- Unpack parameters
	local frame,
		item_name,item_image,
		item_link,item_gep,
		level,burnlevel,
		gauntlets,fireburn,
		firegaunt,cookmatic,
		experience,xpfire,
		tools,brewing,water,
		heat,quest,members,
		ticks,method,
		materials,instructions,
		servings,lphealed,
		totalvar = unpack(...)
	
	local ret = mw.html.create('table')
	ret:addClass('wikitable')
		:css({ width = '50%',
			['max-width'] = '450px' })
	
	-- Caption header
	local caption = item_image..' '..item_name
	if method then
		caption = caption..string.format('<br /><i>(%s)</i>', method)
	end
	ret:tag('caption'):wikitext(caption):done()
	
	ret	:tag('tr')
			:css('text-align','center')
			:tag('th')
				:css('width','20%')
				:wikitext('Nível de '..cookimg)
			:done()
			:tag('th')
				:css('width','20%')
				:wikitext(cookimg..' EXP')
			:done()
			:tag('th')
				:css('width','30%')
				:attr('title','Tiques por ação')
				:attr('colspan','2')
				:css('cursor', 'help')
				:wikitext('[[Arquivo:Ampulheta.png|link=]]')
			:done()
			:tag('th')
				:css('width','30%')
				:css('cursor', 'help')
				:attr('title','Preço médio do Mercado Geral')
				:attr('colspan','2')
				:wikitext('[[Arquivo:Criar-X MG ícone.png|link=]]')
			:done()
		:done()
	
	
	local xpdisp = experience
	if xpfire then
		xpdisp = '<span title="EXP normal" style="cursor: help;">'..experience..' </span><span title="Cozinhando na fogueira" style="cursor: help;"> ('..xpfire..')</span>'
	end
	local ticktd = mw.html.create('td')
		:attr('colspan','2')
	if tonumber(ticks,10) then
		local secs = tonumber(ticks,10) * 0.6
		ticktd:attr('title',ticks..' tiques ('..secs..' segundos) por ação')
			:css('cursor', 'help')
			:wikitext(ticks..' ('..secs..'s) ')
	else
		ticktd:wikitext(ticks)
	end
	local gedisp = '-'
	if item_gep then
		gedisp = commas._add(item_gep)
	end
	ret	:tag('tr')
			:css('text-align','center')
			:tag('td')	:wikitext(level)						:done()
			:tag('td')	:wikitext(xpdisp)						:done()
			:node(ticktd)
			:tag('td')	:attr('colspan','2'):wikitext(gedisp)	:done()
		:done()
	
	if lphealed then
		local lptxt = '<span title="Pontos vitais recuperados" style="cursor: help;">'..lphealed..'</span>'
		if servings > 1 then
			lptxt = '<span title="Por porção" style="cursor: help;">'..commas._add(lphealed)..' </span><span title="Total" style="cursor: help;"> ('..commas._add(lphealed * servings)..')</span>'
		end
		ret:tag('tr')
			:tag('th')
				:wikitext('Porções')
			:done()
			:tag('td')
				:attr('colspan','2')
				:wikitext(servings)
			:done()
			:tag('th')
				:attr('title','Pontos vitais')
				:css('cursor', 'help')
				:wikitext('[[Arquivo:Condição Física ícone.png|21x21px|link=Pontos vitais|alt=Pontos vitais]] HP')
			:done()
			:tag('td')
				:attr('colspan','2')
				:wikitext(lptxt)
			:done()
	elseif servings > 1 then
		ret:tag('tr')
			:tag('th')
				:wikitext('Porções')
			:done()
			:tag('td')
				:attr('colspan','5')
				:wikitext(servings)
			:done()
	end
	
	ret:tag('tr')
			:css('text-align','center')
			:tag('th')
				:attr('colspan','6')
				:wikitext('Requisitos')
			:done()
		:done()
	
	if gauntlets or cookmatic then
		local gctitle = '[[Arquivo:Manoplas de Culinária.png|21x21px|link=Manoplas de Culinária|alt=Manoplas de Culinária]] Manoplas'
		if cookmatic then
			gctitle = '[[Arquivo:Fogão de Cozinha (Castelo de Lumbridge).png|21x21px|link=Fogão de Cozinha (Castelo de Lumbridge)|alt=Cozin-o-mático 25]] Cozin-o-mático 25'
		end
		ret:tag('tr')
			:tag('td')
				:attr('colspan','2')
				:wikitext('Para de queimar no nível')
			:done()
			:tag('td')
				:css('text-align','center')
				:wikitext(burnlevel or editbutton('?'))
			:done()
			:tag('td')
				:attr('colspan','2')
				:wikitext(gctitle)
			:done()
			:tag('td')
				:css('text-align','center')
				:wikitext(gauntlets or cookmatic)
			:done()
	elseif burnlevel or heat then
		ret:tag('tr')
			:tag('td')
				:attr('colspan','5')
				:wikitext('Para de queimar no nível')
			:done()
			:tag('td')
				:css('text-align','center')
				:wikitext(burnlevel or editbutton('?'))
			:done()
	end
	if fireburn or firegaunt then
		if firegaunt then
			ret:tag('tr')
				:tag('td')
					:attr('colspan','2')
					:wikitext('[[Arquivo:Fogueira ícone mapa.png|21x21px|link=Fire|alt=Fire]] Nível para queimar (fire)')
				:done()
				:tag('td')
					:css('text-align','center')
					:wikitext(fireburn or editbutton('?'))
				:done()
				:tag('td')
					:attr('colspan','2')
					:wikitext('[[Arquivo:Fogueira ícone mapa.png|21x21px|link=Fire|alt=Fire]][[Arquivo:Cooking gauntlets.png|21x21px|link=Cooking gauntlets|alt=Cooking gauntlets]] Gauntlets (f)')
				:done()
				:tag('td')
					:css('text-align','center')
					:wikitext(firegaunt or editbutton('?'))
				:done()
		else
			ret:tag('tr')
				:tag('td')
					:attr('colspan','5')
					:wikitext('[[Arquivo:Fogueira ícone mapa.png|21x21px|link=Fire|alt=Fire]] Burn level (fire)')
				:done()
				:tag('td')
					:css('text-align','center')
					:wikitext(fireburn or editbutton('?'))
				:done()
		end
	end
	-- Quest requirement
	if quest then
		ret:tag('tr')
			:tag('td')
				:attr('colspan','6')
				:wikitext(skillpic('missão')..' '..quest)
			:done()
		:done()
	end
	ret:tag('tr'):
			tag('td')
				:attr('colspan','6')
				:wikitext(members)
			:done()
		:done()
	if heat == 'fogão' then
		ret:tag('tr')
			:tag('td')
				:attr('colspan','6')
				:wikitext(mapicons._main('Fogão')..' Só pode ser cozido em um [[fogão]]')
	elseif heat == 'fogueira' then
		ret:tag('tr')
			:tag('td')
				:attr('colspan','6')
				:wikitext(mapicons._main('Fogueira')..' Só pode ser cozido em uma [[fogueira]]')
	elseif heat then
		ret:tag('tr')
			:tag('td')
				:attr('colspan','6')
				:wikitext('Requer um [[Fogão|fogão]] '..mapicons._main('Fogão')..' ou uma [[Fogueira|fogueira]] '..mapicons._main('Fogueira'))
	end
	if brewing then
		ret:tag('tr')
			:tag('td')
				:attr('colspan','6')
				:wikitext(mapicons._main('Brewing')..' Can only be brewed at a [[Brewery]]')
	end
	if water then
		ret:tag('tr')
			:tag('td')
				:attr('colspan','6')
				:wikitext(mapicons._main('Fonte de água')..' Deve ser feito em uma [[fonte de água]]')
	end
	if tools then
		ret:tag('tr')
			:tag('td')
				:wikitext('Ferramentas')
			:done()
			:tag('td')
				:attr('colspan','5')
				:wikitext(tools)
	end
	
	ret:tag('tr')
		:tag('th')
			:attr('colspan','6')
			:css('text-align','center')
			:wikitext('Ingredientes')
		:done()
	:done()
	:tag('tr')
		:tag('th')
			:attr('colspan','3')
			:wikitext('Objeto')
		:done()
		:tag('th')
			:css('width','15%')
			:wikitext('Quantidade')
		:done()
		:tag('th')
			:css('width','15%')
			: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')
				:attr('colspan','2')
				: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','6')
					:css({ ['font-style'] = 'italic',
						['text-align'] = 'center' })
					:wikitext('Ingredientes não listados '..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','5')
				:css('text-align','right')
				:wikitext('Preço total')
			:done()
			:tag('td')
				:css('text-align','right')
				:wikitext(commas._add(total_price))
			:done()
		:done()
	end
	if type(totalvar) == 'string' and #totalvar > 0 then
		mw.ext.VariablesLua.vardefine(totalvar, total_price)
	end
	
	-- Profit
	if valued_items >= 1 and item_gep then
		local profit = item_gep - total_price
		local profclass = 'text-green'
		if profit < 0 then
			profclass = 'text-red'
		end
		ret:tag('tr')
			:tag('th')
				:attr('colspan','5')
				:css('text-align','right')
				:wikitext('Lucro')
			:done()
			:tag('td')
				:css('text-align','right')
				:addClass(profclass)
				:wikitext(commas._add(profit))
			:done()
		:done()
	end
	
	if instructions then
		ret:tag('tr')
			:tag('th')
				:attr('colspan','6')
				:css('text-align','center')
				:wikitext('Instruções')
			:done()
		:done()
		:tag('tr')
			:tag('td')
				:attr('colspan','6')
				:wikitext(instructions)
	end
	
	-- SMW
	if onmain then
		local smw = {}
		local jsonmats = {}
		if materials then
			smw['Made from item'] = {}
			for _, v in ipairs(materials) do
				table.insert(jsonmats, {
					name = v.name_raw,
					quantity = v.quantity,
					image = v.image_raw
				})
				table.insert(smw['Made from item'],v.name_raw)
			end
		end
		
		if tonumber(ticks,10) then
			smw['Production ticks'] = ticks
		end
		
		local facilities = ''
		if heat == 'range' then
			facilities = 'Range'
		elseif heat == 'fire' then
			facilities = 'Fire'
		elseif heat then
			facilities = 'Range or Fire'
		end
		if brewing then
			facilities = 'Brewing'
		end
		if water then
			facilities = 'Water source'
		end
		
		local prodjson = {
			product = item_name,
			link = item_link,
			image = item_image,
			ticks = ticks,
			improved = '',
			method = method or '',
			facility = facilities,
			mats = jsonmats,
			skill = 'Culinária',
			level = tonumber(level) or '?',
			exp = tonumber(experience) or '?',
			skill2 = '?',
			exp2 = '?',
			level2 = '?',
			quantity = 1
		}
		
		prodjson = mw.text.jsonEncode( prodjson )
		smw['Production JSON'] = prodjson
		
		mw.smw.set(smw)
	end
	
	-- Add categories
	cats = {}
	if not experience then
		table.insert(cats,'Receita faltando EXP')
	end
	if not tonumber(level,10) then
		table.insert(cats,'Receita faltando nível de habilidade')
	end
	if not tonumber(ticks,10) and string.lower(ticks) ~= 'varia' then
		table.insert(cats,'Receita faltando tiques')
	end
	for i, v in ipairs(cats) do
		if (v ~= '') then
			cats[i] = string.format('[[Categoria:%s]]',v)
		end
	end
	
	return tostring(ret)..table.concat(cats,'')
end

return p