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

Материал из Space Station 14 Вики
Нет описания правки
мНет описания правки
Строка 36: Строка 36:


-----------------------------------------------------------
-----------------------------------------------------------
-- Функция для поиска пакета рецептов по его ID
-- Функция для получения группы рецептов по её ID
-----------------------------------------------------------
-----------------------------------------------------------
local function getRecipePack(packId)
local function getRecipePack(packId)
Строка 109: Строка 109:
     end
     end


     -----------------------------------------------------------
     -- Обработка staticPacks (ранее staticRecipes)
    -- Подготовка списков (оборачивание в таблицу, если нужно)
    -----------------------------------------------------------
    local staticPacks = {}
     if lathe.Lathe and lathe.Lathe.staticPacks then
     if lathe.Lathe and lathe.Lathe.staticPacks then
         if type(lathe.Lathe.staticPacks) == "string" then
         for _, packId in ipairs(lathe.Lathe.staticPacks) do
            staticPacks = { lathe.Lathe.staticPacks }
            local pack = getRecipePack(packId)
        else
            if pack then
             staticPacks = lathe.Lathe.staticPacks
                if pack.recipes then
                    for _, recipeId in ipairs(pack.recipes) do
                        local recipe = getRecipeDetails(recipeId)
                        if recipe and recipe.result then
                            table.insert(recipes, {
                                result = recipe.result,
                                completetime = recipe.completetime,
                                materials = recipe.materials,
                                discipline = "Static",
                                tier = 0
                            })
                        elseif recipe and recipe.resultReagents then
                            for reagent, amount in pairs(recipe.resultReagents) do
                                local reagentName = chemMapping[reagent] or reagent
                                table.insert(recipes, {
                                    result = reagentName .. "|amount=" .. amount .. "ед.|mode-chem=1",
                                    completetime = recipe.completetime,
                                    materials = recipe.materials,
                                    discipline = "Static",
                                    tier = 0
                                })
                                break
                            end
                        else
                            out = out .. '<div style="color:red;">Ошибка: Рецепт с ID "' .. recipeId .. '" не найден или отсутствуют поля result/resultReagents.</div>'
                        end
                    end
                else
                    out = out .. '<div style="color:red;">Ошибка: Группа рецептов с ID "' .. packId .. '" не содержит рецептов.</div>'
                end
             else
                out = out .. '<div style="color:red;">Ошибка: Группа рецептов с ID "' .. packId .. '" не найдена.</div>'
            end
         end
         end
     end
     end


     local dynamicRecipes = {}
     -- Обработка dynamicRecipes (если они ещё используются)
     if lathe.Lathe and lathe.Lathe.dynamicRecipes then
     if lathe.Lathe and lathe.Lathe.dynamicRecipes then
         if type(lathe.Lathe.dynamicRecipes) == "string" then
         for _, recipeId in ipairs(lathe.Lathe.dynamicRecipes) do
            dynamicRecipes = { lathe.Lathe.dynamicRecipes }
             local recipe = getRecipeDetails(recipeId)
        else
             if recipe then
             dynamicRecipes = lathe.Lathe.dynamicRecipes
                 local researchInfo = findInResearch(recipeId)
        end
                 if researchInfo then
    end
 
    local emagStaticPacks = {}
    if lathe.EmagLatheRecipes and lathe.EmagLatheRecipes.emagStaticPacks then
        if type(lathe.EmagLatheRecipes.emagStaticPacks) == "string" then
             emagStaticPacks = { lathe.EmagLatheRecipes.emagStaticPacks }
        else
            emagStaticPacks = lathe.EmagLatheRecipes.emagStaticPacks
        end
    end
 
    local emagDynamicRecipes = {}
    if lathe.EmagLatheRecipes and lathe.EmagLatheRecipes.emagDynamicRecipes then
        if type(lathe.EmagLatheRecipes.emagDynamicRecipes) == "string" then
            emagDynamicRecipes = { lathe.EmagLatheRecipes.emagDynamicRecipes }
        else
            emagDynamicRecipes = lathe.EmagLatheRecipes.emagDynamicRecipes
        end
    end
 
    -----------------------------------------------------------
    -- Обработка пакетов статических рецептов (staticPacks)
    -----------------------------------------------------------
    for _, packId in ipairs(staticPacks) do
        local pack = getRecipePack(packId)
        if pack and pack.recipes then
            for _, recipeId in ipairs(pack.recipes) do
                 local recipe = getRecipeDetails(recipeId)
                 if recipe and recipe.result 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 = "Static",
                         discipline = researchInfo.discipline,
                         tier = 0
                         tier = researchInfo.tier,
                        researchName = researchInfo.name
                     })
                     })
                elseif recipe and recipe.resultReagents then
                    for reagent, amount in pairs(recipe.resultReagents) do
                        local reagentName = chemMapping[reagent] or reagent
                        table.insert(recipes, {
                            result = reagentName .. "|amount=" .. amount .. "ед.|mode-chem=1",
                            completetime = recipe.completetime,
                            materials = recipe.materials,
                            discipline = "Static",
                            tier = 0
                        })
                        break
                    end
                else
                    out = out .. '<div style="color:red;">Ошибка: Рецепт с ID "' .. recipeId .. '" не найден или отсутствуют поля result/resultReagents.</div>'
                 end
                 end
             end
             end
        else
            out = out .. '<div style="color:red;">Ошибка: Пакет рецептов с ID "' .. packId .. '" не найден или не содержит рецептов.</div>'
         end
         end
     end
     end


    -----------------------------------------------------------
     -- Обработка emagStaticPacks (ранее emagStaticRecipes)
     -- Обработка динамических рецептов
     if lathe.EmagLatheRecipes and lathe.EmagLatheRecipes.emagStaticPacks then
     -----------------------------------------------------------
        for _, packId in ipairs(lathe.EmagLatheRecipes.emagStaticPacks) do
    for _, recipeId in ipairs(dynamicRecipes) do
            local pack = getRecipePack(packId)
        local recipe = getRecipeDetails(recipeId)
            if pack then
        if recipe then
                if pack.recipes then
            local researchInfo = findInResearch(recipeId)
                    for _, recipeId in ipairs(pack.recipes) do
            if researchInfo then
                        local recipe = getRecipeDetails(recipeId)
                table.insert(recipes, {
                        if recipe then
                    result = recipe.result,
                            table.insert(recipes, {
                    completetime = recipe.completetime,
                                result = recipe.result,
                    materials = recipe.materials,
                                completetime = recipe.completetime,
                    discipline = researchInfo.discipline,
                                materials = recipe.materials,
                    tier = researchInfo.tier,
                                discipline = "Static",
                     researchName = researchInfo.name
                                tier = 0,
                 })
                                isEmag = true
                            })
                        else
                            out = out .. '<div style="color:red;">Ошибка: Рецепт с ID "' .. recipeId .. '" не найден.</div>'
                        end
                    end
                else
                     out = out .. '<div style="color:red;">Ошибка: Группа рецептов с ID "' .. packId .. '" не содержит рецептов.</div>'
                end
            else
                 out = out .. '<div style="color:red;">Ошибка: Группа рецептов с ID "' .. packId .. '" не найдена.</div>'
             end
             end
         end
         end
     end
     end


    -----------------------------------------------------------
     -- Обработка emagDynamicRecipes (если они ещё используются)
     -- Обработка пакетов рецептов для EMAG (emagStaticPacks)
     if lathe.EmagLatheRecipes and lathe.EmagLatheRecipes.emagDynamicRecipes then
     -----------------------------------------------------------
        for _, recipeId in ipairs(lathe.EmagLatheRecipes.emagDynamicRecipes) do
    for _, packId in ipairs(emagStaticPacks) do
            local recipe = getRecipeDetails(recipeId)
        local pack = getRecipePack(packId)
            if recipe then
        if pack and pack.recipes then
                 local researchInfo = findInResearch(recipeId)
            for _, recipeId in ipairs(pack.recipes) do
                 if researchInfo then
                 local recipe = getRecipeDetails(recipeId)
                 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 = "Static",
                         discipline = researchInfo.discipline,
                         tier = 0,
                         tier = researchInfo.tier,
                        researchName = researchInfo.name,
                         isEmag = true
                         isEmag = true
                     })
                     })
                else
                    out = out .. '<div style="color:red;">Ошибка: Рецепт с ID "' .. recipeId .. '" не найден.</div>'
                 end
                 end
            end
        else
            out = out .. '<div style="color:red;">Ошибка: Пакет рецептов с ID "' .. packId .. '" не найден или не содержит рецептов.</div>'
        end
    end
    -----------------------------------------------------------
    -- Обработка динамических EMAG рецептов
    -----------------------------------------------------------
    for _, recipeId in ipairs(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,
                    tier = researchInfo.tier,
                    researchName = researchInfo.name,
                    isEmag = true
                })
             end
             end
         end
         end
