Module:Upgrades

local p = {} local h = {}

local DataTable = require("Module:Data Table") local utilsArg = require("Module:UtilsArg") local utilsCargo = require("Module:UtilsCargo") local utilsPage = require("Module:UtilsPage") local utilsTable = require("Module:UtilsTable")

local CARGO_TABLE = "Upgrades" local CATEGORY_INVALID_ARGS = require("Module:Constants/category/invalidArgs") local STORING_PAGE = "Upgrading"

function p.DataTable(frame) return DataTable.Main(frame, h.store, {		requiredColumns = {"Item", "Upgrade"}	}) end function h.store(args) local categories = "" local baseItemLookup = {} local levels = {} local columnIndices = utilsTable.invert(args.headers) local itemColumn = columnIndices["Item"] local upgradeColumn = columnIndices["Upgrade"] local materialsColumn = columnIndices["Materials"] if not itemColumn or not upgradeColumn then return end

for i, row in ipairs(args.rows) do		local item = row.cells[itemColumn].raw local upgrade = row.cells[upgradeColumn].raw local materials = materialsColumn and row.cells[materialsColumn].raw local baseItem = item while baseItemLookup[baseItem] do			baseItem = baseItemLookup[baseItem] end baseItemLookup[upgrade] = baseItem local level = (levels[baseItem] or 1) + 1 levels[baseItem] = level if not utilsPage.inNamespace("Template") then mw.getCurrentFrame:callParserFunction({				name = "#cargo_store:_table="..CARGO_TABLE,				args = {					game = args.game,					baseItem = baseItem,					level = level,					item = item,					upgrade = upgrade,					materials = materials,				}			}) end end end

function p.Query(frame) local args, err = utilsArg.parse(frame:getParent.args, p.Templates["Upgrades"]) if err then return err.categoryText end local categories = "" local queryResults = utilsCargo.query(CARGO_TABLE, "upgrade", {		where = utilsCargo.allOf({ game = args.game, baseItem = args.baseItem, })	})	local upgrades = utilsTable.map(queryResults, "upgrade") local upgradeLookup = utilsTable.invert(upgrades) if #upgrades == 0 then h.warn(string.format(" item   is not a base item with upgrades stored on %s", args.game, args.baseItem, STORING_PAGE)) categories = categories.."" end

local dataTable, dataTableCategories = DataTable.copy({		fromPage = STORING_PAGE,		game = args.game,	}, function(dataRow)		local upgrade = dataRow.cells[2].raw		return upgradeLookup[upgrade]	end)

return dataTable, dataTableCategories..categories end

function h.warn(msg) local utilsError = require("Module:UtilsError") utilsError.warn(msg) end

p.Templates = { ["Data Table/Upgrades"] = { purpose = string.format("An extension of for use on the %s page. Displays information about item upgrades and store that information for use by Template:Upgrades.", STORING_PAGE), format = "block", storesData = true, usage = "See Template:Data Table for more information.", paramOrder = {"name", "game", "columns", "..."}, params = { name = { type = "string", required = true, desc = "See the  parameter of Template:Data Table.", },			game = { type = "string", required = true, desc = "See the  parameter of Template:Data Table.", },			columns = { type = "string", desc = string.format("See the  parameter of Template:Data Table. For this template to function correctly, the two first columns must be   and  ."), },			["..."] = {				name = "cells", placeholder = "cell", desc = "See the  parameter of Template:Data Table.", }		}	},	["Upgrades"] = { purpose = string.format("Displays information on upgrades to a given item by copying the information from the %s page.", STORING_PAGE), usesData = true, params = { [1] = {				name = "game", type = "string", desc = "A game code from Data:Franchise.", },			[2] = {				name = "baseItem", type = "wiki-page-name", desc = "A wiki article name referring to an item with upgrades.", },		},		examples = { vertical = true, {"SS", "Wooden Shield"}, {				desc = "Only works for base items", args = {"SS", "Banded Shield"} },		},	} }

return p