Modul:Firestone/Hero
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 = {}
local function loadTable(name)
local ok, data = pcall(mw.loadData, name)
if ok and type(data) == 'table' then return data end
ok, data = pcall(require, name)
if ok and type(data) == 'table' then return data end
return {}
end
local function mergedHeroes()
local all = {}
local sources = {
'Modul:Firestone/Data/Heroes',
'Modul:Firestone/Data/Guardians',
'Modul:Firestone/Data/WM',
}
for _,name in ipairs(sources) do
local t = loadTable(name)
for k,v in pairs(t) do
all[k] = v
end
end
return all
end
local HEROES = mergedHeroes()
local function autotr(key_name, value)
if type(value) ~= "string" then return value end
local k = Util.norm(key_name)
local i18 = I18n.get()
local m1 = (i18.i18n or {})[k]
local v = m1 and m1[Util.norm(value)]
return v or value
end
function H.handle(frame, args)
local name = args[2]
if Util.isEmpty(name) then
return ""
end
-- passenden Datensatz finden (Case-insensitive usw.)
local realKey = Util.pickKey(HEROES, name)
if not realKey then
-- nichts gefunden → wenn user nur {{Firestone|hero|X}} schreibt, gib X zurück
return name
end
local hero = HEROES[realKey]
-- wenn nur {{Firestone|hero|Talia}} → gib den Key aus
if Util.isEmpty(args[3]) then
return realKey
end
-- ab hier: Pfad wie gehabt
local path, i = {}, 3
while args[i] do
table.insert(path, args[i])
i = i + 1
end
local v = Util.deepGet(hero, path)
local last = path[#path] and tostring(path[#path]) or ""
-- FALLBACK 1:
-- {{Firestone|hero|Talia|name}} → wenn im Datensatz KEIN name steht,
-- nimm einfach den Key.
if v == nil and Util.norm(last) == 'name' then
return realKey
end
-- auto-translate für vorhandene Werte
v = autotr(last, v)
-- FALLBACK 2:
-- falls aus irgendeinem Grund immer noch nil, gib lieber den Key als "table" aus
if v == nil then
return ""
end
return tostring(v)
end
return H