Zum Inhalt springen
Das Halloween-Event (Süßes oder Saures) ist beendet. Du kannst verbleibende Kürbisse noch im Hexenhütte eintauschen.

Modul:Firestone/Hero: Unterschied zwischen den Versionen

Aus Firestone Idle Rpg Wiki
Keine Bearbeitungszusammenfassung
Keine Bearbeitungszusammenfassung
 
(6 dazwischenliegende Versionen desselben Benutzers werden nicht angezeigt)
Zeile 5: Zeile 5:
local H = {}
local H = {}


local function loadTable(name)
-- alle Heldentabellen zusammenziehen
     local ok, data = pcall(mw.loadData, name)
local HEROES = Util.mergeSources{
     if ok and type(data) == 'table' then return data end
    'Modul:Firestone/Data/Heroes',
    ok, data = pcall(require, name)
    'Modul:Firestone/Data/Guardians',
    if ok and type(data) == 'table' then return data end
     'Modul:Firestone/Data/WM',
     return {}
}
 
-- I18n holen
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
end


local function mergedHeroes()
-- ========= FIX: Skins aus mw.loadData sauber einsammeln =========
     local all = {}
-- erlaubt sowohl { "Wild", "Cheerleader" }
     local sources = {
-- als auch { [1] = "Wild", [2] = "Cheerleader" }
         'Modul:Firestone/Data/Heroes',
-- und auch { wild = "Wild" } usw.
        'Modul:Firestone/Data/Guardians',
local function normalizeSkins(skins)
        'Modul:Firestone/Data/WM',
     local list = {}
     }
     if type(skins) ~= "table" then
     for _,name in ipairs(sources) do
         return list
         local t = loadTable(name)
    end
        for k,v in pairs(t) do
 
             all[k] = v
    -- numerische Keys erstmal getrennt sammeln (weil #skins bei loadData oft 0 ist)
     local numeric = {}
     for k, v in pairs(skins) do
         if type(k) == "number" then
            numeric[k] = v
        else
            -- z.B. skins = { wild = "Wild" }
             list[#list+1] = v
         end
         end
     end
     end
     return all
 
    -- numerische Keys sortieren, damit 1,2,3 in richtiger Reihenfolge kommen
    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
end


local HEROES = mergedHeroes()
-- ========================================================================
-- Avatar/Skins: {{Firestone|hero|Talia|avatar}}
-- default:  Talia.png
-- extra:    Talia_Wild.png, Talia_Cheerleader.png ...
-- Labels:    aus i18.ui.skins
-- ========================================================================
local function buildAvatarMarkup(hero, realKey, i18)
    local frame = mw.getCurrentFrame()
 
    local skinLabels = ((i18.ui or {}).skins) or {}
    local skins      = normalizeSkins(hero.skins)
 
    -- FALL: keine Skins → nur Standardbild
    if #skins == 0 then
        return string.format(
            "[[Datei:%s.png|210px|class=avatar-img|link=%s]]",
            realKey,
            realKey
        )
    end
 
    -- wir müssen Tabber über extensionTag ausgeben,
    -- sonst zeigt er nur den Wikitext an
    local panels = {}
 
    -- 1) Default-Tab = Heldenname
    local heroDisplayName = hero.name or realKey
    panels[#panels+1] =
        heroDisplayName .. "=\n" ..
        string.format("[[Datei:%s.png|210px|class=avatar-img|link=%s]]", realKey, realKey)


local function autotr(key_name, value)
    -- 2) alle Skins
    if type(value) ~= "string" then return value end
    for _, id in ipairs(skins) do
     local k = Util.norm(key_name)
        local idNorm  = Util.norm(id)
     local i18 = I18n.get()
        local label    = skinLabels[idNorm] or id
     local m1 = (i18.i18n or {})[k]
        local filename = string.format("%s_%s.png", realKey, id)
     local = m1 and m1[Util.norm(value)]
 
     return v or value
        panels[#panels+1] =
            label .. "=\n" ..
            string.format("[[Datei:%s|210px|class=avatar-img|link=%s]]", filename, realKey)
    end
 
    -- jetzt mit extensionTag wrappen
     local content = table.concat(panels, "\n|-|\n")
    return frame:extensionTag('tabber', content)
end
 
-- UI-Zweig: {{Firestone|hero|ui|basics|class}}
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
end


-- normaler Held: {{Firestone|hero|Talia|...}}
function H.handle(frame, args)
function H.handle(frame, args)
     local name = args[2]
     local name = args[2]
Zeile 46: Zeile 129:
     end
     end


    -- passenden Datensatz finden (Case-insensitive usw.)
     local realKey = Util.pickKey(HEROES, name)
     local realKey = Util.pickKey(HEROES, name)
     if not realKey then
     if not realKey then
        -- nichts gefunden → wenn user nur {{Firestone|hero|X}} schreibt, gib X zurück
         return name
         return name
     end
     end
     local hero = HEROES[realKey]
     local hero = HEROES[realKey]
    local i18  = getI18n()


     -- wenn nur {{Firestone|hero|Talia}} → gib den Key aus
     -- nur {{Firestone|hero|Talia}} → Key
     if Util.isEmpty(args[3]) then
     if Util.isEmpty(args[3]) then
         return realKey
         return realKey
     end
     end


     -- ab hier: Pfad wie gehabt
     -- Avatar?
    if Util.norm(args[3]) == "avatar" then
        return buildAvatarMarkup(hero, realKey, i18)
    end
 
    -- restlicher Pfad
     local path, i = {}, 3
     local path, i = {}, 3
     while args[i] do
     while args[i] do
         table.insert(path, args[i])
         path[#path+1] = args[i]
         i = i + 1
         i = i + 1
     end
     end


     local v = Util.deepGet(hero, path)
     local v     = Util.deepGet(hero, path)
     local last = path[#path] and tostring(path[#path]) or ""
     local last = path[#path] and tostring(path[#path]) or ""
    local nlast = Util.norm(last)


     -- FALLBACK 1:
     if nlast == "unlock_at" then
     -- {{Firestone|hero|Talia|name}} → wenn im Datensatz KEIN name steht,
        return Util.heroUnlockText(v, i18)
     -- nimm einfach den Key.
     elseif nlast == "awakening_id" then
     if v == nil and Util.norm(last) == 'name' then
        return Util.heroAwakeningText(v, i18)
     end
 
     if v == nil and nlast == "name" then
         return realKey
         return realKey
     end
     end


    -- auto-translate für vorhandene Werte
     v = Util.autotrFrom(i18, last, v)
     v = autotr(last, v)
 
    -- FALLBACK 2:
    -- falls aus irgendeinem Grund immer noch nil, gib lieber den Key als "table" aus
     if v == nil then
     if v == nil then
         return ""
         return ""
     end
     end
     return tostring(v)
     return tostring(v)
end
end


return H
return H

Aktuelle Version vom 8. November 2025, 18:17 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 = {}

-- alle Heldentabellen zusammenziehen
local HEROES = Util.mergeSources{
    'Modul:Firestone/Data/Heroes',
    'Modul:Firestone/Data/Guardians',
    'Modul:Firestone/Data/WM',
}

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

-- ========= FIX: Skins aus mw.loadData sauber einsammeln =========
-- erlaubt sowohl { "Wild", "Cheerleader" }
-- als auch { [1] = "Wild", [2] = "Cheerleader" }
-- und auch { wild = "Wild" } usw.
local function normalizeSkins(skins)
    local list = {}
    if type(skins) ~= "table" then
        return list
    end

    -- numerische Keys erstmal getrennt sammeln (weil #skins bei loadData oft 0 ist)
    local numeric = {}
    for k, v in pairs(skins) do
        if type(k) == "number" then
            numeric[k] = v
        else
            -- z.B. skins = { wild = "Wild" }
            list[#list+1] = v
        end
    end

    -- numerische Keys sortieren, damit 1,2,3 in richtiger Reihenfolge kommen
    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/Skins: {{Firestone|hero|Talia|avatar}}
-- default:   Talia.png
-- extra:     Talia_Wild.png, Talia_Cheerleader.png ...
-- Labels:    aus i18.ui.skins
-- ========================================================================
local function buildAvatarMarkup(hero, realKey, i18)
    local frame = mw.getCurrentFrame()

    local skinLabels = ((i18.ui or {}).skins) or {}
    local skins      = normalizeSkins(hero.skins)

    -- FALL: keine Skins → nur Standardbild
    if #skins == 0 then
        return string.format(
            "[[Datei:%s.png|210px|class=avatar-img|link=%s]]",
            realKey,
            realKey
        )
    end

    -- wir müssen Tabber über extensionTag ausgeben,
    -- sonst zeigt er nur den Wikitext an
    local panels = {}

    -- 1) Default-Tab = Heldenname
    local heroDisplayName = hero.name or realKey
    panels[#panels+1] =
        heroDisplayName .. "=\n" ..
        string.format("[[Datei:%s.png|210px|class=avatar-img|link=%s]]", realKey, realKey)

    -- 2) alle Skins
    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

    -- jetzt mit extensionTag wrappen
    local content = table.concat(panels, "\n|-|\n")
    return frame:extensionTag('tabber', content)
end

-- UI-Zweig: {{Firestone|hero|ui|basics|class}}
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

-- normaler Held: {{Firestone|hero|Talia|...}}
function H.handle(frame, args)
    local name = args[2]
    if Util.isEmpty(name) then
        return ""
    end

    local realKey = Util.pickKey(HEROES, name)
    if not realKey then
        return name
    end

    local hero = HEROES[realKey]
    local i18  = getI18n()

    -- nur {{Firestone|hero|Talia}} → Key
    if Util.isEmpty(args[3]) then
        return realKey
    end

    -- Avatar?
    if Util.norm(args[3]) == "avatar" then
        return buildAvatarMarkup(hero, realKey, i18)
    end

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