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

Материал из Space Station 14 Вики
Нет описания правки
мНет описания правки
Строка 62: Строка 62:
                  
                  
                 -- Формирование строки открываемых исследований (unlocks)
                 -- Формирование строки открываемых исследований (unlocks)
                local unlocks = ""
local unlocks = ""
                if tech.recipeUnlocks and #tech.recipeUnlocks > 0 then
if type(tech.recipeUnlocks) == "table" and next(tech.recipeUnlocks) then
                    unlocks = '<ul>'
    unlocks = "<ul>"
                    for _, recipe in ipairs(tech.recipeUnlocks) do
    for _, recipe in ipairs(tech.recipeUnlocks) do
                        if recipe and recipe ~= "" then  
        if type(recipe) == "string" and recipe ~= "" then
                            unlocks = unlocks .. '<li>{{#invoke:Entity Lookup|createimagetooltip|Файл:' .. recipe .. '.png|' .. recipe .. '|Мета=32x32px,link=}} {{#invoke:Entity Lookup|getname|' .. recipe .. '}}</li>'
            unlocks = unlocks .. '<li>{{#invoke:Entity Lookup|createimagetooltip|Файл:'  
                        end
                .. recipe .. '.png|' .. recipe  
                    end
                .. '|Мета=32x32px,link=}} {{#invoke:Entity Lookup|getname|'  
                    unlocks = unlocks .. '</ul>'
                .. recipe .. '}}</li>'
                end
        end
    end
    unlocks = unlocks .. "</ul>"
