Module:Sandbox: Difference between revisions

From The Deadlock Wiki
Jump to navigation Jump to search
test
test
Line 1: Line 1:
local p = {}
local function process_template(text, itemName)
local items_data = mw.loadJsonData("Data:ItemData.json")
    -- This pattern matches {s:SomePropertyName} in the text
    return text:gsub("{s:([^}]+)}", function(property)
        -- Construct the template call using the itemName parameter
        local template = "{{#invoke:ItemData|get_prop|" .. itemName .. "|" .. property .. "}}"
        return template
    end)
end


-- {{#invoke:Sandbox|get_item_key|item name}}
function p.process_item_string(frame)
function p.get_item_key(frame)
     local args = frame.args
     local item_name_input = frame.args[1] -- Get the item name from the template argument
    local parentArgs = frame:getParent().args
     if not item_name_input then
   
         return "Error: Item name not specified."
    -- Get parameters - either from direct invocation or from parent frame
    local stringKey = args[1] or parentArgs[1]
    local itemName = args.item or parentArgs.item
   
     if not stringKey or not itemName then
         return "Error: Missing required parameters. Usage: {{#invoke:ItemStringProcessor|process_item_string|string_key|item=item_name}}"
     end
     end
 
      
     item_name_input = item_name_input:lower() -- For case-independent search
     -- First get the base string from the Lang module
 
     local langModule = require("Module:Lang")
     -- Go through all the items and look for a name match.
    local baseString = langModule.get_string(frame, stringKey)
     for item_key, item_data in pairs(items_data) do
   
        if item_data and item_data["Name"] then
    if not baseString then
            local current_item_name = item_data["Name"]:lower()
        return "Error: String key '" .. stringKey .. "' not found"
            if current_item_name == item_name_input then
                return item_key -- Return the key if the name matches
            end
        end
     end
     end
 
   
     return "Item not found." -- If nothing is found
     -- Process the string to replace property placeholders
    local processedString = process_template(baseString, itemName)
   
    return processedString
end
end
return p

Revision as of 20:43, 11 April 2025

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

local function process_template(text, itemName)
    -- This pattern matches {s:SomePropertyName} in the text
    return text:gsub("{s:([^}]+)}", function(property)
        -- Construct the template call using the itemName parameter
        local template = "{{#invoke:ItemData|get_prop|" .. itemName .. "|" .. property .. "}}"
        return template
    end)
end

function p.process_item_string(frame)
    local args = frame.args
    local parentArgs = frame:getParent().args
    
    -- Get parameters - either from direct invocation or from parent frame
    local stringKey = args[1] or parentArgs[1]
    local itemName = args.item or parentArgs.item
    
    if not stringKey or not itemName then
        return "Error: Missing required parameters. Usage: {{#invoke:ItemStringProcessor|process_item_string|string_key|item=item_name}}"
    end
    
    -- First get the base string from the Lang module
    local langModule = require("Module:Lang")
    local baseString = langModule.get_string(frame, stringKey)
    
    if not baseString then
        return "Error: String key '" .. stringKey .. "' not found"
    end
    
    -- Process the string to replace property placeholders
    local processedString = process_template(baseString, itemName)
    
    return processedString
end