Модуль:Песочница/Pok: различия между версиями
Pok (обсуждение | вклад) мНет описания правки |
Pok (обсуждение | вклад) Нет описания правки |
||
| Строка 1: | Строка 1: | ||
local p = {} | local p = {} | ||
-- Кэш данных | |||
local dataCache = {} | |||
local indexCache = {} | |||
-- Функция processRolls для преобразования диапазона | -- Функция processRolls для преобразования диапазона | ||
| Строка 5: | Строка 9: | ||
local result = {} | local result = {} | ||
if rolls and rolls.range then | if rolls and rolls.range then | ||
-- Если указан range | |||
local min, max = rolls.range:match("(%d+),%s*(%d+)") | local min, max = rolls.range:match("(%d+),%s*(%d+)") | ||
min, max = tonumber(min), tonumber(max) | min, max = tonumber(min), tonumber(max) | ||
| Строка 13: | Строка 18: | ||
end | end | ||
elseif rolls and rolls.value then | elseif rolls and rolls.value then | ||
-- Если указано value | |||
result[#result + 1] = string.format('[%d]', rolls.value) | result[#result + 1] = string.format('[%d]', rolls.value) | ||
else | else | ||
| Строка 20: | Строка 26: | ||
end | end | ||
-- | -- Локальные функции | ||
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) | |||
if indexCache[data] then return indexCache[data] end | |||
if not data then return {} end | |||
local index = {} | |||
for _, item in ipairs(data) do | |||
index[item.id] = item | |||
end | |||
indexCache[data] = index | |||
return index | |||
end | end | ||
-- Поиск данных по ID | -- Поиск данных по ID через хэш-таблицу | ||
findDataById = function(dataIndex, id) | |||
return | return dataIndex and dataIndex[id] or nil | ||
end | end | ||
-- Форматирование содержимого | -- Форматирование содержимого | ||
formatContent = function(content) | |||
if not content.id then | if not content.id then | ||
return "Ошибка: отсутствует id у элемента." | return "Ошибка: отсутствует id у элемента." | ||
| Строка 38: | Строка 64: | ||
local name = string.format('{{#invoke:Entity Lookup|getname|%s}}', content.id) | local name = string.format('{{#invoke:Entity Lookup|getname|%s}}', content.id) | ||
local image = string.format('%s.png', content.id) | local image = string.format('%s.png', content.id) | ||
local amount = (content.amount and content.amount ~= 1) and string.format(" [%d]", content.amount) or "" | |||
local prob = "" | local prob = "" | ||