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

мНет описания правки
мНет описания правки
 
(не показано 105 промежуточных версий этого же участника)
Строка 1: Строка 1:
local p = {}
local p = {}


-----------------------------------------------------------
-- Загрузка данных
-- Загрузка данных
-----------------------------------------------------------
local chemData = mw.loadData("Модуль:IanComradeBot/chem prototypes.json/data")
local latheData      = mw.loadData("Модуль:IanComradeBot/prototypes/lathe.json/data")
local seedsData = mw.loadData("Модуль:IanComradeBot/prototypes/seeds.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 kelvinToCelsius(k)
-- Вспомогательные функции
     return k - 273.15
-----------------------------------------------------------
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
end


local function getRecipeDetails(recipeId)
local function findSeedById(id, data)
     for _, recipe in ipairs(recipeData) do
     for _, seed in ipairs(data) do
         if recipe.id == recipeId then
         if seed.id == id then
             return recipe
             return seed
         end
         end
     end
     end
Строка 44: Строка 18:
end
end


local function findInResearch(recipeId)
local function formatCharacteristics(seed)
     for _, research in ipairs(researchData) do
     local parts = {
         if research and research.recipeUnlocks then
        ("[[Гидропоника#Потенция|Потенция]]: %s"):format(seed.potency or 1),
            for _, unlock in ipairs(research.recipeUnlocks) do
         ("[[Гидропоника#Урожайность|Урожайность]]: %s"):format(seed.yield),
                if unlock == recipeId then
        ("[[Гидропоника#Срок жизни|Срок жизни]]: %s"):format(seed.lifespan),
                    return {
        ("[[Гидропоника#Созревание|Созревание]]: %s"):format(seed.maturation),
                        name = research.name,
        ("[[Гидропоника#Производство|Производство]]: %s"):format(seed.production),
                        tier = research.tier,
        ("[[Гидропоника#Стадии роста|Стадии роста]]: %s"):format(seed.growthStages or 6),
                        discipline = research.discipline
    }
                    }
     return table.concat(parts, '<br>')
                end
            end
        end
    end
     return nil
end
end


local function getRecipePackDetails(packId)
local function formatConditions(seed)
     for _, pack in ipairs(recipePackData) do
     local parts = {
         if pack.id == packId then
        ("[[Гидропоника#Потребление воды|Вода]]: %s"):format(seed.waterConsumption or 0.5),
            return pack
         ("[[Гидропоника#Потребление нутриентов|Удобрение]]: %s"):format(seed.nutrientConsumption or 0.75),
         end
         ("[[Гидропоника#Оптимальная температура|Темп.]]: %.2f°C"):format(kelvinToCelsius(seed.idealHeat or 293)),
     end
     }
     return nil
     return table.concat(parts, '<br>')
end
end


-----------------------------------------------------------
local function formatHarvestType(seed)
-- Функция для сбора рецептов из станка
    return seed.harvestRepeat and "[[Гидропоника#Тип урожая|" .. tostring(seed.harvestRepeat) .. "]]" or "-"
-----------------------------------------------------------
end
local function getLatheRecipes(lathe)
local function formatHarvestType(seed)
     local recipes = {}
     local harvestRepeat = seed.harvestRepeat
     local chemMapping = {}
    if harvestRepeat == "Repeat" then
     for id, chem in pairs(chemData) do
        return "[[Гидропоника#Тип урожая|Многолетнее]]"
         chemMapping[id] = chem.name
     elseif harvestRepeat == "SelfHarvest" then
        return "[[Гидропоника#Тип урожая|Самосбор]]"
     else
         return "[[Гидропоника#Тип урожая|Однолетнее]]"
     end
     end
end


    -- Вспомогательная функция для обработки одного рецепта
local function formatChemicals(seed)
    local function processRecipe(recipeId, defaultDiscipline, isEmag)
    if not seed.chemicals then return "-" end
        local recipe = getRecipeDetails(recipeId)
    local list = {}
        if recipe then
    for chemId, vals in pairs(seed.chemicals) do
            if recipe.result then
        local entry = chemData[chemId]
                local info = {
        local chemName = entry and entry.name or chemId
                    result = recipe.result,
        table.insert(list, string.format(
                    completetime = recipe.completetime,
            "<li>[[Химия#chem_%s|%s]] (мин: %s, макс: %s, дел: %s)</li>",
                    materials = recipe.materials,
            chemId, chemName, vals.Min or 0, vals.Max or 0, vals.PotencyDivisor or 1
                    discipline = defaultDiscipline,
        ))
                    tier = 0,
                    isEmag = isEmag or false
                }
                if defaultDiscipline ~= "Static" then
                    local researchInfo = findInResearch(recipeId)
                    if researchInfo then
                        info.discipline = researchInfo.discipline
                        info.tier = researchInfo.tier
                        info.researchName = researchInfo.name
                    end
                end
                table.insert(recipes, info)
            elseif 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 = defaultDiscipline,
                        tier = 0,
                        isEmag = isEmag or false
                    })
                    break
                end
            end
        end
     end
     end
    return "<ul>" .. table.concat(list) .. "</ul>"
