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 = "User:PhantomCaleb/Sandbox/Upgrading"

function p.Store(frame) local categories = "" local baseItemLookup = {} local levels = {}

local dataTable, dataTableCategories = DataTable.Main(frame, nil, function(args, dataRows, columnHeaders)		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, categories = DataTable.copy({		fromPage = STORING_PAGE,		game = args.game,	}, function(dataRow)		local upgrade = dataRow.cells[2].raw		return upgradeLookup[upgrade]	end)

return dataTable, categories end

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

p.Templates = { ["Upgrades/Store"] = { purpose = string.format("For use on the %s page to display information about item upgrades and store that information for use by Template:Upgrades.", STORING_PAGE), format = "block", storesData = true, usage = "This template is an extension of Tempalte:Data Table.",		paramOrder = {"name", "game", "detailColumns", "..."},		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.",			},			detailColumns = {				type = "string",				desc = "See the   parameter of Template:Data Table.",			},			["..."] = {				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