Module:Sandbox: Difference between revisions

From The Deadlock Wiki
Jump to navigation Jump to search
test
test
Line 1: Line 1:
local function process_template(text, itemName)
local p = {}
     -- This pattern matches {s:SomePropertyName} in the text
 
local function get_item_property(itemName, property)
    local itemData = require("Module:ItemData")
    local success, value = pcall(itemData.get_prop, itemData, itemName, property)
    return success and value or nil
end
 
local function get_lang_string(stringKey)
     local lang = require("Module:Lang")
    local success, value = pcall(lang.get_string, lang, stringKey)
    return success and value or nil
end
 
local function process_string(text, itemName)
     return text:gsub("{s:([^}]+)}", function(property)
     return text:gsub("{s:([^}]+)}", function(property)
         -- Construct the template call using the itemName parameter
         local propValue = get_item_property(itemName, property)
         local template = "{{#invoke:ItemData|get_prop|" .. itemName .. "|" .. property .. "}}"
         return propValue or ("{" .. property .. "}")  -- Return original if property not found
        return template
     end)
     end)
end
end
Line 12: Line 24:
     local parentArgs = frame:getParent().args
     local parentArgs = frame:getParent().args
      
      
     -- Get parameters - either from direct invocation or from parent frame
     -- Get parameters
     local stringKey = args[1] or parentArgs[1]
     local stringKey = args[1] or parentArgs[1]
     local itemName = args.item or parentArgs.item
     local itemName = args.item or parentArgs.item
      
      
     if not stringKey or not itemName then
     if not stringKey or not itemName then
         return "Error: Missing required parameters. Usage: {{#invoke:ItemStringProcessor|process_item_string|string_key|item=item_name}}"
         return "Error: Missing parameters. Usage: {{#invoke:ItemStringProcessor|process_item_string|string_key|item=item_name}}"
     end
     end
      
      
     -- First get the base string from the Lang module
     -- Get base string from Lang module
    local langModule = require("Module:Lang")
     local baseString = get_lang_string(stringKey)
     local baseString = langModule.get_string(frame, stringKey)
   
     if not baseString then
     if not baseString then
         return "Error: String key '" .. stringKey .. "' not found"
         return "Error: String key '" .. stringKey .. "' not found in Lang module"
     end
     end
      
      
     -- Process the string to replace property placeholders
     -- Process the string with item properties
     local processedString = process_template(baseString, itemName)
     local processedString = process_string(baseString, itemName)
      
      
     return processedString
     return processedString
end
end
return p

Revision as of 20:45, 11 April 2025

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

local p = {}

local function get_item_property(itemName, property)
    local itemData = require("Module:ItemData")
    local success, value = pcall(itemData.get_prop, itemData, itemName, property)
    return success and value or nil
end

local function get_lang_string(stringKey)
    local lang = require("Module:Lang")
    local success, value = pcall(lang.get_string, lang, stringKey)
    return success and value or nil
end

local function process_string(text, itemName)
    return text:gsub("{s:([^}]+)}", function(property)
        local propValue = get_item_property(itemName, property)
        return propValue or ("{" .. property .. "}")  -- Return original if property not found
    end)
end

function p.process_item_string(frame)
    local args = frame.args
    local parentArgs = frame:getParent().args
    
    -- Get parameters
    local stringKey = args[1] or parentArgs[1]
    local itemName = args.item or parentArgs.item
    
    if not stringKey or not itemName then
        return "Error: Missing parameters. Usage: {{#invoke:ItemStringProcessor|process_item_string|string_key|item=item_name}}"
    end
    
    -- Get base string from Lang module
    local baseString = get_lang_string(stringKey)
    if not baseString then
        return "Error: String key '" .. stringKey .. "' not found in Lang module"
    end
    
    -- Process the string with item properties
    local processedString = process_string(baseString, itemName)
    
    return processedString
end

return p