Module:Item Usage
From The Petit Planet Wiki
More actions
Documentation for this module may be created at Module:Item Usage/doc
local p = {}
local lib = require('Module:Feature')
local Parse = require('Module:Parser').getTemplateArgs
local Rlib = require('Module:Recipe').param
local Item = require('Module:Item')._main
function p.main(frame)
local args = require('Module:Arguments').getArgs(frame, {
parentFirst = true,
wrappers = {'Template:Item Usage'}
})
local item = args[1] or mw.title.getCurrentTitle().rootText
local count = 0
local craftType = args['type']
local out = mw.html.create()
if lib.isNotEmpty(frame.args.pages) and frame.args.pages:find('%$%$%$') then
local valids = {}
for page in lib.gsplit(frame.args.pages, '$$$') do
local data = Parse(page, { only='Recipe' } )
if type(data[1]) ~= 'table' then data = {data} end
for _, call in ipairs(data) do
if lib.isNotEmpty(call['sort']) and lib.isNotEmpty(call[item]) then
for name in lib.gsplit(call['sort'], lib.ternary(call['sort']:find(Rlib.sSortDelim2), Rlib.sSortDelim2, Rlib.sSortDelim)) do
if (lib.isNotEmpty(name) and name == item) then
valids[page] = valids[page] or {}
table.insert(valids[page], call)
count = count + 1
end
end
end
end
end
if craftType == 'Furnishing Set' then
out:wikitext('There ', lib.ternary(count > 1,'are', 'is'), ' <b>', tostring(count), '</b> [[Furnishing Set]]', lib.ternary(count > 1, 's', ''), ' that use', lib.ternary(count > 1, '', 's'), ' [[', item , ']] as a component:\n')
else
out:wikitext('There ', lib.ternary(count > 1,'are', 'is'), ' <b>', tostring(count), '</b> use', lib.ternary(count > 1, 's', ''), ' for [[', item , ']]:\n')
end
out:node(p._main(valids, craftType))
return out
else
if craftType == 'Furnishing Set' then
out:wikitext('No [[Furnishing Set]]s use [[', item, ']] as a component.')
else
out:wikitext('No recipes use [[', item, ']] as an ingredient.')
end
return out
end
end
function p._main(valids, craftType)
local _table = mw.html.create('table'):addClass('wikitable article-table sortable')
-- header row
local tr = _table:tag('tr')
tr:tag('th'):wikitext('Item')
tr:tag('th'):wikitext('Craft Type')
tr:tag('th'):wikitext(craftType == 'Furnishing Set' and 'Components' or 'Recipe')
-- mw.logObject(valids)
-- rest of table
for page, recipes in lib.spairs(valids) do
local tr = _table:tag('tr')
for i, recipe in pairs(recipes) do
if i == 1 then
local cell = tr:tag('td'):attr('rowspan', #valids[page])
if recipe[Rlib.sTypeArg]:find('Set') then
cell
:tag('div')
:css{['text-align'] = 'center', ['width'] = '165px'}
:wikitext('[[File:Furnishing Set ', page, ' Display.png|160x80px|alt=', page, '|link=', page, ']]<br>[[', page, ']]')
else
cell:wikitext(Item({ page, x = recipe[Rlib.sYieldArg] }))
end
else
tr = _table:tag('tr')
end
tr:tag('td'):css{['text-align'] = 'center'}:wikitext('[[', recipe[Rlib.sTypeArg], ']]')
local ingredients = {}
for name in lib.gsplit(recipe['sort'], lib.ternary(recipe['sort']:find(Rlib.sSortDelim2), Rlib.sSortDelim2, Rlib.sSortDelim)) do
if (lib.isNotEmpty(name) and lib.isNotEmpty(recipe[name])) then
table.insert(ingredients, Item({ name, size = 20, x = recipe[name] }))
end
end
tr:tag('td'):wikitext(table.concat(ingredients, '<br>'))
end
end
-- mw.logObject(_table) --debug
return _table
end
return p