Модуль:Песочница/Pok: различия между версиями
Pok (обсуждение | вклад) мНет описания правки |
Pok (обсуждение | вклад) мНет описания правки |
||
| Строка 5: | Строка 5: | ||
local p = {} | local p = {} | ||
-- | -- Кэш данных | ||
local | local dataCache = {} | ||
local indexCache = {} | |||
-- Функция processRolls для преобразования диапазона | -- Функция processRolls для преобразования диапазона | ||
| Строка 34: | Строка 31: | ||
-- Локальные функции | -- Локальные функции | ||
local findDataById, formatContent, getContentsOutput, processNestedSelectors, getTableOutput, getContainedOutput, getChemOutput, handleGroupSelector, handleAllSelector, handleNestedSelector | local loadData, findDataById, formatContent, getContentsOutput, processNestedSelectors, getTableOutput, getContainedOutput, getChemOutput, handleGroupSelector, handleAllSelector, handleNestedSelector | ||
-- Функция для загрузки данных с кэшированием | |||
loadData = function(filePath) | |||
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) | 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 = {} | ||
| Строка 43: | Строка 51: | ||
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) | findDataById = function(dataIndex, id) | ||
return dataIndex and dataIndex[id] or nil | return dataIndex and dataIndex[id] or nil | ||
| Строка 71: | Строка 80: | ||
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: | Строка 99: | ||
local results = {} | 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) | 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) | ||
end | end | ||
end | end | ||
| Строка 127: | Строка 137: | ||
getContainedOutput = function(data, id) | getContainedOutput = function(data, id) | ||
local item = findDataById(data, id) | local item = findDataById(data, id) | ||
if not item then return ' | if not item then return '' end | ||
local result = {} | local result = {} | ||
| Строка 142: | Строка 152: | ||
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) | ||
end | end | ||
| Строка 151: | Строка 158: | ||
result[#result + 1] = getTableOutput(containers.entity_storage.tableId) | result[#result + 1] = getTableOutput(containers.entity_storage.tableId) | ||
end | end | ||
end | end | ||
| Строка 159: | Строка 164: | ||
result[#result + 1] = getTableOutput(containers.storagebase.tableId) | result[#result + 1] = getTableOutput(containers.storagebase.tableId) | ||
end | end | ||
end | end | ||
| Строка 174: | Строка 177: | ||
-- Обработка GroupSelector | -- Обработка GroupSelector | ||
handleGroupSelector = function(groupSelector) | handleGroupSelector = function(groupSelector) | ||
if not groupSelector.children then | if not groupSelector.children then return '' end | ||
local result = {} | local result = {} | ||
local wrapperStart, wrapperEnd = "", "" | local wrapperStart, wrapperEnd = "", "" | ||
| Строка 261: | Строка 262: | ||
if not id then return 'Не указан ID.' end | if not id then return 'Не указан ID.' end | ||
local dataIndex = buildIndex(data) | |||
if not data then return 'Не удалось загрузить данные.' end | if not data then return 'Не удалось загрузить данные.' end | ||