Zum Inhalt springen

Modul:Get: Unterschied zwischen den Versionen

Aus Firestone Idle Rpg Wiki
Keine Bearbeitungszusammenfassung
Keine Bearbeitungszusammenfassung
 
Zeile 318: Zeile 318:
         return buildLabel(short, nil)
         return buildLabel(short, nil)


    elseif method == 'link' then
elseif method == 'link' then
        if tipFlag then
    local label = buildLabel(short, link)
            return buildTooltip(short, title, desc, imgName(), link, tipMax)
        end
    if tipFlag then
        return buildLabel(short, link)
        return buildTooltip(label, title, desc, imgName(), link, tipMax)
    end
    return label


     elseif method == 'tooltip' then
     elseif method == 'tooltip' then

Aktuelle Version vom 10. Dezember 2025, 04:21 Uhr

Die Dokumentation für dieses Modul kann unter Modul:Get/Doku erstellt werden

-- Modul:Get
-- Allgemeiner Zugriff auf strukturierte Daten (Item/Event/Hero/Guardian/…)

local p = {}

local DATA_MODULE = 'Modul:Get/Data'
local data = require(DATA_MODULE)

local ustr    = mw.ustring
local textlib = mw.text

local DEFAULT_TOOLTIP_MAX = 140

-- --------------------------------------------------------
-- Datei-Existenz prüfen → Dateiname (mit Endung) oder nil
-- --------------------------------------------------------

local function fileExists(name)
    if not name or name == '' then
        return nil
    end

    local function check(n)
        local titleObj = mw.title.makeTitle('File', n)
        if not titleObj then
            return nil
        end
        local fileObj = titleObj.file
        if fileObj and fileObj.exists then
            return titleObj.text    -- z.B. "CurrencyGold.png"
        end
        return nil
    end

    -- 1. Versuch: Name wie er übergeben wird
    local found = check(name)
    if found then
        return found
    end

    -- 2. Versuch: wenn keine Endung angegeben → .png anhängen
    if not name:match('%.') then
        found = check(name .. '.png')
        if found then
            return found
        end
    end

    return nil
end

-- --------------------------------------------------------
-- Platzhalter / Basics
-- --------------------------------------------------------

-- $1 → title
local function applyPlaceholder(text, title)
    if not text or text == '' then
        return nil
    end

    return text:gsub('%$1', function()
        return title or ''
    end)
end

-- Nur Link bauen, KEIN Tooltip
local function buildLabel(labelText, linkTarget)
    if not labelText or labelText == '' then
        return ''
    end

    if linkTarget and linkTarget ~= '' then
        if linkTarget:match('^%[%[') or linkTarget:match('^%[') then
            -- bereits fertiger Wiki-/Externer Link
            return linkTarget
        else
            return '[[' .. linkTarget .. '|' .. labelText .. ']]'
        end
    end

    return labelText
end

-- Keys normalisieren: trim, lower, Leerzeichen → _
local function normalizeKey(key)
    if not key then
        return nil
    end

    key = textlib.trim(tostring(key))
    if key == '' then
        return nil
    end

    key = ustr.lower(key)
    key = key:gsub('[%s_]+', '_')
    return key
end

-- Datensatz-Namen (Item/Items, Hero/Heroes…) normalisieren
local function normalizeDatasetKey(datasetRaw)
    local datasetKey = normalizeKey(datasetRaw)
    if not datasetKey then
        return nil
    end

    if datasetKey == 'items' then
        datasetKey = 'item'
    elseif datasetKey == 'events' then
        datasetKey = 'event'
    elseif datasetKey == 'heroes' then
        datasetKey = 'hero'
    elseif datasetKey == 'guardians' then
        datasetKey = 'guardian'
    end

    return datasetKey
end

-- --------------------------------------------------------
-- Markup → Plaintext (für Tooltip)
-- --------------------------------------------------------

local function stripMarkup(str)
    if not str or str == '' then
        return ''
    end
    str = tostring(str)

    -- interne Links [[Seite|Label]] / [[Seite]]
    str = str:gsub('%[%[([^%]|]+)|([^%]]+)%]%]', '%2')
    str = str:gsub('%[%[([^%]]+)%]%]', '%1')

    -- externe Links [url Label]
    str = str:gsub('%[[^%s%]]+%s+([^%]]+)%]', '%1')

    -- Hervorhebungen
    str = str:gsub("''+", '')

    -- Templates {{...}}
    str = str:gsub('{{.-}}', '')

    -- HTML-Tags
    str = str:gsub('<.->', '')

    -- Whitespace normalisieren
    str = str:gsub('%s+', ' ')
    if textlib and textlib.trim then
        str = textlib.trim(str)
    end

    return str
end

