Модуль:Prototypes/Машина/Станок: различия между версиями

Материал из Space Station 14 Вики
мНет описания правки
Метка: ручная отмена
Нет описания правки
Строка 38: Строка 38:
local function sortRecipesByPriority(recipes)
local function sortRecipesByPriority(recipes)
     table.sort(recipes, function(a, b)
     table.sort(recipes, function(a, b)
        -- Приоритеты дисциплин
         local priority = { Static = 1, Unknown = 2, EMAG = 4 }
         local priority = { Static = 1, Unknown = 2, EMAG = 4 }
         local aPriority = priority[a.discipline] or 3
         local aPriority = priority[a.discipline] or 3
         local bPriority = priority[b.discipline] or 3
         local bPriority = priority[b.discipline] or 3


        -- EMAG рецепты всегда в конце
        if a.isEmag ~= b.isEmag then
            return not a.isEmag
        end
        -- Если приоритеты совпадают
         if aPriority == bPriority then
         if aPriority == bPriority then
             if a.discipline == b.discipline then
            -- Сравнение по уровню исследования
                 return a.tier < b.tier
             if a.tier == b.tier then
                -- Если уровни совпадают, сортируем по discipline
                 return a.discipline < b.discipline
             end
             end
             return a.discipline < b.discipline
             return a.tier < b.tier
         end
         end
        -- Сортируем по приоритету discipline
         return aPriority < bPriority
         return aPriority < bPriority
     end)
     end)
Строка 149: Строка 160:
     end
     end


    -- Обработка emagStaticRecipes  
-- Обработка emagStaticRecipes
    if lathe.EmagLatheRecipes and lathe.EmagLatheRecipes.emagStaticRecipes then
if lathe.EmagLatheRecipes and lathe.EmagLatheRecipes.emagStaticRecipes then
        for _, recipeId in ipairs(lathe.EmagLatheRecipes.emagStaticRecipes) do
    for _, recipeId in ipairs(lathe.EmagLatheRecipes.emagStaticRecipes) do
            local recipe = getRecipeDetails(recipeId)
        local recipe = getRecipeDetails(recipeId)
            if recipe then
        if recipe then
                table.insert(recipes, {
            table.insert(recipes, {
                    result = recipe.result,
                result = recipe.result,
                    completetime = recipe.completetime,
                completetime = recipe.completetime,
                    materials = recipe.materials,
                materials = recipe.materials,
                    discipline = "EMAG",  
                discipline = "Static",
                    tier = 0
                tier = 0,
                })
                isEmag = true
            end
            })
        end
        end
    end
    end
 
end
    -- Обработка emagDynamicRecipes
    if lathe.EmagLatheRecipes and lathe.EmagLatheRecipes.emagDynamicRecipes then
        for _, recipeId in ipairs(lathe.EmagLatheRecipes.emagDynamicRecipes) do
            local recipe = getRecipeDetails(recipeId)
            if recipe then
                local researchInfo = findInResearch(recipeId)
                if researchInfo then
                    table.insert(recipes, {
                        result = recipe.result,
                        completetime = recipe.completetime,
                        materials = recipe.materials,
                        discipline = "EMAG", 
                        tier = 0,
                        researchName = researchInfo.name
                    })
                else
                    table.insert(recipes, {
                        result = recipe.result,
                        completetime = recipe.completetime,
                        materials = recipe.materials,
                        discipline = "EMAG",
                        tier = 0
                    })
                end
            end
        end
    end
 
    -- Сортировка рецептов
    sortRecipesByPriority(recipes)


    -- Вывод рецептов
-- Обработка emagDynamicRecipes
    for _, recipe in ipairs(recipes) do
if lathe.EmagLatheRecipes and lathe.EmagLatheRecipes.emagDynamicRecipes then
        output = output .. '<li>' .. recipe.result
    for _, recipeId in ipairs(lathe.EmagLatheRecipes.emagDynamicRecipes) do
        output = output .. ' (Время изготовления: ' .. format_seconds_to_short_string(recipe.completetime) .. ')'
        local recipe = getRecipeDetails(recipeId)
        output = output .. '<ul>'
        if recipe then
        for material, amount in pairs(recipe.materials) do
            local researchInfo = findInResearch(recipeId)
            output = output .. '<li>' .. material .. ': ' .. amount .. '</li>'
            if researchInfo then
        end
                table.insert(recipes, {
        output = output .. '</ul>'
                    result = recipe.result,
        if recipe.discipline ~= "Static" and recipe.discipline ~= "Unknown" and recipe.discipline ~= "EMAG" then
                    completetime = recipe.completetime,
            output = output .. '<div>Исследование: ' .. recipe.discipline .. ' - ' .. recipe.researchName
                    materials = recipe.materials,
            output = output .. ' (Уровень: ' .. recipe.tier .. ')</div>'
                    discipline = researchInfo.discipline, -- Discipline из исследования
        elseif recipe.discipline == "EMAG" then
                    tier = researchInfo.tier, -- Уровень из исследования
            output = output .. '<div>Пометка: EMAG</div>'
                    researchName = researchInfo.name,
        end
                    isEmag = true -- Флаг для пометки EMAG
        output = output .. '</li>'
                })
    end
            else
                table.insert(recipes, {
                    result = recipe.result,
                    completetime = recipe.completetime,
                    materials = recipe.materials,
                    discipline = "Unknown", -- Если нет информации об исследовании
                    tier = 0,
                    isEmag = true -- Флаг для пометки EMAG
                })
            end
        end
    end
