Модуль:Песочница/Pok: различия между версиями
Pok (обсуждение | вклад) Нет описания правки |
Pok (обсуждение | вклад) Нет описания правки |
||
| Строка 1: | Строка 1: | ||
local p = {} | local p = {} | ||
-- | -- Кэш данных | ||
local | local dataCache = {} | ||
local indexCache = {} | |||
-- Функция processRolls для преобразования диапазона | -- Функция processRolls для преобразования диапазона | ||
| Строка 34: | Строка 27: | ||
-- Локальные функции | -- Локальные функции | ||
local findDataById, formatContent, getContentsOutput, processNestedSelectors, getTableOutput, getContainedOutput, getChemOutput, handleGroupSelector, handleAllSelector, handleNestedSelector | local loadData, findDataById, formatContent, getContentsOutput, processNestedSelectors, getTableOutput, getContainedOutput, getChemOutput, handleGroupSelector, handleAllSelector, handleNestedSelector | ||
-- Функция для | -- Функция для загрузки данных с кэшированием | ||
local function buildIndex( | loadData = function(filePath) | ||
if not | if not dataCache[filePath] then | ||
local page = mw.title.new(filePath) | |||
local content = page:getContent() | |||
dataCache[filePath] = content and mw.text.jsonDecode(content) or nil | |||
end | |||
return dataCache[filePath] | |||
end | |||
-- Создание хэш-таблицы для быстрого поиска по ID | |||
local function buildIndex(data) | |||
if indexCache[data] then return indexCache[data] end | |||
if not data then return {} end | |||
local index = {} | local index = {} | ||
for _, item in ipairs( | for _, item in ipairs(data) do | ||
index[item.id] = item | index[item.id] = item | ||
end | end | ||
indexCache[data] = index | |||
return index | return index | ||
end | end | ||
-- Поиск данных по ID через | -- Поиск данных по ID через хэш-таблицу | ||
findDataById = function( | findDataById = function(dataIndex, id) | ||
return | return dataIndex and dataIndex[id] or nil | ||
end | end | ||
| Строка 71: | Строка 76: | ||
return string.format( | return string.format( | ||
'{{LinkСard|SideStyle=1|background-color=# | '{{LinkСard|SideStyle=1|background-color=#cbcbff0b|image=%s|name=%s%s%s {{#invoke:Prototypes/Хранилище/Предмет|main|framing|contained|%s}} }}', | ||
image, name, amount, prob, content.id | image, name, amount, prob, content.id | ||
) | ) | ||
| Строка 90: | Строка 95: | ||
local results = {} | local results = {} | ||
local nestedCache = {} | |||
for _, child in ipairs(children) do | for _, child in ipairs(children) do | ||
| Строка 95: | Строка 101: | ||
results[#results + 1] = formatContent(child) | results[#results + 1] = formatContent(child) | ||
elseif child["!type"] == "NestedSelector" then | elseif child["!type"] == "NestedSelector" then | ||
results[#results + 1] = | -- Кэшируем результат вызова 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) | results[#results + 1] = handleGroupSelector(child) | ||
| Строка 106: | Строка 116: | ||
-- Обработка таблиц | -- Обработка таблиц | ||
getTableOutput = function(tableId) | getTableOutput = function(tableId) | ||
local | local allSelectors = loadData('User:IanComradeBot/prototypes/AllSelector.json') | ||
local | local tableData = findDataById(buildIndex(allSelectors), tableId) | ||
if not | if not tableData then return 'Таблица не найдена.' end | ||
if | if tableData['!type:GroupSelector'] then | ||
return handleGroupSelector( | return handleGroupSelector(tableData['!type:GroupSelector']) | ||
elseif | elseif tableData['!type:AllSelector'] then | ||
return processNestedSelectors( | return processNestedSelectors(tableData['!type:AllSelector'].children) | ||
end | end | ||
| Строка 121: | Строка 131: | ||
-- Формирование списка содержащихся предметов или таблиц | -- Формирование списка содержащихся предметов или таблиц | ||
getContainedOutput = function(itemData, id) | getContainedOutput = function(itemData, id, visited) | ||
visited = visited or {} | |||
if visited[id] then | |||
return '' | |||
end | |||
visited[id] = true | |||
local item = findDataById(itemData, id) | local item = findDataById(itemData, id) | ||
if not item then return '' end | if not item then return '' end | ||
| Строка 138: | Строка 156: | ||
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(containers.entity_storage.children) | result[#result + 1] = processNestedSelectors(containers.entity_storage.children, visited) | ||
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, visited) | ||
end | end | ||
end | end | ||
| Строка 148: | Строка 165: | ||
-- Обработка storagebase | -- Обработка storagebase | ||
if containers.storagebase and containers.storagebase.tableId then | if containers.storagebase and containers.storagebase.tableId then | ||
result[#result + 1] = getTableOutput(containers.storagebase.tableId) | result[#result + 1] = getTableOutput(containers.storagebase.tableId, visited) | ||
end | end | ||
end | end | ||
| Строка 229: | Строка 246: | ||
-- Формирование списка химии | -- Формирование списка химии | ||
getChemOutput = function( | getChemOutput = function(data, id) | ||
local item = findDataById( | 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 | ||
| Строка 248: | Строка 265: | ||
if not id then return 'Не указан ID.' end | if not id then return 'Не указан ID.' end | ||
local data = loadData('User:IanComradeBot/prototypes/fills/Item.json') | |||
local | local dataIndex = buildIndex(data) | ||
if not | if not data then return 'Не удалось загрузить данные.' end | ||
if mode == 'framing' then | if mode == 'framing' then | ||
| Строка 263: | Строка 280: | ||
if subMode == 'chem' then | if subMode == 'chem' then | ||
return frame:preprocess('{{СollapsibleMenu|' .. getChemOutput( | return frame:preprocess('{{СollapsibleMenu|' .. getChemOutput(dataIndex, id) .. '}}') | ||
elseif subMode == 'contained' then | elseif subMode == 'contained' then | ||
return frame:preprocess('{{СollapsibleMenu|' .. getContainedOutput( | return frame:preprocess('{{СollapsibleMenu|' .. 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(getChemOutput(dataIndex, id)) | ||
elseif mode == 'contained' then | elseif mode == 'contained' then | ||
return frame:preprocess(getContainedOutput( | return frame:preprocess(getContainedOutput(dataIndex, id)) | ||
elseif mode == 'rolls' then | elseif mode == 'rolls' then | ||
local entity = findDataById( | local entity = findDataById(dataIndex, id) | ||
if not entity then return 'ID не найден в данных.' end | if not entity then return 'ID не найден в данных.' end | ||