Модуль:Песочница/Pok: различия между версиями
Pok (обсуждение | вклад) Нет описания правки |
Pok (обсуждение | вклад) Нет описания правки |
||
| Строка 33: | Строка 33: | ||
local findDataById, formatContent, getContentsOutput, processNestedSelectors, getTableOutput, getContainedOutput, getChemOutput, handleGroupSelector, handleAllSelector, handleNestedSelector | local findDataById, formatContent, getContentsOutput, processNestedSelectors, getTableOutput, getContainedOutput, getChemOutput, handleGroupSelector, handleAllSelector, handleNestedSelector | ||
-- | -- Функция создания хэш-таблицы для быстрого поиска по ID (рекурсивно) | ||
local function buildIndex(itemData) | local function buildIndex(itemData) | ||
local index = {} | local index = {} | ||
| Строка 56: | Строка 56: | ||
end | end | ||
-- Форматирование содержимого | -- Форматирование содержимого предмета. | ||
-- Если у элемента есть вложенные контейнеры, добавляется вызов рекурсивного инвока; | |||
-- для простых элементов – выводится только информация по предмету. | |||
formatContent = function(content) | formatContent = function(content) | ||
if not content.id then | if not content.id then | ||
| Строка 66: | Строка 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 | |||
local containedPart = "" | |||
-- Добавляем вложенный вызов только если у элемента действительно есть вложенные данные | |||
if content.StorageFill or (content.EntityTableContainerFill and content.EntityTableContainerFill.containers) then | |||
containedPart = string.format(" {{#invoke:Prototypes/Хранилище/Предмет|main|framing|contained|%s}}", content.id) | |||
end | end | ||
return string.format( | return string.format( | ||
'{{LinkСard|SideStyle=1|background-color=#cbcbff0a|image=%s|name=%s%s%s | '{{LinkСard|SideStyle=1|background-color=#cbcbff0a|image=%s|name=%s%s%s%s}}', | ||
image, name, amount, prob, | image, name, amount, prob, containedPart | ||
) | ) | ||
end | end | ||
| Строка 95: | Строка 102: | ||
local results = {} | local results = {} | ||
for _, child in ipairs(children) do | for _, child in ipairs(children) do | ||
if child.id then | if child.id then | ||
| Строка 105: | Строка 111: | ||
end | end | ||
end | end | ||
return table.concat(results) | return table.concat(results) | ||
end | end | ||
| Строка 125: | Строка 130: | ||
end | end | ||
-- Формирование списка содержащихся предметов или таблиц | -- Формирование списка содержащихся предметов или таблиц. | ||
-- Если у найденного элемента нет ни StorageFill, ни EntityTableContainerFill, он обрабатывается как простой элемент. | |||
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 | ||
if not (item.StorageFill or (item.EntityTableContainerFill and item.EntityTableContainerFill.containers)) then | |||
return formatContent(item) | |||
end | |||
local result = {} | local result = {} | ||
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) | ||
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 | ||
| Строка 145: | Строка 151: | ||
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) | ||
| Строка 156: | Строка 161: | ||
end | end | ||
end | end | ||
return table.concat(result) | return table.concat(result) | ||
end | end | ||
| Строка 172: | Строка 176: | ||
local wrapperStart, wrapperEnd = "", "" | local wrapperStart, wrapperEnd = "", "" | ||
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) | ||
| Строка 201: | Строка 204: | ||
local result = {} | local result = {} | ||
local classesRolls, classesProb | local classesRolls, classesProb | ||
| Строка 253: | Строка 255: | ||
if not id then return 'Не указан ID.' end | if not id then return 'Не указан ID.' end | ||
local itemDataIndex = buildIndex(itemData) | local itemDataIndex = buildIndex(itemData) | ||