Модуль:Prototypes/Хранилище/Предмет: различия между версиями
Pok (обсуждение | вклад) мНет описания правки |
Pok (обсуждение | вклад) мНет описания правки |
||
| Строка 3: | Строка 3: | ||
-- Кэш данных | -- Кэш данных | ||
local dataCache = {} | local dataCache = {} | ||
local indexCache = {} | |||
-- Функция processRolls для преобразования диапазона | -- Функция processRolls для преобразования диапазона | ||
| Строка 24: | Строка 25: | ||
return table.concat(result) | return table.concat(result) | ||
end | end | ||
-- Локальные функции | |||
local loadData, findDataById, formatContent, getContentsOutput, processNestedSelectors, getTableOutput, getContainedOutput, getChemOutput, handleGroupSelector, handleAllSelector, handleNestedSelector | |||
-- Функция для загрузки данных с кэшированием | -- Функция для загрузки данных с кэшированием | ||
loadData = function(filePath) | |||
if not dataCache[filePath] then | if not dataCache[filePath] then | ||
local page = mw.title.new(filePath) | local page = mw.title.new(filePath) | ||
| Строка 37: | Строка 41: | ||
-- Создание хэш-таблицы для быстрого поиска по ID | -- Создание хэш-таблицы для быстрого поиска по ID | ||
local function buildIndex(data) | local function buildIndex(data) | ||
if indexCache[data] then return indexCache[data] end | |||
if not data then return {} end | if not data then return {} end | ||
local index = {} | local index = {} | ||
| Строка 42: | Строка 47: | ||
index[item.id] = item | index[item.id] = item | ||
end | end | ||
indexCache[data] = index | |||
return index | return index | ||
end | end | ||
-- Поиск данных по ID через хэш-таблицу | -- Поиск данных по ID через хэш-таблицу | ||
findDataById = function(dataIndex, id) | |||
return dataIndex and dataIndex[id] or nil | return dataIndex and dataIndex[id] or nil | ||
end | end | ||
-- Форматирование содержимого | -- Форматирование содержимого | ||
ormatContent = function(content) | |||
if not content.id then | if not content.id then | ||
return "Ошибка: отсутствует id у элемента." | return "Ошибка: отсутствует id у элемента." | ||
| Строка 76: | Строка 82: | ||
-- Получение содержимого через таблицу | -- Получение содержимого через таблицу | ||
getContentsOutput = function(contents) | |||
local result = {} | local result = {} | ||
for _, content in ipairs(contents) do | for _, content in ipairs(contents) do | ||
| Строка 85: | Строка 91: | ||
-- Обработка вложенных таблиц | -- Обработка вложенных таблиц | ||
processNestedSelectors = function(children) | |||
if not children or #children == 0 then return "" end | |||
local | |||
local results = {} | |||
local nestedCache = {} | |||
for _, child in ipairs(children) do | for _, child in ipairs(children) do | ||
if child.id then | if child.id then | ||
results[#results + 1] = formatContent(child) | |||
elseif child["!type"] == "NestedSelector" | elseif child["!type"] == "NestedSelector" then | ||
-- Кэшируем результат вызова handleNestedSelector | |||
if not nestedCache[child.tableId] then | |||
nestedCache[child.tableId] = handleNestedSelector(child, true) | |||
end | |||
results[#results + 1] = nestedCache[child.tableId] | |||
elseif child["!type"] == "GroupSelector" then | elseif child["!type"] == "GroupSelector" then | ||
results[#results + 1] = handleGroupSelector(child) | |||
end | end | ||
end | end | ||
return table.concat( | |||
return table.concat(results) | |||
end | end | ||
-- Обработка таблиц | -- Обработка таблиц | ||
getTableOutput = function(tableId) | |||
local allSelectors = loadData('User:IanComradeBot/prototypes/AllSelector.json') | local allSelectors = loadData('User:IanComradeBot/prototypes/AllSelector.json') | ||
local tableData = findDataById(buildIndex(allSelectors), tableId) | local tableData = findDataById(buildIndex(allSelectors), tableId) | ||
| Строка 118: | Строка 131: | ||
-- Формирование списка содержащихся предметов или таблиц | -- Формирование списка содержащихся предметов или таблиц | ||
getContainedOutput = function(data, id) | |||
local item = findDataById(data, id) | local item = findDataById(data, id) | ||
if not item then return '' end | if not item then return '' end | ||
| Строка 153: | Строка 166: | ||
-- Обработка AllSelector | -- Обработка AllSelector | ||
handleAllSelector = function(allSelector) | |||
if not allSelector.children then return '' end | if not allSelector.children then return '' end | ||
return processNestedSelectors(allSelector.children) | return processNestedSelectors(allSelector.children) | ||
| Строка 159: | Строка 172: | ||
-- Обработка GroupSelector | -- Обработка GroupSelector | ||
handleGroupSelector = function(groupSelector) | |||
if not groupSelector.children then return '' end | if not groupSelector.children then return '' end | ||
local result = {} | local result = {} | ||
| Строка 189: | Строка 202: | ||
-- Обработка NestedSelector | -- Обработка NestedSelector | ||
handleNestedSelector = function(nestedSelector, wrapped) | |||
if not nestedSelector.tableId then return '' end | if not nestedSelector.tableId then return '' end | ||
| Строка 218: | Строка 231: | ||
-- Формирование списка химии | -- Формирование списка химии | ||
getChemOutput = function(data, id) | |||
local item = findDataById(data, id) | local item = findDataById(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 | ||
| Строка 265: | Строка 278: | ||
local entity = findDataById(dataIndex, id) | local entity = findDataById(dataIndex, id) | ||
if not entity then return 'ID не найден в данных.' end | if not entity then return 'ID не найден в данных.' end | ||
if entity.EntityTableContainerFill then | if entity.EntityTableContainerFill then | ||
local containers = entity.EntityTableContainerFill.containers | local containers = entity.EntityTableContainerFill.containers | ||
| Строка 274: | Строка 287: | ||
return 'Режим rolls не найден для этого ID.' | return 'Режим rolls не найден для этого ID.' | ||
else | |||
return 'Неизвестный режим: ' .. mode | return 'Неизвестный режим: ' .. mode | ||
end | end | ||