Module:Equipment: Difference between revisions

From Zelda Wiki, the Zelda encyclopedia
Jump to navigation Jump to search
(Deal with arrows as a special case)
mNo edit summary
Line 28: Line 28:
local suffix = ""
local suffix = ""
if #equipmentTypeQuery == 0 then
if #equipmentTypeQuery == 0 then
if not string.find(args.equipment, "Arrows") then
if not string.find(args.equipment, "Arrow") then
return h.err("Invalid game/equipment combination")
return h.err("Invalid game/equipment combination")
end
end
Line 34: Line 34:
-- Arrows are a special case, are classified as Bow even if
-- Arrows are a special case, are classified as Bow even if
-- they are not in the Cargo table and have a pluralized link
-- they are not in the Cargo table and have a pluralized link
args.equipment = args.equipment:sub(1, #args.equipment - 1)
suffix = "s"
suffix = "s"
equipmentTypeQuery = { { type = "Bow" } }
equipmentTypeQuery = { { type = "Bow" } }

Revision as of 01:23, 2 May 2023

This is the main module for the following templates:
local p = {}
local h = {}

local File = require("Module:File")
local Franchise = require("Module:Franchise")
local utilsArg = require("Module:UtilsArg")
local utilsCargo = require("Module:UtilsCargo")
local utilsTable = require("Module:UtilsTable")

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

function p.Main(frame)
	local args, err = utilsArg.parse(frame:getParent().args, p.Templates["Equipment"])
	if err then
		return "", err.categoryText
	end
	
	local tableName = args.game .. "ItemProperties"  -- For now, there are
	-- different cargo tables per game, e.g. one for BotW, one for TotK
	
	local equipmentTypeQuery = utilsCargo.query(tableName, "type", {
		where = "_pageName = '" .. utilsCargo.escape(args.equipment) .. "'",
	})

	local suffix = ""
	if #equipmentTypeQuery == 0 then
		if not string.find(args.equipment, "Arrow") then
			return h.err("Invalid game/equipment combination")
		end
		
		-- Arrows are a special case, are classified as Bow even if
		-- they are not in the Cargo table and have a pluralized link
		suffix = "s"
		equipmentTypeQuery = { { type = "Bow" } }
	end
	
	local equipmentType = equipmentTypeQuery[1].type
	local icon = File.gameImage(args.game, equipmentType, "Icon", {
		size = "x18px",
	})

	local html = mw.html.create("small")
		:wikitext(icon .. " [[" .. args.equipment .. "]]" .. suffix)
	return tostring(html)
end

p.Templates = {
	["Equipment"] = {
		description = "Displays a piece of equipment alongside its type icon.",
		purpose = "Displays a piece of equipment alongside its type icon.",
		params = {
			[1] = {
				name = "game",
				required = true,
				enum = Franchise.enum(),
				type = "string",
				desc = "A game code.",
				trim = true,
				nilIfEmpty= true,
			},
			[2] = {
				name = "equipment",
				required = true,
				type = "string",
				desc = "The equipment's name.",
				tim = true,
				nilIfEmpty = true,
			},
		},
		examples = {
			{
				desc = "Blue Boko Reaper",
				args = {"TotK", "Blue Boko Reaper"},
			},
			{
				desc = "Master Sword",
				args = {"BotW", "Master Sword"}
			},
		},
	},
}

return p