Открыть меню
Переключить меню настроек
Открыть персональное меню
Вы не представились системе
Ваш IP-адрес будет виден всем, если вы внесёте какие-либо изменения.

Модуль:ChemCard: различия между версиями

Материал из Space Stories Wiki
Нет описания правки
Метка: отменено
Нет описания правки
 
(не показано 28 промежуточных версий этого же участника)
Строка 1: Строка 1:
local prototypes = mw.loadData("Module:Chemistry Lookup/data")
local p = {}
local mw = mw
local html = mw.html
 
local function getArgs(frame)
    if type(frame) == "table" and frame.args then return frame.args end
    return mw.getCurrentFrame():getParent().args or {}
end
 
local function splitLines(s)
    local t = {}
    if not s then return t end
    for line in mw.text.gsplit(s, "\n") do
        line = mw.text.trim(line)
        if line ~= "" then table.insert(t, line) end
    end
    return t
end
 
local function parseLineToItem(l)
    local name, qty = mw.ustring.match(l, "^(.*)%s*%[%s*([%d,%.]+)%s*%]%s*$")
    if not name then name = l; qty = "1" end
    return { name = mw.text.trim(name), qty = qty or "1" }
end


local p = {}
local function parseRecipeString(recipeStr)
p.chem = prototypes.chem
    if not recipeStr or recipeStr == "" then return {} end
p.react = prototypes.react
    local lines = splitLines(recipeStr)
p.groupDirection = prototypes.groupDirection
    local steps = {}
    local currentInputs = {}
    local i = 1
    while i <= #lines do
        local line = lines[i]
        local lower = mw.ustring.lower(line)
        if lower:find("смеш") then
            if #currentInputs > 0 then
                local outputs = {}
                i = i + 1
                while i <= #lines do
                    local nextLine = lines[i]
                    local nextLower = mw.ustring.lower(nextLine)
                    if nextLower:find("смеш") then break end
                    table.insert(outputs, parseLineToItem(nextLine))
                    i = i + 1
                end
                table.insert(steps, {
                    inputs = currentInputs,
                    outputs = outputs,
                    action = mw.text.trim(line)
                })
                currentInputs = {}
            else
                i = i + 1
            end
        else
            table.insert(currentInputs, parseLineToItem(line))
            i = i + 1
        end
    end
    if #currentInputs > 0 then
        table.insert(steps, { inputs = currentInputs, outputs = {}, action = "" })
    end
    return steps
end
 
local FILE_PATTERN = "(%[%[File:[^]]+%]%])"
 
