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

Нет описания правки
Нет описания правки
Строка 1: Строка 1:
-- Загрузка данных
-- local itemData = mw.loadData("Модуль:IanComradeBot/prototypes/fills/Item.json/data")
-- local tableData = 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


-- Функция для загрузки данных с кэшированием
-- Функция для создания хэш-таблицы для быстрого поиска по ID
loadData = function(filePath)
local function buildIndex(itemData)
    if not dataCache[filePath] then
     if not itemData then return {} end
        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
local function buildIndex(data)
    if indexCache[data] then return indexCache[data] end
     if not data then return {} end
     local index = {}
     local index = {}
     for _, item in ipairs(data) do
     for _, item in ipairs(itemData) do
         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(itemDataIndex, id)
     return dataIndex and dataIndex[id] or nil
     return itemDataIndex and itemDataIndex[id] or nil
end
end


Строка 76: Строка 71:


     return string.format(
     return string.format(
         '{{LinkСard|SideStyle=1|background-color=#cbcbff0b|image=%s|name=%s%s%s {{#invoke:Prototypes/Хранилище/Предмет|main|framing|contained|%s}} }}',
         '{{LinkСard|SideStyle=1|background-color=#cbcbff0a|image=%s|name=%s%s%s {{#invoke:Prototypes/Хранилище/Предмет|main|framing|contained|%s}} }}',
         image, name, amount, prob, content.id
         image, name, amount, prob, content.id
     )
     )
Строка 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 tableData = loadData('User:IanComradeBot/prototypes/table.json')
     local tableData = findDataById(buildIndex(allSelectors), tableId)
     local tableDataIndex = findDataById(buildIndex(tableData), tableId)


     if not tableData then return 'Таблица не найдена.' end
     if not tableDataIndex then return 'Таблица не найдена.' end


     if tableData['!type:GroupSelector'] then
     if tableDataIndex['!type:GroupSelector'] then
         return handleGroupSelector(tableData['!type:GroupSelector'])
         return handleGroupSelector(tableDataIndex['!type:GroupSelector'])
     elseif tableData['!type:AllSelector'] then
     elseif tableDataIndex['!type:AllSelector'] then
         return processNestedSelectors(tableData['!type:AllSelector'].children)
         return processNestedSelectors(tableDataIndex['!type:AllSelector'].children)
     end
     end


Строка 246: Строка 236:


-- Формирование списка химии
-- Формирование списка химии
getChemOutput = function(data, id)
getChemOutput = function(itemData, id)
     local item = findDataById(data, id)
     local item = findDataById(itemData, 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: Строка 255:


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


     if mode == 'framing' then
     if mode == 'framing' then
Строка 280: Строка 270:


         if subMode == 'chem' then
         if subMode == 'chem' then
             return frame:preprocess('{{СollapsibleMenu|' .. getChemOutput(dataIndex, id) .. '}}')
             return frame:preprocess('{{СollapsibleMenu|' .. getChemOutput(itemDataIndex, id) .. '}}')
         elseif subMode == 'contained' then
         elseif subMode == 'contained' then
             return frame:preprocess('{{СollapsibleMenu|' .. getContainedOutput(dataIndex, id) .. '}}')
             return frame:preprocess('{{СollapsibleMenu|' .. getContainedOutput(itemDataIndex, id) .. '}}')
         else
         else
             return 'Неизвестный подрежим для framing: ' .. subMode
             return 'Неизвестный подрежим для framing: ' .. subMode
         end
         end
     elseif mode == 'chem' then
     elseif mode == 'chem' then
         return frame:preprocess(getChemOutput(dataIndex, id))
         return frame:preprocess(getChemOutput(itemDataIndex, id))
     elseif mode == 'contained' then
     elseif mode == 'contained' then
         return frame:preprocess(getContainedOutput(dataIndex, id))
         return frame:preprocess(getContainedOutput(itemDataIndex, id))
     elseif mode == 'rolls' then
     elseif mode == 'rolls' then
         local entity = findDataById(dataIndex, id)
         local entity = findDataById(itemDataIndex, id)
         if not entity then return 'ID не найден в данных.' end
         if not entity then return 'ID не найден в данных.' end