Zum Inhalt springen

Modul:Item

Aus Firestone Idle Rpg Wiki

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

-- Modul:Item – Firestone-Version (Daten + Tooltip)

local p        = {}
local DATA     = mw.loadData('Modul:Item/Data')
local ItemType = require('Modul:Item/Type')

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

-- =========================
-- Konfiguration
-- =========================

local DEFAULT_404_IMG      = '404'
local DEFAULT_404_LINK     = 'Modul:Item/Data'
local DEFAULT_404_TYPE     = 'unknown'
local DEFAULT_TOOLTIP_MAX  = 140

-- interne Typ-Namen → deutsche Labels
local TYPE_LABELS = {
    currencies = 'Währung',
    unknown    = 'Unbekannt',
}

-- =========================
-- Helpers
-- =========================

local function trim(s)
    if s == nil then
        return ''
    end
    s = tostring(s)
    if text and text.trim then
        return text.trim(s)
    end
    return s:gsub('^%s+', ''):gsub('%s+$', '')
end

local function normalizeKey(name)
    if not name then
        return nil
    end
    local s = ustr.lower(tostring(name))
    s = s:gsub('%s+', '')
    return s
end

local function clone(tbl)
    local copy = {}
    for k, v in pairs(tbl or {}) do
        copy[k] = v
    end
    return copy
end

local function object404(name)
    name = tostring(name or '?')
    return {
        key      = '404',
        title    = 'ERROR: ' .. name,
        link     = DEFAULT_404_LINK,
        img      = DEFAULT_404_IMG,
        type     = DEFAULT_404_TYPE,
        describe = '',
    }
end

local function getItem(raw)
    local key = normalizeKey(raw or '')
    if not key or key == '' then
        return object404(raw)
    end

    local base = DATA[key]
    if not base then
        return object404(key)
    end

    local item = clone(base)
    item.key   = key
    return item
end

local function capitalize(str)
    if not str then
        return ''
    end
    local s = string.lower(tostring(str))
    return string.gsub(' ' .. s, '%W%l', string.upper):sub(2)
end

local function title2link(title)
    return (tostring(title or '')):gsub('%s', '_')
end

local function getTitle(item)
    if item.title and item.title ~= '' then
        return item.title
    end
    return capitalize(item.key)
end

local function imgPngExists(imgname)
    if type(imgname) == 'string' and #imgname ~= 0 then
        local t = mw.title.new('Datei:' .. imgname .. '.png')
        return t and t.file and t.file.exists or false
    end
    return false
end

local function getImage(item)
    if item.img and imgPngExists(item.img) then
        return item.img
    end

    local guess = title2link(getTitle(item))
    if imgPngExists(guess) then
        return guess
    end

    return object404(item.key).img
end

local function getLinkTarget(item)
    if item.link and item.link ~= '' then
        return item.link
    end
    return title2link(getTitle(item))
end

local function getTypeRaw(item)
    local group = ItemType.typenameForKey(item.key)
    return group or DEFAULT_404_TYPE
end

local function getTypeLabel(item)
    local group = getTypeRaw(item)
    local label = TYPE_LABELS[group]
    if label and label ~= '' then
        return label
    end
    return group
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
    str = str:gsub('%s+', ' ')
    if text and text.trim then
        str = text.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

-- ---------- Tooltip-Aufbau ----------

local function buildTooltip(item, opts)
    opts = opts or {}
    local max   = opts.max or DEFAULT_TOOLTIP_MAX
    local label = opts.label or ''

    local title     = getTitle(item)
    local desc      = item.describe or item.description or ''
    local shortDesc = shorten(desc, max)
    local img       = getImage(item)
    local target    = getLinkTarget(item)

    local parts = {}

    -- Wrapper (fs-item) – Tooltip nur per CSS, wenn .tooltip vorhanden
    table.insert(parts, '<span class="fs-item fs-item--has-tooltip">')

    -- Sichtbares Label (z.B. {{Icon}} oder {{Amount}}-HTML)
    if label ~= '' then
        table.insert(parts, label)
    else
        table.insert(parts, '[[' .. target .. '|' .. title .. ']]')
    end

    -- Tooltip-Box
    table.insert(parts, '<span class="tooltip">')

    -- Header: Icon + Titel
    table.insert(parts, '<span class="header">')
    if img and img ~= '' then
        table.insert(parts, '[[Datei:' .. img .. '.png|24x24px|class=icon]] ')
    end
    table.insert(parts, '<span class="title">')
    table.insert(parts, title)
    table.insert(parts, '</span></span>') -- title + header

    -- Body
    if shortDesc ~= '' then
        table.insert(parts, '<span class="body">')
        table.insert(parts, shortDesc)
        table.insert(parts, '</span>')
    end

    table.insert(parts, '</span>') -- tooltip
    table.insert(parts, '</span>') -- fs-item

    return table.concat(parts)
end

-- ---------- Frame-Key ----------

local function getKey(frame)
    if not frame then
        return ''
    end

    local args = frame.args or {}
    local key  = args[1] or args.key or args.name or args.id or args.nameid

    if (not key or key == '') and frame.getParent then
        local parent = frame:getParent()
        if parent and parent.args then
            args = parent.args
            key  = args[1] or args.key or args.name or args.id or args.nameid
        end
    end

    return trim(key)
end

-- =========================
-- Öffentliche Funktionen (Daten)
-- =========================

function p.title(frame)
    local key  = getKey(frame)
    local item = getItem(key)
    return getTitle(item)
end

function p.short(frame)
    local key  = getKey(frame)
    local item = getItem(key)
    return item.short or getTitle(item)
end

function p.description(frame)
    local key  = getKey(frame)
    local item = getItem(key)
    return item.describe or item.description or '-'
end

p.desc = p.description

function p.link(frame)
    local key  = getKey(frame)
    local item = getItem(key)
    return getLinkTarget(item)
end

function p.img(frame)
    local key  = getKey(frame)
    local item = getItem(key)
    return getImage(item)
end

function p.type(frame)
    local key  = getKey(frame)
    local item = getItem(key)
    return getTypeLabel(item)
end

function p.type_raw(frame)
    local key  = getKey(frame)
    local item = getItem(key)
    return getTypeRaw(item)
end

-- =========================
-- Tooltip-Wrapper
-- =========================
-- Verwendung:
--   {{#invoke:Item|tooltip|gold|label=<…Markup…>|max=200}}

function p.tooltip(frame)
    local args  = frame.args or {}
    local key   = getKey(frame)
    local item  = getItem(key)
    local label = args.label or ''
    local max   = tonumber(args.max or args.len or args.limit) or DEFAULT_TOOLTIP_MAX

    return buildTooltip(item, { label = label, max = max })
end

return p