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

мНет описания правки
мНет описания правки
Метка: ручная отмена
Строка 2: Строка 2:


-- Загрузка данных
-- Загрузка данных
local jsonData = nil
local function loadData(filePath)
local function loadData(filePath)
     if not jsonData then
     local page = mw.title.new(filePath)
        local page = mw.title.new(filePath)
    local content = page and page:getContent()
        local content = page and page:getContent()
    return content and mw.text.jsonDecode(content) or nil
        jsonData = content and mw.text.jsonDecode(content) or nil
    end
    return jsonData
end
end


Строка 75: Строка 70:
     end
     end
     return nil
     return nil
end
local function cleanTable(tbl)
    for k in pairs(tbl) do
        tbl[k] = nil
    end
end
end


Строка 112: Строка 101:
             end
             end
             local firstId = group[1].id
             local firstId = group[1].id
             table.insert(result, "{{Entity Sprite/Image|" .. entry.id .. "|" .. spritePath .. "}}")
             table.insert(result, mw.getCurrentFrame():preprocess("{{Entity Sprite/Repeat|" .. table.concat(idLinks, " ") .. "|" .. firstId .. "}}"))
         end
         end
     end
     end
   
    cleanTable(spritePathIndex)
    cleanTable(jsonData)


     return mw.getCurrentFrame():preprocess(table.concat(result, "\n"))
     return table.concat(result, "\n")
end
end


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


Строка 240: Строка 222:
         end
         end
         return nil  
         return nil  
    elseif param == "image" and secondaryParam then
        for _, entry in ipairs(data) do
            if entry.id == secondaryParam then
                return getSpritePath(entry) or "Ошибка: Спрайт не найден."
            end
        end
        return "Ошибка: ID не найден."
     elseif param == "image" or param == "path" then
     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, data, spritePathIndex)
             local template = generateTemplate(entry, param, secondaryParam, data, spritePathIndex)
             if template then
             if template then
                 table.insert(result, template)
                 table.insert(result, template)