Módulo:Sandbox/MaximusGugu2
A documentação para este módulo pode ser criada em Módulo:Sandbox/MaximusGugu2/doc
--<nowiki>
--[=[<pre>
-- Implements [[Template:Infobox recipe]]
--]=]
local p = {}
local yesno = require('Module:Yesno')
local params = require('Module:Paramtest')
local commas = require('Module:Addcommas')
local geprice = require('Module:Mercado')._price
local habilidadepic = require('Module:Ícone')._main
local mapicons = require('Module:Map_clickpic')
local editbutton = require('Module:Edit button')
local onmain = require('Module:Mainonly').on_main
-- Name mapping for special (multiple choice) items
local specmats = {
['Arenito'] = { ['Arenito (1 kg)'] = 1, ['Arenito (2 kg)'] = 1, ['Arenito (5 kg)'] = 1, ['Arenito (10 kg)'] = 1 },
['Energia divina'] = { ['Energia pálida'] = 2000, ['Energia cintilante'] = 1000, ['Energia resplandecente'] = 900, ['Energia luminescente'] = 850, ['Energia faiscante'] = 800,
['Energia reluzente'] = 700, ['Energia lampejante'] = 650, ['Energia lustrosa'] = 600, ['Energia anciã'] = 500, ['Energia brilhante'] = 450, ['Energia radiante'] = 400,
['Energia luminosa'] = 350, ['Energia incandescente'] = 225 },
}
--[[
Function to get the lowest price for multiple choice items
--]]
local function specprice(item)
local low = 9999999999
for i,v in pairs( specmats[item] ) do
local price = geprice(i) * v
if price <= low then
low = price
end
end
return low
end
function p.main(frame)
local args = frame:getParent().args
--[[
Function to handle all the materiais listed
Creates a table to pass to _main() consisting of
* Item name, as a wikilink
* Item image, as a wikilink, includes link to page
* quantidade
* Formatted price for display
* Clean price for calculations
If the parameter mat# is defined and not blank
Every parameter (mat#img, mat#quantidade, 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 quantidade = 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
if specmats[name] then
price_raw = specprice(name)
price = commas._add(price_raw)
else
price_raw = geprice(name)
price = commas._add(price_raw)
end
-- 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 materiais
-- 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,
quantidade = quantidade,
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,
ticks,ticks2,ticksnote,
habilidade,level,experience,
metodo,melhorada,instrucoes,totalvar = params.defaults{
{args.nome,mw.title.getCurrentTitle().text},
{args.imagem,nil},
{args.ticks,editbutton('?')},
{args.ticks2,nil},
{args.ticksnote,nil},
{args.habilidade,nil},
{args.nivel,editbutton('?')},
{args.habilidade_exp,nil},
{args.metodo,nil},
{args.melhorada,0},
{args.instrucoes,nil},
{args.totalvar,nil}
}
local item_link = mw.title.getCurrentTitle().text
local habilidade2,level2,experience2,
facility,quest = params.defaults{
{args.habilidade_sec,nil},
{args.nivel_sec,nil},
{args.habilidade_sec_exp,nil},
{args.facilidade,nil},
{args.missao,nil}
}
-- Add [[Arquivo: 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
-- Allow amount to be removed if it's 1
local item_amount = tonumber(args.quantidade,10) or 1
-- Check that melhorada is a number
if tonumber(melhorada,10) then
melhorada = tonumber(melhorada,10)
else
melhorada = 0
end
-- Tables to pass; call above functions
local materiais = mat_list()
local misc_reqs = get_other_reqs()
-- Look for gemw in the parameter, otherwise nil
local item_ge_v = string.lower(args.geValue 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 '[[Arquivo:P2P ícone.png|21px|link=]] Somente para membros' or '[[Arquivo:F2P ícone.png|21px|link=]] Acesso gratuito'
-- Pass parameters to _main
return p._main{ frame,
item_name,
item_link,
item_image,
item_amount,
ticks,
ticks2,
ticksnote,
melhorada,
metodo,
materiais,
facility,
misc_reqs,
habilidade,
level,
experience,
habilidade2,
level2,
experience2,
item_gep,
membros,
quest,
yesno(args.nosmw) or false,
instrucoes,
totalvar
}
end
--
-- Main
--
function p._main(...)
-- Unpack parameters
local frame,
item_name,item_link,
item_image,item_amount,
ticks,ticks2,ticksnote,
melhorada,metodo,
materiais,facility,misc_reqs,
habilidade,level,experience,
habilidade2,level2,experience2,item_gep,
membros,quest,nosmw,instrucoes,totalvar = unpack(...)
local hasreftag = false
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
if melhorada > 0 then
caption = caption..string.format(' <i><span title="Receita melhorada %s">(+%s)</span></i>', melhorada, melhorada)
elseif metodo then
caption = caption..string.format('<br /><i>(%s)</i>', metodo)
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%')
if habilidade then
if habilidade:lower() == 'no' then
row1a:wikitext('-')
else
row1a:wikitext(habilidadepic(habilidade))
end
else
row1a:wikitext('\'\'? habilidade\'\' '..editbutton())
end
local row1e = row1:tag('th')
:css('width','20%')
if habilidade2 and not (habilidade2:lower() == 'no') then
row1e:wikitext(habilidadepic(habilidade2))
else
row1e:wikitext('-')
end
row1:tag('th')
:attr('title','Tiques por ação')
:css('width','40%')
:attr('colspan','2')
:wikitext('[[Arquivo:Ampulheta.png|link=Tique]]')
:done()
: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')
local row2b = row2:tag('td')
local row2c = row2:tag('td')
:attr('colspan','2')
local row2d = row2:tag('td')
if habilidade and habilidade:lower() == 'no' then
row2a:wikitext('-')
elseif experience then
row2a:wikitext(commas._add(experience)..' EXP')
else
row2a:wikitext('\'\'? EXP\'\' '..editbutton())
end
if experience2 then
row2b:wikitext(commas._add(experience2)..' EXP')
else
row2b:wikitext('-')
end
if tonumber(ticks,10) then
local secs = tonumber(ticks,10) * 0.6
local note = ''
if ticksnote and string.find(ticksnote, '%S') then
if ticksnote == '1' then
ticksnote = 'Ao criar um único objeto'
end
note = frame:extensionTag{ name='ref', content = ticksnote, args = {group="recipe"} }
hasreftag = true
end
if tonumber(ticks2,10) then
local secs2 = tonumber(ticks2,10) * 0.6
row2c:tag('span')
:attr('title',ticks..' ticks ('..secs..' segundos) por ação')
:wikitext(ticks..' ('..secs..'s) ')
:done()
:wikitext(' / ')
:tag('span')
:attr('title',ticks2..' ticks ('..secs2..' segundos) por ação, ')
:wikitext('<i> '..ticks2..' ('..secs2..'s)</i> ')
:done()
:wikitext(note)
else
row2c:attr('title',ticks..' ticks ('..secs..' segundos) por ação')
:wikitext(ticks..' ('..secs..'s) '..note)
end
elseif string.lower(ticks) == 'varies' then
local note = ''
if ticksnote and string.find(ticksnote, '%S') then
note = frame:extensionTag{ name='ref', content = ticksnote, args = {group="recipe"} }
hasreftag = true
end
row2c:attr('title', 'Varies')
:wikitext('Varies ' .. note)
else
row2c:wikitext(ticks)
end
if item_gep then
if item_gep == 'no' then
row2d:attr('title','Não pode ser negociado no Mercado Geral')
:wikitext('-')
elseif item_amount > 1 then
row2d:attr('title',commas._add(item_gep)..' moedas cada')
:wikitext(commas._add(item_gep * item_amount))
else
row2d:wikitext(commas._add(item_gep))
end
else
row2d:wikitext('\'\'?\'\' '..editbutton())
end
-- Add "Requirements" header
ret:tag('tr')
:tag('th')
:attr('colspan','5')
:wikitext('Requerimentos')
:done()
:done()
-- habilidade requirement 1
if habilidade and not (habilidade:lower() == 'no') then
ret:tag('tr')
:tag('td')
:attr('colspan','4')
:wikitext(habilidadepic(habilidade)..' '..
params.ucflc(habilidade)..' nível')
:done()
:tag('td')
:css('text-align','center')
:wikitext(level)
:done()
:done()
end
-- habilidade requirement 2
if habilidade2 and level2 then
ret:tag('tr')
:tag('td')
:attr('colspan','4')
:wikitext(habilidadepic(habilidade2)..' '..
params.ucflc(habilidade2)..' nível')
: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(habilidadepic('quest')..' '..quest)
:done()
:done()
end
-- Facility requirement
function facility_link(facility_arg)
if type(facility_arg) == 'string' and mapicons.filelink(facility_arg:lower())[1] then
local link = mapicons.filelink(facility_arg:lower())[2]
local txt = mapicons.filelink(facility_arg:lower())[3]
local art = mapicons.filelink(facility_arg:lower())[4]
local vowels = {a=true, e=true, i=true, o=true, u=true}
local article
if art then
article = art
elseif vowels[string.sub(link:lower(), 1, 1)] then
article = 'an'
else
article = 'a'
end
return article.." [["..link.."|"..(txt or link:lower()).."]]"
else
return facility_arg
end
end
if facility then
ret:tag('tr')
:tag('td')
:attr('colspan','5')
:wikitext(mapicons._main(facility)..' Deve ser feito em '..facility_link(facility))
: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
-- membros requirement
ret:tag('tr')
:tag('td')
:attr('colspan','5')
:wikitext(membros)
:done()
:done()
-- materiais 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')
:wikitext('Quantidade')
:done()
:tag('th')
:wikitext('Preço')
:done()
:tag('th')
:wikitext('Total')
:done()
:done()
-- materiais
local total_price = 0
local valued_items = 0
-- Iterates through the tables in the table of materiais
for i, v in ipairs(materiais) 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_quantidade = v.quantidade
local mat_img = v.image
-- If no price is given for item, delete total
-- Otherwise quantidade * price each
if mat_raw == 0 then
mat_ttl = '-'
else
mat_ttl = mat_quantidade * 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_quantidade))
: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 materiais found
-- Indicate nothing listed, add an edit button for the page
if #materiais == 0 then
ret:tag('tr')
:tag('td')
:attr('colspan','5')
:css({ ['font-style'] = 'italic',
['text-align'] = 'center' })
:wikitext('materiais unlisted '..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
if type(totalvar) == 'string' and #totalvar > 0 then
mw.ext.VariablesLua.vardefine(totalvar, total_price)
end
-- Profit
if valued_items >= 1 then
if item_gep and item_gep ~= 'no' 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 instrucoes then
ret:tag('tr')
:tag('th')
:attr('colspan','6')
:css('text-align','center')
:wikitext('Instrucoes')
:done()
:done()
:tag('tr')
:tag('td')
:attr('colspan','6')
:wikitext(instrucoes)
end
if onmain and not nosmw then
smw = {}
table.insert(smw,'Required materiais:')
local jsonmats = {}
for _, v in ipairs(materiais) do
table.insert(jsonmats,{ name = v.name_raw, quantidade = v.quantidade, image = v.image_raw })
table.insert(smw,string.format('[[Made from item::%s]]',v.name_raw))
end
if tonumber(ticks,10) then
table.insert(smw,string.format('[[Production ticks::%s]]',ticks))
end
if tonumber(ticks2,10) then
table.insert(smw,string.format('[[Production ticks secondary::%s]]',ticks))
end
if ticksnote and string.find(ticksnote, '%S') then
table.insert(smw,string.format('[[Production ticks note::%s]]',ticksnote))
end
local tojson = {
product = item_name,
link = item_link,
image = item_image,
ticks = ticks,
melhorada = melhorada,
metodo = metodo or '',
facility = facility or '',
mats = jsonmats,
habilidade = habilidade or '?',
level = tonumber(level) or '?',
exp = tonumber(experience) or '?',
habilidade2 = habilidade2 or '?',
level2 = tonumber(level2) or '?',
exp2 = tonumber(experience2) or '?',
quantidade = item_amount
}
mw.logObject(tojson)
local json = mw.text.nowiki(mw.text.jsonEncode(tojson))
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')
:wikitext(table.concat(smw,'\n'))
:done()
end
-- Add categories
cats = {}
if not habilidade then
table.insert(cats,'Receitas faltando habilidades')
end
if habilidade and not (habilidade:lower() == 'no') then
if not experience then
table.insert(cats,'Receitas faltando EXP')
end
if not tonumber(level,10) then
table.insert(cats,'Receitas faltando nível')
end
end
if not tonumber(ticks,10) and string.lower(ticks) ~= 'varies' then
table.insert(cats,'Receitas faltando tiques')
end
if not item_gep then
table.insert(cats,'Receitas faltando valores de objetos')
end
for i, v in ipairs(cats) do
if (v ~= '') then
cats[i] = string.format('[[Categoria:%s]]',v)
end
end
local outro = ''
if hasreftag then
outro = '\n' .. frame:extensionTag{ name='references', args = {group="recipe"} }
end
return tostring(ret)..table.concat(cats,'')..outro
end
return p