Module:ItemGrid

local util_args = require('Module:ArgsUtil') local util_cargo = require('Module:CargoUtil') local util_html = require('Module:HtmlUtil') local util_table = require('Module:TableUtil') local util_text = require('Module:TextUtil')

local h = {}

local p = {} function p.main(frame) local args = util_args.merge(true) local itemData = h.getItemData util_table.mapRowsInPlace(itemData, h.castItemRow) local itemsByRecipe = h.gatherItemsByRecipe(itemData) h.sortItems(itemsByRecipe) return h.makeOutput(itemsByRecipe) end

function h.getItemData return util_cargo.queryAndCast({		tables = 'Items',		fields = { 'Name', 'Tier', 'Recipe' },		where = 'Recipe__full IS NOT NULL',	}) end

function h.castItemRow(row) row.Recipe = util_text.split(row.Recipe) end

function h.gatherItemsByRecipe(itemData) local itemsByRecipe = {} for _, row in ipairs(itemData) do		h.handleItem(itemsByRecipe, row) end return itemsByRecipe end

function h.handleItem(itemsByRecipe, row) h.handleItemRecipe(itemsByRecipe, row.Name, row.Recipe[1], row.Recipe[2]) h.handleItemRecipe(itemsByRecipe, row.Name, row.Recipe[2], row.Recipe[1]) end

function h.handleItemRecipe(itemsByRecipe, item, this, other) util_table.initDict(itemsByRecipe, this) util_table.initDict(itemsByRecipe[this], other) itemsByRecipe[this][other] = item end

function h.sortItems(itemsByRecipe) table.sort(itemsByRecipe) for _, item in ipairs(itemsByRecipe) do		table.sort(itemsByRecipe[item]) end end

function h.makeOutput(itemsByRecipe) local output = mw.html.create('table') :addClass('item-grid') h.printHeader(output, itemsByRecipe) h.printRows(output, itemsByRecipe) return output end

function h.printHeader(output, itemsByRecipe) local tr = output:tag('tr') tr:tag('th') for i, item in ipairs(itemsByRecipe) do		tr:tag('th') :attr('data-item-vs', i)			:addClass('item-grid-cell') :wikitext(h.formatItem(item)) end end

function h.formatItem(item) return (''):format(item, item) end

function h.printRows(output, itemsByRecipe) for _, item in ipairs(itemsByRecipe) do		local tr = output:tag('tr') :addClass('item-grid-row') tr:tag('th'):wikitext(h.formatItem(item)) h.printItemRow(tr, itemsByRecipe[item]) end end function h.printItemRow(tr, itemData) for i, item in ipairs(itemData) do		tr:tag('td') :addClass('item-grid-cell') :attr('data-item-vs', i)			:wikitext(h.formatItem(itemData[item])) end end

return p