Zum Inhalt springen


Modul:HeroCard

Aus Firestone Idle RPG Wiki

Die Dokumentation für dieses Modul kann unter Modul:HeroCard/Doku erstellt werden

-- Modul:HeroCard
local p = {}

-- ===== Helpers =====
local function isempty(v) return v == nil or v == "" end
local function pick(...) local n=select('#',...); for i=1,n do local v=select(i,...); if not isempty(v) then return v end end end
local function norm(s) if isempty(s) then return "" end return mw.ustring.lower(mw.text.trim(s)):gsub("%s+"," ") end

local function getArgs(frame)
  local args, f1, f2 = {}, frame.args or {}, (frame:getParent() and frame:getParent().args) or {}
  for k,v in pairs(f1) do if not isempty(v) then args[k]=v end end
  for k,v in pairs(f2) do if not isempty(v) then args[k]=v end end
  return args
end

-- ===== Lazy load: Config / I18n =====
local CFG, I18N
local function loadConfig()
  if CFG then return CFG end
  for _,t in ipairs{ "Modul:HeroConfig", "Module:HeroConfig" } do
    local ok, mod = pcall(require, t)
    if ok and type(mod)=="table" then CFG=mod; return CFG end
  end
  error("HeroConfig not found")
end
local function loadI18n()
  if I18N then return I18N end
  for _,t in ipairs{ "Modul:HeroI18n", "Module:HeroI18n" } do
    local ok, mod = pcall(require, t)
    if ok and type(mod)=="table" then I18N=mod; return I18N end
  end
  I18N = { i18n = {}, aw_desc = {}, ui = {} }; return I18N
end

-- ===== HeroData (de/en) =====
local function loadHeroes()
  for _,t in ipairs{ "Modul:HeroData", "Module:HeroData" } do
    local ok, mod = pcall(require, t)
    if ok and type(mod)=="table" and type(mod.heroes)=="table" then
      return mod.heroes, {ok=true, from=t}
    end
  end
  return nil, {ok=false}
end

-- ===== MediaWiki-Bildmarkup =====
local function fileWikitext(filename, opts)
  if isempty(filename) then return "" end
  local parts = { ("Datei:%s"):format(filename) }
  if opts and opts.size  then table.insert(parts, opts.size) end
  if opts and opts.link  then table.insert(parts, "link="..opts.link) end
  if opts and opts.class then table.insert(parts, "class="..opts.class) end
  return string.format("[[%s]]", table.concat(parts, "|"))
end

-- ===== i18n-Mapping =====
local function tr(kind, value)
  if isempty(value) then return value end
  local map = loadI18n().i18n[kind] or {}
  return map[norm(value)] or value
end

-- ===== Tooltips =====
local function addTooltip(box, title, desc)
  if (isempty(title) and isempty(desc)) then return end
  local tip = box:tag("span"):addClass("kr-tipbox")
  if not isempty(title) then tip:tag("span"):addClass("kr-tip-title"):wikitext(title) end
  if not (isempty(title) or isempty(desc)) then tip:tag("span"):addClass("kr-tip-sep") end
  if not isempty(desc)  then tip:tag("span"):addClass("kr-tip-desc"):wikitext(desc) end
end

-- ===== kleine Bausteine =====
local function pill(parent, label, val, icon, showLabels, desc)
  if isempty(val) then return end
  local box = parent:tag("div"):addClass("kr-hc-pill kr-tip")
  local left = box:tag("div"):addClass("kr-hc-pill-icon")
  if not isempty(icon) then left:wikitext(fileWikitext(icon, {size="36x36px", link=""})) end
  local text = box:tag("div"):addClass("kr-hc-pill-text")
  local lab = text:tag("div"):addClass("kr-hc-pill-label"):wikitext(label or "")
  if not showLabels then lab:addClass("is-hidden") end
  text:tag("div"):addClass("kr-hc-pill-value"):wikitext(val)
  addTooltip(box, label, desc)
end
local function adv(parent, label, val, icon, showLabels, desc)
  if isempty(val) then return end
  local box = parent:tag("div"):addClass("kr-hc-adv kr-tip")
  local left = box:tag("div"):addClass("kr-hc-adv-icon")
  if not isempty(icon) then left:wikitext(fileWikitext(icon, {size="36x36px", link=""})) end
  local main = box:tag("div"):addClass("kr-hc-adv-main")
  local lab = main:tag("div"):addClass("kr-hc-adv-label"):wikitext(label or "")
  if not showLabels then lab:addClass("is-hidden") end
  main:tag("div"):addClass("kr-hc-adv-value"):wikitext(val)
  addTooltip(box, label, desc)
