Модуль:Песочница/Pok: различия между версиями
Pok (обсуждение | вклад) Нет описания правки Метка: ручная отмена |
Pok (обсуждение | вклад) Нет описания правки |
||
| Строка 1: | Строка 1: | ||
-- Загрузка данных | -- Загрузка данных | ||
local | local itemData = mw.loadData("Модуль:IanComradeBot/prototypes/fills/Item.json/data") | ||
local | local tableData = mw.loadData("Модуль:IanComradeBot/prototypes/table.json/data") | ||
local p = {} | local p = {} | ||
| Строка 37: | Строка 37: | ||
-- Функция для создания хэш-таблицы для быстрого поиска по ID | -- Функция для создания хэш-таблицы для быстрого поиска по ID | ||
local function buildIndex( | local function buildIndex(itemData) | ||
if not | if not itemData then return {} end | ||
local index = {} | local index = {} | ||
for _, item in ipairs( | for _, item in ipairs(itemData) do | ||
index[item.id] = item | index[item.id] = item | ||
end | end | ||
| Строка 47: | Строка 47: | ||
-- Поиск данных по ID через индекс | -- Поиск данных по ID через индекс | ||
findDataById = function( | findDataById = function(itemDataIndex, id) | ||
return | return itemDataIndex and itemDataIndex[id] or nil | ||
end | end | ||
| Строка 106: | Строка 106: | ||
-- Обработка таблиц | -- Обработка таблиц | ||
getTableOutput = function(tableId) | getTableOutput = function(tableId) | ||
local | local tableData = loadData('User:IanComradeBot/prototypes/table.json') | ||
local | local tableDataIndex = findDataById(buildIndex(tableData), tableId) | ||
if not | if not tableDataIndex then return 'Таблица не найдена.' end | ||
if | if tableDataIndex['!type:GroupSelector'] then | ||
return handleGroupSelector(tableData['!type:GroupSelector']) | return handleGroupSelector(tableData['!type:GroupSelector']) | ||
elseif | elseif tableDataIndex['!type:AllSelector'] then | ||
return processNestedSelectors(tableData['!type:AllSelector'].children) | return processNestedSelectors(tableData['!type:AllSelector'].children) | ||
end | end | ||
| Строка 121: | Строка 121: | ||
-- Формирование списка содержащихся предметов или таблиц | -- Формирование списка содержащихся предметов или таблиц | ||
getContainedOutput = function( | getContainedOutput = function(itemData, id) | ||
local item = findDataById( | local item = findDataById(itemData, id) | ||
if not item then return '' end | if not item then return '' end | ||
| Строка 229: | Строка 229: | ||
-- Формирование списка химии | -- Формирование списка химии | ||
getChemOutput = function( | getChemOutput = function(itemData, id) | ||
local item = findDataById( | local item = findDataById(itemData, 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 | ||
| Строка 249: | Строка 249: | ||
if not id then return 'Не указан ID.' end | if not id then return 'Не указан ID.' end | ||
local | local itemDataIndex = buildIndex(itemData) | ||
if not | if not itemData then return 'Не удалось загрузить данные.' end | ||
if mode == 'framing' then | if mode == 'framing' then | ||
| Строка 262: | Строка 262: | ||
if subMode == 'chem' then | if subMode == 'chem' then | ||
return frame:preprocess('{{СollapsibleMenu|' .. getChemOutput( | return frame:preprocess('{{СollapsibleMenu|' .. getChemOutput(itemDataIndex, id) .. '}}') | ||
elseif subMode == 'contained' then | elseif subMode == 'contained' then | ||
return frame:preprocess('{{СollapsibleMenu|' .. getContainedOutput( | return frame:preprocess('{{СollapsibleMenu|' .. getContainedOutput(itemDataIndex, 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(itemDataIndex, id)) | ||
elseif mode == 'contained' then | elseif mode == 'contained' then | ||
return frame:preprocess(getContainedOutput( | return frame:preprocess(getContainedOutput(itemDataIndex, id)) | ||
elseif mode == 'rolls' then | elseif mode == 'rolls' then | ||
local entity = findDataById( | local entity = findDataById(itemDataIndex, id) | ||
if not entity then return 'ID не найден в данных.' end | if not entity then return 'ID не найден в данных.' end | ||