Строка 254: Строка 223:
     sortRecipesByPriority(recipes)
     sortRecipesByPriority(recipes)


    -----------------------------------------------------------
     -- Таблица для перевода названий дисциплин
     -- Таблица для перевода названий дисциплин
    -----------------------------------------------------------
     local disciplineMapping = {
     local disciplineMapping = {
         Arsenal = "Арсенал",
         Arsenal = "Арсенал",
Строка 264: Строка 231:
     }
     }


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

Версия от 06:11, 26 февраля 2025

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

local p = {}

-----------------------------------------------------------
-- Загрузка данных
-----------------------------------------------------------
local latheData      = mw.loadData("Модуль:IanComradeBot/prototypes/lathe.json/data")
local recipeData     = mw.loadData("Модуль:IanComradeBot/prototypes/lathe/recipes.json/data")
local recipePackData = mw.loadData("Модуль:IanComradeBot/prototypes/lathe/recipe pack.json/data")
local researchData   = mw.loadData("Модуль:IanComradeBot/prototypes/research.json/data")
local materialData   = mw.loadData("Модуль:IanComradeBot/prototypes/materials.json/data")
local chemData       = mw.loadData("Модуль:IanComradeBot/chem prototypes.json/data")

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

        if a.isEmag ~= b.isEmag then
            return not a.isEmag
        end

        if aPriority == bPriority then
            if a.tier == b.tier then
                return a.discipline < b.discipline
            end
            return a.tier < b.tier
        end

        return aPriority < bPriority
    end)