end

-- ===== Freischaltungs-Text =====
local function unlock_text_de(s)
  if isempty(s) then return nil end
  local t = norm(s)
  local n = t:match("stage%s*(%d+)"); if n then return "Freischaltung bei Abschnitt: " .. n end
  if t:match("pirate%s*ship") then return "Wird freigeschaltet auf dem Piratenschiff." end
  if s:match("[%.%!%?]$") or t:find(" ") then return s end
  return "Freischaltung: " .. s
end

-- ===== Awakening-Descriptions (englische Keys) =====
local function awakeningDesc(raw_value, override)
  if not isempty(override) then return override end
  if isempty(raw_value) then return "Bonus, der beim Erwachen freigeschaltet wird." end
  local map = loadI18n().aw_desc or {}
  return map[norm(raw_value)] or "Bonus, der beim Erwachen freigeschaltet wird."
end

-- ===== Avatar (Single oder Tabber) =====
local function buildAvatar(name, linkto, defaultFile, skinsTable, defaultLabel)
  if type(skinsTable) ~= "table" or next(skinsTable) == nil then
    return fileWikitext(defaultFile, { class="kr-hc-avatar-img", link=linkto })
  end
  local frame = mw.getCurrentFrame()
  defaultLabel = defaultLabel or (loadI18n().ui and loadI18n().ui.default_skin) or "Standard"

  local panels = {}
  local function push(label, file)
    if file == defaultFile then return end -- Duplikate vermeiden
    table.insert(panels, tostring(label) .. "=\n" ..
      fileWikitext(file, { class="kr-hc-avatar-img", link=linkto }))
  end

  table.insert(panels, defaultLabel .. "=\n" ..
    fileWikitext(defaultFile, { class="kr-hc-avatar-img", link=linkto }))

  if skinsTable[1] ~= nil then
    for _, v in ipairs(skinsTable) do
      if type(v) == "table" then
        local label = v.label or v[1]
        local file  = v.file  or (name .. "_" .. (v.filename or label) .. ".png")
        push(label, file)
      else
        local label = tostring(v)
        push(label, name .. "_" .. label .. ".png")
      end
    end
  else
    local tmp = {}
    for k, v in pairs(skinsTable) do
      if type(v) == "table" then
        local label = v.label or v[1] or tostring(k)
        local file  = v.file  or (name .. "_" .. (v.filename or label) .. ".png")
        table.insert(tmp, {label=label, file=file})
      else
        local label = tostring(v)
        table.insert(tmp, {label=label, file=(name .. "_" .. label .. ".png")})
      end
    end
    table.sort(tmp, function(a,b) return tostring(a.label) < tostring(b.label) end)
    for _,it in ipairs(tmp) do push(it.label, it.file) end
  end

  local content = table.concat(panels, "\n|-|\n")
  return frame:extensionTag('tabber', content)
end

