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

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


local p = {}
local p = {}
Строка 37: Строка 37:


-- Функция для создания хэш-таблицы для быстрого поиска по ID
-- Функция для создания хэш-таблицы для быстрого поиска по ID
local function buildIndex(data)
local function buildIndex(itemData)
     if not data then return {} end
     if not itemData 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
Строка 47: Строка 47:


-- Поиск данных по 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


Строка 86: Строка 86:


-- Обработка вложенных таблиц
-- Обработка вложенных таблиц
processNestedSelectors = function(children)
processNestedSelectors = function(children, visited)
visited = visited or {}
     if not children or #children == 0 then return "" end
     if not children or #children == 0 then return "" end


Строка 93: Строка 94:
     for _, child in ipairs(children) do
     for _, child in ipairs(children) do
         if child.id then
         if child.id then
            results[#results + 1] = formatContent(child)
if not visited[child.id] then
    results[#results + 1] = formatContent(child)
end
         elseif child["!type"] == "NestedSelector" then
         elseif child["!type"] == "NestedSelector" then
             results[#results + 1] = handleNestedSelector(child, true)
             results[#results + 1] = handleNestedSelector(child, true, visited)
         elseif child["!type"] == "GroupSelector" then
         elseif child["!type"] == "GroupSelector" then
             results[#results + 1] = handleGroupSelector(child)
             results[#results + 1] = handleGroupSelector(child, visited)
         end
         end
     end
     end
Строка 105: Строка 108:


-- Обработка таблиц
-- Обработка таблиц
getTableOutput = function(tableId)
getTableOutput = function(tableId, visited)
     local allSelectors = loadData('User:IanComradeBot/prototypes/table.json')
     visited = visited or {}
    local tableData = findDataById(buildIndex(allSelectors), tableId)


     if not tableData then return 'Таблица не найдена.' end
    if visited[tableId] then
        return ''
    end
 
    visited[tableId] = true
 
    local tableData = loadData('User:IanComradeBot/prototypes/table.json')
    local tableDataIndex = findDataById(buildIndex(tableData), tableId)
 
     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'], visited)
     elseif tableData['!type:AllSelector'] then
     elseif tableDataIndex['!type:AllSelector'] then
         return processNestedSelectors(tableData['!type:AllSelector'].children)
         return processNestedSelectors(tableDataIndex['!type:AllSelector'].children, visited)
     end
     end


Строка 121: Строка 132:


-- Формирование списка содержащихся предметов или таблиц
-- Формирование списка содержащихся предметов или таблиц
getContainedOutput = function(data, id)
getContainedOutput = function(itemData, id, visited)
     local item = findDataById(data, id)
    visited = visited or {}
 
    if visited[id] then
        return ''
    end
 
    visited[id] = true
 
     local item = findDataById(itemData, id)
     if not item then return '' end
     if not item then return '' end


Строка 138: Строка 157:
         if containers.entity_storage then
         if containers.entity_storage then
             if containers.entity_storage.children then
             if containers.entity_storage.children then
                 result[#result + 1] = processNestedSelectors(containers.entity_storage.children)
                 result[#result + 1] = processNestedSelectors(containers.entity_storage.children, visited)
             end
             end
             if containers.entity_storage.tableId then
             if containers.entity_storage.tableId then
                 result[#result + 1] = getTableOutput(containers.entity_storage.tableId)
                 result[#result + 1] = getTableOutput(containers.entity_storage.tableId, visited)
             end
             end
         end
         end
Строка 148: Строка 166:
         -- Обработка storagebase
         -- Обработка storagebase
         if containers.storagebase and containers.storagebase.tableId then
         if containers.storagebase and containers.storagebase.tableId then
             result[#result + 1] = getTableOutput(containers.storagebase.tableId)
             result[#result + 1] = getTableOutput(containers.storagebase.tableId, visited)
         end
         end
     end
     end
Строка 192: Строка 210:


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


     local result = {}
     local result = {}
     local classesRolls, classesProb
     local classesRolls, classesProb


     if wrapped then
     if wrapped then
if nestedSelector.rolls and nestedSelector.rolls.range then
        if nestedSelector.rolls and nestedSelector.rolls.range then
    local rollsResult = processRolls(nestedSelector.rolls)
            local rollsResult = processRolls(nestedSelector.rolls)
    if rollsResult and #rollsResult > 0 then
            if rollsResult and #rollsResult > 0 then
        classesRolls = ', максимум может выпасть: ' .. rollsResult
                classesRolls = ', максимум может выпасть: ' .. rollsResult
    end
            end
end
        end
         if nestedSelector.prob then
         if nestedSelector.prob then
             classesProb = string.format(" <div>%s%%</div>", nestedSelector.prob * 100 >= 1 and math.floor(nestedSelector.prob * 100) or nestedSelector.prob * 100)
             classesProb = string.format(" <div>%s%%</div>", nestedSelector.prob * 100 >= 1 and math.floor(nestedSelector.prob * 100) or nestedSelector.prob * 100)
Строка 211: Строка 229:
     end
     end


if wrapped and (classesRolls or classesProb) then
    if wrapped and (classesRolls or classesProb) then
    if (classesRolls and #classesRolls > 0) or (classesProb and #classesProb > 0) then
        result[#result + 1] = string.format('{{LinkСard/Сollapsible|name=Группа предметов%s%s|content=', classesRolls or "", classesProb or "")
        result[#result + 1] = string.format('{{LinkСard/Сollapsible|name=Группа предметов%s%s|content=', classesRolls or "", classesProb or "")
    end
    end
end


     result[#result + 1] = getTableOutput(nestedSelector.tableId)
     result[#result + 1] = getTableOutput(nestedSelector.tableId, visited)


if wrapped and (classesRolls or classesProb) then
    if wrapped and (classesRolls or classesProb) then
    if (classesRolls and #classesRolls > 0) or (classesProb and #classesProb > 0) then
         result[#result + 1] = "}}"
         result[#result + 1] = "}}"
    end
    end
end


     return table.concat(result)
     return table.concat(result)
Строка 229: Строка 243:


-- Формирование списка химии
-- Формирование списка химии
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


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


     if mode == 'framing' then
     if mode == 'framing' then
Строка 262: Строка 277:


         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