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

мНет описания правки
мНет описания правки
Строка 5: Строка 5:
local p = {}
local p = {}


-- Функция для загрузки данных  
-- Кэш данных
local loadData = function(filePath)
local dataCache = {}
    local page = mw.title.new(filePath)
local indexCache = {}
    local content = page:getContent()
    return content and mw.text.jsonDecode(content) or nil
end


-- Функция processRolls для преобразования диапазона
-- Функция processRolls для преобразования диапазона
Строка 34: Строка 31:


-- Локальные функции
-- Локальные функции
local findDataById, formatContent, getContentsOutput, processNestedSelectors, getTableOutput, getContainedOutput, getChemOutput, handleGroupSelector, handleAllSelector, handleNestedSelector
local loadData, findDataById, formatContent, getContentsOutput, processNestedSelectors, getTableOutput, getContainedOutput, getChemOutput, handleGroupSelector, handleAllSelector, handleNestedSelector
 
-- Функция для загрузки данных с кэшированием
loadData = function(filePath)
    if not dataCache[filePath] then
        local page = mw.title.new(filePath)
        local content = page:getContent()
        dataCache[filePath] = content and mw.text.jsonDecode(content) or nil
    end
    return dataCache[filePath]
end


-- Функция для создания хэш-таблицы для быстрого поиска по ID
-- Создание хэш-таблицы для быстрого поиска по ID
local function buildIndex(data)
local function buildIndex(data)
    if indexCache[data] then return indexCache[data] end
     if not data then return {} end
     if not data then return {} end
     local index = {}
     local index = {}
Строка 43: Строка 51:
         index[item.id] = item
         index[item.id] = item
     end
     end
    indexCache[data] = index
     return index
     return index
end
end


-- Поиск данных по ID через индекс
-- Поиск данных по ID через хэш-таблицу
findDataById = function(dataIndex, id)
findDataById = function(dataIndex, id)
     return dataIndex and dataIndex[id] or nil
     return dataIndex and dataIndex[id] or nil
Строка 71: Строка 80:


     return string.format(
     return string.format(
         '{{LinkСard|SideStyle=1|background-color=#cbcbff0a|image=%s|name=%s%s%s {{#invoke:Prototypes/Хранилище/Предмет|main|framing|contained|%s}} }}',
         '{{LinkСard|SideStyle=1|background-color=#cbcbff0b|image=%s|name=%s%s%s {{#invoke:Prototypes/Хранилище/Предмет|main|framing|contained|%s}} }}',
         image, name, amount, prob, content.id
         image, name, amount, prob, content.id
     )
     )
Строка 90: Строка 99:


     local results = {}
     local results = {}
    local nestedCache = {}


     for _, child in ipairs(children) do
     for _, child in ipairs(children) do
        -- Если у элемента есть id, то выводим его
         if child.id then
         if child.id then
             results[#results + 1] = formatContent(child)
             results[#results + 1] = formatContent(child)
         elseif child["!type"] == "NestedSelector" then
         elseif child["!type"] == "NestedSelector" then
             results[#results + 1] = handleNestedSelector(child, true)
            -- Кэшируем результат вызова handleNestedSelector
            if not nestedCache[child.tableId] then
                nestedCache[child.tableId] = handleNestedSelector(child, true)
            end
             results[#results + 1] = nestedCache[child.tableId]
         elseif child["!type"] == "GroupSelector" then
         elseif child["!type"] == "GroupSelector" then
             results[#results + 1] = handleGroupSelector(child)
             results[#results + 1] = handleGroupSelector(child)
        else
            -- Если это не поддерживаемый тип, выводим ошибку
            results[#results + 1] = "<div>Ошибка: не поддерживаемый тип элемента.</div>"
         end
         end
     end
     end
Строка 127: Строка 137:
getContainedOutput = function(data, id)
getContainedOutput = function(data, id)
     local item = findDataById(data, id)
     local item = findDataById(data, id)
     if not item then return 'Ошибка: элемент не найден по ID.' end
     if not item then return '' end


     local result = {}
     local result = {}
Строка 142: Строка 152:
         if containers.entity_storage then
         if containers.entity_storage then
             if containers.entity_storage.children then
             if containers.entity_storage.children then
                -- Добавим обработку обычных элементов с id
                 result[#result + 1] = processNestedSelectors(containers.entity_storage.children)
                 result[#result + 1] = processNestedSelectors(containers.entity_storage.children)
            else
                result[#result + 1] = '<div>Ошибка: нет children в entity_storage.</div>'
             end
             end


Строка 151: Строка 158:
                 result[#result + 1] = getTableOutput(containers.entity_storage.tableId)
                 result[#result + 1] = getTableOutput(containers.entity_storage.tableId)
             end
             end
        else
            result[#result + 1] = '<div>Ошибка: нет entity_storage в EntityTableContainerFill.</div>'
         end
         end


Строка 159: Строка 164:
             result[#result + 1] = getTableOutput(containers.storagebase.tableId)
             result[#result + 1] = getTableOutput(containers.storagebase.tableId)
         end
         end
    else
        result[#result + 1] = '<div>Ошибка: нет контейнеров в EntityTableContainerFill.</div>'
     end
     end


Строка 174: Строка 177:
-- Обработка GroupSelector
-- Обработка GroupSelector
handleGroupSelector = function(groupSelector)
handleGroupSelector = function(groupSelector)
     if not groupSelector.children then
     if not groupSelector.children then return '' end
        return '<div>Ошибка: нет children в GroupSelector.</div>'
    end
     local result = {}
     local result = {}
     local wrapperStart, wrapperEnd = "", ""
     local wrapperStart, wrapperEnd = "", ""
Строка 261: Строка 262:


     if not id then return 'Не указан ID.' end
     if not id then return 'Не указан ID.' end
      
 
local dataIndex = buildIndex(data)
     local dataIndex = buildIndex(data)
 
     if not data then return 'Не удалось загрузить данные.' end
     if not data then return 'Не удалось загрузить данные.' end