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

Нет описания правки
Нет описания правки
Строка 15: Строка 15:
     local result = {}
     local result = {}
     if rolls and rolls.range then
     if rolls and rolls.range then
        -- Если указан range
         local min, max = rolls.range:match("(%d+),%s*(%d+)")
         local min, max = rolls.range:match("(%d+),%s*(%d+)")
         min, max = tonumber(min), tonumber(max)
         min, max = tonumber(min), tonumber(max)
Строка 24: Строка 23:
         end
         end
     elseif rolls and rolls.value then
     elseif rolls and rolls.value then
        -- Если указано value
         result[#result + 1] = string.format('[%d]', rolls.value)
         result[#result + 1] = string.format('[%d]', rolls.value)
     else
     else
Строка 35: Строка 33:
local findDataById, formatContent, getContentsOutput, processNestedSelectors, getTableOutput, getContainedOutput, getChemOutput, handleGroupSelector, handleAllSelector, handleNestedSelector
local findDataById, formatContent, getContentsOutput, processNestedSelectors, getTableOutput, getContainedOutput, getChemOutput, handleGroupSelector, handleAllSelector, handleNestedSelector


-- Функция для создания хэш-таблицы для быстрого поиска по ID
-- Исправленная функция создания хэш-таблицы для быстрого поиска по ID (рекурсивная версия)
local function buildIndex(itemData)
local function buildIndex(itemData)
    if not itemData then return {} end
     local index = {}
     local index = {}
     for _, item in ipairs(itemData) do
     local function recursiveIndex(tbl)
         index[item.id] = item
        if type(tbl) ~= "table" then return end
         if tbl.id then
            index[tbl.id] = tbl
        end
        for k, v in pairs(tbl) do
            if type(v) == "table" then
                recursiveIndex(v)
            end
        end
     end
     end
    recursiveIndex(itemData)
     return index
     return index
end
end
Строка 58: Строка 64:
     local name = string.format('{{#invoke:Entity Lookup|getname|%s}}', content.id)
     local name = string.format('{{#invoke:Entity Lookup|getname|%s}}', content.id)
     local image = string.format('%s.png', content.id)
     local image = string.format('%s.png', content.id)
local amount = (content.amount and content.amount ~= 1) and string.format(" [%d]", content.amount) or ""
    local amount = (content.amount and content.amount ~= 1) and string.format(" [%d]", content.amount) or ""
     local prob = ""
     local prob = ""


Строка 93: Строка 99:
         if child.id then
         if child.id then
             results[#results + 1] = formatContent(child)
             results[#results + 1] = formatContent(child)
        elseif child["!type"] == "NestedSelector" then
            results[#results + 1] = handleNestedSelector(child, true)
         elseif child["!type"] == "GroupSelector" then
         elseif child["!type"] == "GroupSelector" then
             results[#results + 1] = handleGroupSelector(child)
             results[#results + 1] = handleGroupSelector(child)
        elseif child["!type"] == "AllSelector" then
            results[#results + 1] = handleAllSelector(child)
        end
        if child.children then
            results[#results + 1] = processNestedSelectors(child.children)
         end
         end
     end
     end
Строка 203: Строка 205:


     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)
Строка 214: Строка 216:
     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
        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
    end


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


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
        if (classesRolls and #classesRolls > 0) or (classesProb and #classesProb > 0) then
        result[#result + 1] = "}}"
            result[#result + 1] = "}}"
    end
        end
end
    end


     return table.concat(result)
     return table.concat(result)
Строка 252: Строка 254:
     if not id then return 'Не указан ID.' end
     if not id then return 'Не указан ID.' end
      
      
local itemDataIndex = buildIndex(itemData)
    local itemDataIndex = buildIndex(itemData)
   
     if not itemData then return 'Не удалось загрузить данные.' end
     if not itemData then return 'Не удалось загрузить данные.' end