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

Нет описания правки
мНет описания правки
Строка 1: Строка 1:
local p = {}
local p = {}
-- Кеш для загруженных данных и для индекса спрайтов
local cachedData = nil
local cachedSpritePathIndex = nil


-- Загрузка данных
-- Загрузка данных
local data = mw.loadData("Module:Entity Sprite/data")
local function loadData(filePath)
if not data or type(data) ~= "table" then
     return mw.loadData(filePath)
     return "Ошибка: Невозможно загрузить данные из JSON."
end
end


Строка 113: Строка 108:
-- Создаём индекс для путей
-- Создаём индекс для путей
local function createSpritePathIndex(data)
local function createSpritePathIndex(data)
    if cachedSpritePathIndex then
        return cachedSpritePathIndex
    end
     local index = {}
     local index = {}
     for _, entry in ipairs(data) do
     for _, entry in ipairs(data) do
Строка 123: Строка 115:
         end
         end
     end
     end
     cachedSpritePathIndex = index
     return index
    return cachedSpritePathIndex
end
end


Строка 208: Строка 199:
     local param = frame.args[1]
     local param = frame.args[1]
     local secondaryParam = frame.args[2]
     local secondaryParam = frame.args[2]
    local data = loadData('User:IanComradeBot/prototypes/entity sprite.json')
    if not data or type(data) ~= 'table' then
        return 'Ошибка: Невозможно загрузить данные из JSON.'
    end


     -- Индекс путей
     -- Индекс путей
Строка 224: Строка 220:
     elseif param == "image" and secondaryParam then
     elseif param == "image" and secondaryParam then
         for _, entry in ipairs(data) do
         for _, entry in ipairs(data) do
             if entry.id == secondaryParam then
             if tostring(entry.id) == tostring(secondaryParam) then
                 return getSpritePath(entry) or "Ошибка: Спрайт не найден."
                 return getSpritePath(entry) or "Ошибка: Спрайт не найден."
             end
             end