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.Store(frame) local categories = err and err.categoryText or "" local extensionArgs = { sortable = "false", }	local baseItemLookup = {} local levels = {} local dataTable, dataTableCategories = DataTable.Main(frame, extensionArgs, function(args, dataRows, columnHeaders)		if columnHeaders[1] ~= "Item" then			h.warn(string.format("Column 1 must be  but is:  .", columnHeaders[1] or "nil"))			categories = categories..""		end		if columnHeaders[2] ~= "Upgrade" then			h.warn(string.format("Column 2 must be   but is:  .", columnHeaders[2] or "nil"))			categories = categories..""		end		for i, row in ipairs(dataRows) do			local item = row.cells[1].raw			local upgrade = row.cells[2].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				frame:callParserFunction({ name = "#cargo_store:_table="..CARGO_TABLE, args = { game = args.game, baseItem = baseItem, level = level, item = item, upgrade = upgrade, }				})			end		end	end) return dataTable, categories..dataTableCategories 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 = { ["Upgrades/Store"] = { 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