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, h.extensionArgs) end function h.extensionArgs(args) return { requiredColumns = {"Item", "Upgrade"} } end function h.store(args, rows) local categories = "" local baseItemLookup = {} local levels = {}

for i, row in ipairs(rows) do		local item = row["Item"] local upgrade = row["Upgrade"] if item and item ~= "" and upgrade and upgrade ~= "" then 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 = row["Materials"],						location = row["Location"],						cost = row["Cost"],					}				}) end end end end

function p.TreasureUpgrades(frame) local args, err = utilsArg.parse(frame:getParent.args, p.Templates["Treasure Upgrades"]) if err then return err.categoryText end local treasure = args.treasure or mw.title.getCurrentTitle.subpageText local upgradeRows = utilsCargo.query(CARGO_TABLE, "baseItem, level, item, upgrade, materials", {		where = utilsCargo.allOf( {				_pageName = STORING_PAGE, game = args.game, }, 			"materials HOLDS LIKE '%"..utilsCargo.escape(treasure).."'" ),		sortBy = "baseItem, level",	}) if #upgradeRows == 0 then local utilsError = require("Module:UtilsError") return utilsError.error("Data not found", string.format("No data found for Treasure  on page %s", treasure, STORING_PAGE)) end local tableRows = utilsTable.map(upgradeRows, function(upgrade)		return {			cells = {upgrade.item, upgrade.upgrade, upgrade.materials}		}	end) local dataTable = DataTable.printTable(tableRows, {		game = args.game,		columns = {"Item [Image][Term]", "Upgrade [Image][Term]", "Materials [Amounts]"}	}) return dataTable 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, "level, item, upgrade, materials, cost, location", {		where = utilsCargo.allOf({ _pageName = STORING_PAGE, game = args.game, baseItem = args.baseItem, }),		orderBy = "level",	}) if #queryResults == 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 hasLocations = utilsTable.find(queryResults, "location") local columns = {"Item [Image][Term]", "Upgrade [Image][Term]", "Materials [Amounts]", "Cost [Rupees]"} if hasLocations then table.insert(columns, "Location [Term]") end local rows = {} for i, upgrade in ipairs(queryResults) do		local row = {} row.cells = { upgrade.item, upgrade.upgrade, upgrade.materials, upgrade.cost == 0 and "N/A" or upgrade.cost, }		if hasLocations then table.insert(row.cells, upgrade.location) end table.insert(rows, row) end local dataTable = DataTable.printTable(rows, {		game = args.game,		columns = columns,	}) return dataTable, 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.", }		}	},	["Treasure Upgrades"] = { description = string.format("Lists upgrades that require a particular Treasure."), purpose = string.format("Lists upgrades that require a particular Treasure based on the data from the %s page. See also Template:Material Armors.", STORING_PAGE), params = { [1] = {				name = "game", required = true, type = "string", desc = "A game code from Data:Franchise.", },			[2] = {				name = "treasure", type = "wiki-page-name", desc = "An article name referring to a particular Treasure. Defaults to the current page.", },		},		examples = { vertical = true, {"SS", "Amber Relic"}, {"SS", "Not a Treasure"}, },	},	["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