Modul:HeroCard
Die Dokumentation für dieses Modul kann unter Modul:HeroCard/Doku erstellt werden
-- Modul:HeroCard
local p = {}
-- ======= CONFIG / DEFAULTS =======
local CFG = {
-- Reihenfolge + Labels/Standard-Icons für Haupt-/Erweiterte Attribute
main_order = { "damage", "hp", "armor" },
main_meta = {
damage = { label = "Damage", icon = "AttackIcon.png" },
hp = { label = "Health", icon = "HealthIcon.png" },
armor = { label = "Armor", icon = "ArmorIcon.png" },
},
adv_order = { "precision", "bonus_damage", "speed", "dodge" },
adv_meta = {
precision = { label = "Precision", icon = "Icon_Target.png" },
bonus_damage = { label = "Damage Bonus", icon = "Icon_DamageUp.png" },
speed = { label = "Speed", icon = "Icon_Boots.png" },
dodge = { label = "Dodge", icon = "Icon_Dodge.png" },
},
-- Icon-Mapping nach Wert (case-/whitespace-insensitiv)
icon_map = {
attackstyle = {
malee = "MeleeIcon.png",
spellcaster = "SpellcasterIcon.png",
ranged = "RangedIcon.png",
},
specialization = {
healer = "HealerIcon.png",
damage = "DamageIcon.png",
tank = "TankIcon.png",
},
resource = {
mana = "ManaIcon.png",
rage = "RageIcon.png",
energy = "EnergyIcon.png",
},
awakening_bonus = {
["raining gold"] = "RainingGold.png",
["all main attributes"] = "AllAttributes.png",
prestigious = "Prestigious.png",
}
}
}
-- ======= HELPERS =======
local function isempty(v) return v == nil or v == "" end
local function pick(...)
local n = select('#', ...) -- echte Anzahl der Argumente (inkl. nil)
for i = 1, n do
local v = select(i, ...)
if not (v == nil or v == "") then
return v
end
end
return nil
end
local function norm(s)
if isempty(s) then return "" end
s = mw.ustring.lower(mw.text.trim(s))
s = s:gsub("%s+", " ")
return s
end
local function getArgs(frame)
local args = {}
local 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
-- Versucht beide Namensräume (de/en) zu laden und gibt die Heroes-Tabelle zurück.
local function loadHeroes()
local tried, lastErr = {}, nil
for _,title in ipairs{ "Modul:HeroData", "Module:HeroData" } do
local ok, mod = pcall(require, title)
if ok and type(mod) == "table" and type(mod.heroes) == "table" then
return mod.heroes, { ok=true, from=title }
else
table.insert(tried, title)
lastErr = mod
end
end
return nil, { ok=false, tried=tried, err=lastErr }
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
return string.format("[[%s]]", table.concat(parts, "|"))
end
local function mapIcon(kind, value, override)
if not isempty(override) then return override end
local t = CFG.icon_map[kind] or {}
return t[norm(value)]
end
local function pill(htmlParent, label, val, icon)
if isempty(val) then return end
local box = htmlParent:tag("div"):addClass("kr-hc-pill")
local left = box:tag("div"):addClass("kr-hc-pill-icon")
if not isempty(icon) then left:wikitext(fileWikitext(icon, { size="24x24px", link="" })) end
local text = box:tag("div"):addClass("kr-hc-pill-text")
text:tag("div"):addClass("kr-hc-pill-label"):wikitext(label or "")
text:tag("div"):addClass("kr-hc-pill-value"):wikitext(val)
end
local function adv(htmlParent, label, val, icon)
if isempty(val) then return end
local box = htmlParent:tag("div"):addClass("kr-hc-adv")
local left = box:tag("div"):addClass("kr-hc-adv-icon")
if not isempty(icon) then left:wikitext(fileWikitext(icon, { size="24x24px", link="" })) end
local main = box:tag("div"):addClass("kr-hc-adv-main")
main:tag("div"):addClass("kr-hc-adv-label"):wikitext(label or "")
main:tag("div"):addClass("kr-hc-adv-value"):wikitext(val)
end
-- ======= RENDER =======
function p.render(frame)
local args = getArgs(frame)
local name = pick(args.name, args[1]) or ""
local heroes, info = loadHeroes()
local data = (heroes and heroes[name]) or (heroes and heroes[mw.ustring.gsub(name or "", "_", " ")]) or {}
local debugWanted = norm(args.debug) == "1" or norm(args.debug) == "yes"
-- Basics (Werte)
local class = pick(args.class, data.class)
local attackstyle = pick(args.attackstyle, data.attackstyle)
local specialization = pick(args.specialization, data.specialization)
local resource = pick(args.resource, data.resource)
local unlock_at = pick(args.unlock_at, data.unlock_at)
local awakening_bonus= pick(args.awakening_bonus,data.awakening_bonus)
-- Icons (kein Icon für class)
local attackstyle_icon = mapIcon("attackstyle", attackstyle, pick(args.attackstyle_icon, data.attackstyle_icon))
local specialization_icon = mapIcon("specialization", specialization, pick(args.specialization_icon, data.specialization_icon))
local resource_icon = mapIcon("resource", resource, pick(args.resource_icon, data.resource_icon))
local awakening_bonus_icon= mapIcon("awakening_bonus",awakening_bonus,pick(args.awakening_bonus_icon,data.awakening_bonus_icon))
-- Bilder/Links
local linkto = pick(args.linkto, name)
local avatar = pick(args.avatar, (not isempty(name) and (name .. ".png")) or nil)
local root = mw.html.create("div"):addClass("kr-hero-card")
-- Avatar (links)
local fig = root:tag("div"):addClass("kr-hc-avatar")
fig:wikitext(fileWikitext(avatar, { class="kr-hc-avatar-img", link=linkto }))
-- Content (rechts)
local content = root:tag("div"):addClass("kr-hc-content")
if not isempty(name) then content:tag("h2"):addClass("kr-hc-name"):wikitext(name) end
-- Wenn das Datenmodul nicht geladen werden konnte, optional Debug-Hinweis
if (not info or not info.ok) and debugWanted then
content:tag("div"):addClass("kr-hc-debug")
:wikitext("<strong>HeroData not loaded.</strong> Tried: "
.. mw.text.nowiki(table.concat((info and info.tried) or {}, ", ")))
end
-- Basics
local basics = content:tag("div"):addClass("kr-hc-basics")
local function row(label, value, icon)
if isempty(value) then return end
basics:tag("div"):addClass("kr-hc-kv kr-hc-k"):wikitext(label)
local cell = basics:tag("div"):addClass("kr-hc-kv kr-hc-v")
if not isempty(icon) then cell:tag("span"):addClass("kr-hc-v-icon"):wikitext(fileWikitext(icon, { size="20x20px", link="" })) end
cell:wikitext(value)
end
row("Class:", class, nil)
row("Attack style:", attackstyle, attackstyle_icon)
row("Specialization:", specialization, specialization_icon)
row("Resource:", resource, resource_icon)
row("Unlock at:", unlock_at, nil)
-- Main attributes
content:tag("h3"):addClass("kr-hc-h"):wikitext("Main attributes")
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(CFG.main_order) do
local meta = CFG.main_meta[key]
local val = pick(args["main"..i.."_val"], heroMain[key])
local label = pick(args["main"..i.."_label"], meta.label)
local icon = pick(args["main"..i.."_icon"], meta.icon)
pill(mains, label, val, icon)
end
-- Advanced attributes
content:tag("h3"):addClass("kr-hc-h"):wikitext("Advanced attributes")
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(CFG.adv_order) do
local meta = CFG.adv_meta[key]
local val = pick(args["adv"..i.."_val"], heroAdv[key])
local label = pick(args["adv"..i.."_label"], meta.label)
local icon = pick(args["adv"..i.."_icon"], meta.icon)
adv(advs, label, val, icon)
end
for i = #CFG.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"])
end
-- Awakening bonus
if not isempty(awakening_bonus) then
content:tag("h3"):addClass("kr-hc-h"):wikitext("Awakening bonus")
local awk = content:tag("div"):addClass("kr-hc-awakening")
local box = awk:tag("div"):addClass("kr-hc-adv")
if not isempty(awakening_bonus_icon) then
box:tag("div"):addClass("kr-hc-adv-icon")
:wikitext(fileWikitext(awakening_bonus_icon, { size="24x24px", link="" }))
end
local main = box:tag("div"):addClass("kr-hc-adv-main")
main:tag("div"):addClass("kr-hc-adv-label"):wikitext("Bonus")
main:tag("div"):addClass("kr-hc-adv-value"):wikitext(awakening_bonus)
end
-- Extra-Debug
if debugWanted then
content:tag("pre"):addClass("kr-hc-debug")
:wikitext(mw.text.nowiki(mw.dumpObject({
name = name,
dataFound = (heroes and heroes[name]) ~= nil,
heroesModule = info,
data = data
})))
end
return tostring(root)
end
-- Selbsttest: zeigt, ob HeroData gefunden wird und listet Heldennamen
function p.selftest()
local heroes, info = loadHeroes()
local out = mw.html.create("div")
if not (info and info.ok) then
out:tag("p"):wikitext("HeroData NOT loaded. Tried: " .. mw.text.nowiki(table.concat((info and info.tried) or {}, ", ")))
return tostring(out)
end
out:tag("p"):wikitext("HeroData loaded from: " .. info.from)
local ul = out:tag("ul")
for name,_ in pairs(heroes) do ul:tag("li"):wikitext(name) end
return tostring(out)
end
return p