Module:SmithingUsage

From Tensura: Reincarnated Wiki
Revision as of 10:55, 25 September 2026 by imported>Merge (merge.py)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Documentation for this module may be created at Module:SmithingUsage/doc

local cargo = require("mw.ext.cargo")
local ui = require("Module:UI") -- Your module containing the `smithing` function

local p = {} -- Define the module's namespace

local function escapeString(str)
    return mw.text.jsonEncode(str):sub(2, -2) -- Escapes special characters
end

function p.smithingUsage(frame)
    local args = frame:getParent().args
    local itemName = args["item"] or mw.title.getCurrentTitle().text

    -- Ensure itemName is a string and escape special characters
    if type(itemName) ~= "string" then
        return "Error: Invalid item name provided."
    end

    itemName = escapeString(itemName)

    -- Extract base item name (remove quantity if present)
    local baseItemName = itemName:match("([^,]+)")

    -- Construct the where clause as a string
    local whereClause = string.format(
        "Input1 LIKE '%%%s%%' OR Input2='%s' OR Input3='%s' OR Input4='%s' OR Input5='%s' OR OutputName='%s'",
        baseItemName, itemName, itemName, itemName, itemName, itemName
    )

    -- Debugging: Log the whereClause
    mw.logObject(whereClause)
    mw.logObject(type(whereClause))

    -- Query Cargo for smithing recipes involving the item
    local results = cargo.query({
        tables = "SmithingBenchRecipes",
        fields = "Input1, Input2, Input3, Input4, Input5, OutputName",
        where = whereClause,
        limit = 500 -- Reasonable limit for results
    })

    -- No results found
    if #results == 0 then
        return "No smithing recipes found for this item."
    end

    -- Render each smithing recipe
    local out = {}
    for _, recipe in ipairs(results) do
        -- Prepare arguments for the smithing function
        local recipeArgs = {
            ["Input1"] = recipe.Input1,
            ["Input2"] = recipe.Input2,
            ["Input3"] = recipe.Input3,
            ["Input4"] = recipe.Input4,
            ["Input5"] = recipe.Input5,
            ["Output"] = recipe.OutputName
        }

        -- Call the smithing function and append its output
        table.insert(out, ui.smithing(recipeArgs))
    end

    -- Return the combined HTML
    return table.concat(out, "\n")
end

return p -- Return the module's namespace