end
-- Вывод рецептов
for _, recipe in ipairs(recipes) do
    output = output .. '<li>' .. recipe.result
    output = output .. ' (Время изготовления: ' .. format_seconds_to_short_string(recipe.completetime) .. ')'
    output = output .. '<ul>'
    for material, amount in pairs(recipe.materials) do
        output = output .. '<li>' .. material .. ': ' .. amount .. '</li>'
    end
    output = output .. '</ul>'
   
    -- Информация об исследовании
    if recipe.discipline ~= "Static" and recipe.discipline ~= "Unknown" then
        output = output .. '<div>Исследование: ' .. recipe.discipline .. ' - ' .. recipe.researchName
        output = output .. ' (Уровень: ' .. recipe.tier .. ')</div>'
    end
   
    -- Пометка EMAG
    if recipe.isEmag then
        output = output .. '<div>Пометка: EMAG</div>'
    end
   
    output = output .. '</li>'
end


     output = output .. '</ul>'
     output = output .. '</ul>'

Версия от 03:51, 28 января 2025

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

local p = {}

-- Функция для загрузки данных станков
local function loadLatheData()
    return mw.text.jsonDecode(mw.title.new("User:IanComradeBot/prototypes/lathe.json"):getContent())
end

-- Функция для загрузки данных рецептов
local function loadRecipeData()
    return mw.text.jsonDecode(mw.title.new("User:IanComradeBot/prototypes/lathe/recipes.json"):getContent())
end

-- Функция для загрузки данных исследований
local function loadResearchData()
    return mw.text.jsonDecode(mw.title.new("User:IanComradeBot/prototypes/research.json"):getContent())
end

-- Функция для форматирования времени
local function format_seconds_to_short_string(input_seconds)
    local minutes = math.floor(input_seconds / 60)
    local seconds = input_seconds % 60

    local minutes_part = minutes > 0 and (minutes .. " мин.") or nil
    local seconds_part = seconds > 0 and (seconds .. " сек.") or nil

    if minutes_part and seconds_part then
        return minutes_part .. " " .. seconds_part
    elseif seconds_part then
        return seconds_part
    elseif minutes_part then
        return minutes_part
    else
        return ''
    end
end

-- Функция для сортировки рецептов
local function sortRecipesByPriority(recipes)
    table.sort(recipes, function(a, b)
        -- Приоритеты дисциплин
        local priority = { Static = 1, Unknown = 2, EMAG = 4 }
        local aPriority = priority[a.discipline] or 3
        local bPriority = priority[b.discipline] or 3

        -- EMAG рецепты всегда в конце
        if a.isEmag ~= b.isEmag then
            return not a.isEmag
        end

        -- Если приоритеты совпадают
        if aPriority == bPriority then
            -- Сравнение по уровню исследования
            if a.tier == b.tier then
                -- Если уровни совпадают, сортируем по discipline
                return a.discipline < b.discipline
            end
            return a.tier < b.tier
        end

        -- Сортируем по приоритету discipline
        return aPriority < bPriority
    end)
end