end

-----------------------------------------------------------
-- Функция для получения группы рецептов по её ID
-----------------------------------------------------------
local function getRecipePack(packId)
    for _, pack in ipairs(recipePackData) do
        if pack.id == packId then
            return pack
        end
    end
    return nil
end

-----------------------------------------------------------
-- Основная функция модуля
-----------------------------------------------------------
function p.main(frame)
    local latheId = frame.args[1] or ""
    if latheId == "" then
        return '<div style="color:red;">Не указан ID станка.</div>'
    end

    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 materialMapping = {}
    for _, material in ipairs(materialData) do
        if material.id then
            materialMapping[material.id] = material.stackEntity or material.id or material.name
        end
    end

    local chemMapping = {}
    for id, chem in pairs(chemData) do
        chemMapping[id] = chem.name
    end

    local out = ''
    local recipes = {}

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

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

    -- Обработка staticPacks (ранее staticRecipes)
    if lathe.Lathe and lathe.Lathe.staticPacks then
        for _, packId in ipairs(lathe.Lathe.staticPacks) do
            local pack = getRecipePack(packId)
            if pack then
                if pack.recipes then
                    for _, recipeId in ipairs(pack.recipes) do
                        local recipe = getRecipeDetails(recipeId)
                        if recipe and recipe.result then
                            table.insert(recipes, {
                                result = recipe.result,
                                completetime = recipe.completetime,
                                materials = recipe.materials,
                                discipline = "Static",
                                tier = 0
                            })
                        elseif recipe and recipe.resultReagents then
                            for reagent, amount in pairs(recipe.resultReagents) do
                                local reagentName = chemMapping[reagent] or reagent
                                table.insert(recipes, {
                                    result = reagentName .. "|amount=" .. amount .. "ед.|mode-chem=1",
                                    completetime = recipe.completetime,
                                    materials = recipe.materials,
                                    discipline = "Static",
                                    tier = 0
                                })
                                break
                            end
                        else
                            out = out .. '<div style="color:red;">Ошибка: Рецепт с ID "' .. recipeId .. '" не найден или отсутствуют поля result/resultReagents.</div>'
                        end
                    end
                else
                    out = out .. '<div style="color:red;">Ошибка: Группа рецептов с ID "' .. packId .. '" не содержит рецептов.</div>'
                end
            else
                out = out .. '<div style="color:red;">Ошибка: Группа рецептов с ID "' .. packId .. '" не найдена.</div>'
            end
        end
    end

    -- Обработка dynamicRecipes (если они ещё используются)
    if lathe.Lathe and 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
                    })
                end
            end
        end
    end

    -- Обработка emagStaticPacks (ранее emagStaticRecipes)
    if lathe.EmagLatheRecipes and lathe.EmagLatheRecipes.emagStaticPacks then
        for _, packId in ipairs(lathe.EmagLatheRecipes.emagStaticPacks) do
            local pack = getRecipePack(packId)
            if pack then
                if pack.recipes then
                    for _, recipeId in ipairs(pack.recipes) 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
                            })
                        else
                            out = out .. '<div style="color:red;">Ошибка: Рецепт с ID "' .. recipeId .. '" не найден.</div>'
                        end
                    end
                else
                    out = out .. '<div style="color:red;">Ошибка: Группа рецептов с ID "' .. packId .. '" не содержит рецептов.</div>'
                end
            else
                out = out .. '<div style="color:red;">Ошибка: Группа рецептов с ID "' .. packId .. '" не найдена.</div>'
            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,
                        tier = researchInfo.tier,
                        researchName = researchInfo.name,
                        isEmag = true
                    })
                end
            end
        end
    end

    sortRecipesByPriority(recipes)

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

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

    local materialUseMultiplier = (lathe.Lathe and lathe.Lathe.materialUseMultiplier) or 1
    local timeMultiplier = (lathe.Lathe and lathe.Lathe.timeMultiplier) or 1

    for _, recipe in ipairs(recipes) do
        local scaledTime = recipe.completetime * timeMultiplier
        out = out .. '{{Шаблон:Prototypes/Машина/Станок|product=' .. recipe.result
        out = out .. '|complete-time={{#invoke:Code/Формат/Время|main|seconds|' .. scaledTime
        out = out .. '}}|materials='

        if recipe.materials then
            local materialEntries = {}
            for material, amount in pairs(recipe.materials) do
                local stackEntity = materialMapping[material] or material
                local scaledAmount = (amount * materialUseMultiplier) / 100
                table.insert(materialEntries, string.format('<b>[[File:%s.png|32x32px|link=]] %g {{#invoke:Entity Lookup|getname|%s}}</b>', stackEntity, scaledAmount, stackEntity))
            end
            out = out .. table.concat(materialEntries)
        else
            out = out .. 'Нет данных о материалах'
        end

        -- Информация об исследовании
        if recipe.discipline ~= "Static" then
            local tierColor = tierColors[recipe.tier] or "#FFFFFF"
            local disciplineName = disciplineMapping[recipe.discipline] or "Неизвестная дисциплина"

            out = out .. '|info=<div style="font-weight:600;"><span style="margin:8px;">[[File:' .. recipe.discipline .. '.png|16x16px|link=]]</span> [[Руководство по исследованию и разработке|' .. disciplineName
            out = out .. ']], уровень: <span style="color: ' .. tierColor .. '">' .. recipe.tier .. '</span> </div>'
        end

        -- Пометка при взломе EMAG
        if recipe.isEmag then
            out = out .. '|mode-emag=1'
        end

        -- Пометка для исследуемой технологии
        if recipe.discipline ~= "Static" then
            out = out .. '|mode-research=1'
        end

        out = out .. '}}'
    end

    return mw.getCurrentFrame():preprocess(out)
end

return p