end
                  
                  
                 local templateArgs = {
                 local templateArgs = {

Версия от 15:50, 4 февраля 2025

Для документации этого модуля может быть создана страница Модуль:Песочница/Pok/doc

-- Загрузка данных
local p = {}

-- Таблица для перевода названий дисциплин
local disciplineMapping = {
    Arsenal = "Арсенал",
    Industrial = "Промышленность",
    Experimental = "Экспериментальное",
    CivilianServices = "Обслуживание персонала"
}

-- Таблица для цветов по уровням
local tierColors = {
    [1] = "#54d554",
    [2] = "#ed9000",
    [3] = "#d72a2a"
}

function p.main(frame)
    local input = frame.args[1] or ""
    local manualIcon = frame.args[2] or ""
    local out = ""
    
    if input == "" then
        return '<div style="color:red;">Не указан идентификатор исследования или дисциплина.</div>'
    end
    
    -- Получение актуальных данных
    local researchData = mw.loadData("Модуль:IanComradeBot/prototypes/research.json/data")
    
    -- Если первый параметр соответствует дисциплине, работаем в режиме категории
    if disciplineMapping[input] then
        local found = false
        for _, research in ipairs(researchData) do
            if research.technology and research.technology.discipline == input then
                found = true
                local tech = research.technology
                local iconPath = tech.icon and tech.icon.sprite or nil
                local tierColor = tierColors[tech.tier] or "#FFFFFF"
                local disciplineName = disciplineMapping[tech.discipline] or "Неизвестная дисциплина"
                
                -- Формирование строки необходимых исследований (prerequisites)
                local prerequisites = ""
                if tech.technologyPrerequisites and #tech.technologyPrerequisites > 0 then
                    prerequisites = '<ul>'
                    for _, prerequisiteId in ipairs(tech.technologyPrerequisites) do
                        if prerequisiteId and prerequisiteId ~= "" then
                            local prerequisiteName = ""
                            for _, r in ipairs(researchData) do
                                if r.technology and r.technology.id == prerequisiteId then
                                    prerequisiteName = r.technology.name
                                    break
                                end
                            end
                            if prerequisiteName ~= "" then
                                prerequisites = prerequisites .. '<li>{{#invoke:Ftl|main|translation|' .. prerequisiteName .. '}}</li>'
                            end
                        end
                    end
                    prerequisites = prerequisites .. '</ul>'
                end
                
                -- Формирование строки открываемых исследований (unlocks)
				local unlocks = ""
				if type(tech.recipeUnlocks) == "table" and next(tech.recipeUnlocks) then
				    unlocks = "<ul>"
				    for _, recipe in ipairs(tech.recipeUnlocks) do
				        if type(recipe) == "string" and recipe ~= "" then
				            unlocks = unlocks .. '<li>{{#invoke:Entity Lookup|createimagetooltip|Файл:' 
				                .. recipe .. '.png|' .. recipe 
				                .. '|Мета=32x32px,link=}} {{#invoke:Entity Lookup|getname|' 
				                .. recipe .. '}}</li>'
				        end
				    end
				    unlocks = unlocks .. "</ul>"
				end
                
                local templateArgs = {
                    id = tech.id,
                    icon = iconPath,
                    name = tech.name,
                    discipline = tech.discipline,
                    tier = tech.tier,
                    tierColor = tierColor,
                    disciplineName = disciplineName,
                    cost = tech.cost,
                    unlocks = unlocks
                }
                if prerequisites ~= "" then
                    templateArgs.prerequisites = prerequisites
                end
                
                out = out .. frame:expandTemplate({
                    title = 'Prototypes/Механика/Исследование',
                    args = templateArgs
                })
            end
        end
        if not found then
            out = out .. '<div style="color:red;">Не найдено исследований для дисциплины "' .. input .. '".</div>'
        end
        return mw.getCurrentFrame():preprocess(out)
    else
        -- Режим поиска по ID исследования
        local tech = nil
        for _, research in ipairs(researchData) do
            if research.technology and research.technology.id == input then
                tech = research.technology
                break
            end
        end
        
        if not tech then
            out = out .. '<div style="color:red;">Исследование с ID "' .. input .. '" не найдено.</div>'
        else
            local tierColor = tierColors[tech.tier] or "#FFFFFF"
            local disciplineName = disciplineMapping[tech.discipline] or "Неизвестная дисциплина"
            local iconPath = manualIcon ~= "" and manualIcon or (tech.icon and tech.icon.sprite or nil)
            
            local prerequisites = ""
            if tech.technologyPrerequisites and #tech.technologyPrerequisites > 0 then
                prerequisites = '<ul>'
                for _, prerequisiteId in ipairs(tech.technologyPrerequisites) do
                    if prerequisiteId and prerequisiteId ~= "" then 
                        local prerequisiteName = ""
                        for _, research in ipairs(researchData) do
                            if research.technology and research.technology.id == prerequisiteId then
                                prerequisiteName = research.technology.name
                                break
                            end
                        end
                        if prerequisiteName ~= "" then
                            prerequisites = prerequisites .. '<li>{{#invoke:Ftl|main|translation|' .. prerequisiteName .. '}}</li>'
                        end
                    end
                end
                prerequisites = prerequisites .. '</ul>'
            end
            
            local unlocks = ""
            if tech.recipeUnlocks and #tech.recipeUnlocks > 0 then
                unlocks = '<ul>'
                for _, recipe in ipairs(tech.recipeUnlocks) do
                    if recipe and recipe ~= "" then 
                        unlocks = unlocks .. '<li>{{#invoke:Entity Lookup|createimagetooltip|Файл:' .. recipe .. '.png|' .. recipe .. '|Мета=32x32px,link=}} {{#invoke:Entity Lookup|getname|' .. recipe .. '}}</li>'
                    end
                end
                unlocks = unlocks .. '</ul>'
            end
            
            local templateArgs = {
                id = tech.id,
                icon = iconPath,
                name = tech.name,
                discipline = tech.discipline,
                tier = tech.tier,
                tierColor = tierColor,
                disciplineName = disciplineName, 
                cost = tech.cost,
                unlocks = unlocks
            }
            if prerequisites ~= "" then
                templateArgs.prerequisites = prerequisites
            end
            
            out = out .. frame:expandTemplate({
                title = 'Prototypes/Механика/Исследование',
                args = templateArgs
            })
        end
        return mw.getCurrentFrame():preprocess(out)
    end
end

return p