Module:Armor/Query

local p = {} local h = {}

local DataTable = require("Module:Data Table") local Term = require("Module:Term") local utilsArg = require("Module:UtilsArg") local utilsCargo = require("Module:UtilsCargo") local utilsString = require("Module:UtilsString") local utilsTable = require("Module:UtilsTable")

local CATEGORY_INVALID_ARGS = ""

function h.err(errMsg, warnMsg) local utilsError = require("Module:UtilsError") return utilsError.error(errMsg, warnMsg) end

function p.MaterialArmors(frame) local args, err = utilsArg.parse(frame:getParent.args, p.Templates["Material Armors"]) local categories = err and err.categoryText or "" if not args.game then return categories end

-- Legacy code. Going forward, the material name should always be specified to avoid bugs that can occur when the same term can refer to multiple subjects local currentPage = mw.title.getCurrentTitle.subpageText -- subpageText for the benefit of sandboxes local material = args.material or Term.fetchTerm(currentPage, args.game) or currentPage local armorRows = utilsCargo.query("ArmorLevels", "armor, level, materials", {		where = "game = '"..args.game.."' AND materials LIKE '%"..utilsCargo.escape(material).."%'",		orderBy = "armor, level"	})

-- Do an exact match on the material name as the SQL query above will return rows containing "Giant Ancient Core" for material "Ancient Row" -- At the same time, compute the total of this material that is required to upgrade everything local totalAmountRequired = 0 armorRows = utilsTable.filter(armorRows, function(armorRow)		local materials = utilsString.split(armorRow.materials)		armorRow.materialAmounts = {}		for i, armorMaterial in ipairs(materials) do			local amount = string.match(armorMaterial, "^(%d+)%s+")			local materialName = string.gsub(armorMaterial, "^(%d+)%s+", "")			if materialName == material then				totalAmountRequired = totalAmountRequired + tonumber(amount)				return true			end		end	end)

if #armorRows == 0 then return h.err("Data not found", string.format("No upgrade data exists for material  on page Data:Armor/%s", material, args.game)), CATEGORY_INVALID_ARGS end local tableRows = utilsTable.map(armorRows, function(armorRow)		return {			cells = {				armorRow.armor,				armorRow.level,				armorRow.materials,			}		}	end) local dataTable = DataTable.printTable(tableRows, {		game = args.game,		columns = {"Armor [Rowspan][Term]", "Level", "Materials [Amounts]"},		sortable = false,	})

local plural = Term.plural(material, args.game) local totalRequired = string.format("Total %s required: %d", plural, totalAmountRequired)

local result = dataTable.." "..totalRequired.." " return result, categories end

p.Templates = { ["Material Armors"] = { params = { [1] = {				name = "game", required = true, type = "string", desc = "A game code from Data:Franchise.", },			[2] = {				name = "material", required = true, type = "wiki-page-name", desc = "A wiki page name referring to a particular Material as named in .", },		},	} }

return p