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

мНет описания правки
мНет описания правки
 
(не показано 5 промежуточных версий этого же участника)
Строка 80: Строка 80:
     end
     end


-- Вспомогательная функция для обработки одного рецепта
    -- Вспомогательная функция для обработки одного рецепта
local function processRecipe(recipeId, defaultDiscipline, isEmag)
    local function processRecipe(recipeId, defaultDiscipline, isEmag)
    local recipe = getRecipeDetails(recipeId)
        local recipe = getRecipeDetails(recipeId)
    if recipe then
        if recipe then
        if recipe.result then
            if recipe.result then
            local info = {
                local info = {
                id = recipe.id,
                    id = recipe.id,
                result = recipe.result,
                    result = recipe.result,
                completetime = recipe.completetime,
                    completetime = recipe.completetime,
                materials = recipe.materials,
                    materials = recipe.materials,
                discipline = defaultDiscipline,
                    discipline = defaultDiscipline,
                tier = 0,
                    tier = 0,
                isEmag = isEmag or false
                    isEmag = isEmag or false
            }
                }
            if defaultDiscipline ~= "Static" then
                if defaultDiscipline ~= "Static" then
                local researchInfo = findInResearch(recipeId)
                    local researchInfo = findInResearch(recipeId)
                if researchInfo then
                    if researchInfo then
                    info.discipline = researchInfo.discipline
                        info.discipline = researchInfo.discipline
                    info.tier = researchInfo.tier
                        info.tier = researchInfo.tier
                    info.researchName = researchInfo.name
                        info.researchName = researchInfo.name
                end
                    end
            end
                end
            table.insert(recipes, info)
                table.insert(recipes, info)
        elseif recipe.resultReagents then
            elseif recipe.resultReagents then
            for reagent, amount in pairs(recipe.resultReagents) do
                for reagent, amount in pairs(recipe.resultReagents) do
                local reagentName = chemMapping[reagent] or reagent
                    local reagentName = chemMapping[reagent] or reagent
                table.insert(recipes, {
                    table.insert(recipes, {
                    id = recipe.id,
                        id = recipe.id,
                    result = reagentName .. "|amount=" .. amount .. "ед.|mode-chem=1",
                        result = reagentName .. "|amount=" .. amount .. "ед.|mode-chem=1",
                    completetime = recipe.completetime,
                        completetime = recipe.completetime,
                    materials = recipe.materials,
                        materials = recipe.materials,
                    discipline = defaultDiscipline,
                        discipline = defaultDiscipline,
                    tier = 0,
                        tier = 0,
                    isEmag = isEmag or false
                        isEmag = isEmag or false
                })
                    })
                break
                    break
            end
                end
        end
            end
    end
        end
end
    end


     -- Обработка рецептов для lathe.Lathe (старый и новый формат)
     -- Обработка рецептов для lathe.Lathe (старый и новый формат)
Строка 220: Строка 220:
-- Функция для формирования строки рецепта
-- Функция для формирования строки рецепта
-----------------------------------------------------------
-----------------------------------------------------------
local function formatRecipe(recipe, timeMultiplier, materialUseMultiplier, itemMode)
local function formatRecipe(recipe, timeMultiplier, materialUseMultiplier)
     local out = ""
     local out = ""
     local scaledTime = recipe.completetime * timeMultiplier
     local ct = recipe.completetime or 0
     out = out .. '{{Шаблон:Prototypes/Машина/Станок|product=' .. recipe.result
    local scaledTime = ct * timeMultiplier
    if itemMode then
     out = out .. '{{Шаблон:Prototypes/Машина/Станок/base|product=' .. recipe.result
        out = out .. '|no-product=1'
    end
     out = out .. '|complete-time={{#invoke:Code/Формат/Время|main|seconds|' .. scaledTime .. '}}|materials='
     out = out .. '|complete-time={{#invoke:Code/Формат/Время|main|seconds|' .. scaledTime .. '}}|materials='


Строка 301: Строка 299:
     end
     end


     local out = '{| class="wikitable"' .. "\n!Рецепт\n !Станок\n"
     local recipesOutput = ""
     local foundAny = false
     local foundAny = false


