Модуль:Песочница/Pok: различия между версиями
Pok (обсуждение | вклад) мНет описания правки |
Pok (обсуждение | вклад) мНет описания правки |
||
| Строка 1: | Строка 1: | ||
local iterateArray | |||
local findDataById, formatContent, getContentsOutput, processNestedSelectors, getTableOutput, getContainedOutput, getChemOutput, handleGroupSelector, handleAllSelector, handleNestedSelector, processRolls, getStackCount | |||
--------------------------------------------------------------------- | |||
-- Вспомогательная функция для итерации по «массивоподобным» таблицам | -- Вспомогательная функция для итерации по «массивоподобным» таблицам | ||
iterateArray = function(t) | |||
if type(t) ~= "table" then | if type(t) ~= "table" then | ||
return function() return nil end | return function() return nil end | ||
| Строка 34: | Строка 38: | ||
end | end | ||
end | end | ||
--------------------------------------------------------------------- | --------------------------------------------------------------------- | ||
-- Функция | -- Функция поиска данных по ID (поддерживает оба формата таблиц) | ||
findDataById = function(data, id) | findDataById = function(data, id) | ||
if not data then | if not data then | ||
return nil | return nil | ||
end | end | ||
if data[id] then | if data[id] then | ||
return data[id] | return data[id] | ||
end | end | ||
for _, item in iterateArray(data) do | for _, item in iterateArray(data) do | ||
if type(item) == "table" and item.id == id then | if type(item) == "table" and item.id == id then | ||
| Строка 68: | Строка 58: | ||
--------------------------------------------------------------------- | --------------------------------------------------------------------- | ||
-- Форматирование содержимого | -- Форматирование содержимого | ||
formatContent = function(content) | |||
if type(content) == "table" and not content.id then | if type(content) == "table" and not content.id then | ||
return "Ошибка: отсутствует id у элемента." | return "Ошибка: отсутствует id у элемента." | ||
| Строка 103: | Строка 93: | ||
--------------------------------------------------------------------- | --------------------------------------------------------------------- | ||
-- Получение содержимого через таблицу | -- Получение содержимого через таблицу | ||
getContentsOutput = function(contents) | |||
local result = "" | local result = "" | ||
for _, content in iterateArray(contents) do | for _, content in iterateArray(contents) do | ||
| Строка 113: | Строка 103: | ||
--------------------------------------------------------------------- | --------------------------------------------------------------------- | ||
-- Обработка вложенных таблиц | -- Обработка вложенных таблиц | ||
processNestedSelectors = function(children, visited) | |||
visited = visited or {} | |||
if not children then return "" end | if not children then return "" end | ||
| Строка 120: | Строка 110: | ||
for _, child in iterateArray(children) do | for _, child in iterateArray(children) do | ||
if child.id then | if child.id then | ||
if not visited[child.id] then | |||
result = result .. formatContent(child) | |||
end | |||
elseif child["!type"] == "NestedSelector" then | elseif child["!type"] == "NestedSelector" then | ||
result = result .. handleNestedSelector(child, true, visited) | result = result .. handleNestedSelector(child, true, visited) | ||
| Строка 135: | Строка 125: | ||
--------------------------------------------------------------------- | --------------------------------------------------------------------- | ||
-- Обработка таблиц (для table.json) | -- Обработка таблиц (для table.json) | ||
getTableOutput = function(tableId, visited) | |||
visited = visited or {} | visited = visited or {} | ||
| Строка 162: | Строка 152: | ||
--------------------------------------------------------------------- | --------------------------------------------------------------------- | ||
-- Формирование списка содержащихся предметов или таблиц | -- Формирование списка содержащихся предметов или таблиц | ||
getContainedOutput = function(itemData, id, visited) | |||
visited = visited or {} | visited = visited or {} | ||
| Строка 230: | Строка 220: | ||
--------------------------------------------------------------------- | --------------------------------------------------------------------- | ||
-- Обработка AllSelector | -- Обработка AllSelector | ||
handleAllSelector = function(allSelector) | |||
if not allSelector.children then return '' end | if not allSelector.children then return '' end | ||
return processNestedSelectors(allSelector.children) | return processNestedSelectors(allSelector.children) | ||
| Строка 268: | Строка 258: | ||
-- Обработка NestedSelector | -- Обработка NestedSelector | ||
handleNestedSelector = function(nestedSelector, wrapped, visited) | handleNestedSelector = function(nestedSelector, wrapped, visited) | ||
visited = visited or {} | |||
if not nestedSelector.tableId then return '' end | if not nestedSelector.tableId then return '' end | ||
| Строка 301: | Строка 291: | ||
--------------------------------------------------------------------- | --------------------------------------------------------------------- | ||
-- Функция для преобразования диапазона (rolls) | -- Функция для преобразования диапазона (rolls) | ||
processRolls = function(rolls) | |||
local result = "" | local result = "" | ||
if rolls and rolls.range then | if rolls and rolls.range then | ||
| Строка 321: | Строка 311: | ||
--------------------------------------------------------------------- | --------------------------------------------------------------------- | ||
-- Формирование списка химии | -- Формирование списка химии | ||
getChemOutput = function(itemData, id) | |||
local item = findDataById(itemData, id) | local item = findDataById(itemData, id) | ||
if not item | if not item | ||
| Строка 377: | Строка 367: | ||
--------------------------------------------------------------------- | --------------------------------------------------------------------- | ||
-- Функция поиска count в itemStackData | -- Функция поиска count в itemStackData | ||
getStackCount = function(id) | |||
local item = findDataById(itemStackData, id) | local item = findDataById(itemStackData, id) | ||
if item and item.Stack and item.Stack.count then | if item and item.Stack and item.Stack.count then | ||
| Строка 384: | Строка 374: | ||
return nil | return nil | ||
end | end | ||
--------------------------------------------------------------------- | |||
-- Загрузка данных | |||
local itemData = mw.loadData("Модуль:IanComradeBot/prototypes/fills/Item.json/data") | |||
local itemSlotsData = mw.loadData("Модуль:IanComradeBot/prototypes/ItemSlots.json/data") | |||
local itemStackData = mw.loadData("Модуль:IanComradeBot/prototypes/fills/stack.json/data") | |||
local chemData = mw.loadData("Модуль:IanComradeBot/prototypes/fills/chem.json/data") | |||
local chemTranslateData = mw.loadData("Модуль:IanComradeBot/chem prototypes.json/data") | |||
local p = {} | |||
--------------------------------------------------------------------- | --------------------------------------------------------------------- | ||