Module:SmithingBench

From Tensura 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:SmithingBench/doc

local p = {}

local i18n = {
    moduleArgs = [[Module:ProcessArgs]],
    moduleRecipe = [[Module:Recipe table]],
    moduleSlot = [[Module:Inventory slot]],
    moduleUi = [[Module:UI]],
    type = 'Smithing Bench'
}

p.i18n = i18n

local slot = require(i18n.moduleSlot)
local recipeTable = require(i18n.moduleRecipe).table
local aliases = mw.loadData(i18n.moduleSlot .. '/Aliases')
local cArgVals = { 'Input1', 'Input2', 'Input3', 'Input4', 'Input5', 'Output' }
p.cArgVals = cArgVals

function p.table(f)
    local args = f
    if f == mw.getCurrentFrame() then
        args = require(i18n.moduleArgs).merge(true)
    else
        f = mw.getCurrentFrame()
    end

    -- Assign input and output slots
    for i = 1, 5 do
        args['Input' .. i] = args[i] or nil
    end
    args.Output = args[6]
    for i = 1, 6 do
        args[i] = nil
    end

    -- Create recipe table and list of ingredients
    local out, ingredientSets, outputSets = recipeTable(args, {
        uiFunc = 'smithingBenchUI',
        type = i18n.type,
        ingredientArgs = cArgVals,
        outputArgs = { 'Output' },
    })

    local title = mw.title.getCurrentTitle()
    if args.ignoreusage or args.nocat == '1' or title.namespace ~= 0 or title.isSubpage then
        return out
    end

    -- SMW Integration
    local smw_json = {
        ["type"] = args.type,
        ["description"] = mw.text.unstrip(args.description or ''), -- Remove ref tags
        ["name"] = args.name,
        ["ingredients"] = args.ingredients,
        ["Input1"] = args.Input1,
        ["Input2"] = args.Input2,
        ["Input3"] = args.Input3,
        ["Input4"] = args.Input4,
        ["Input5"] = args.Input5,
        ["Output"] = args.Output,
        ["group"] = args.group
    }

    -- Flatten item sets
    local function makeNameList(itemSets)
        local retTable = {}
        local usedNames = {}
        for _, itemSet in pairs(itemSets) do
            for _, item in pairs(itemSet) do
                if not usedNames[item.name] then
                    table.insert(retTable, item.name)
                    usedNames[item.name] = true

                    if aliases[item.name] then
                        for _, aliasEntry in ipairs(aliases[item.name]) do
                            if not usedNames[aliasEntry] then
                                table.insert(retTable, (aliasEntry.name or aliasEntry))
                                usedNames[aliasEntry] = true
                            end
                        end
                    end
                end
            end
        end
        return retTable
    end

    local output_names = makeNameList(outputSets)
    local ingredient_names = makeNameList(ingredientSets)

    local json = mw.text.jsonEncode(smw_json)
    local subname = 'SMITHING_BENCH_' .. table.concat(output_names, '_') .. '_' .. mw.hash.hashValue("md4", json)
    local smw_sub = {
        ['Smithing ingredient'] = ingredient_names,
        ['Smithing output'] = output_names,
        ['Smithing JSON'] = json,
        ['Smithing type'] = args.type
    }
    mw.smw.subobject(smw_sub, subname)

    return out
end

return p