Módulo:C.Generate

De Wiki Gla
Revisão de 21h32min de 31 de dezembro de 2025 por Gurren1 (discussão | contribs) (Criou página com '-- Módulo:C.Generate — função generate() local p = {} local utils = require("Módulo:C.Utils") local i18n = require("Módulo:C.i18n") local trim = utils.trim local file...')
(dif) ← Edição anterior | Revisão atual (dif) | Versão posterior → (dif)
Ir para navegação Ir para pesquisar

A documentação para este módulo pode ser criada em Módulo:C.Generate/doc

-- Módulo:C.Generate — função generate()
local p = {}

local utils = require("Módulo:C.Utils")
local i18n = require("Módulo:C.i18n")

local trim = utils.trim
local fileURL = utils.fileURL
local makeAttrString = utils.makeAttrString
local requireCharModule = utils.requireCharModule
local collectJsonObjects = utils.collectJsonObjects

local function ensureFileExists(fileName, fallback)
    fileName = trim(fileName or "")
    if fileName == "" then
        return fallback
    end
    local title = mw.title.new("Arquivo:" .. fileName)
    if title and title.exists then
        return fileName
    end
    return fallback
end

local ATTR_I18N = i18n.ATTR_I18N
local FLAGS_I18N = i18n.FLAGS_I18N
local TAB_I18N = i18n.TAB_I18N
local tierPackFrom = i18n.tierPackFrom
local tagsPackFrom = i18n.tagsPackFrom

-- Importa geradores
local iSkills = require("Módulo:I.Skills")

--------------------------------------------------------------------------------
-- Componente principal
--------------------------------------------------------------------------------

