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

мНет описания правки
Нет описания правки
Строка 1: Строка 1:
-- Загрузка данных
local data = mw.loadData("Модуль:IanComradeBot/prototypes/fills/Item.json/data")
local allSelectorData = mw.loadData("Модуль:IanComradeBot/prototypes/table.json/data")
local p = {}
local p = {}


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


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


-- Локальные функции
-- Локальные функции
local loadData, findDataById, formatContent, getContentsOutput, processNestedSelectors, getTableOutput, getContainedOutput, getChemOutput, handleGroupSelector, handleAllSelector, handleNestedSelector
local 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 = {}
Строка 47: Строка 43:
         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
Строка 95: Строка 90:


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


     for _, child in ipairs(children) do
     for _, child in ipairs(children) do
Строка 101: Строка 95:
             results[#results + 1] = formatContent(child)
             results[#results + 1] = formatContent(child)
         elseif child["!type"] == "NestedSelector" then
         elseif child["!type"] == "NestedSelector" then
             -- Кэшируем результат вызова handleNestedSelector
             results[#results + 1] = handleNestedSelector(child, true)
            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)
Строка 116: Строка 106:
-- Обработка таблиц
-- Обработка таблиц
getTableOutput = function(tableId)
getTableOutput = function(tableId)
     local allSelectors = loadData('User:IanComradeBot/prototypes/AllSelector.json')
     local allSelectors = loadData('User:IanComradeBot/prototypes/table.json')
     local tableData = findDataById(buildIndex(allSelectors), tableId)
     local tableData = findDataById(buildIndex(allSelectors), tableId)


Строка 258: Строка 248:


     if not id then return 'Не указан ID.' end
     if not id then return 'Не указан ID.' end
 
      
     local data = loadData('User:IanComradeBot/prototypes/fills/Item.json')
local dataIndex = buildIndex(data)
    local dataIndex = buildIndex(data)
 
     if not data then return 'Не удалось загрузить данные.' end
     if not data then return 'Не удалось загрузить данные.' end