Модуль:Песочница/Pok: различия между версиями

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


local cachedData = nil
local cachedData = nil
local loadCount = 0


local function loadData(filePath)
local function loadData(filePath)
Строка 7: Строка 8:
         return cachedData
         return cachedData
     end
     end
    loadCount = loadCount + 1
     local page = mw.title.new(filePath)
     local page = mw.title.new(filePath)
     local content = page and page:getContent()
     local content = page and page:getContent()
     cachedData = content and mw.text.jsonDecode(content) or nil
     cachedData = content and mw.text.jsonDecode(content) or nil
     return cachedData
     return cachedData
end
local function getSpritePath(entry)
    return entry.Sprite and entry.Sprite.sprite or (entry.Icon and entry.Icon.sprite)
end
local indexById = nil
local function buildIndex(data)
    local index = {}
    for _, entry in ipairs(data) do
        if entry.id then
            index[entry.id] = entry
        end
    end
    return index
end
end


Строка 33: Строка 19:
     local secondaryParam = frame.args[2]
     local secondaryParam = frame.args[2]


     if not cachedData then
     local data = loadData('User:IanComradeBot/prototypes/entity sprite.json')
        cachedData = loadData('User:IanComradeBot/prototypes/entity sprite.json')
    if not data then
        if cachedData then
         return 'Ошибка: Невозможно загрузить данные из JSON.'
            indexById = buildIndex(cachedData)
         end
     end
     end


     if not cachedData then
     if param == "debug" then
         return 'Ошибка: Невозможно загрузить данные из JSON.'
         return "Количество загрузок JSON: " .. loadCount
     end
     end


     if param == "image" and secondaryParam then
     if param == "image" and secondaryParam then
         local entry = indexById and indexById[secondaryParam]
         for _, entry in ipairs(data) do
        return entry and getSpritePath(entry) or "Ошибка: ID не найден."
            if entry.id == secondaryParam then
                return entry.Sprite and entry.Sprite.sprite or (entry.Icon and entry.Icon.sprite) or "Ошибка: Спрайт не найден."
            end
        end
        return "Ошибка: ID не найден."
     end
     end
end
end


return p
return p