end


    -- Обработка рецептов для lathe.Lathe (старый и новый формат)
local function formatMutations(seed, data)
     if lathe.Lathe then
     if not seed.mutationPrototypes then return "-" end
        if lathe.Lathe.staticRecipes then
    local list = {}
            for _, recipeId in ipairs(lathe.Lathe.staticRecipes) do
    for _, mu in ipairs(seed.mutationPrototypes) do
                processRecipe(recipeId, "Static", false)
         local target = findSeedById(mu, data)
            end
         if target and target.productPrototypes then
        end
             for _, prod in ipairs(target.productPrototypes) do
        if lathe.Lathe.dynamicRecipes then
                 table.insert(list, ("<li>{{Предмет|%s|link=Гидропоника#{{#invoke:Entity Lookup|getname|%s}}}}</li>"):format(prod, prod))
            for _, recipeId in ipairs(lathe.Lathe.dynamicRecipes) do
                processRecipe(recipeId, "Dynamic", false)
            end
        end
        if lathe.Lathe.staticPacks then
            for _, packId in ipairs(lathe.Lathe.staticPacks) do
                local pack = getRecipePackDetails(packId)
                if pack and pack.recipes then
                    local packRecipes = type(pack.recipes) ~= "table" and { pack.recipes } or pack.recipes
                    for _, recipeId in ipairs(packRecipes) do
                        processRecipe(recipeId, "Static", false)
                    end
                end
            end
        end
         if lathe.Lathe.dynamicPacks then
            for _, packId in ipairs(lathe.Lathe.dynamicPacks) do
                local pack = getRecipePackDetails(packId)
                if pack and pack.recipes then
                    local packRecipes = type(pack.recipes) ~= "table" and { pack.recipes } or pack.recipes
                    for _, recipeId in ipairs(packRecipes) do
                        processRecipe(recipeId, "Dynamic", false)
                    end
                end
            end
         end
    end
 
    if lathe.EmagLatheRecipes then
        if lathe.EmagLatheRecipes.emagStaticRecipes then
             for _, recipeId in ipairs(lathe.EmagLatheRecipes.emagStaticRecipes) do
                 processRecipe(recipeId, "Static", true)
            end
        end
        if lathe.EmagLatheRecipes.emagDynamicRecipes then
            for _, recipeId in ipairs(lathe.EmagLatheRecipes.emagDynamicRecipes) do
                processRecipe(recipeId, "Dynamic", true)
            end
        end
        if lathe.EmagLatheRecipes.emagStaticPacks then
            for _, packId in ipairs(lathe.EmagLatheRecipes.emagStaticPacks) do
                local pack = getRecipePackDetails(packId)
                if pack and pack.recipes then
                    local packRecipes = type(pack.recipes) ~= "table" and { pack.recipes } or pack.recipes
                    for _, recipeId in ipairs(packRecipes) do
                        processRecipe(recipeId, "Static", true)
                    end
                end
            end
        end
        if lathe.EmagLatheRecipes.emagDynamicPacks then
            for _, packId in ipairs(lathe.EmagLatheRecipes.emagDynamicPacks) do
                local pack = getRecipePackDetails(packId)
                if pack and pack.recipes then
                    local packRecipes = type(pack.recipes) ~= "table" and { pack.recipes } or pack.recipes
                    for _, recipeId in ipairs(packRecipes) do
                        processRecipe(recipeId, "Dynamic", true)
                    end
                end
             end
             end
         end
         end
     end
     end
    return "<ul>" .. table.concat(list) .. "</ul>"
