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

мНет описания правки
мНет описания правки
Строка 1: Строка 1:
local loadData, findDataById, formatContent, getContentsOutput, processNestedSelectors, getTableOutput, getContainedOutput, getChemOutput
local p = {}
local p = {}


-- Функции как локальные переменные, которые будут привязаны позже
-- Функция для загрузки данных
local loadData, findDataById, formatContent, getContentsOutput, processNestedSelectors, getTableOutput, getContainedOutput, getChemOutput
 
-- Функция для загрузки данных без кеширования
loadData = function(filePath)
loadData = function(filePath)
     local page = mw.title.new(filePath)
     local page = mw.title.new(filePath)
Строка 50: Строка 48:
end
end


-- Обработка вложенных таблиц
-- Функция выбора элемента на основе веса
processNestedSelectors = function(children)
pickByWeight = function(children)
     local result = ''
     local totalWeight = 0
    for _, child in ipairs(children) do
        totalWeight = totalWeight + (child.weight or 1) -- Если вес не указан, принимаем за 1
    end
 
    local rand = math.random() * totalWeight -- Случайное число от 0 до totalWeight
    local cumulativeWeight = 0
 
     for _, child in ipairs(children) do
     for _, child in ipairs(children) do
         if child.id then
         cumulativeWeight = cumulativeWeight + (child.weight or 1)
            result = result .. formatContent(child)
         if rand <= cumulativeWeight then
         elseif child["!type"] == "NestedSelector" and child.tableId then
             return child
             result = result .. getTableOutput(child.tableId)
         end
         end
     end
     end
     return result
 
     return nil -- На случай, если ничего не выбрано (теоретически невозможно)
end
end


-- Обработка таблиц
-- Обработка таблиц
getTableOutput = function(tableId)
getTableOutput = function(tableId, rolls)
     local allSelectors = loadData('User:IanComradeBot/prototypes/AllSelector.json')
     local allSelectors = loadData('User:IanComradeBot/prototypes/AllSelector.json')
     local tableData = findDataById(allSelectors, tableId)
     local tableData = findDataById(allSelectors, tableId)
     local children = tableData and tableData['!type:AllSelector'] and tableData['!type:AllSelector'].children
     local children = tableData and (tableData['!type:GroupSelector'] and tableData['!type:GroupSelector'].children or tableData['!type:AllSelector'] and tableData['!type:AllSelector'].children)


     if not children then return 'Таблица не содержит элементов.' end
     if not children then return 'Таблица не содержит элементов.' end


     return processNestedSelectors(children)
     if tableData['!type:AllSelector'] then
        return processAllSelectors(children)
    else
        return processNestedSelectors(children, rolls)
    end
end
 
-- Обработка AllSelector
processAllSelectors = function(children)
    local result = ''
    for _, child in ipairs(children) do
        if child.id then
            result = result .. formatContent(child)
        elseif child["!type"] == "GroupSelector" and child.children then
            result = result .. processNestedSelectors(child.children, 1)
        elseif child["!type"] == "AllSelector" and child.children then
            result = result .. processAllSelectors(child.children)
        elseif child["!type"] == "NestedSelector" and child.tableId then
            local nestedRolls
            if child.rolls then
                if child.rolls.value then
                    nestedRolls = child.rolls.value
                elseif child.rolls.range then
                    local rangeParts = {string.match(child.rolls.range, "(%d+),%s*(%d+)")}
                    nestedRolls = math.random(tonumber(rangeParts[1]), tonumber(rangeParts[2]))
                else
                    nestedRolls = 1
                end
            else
                nestedRolls = 1
            end
            result = result .. getTableOutput(child.tableId, nestedRolls)
        end
    end
    return result
end
 
-- Обработка вложенных таблиц
processNestedSelectors = function(children, rolls)
    local result = ''
    for i = 1, rolls do
        local selectedChild = pickByWeight(children)
        if selectedChild then
            if selectedChild.id then
                result = result .. formatContent(selectedChild)
            elseif selectedChild["!type"] == "GroupSelector" and selectedChild.children then
                result = result .. processNestedSelectors(selectedChild.children, 1)
            elseif selectedChild["!type"] == "AllSelector" and selectedChild.children then
                result = result .. processAllSelectors(selectedChild.children)
            elseif selectedChild["!type"] == "NestedSelector" and selectedChild.tableId then
                local nestedRolls
                if selectedChild.rolls then
                    if selectedChild.rolls.value then
                        nestedRolls = selectedChild.rolls.value
                    elseif selectedChild.rolls.range then
                        local rangeParts = {string.match(selectedChild.rolls.range, "(%d+),%s*(%d+)")}
                        nestedRolls = math.random(tonumber(rangeParts[1]), tonumber(rangeParts[2]))
                    else
                        nestedRolls = 1
                    end
                else
                    nestedRolls = 1
                end
                result = result .. getTableOutput(selectedChild.tableId, nestedRolls)
            end
        end
    end
    return result
end
end


Строка 87: Строка 159:
                         or containers.entity_storage and containers.entity_storage.tableId
                         or containers.entity_storage and containers.entity_storage.tableId
                         or containers.other_storage and containers.other_storage.tableId
                         or containers.other_storage and containers.other_storage.tableId
        local rolls = containers.storagebase and containers.storagebase.rolls and containers.storagebase.rolls.value
                  or containers.entity_storage and containers.entity_storage.rolls and containers.entity_storage.rolls.value
                  or 1


         if tableId then
         if tableId then
             result = result .. getTableOutput(tableId)
             result = result .. getTableOutput(tableId, rolls)
         else
         else
             result = result .. 'Таблица не найдена.'
             result = result .. 'Таблица не найдена.'
Строка 123: Строка 199:
     if not id then return 'Не указан ID.' end
     if not id then return 'Не указан ID.' end


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