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 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

-- ======= 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

  root:tag("div"):addClass("kr-hc-avatar")
      :wikitext(fileWikitext(avatar, { class="kr-hc-avatar-img", link=linkto, lazy=true }))

  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