Zum Inhalt springen


Modul:Firestone

Aus Firestone Idle RPG Wiki

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

-- Modul:Firestone
local M = {}

-- ===== helpers =====
local function isempty(v) return v==nil or v=="" end
local function norm(s)
  if s==nil then return "" end
  s = mw.text.trim(tostring(s))
  return mw.ustring.lower(s):gsub("%s+"," ")
end
local function getArgs(frame)
  local a, f1, f2 = {}, frame.args or {}, (frame:getParent() and frame:getParent().args) or {}
  for k,v in pairs(f1) do if not isempty(v) then a[k]=v end end
  for k,v in pairs(f2) do if not isempty(v) and a[k]==nil then a[k]=v end end
  return a
end
local function pick_key(tbl, key)
  if type(tbl)~="table" then return nil end
  if tbl[key]~=nil then return key end
  local k2 = tostring(key)
  if tbl[k2]~=nil then return k2 end
  local kn = norm(k2)
  for k,_ in pairs(tbl) do if norm(k)==kn then return k end end
  return nil
end
local function deep_get(tbl, path)
  local cur = tbl
  for _,k in ipairs(path) do
    if type(cur)~="table" then return nil end
    local real = pick_key(cur, k); if not real then return nil end
    cur = cur[real]
  end
  return cur
end
local function fileWikitext(file, size)
  if isempty(file) then return "" end
  local parts = {"Datei:"..file}
  if not isempty(size) then table.insert(parts, size) end
  return string.format("[[%s]]", table.concat(parts, "|"))
end

-- ===== lazy loads =====
local HEROES, EVENTS, I18N

local function heroes()
  if HEROES then return HEROES end
  for _,t in ipairs{ "Module:HeroData", "Modul:HeroData" } do
    local ok, data = pcall(mw.loadData, t)
    if ok and type(data)=="table" and type(data.heroes)=="table" then HEROES = data.heroes; return HEROES end
    local ok2, mod = pcall(require, t)
    if ok2 and type(mod)=="table" and type(mod.heroes)=="table" then HEROES = mod.heroes; return HEROES end
  end
  HEROES = {}; return HEROES
end

local function events()
  if EVENTS then return EVENTS end
  -- erst loadData (empfohlen für reine Datenmodule), dann fallback require
  for _,t in ipairs{ "Module:EventData", "Modul:EventData" } do
    local ok, data = pcall(mw.loadData, t)
    if ok and type(data)=="table" and type(data.events)=="table" then EVENTS = data.events; return EVENTS end
  end
  for _,t in ipairs{ "Module:EventData", "Modul:EventData" } do
    local ok, mod = pcall(require, t)
    if ok and type(mod)=="table" and type(mod.events)=="table" then EVENTS = mod.events; return EVENTS end
  end
  EVENTS = {}; return EVENTS
end

local function i18n()
  if I18N then return I18N end
  for _,t in ipairs{ "Module:HeroI18n", "Modul:HeroI18n" } do
    local ok, data = pcall(mw.loadData, t)
    if ok and type(data)=="table" then I18N = data; return I18N end
    local ok2, mod = pcall(require, t)
    if ok2 and type(mod)=="table" then I18N = mod; return I18N end
  end
  I18N = { i18n={}, events={names={},sections={},infobox={},phrases={}} }
  return I18N
end

-- ===== translations =====
local function tr(kind, v)
  if isempty(v) then return v end
  local map=(i18n().i18n or {})[kind] or {}
  return map[norm(v)] or v
end
local function tr_currency(k) return tr("currency", k) end
local function tr_event_name(k)
  local names=(i18n().events or {}).names or {}
  return names[norm(k)] or k
end
local function section_label(evkey, which)
  local E = i18n().events or {}
  local ev = (E.sections and E.sections[norm(evkey)]) or {}
  local def= (E.sections and E.sections.default) or {}
  return (ev[which] or def[which] or "")
end

-- ===== dispatch helpers =====
local function resolve_event(key)
  local evs = events()
  local real = pick_key(evs, key)
  return real and evs[real] or nil
end

-- ===== feature: currency =====
-- {{Firestone|Currency|Gold}}
local function fn_currency(args)
  local key = args[2]; if isempty(key) then return "" end
  return tostring( tr_currency(key) or "" )
end

-- ===== feature: event data =====
-- {{Firestone|Event|Halloween|field|...}}
local function fn_event(args)
  local key = args[2]; if isempty(key) then return "" end
  local ev = resolve_event(key); if not ev then return "" end
  if not args[3] then return "" end
  local path, i = {}, 3
  while args[i] do table.insert(path, args[i]); i=i+1 end
  local v = deep_get(ev, path)
  return tostring(v or "")
end

-- {{Firestone|EventName|Halloween}}  (nur Titel aus I18n)
local function fn_event_name(args)
  local key = args[2]; if isempty(key) then return "" end
  return tostring(tr_event_name(key))
end

-- {{Firestone|EventLabel|Halloween|hub}} → "Hexenhütte"
-- optional {{Firestone|EventLabel|Halloween|name}} (wenn du Namenslabel über sections lösen willst)
local function fn_event_label(args)
  local key, which = args[2], args[3]
  if isempty(key) or isempty(which) then return "" end
  return section_label(key, which)
