Zum Inhalt springen


Modul:HeroCard: Unterschied zwischen den Versionen

Aus Firestone Idle RPG Wiki
Keine Bearbeitungszusammenfassung
Keine Bearbeitungszusammenfassung
Zeile 112: Zeile 112:


-- Baut den Avatar (Single-Bild oder TabberNeue mit Skins)
-- Baut den Avatar (Single-Bild oder TabberNeue mit Skins)
local function buildAvatarWikitext(name, linkto, defaultFile, skinsTable)
local function buildAvatar(name, linkto, defaultFile, skinsTable)
   -- kein Skins-Array/Tabelle → einfach das Standardbild zurückgeben
   -- kein Skins-Array → einfach Standardbild
   if type(skinsTable) ~= "table" or next(skinsTable) == nil then
   if type(skinsTable) ~= "table" or next(skinsTable) == nil then
     return fileWikitext(defaultFile, { class="kr-hc-avatar-img", link=linkto, lazy=true })
     return fileWikitext(defaultFile, { class="kr-hc-avatar-img", link=linkto, lazy=true })
   end
   end


   -- Label für den Standard-Tab
   local frame = mw.getCurrentFrame()
   local defaultLabel = (loadI18n().ui and loadI18n().ui.default_skin) or "Standard"
   local defaultLabel = "Standard"   -- ggf. aus I18n holen


   -- Standard-Tab zuerst
   -- Panels aufbauen
   local tabs = {
   local panels = {}
    defaultLabel .. "=\n" ..
    fileWikitext(defaultFile, { class="kr-hc-avatar-img", link=linkto, lazy=true })
  }
 
  -- Skins in eine geordnete Liste bringen (Liste oder Map erlaubt)
  local ordered = {}
   local function push(label, file)
   local function push(label, file)
     table.insert(ordered, { label = label, file = file })
     table.insert(panels,
      tostring(label) .. "=\n" ..
      fileWikitext(file, { class="kr-hc-avatar-img", link=linkto, lazy=true })
    )
   end
   end


   -- numerische Liste?
   -- Standard zuerst
   local isList = (skinsTable[1] ~= nil)
  push(defaultLabel, defaultFile)
  if isList then
 
     for i, v in ipairs(skinsTable) do
  -- Skins: Liste ODER Map
   if skinsTable[1] ~= nil then
    -- numerische Liste: {"Wild","Cheerleader"} oder { {label="…",file="…"} }
     for _, v in ipairs(skinsTable) do
       if type(v) == "table" then
       if type(v) == "table" then
         local label = v.label or v[1] or tostring(i)
         local label = v.label or v[1]
         local file  = v.file  or (name .. "_" .. (v.filename or label) .. ".png")
         local file  = v.file  or (name .. "_" .. (v.filename or label) .. ".png")
         push(label, file)
         push(label, file)
Zeile 147: Zeile 147:
     end
     end
   else
   else
     -- Map (key = suffix, value = Label ODER Objekt)
     -- Map: { wild="Wild", cheerleader="Cheerleader" } oder { wild={label="…",file="…"} }
    local tmp = {}
     for k, v in pairs(skinsTable) do
     for k, v in pairs(skinsTable) do
       if type(v) == "table" then
       if type(v) == "table" then
         local label = v.label or v[1] or tostring(k)
         local label = v.label or v[1] or tostring(k)
         local file  = v.file  or (name .. "_" .. (v.filename or label) .. ".png")
         local file  = v.file  or (name .. "_" .. (v.filename or label) .. ".png")
         push(label, file)
         table.insert(tmp, {label=label, file=file})
       else
       else
         local label = tostring(v)
         local label = tostring(v)
         push(label, name .. "_" .. label .. ".png")
         table.insert(tmp, {label=label, file=(name .. "_" .. label .. ".png")})
       end
       end
     end
     end
    -- stabile Reihenfolge: nach Label sortieren
     table.sort(tmp, function(a,b) return tostring(a.label) < tostring(b.label) end)
     table.sort(ordered, function(a,b) return tostring(a.label) < tostring(b.label) end)
    for _,it in ipairs(tmp) do push(it.label, it.file) end
  end
 
  for _, it in ipairs(ordered) do
    table.insert(tabs,
      it.label .. "=\n" ..
      fileWikitext(it.file, { class="kr-hc-avatar-img", link=linkto, lazy=true })
    )
   end
   end


   -- TabberNeue-Markup ausgeben
   local content = table.concat(panels, "\n|-|\n")
  return "<tabber>\n" .. table.concat(tabs, "\n|-|\n") .. "\n</tabber>"
  return frame:extensionTag('tabber', content)  -- <— WICHTIG
end
end