local function shorten(str, max)
    str = stripMarkup(str)
    max = tonumber(max) or 0

    if max <= 0 or ustr.len(str) <= max then
        return str
    end

    local cut = ustr.sub(str, 1, max)
    local lastSpace = ustr.find(cut, ' [^ ]*$')
    if lastSpace and lastSpace > max * 0.6 then
        cut = ustr.sub(cut, 1, lastSpace - 1)
    end

    return cut .. '…'
end

-- --------------------------------------------------------
-- Bildauflösung
-- --------------------------------------------------------

local function getImageName(raw, title)
    local imgName

    if raw.img then
        imgName = fileExists(raw.img)
    end

    if not imgName and title then
        imgName = fileExists(title)
    end

    return imgName
end

-- --------------------------------------------------------
-- fs-tip Tooltip (wie früher in Modul:Item)
-- --------------------------------------------------------

local function buildTooltip(label, title, desc, imgName, linkTarget, max)
    max = tonumber(max) or DEFAULT_TOOLTIP_MAX
    desc = desc or ''
    local shortDesc = shorten(desc, max)
    local hasImg    = imgName and imgName ~= ''

    local out = {}

    -- Wrapper
    table.insert(out, '<span class="fs-tip">')

    -- sichtbares Label
    if label and label ~= '' then
        table.insert(out, label)
    else
        if linkTarget and linkTarget ~= '' then
            table.insert(out, '[[' .. linkTarget .. '|' .. (title or '') .. ']]')
        else
            table.insert(out, title or '')
        end
    end

    -- Tooltip-Content
    table.insert(out, '<span class="content">')

    if hasImg then
        table.insert(out, '<span class="box">')
        table.insert(out, '[[Datei:' .. imgName .. '|48x48px]]')
        table.insert(out, '<span class="text">')
    else
        table.insert(out, '<span class="text">')
    end

    -- Titel
    table.insert(out, '<span class="title">')
    table.insert(out, title or '')
    table.insert(out, '</span>')

    -- Beschreibung
    if shortDesc ~= '' then
        table.insert(out, '<span class="desc">')
        table.insert(out, shortDesc)
        table.insert(out, '</span>')
    end

    -- Close .text
    table.insert(out, '</span>')

    -- Close .box, falls Icon
    if hasImg then
        table.insert(out, '</span>')
    end

    -- Close .content + .fs-tip
    table.insert(out, '</span>')
    table.insert(out, '</span>')

    return table.concat(out)
end

-- --------------------------------------------------------
-- Hauptfunktion
-- --------------------------------------------------------

function p.main(frame)
    local args       = frame.args
    local datasetRaw = args.dataset or args[1]
    local method     = args.method  or args[2] or 'link'
    local keyRaw     = args.key     or args[3]

    -- neue Steuerung: Tooltip nur, wenn tip=1 (oder yes/true)
    local tipFlag = (args.tip == '1' or args.tip == 'yes' or args.tip == 'true' or args.tooltip == '1')
    local tipMax  = tonumber(args.max or args.len or args.limit) or DEFAULT_TOOLTIP_MAX

    local datasetKey = normalizeDatasetKey(datasetRaw)
    local key        = normalizeKey(keyRaw)

    if not datasetKey or not key then
        return ''
    end

    local group = data[datasetKey]
    if not group then
        return keyRaw or ''
    end

    local raw = group[key]
    if not raw then
        return keyRaw or ''
    end

    local displayKey = keyRaw or key
    local title      = raw.title or displayKey
    local short      = raw.short or title

    -- Platzhalter anwenden
    local desc = applyPlaceholder(raw.desc, title)
    local link = applyPlaceholder(raw.link, title)

    method = tostring(method):lower()

    -- kleines Lazy-Getter für Bildname, damit wir nicht doppelt suchen
    local resolvedImgName
    local function imgName()
        if resolvedImgName == nil then
            resolvedImgName = getImageName(raw, title)
        end
        return resolvedImgName
    end

    -- -------------- Methoden --------------

    if method == 'title' then
        if tipFlag then
            return buildTooltip(nil, title, desc, imgName(), link, tipMax)
        end
        return buildLabel(title, nil)

    elseif method == 'short' then
        if tipFlag then
            -- Label: Kurzname, Inhaltstitel: voller Titel
            return buildTooltip(short, title, desc, imgName(), link, tipMax)
        end
        return buildLabel(short, nil)

	elseif method == 'link' then
	    local label = buildLabel(short, link)
	
	    if tipFlag then
	        return buildTooltip(label, title, desc, imgName(), link, tipMax)
	    end
	    return label

    elseif method == 'tooltip' then
        -- explizit nur Tooltip
        return buildTooltip(short, title, desc, imgName(), link, tipMax)

    elseif method == 'img' then
        local name = imgName()
        if not name then
            return ''
        end
        return name

    elseif method == 'desc' then
        return desc or ''

    else
        -- Fallback: wie "link"
        if tipFlag then
            return buildTooltip(short, title, desc, imgName(), link, tipMax)
        end
        return buildLabel(short, link)
    end
end

return p