end

-- {{Firestone|EventInfobox|Halloween|type}} → "Kalenderevent"
-- {{Firestone|EventInfobox|Halloween|unlocks_at}} → Label "Freigeschaltet bei"
local function fn_event_infobox(args)
  local key, which = args[2], args[3]
  if isempty(key) or isempty(which) then return "" end
  local E = i18n().events or {}
  if which == "type" then
    local ev = resolve_event(key); if not ev then return "" end
    local t = ev.type
    local map = (E.infobox and E.infobox.type) or {}
    return tostring(map and map[norm(t or "")] or (t or ""))
  else
    local lbl = (E.infobox and E.infobox[which]) or ""
    return tostring(lbl or "")
  end
end

-- {{Firestone|EventPhrases|offline_cap|24h}}
local function fn_event_phrase(args)
  local phkey, arg1, arg2, arg3 = args[2], args[3], args[4], args[5]
  if isempty(phkey) then return "" end
  local P = (i18n().events or {}).phrases or {}
  local tpl = P[phkey]; if isempty(tpl) then return "" end
  local s = tpl
  s = s:gsub("%$1", tostring(arg1 or "")):gsub("%$2", tostring(arg2 or "")):gsub("%$3", tostring(arg3 or ""))
  return s
end

-- {{Firestone|EventAvatars|Halloween|2024}} → Tabelle
local function fn_event_avatars(args)
  local key, year = args[2], tonumber(args[3] or "")
  if isempty(key) or not year then return "" end
  local ev = resolve_event(key); if not ev then return "" end
  local list = ev.avatars and ev.avatars[year]; if type(list)~="table" then return "" end

  local token_key = ev.token and ev.token.key or ""
  local token_icon= ev.token and ev.token.icon or ""
  local out = {}
  table.insert(out, '{| class="article-table" style="font-size:14px;"')
  table.insert(out, '! colspan="2" | Avatar\n! Preis')
  for _,it in ipairs(list) do
    table.insert(out, '|-')
    table.insert(out, string.format('|%s || %s || %s %s %s',
      fileWikitext(it.file, "50px"),
      it.title or "",
      fileWikitext(token_icon, "25px"),
      tostring(it.cost or 0),
      tr_currency(token_key)
    ))
  end
  table.insert(out, '|}')
  return table.concat(out, "\n")
end

-- {{Firestone|EventHistory|Halloween}}
local function fn_event_history(args)
  local key = args[2]; if isempty(key) then return "" end
  local ev = resolve_event(key); if not ev then return "" end
  local H = ev.history or {}; if #H==0 then return "" end
  local P = (i18n().events or {}).phrases or {}
  local alt_on_gp = P.alt_platform or "auf Google Play"
  local lines = {}
  for _,h in ipairs(H) do
    local base = string.format("* %s: %s bis %s", tostring(h.year or ""), tostring(h.start or ""), tostring(h['end'] or ""))
    if h.alt_platform then base = base .. string.format(" (%s %s)", tostring(h.alt_platform), alt_on_gp) end
    table.insert(lines, base)
  end
  return table.concat(lines, "\n")
end

-- ===== feature: hero =====
-- {{Firestone|Hero|Talia|main|damage}}
-- Alias: {{Firestone|Talia|main|damage}} (wenn 1. Param direkt ein Held ist)
local function fn_hero(args)
  local name = args[2]; if isempty(name) then return "" end
  local H = heroes()
  local real = pick_key(H, name)
  if not real then return "" end
  local hero = H[real]; if not hero then return "" end
  local path, i = {}, 3
  while args[i] do table.insert(path, args[i]); i=i+1 end
  local v = deep_get(hero, path)
  local last = norm(path[#path] or "")
  if last=="class" then return tr("class", v) or ""
  elseif last=="attackstyle" then return tr("attackstyle", v) or ""
  elseif last=="specialization" then return tr("specialization", v) or ""
  elseif last=="resource" then return tr("resource", v) or ""
  else return tostring(v or "") end
end

-- ===== main dispatcher =====
function M.main(frame)
  local a = getArgs(frame)
  for i=1,10 do a[i] = a[i] and tostring(a[i]) or nil end
  local dom = a[1] and norm(a[1]) or ""

  if dom=="currency" then return fn_currency(a)
  elseif dom=="event" then return fn_event(a)
  elseif dom=="eventname" or dom=="event_name" then return fn_event_name(a)
  elseif dom=="eventlabel" or dom=="event_label" then return fn_event_label(a)
  elseif dom=="eventinfobox" or dom=="event_infobox" then return fn_event_infobox(a)
  elseif dom=="eventphrases" or dom=="event_phrase" then return fn_event_phrase(a)
  elseif dom=="eventavatars" or dom=="event_avatars" then return fn_event_avatars(a)
  elseif dom=="eventhistory" or dom=="event_history" then return fn_event_history(a)
  elseif dom=="hero" then return fn_hero(a)
  else
    -- Fallback: nichts passendes → leer
    return ""
  end
end

return M