end


    sortRecipesByPriority(recipes)
local function generateHeader()
     return recipes
     return [[
{| id="BOTANY" class="wikitable sortable mw-collapsible" style="width:100%;"
! rowspan="2" style="width:10%;" | Плод
! rowspan="2" class="unsortable" style="width:5%;" | Семена
! rowspan="2" class="unsortable" style="width:5%;" | Растение
! colspan="3" class="unsortable" style="width:30%;" id="no-highlight" | Характеристики
! rowspan="2" class="unsortable" style="width:30%;" | Содержит вещества
! rowspan="2" style="width:20%;" | Мутации
|-
! style="width:10%;" class="unsortable" | Рост
! style="width:10%;" class="unsortable" | Условия
! style="width:5%;" class="unsortable" | Тип сбора
]]
end
end


-----------------------------------------------------------
local function generateFooter()
-- Общие таблицы для форматирования вывода
     return "|}"
-----------------------------------------------------------
local disciplineMapping = {
    Arsenal = "Арсенал",
     Industrial = "Промышленность",
    Experimental = "Экспериментальное",
    CivilianServices = "Обслуживание персонала"
}
local tierColors = {
    [1] = "#54d554",
    [2] = "#ed9000",
    [3] = "#d72a2a"
}
local materialMappingGlobal = {}
for _, material in ipairs(materialData) do
    if material.id then
        materialMappingGlobal[material.id] = material.stackEntity or material.id or material.name
    end
end
end


-----------------------------------------------------------
function p.table(frame)
-- Функция для формирования строки рецепта
     local data = mw.loadData("Модуль:IanComradeBot/prototypes/seeds.json/data")
-----------------------------------------------------------
     local rows = {}
local function formatRecipe(recipe, timeMultiplier, materialUseMultiplier)
     local out = ""
    local scaledTime = recipe.completetime * timeMultiplier
    out = out .. '{{Шаблон:Prototypes/Машина/Станок|product=' .. recipe.result
     out = out .. '|complete-time={{#invoke:Code/Формат/Время|main|seconds|' .. scaledTime .. '}}|materials='


     if recipe.materials then
     for _, seed in ipairs(data) do
        local materialEntries = {}
        local prodId = seed.productPrototypes[1]
        for material, amount in pairs(recipe.materials) do
        local seedId = seed.packetPrototype
            local stackEntity = materialMappingGlobal[material] or material
        local seedName = string.format('{{#invoke:Entity Lookup|getname|%s}}', seedId)
            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 anchor  = string.format('{{anchor|%s}}', seedName)
         local tierColor = tierColors[recipe.tier] or "#FFFFFF"
         local fruitImg = string.format(
         local disciplineName = disciplineMapping[recipe.discipline] or "Неизвестная дисциплина"
            '{{Предмет|%s|size=64px|vertical=1|imageTooltip=1|link=%s}}',
         out = out .. '|info=<div style="font-weight:600;"><span style="margin:8px;">[[File:' .. recipe.discipline .. '.png|16x16px|link=]]</span> [[Руководство по исследованию и разработке|' .. disciplineName
            prodId, seedName
         out = out .. ']], уровень: <span style="color: ' .. tierColor .. '">' .. recipe.tier .. '</span> </div>'
        )
    end
         local seedImg  = string.format(
            '{{Предмет|%s|size=64px|vertical=1|imageTooltip=1|l=|link=%s}}',
            seedId, seedName
        )
         local plantImg = string.format(
            '{{Предмет|%s-harvest|size=64px|l=|link=%s}}',
            seedId, seedName
        )


     if recipe.isEmag then
        local colGrowth     = formatCharacteristics(seed)
         out = out .. '|mode-emag=1'
        local colConditions = formatConditions(seed)
    end
         local colHarvest    = formatHarvestType(seed)
        local colChemicals  = formatChemicals(seed)
        local colMutations  = formatMutations(seed, data)


    if recipe.discipline ~= "Static" then
         local row = frame:preprocess(string.format(
         out = out .. '|mode-research=1'
            [[|-
            ! %s
            ! %s
            ! %s
            | %s
            | %s
            | %s
            | %s
            | %s ]],
            fruitImg, seedImg, plantImg,
            colGrowth, colConditions, colHarvest,
            colChemicals, colMutations
        ))
        table.insert(rows, row)
     end
     end


     out = out .. '}}'
     return generateHeader() .. table.concat(rows, '\n') .. '\n' .. generateFooter()
    return out
end
end


-----------------------------------------------------------
function p.main(frame)
-- Основная функция
     local args = frame.args
-----------------------------------------------------------
    local id = args[1]
function p.lathe(frame)
     local mode  = mw.text.trim(args[2] or ""):lower()
     local latheId = frame.args[1] or ""
     local seed = findSeedById(id, seedsData)
     if latheId == "" then
    if not seed then return "" end
        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
     if mode  == "growth" then
         return '<div style="color:red;">Станок с ID "' .. latheId .. '" не найден.</div>'
         return formatCharacteristics(seed)
    elseif mode  == "conditions" then
        return formatConditions(seed)
    elseif mode  == "harvest" then
        return formatHarvestType(seed)
    elseif mode  == "chemicals" then
        return formatChemicals(seed)
    elseif mode  == "mutations" then
        return formatMutations(seed, seedsData)
    else
        return ""
     end
     end
    local materialUseMultiplier = (lathe.Lathe and lathe.Lathe.materialUseMultiplier) or 1
    local timeMultiplier = (lathe.Lathe and lathe.Lathe.timeMultiplier) or 1
    local recipes = getLatheRecipes(lathe)
    local out = ""
    for _, recipe in ipairs(recipes) do
        out = out .. formatRecipe(recipe, timeMultiplier, materialUseMultiplier)
    end
    return mw.getCurrentFrame():preprocess(out)
end
-----------------------------------------------------------
-- Функция для поиска и вывода рецептов по ID предмета
-----------------------------------------------------------
function p.item(frame)
    local itemId = frame.args[1] or ""
    if itemId == "" then
        return '<div style="color:red;">Не указан ID предмета.</div>'
    end
    local out = '{| class="wikitable"\n! Станок ID\n! Рецепт\n'
    local foundAny = false
    for _, lathe in ipairs(latheData) do
        local recipes = getLatheRecipes(lathe)
        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
            if recipe.result == itemId then
                foundAny = true
                out = out .. '|-\n'
                out = out .. '| ' .. lathe.id .. '\n'
                out = out .. '| ' .. formatRecipe(recipe, timeMultiplier, materialUseMultiplier) .. '\n'
            end
        end
    end
    if not foundAny then
        return '<div style="color:red;">Рецепт для предмета с ID "' .. itemId .. '" не найден во всех станках.</div>'
    end
    out = out .. '|}'
    return mw.getCurrentFrame():preprocess(out)
end
end


return p
return p