Module:Sandbox/LVL: Difference between revisions

Jump to navigation Jump to search
LVL (talk | contribs)
No edit summary
LVL (talk | contribs)
No edit summary
Line 5: Line 5:
local util_module = require('Module:Utilities')
local util_module = require('Module:Utilities')


-- Helper function to flatten item data (stats, descriptions, etc.) into a searchable string
-- Helper function to flatten item data into searchable text using existing Lang JSON files
local function extract_search_terms(data)
local function extract_search_terms(data, lang_code)
    -- Load the language file directly for fast, frame-less lookups
    local lang_file_name = string.format("Data:Lang_%s.json", lang_code or "en")
    local success, lang_data = pcall(mw.loadJsonData, lang_file_name)
    if not success then lang_data = {} end
 
     local terms = {}
     local terms = {}
     local function recurse(tbl)
     local function recurse(tbl)
         if type(tbl) ~= "table" then return end
         if type(tbl) ~= "table" then return end
         for k, v in pairs(tbl) do
         for k, v in pairs(tbl) do
             table.insert(terms, tostring(k)) -- Add the key (e.g., "SlowPercent", "Damage")
             -- Only process string keys
            if type(k) == "string" then
                -- Check if the lang file has a readable string for this key (e.g., "TechResist" -> "TechResist_label")
                local term = lang_data[k .. "_label"] or lang_data[k]
                if term and type(term) == "string" then
                    -- Clean up any HTML tags or inline attributes from the localization string
                    term = term:gsub("<[^>]+>", " ")
                    term = term:gsub("{g:citadel_inline_attribute:'(.-)'}", "%1")
                    table.insert(terms, string.lower(term))
                else
                    -- Fallback: Convert CamelCase to spaces (e.g., BonusHealth -> bonus health)
                    local fallback_term = string.lower(k:gsub("(%u)", " %1"):gsub("^%s", ""))
                    table.insert(terms, fallback_term)
                end
            end
 
             if type(v) == "table" then
             if type(v) == "table" then
                 recurse(v)
                 recurse(v)
Line 20: Line 40:
                 str = str:gsub("{g:citadel_inline_attribute:'(.-)'}", "%1")
                 str = str:gsub("{g:citadel_inline_attribute:'(.-)'}", "%1")
                 str = mw.text.decode(str)
                 str = mw.text.decode(str)
                 table.insert(terms, str)
                 table.insert(terms, string.lower(str))
             else
             elseif type(v) == "number" or type(v) == "boolean" then
                table.insert(terms, tostring(v)) -- Add the value (e.g., "30", "Passive")
                -- Explicitly ignore numbers and true/false values
                -- We only want the stat names (keys) and descriptions (strings)
             end
             end
         end
         end
     end
     end
     recurse(data)
     recurse(data)
     local result = string.lower(table.concat(terms, " "))
     local result = table.concat(terms, " ")
     result = result:gsub("%s+", " ") -- Normalize spaces
     result = result:gsub("%s+", " ") -- Normalize spaces
     return result
     return result
Line 109: Line 130:
                      
                      
                     -- Append flattened item data (stats, descriptions, shop filters)
                     -- Append flattened item data (stats, descriptions, shop filters)
                     search_data = search_data .. " " .. extract_search_terms(item_data)
                    -- Pass lang_code so it can look up the proper in-game names from Lang JSON
                     search_data = search_data .. " " .. extract_search_terms(item_data, lang_code)
                      
                      
                     -- Escape double quotes so it doesn't break the HTML attribute
                     -- Escape double quotes so it doesn't break the HTML attribute