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

мНет описания правки
Нет описания правки
Строка 3: Строка 3:
-- Функция для загрузки данных исследований из JSON-файла
-- Функция для загрузки данных исследований из JSON-файла
local function loadResearchData()
local function loadResearchData()
     local jsonContent = mw.title.new("User:IanComradeBot/Песочница.json"):getContent()
     return mw.text.jsonDecode(mw.title.new("User:IanComradeBot/Песочница.json"):getContent())
    if not jsonContent or jsonContent == "" then
        error("Не удалось загрузить содержимое JSON-файла.")
    end
    local success, data = pcall(mw.text.jsonDecode, jsonContent)
    if not success then
        error("Ошибка декодирования JSON: " .. tostring(data))
    end
    return data
end
 
-- Функция для поиска исследований по дисциплине
local function findResearchByDiscipline(dataCache, discipline)
    local results = {}
    for _, research in ipairs(dataCache) do
        if research.technology and research.technology.discipline == discipline then
            table.insert(results, research.technology)
        end
    end
    return results
end
end


Строка 40: Строка 21:
}
}


-- Функция для обработки вывода
function p.main(frame)
function p.main(frame)
local tierColor = tierColors[tech.tier] or "#FFFFFF"
local disciplineName = disciplineMapping[tech.discipline] or "Неизвестная дисциплина"
     -- Подключение CSS
     -- Подключение CSS
     local cssLink = frame:extensionTag('templatestyles', '', {
     local cssLink = frame:extensionTag('templatestyles', '', {
Строка 47: Строка 30:
     })
     })


    -- Загрузка данных из JSON
     local dataCache = loadResearchData()
     local dataCache = loadResearchData()
    local discipline = frame.args[1] or ""


     if discipline and discipline ~= "" then
    -- Получаем ID и иконку из параметров
        -- Инициализация строки вывода
    local researchId = frame.args[1] or ""
    local icon = frame.args[2] or ""
 
     if researchId and researchId ~= "" then
         local out = cssLink .. '<div class="research-group">'
         local out = cssLink .. '<div class="research-group">'


         -- Получаем список исследований по дисциплине
         -- Поиск исследования по ID
         local researches = findResearchByDiscipline(dataCache, discipline)
         local tech = nil
 
        for _, research in ipairs(dataCache) do
        if #researches == 0 then
            if research.technology and research.technology.id == researchId then
            out = out .. '<div style="color:red;">Нет исследований для дисциплины "' .. discipline .. '"</div>'
                tech = research.technology
                break
            end
         end
         end


         -- Перебор всех исследований и формирование их вывода
         if not tech then
        for _, tech in ipairs(researches) do
             out = out .. '<div style="color:red;">Исследование с ID "' .. researchId .. '" не найдено.</div>'
             local disciplineName = disciplineMapping[tech.discipline] or "Неизвестная дисциплина"
        else
             local tierColor = tierColors[tech.tier] or "#FFFFFF"
             local tierColor = tierColors[tech.tier] or "#FFFFFF"
             local iconPath = tech.icon and tech.icon.sprite or ""
             local iconPath = icon ~= "" and icon or (tech.icon and tech.icon.sprite or nil)


             -- Формирование строки prerequisites
             -- Формирование строки prerequisites
Строка 73: Строка 59:
                 prerequisitesStr = '<ul>'
                 prerequisitesStr = '<ul>'
                 for _, prerequisite in ipairs(tech.technologyPrerequisites) do
                 for _, prerequisite in ipairs(tech.technologyPrerequisites) do
                     if prerequisite and prerequisite ~= "" then
                     if prerequisite and prerequisite ~= "" then  
                         prerequisitesStr = prerequisitesStr .. '<li>' .. mw.text.encode(prerequisite) .. '</li>'
                         prerequisitesStr = prerequisitesStr .. '<li>{{#invoke:Entity Lookup|createimagetooltip|Файл:'  
                            .. prerequisite .. '.png|' .. prerequisite
                            .. '|Мета=32x32px,link=}}'
                            .. prerequisite .. '</li>'
                     end
                     end
                 end
                 end
Строка 85: Строка 74:
                 unlocksStr = '<ul>'
                 unlocksStr = '<ul>'
                 for _, recipe in ipairs(tech.recipeUnlocks) do
                 for _, recipe in ipairs(tech.recipeUnlocks) do
                     if recipe and recipe ~= "" then
                     if recipe and recipe ~= "" then  
                         unlocksStr = mw.getCurrentFrame():preprocess('<li style="display:none;">' .. mw.text.encode(recipe) .. '</li>'.. '<li>{{#invoke:Entity Lookup|createimagetooltip|Файл:'  
                         unlocksStr = unlocksStr .. '<li>{{#invoke:Entity Lookup|createimagetooltip|Файл:'  
                    .. mw.text.encode(recipe) .. '.png|' .. mw.text.encode(recipe)
                            .. recipe .. '.png|' .. recipe  
                    .. '|Мета=32x32px,link=}} {{#invoke:Entity Lookup|getname|'  
                            .. '|Мета=32x32px,link=}} {{#invoke:Entity Lookup|getname|'  
                    .. mw.text.encode(recipe) .. '}}</li>')
                            .. recipe .. '}}</li>'
                     end
                     end
                 end
                 end
Строка 95: Строка 84:
             end
             end


             -- Формируем строку с использованием шаблона
             -- Шаблон для отображения блока исследования
             local templateCall = '{{Prototypes/Механика/Исследование' ..
             out = out .. frame:expandTemplate({
                 '|id=' .. (tech.id or "") ..
                title = 'Prototypes/Механика/Исследование',
                '|icon=' .. (iconPath or "") ..
                 args = {
                '|name=' .. (tech.name or "") ..
                    id = tech.id,
                '|discipline=' .. (tech.discipline or "") ..
                    icon = iconPath,
                '|tier=' .. (tech.tier or 0) ..
                    name = tech.name,
                '|tierColor=' .. (tierColor or "#FFFFFF") ..
                    discipline = tech.discipline,
                '|disciplineName=' .. (disciplineName or "") ..
                    tier = tech.tier,
                '|cost=' .. (tech.cost or 0) ..
                    tierColor = tierColor,
                '|prerequisites=' .. (prerequisitesStr or "") ..
                    disciplineName = tech.discipline,
                '|unlocks=' .. (unlocksStr or "") ..
                    cost = tech.cost,
                 '}}'
                    prerequisites = mw.getCurrentFrame():preprocess(prerequisitesStr),
                    unlocks = mw.getCurrentFrame():preprocess(unlocksStr)
                 }
            })
        end


            -- Печатаем шаблон в вывод
         return out
            out = out .. templateCall
        end
       
        out = out .. '</div>'
         return mw.getCurrentFrame():preprocess(out)
     else
     else
         return cssLink .. '<div style="color:red;">Дисциплина "' .. discipline .. '" не найдена.</div>'
         return cssLink .. '<div style="color:red;">Не указан ID исследования.</div>'
     end
     end
end
end


return p
return p