Модуль:Prototypes/Хранилище/Предмет: различия между версиями
Pok (обсуждение | вклад) мНет описания правки |
Pok (обсуждение | вклад) Нет описания правки |
||
| Строка 1: | Строка 1: | ||
-- Загрузка данных | |||
local data = mw.loadData("Модуль:IanComradeBot/prototypes/fills/Item.json/data") | |||
local allSelectorData = 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(data) | local function buildIndex(data) | ||
if not data then return {} end | if not data then return {} end | ||
local index = {} | local index = {} | ||
| Строка 47: | Строка 43: | ||
index[item.id] = item | index[item.id] = item | ||
end | end | ||
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 | ||
| Строка 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 allSelectors = loadData('User:IanComradeBot/prototypes/ | local allSelectors = loadData('User:IanComradeBot/prototypes/table.json') | ||
local tableData = findDataById(buildIndex(allSelectors), tableId) | local tableData = findDataById(buildIndex(allSelectors), tableId) | ||
| Строка 258: | Строка 248: | ||
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 | ||