local function buildRecipeStepHtml(rec)
    if not rec or (#rec.inputs == 0 and #rec.outputs == 0) then return nil end
 
    local inner = html.create("div"):addClass("chem-recipe")
 
    local inputsDiv = html.create("div"):addClass("recipe-inputs")
    for _, it in ipairs(rec.inputs) do
        local item = inputsDiv:tag("div"):addClass("recipe-item")
        item:wikitext('[[File:Beaker.png|36px|class=chem-reagent-icon]] ' .. it.name .. ' [' .. it.qty .. ']')
    end
    inner:node(inputsDiv)
 
    if rec.action and rec.action ~= "" then
        local actionDiv = inner:tag("div"):addClass("recipe-action")
        local content = actionDiv:tag("div"):addClass("recipe-action-content")
 
        local sprite = '[[File:Beaker.png|36px|class=chem-reagent-icon]]'
        local actionText = rec.action
 
        local customSprite = mw.ustring.match(actionText, FILE_PATTERN)
        if customSprite then
            sprite = customSprite
            actionText = mw.text.trim(mw.ustring.gsub(actionText, FILE_PATTERN, ""))
        end
 
        content:tag("div"):addClass("action-sprite"):wikitext(sprite)
        content:tag("div"):addClass("action-text"):wikitext(actionText)
    end
 
    local outputsDiv = html.create("div"):addClass("recipe-outputs")
    for _, it in ipairs(rec.outputs) do
        local item = outputsDiv:tag("div"):addClass("recipe-item")
        item:wikitext('[[File:Beaker.png|36px|class=chem-reagent-icon]] ' .. it.name .. ' [' .. it.qty .. ']')
    end
    inner:node(outputsDiv)


local function tablelength(T)
    return inner
  local count = 0
  for _ in pairs(T) do count = count + 1 end
  return count
end
end


local function safe(val, default)
local function buildEffectsHtml(effects)
if val == nil then return default end
    if not effects or effects == "" or effects == "Нет" then return nil end
return val
    local container = html.create("div"):addClass("chem-effects-block")
    local heading = html.create("div"):addClass("chem-heading"):attr("data-kind", "effects")
    local hcontent = heading:tag("div"):addClass("chem-heading-content")
    hcontent:tag("span"):addClass("heading-text"):wikitext("Эффекты")
    hcontent:tag("span"):addClass("collapse-btn"):wikitext("развернуть")
    container:node(heading)
 
    local collapsible = html.create("div"):addClass("collapsible collapsed"):attr("data-kind", "effects")
    local inner = html.create("div"):addClass("chem-effects")
    for effect in mw.text.gsplit(effects, "[;\n]") do
        effect = mw.text.trim(effect)
        if effect ~= "" then
            inner:tag("div"):addClass("chem-effect-line"):wikitext(effect)
        end
    end
    collapsible:node(inner)
    container:node(collapsible)
    return container
end
end


function p.card(frame)
function p.card(frame)
local key = frame.args[1]
    local args = getArgs(frame)
local chem = p.chem[key]
    local name = args.name or ""
if not chem then return "" end
    local color = args.color or "#8cf"
    local border = args.border or "#444"
local bg = safe(chem.bg, "#101010")
    local desc = args.desc or ""
local border = safe(chem.border, "#777")
 
local color = safe(chem.color, "#FFF")
    local variants = {}
    local i = 1
local out = '<div class="chem-card" style="background:'..bg..'; border:2px solid '..border..'; padding:8px; border-radius:8px; margin:4px;">'
    while true do
out = out .. '<div class="chem-card-header" style="color:'..color..'; font-weight:700; font-size:1.1em; margin-bottom:4px;">'..safe(chem.name,"Unknown")..'</div>'
        local key = i == 1 and "recipe" or ("recipe" .. i)
        local recipeStr = args[key]
if chem.recipes and tablelength(chem.recipes) > 0 then
        if not recipeStr then break end
out = out .. '<div class="chem-card-recipes" style="margin-bottom:4px;"><b>Рецепты:</b><br>'
        local steps = parseRecipeString(recipeStr)
for _, recipe in pairs(chem.recipes) do
        if #steps > 0 then
local reactid = recipe.id or ""
            table.insert(variants, {id = i, steps = steps})
local reactdata = p.react[reactid]
        end
if reactdata then
        i = i + 1
out = out .. '<b>Ингредиенты:</b> '
    end
for i, ing in pairs(reactdata.reactants) do
 
local iname = safe(ing.name, "Unknown")
    local container = html.create("div"):addClass("chem-card")
local iamount = safe(ing.amount, 1)
    container:attr("style", "border-color:" .. border .. "; --card-accent:" .. color)
out = out .. iname .. " [" .. iamount .. "]"
    container:tag("div"):addClass("chem-name-header"):wikitext(name)
if i<#reactdata.reactants then out = out .. ", " end
 
end
    if #variants > 0 then
out = out .. '<br><b>Продукты:</b> '
        local recipeBlock = html.create("div"):addClass("chem-recipe-block")
for i, prod in pairs(reactdata.products) do
        local heading = html.create("div"):addClass("chem-heading"):attr("data-kind", "recipe")
local pname = safe(prod.name, "Unknown")
        local hcontent = heading:tag("div"):addClass("chem-heading-content")
local pamount = safe(prod.amount, 1)
        hcontent:tag("span"):addClass("heading-text"):wikitext("Рецепты")
out = out .. pname .. " [" .. pamount .. "]"
        hcontent:tag("span"):addClass("collapse-btn"):wikitext("развернуть")
if i<#reactdata.products then out = out .. ", " end
        recipeBlock:node(heading)
end
 
out = out .. '<br><hr style="border:1px solid #444;">'
        local collapsible = html.create("div"):addClass("collapsible collapsed"):attr("data-kind", "recipe")
end
        local stepsContainer = html.create("div"):addClass("chem-recipe-steps")
end
 
out = out .. '</div>'
        for vidx, variant in ipairs(variants) do
end
            if #variants > 1 then
                local varHeader = stepsContainer:tag("div"):addClass("recipe-variant-header")
if chem.metabolisms and tablelength(chem.metabolisms) > 0 then
                varHeader:wikitext("Вариант " .. vidx)
out = out .. '<div class="chem-card-effects" style="margin-bottom:4px;"><b>Эффекты:</b><br>'
            end
for k, met in pairs(chem.metabolisms) do
 
for _, e in pairs(met.effects) do
            for _, rec in ipairs(variant.steps) do
out = out .. safe(e.description,"") .. "<br>"
                local step = buildRecipeStepHtml(rec)
end
                if step then stepsContainer:node(step) end
end
            end
out = out .. '</div>'
        end
end
 
        collapsible:node(stepsContainer)
out = out .. '<div class="chem-card-desc" style="font-size:0.9em;">'..safe(chem.desc,"")..'</div>'
        recipeBlock:node(collapsible)
out = out .. '</div>'
        container:node(recipeBlock)
return out
    end
 
    local effectsBlock = buildEffectsHtml(args.effects)
    if effectsBlock then container:node(effectsBlock) end
 
    if desc ~= "" then
        container:tag("div"):addClass("chem-desc"):wikitext(desc)
    end
 
    return tostring(container)
end
end


return p
return p

Текущая версия от 13:20, 4 апреля 2026

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

local p = {}
local mw = mw
local html = mw.html

local function getArgs(frame)
    if type(frame) == "table" and frame.args then return frame.args end
    return mw.getCurrentFrame():getParent().args or {}
end

local function splitLines(s)
    local t = {}
    if not s then return t end
    for line in mw.text.gsplit(s, "\n") do
        line = mw.text.trim(line)
        if line ~= "" then table.insert(t, line) end
    end
    return t
end

local function parseLineToItem(l)
    local name, qty = mw.ustring.match(l, "^(.*)%s*%[%s*([%d,%.]+)%s*%]%s*$")
    if not name then name = l; qty = "1" end
    return { name = mw.text.trim(name), qty = qty or "1" }
end

local function parseRecipeString(recipeStr)
    if not recipeStr or recipeStr == "" then return {} end
    local lines = splitLines(recipeStr)
    local steps = {}
    local currentInputs = {}
    local i = 1
    while i <= #lines do
        local line = lines[i]
        local lower = mw.ustring.lower(line)
        if lower:find("смеш") then
            if #currentInputs > 0 then
                local outputs = {}
                i = i + 1
                while i <= #lines do
                    local nextLine = lines[i]
                    local nextLower = mw.ustring.lower(nextLine)
                    if nextLower:find("смеш") then break end
                    table.insert(outputs, parseLineToItem(nextLine))
                    i = i + 1
                end
                table.insert(steps, {
                    inputs = currentInputs,
                    outputs = outputs,
                    action = mw.text.trim(line)
                })
                currentInputs = {}
            else
                i = i + 1
            end
        else
            table.insert(currentInputs, parseLineToItem(line))
            i = i + 1
        end
    end
    if #currentInputs > 0 then
        table.insert(steps, { inputs = currentInputs, outputs = {}, action = "" })
    end
    return steps
end

local FILE_PATTERN = "(%[%[File:[^]]+%]%])"

local function buildRecipeStepHtml(rec)
    if not rec or (#rec.inputs == 0 and #rec.outputs == 0) then return nil end

    local inner = html.create("div"):addClass("chem-recipe")

    local inputsDiv = html.create("div"):addClass("recipe-inputs")
    for _, it in ipairs(rec.inputs) do
        local item = inputsDiv:tag("div"):addClass("recipe-item")
        item:wikitext('[[File:Beaker.png|36px|class=chem-reagent-icon]] ' .. it.name .. ' [' .. it.qty .. ']')
    end
    inner:node(inputsDiv)

    if rec.action and rec.action ~= "" then
        local actionDiv = inner:tag("div"):addClass("recipe-action")
        local content = actionDiv:tag("div"):addClass("recipe-action-content")

        local sprite = '[[File:Beaker.png|36px|class=chem-reagent-icon]]'
        local actionText = rec.action

        local customSprite = mw.ustring.match(actionText, FILE_PATTERN)
        if customSprite then
            sprite = customSprite
            actionText = mw.text.trim(mw.ustring.gsub(actionText, FILE_PATTERN, ""))
        end

        content:tag("div"):addClass("action-sprite"):wikitext(sprite)
        content:tag("div"):addClass("action-text"):wikitext(actionText)
    end

    local outputsDiv = html.create("div"):addClass("recipe-outputs")
    for _, it in ipairs(rec.outputs) do
        local item = outputsDiv:tag("div"):addClass("recipe-item")
        item:wikitext('[[File:Beaker.png|36px|class=chem-reagent-icon]] ' .. it.name .. ' [' .. it.qty .. ']')
    end
    inner:node(outputsDiv)

    return inner
end

local function buildEffectsHtml(effects)
    if not effects or effects == "" or effects == "Нет" then return nil end
    local container = html.create("div"):addClass("chem-effects-block")
    local heading = html.create("div"):addClass("chem-heading"):attr("data-kind", "effects")
    local hcontent = heading:tag("div"):addClass("chem-heading-content")
    hcontent:tag("span"):addClass("heading-text"):wikitext("Эффекты")
    hcontent:tag("span"):addClass("collapse-btn"):wikitext("развернуть")
    container:node(heading)

    local collapsible = html.create("div"):addClass("collapsible collapsed"):attr("data-kind", "effects")
    local inner = html.create("div"):addClass("chem-effects")
    for effect in mw.text.gsplit(effects, "[;\n]") do
        effect = mw.text.trim(effect)
        if effect ~= "" then
            inner:tag("div"):addClass("chem-effect-line"):wikitext(effect)
        end
    end
    collapsible:node(inner)
    container:node(collapsible)
    return container
end

function p.card(frame)
    local args = getArgs(frame)
    local name = args.name or "—"
    local color = args.color or "#8cf"
    local border = args.border or "#444"
    local desc = args.desc or ""

    local variants = {}
    local i = 1
    while true do
        local key = i == 1 and "recipe" or ("recipe" .. i)
        local recipeStr = args[key]
        if not recipeStr then break end
        local steps = parseRecipeString(recipeStr)
        if #steps > 0 then
            table.insert(variants, {id = i, steps = steps})
        end
        i = i + 1
    end

    local container = html.create("div"):addClass("chem-card")
    container:attr("style", "border-color:" .. border .. "; --card-accent:" .. color)
    container:tag("div"):addClass("chem-name-header"):wikitext(name)

    if #variants > 0 then
        local recipeBlock = html.create("div"):addClass("chem-recipe-block")
        local heading = html.create("div"):addClass("chem-heading"):attr("data-kind", "recipe")
        local hcontent = heading:tag("div"):addClass("chem-heading-content")
        hcontent:tag("span"):addClass("heading-text"):wikitext("Рецепты")
        hcontent:tag("span"):addClass("collapse-btn"):wikitext("развернуть")
        recipeBlock:node(heading)

        local collapsible = html.create("div"):addClass("collapsible collapsed"):attr("data-kind", "recipe")
        local stepsContainer = html.create("div"):addClass("chem-recipe-steps")

        for vidx, variant in ipairs(variants) do
            if #variants > 1 then
                local varHeader = stepsContainer:tag("div"):addClass("recipe-variant-header")
                varHeader:wikitext("Вариант " .. vidx)
            end

            for _, rec in ipairs(variant.steps) do
                local step = buildRecipeStepHtml(rec)
                if step then stepsContainer:node(step) end
            end
        end

        collapsible:node(stepsContainer)
        recipeBlock:node(collapsible)
        container:node(recipeBlock)
    end

    local effectsBlock = buildEffectsHtml(args.effects)
    if effectsBlock then container:node(effectsBlock) end

    if desc ~= "" then
        container:tag("div"):addClass("chem-desc"):wikitext(desc)
    end

    return tostring(container)
end

return p