-- ======= RENDER =======
function p.render(frame)
  local args  = getArgs(frame)
  local name  = pick(args.name, args[1]) or ""

  local heroes = select(1, loadHeroes())
  local data   = (heroes and (heroes[name] or heroes[mw.ustring.gsub(name or "", "_", " ")])) or {}

  local showLabels = norm(args.show_labels) == "yes" or norm(args.show_labels) == "1"

  -- UI-Labels & Guardian-Flag
  local UI      = (loadI18n().ui or {})
  local entity  = norm(pick(args.entity, data.entity))
  local isGuardian = (entity == "guardian")
      or norm(pick(args.guardian, data.guardian)) == "1"
      or pick(args.is_guardian, data.is_guardian) == true

  local labelsHero     = (UI.labels and UI.labels.hero)     or { class="Klasse:",   awakening="Erweckungsbonus" }
  local labelsGuardian = (UI.labels and UI.labels.guardian) or { class="Typ:",      awakening="Wächteraura"     }
  local L = isGuardian and labelsGuardian or labelsHero

  -- Daten + Übersetzungen
  local class             = tr("class",          pick(args.class,          data.class))
  local attackstyleRaw    = pick(args.attackstyle,    data.attackstyle)
  local specializationRaw = pick(args.specialization, data.specialization)
  local resourceRaw       = pick(args.resource,       data.resource)
  local attackstyle       = tr("attackstyle",    attackstyleRaw)
  local specialization    = tr("specialization", specializationRaw)
  local resource          = tr("resource",       resourceRaw)
  local unlock_at         = pick(args.unlock_at, data.unlock_at)

  local awakening_bonusRaw = pick(
    args.awakening_bonus, data.awakening_bonus,   -- Held
    args.guardian_aura,   data.guardian_aura,     -- Wächter
    data.aura
  )
  local awakening_bonus = tr("awakening_bonus", awakening_bonusRaw)
  local awakening_desc  = awakeningDesc(awakening_bonusRaw, pick(args.awakening_desc, data.awakening_desc))

  local CFGmod   = loadConfig()
  local iconmap  = CFGmod.icon_map or {}
  local advGuard = CFGmod.adv_order_guardian or {"precision","bonus_damage"}

  local attackstyle_icon     = (iconmap.attackstyle     or {})[norm(attackstyleRaw)]
  local specialization_icon  = (iconmap.specialization  or {})[norm(specializationRaw)]
  local resource_icon        = (iconmap.resource        or {})[norm(resourceRaw)]
  local awakening_bonus_icon = (iconmap.awakening_bonus or {})[norm(awakening_bonusRaw)]

  -- Bilder/Links – Default-Avatar beachten (avatar/default_skin)
  local linkto       = pick(args.linkto, name)
  local defaultSkin  = pick(args.default_skin, data.default_skin)
  local avatar = pick(
    args.avatar, data.avatar,
    (not isempty(defaultSkin) and (name .. "_" .. defaultSkin .. ".png")) or
    ((not isempty(name)) and (name .. ".png")) or nil
  )
  local defaultTabLabel = pick(args.default_skin_label, data.default_skin_label, (UI.default_skin or "Standard"))

  -- Root
  local root = mw.html.create("div"):addClass("kr-hero-card")
  if showLabels then root:addClass("kr-show-labels") end
  if isGuardian then root:addClass("kr-layout-guardian") else root:addClass("kr-layout-hero") end

  -- Avatar (links)
  local avatarCol = root:tag("div"):addClass("kr-hc-avatar")
  avatarCol:wikitext( buildAvatar(name, linkto, avatar, data.skins, defaultTabLabel) )

  -- Content (rechts)
  local content = root:tag("div"):addClass("kr-hc-content")
  if not isempty(name) then
    content:tag("h2"):addClass("kr-hc-name"):wikitext(string.format("[[%s|%s]]", linkto, name))
    local ut = unlock_text_de(unlock_at)
    if not isempty(ut) then
      content:tag("div"):addClass("kr-hc-unlockline"):wikitext("🔒 " .. ut)
    end
  end

  -- Basics-Zeile (2×2)
  local basics = content:tag("div"):addClass("kr-hc-basics2")
  local function pair(label, value, icon)
    if isempty(value) then return end
    local it = basics:tag("div"):addClass("kr-hc-basic")
    it:tag("span"):addClass("label"):wikitext(label)
    local v = it:tag("span"):addClass("value"):wikitext(value)
    if icon then v:tag("span"):addClass("value-icon"):wikitext(fileWikitext(icon, {size="24x24px", link=""})) end
  end
  pair(L.class, class, nil)
  pair("Spezialisierung:", specialization, specialization_icon)
  pair("Angriffsart:",     attackstyle,    attackstyle_icon)
  pair("Ressource:",       resource,       resource_icon)

  -- ===== Layouts =====
  if isGuardian then
    -- ---- 3 Spalten: Haupt | Fortschritt (2 rows) | Aura ----
    local grid = content:tag("div"):addClass("kr-g3")

    -- Spalte 1: Hauptattribute
    local c1 = grid:tag("div"):addClass("kr-col")
    c1:tag("h3"):addClass("kr-hc-h"):wikitext("Hauptattribute")
    local mwrap = c1:tag("div"):addClass("kr-hc-main-attrs")
    local heroMain = type(data.main)=="table" and data.main or {}
    for i, key in ipairs(CFGmod.main_order) do
      local meta = CFGmod.main_meta[key]
      local val  = pick(args["main"..i.."_val"], heroMain[key])
      if not isempty(val) then
        pill(mwrap, meta.label, val, pick(args["main"..i.."_icon"], meta.icon), showLabels, pick(args["main"..i.."_desc"], meta.desc))
      end
    end

    -- Spalte 2: Fortschrittsattribute (nur ausgewählte)
    local c2 = grid:tag("div"):addClass("kr-col")
    c2:tag("h3"):addClass("kr-hc-h"):wikitext("Fortschrittsattribute")
    local awrap = c2:tag("div"):addClass("kr-hc-adv-attrs kr-adv-guardian")
    local heroAdv = type(data.adv)=="table" and data.adv or {}
    for idx, key in ipairs(advGuard) do
      local meta = CFGmod.adv_meta[key]
      if meta then
        adv(awrap, meta.label, pick(args["adv"..idx.."_val"], heroAdv[key]),
            pick(args["adv"..idx.."_icon"], meta.icon), showLabels,
            pick(args["adv"..idx.."_desc"], meta.desc))
      end
    end

    -- Spalte 3: Wächteraura
    local c3 = grid:tag("div"):addClass("kr-col")
    c3:tag("h3"):addClass("kr-hc-h"):wikitext(L.awakening)
    if not isempty(awakening_bonus) then
      local box = c3:tag("div"):addClass("kr-hc-adv kr-tip")
      addTooltip(box, awakening_bonus, awakening_desc)
      if awakening_bonus_icon then
        box:tag("div"):addClass("kr-hc-adv-icon")
           :wikitext(fileWikitext(awakening_bonus_icon, {size="64x64px", link=""}))
      end
      local main = box:tag("div"):addClass("kr-hc-adv-main")
      local lab  = main:tag("div"):addClass("kr-hc-adv-label"):wikitext(L.awakening)
      if not showLabels then lab:addClass("is-hidden") end
      main:tag("div"):addClass("kr-hc-adv-value"):wikitext(awakening_bonus)
    end

  else
    -- ---- 2×2 Quadranten: TL Haupt | TR Fortschritt | BL Fähigkeiten (leer) | BR Awakening ----
    local quad = content:tag("div"):addClass("kr-hero-quad")

    -- TL: Hauptattribute
    local q1 = quad:tag("div"):addClass("kr-quad")
    q1:tag("h3"):addClass("kr-hc-h"):wikitext("Hauptattribute")
    local mwrap = q1:tag("div"):addClass("kr-hc-main-attrs")
    local heroMain = type(data.main)=="table" and data.main or {}
    for i, key in ipairs(CFGmod.main_order) do
      local meta = CFGmod.main_meta[key]
      pill(mwrap, meta.label, pick(args["main"..i.."_val"], heroMain[key]),
           pick(args["main"..i.."_icon"], meta.icon), showLabels,
           pick(args["main"..i.."_desc"], meta.desc))
    end

    -- TR: Fortschrittsattribute
    local q2 = quad:tag("div"):addClass("kr-quad")
    q2:tag("h3"):addClass("kr-hc-h"):wikitext("Fortschrittsattribute")
    local awrap = q2:tag("div"):addClass("kr-hc-adv-attrs")
    local heroAdv = type(data.adv)=="table" and data.adv or {}
    for i, key in ipairs(CFGmod.adv_order) do
      local meta = CFGmod.adv_meta[key]
      adv(awrap, meta.label, pick(args["adv"..i.."_val"], heroAdv[key]),
          pick(args["adv"..i.."_icon"], meta.icon), showLabels,
          pick(args["adv"..i.."_desc"], meta.desc))
    end

    -- BL: Fähigkeiten (leer/optional)
    local q3 = quad:tag("div"):addClass("kr-quad")
    q3:tag("h3"):addClass("kr-hc-h"):wikitext("Fähigkeiten")
    -- Wenn später abilities vorliegen, hier rendern; jetzt leer halten für Layout-Stabilität
    q3:tag("div"):addClass("kr-hc-abilities-placeholder"):wikitext("")

    -- BR: Erweckungsbonus
    local q4 = quad:tag("div"):addClass("kr-quad")
    q4:tag("h3"):addClass("kr-hc-h"):wikitext(L.awakening)
    if not isempty(awakening_bonus) then
      local box = q4:tag("div"):addClass("kr-hc-adv kr-tip")
      addTooltip(box, awakening_bonus, awakening_desc)
      if awakening_bonus_icon then
        box:tag("div"):addClass("kr-hc-adv-icon")
           :wikitext(fileWikitext(awakening_bonus_icon, {size="64x64px", link=""}))
      end
      local main = box:tag("div"):addClass("kr-hc-adv-main")
      local lab  = main:tag("div"):addClass("kr-hc-adv-label"):wikitext(L.awakening)
      if not showLabels then lab:addClass("is-hidden") end
      main:tag("div"):addClass("kr-hc-adv-value"):wikitext(awakening_bonus)
    end
  end

  return tostring(root)
end

return p