function p.main(frame)
    local latheId = frame.args[1] or ""
    if latheId == "" then
        return '<div style="color:red;">Не указан ID станка.</div>'
    end

    local latheData = loadLatheData()
    local recipeData = loadRecipeData()
    local researchData = loadResearchData()

    local lathe = nil
    for _, data in ipairs(latheData) do
        if data.id == latheId then
            lathe = data
            break
        end
    end

    if not lathe then
        return '<div style="color:red;">Станок с ID "' .. latheId .. '" не найден.</div>'
    end

    local output = '<h3>Рецепты станка: ' .. latheId .. '</h3>'
    output = output .. '<ul>'

    local recipes = {}

    local function getRecipeDetails(recipeId)
        for _, recipe in ipairs(recipeData) do
            if recipe.id == recipeId then
                return recipe.latheRecipe
            end
        end
        return nil
    end

    local function findInResearch(recipeId)
        for _, research in ipairs(researchData) do
            if research.technology and research.technology.recipeUnlocks then
                for _, unlock in ipairs(research.technology.recipeUnlocks) do
                    if unlock == recipeId then
                        return {
                            name = research.technology.name,
                            tier = research.technology.tier,
                            discipline = research.technology.discipline
                        }
                    end
                end
            end
        end
        return nil
    end

    -- Обработка staticRecipes
    if lathe.Lathe.staticRecipes then
        for _, recipeId in ipairs(lathe.Lathe.staticRecipes) do
            local recipe = getRecipeDetails(recipeId)
            if recipe then
                table.insert(recipes, {
                    result = recipe.result,
                    completetime = recipe.completetime,
                    materials = recipe.materials,
                    discipline = "Static",
                    tier = 0
                })
            end
        end
    end

    -- Обработка dynamicRecipes
    if lathe.Lathe.dynamicRecipes then
        for _, recipeId in ipairs(lathe.Lathe.dynamicRecipes) do
            local recipe = getRecipeDetails(recipeId)
            if recipe then
                local researchInfo = findInResearch(recipeId)
                if researchInfo then
                    table.insert(recipes, {
                        result = recipe.result,
                        completetime = recipe.completetime,
                        materials = recipe.materials,
                        discipline = researchInfo.discipline,
                        tier = researchInfo.tier,
                        researchName = researchInfo.name
                    })
                else
                    table.insert(recipes, {
                        result = recipe.result,
                        completetime = recipe.completetime,
                        materials = recipe.materials,
                        discipline = "Unknown",
                        tier = 0
                    })
                end
            end
        end
    end

	-- Обработка emagStaticRecipes
	if lathe.EmagLatheRecipes and lathe.EmagLatheRecipes.emagStaticRecipes then
	    for _, recipeId in ipairs(lathe.EmagLatheRecipes.emagStaticRecipes) do
	        local recipe = getRecipeDetails(recipeId)
	        if recipe then
	            table.insert(recipes, {
	                result = recipe.result,
	                completetime = recipe.completetime,
	                materials = recipe.materials,
	                discipline = "Static",
	                tier = 0,
	                isEmag = true
	            })
	        end
	    end
	end

	-- Обработка emagDynamicRecipes 
	if lathe.EmagLatheRecipes and lathe.EmagLatheRecipes.emagDynamicRecipes then
	    for _, recipeId in ipairs(lathe.EmagLatheRecipes.emagDynamicRecipes) do
	        local recipe = getRecipeDetails(recipeId)
	        if recipe then
	            local researchInfo = findInResearch(recipeId)
	            if researchInfo then
	                table.insert(recipes, {
	                    result = recipe.result,
	                    completetime = recipe.completetime,
	                    materials = recipe.materials,
	                    discipline = researchInfo.discipline, -- Discipline из исследования
	                    tier = researchInfo.tier, -- Уровень из исследования
	                    researchName = researchInfo.name,
	                    isEmag = true -- Флаг для пометки EMAG
	                })
	            else
	                table.insert(recipes, {
	                    result = recipe.result,
	                    completetime = recipe.completetime,
	                    materials = recipe.materials,
	                    discipline = "Unknown", -- Если нет информации об исследовании
	                    tier = 0,
	                    isEmag = true -- Флаг для пометки EMAG
	                })
	            end
	        end
	    end
	end
	
	-- Вывод рецептов
	for _, recipe in ipairs(recipes) do
	    output = output .. '<li>' .. recipe.result
	    output = output .. ' (Время изготовления: ' .. format_seconds_to_short_string(recipe.completetime) .. ')'
	    output = output .. '<ul>'
	    for material, amount in pairs(recipe.materials) do
	        output = output .. '<li>' .. material .. ': ' .. amount .. '</li>'
	    end
	    output = output .. '</ul>'
	    
	    -- Информация об исследовании
	    if recipe.discipline ~= "Static" and recipe.discipline ~= "Unknown" then
	        output = output .. '<div>Исследование: ' .. recipe.discipline .. ' - ' .. recipe.researchName
	        output = output .. ' (Уровень: ' .. recipe.tier .. ')</div>'
	    end
	    
	    -- Пометка EMAG
	    if recipe.isEmag then
	        output = output .. '<div>Пометка: EMAG</div>'
	    end
	    
	    output = output .. '</li>'
	end

    output = output .. '</ul>'
    return output
end

return p