function p.generate(frame)
    local parent = frame:getParent() or frame
    local args = parent.args or {}

    -- Requer |module= explícito
    local moduleName = trim(args.module or "")
    if moduleName == "" then
        return "<!-- C.Generate: |module= é obrigatório -->"
    end

    -- Carrega módulo do personagem
    local data = requireCharModule(moduleName) or {}

    -- Nome: |name= ou do módulo
    local charName = trim(args.name or data.name or "")

    local html = mw.html.create('div')
    local box = html:tag('div'):addClass('character-box')

    ----------------------------------------------------------------------------
    -- Background (do módulo)
    ----------------------------------------------------------------------------
    do
        local bgParam = data.background or ""
        if bgParam ~= "" then
            box:attr('data-bg-file', bgParam)
            local url = fileURL(bgParam)
            if url and url ~= "" then
                box:attr('data-bg-url', url)
                local style = string.format("--character-bg: url('%s'); background-image: url('%s');", url, url)
                box:attr('style', style)
            end
        end
    end

    ----------------------------------------------------------------------------
    -- Weapon Icon (do módulo)
    ----------------------------------------------------------------------------
    do
        local weaponIconParam = data.weaponicon or ""
        if weaponIconParam ~= "" and weaponIconParam ~= "Nada.png" then
            box:attr('data-weaponicon', weaponIconParam)
        end
    end

    ----------------------------------------------------------------------------
    -- Header
    ----------------------------------------------------------------------------
    local header = box:tag('div'):addClass('character-header')

    -- topbar (avatar + nome + tags)
    local topbar = header:tag('div'):addClass('character-topbar')
    local nameBox = topbar:tag('div'):addClass('character-name-box')

    local avatarImg = ensureFileExists(data.avatar, "Franky_ts_medal.png")
    nameBox:wikitext(string.format('[[Arquivo:%s|class=topbar-icon|link=|alt=Avatar]]', avatarImg))

    local nameGroup = nameBox:tag('div'):addClass('character-name-group')
    nameGroup:tag('div'):addClass('character-name'):wikitext(charName)

    -- Class tags (TIER + TAGS, com i18n injetada)
    local classTags = nameGroup:tag('div'):addClass('class-tags')

    -- Resolve Tier/Tags do módulo
    local rawTier = ""
    if data.tier_i18n and type(data.tier_i18n) == "table" then
        rawTier = data.tier_i18n.pt or data.tier_i18n.en or data.tier or ""
    else
        rawTier = data.tier or ""
    end

    -- Chip de TIER
    local tierDiv
    if rawTier ~= "" then
        tierDiv = classTags:tag('div'):addClass('class-tag tier'):wikitext(rawTier)
    end

    -- Injeta i18n do TIER como data-*
    do
        local tI = data.tier_i18n
        if type(tI) ~= "table" then
            tI = tierPackFrom(rawTier ~= "" and rawTier or (data.tier or ""))
        else
            local seed = tI.pt or tI.en or tI.es or tI.pl or rawTier or data.tier or ""
            local base = tierPackFrom(seed)
            tI = {
                pt = tI.pt or base.pt,
                en = tI.en or base.en,
                es = tI.es or base.es,
                pl = tI.pl or base.pl
            }
        end
        if tierDiv then
            tierDiv:attr('data-tier-pt', tI.pt or rawTier)
                :attr('data-tier-en', tI.en or tI.pt or rawTier)
                :attr('data-tier-es', tI.es or tI.pt or rawTier)
                :attr('data-tier-pl', tI.pl or tI.pt or rawTier)
        end
    end

    -- Injeta i18n das TAGS em JSON no container
    do
        local tagsI = nil
        if type(data.tags_i18n) == "table" then
            local basis = tagsPackFrom(data.tags_i18n.pt or data.tags_i18n.en or data.tags_i18n.es or data.tags_i18n.pl or
            {})
            tagsI = {
                pt = data.tags_i18n.pt or basis.pt,
                en = data.tags_i18n.en or basis.en,
                es = data.tags_i18n.es or basis.es,
                pl = data.tags_i18n.pl or basis.pl
            }
        else
            local baseList = {}
            if type(data.tags) == "table" then
                baseList = data.tags
            elseif type(data.tags) == "string" then
                for entry in mw.text.gsplit(data.tags, '/', true) do
                    local t = mw.text.trim(entry or '')
                    if t ~= '' then
                        table.insert(baseList, t)
                    end
                end
            end
            tagsI = tagsPackFrom(baseList)
        end
        if tagsI then
            classTags:attr('data-tags-i18n', mw.text.jsonEncode(tagsI))
            -- Render inicial das TAGS (PT)
            if tagsI.pt and type(tagsI.pt) == "table" then
                for _, tag in ipairs(tagsI.pt) do
                    if trim(tag) ~= "" then
                        classTags:tag('div'):addClass('class-tag'):wikitext(tag)
                    end
                end
            end
        end
    end

    -- Descrição geral (do módulo)
    if data.desc then
        local descText = ""
        if type(data.desc) == "table" then
            descText = data.desc.pt or data.desc.en or data.desc.es or data.desc.pl or ""
        else
            descText = tostring(data.desc)
        end
        if descText ~= "" then
            header:tag('div'):addClass('topbar-description'):wikitext(descText)
        end
    end

    ----------------------------------------------------------------------------
    -- Idioma para tabs (PT padrão + aceita pt-br,en-US,... via |lang=)
    ----------------------------------------------------------------------------
    local rawLang = trim(args.lang or "pt")
    rawLang = mw.ustring.lower(rawLang)
    local baseLang = rawLang:match("^([a-z][a-z])") or rawLang

    local TAB = TAB_I18N[rawLang] or TAB_I18N[baseLang] or TAB_I18N.pt

    ----------------------------------------------------------------------------
    -- Abas (tabs)
    ----------------------------------------------------------------------------
    local tabs = header:tag('div'):addClass('character-tabs')
    tabs:tag('div'):addClass('tab-btn active'):attr('data-tab', 'skills'):wikitext(TAB.skills)
    tabs:tag('div'):addClass('tab-btn'):attr('data-tab', 'skins'):wikitext(TAB.skins)

    ----------------------------------------------------------------------------
    -- Aba: Skills
    ----------------------------------------------------------------------------
    local skillsTab = box:tag('div'):addClass('tab-content active'):attr('id', 'skills')
    local iconBar = skillsTab:tag('div'):addClass('icon-bar')
    local skillsContainer = skillsTab:tag('div'):addClass('skills-container')
    local details = skillsContainer:tag('div'):addClass('skills-details')
    local descBox = details:tag('div'):addClass('desc-box')
    skillsContainer:tag('div'):addClass('video-container'):done()

    skillsTab:attr('data-i18n-attrs', mw.text.jsonEncode(ATTR_I18N))
    skillsTab:attr('data-i18n-flags', mw.text.jsonEncode(FLAGS_I18N))
    if baseLang then
        skillsTab:attr('data-i18n-default', baseLang)
    end

    -- Gera skills do módulo: chama I.Skills
    local skillsJSON = iSkills.skill(frame)
    local idx = 0
    if skillsJSON and skillsJSON ~= "" then
        for _, chunk in ipairs(collectJsonObjects(skillsJSON)) do
            local ok, sk = pcall(mw.text.jsonDecode, chunk)
            if ok and type(sk) == "table" then
                local name = trim(sk.name or sk.n or "")
                local icon = trim(sk.icon or "")
                if icon ~= "" then
                    idx = idx + 1
                    local attrs = makeAttrString(
                        trim(tostring(sk.powerpve or "")),
                        trim(tostring(sk.powerpvp or "")),
                        trim(tostring(sk.energy or "")),
                        trim(tostring(sk.cooldown or ""))
                    )

                    local iconWrap = iconBar:tag('div'):addClass('skill-icon')
                        :attr('data-index', idx)
                        :attr('data-nome', name)
                        :attr('data-desc', trim(sk.desc or ""))
                        :attr('data-atr', attrs)
                        :attr('data-video', (sk.video and sk.video ~= "" and fileURL(sk.video)) or "")
                        :attr('data-video-preload', 'auto')
                        :attr('data-icon-file', icon)
                        :attr('data-video-file', sk.video or "")

                    local level = trim(tostring(sk.level or ""))
                    if level ~= "" and mw.ustring.upper(level) ~= "NIVEL" then
                        iconWrap:attr('data-level', level)
                    end

                    -- Descrições i18n
                    if type(sk.desc_i18n) == "table" then
                        iconWrap:attr('data-desc-pt', sk.desc_i18n.pt or "")
                            :attr('data-desc-en', sk.desc_i18n.en or "")
                            :attr('data-desc-es', sk.desc_i18n.es or "")
                            :attr('data-desc-pl', sk.desc_i18n.pl or "")
                    end

                    -- Subskills (suporte recursivo nativo)
                    if type(sk.subs) == "table" and #sk.subs > 0 then
                        iconWrap:attr('data-subs', mw.text.jsonEncode(sk.subs))
                    end
                    if type(sk.suborder) == "table" and #sk.suborder > 0 then
                        iconWrap:attr('data-suborder', mw.text.jsonEncode(sk.suborder))
                    end

                    -- Flags
                    if type(sk.flags) == "table" and #sk.flags > 0 then
                        iconWrap:attr('data-flags', mw.text.jsonEncode(sk.flags))
                    end

                    -- Weapon
                    if type(sk.weapon) == "table" then
                        iconWrap:attr('data-weapon', mw.text.jsonEncode(sk.weapon))
                    end
                    if sk.weaponicon and sk.weaponicon ~= "" and sk.weaponicon ~= "Nada.png" then
                        iconWrap:attr('data-weaponicon', sk.weaponicon)
                    end

                    -- Imagem do ícone
                    iconWrap:wikitext(string.format('[[Arquivo:%s|class=skill-icon-img|link=]]', icon))

                    -- Slot de descrição
                    descBox:tag('div'):addClass('skill-desc'):attr('data-index', idx)
                end
            end
        end
    end

    ----------------------------------------------------------------------------
    -- Aba: Skins (podium isométrico)
    ----------------------------------------------------------------------------
    local skinsTab = box:tag('div'):addClass('tab-content'):attr('id', 'skins')
    local cardSkins = skinsTab:tag('div'):addClass('card-skins')
    local podium = cardSkins:tag('div'):addClass('skins-podium')

    -- Gera skins do módulo: chama I.Skills
    local skinsJSON = iSkills.skin(frame)
    local skinIndex = 0
    if skinsJSON and skinsJSON ~= "" then
        for _, chunk in ipairs(collectJsonObjects(skinsJSON)) do
            local ok, sk = pcall(mw.text.jsonDecode, chunk)
            if ok and type(sk) == "table" then
                skinIndex = skinIndex + 1
                local imageFile = trim(sk.sprite or "")
                local tooltipRaw = trim(sk.tooltip or "")
                local skinTitle = trim(sk.name or "")

                -- Processa tooltip (pode ser string ou objeto i18n)
                local tipPack, chosen = nil, ""
                if tooltipRaw:match("^%s*{") then
                    local ok2, obj2 = pcall(mw.text.jsonDecode, tooltipRaw)
                    if ok2 and type(obj2) == "table" then
                        tipPack = obj2
                        chosen = trim(obj2.pt or obj2.en or obj2.es or obj2.pl or "")
                    end
                end
                if chosen == "" then
                    chosen = tooltipRaw
                end

                -- HTML do tooltip
                local titleHtml = skinTitle ~= "" and
                    ('<div class="skin-tooltip-title" style="margin:0">' .. skinTitle .. '</div>') or ""
                local bodyHtml = chosen:gsub("'''([^']+)'''", "<b>%1</b>"):gsub("\n", "<br>")
                bodyHtml = bodyHtml ~= "" and ('<b>' .. bodyHtml .. '</b>') or ""
                local tooltipHtml = titleHtml .. bodyHtml

                -- Podium slot
                local slot = podium:tag('div'):addClass('podium-slot')
                    :attr('data-skin-tooltip', tooltipHtml)
                    :attr('data-skin-index', skinIndex)

                -- Offset X
                local offsetXStr = trim(sk.offset_x or "")
                if offsetXStr ~= "" and offsetXStr ~= "0" then
                    local offsetX = tonumber(offsetXStr)
                    if offsetX and offsetX ~= 0 then
                        slot:attr('data-offset-x', tostring(offsetX))
                    end
                end

                -- YouTube
                local yt = trim(sk.youtube or "")
                if yt ~= "" then
                    if not yt:match("^https?://") then
                        if yt:match("^[%w%-%_]+$") then
                            yt = "https://youtu.be/" .. yt
                        else
                            yt = "https://" .. yt
                        end
                    end
                    slot:attr('data-youtube', yt):addClass('is-clickable')
                        :attr('tabindex', '0')
                        :attr('role', 'button')
                        :attr('aria-label', 'YouTube: ' .. (skinTitle ~= "" and skinTitle or 'skin'))
                end

                if skinTitle ~= "" then
                    slot:attr('data-skin-title', skinTitle)
                end
                if tipPack then
                    slot:attr('data-skin-tooltip-i18n', mw.text.jsonEncode(tipPack))
                end

                -- Sprite container
                local spriteContainer = slot:tag('div'):addClass('podium-sprite-container')
                local spriteDiv = spriteContainer:tag('div'):addClass('podium-sprite')

                if imageFile ~= "" then
                    spriteDiv:wikitext(string.format('[[Arquivo:%s|link=]]', imageFile))
                else
                    spriteDiv:wikitext("")
                end

                -- Tile
                local platform = spriteDiv:tag('div'):addClass('podium-platform')
                local platformTop = platform:tag('div'):addClass('podium-platform-top')

                local tileParam = trim(sk.tile or "")
                local tileFile = "Skintile.png"
                if mw.ustring.lower(tileParam) == "medium" then
                    tileFile = "SkintileM.png"
                end

                platformTop:wikitext(string.format('[[Arquivo:%s|link=]]', tileFile))

                -- Posição do tile
                local tileX = trim(sk.tile_x or "")
                local tileY = trim(sk.tile_y or "")
                local platformStyle = ""
                if tileX ~= "" then
                    platformStyle = platformStyle .. "right:" .. tileX .. "px;"
                else
                    platformStyle = platformStyle .. "right:-25px;"
                end
                if tileY ~= "" then
                    platformStyle = platformStyle .. "bottom:" .. tileY .. "px;"
                else
                    platformStyle = platformStyle .. "bottom:-15px;"
                end
                if platformStyle ~= "" then
                    platform:attr('style', platformStyle)
                end
            end
        end
    end

    ----------------------------------------------------------------------------
    -- Tier -> classe CSS (visual)
    ----------------------------------------------------------------------------
    do
        local tierKey = mw.ustring.lower(rawTier or "")
        local tierMap = {
            bronze = "tier-bronze",
            bronce = "tier-bronze",
            silver = "tier-silver",
            prata = "tier-silver",
            gold = "tier-gold",
            ouro = "tier-gold",
            diamond = "tier-diamond",
            diamante = "tier-diamond"
        }
        local tierClass = tierMap[tierKey]
        if tierClass then
            box:addClass(tierClass)
        end
    end

    ----------------------------------------------------------------------------
    -- Retorno
    ----------------------------------------------------------------------------
    return tostring(html)
end

return p