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

мНет описания правки
мНет описания правки
Строка 52: Строка 52:
end
end


-- Получение содержимого
local function getContentsOutput(contents)
local function getContentsOutput(contents)
     local result = ''
     local result = ''
Строка 59: Строка 58:
     end
     end
     return result
     return result
end
-- Функция выбора элемента на основе веса
local function pickByWeight(children)
    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
        cumulativeWeight = cumulativeWeight + (child.weight or 1)
        if rand <= cumulativeWeight then
            return child
        end
    end
    return nil -- На случай, если ничего не выбрано (теоретически невозможно)
end
end


Строка 91: Строка 70:
             result = result .. processAllSelectors(child.children)
             result = result .. processAllSelectors(child.children)
         elseif child["!type"] == "NestedSelector" and child.tableId then
         elseif child["!type"] == "NestedSelector" and child.tableId then
             local nestedRolls = child.rolls and (child.rolls.value or (child.rolls.range and math.random(string.match(child.rolls.range, "(%d+),%s*(%d+)")))) or 1
             local nestedRolls = child.rolls and (child.rolls.value or math.random(tonumber(child.rolls.range:match("(%d+),%s*(%d+)")))) or 1
             result = result .. getTableOutput(child.tableId, nestedRolls)
             result = result .. getTableOutput(child.tableId, nestedRolls)
         end
         end
Строка 98: Строка 77:
end
end


-- Обработка вложенных таблиц
local function processNestedSelectors(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 = selectedChild.rolls and (selectedChild.rolls.value or (selectedChild.rolls.range and math.random(string.match(selectedChild.rolls.range, "(%d+),%s*(%d+)")))) or 1
                result = result .. getTableOutput(selectedChild.tableId, nestedRolls)
            end
        end
    end
    return result
end
-- Обработка таблиц
local function getTableOutput(tableId, rolls)
local function getTableOutput(tableId, rolls)
     local allSelectors = loadData('User:IanComradeBot/prototypes/AllSelector.json', 'selectors')
     local allSelectors = loadData('User:IanComradeBot/prototypes/AllSelector.json', 'selectors')
Строка 134: Строка 91:
end
end


-- Формирование списка содержащихся предметов или таблиц
local function processNestedSelectors(children, rolls)
local function getContainedOutput(data, id, wrapped)
    local item = findDataById(data, id)
    if not item then return '' end
 
     local result = ''
     local result = ''
     if item.StorageFill and item.StorageFill.contents then
     for i = 1, rolls do
        result = result .. getContentsOutput(item.StorageFill.contents)
        local selectedChild = pickByWeight(children)
    elseif item.EntityTableContainerFill and item.EntityTableContainerFill.containers then
        if selectedChild then
        local containers = item.EntityTableContainerFill.containers
            if selectedChild.id then
        local tableId = containers.storagebase and containers.storagebase.tableId or containers.entity_storage and containers.entity_storage.tableId or containers.other_storage and containers.other_storage.tableId
                result = result .. formatContent(selectedChild)
        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
            elseif selectedChild["!type"] == "GroupSelector" and selectedChild.children then
 
                result = result .. processNestedSelectors(selectedChild.children, 1)
        if tableId then
            elseif selectedChild["!type"] == "AllSelector" and selectedChild.children then
            result = result .. getTableOutput(tableId, rolls)
                result = result .. processAllSelectors(selectedChild.children)
        else
            elseif selectedChild["!type"] == "NestedSelector" and selectedChild.tableId then
             result = result .. 'Таблица не найдена.'
                local nestedRolls = selectedChild.rolls and (selectedChild.rolls.value or math.random(tonumber(selectedChild.rolls.range:match("(%d+),%s*(%d+)")))) or 1
                result = result .. getTableOutput(selectedChild.tableId, nestedRolls)
             end
         end
         end
     end
     end
Строка 160: Строка 115:
     local mode = frame.args[1]
     local mode = frame.args[1]
     local id = frame.args[2]
     local id = frame.args[2]
     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', 'items')
     if not data then return 'Не удалось загрузить данные.' end
     if not data then return 'Не удалось загрузить данные.' end
     if mode == 'contained' then
 
     -- При режиме framing
    if mode == 'framing' then
        local subMode = frame.args[2]
        local id = frame.args[3]
   
        if not id then
            return 'Не указан ID для режима framing.'
        end
   
        if subMode == 'chem' then
            return frame:preprocess('{{СollapsibleMenu|' .. getChemOutput(data, id) .. '}}')
        elseif subMode == 'contained' then
            return frame:preprocess('{{СollapsibleMenu|' .. getContainedOutput(data, id, false) .. '}}')
        else
            return 'Неизвестный подрежим для framing: ' .. subMode
        end
    end
 
    -- При нормальном режиме
    if mode == 'chem' then
        return frame:preprocess(getChemOutput(data, id))
    elseif mode == 'contained' then
         return frame:preprocess(getContainedOutput(data, id, false))
         return frame:preprocess(getContainedOutput(data, id, false))
     else
     else