Модуль:Entity Sprite: различия между версиями

мНет описания правки
мНет описания правки
Строка 8: Строка 8:
     if type(t1) ~= "table" or type(t2) ~= "table" then return false end
     if type(t1) ~= "table" or type(t2) ~= "table" then return false end


    -- Если это массивы, проверяем их содержимое без учета порядка
     local function isArray(t)
     local function isArray(t)
         local i = 0
         local i = 0
Строка 35: Строка 34:
     end
     end


    -- Если это таблицы, проверяем их содержимое
     for k, v in pairs(t1) do
     for k, v in pairs(t1) do
         if t2[k] == nil or not deepEqual(v, t2[k]) then
         if t2[k] == nil or not deepEqual(v, t2[k]) then
Строка 101: Строка 99:


     return table.concat(result, "\n")
     return table.concat(result, "\n")
end
-- Создаём индекс для путей
local function createSpritePathIndex(data)
    local index = {}
    for _, entry in ipairs(data) do
        local spritePath = getSpritePath(entry)
        if spritePath then
            index[spritePath] = entry.id
        end
    end
    return index
end
end


-- Обновляем основную функцию шаблонизации
-- Обновляем основную функцию шаблонизации
local function generateTemplate(entry, param, secondaryParam, data, spritePathIndex)
local function generateTemplate(entry, param, secondaryParam)
     local spritePath = getSpritePath(entry)
     local spritePath = getSpritePath(entry)
     if not entry.id or not spritePath then
     if not entry.id or not spritePath then
Строка 130: Строка 116:
         else
         else
             return mw.getCurrentFrame():preprocess("{{Entity Sprite/Image|" .. entry.id .. "|" .. spritePath .. "}}")
             return mw.getCurrentFrame():preprocess("{{Entity Sprite/Image|" .. entry.id .. "|" .. spritePath .. "}}")
        end
    elseif param == "path" then
        if secondaryParam then
            -- Используем индекс для быстрого поиска
            local id = spritePathIndex[secondaryParam]
            if id then
                return id
            end
            return nil
        else
            return mw.getCurrentFrame():preprocess("{{Entity Sprite/Path|" .. entry.id .. "|" .. spritePath .. "}}")
         end
         end
     end
     end
Строка 158: Строка 133:
     local path = params.Path
     local path = params.Path


    -- Поиск записи с указанным ID
     local entry = nil
     local entry = nil
     if id and id ~= "" then
     if id and id ~= "" then
Строка 169: Строка 143:
     end
     end


    -- Если запись найдена, получаем путь спрайта
     if entry then
     if entry then
         spritePath = getSpritePath(entry)
         spritePath = getSpritePath(entry)
Строка 177: Строка 150:
     end
     end
      
      
    -- Если Path не указан, подставляем путь из JSON-данных
     if not path or path == "" then
     if not path or path == "" then
         path = "Resources/Textures/" .. (spritePath or "")
         path = "Resources/Textures/" .. (spritePath or "")
     end
     end


    -- Формирование шаблона
     return mw.getCurrentFrame():preprocess(
     return mw.getCurrentFrame():preprocess(
         "{{Файл\n" ..
         "{{Файл\n" ..
Строка 202: Строка 173:
         return 'Ошибка: Невозможно загрузить данные из JSON.'
         return 'Ошибка: Невозможно загрузить данные из JSON.'
     end
     end
    -- Создаём индекс путей
    local spritePathIndex = createSpritePathIndex(data)


     if param == "repeat" then
     if param == "repeat" then
         return generateRepeatTemplate(data)
         return generateRepeatTemplate(data)
    elseif param == "path" and secondaryParam then
     elseif param == "image" then
        for _, entry in ipairs(data) do
            local template = generateTemplate(entry, param, secondaryParam, data, spritePathIndex)
            if template then
                return template
            end
        end
        return nil
     elseif param == "image" or param == "path" then
         local result = {}
         local result = {}
         for _, entry in ipairs(data) do
         for _, entry in ipairs(data) do
             local template = generateTemplate(entry, param, secondaryParam, data, spritePathIndex)
             local template = generateTemplate(entry, param, secondaryParam)
             if template then
             if template then
                 table.insert(result, template)
                 table.insert(result, template)
Строка 226: Строка 186:
         return table.concat(result, "\n")
         return table.concat(result, "\n")
     else
     else
        -- Если не указан режим, генерируем шаблон по умолчанию
         return generateDefaultTemplate(data, frame.args)
         return generateDefaultTemplate(data, frame.args)
     end
     end