Zeile 216: Zeile 210:
   -- Avatar (links)
   -- Avatar (links)
   local avatarDiv = root:tag("div"):addClass("kr-hc-avatar")
   local avatarDiv = root:tag("div"):addClass("kr-hc-avatar")
   avatarDiv:wikitext(buildAvatarWikitext(name, linkto, avatar, data.skins))
   avatarDiv:wikitext( buildAvatar(name, linkto, avatar, data.skins) )


   local content = root:tag("div"):addClass("kr-hc-content")
   local content = root:tag("div"):addClass("kr-hc-content")

Version vom 9. Oktober 2025, 13:28 Uhr

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 der ausgelagerten Module
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 = {} }; 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

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
  if opts and opts.lazy  then table.insert(parts, "loading=lazy") end
  return string.format("[[%s]]", table.concat(parts, "|"))
end

-- Übersetzen per HeroI18n
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

-- Tooltip-HTML (Dark-Box; Pfeil wird per CSS entfernt)
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="", lazy=true})) 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="", lazy=true})) 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

-- Unlock-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 auf dem Piratenschiff freigeschaltet." end
  if s:match("[%.%!%?]$") or t:find(" ") then return s end
  return "Freischaltung: " .. s
end

-- Awakening-Desc: NUR englische Keys (wie in HeroData)
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

-- Baut den Avatar (Single-Bild oder TabberNeue mit Skins)
local function buildAvatar(name, linkto, defaultFile, skinsTable)
  -- kein Skins-Array → einfach Standardbild
  if type(skinsTable) ~= "table" or next(skinsTable) == nil then
    return fileWikitext(defaultFile, { class="kr-hc-avatar-img", link=linkto, lazy=true })
  end

  local frame = mw.getCurrentFrame()
  local defaultLabel = "Standard"   -- ggf. aus I18n holen

  -- Panels aufbauen
  local panels = {}
  local function push(label, file)
    table.insert(panels,
      tostring(label) .. "=\n" ..
      fileWikitext(file, { class="kr-hc-avatar-img", link=linkto, lazy=true })
    )
  end

  -- Standard zuerst
  push(defaultLabel, defaultFile)

  -- Skins: Liste ODER Map
  if skinsTable[1] ~= nil then
    -- numerische Liste: {"Wild","Cheerleader"} oder { {label="…",file="…"} }
    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
    -- Map: { wild="Wild", cheerleader="Cheerleader" } oder { wild={label="…",file="…"} }
    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)   -- <— WICHTIG
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"

  -- 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)
  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 icon_map = CFGmod.icon_map

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

  -- Bilder/Links
  local linkto = pick(args.linkto, name)
  local avatar = pick(args.avatar, (not isempty(name) and (name .. ".png")) or nil)

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

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

  local content = root:tag("div"):addClass("kr-hc-content")
  if not isempty(name) then
    local h = content:tag("h2"):addClass("kr-hc-name")
    local unlock_text = unlock_text_de(unlock_at)
    h:wikitext(string.format("[[%s|%s]]%s", linkto, name,
      unlock_text and (" <span class=\"kr-hc-unlock\">(" .. unlock_text .. ")</span>") or ""))
  end

  -- Basics 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="", lazy=true})) end
  end
  pair("Klasse:",          class,          nil)
  pair("Spezialisierung:", specialization, specialization_icon)
  pair("Angriffsart:",     attackstyle,    attackstyle_icon)
  pair("Ressource:",       resource,       resource_icon)

  -- Hauptattribute
  content:tag("h3"):addClass("kr-hc-h"):wikitext("Hauptattribute")
  local mains = content: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(mains, 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

  -- Erweiterte Attribute
  content:tag("h3"):addClass("kr-hc-h"):wikitext("Erweiterte Attribute")
  local advs = content: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(advs, 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
  for i=#CFGmod.adv_order+1,8 do
    local val=args["adv"..i.."_val"]; if isempty(val) then break end
    adv(advs, args["adv"..i.."_label"] or "", val, args["adv"..i.."_icon"], showLabels, args["adv"..i.."_desc"])
  end

  -- Erweckungsbonus – Tooltip-Titel = übersetzter Bonusname
  if not isempty(awakening_bonus) then
    content:tag("h3"):addClass("kr-hc-h"):wikitext("Erweckungsbonus")
    local awk = content:tag("div"):addClass("kr-hc-awakening")
    local box = awk: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="", lazy=true}))
    end
    local main = box:tag("div"):addClass("kr-hc-adv-main")
    local lab = main:tag("div"):addClass("kr-hc-adv-label"):wikitext("Erweckungsbonus")
    main:tag("div"):addClass("kr-hc-adv-value"):wikitext(awakening_bonus)
    -- Labels standardmäßig ausgeblendet
    if not showLabels then lab:addClass("is-hidden") end
  end

  return tostring(root)
end

return p