Строка 312: Строка 310:
             if recipe.id == itemId or recipe.result == itemId then
             if recipe.id == itemId or recipe.result == itemId then
                 foundAny = true
                 foundAny = true
                 out = out .. '|-' .. "\n"
                 local recipeStr = formatRecipe(recipe, timeMultiplier, materialUseMultiplier)
                out = out .. '| ' .. formatRecipe(recipe, timeMultiplier, materialUseMultiplier, true) .. "\n"
                 recipeStr = recipeStr:gsub("}}$", "|method-container=" .. lathe.id .. "}}")
                 out = out .. '| [[File:' .. lathe.id .. '.png|32x32px|link=]] [[{{#invoke:Entity Lookup|getname|' .. lathe.id .. '}}|{{#invoke:Entity Lookup|getname|' .. lathe.id .. '}}]]' .. "\n"
                recipesOutput = recipesOutput .. recipeStr
             end
             end
         end
         end
Строка 323: Строка 321:
     end
     end


     out = out .. '|}'
     local out = '<div class="grid-item-compressed">' .. recipesOutput .. '</div>'
     return mw.getCurrentFrame():preprocess(out)
     return mw.getCurrentFrame():preprocess(out)
end
end
Строка 336: Строка 334:
     end
     end


     local out = '{| class="wikitable"' .. "\n!Рецепт\n !Станок\n"
     local recipesOutput = ""
     local foundAny = false
     local foundAny = false


Строка 343: Строка 341:
         local materialUseMultiplier = (lathe.Lathe and lathe.Lathe.materialUseMultiplier) or 1
         local materialUseMultiplier = (lathe.Lathe and lathe.Lathe.materialUseMultiplier) or 1
         local timeMultiplier = (lathe.Lathe and lathe.Lathe.timeMultiplier) or 1
         local timeMultiplier = (lathe.Lathe and lathe.Lathe.timeMultiplier) or 1
        local matchingRecipes = {}


         for _, recipe in ipairs(recipes) do
         for _, recipe in ipairs(recipes) do
Строка 349: Строка 346:
                 for matId, _ in pairs(recipe.materials) do
                 for matId, _ in pairs(recipe.materials) do
                     if matId == materialId then
                     if matId == materialId then
                         table.insert(matchingRecipes, recipe)
                         foundAny = true
                        local recipeStr = formatRecipe(recipe, timeMultiplier, materialUseMultiplier)
                        recipeStr = recipeStr:gsub("}}$", "|method-container=" .. lathe.id .. "}}")
                        recipesOutput = recipesOutput .. recipeStr
                         break
                         break
                     end
                     end
                end
            end
        end
        if #matchingRecipes > 0 then
            foundAny = true
            local rowspan = #matchingRecipes
            for i, recipe in ipairs(matchingRecipes) do
                out = out .. '|-\n'
                out = out .. '| ' .. formatRecipe(recipe, timeMultiplier, materialUseMultiplier, false) .. "\n"
                if i == 1 then
                    out = out .. string.format('| rowspan="%d"|[[File:%s.png|32x32px|link=]] [[{{#invoke:Entity Lookup|getname|%s}}|{{#invoke:Entity Lookup|getname|%s}}]]\n', rowspan, lathe.id, lathe.id, lathe.id)
                 end
                 end
             end
             end
Строка 373: Строка 361:
     end
     end


     out = out .. '|}'
     local out = '<div class="grid-item-compressed">' .. recipesOutput .. '</div>'
     return mw.getCurrentFrame():preprocess(out)
     return mw.getCurrentFrame():preprocess(out)
end
-----------------------------------------------------------
-- Функция для универсального вызова из шаблона
-----------------------------------------------------------
function p.main(frame)
    local arguments = require("Модуль:Arguments").getArgs(frame, { unwrap = true })
    local mode = arguments[1] or ""
    local id  = arguments[2] or ""
   
    -- Для обеспечения работы mw.getCurrentFrame(), переиспользуем исходный frame
    local newFrame = {
        args = { id },
        getCurrentFrame = frame.getCurrentFrame or function() return frame end
    }
   
    if mode == "lathe" then
        return p.lathe(newFrame)
    elseif mode == "item" then
        return p.item(newFrame)
    elseif mode == "material" then
        return p.material(newFrame)
    else
        return '<div style="color:red;">Неверный режим вызова: "' .. mode .. '". Используйте "lathe", "item" или "material".</div>'
    end
end
end


return p
return p