Модуль:Prototypes/Хранилище/Предмет: различия между версиями
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 | ||
| Строка 86: | Строка 86: | ||
-- Обработка вложенных таблиц | -- Обработка вложенных таблиц | ||
processNestedSelectors = function(children) | processNestedSelectors = function(children, visited) | ||
visited = visited or {} | |||
if not children or #children == 0 then return "" end | if not children or #children == 0 then return "" end | ||
| Строка 93: | Строка 94: | ||
for _, child in ipairs(children) do | for _, child in ipairs(children) do | ||
if child.id then | if child.id then | ||
if not visited[child.id] then | |||
results[#results + 1] = formatContent(child) | |||
end | |||
elseif child["!type"] == "NestedSelector" then | elseif child["!type"] == "NestedSelector" then | ||
results[#results + 1] = handleNestedSelector(child, true) | results[#results + 1] = handleNestedSelector(child, true, visited) | ||
elseif child["!type"] == "GroupSelector" then | elseif child["!type"] == "GroupSelector" then | ||
results[#results + 1] = handleGroupSelector(child) | results[#results + 1] = handleGroupSelector(child, visited) | ||
end | end | ||
end | end | ||
| Строка 105: | Строка 108: | ||
-- Обработка таблиц | -- Обработка таблиц | ||
getTableOutput = function(tableId) | getTableOutput = function(tableId, visited) | ||
visited = visited or {} | |||
if not | if visited[tableId] then | ||
return '' | |||
end | |||
visited[tableId] = true | |||
local tableData = loadData('User:IanComradeBot/prototypes/table.json') | |||
local tableDataIndex = findDataById(buildIndex(tableData), tableId) | |||
if not tableDataIndex then return 'Таблица не найдена.' end | |||
if | if tableDataIndex['!type:GroupSelector'] then | ||
return handleGroupSelector( | return handleGroupSelector(tableDataIndex['!type:GroupSelector'], visited) | ||
elseif | elseif tableDataIndex['!type:AllSelector'] then | ||
return processNestedSelectors( | return processNestedSelectors(tableDataIndex['!type:AllSelector'].children, visited) | ||
end | end | ||
| Строка 121: | Строка 132: | ||
-- Формирование списка содержащихся предметов или таблиц | -- Формирование списка содержащихся предметов или таблиц | ||
getContainedOutput = function( | getContainedOutput = function(itemData, id, visited) | ||
local item = findDataById( | visited = visited or {} | ||
if visited[id] then | |||
return '' | |||
end | |||
visited[id] = true | |||
local item = findDataById(itemData, id) | |||
if not item then return '' end | if not item then return '' end | ||
| Строка 138: | Строка 157: | ||
if containers.entity_storage then | if containers.entity_storage then | ||
if containers.entity_storage.children then | if containers.entity_storage.children then | ||
result[#result + 1] = processNestedSelectors(containers.entity_storage.children) | result[#result + 1] = processNestedSelectors(containers.entity_storage.children, visited) | ||
end | end | ||
if containers.entity_storage.tableId then | if containers.entity_storage.tableId then | ||
result[#result + 1] = getTableOutput(containers.entity_storage.tableId) | result[#result + 1] = getTableOutput(containers.entity_storage.tableId, visited) | ||
end | end | ||
end | end | ||
| Строка 148: | Строка 166: | ||
-- Обработка storagebase | -- Обработка storagebase | ||
if containers.storagebase and containers.storagebase.tableId then | if containers.storagebase and containers.storagebase.tableId then | ||
result[#result + 1] = getTableOutput(containers.storagebase.tableId) | result[#result + 1] = getTableOutput(containers.storagebase.tableId, visited) | ||
end | end | ||
end | end | ||
| Строка 192: | Строка 210: | ||
-- Обработка NestedSelector | -- Обработка NestedSelector | ||
handleNestedSelector = function(nestedSelector, wrapped) | handleNestedSelector = function(nestedSelector, wrapped, visited) | ||
visited = visited or {} | |||
if not nestedSelector.tableId then return '' end | if not nestedSelector.tableId then return '' end | ||
local result = {} | local result = {} | ||
local classesRolls, classesProb | local classesRolls, classesProb | ||
if wrapped then | if wrapped then | ||
if nestedSelector.rolls and nestedSelector.rolls.range then | |||
local rollsResult = processRolls(nestedSelector.rolls) | |||
if rollsResult and #rollsResult > 0 then | |||
classesRolls = ', максимум может выпасть: ' .. rollsResult | |||
end | |||
end | |||
if nestedSelector.prob then | if nestedSelector.prob then | ||
classesProb = string.format(" <div>%s%%</div>", nestedSelector.prob * 100 >= 1 and math.floor(nestedSelector.prob * 100) or nestedSelector.prob * 100) | classesProb = string.format(" <div>%s%%</div>", nestedSelector.prob * 100 >= 1 and math.floor(nestedSelector.prob * 100) or nestedSelector.prob * 100) | ||
| Строка 211: | Строка 229: | ||
end | end | ||
if wrapped and (classesRolls or classesProb) then | |||
result[#result + 1] = string.format('{{LinkСard/Сollapsible|name=Группа предметов%s%s|content=', classesRolls or "", classesProb or "") | |||
end | |||
result[#result + 1] = getTableOutput(nestedSelector.tableId) | result[#result + 1] = getTableOutput(nestedSelector.tableId, visited) | ||
if wrapped and (classesRolls or classesProb) then | |||
result[#result + 1] = "}}" | result[#result + 1] = "}}" | ||
end | |||
return table.concat(result) | return table.concat(result) | ||
| Строка 229: | Строка 243: | ||
-- Формирование списка химии | -- Формирование списка химии | ||
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: | Строка 263: | ||
if not id then return 'Не указан ID.' end | if not id then return 'Не указан ID.' end | ||
local | local itemData = loadData('User:IanComradeBot/prototypes/fills/Item.json') | ||
local itemDataIndex = buildIndex(itemData) | |||
if not | |||
if not itemData then return 'Не удалось загрузить данные.' end | |||
if mode == 'framing' then | if mode == 'framing' then | ||
| Строка 262: | Строка 277: | ||
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 | ||