Модуль:Песочница/Pok: различия между версиями

Нет описания правки
мНет описания правки
Строка 1: Строка 1:
-- Загрузка данных
local dataModule = mw.loadData("Модуль:Prototypes/Хранилище/Предмет/data")
local itemData = dataModule.Item
local allSelectorData = dataModule.AllSelector
local p = {}
local p = {}
-- Кэш данных
local dataCache = {}
local indexCache = {}


-- Функция processRolls для преобразования диапазона
-- Функция processRolls для преобразования диапазона
Строка 9: Строка 10:
     local result = {}
     local result = {}
     if rolls and rolls.range then
     if rolls and rolls.range then
        -- Если указан range
         local min, max = rolls.range:match("(%d+),%s*(%d+)")
         local min, max = rolls.range:match("(%d+),%s*(%d+)")
         min, max = tonumber(min), tonumber(max)
         min, max = tonumber(min), tonumber(max)
Строка 18: Строка 18:
         end
         end
     elseif rolls and rolls.value then
     elseif rolls and rolls.value then
        -- Если указано value
         result[#result + 1] = string.format('[%d]', rolls.value)
         result[#result + 1] = string.format('[%d]', rolls.value)
     else
     else
Строка 26: Строка 25:
end
end


-- Локальные функции
-- Поиск данных по ID
local loadData, findDataById, formatContent, getContentsOutput, processNestedSelectors, getTableOutput, getContainedOutput, getChemOutput, handleGroupSelector, handleAllSelector, handleNestedSelector
local function findDataById(data, id)
 
-- Функция для загрузки данных с кэшированием
loadData = function(filePath)
    if not dataCache[filePath] then
        local page = mw.title.new(filePath)
        local content = page:getContent()
        dataCache[filePath] = content and mw.text.jsonDecode(content) or nil
    end
    return dataCache[filePath]
end
 
-- Создание хэш-таблицы для быстрого поиска по ID
local function buildIndex(data)
    if indexCache[data] then return indexCache[data] end
    if not data then return {} end
    local index = {}
     for _, item in ipairs(data) do
     for _, item in ipairs(data) do
         index[item.id] = item
         if item.id == id then
            return item
        end
     end
     end
    indexCache[data] = index
     return nil
     return index
end
 
-- Поиск данных по ID через хэш-таблицу
findDataById = function(dataIndex, id)
    return dataIndex and dataIndex[id] or nil
end
end


-- Форматирование содержимого
-- Форматирование содержимого
formatContent = function(content)
local function formatContent(content)
     if not content.id then
     if not content.id then
         return "Ошибка: отсутствует id у элемента."
         return "Ошибка: отсутствует id у элемента."
Строка 64: Строка 43:
     local name = string.format('{{#invoke:Entity Lookup|getname|%s}}', content.id)
     local name = string.format('{{#invoke:Entity Lookup|getname|%s}}', content.id)
     local image = string.format('%s.png', content.id)
     local image = string.format('%s.png', content.id)
local amount = (content.amount and content.amount ~= 1) and string.format(" [%d]", content.amount) or ""
    local amount = (content.amount and content.amount ~= 1) and string.format(" [%d]", content.amount) or ""
     local prob = ""
     local prob = ""


Строка 80: Строка 59:
     )
     )
end
end
-- Локальные функции
local loadData, findDataById, formatContent, getContentsOutput, processNestedSelectors, getTableOutput, getContainedOutput, getChemOutput, handleGroupSelector, handleAllSelector, handleNestedSelector


-- Получение содержимого через таблицу
-- Получение содержимого через таблицу
Строка 258: Строка 240:


     if not id then return 'Не указан ID.' end
     if not id then return 'Не указан ID.' end
    local data = loadData('User:IanComradeBot/prototypes/fills/Item.json')
    local dataIndex = buildIndex(data)
    if not data then return 'Не удалось загрузить данные.' end


     if mode == 'framing' then
     if mode == 'framing' then
Строка 273: Строка 250:


         if subMode == 'chem' then
         if subMode == 'chem' then
             return frame:preprocess('{{СollapsibleMenu|' .. getChemOutput(dataIndex, id) .. '}}')
             return frame:preprocess('{{СollapsibleMenu|' .. getChemOutput(itemData, id) .. '}}')
         elseif subMode == 'contained' then
         elseif subMode == 'contained' then
             return frame:preprocess('{{СollapsibleMenu|' .. getContainedOutput(dataIndex, id) .. '}}')
             return frame:preprocess('{{СollapsibleMenu|' .. getContainedOutput(itemData, id) .. '}}')
         else
         else
             return 'Неизвестный подрежим для framing: ' .. subMode
             return 'Неизвестный подрежим для framing: ' .. subMode
         end
         end
     elseif mode == 'chem' then
     elseif mode == 'chem' then
         return frame:preprocess(getChemOutput(dataIndex, id))
         return frame:preprocess(getChemOutput(itemData, id))
     elseif mode == 'contained' then
     elseif mode == 'contained' then
         return frame:preprocess(getContainedOutput(dataIndex, id))
         return frame:preprocess(getContainedOutput(itemData, id))
     elseif mode == 'rolls' then
     elseif mode == 'rolls' then
         local entity = findDataById(dataIndex, id)
         local entity = findDataById(itemData, id)
         if not entity then return 'ID не найден в данных.' end
         if not entity then return 'ID не найден в данных.' end