Модуль:Песочница/Pok: различия между версиями
Pok (обсуждение | вклад) мНет описания правки |
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) | ||
| Строка 23: | Строка 24: | ||
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 | ||
| Строка 33: | Строка 35: | ||
local findDataById, formatContent, getContentsOutput, processNestedSelectors, getTableOutput, getContainedOutput, getChemOutput, handleGroupSelector, handleAllSelector, handleNestedSelector | local findDataById, formatContent, getContentsOutput, processNestedSelectors, getTableOutput, getContainedOutput, getChemOutput, handleGroupSelector, handleAllSelector, handleNestedSelector | ||
-- | -- Исправленная функция buildIndex, поддерживающая как одиночный объект, так и массив объектов | ||
local function buildIndex(itemData) | local function buildIndex(itemData) | ||
if not itemData then return {} end | |||
local index = {} | local index = {} | ||
if itemData.id then | |||
-- Если данные представляют собой одиночный объект | |||
index[itemData.id] = itemData | |||
else | |||
-- Если данные представляют собой массив объектов | |||
for | for _, item in ipairs(itemData) do | ||
if | if item.id then | ||
index[item.id] = item | |||
end | end | ||
end | end | ||
end | end | ||
return index | return index | ||
end | end | ||
| Строка 56: | Строка 58: | ||
end | end | ||
-- Форматирование содержимого | -- Форматирование содержимого | ||
formatContent = function(content) | formatContent = function(content) | ||
if not content.id then | if not content.id then | ||
| Строка 68: | Строка 68: | ||
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 = "" | ||
if content.weight then | if content.weight then | ||
content.prob = content.weight / 100 | content.prob = content.weight / 100 | ||
end | end | ||
if content.prob then | if content.prob then | ||
prob = string.format(" <div>%s%%</div>", content.prob * 100 >= 1 and math.floor(content.prob * 100) or content.prob * 100) | prob = string.format(" <div>%s%%</div>", content.prob * 100 >= 1 and math.floor(content.prob * 100) or content.prob * 100) | ||
end | end | ||
return string.format( | return string.format( | ||
'{{LinkСard|SideStyle=1|background-color=#cbcbff0a|image=%s|name=%s%s%s%s}}', | '{{LinkСard|SideStyle=1|background-color=#cbcbff0a|image=%s|name=%s%s%s {{#invoke:Prototypes/Хранилище/Предмет|main|framing|contained|%s}} }}', | ||
image, name, amount, prob, | image, name, amount, prob, content.id | ||
) | ) | ||
end | end | ||
| Строка 102: | Строка 97: | ||
local results = {} | local results = {} | ||
for _, child in ipairs(children) do | for _, child in ipairs(children) do | ||
if child.id then | if child.id then | ||
| Строка 111: | Строка 107: | ||
end | end | ||
end | end | ||
return table.concat(results) | return table.concat(results) | ||
end | end | ||
| Строка 117: | Строка 114: | ||
getTableOutput = function(tableId) | getTableOutput = function(tableId) | ||
local tableData = loadData('User:IanComradeBot/prototypes/table.json') | local tableData = loadData('User:IanComradeBot/prototypes/table.json') | ||
local tableDataIndex = findDataById(tableData, tableId) | local tableDataIndex = findDataById(buildIndex(tableData), tableId) | ||
if not tableDataIndex then return 'Таблица не найдена.' end | if not tableDataIndex then return 'Таблица не найдена.' end | ||
| Строка 130: | Строка 127: | ||
end | end | ||
-- Формирование списка содержащихся предметов или таблиц | -- Формирование списка содержащихся предметов или таблиц | ||
getContainedOutput = function(itemData, id) | getContainedOutput = function(itemData, id) | ||
local item = findDataById(itemData, id) | local item = findDataById(itemData, id) | ||
if not item then return '' end | if not item then return '' end | ||
local result = {} | |||
-- Обработка StorageFill | |||
if item.StorageFill and item.StorageFill.contents then | if item.StorageFill and item.StorageFill.contents then | ||
result[#result + 1] = getContentsOutput(item.StorageFill.contents) | result[#result + 1] = getContentsOutput(item.StorageFill.contents) | ||
-- Обработка EntityTableContainerFill | |||
elseif item.EntityTableContainerFill and item.EntityTableContainerFill.containers then | elseif item.EntityTableContainerFill and item.EntityTableContainerFill.containers then | ||
local containers = item.EntityTableContainerFill.containers | local containers = item.EntityTableContainerFill.containers | ||
| Строка 151: | Строка 147: | ||
result[#result + 1] = processNestedSelectors(containers.entity_storage.children) | result[#result + 1] = processNestedSelectors(containers.entity_storage.children) | ||
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) | ||
| Строка 161: | Строка 158: | ||
end | end | ||
end | end | ||
return table.concat(result) | return table.concat(result) | ||
end | end | ||
| Строка 176: | Строка 174: | ||
local wrapperStart, wrapperEnd = "", "" | local wrapperStart, wrapperEnd = "", "" | ||
-- Проверка для контейнера EntityTableContainerFill | |||
if groupSelector.weight and groupSelector.weight ~= "default" then | if groupSelector.weight and groupSelector.weight ~= "default" then | ||
wrapperStart = string.format('{{LinkСard/Сollapsible|name=Группа предметов %s%%|content=', groupSelector.weight) | wrapperStart = string.format('{{LinkСard/Сollapsible|name=Группа предметов %s%%|content=', groupSelector.weight) | ||
| Строка 204: | Строка 203: | ||
local result = {} | local result = {} | ||
local classesRolls, classesProb | local classesRolls, classesProb | ||
| Строка 255: | Строка 255: | ||
if not id then return 'Не указан ID.' end | if not id then return 'Не указан ID.' end | ||
local itemDataIndex = buildIndex(itemData) | local itemDataIndex = buildIndex(itemData) | ||