Модуль:Prototypes/Хранилище/Предмет: различия между версиями

мНет описания правки
мНет описания правки
Строка 3: Строка 3:
-- Кэш данных
-- Кэш данных
local dataCache = {}
local dataCache = {}
local indexCache = {}


-- Функция processRolls для преобразования диапазона
-- Функция processRolls для преобразования диапазона
Строка 24: Строка 25:
     return table.concat(result)
     return table.concat(result)
end
end
-- Локальные функции
local loadData, findDataById, formatContent, getContentsOutput, processNestedSelectors, getTableOutput, getContainedOutput, getChemOutput, handleGroupSelector, handleAllSelector, handleNestedSelector


-- Функция для загрузки данных с кэшированием
-- Функция для загрузки данных с кэшированием
local loadData = function(filePath)
loadData = function(filePath)
     if not dataCache[filePath] then
     if not dataCache[filePath] then
         local page = mw.title.new(filePath)
         local page = mw.title.new(filePath)
Строка 37: Строка 41:
-- Создание хэш-таблицы для быстрого поиска по 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 = {}
Строка 42: Строка 47:
         index[item.id] = item
         index[item.id] = item
     end
     end
    indexCache[data] = index
     return index
     return index
end
end


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


-- Форматирование содержимого
-- Форматирование содержимого
local formatContent = function(content)
ormatContent = function(content)
     if not content.id then
     if not content.id then
         return "Ошибка: отсутствует id у элемента."
         return "Ошибка: отсутствует id у элемента."
Строка 76: Строка 82:


-- Получение содержимого через таблицу
-- Получение содержимого через таблицу
local getContentsOutput = function(contents)
getContentsOutput = function(contents)
     local result = {}
     local result = {}
     for _, content in ipairs(contents) do
     for _, content in ipairs(contents) do
Строка 85: Строка 91:


-- Обработка вложенных таблиц
-- Обработка вложенных таблиц
local processNestedSelectors = function(children)
processNestedSelectors = function(children)
local handleNestedSelector, handleGroupSelector
    if not children or #children == 0 then return "" end
     local result = {}
 
    local results = {}
     local nestedCache = {}
 
     for _, child in ipairs(children) do
     for _, child in ipairs(children) do
         if child.id then
         if child.id then
             result[#result + 1] = formatContent(child)
             results[#results + 1] = formatContent(child)
         elseif child["!type"] == "NestedSelector" and child.tableId then
         elseif child["!type"] == "NestedSelector" then
            result[#result + 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
             result[#result + 1] = handleGroupSelector(child)
             results[#results + 1] = handleGroupSelector(child)
         end
         end
     end
     end
     return table.concat(result)
 
     return table.concat(results)
end
end


-- Обработка таблиц
-- Обработка таблиц
local getTableOutput = function(tableId)
getTableOutput = function(tableId)
local handleGroupSelector, processNestedSelectors
     local allSelectors = loadData('User:IanComradeBot/prototypes/AllSelector.json')
     local allSelectors = loadData('User:IanComradeBot/prototypes/AllSelector.json')
     local tableData = findDataById(buildIndex(allSelectors), tableId)
     local tableData = findDataById(buildIndex(allSelectors), tableId)
Строка 118: Строка 131:


-- Формирование списка содержащихся предметов или таблиц
-- Формирование списка содержащихся предметов или таблиц
local getContainedOutput = function(data, id)
getContainedOutput = function(data, id)
     local item = findDataById(data, id)
     local item = findDataById(data, id)
     if not item then return '' end
     if not item then return '' end
Строка 153: Строка 166:


-- Обработка AllSelector
-- Обработка AllSelector
local handleAllSelector = function(allSelector)
handleAllSelector = function(allSelector)
     if not allSelector.children then return '' end
     if not allSelector.children then return '' end
     return processNestedSelectors(allSelector.children)
     return processNestedSelectors(allSelector.children)
Строка 159: Строка 172:


-- Обработка GroupSelector
-- Обработка GroupSelector
local handleGroupSelector = function(groupSelector)
handleGroupSelector = function(groupSelector)
     if not groupSelector.children then return '' end
     if not groupSelector.children then return '' end
     local result = {}
     local result = {}
Строка 189: Строка 202:


-- Обработка NestedSelector
-- Обработка NestedSelector
local handleNestedSelector = function(nestedSelector, wrapped)
handleNestedSelector = function(nestedSelector, wrapped)
     if not nestedSelector.tableId then return '' end
     if not nestedSelector.tableId then return '' end


Строка 218: Строка 231:


-- Формирование списка химии
-- Формирование списка химии
local getChemOutput = function(data, id)
getChemOutput = function(data, id)
     local item = findDataById(data, id)
     local item = findDataById(data, id)
     if not item or not item.SolutionContainerManager or not item.SolutionContainerManager.solutions then return '' end
     if not item or not item.SolutionContainerManager or not item.SolutionContainerManager.solutions then return '' end
Строка 265: Строка 278:
         local entity = findDataById(dataIndex, id)
         local entity = findDataById(dataIndex, id)
         if not entity then return 'ID не найден в данных.' end
         if not entity then return 'ID не найден в данных.' end
 
         if entity.EntityTableContainerFill then
         if entity.EntityTableContainerFill then
             local containers = entity.EntityTableContainerFill.containers
             local containers = entity.EntityTableContainerFill.containers
Строка 274: Строка 287:


         return 'Режим rolls не найден для этого ID.'
         return 'Режим rolls не найден для этого ID.'
    else
    else
         return 'Неизвестный режим: ' .. mode
         return 'Неизвестный режим: ' .. mode
     end
     end