Модуль:Prototypes/Хранилище/Предмет: различия между версиями
Pok (обсуждение | вклад) Нет описания правки |
Pok (обсуждение | вклад) мНет описания правки |
||
| Строка 4: | Строка 4: | ||
local dataCache = {} | local dataCache = {} | ||
local dataIndexCache = {} | local dataIndexCache = {} | ||
-- Проверка на существование функции | |||
local function safeCall(func, ...) | |||
if type(func) == "function" then | |||
return func(...) | |||
else | |||
return nil, "Функция не определена." | |||
end | |||
end | |||
-- Функция processRolls для преобразования диапазона | -- Функция processRolls для преобразования диапазона | ||
| Строка 31: | Строка 40: | ||
dataCache[filePath] = content and mw.text.jsonDecode(content) or nil | dataCache[filePath] = content and mw.text.jsonDecode(content) or nil | ||
if dataCache[filePath] then | if dataCache[filePath] then | ||
dataIndexCache[filePath] = buildIndex | dataIndexCache[filePath] = safeCall(buildIndex, dataCache[filePath]) | ||
end | end | ||
end | end | ||
| Строка 81: | Строка 90: | ||
local result = {} | local result = {} | ||
for _, content in ipairs(contents) do | for _, content in ipairs(contents) do | ||
result[#result + 1] = formatContent | result[#result + 1] = safeCall(formatContent, content) | ||
end | end | ||
return table.concat(result) | return table.concat(result) | ||
| Строка 91: | Строка 100: | ||
for _, child in ipairs(children) do | for _, child in ipairs(children) do | ||
if child.id then | if child.id then | ||
result[#result + 1] = formatContent | result[#result + 1] = safeCall(formatContent, child) | ||
elseif child["!type"] == "NestedSelector" and child.tableId then | elseif child["!type"] == "NestedSelector" and child.tableId then | ||
result[#result + 1] = handleNestedSelector | result[#result + 1] = safeCall(handleNestedSelector, child, true) | ||
elseif child["!type"] == "GroupSelector" then | elseif child["!type"] == "GroupSelector" then | ||
result[#result + 1] = handleGroupSelector | result[#result + 1] = safeCall(handleGroupSelector, child) | ||
end | end | ||
end | end | ||
| Строка 106: | Строка 115: | ||
if not allSelectors then return 'Таблица не найдена.' end | if not allSelectors then return 'Таблица не найдена.' end | ||
local tableData = findDataById | local tableData = safeCall(findDataById, dataIndex, tableId) | ||
if not tableData then return 'Таблица не найдена.' end | if not tableData then return 'Таблица не найдена.' end | ||
if tableData['!type:GroupSelector'] then | if tableData['!type:GroupSelector'] then | ||
return handleGroupSelector | return safeCall(handleGroupSelector, tableData['!type:GroupSelector']) | ||
elseif tableData['!type:AllSelector'] then | elseif tableData['!type:AllSelector'] then | ||
return processNestedSelectors | return safeCall(processNestedSelectors, tableData['!type:AllSelector'].children) | ||
end | end | ||
| Строка 120: | Строка 129: | ||
-- Формирование списка содержащихся предметов или таблиц | -- Формирование списка содержащихся предметов или таблиц | ||
local getContainedOutput = function(data, id) | local getContainedOutput = function(data, id) | ||
local item = findDataById(buildIndex | local item = safeCall(findDataById, safeCall(buildIndex, data), id) | ||
if not item then return '' end | if not item then return '' end | ||
| Строка 127: | Строка 136: | ||
-- Обработка StorageFill | -- Обработка StorageFill | ||
if item.StorageFill and item.StorageFill.contents then | if item.StorageFill and item.StorageFill.contents then | ||
result[#result + 1] = getContentsOutput | result[#result + 1] = safeCall(getContentsOutput, item.StorageFill.contents) | ||
-- Обработка EntityTableContainerFill | -- Обработка EntityTableContainerFill | ||
| Строка 136: | Строка 145: | ||
if containers.entity_storage then | if containers.entity_storage then | ||
if containers.entity_storage.children then | if containers.entity_storage.children then | ||
result[#result + 1] = processNestedSelectors | result[#result + 1] = safeCall(processNestedSelectors, containers.entity_storage.children) | ||
end | end | ||
if containers.entity_storage.tableId then | if containers.entity_storage.tableId then | ||
result[#result + 1] = getTableOutput | result[#result + 1] = safeCall(getTableOutput, containers.entity_storage.tableId) | ||
end | end | ||
end | end | ||
| Строка 146: | Строка 155: | ||
-- Обработка storagebase | -- Обработка storagebase | ||
if containers.storagebase and containers.storagebase.tableId then | if containers.storagebase and containers.storagebase.tableId then | ||
result[#result + 1] = getTableOutput | result[#result + 1] = safeCall(getTableOutput, containers.storagebase.tableId) | ||
end | end | ||
end | end | ||
| Строка 156: | Строка 165: | ||
local handleAllSelector = function(allSelector) | local handleAllSelector = function(allSelector) | ||
if not allSelector.children then return '' end | if not allSelector.children then return '' end | ||
return processNestedSelectors | return safeCall(processNestedSelectors, allSelector.children) | ||
end | end | ||
| Строка 175: | Строка 184: | ||
for _, child in ipairs(groupSelector.children) do | for _, child in ipairs(groupSelector.children) do | ||
if child["!type"] == "GroupSelector" then | if child["!type"] == "GroupSelector" then | ||
result[#result + 1] = handleGroupSelector | result[#result + 1] = safeCall(handleGroupSelector, child) | ||
elseif child["!type"] == "AllSelector" then | elseif child["!type"] == "AllSelector" then | ||
result[#result + 1] = string.format('<div class="AllSelector">%s</div>', handleAllSelector | result[#result + 1] = string.format('<div class="AllSelector">%s</div>', safeCall(handleAllSelector, child)) | ||
elseif child.id then | elseif child.id then | ||
result[#result + 1] = formatContent | result[#result + 1] = safeCall(formatContent, child) | ||
else | else | ||
result[#result + 1] = "<div>Ошибка: отсутствует id у элемента.</div>" | result[#result + 1] = "<div>Ошибка: отсутствует id у элемента.</div>" | ||
| Строка 197: | Строка 206: | ||
if wrapped then | if wrapped then | ||
if nestedSelector.rolls and nestedSelector.rolls.range then | if nestedSelector.rolls and nestedSelector.rolls.range then | ||
classes[#classes + 1] = 'rolls-' .. processRolls | classes[#classes + 1] = 'rolls-' .. safeCall(processRolls, nestedSelector.rolls) | ||
end | end | ||
if nestedSelector.prob then | if nestedSelector.prob then | ||
| Строка 208: | Строка 217: | ||
end | end | ||
result[#result + 1] = getTableOutput | result[#result + 1] = safeCall(getTableOutput, nestedSelector.tableId) | ||
if wrapped and #classes > 0 then | if wrapped and #classes > 0 then | ||
| Строка 219: | Строка 228: | ||
-- Формирование списка химии | -- Формирование списка химии | ||
local getChemOutput = function(data, id) | local getChemOutput = function(data, id) | ||
local item = findDataById(buildIndex | local item = safeCall(findDataById, safeCall(buildIndex, data), id) | ||
if not item or not item.SolutionContainerManager or not item.SolutionContainerManager.solutions then return '' end | if not item or not item.SolutionContainerManager or not item.SolutionContainerManager.solutions then return '' end | ||
| Строка 250: | Строка 259: | ||
if subMode == 'chem' then | if subMode == 'chem' then | ||
return frame:preprocess('{{СollapsibleMenu|' .. getChemOutput | return frame:preprocess('{{СollapsibleMenu|' .. safeCall(getChemOutput, dataIndex, id) .. '}}') | ||
elseif subMode == 'contained' then | elseif subMode == 'contained' then | ||
return frame:preprocess('{{СollapsibleMenu|' .. getContainedOutput | return frame:preprocess('{{СollapsibleMenu|' .. safeCall(getContainedOutput, dataIndex, id) .. '}}') | ||
else | else | ||
return 'Неизвестный подрежим для framing: ' .. subMode | return 'Неизвестный подрежим для framing: ' .. subMode | ||
end | end | ||
elseif mode == 'chem' then | elseif mode == 'chem' then | ||
return frame:preprocess(getChemOutput | return frame:preprocess(safeCall(getChemOutput, dataIndex, id)) | ||
elseif mode == 'contained' then | elseif mode == 'contained' then | ||
return frame:preprocess(getContainedOutput | return frame:preprocess(safeCall(getContainedOutput, dataIndex, id)) | ||
elseif mode == 'rolls' then | elseif mode == 'rolls' then | ||
local entity = findDataById | local entity = safeCall(findDataById, dataIndex, id) | ||
if not entity then return 'ID не найден в данных.' end | if not entity then return 'ID не найден в данных.' end | ||
| Строка 267: | Строка 276: | ||
local containers = entity.EntityTableContainerFill.containers | local containers = entity.EntityTableContainerFill.containers | ||
if containers.entity_storage and containers.entity_storage.rolls then | if containers.entity_storage and containers.entity_storage.rolls then | ||
return processRolls | return safeCall(processRolls, containers.entity_storage.rolls) | ||
end | end | ||
end | end | ||