Modul:Firestone/Hero: Unterschied zwischen den Versionen
Keine Bearbeitungszusammenfassung |
Keine Bearbeitungszusammenfassung |
||
| Zeile 5: | Zeile 5: | ||
local H = {} | local H = {} | ||
-- | -- Helden immer frisch laden | ||
local | local function getHeroes() | ||
return Util.loadTable('Modul:Firestone/Data/Heroes') or {} | |||
end | |||
-- I18n holen | -- I18n holen (wie bei dir) | ||
local function getI18n() | local function getI18n() | ||
if type(I18n) == "table" and type(I18n.get) == "function" then | if type(I18n) == "table" and type(I18n.get) == "function" then | ||
| Zeile 19: | Zeile 21: | ||
end | end | ||
-- Skins | -- Skins normalisieren (dein Code) | ||
local function normalizeSkins(skins) | local function normalizeSkins(skins) | ||
local list = {} | local list = {} | ||
| Zeile 25: | Zeile 27: | ||
return list | return list | ||
end | end | ||
local numeric = {} | local numeric = {} | ||
for k, v in pairs(skins) do | for k,v in pairs(skins) do | ||
if type(k) == "number" then | if type(k) == "number" then | ||
numeric[k] = v | numeric[k] = v | ||
| Zeile 35: | Zeile 35: | ||
end | end | ||
end | end | ||
local idx = {} | local idx = {} | ||
for k in pairs(numeric) do | for k in pairs(numeric) do | ||
| Zeile 41: | Zeile 40: | ||
end | end | ||
table.sort(idx) | table.sort(idx) | ||
for _, k in ipairs(idx) do | for _,k in ipairs(idx) do | ||
list[#list+1] = numeric[k] | list[#list+1] = numeric[k] | ||
end | end | ||
return list | return list | ||
end | end | ||
-- Avatar bauen (dein Tabber-Weg) | |||
-- Avatar | |||
local function buildAvatarMarkup(hero, realKey, i18) | local function buildAvatarMarkup(hero, realKey, i18) | ||
local frame = mw.getCurrentFrame() | local frame = mw.getCurrentFrame() | ||
| Zeile 56: | Zeile 52: | ||
local skins = normalizeSkins(hero.skins) | local skins = normalizeSkins(hero.skins) | ||
if #skins == 0 then | if #skins == 0 then | ||
return string.format( | return string.format( | ||
"[[Datei:%s.png|210px|class=avatar-img|link=%s]]", | "[[Datei:%s.png|210px|class=avatar-img|link=%s]]", | ||
realKey, | realKey, realKey | ||
) | ) | ||
end | end | ||
local panels = {} | local panels = {} | ||
local heroDisplayName = hero.name or realKey | local heroDisplayName = hero.name or realKey | ||
panels[#panels+1] = | panels[#panels+1] = | ||
| Zeile 74: | Zeile 66: | ||
string.format("[[Datei:%s.png|210px|class=avatar-img|link=%s]]", realKey, realKey) | string.format("[[Datei:%s.png|210px|class=avatar-img|link=%s]]", realKey, realKey) | ||
for _,id in ipairs(skins) do | |||
for _, id in ipairs(skins) do | |||
local idNorm = Util.norm(id) | local idNorm = Util.norm(id) | ||
local label = skinLabels[idNorm] or id | local label = skinLabels[idNorm] or id | ||
local filename = string.format("%s_%s.png", realKey, id) | local filename = string.format("%s_%s.png", realKey, id) | ||
panels[#panels+1] = | panels[#panels+1] = | ||
label .. "=\n" .. | label .. "=\n" .. | ||
| Zeile 89: | Zeile 79: | ||
end | end | ||
-- UI | -- UI: {{Firestone|hero|ui|...}} | ||
function H.ui(frame, args) | function H.ui(frame, args) | ||
local i18 = getI18n() | local i18 = getI18n() | ||
local uiRoot = i18.ui or {} | local uiRoot = i18.ui or {} | ||
local path = {} | local path = {} | ||
for i = 3, #args do | for i = 3, #args do | ||
path[#path+1] = args[i] | path[#path+1] = args[i] | ||
end | end | ||
local val = Util.deepGet(uiRoot, path) | local val = Util.deepGet(uiRoot, path) | ||
if type(val) == | if type(val) == "string" then | ||
return mw.text.trim(val) | return mw.text.trim(val) | ||
end | end | ||
return | return "" | ||
end | end | ||
-- | -- LISTE: {{Firestone|hero|list}} | ||
function H.list(frame, args) | function H.list(frame, args) | ||
local heroes = getHeroes() | |||
local iconSize = args.icon_size or args.size or "32" | local iconSize = args.icon_size or args.size or "32" | ||
if not next(heroes) then | |||
if not next( | |||
return "<div class='error'>[Hero.list] keine Helden in Modul:Firestone/Data/Heroes gefunden.</div>" | return "<div class='error'>[Hero.list] keine Helden in Modul:Firestone/Data/Heroes gefunden.</div>" | ||
end | end | ||
-- | -- sortierte Namen | ||
local names = {} | local names = {} | ||
for name in pairs( | for name in pairs(heroes) do | ||
names[#names+1] = name | names[#names+1] = name | ||
end | end | ||
table.sort(names) | table.sort(names) | ||
local panels = {} | local panels = {} | ||
for _,name in ipairs(names) do | |||
for _, name in ipairs(names) do | |||
local ok, card = pcall(function() | local ok, card = pcall(function() | ||
return frame:expandTemplate{ | return frame:expandTemplate{ | ||
title = 'Card/Hero', | title = 'Card/Hero', | ||
args = { | args = { | ||
[1] | [1] = name, | ||
icon_size | icon_size = iconSize, | ||
}, | }, | ||
} | } | ||
end) | end) | ||
if not ok or not card or card == "" then | |||
if not ok or not card or card == | |||
card = "<div class='error'>[Hero.list] Karte für „" .. name .. "“ nicht gefunden.</div>" | card = "<div class='error'>[Hero.list] Karte für „" .. name .. "“ nicht gefunden.</div>" | ||
end | end | ||
panels[#panels+1] = name .. "=\n" .. card | panels[#panels+1] = name .. "=\n" .. card | ||
end | end | ||
local content = table.concat(panels, "\n|-|\n") | local content = table.concat(panels, "\n|-|\n") | ||
return frame:extensionTag('tabber', content) | return frame:extensionTag('tabber', content) | ||
end | end | ||
-- normaler Held | -- normaler Held | ||
function H.handle(frame, args) | function H.handle(frame, args) | ||
local name = args[2] | local name = args[2] | ||
| Zeile 158: | Zeile 138: | ||
end | end | ||
local heroes = getHeroes() | |||
local realKey = Util.pickKey( | local realKey = Util.pickKey(heroes, name) | ||
if not realKey then | if not realKey then | ||
return name | return name | ||
end | end | ||
local hero = | local hero = heroes[realKey] | ||
local i18 = getI18n() | local i18 = getI18n() | ||
-- nur {{Firestone|hero|Talia}} | -- nur {{Firestone|hero|Talia}} | ||
if Util.isEmpty(args[3]) then | if Util.isEmpty(args[3]) then | ||
return realKey | return realKey | ||
end | end | ||
if Util.norm(args[3]) == "avatar" then | if Util.norm(args[3]) == "avatar" then | ||
return buildAvatarMarkup(hero, realKey, i18) | return buildAvatarMarkup(hero, realKey, i18) | ||
end | end | ||
local path, i = {}, 3 | local path, i = {}, 3 | ||
while args[i] do | while args[i] do | ||
| Zeile 202: | Zeile 180: | ||
return "" | return "" | ||
end | end | ||
return tostring(v) | return tostring(v) | ||
end | end | ||
return H | return H | ||
Version vom 12. November 2025, 13:46 Uhr
Die Dokumentation für dieses Modul kann unter Modul:Firestone/Hero/Doku erstellt werden
-- Modul:Firestone/Hero
local Util = require('Modul:Firestone/Util')
local I18n = require('Modul:Firestone/I18n')
local H = {}
-- Helden immer frisch laden
local function getHeroes()
return Util.loadTable('Modul:Firestone/Data/Heroes') or {}
end
-- I18n holen (wie bei dir)
local function getI18n()
if type(I18n) == "table" and type(I18n.get) == "function" then
local ok, data = pcall(I18n.get)
if ok and type(data) == "table" then
return data
end
end
return I18n or {}
end
-- Skins normalisieren (dein Code)
local function normalizeSkins(skins)
local list = {}
if type(skins) ~= "table" then
return list
end
local numeric = {}
for k,v in pairs(skins) do
if type(k) == "number" then
numeric[k] = v
else
list[#list+1] = v
end
end
local idx = {}
for k in pairs(numeric) do
idx[#idx+1] = k
end
table.sort(idx)
for _,k in ipairs(idx) do
list[#list+1] = numeric[k]
end
return list
end
-- Avatar bauen (dein Tabber-Weg)
local function buildAvatarMarkup(hero, realKey, i18)
local frame = mw.getCurrentFrame()
local skinLabels = ((i18.ui or {}).skins) or {}
local skins = normalizeSkins(hero.skins)
if #skins == 0 then
return string.format(
"[[Datei:%s.png|210px|class=avatar-img|link=%s]]",
realKey, realKey
)
end
local panels = {}
local heroDisplayName = hero.name or realKey
panels[#panels+1] =
heroDisplayName .. "=\n" ..
string.format("[[Datei:%s.png|210px|class=avatar-img|link=%s]]", realKey, realKey)
for _,id in ipairs(skins) do
local idNorm = Util.norm(id)
local label = skinLabels[idNorm] or id
local filename = string.format("%s_%s.png", realKey, id)
panels[#panels+1] =
label .. "=\n" ..
string.format("[[Datei:%s|210px|class=avatar-img|link=%s]]", filename, realKey)
end
local content = table.concat(panels, "\n|-|\n")
return frame:extensionTag('tabber', content)
end
-- UI: {{Firestone|hero|ui|...}}
function H.ui(frame, args)
local i18 = getI18n()
local uiRoot = i18.ui or {}
local path = {}
for i = 3, #args do
path[#path+1] = args[i]
end
local val = Util.deepGet(uiRoot, path)
if type(val) == "string" then
return mw.text.trim(val)
end
return ""
end
-- LISTE: {{Firestone|hero|list}}
function H.list(frame, args)
local heroes = getHeroes()
local iconSize = args.icon_size or args.size or "32"
if not next(heroes) then
return "<div class='error'>[Hero.list] keine Helden in Modul:Firestone/Data/Heroes gefunden.</div>"
end
-- sortierte Namen
local names = {}
for name in pairs(heroes) do
names[#names+1] = name
end
table.sort(names)
local panels = {}
for _,name in ipairs(names) do
local ok, card = pcall(function()
return frame:expandTemplate{
title = 'Card/Hero',
args = {
[1] = name,
icon_size = iconSize,
},
}
end)
if not ok or not card or card == "" then
card = "<div class='error'>[Hero.list] Karte für „" .. name .. "“ nicht gefunden.</div>"
end
panels[#panels+1] = name .. "=\n" .. card
end
local content = table.concat(panels, "\n|-|\n")
return frame:extensionTag('tabber', content)
end
-- normaler Held
function H.handle(frame, args)
local name = args[2]
if Util.isEmpty(name) then
return ""
end
local heroes = getHeroes()
local realKey = Util.pickKey(heroes, name)
if not realKey then
return name
end
local hero = heroes[realKey]
local i18 = getI18n()
-- nur {{Firestone|hero|Talia}}
if Util.isEmpty(args[3]) then
return realKey
end
if Util.norm(args[3]) == "avatar" then
return buildAvatarMarkup(hero, realKey, i18)
end
local path, i = {}, 3
while args[i] do
path[#path+1] = args[i]
i = i + 1
end
local v = Util.deepGet(hero, path)
local last = path[#path] and tostring(path[#path]) or ""
local nlast = Util.norm(last)
if nlast == "unlock_at" then
return Util.heroUnlockText(v, i18)
elseif nlast == "awakening_id" then
return Util.heroAwakeningText(v, i18)
end
if v == nil and nlast == "name" then
return realKey
end
v = Util.autotrFrom(i18, last, v)
if v == nil then
return ""
end
return tostring(v)
end
return H