Модуль:Песочница/Pok: различия между версиями
Pok (обсуждение | вклад) Нет описания правки |
Pok (обсуждение | вклад) Нет описания правки |
||
| Строка 1: | Строка 1: | ||
-- Загрузка данных | |||
-- local itemData = mw.loadData("Модуль:IanComradeBot/prototypes/fills/Item.json/data") | |||
-- local tableData = mw.loadData("Модуль:IanComradeBot/prototypes/table.json/data") | |||
local p = {} | local p = {} | ||
-- | -- Функция для загрузки данных | ||
local | local loadData = function(filePath) | ||
local | local page = mw.title.new(filePath) | ||
local content = page:getContent() | |||
return content and mw.text.jsonDecode(content) or nil | |||
end | |||
-- Функция processRolls для преобразования диапазона | -- Функция processRolls для преобразования диапазона | ||
| Строка 27: | Строка 34: | ||
-- Локальные функции | -- Локальные функции | ||
local | local findDataById, formatContent, getContentsOutput, processNestedSelectors, getTableOutput, getContainedOutput, getChemOutput, handleGroupSelector, handleAllSelector, handleNestedSelector | ||
-- Функция для | -- Функция для создания хэш-таблицы для быстрого поиска по ID | ||
local function buildIndex(itemData) | |||
if not itemData then return {} end | |||
local function buildIndex( | |||
if not | |||
local index = {} | local index = {} | ||
for _, item in ipairs( | for _, item in ipairs(itemData) do | ||
index[item.id] = item | index[item.id] = item | ||
end | end | ||
return index | return index | ||
end | end | ||
-- Поиск данных по ID через | -- Поиск данных по ID через индекс | ||
findDataById = function( | findDataById = function(itemDataIndex, id) | ||
return | return itemDataIndex and itemDataIndex[id] or nil | ||
end | end | ||
| Строка 76: | Строка 71: | ||
return string.format( | return string.format( | ||
'{{LinkСard|SideStyle=1|background-color=# | '{{LinkСard|SideStyle=1|background-color=#cbcbff0a|image=%s|name=%s%s%s {{#invoke:Prototypes/Хранилище/Предмет|main|framing|contained|%s}} }}', | ||
image, name, amount, prob, content.id | image, name, amount, prob, content.id | ||
) | ) | ||
| Строка 95: | Строка 90: | ||
local results = {} | local results = {} | ||
for _, child in ipairs(children) do | for _, child in ipairs(children) do | ||
| Строка 101: | Строка 95: | ||
results[#results + 1] = formatContent(child) | results[#results + 1] = formatContent(child) | ||
elseif child["!type"] == "NestedSelector" then | elseif child["!type"] == "NestedSelector" then | ||
results[#results + 1] = handleNestedSelector(child, true) | |||
elseif child["!type"] == "GroupSelector" then | elseif child["!type"] == "GroupSelector" then | ||
results[#results + 1] = handleGroupSelector(child) | results[#results + 1] = handleGroupSelector(child) | ||
| Строка 116: | Строка 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( | return handleGroupSelector(tableDataIndex['!type:GroupSelector']) | ||
elseif | elseif tableDataIndex['!type:AllSelector'] then | ||
return processNestedSelectors( | return processNestedSelectors(tableDataIndex['!type:AllSelector'].children) | ||
end | end | ||
| Строка 246: | Строка 236: | ||
-- Формирование списка химии | -- Формирование списка химии | ||
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 | ||
| Строка 265: | Строка 255: | ||
if not id then return 'Не указан ID.' end | if not id then return 'Не указан ID.' end | ||
local itemData = loadData('User:IanComradeBot/prototypes/fills/Item.json') | |||
local | local itemDataIndex = buildIndex(itemData) | ||
if not | if not itemData then return 'Не удалось загрузить данные.' end | ||
if mode == 'framing' then | if mode == 'framing' then | ||
| Строка 280: | Строка 270: | ||
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 | ||