From Arms of God Wiki

Revision as of 08:30, 10 June 2026 by Ta1ha (talk | contribs) (bot: render-time derivation refactor (phase 2))

Shared helper that renders cross-reference link lists between categories.

Library module shipped by the publishing bot; shared across categories. Bot-published — edits are overwritten on re-publish.


-- Module:CrossRef — render-time cross-reference computation (Arms of God).
-- Computes at render, from the source Data:<Category>.json pages:
--   * base <-> '<X> Plus' variant links within a category (id match)
--   * tag membership sections for Tag pages (via Module:TagIndex)
-- Records carry NO baked cross_refs; everything here recomputes on purge.
--
-- Entry point (wired by the per-category shims):
--   {{#invoke:<Cat>|crossRefs|id=<slug-or-id>}}
local Core = require('Module:Core')
local p = {}

-- Render a single labelled section: '== <label> ==' + icon-link bullets.
function p.renderSection(label, refs)
  if not refs or #refs == 0 then return '' end
  local out = {'', '== ' .. label .. ' =='}
  for _, rec in ipairs(refs) do
    out[#out + 1] = '* ' .. Core.iconLink(rec, 24)
  end
  return table.concat(out, '\n')
end

-- Variant pairing: RAW id first ('<X> Plus' / '<x>-plus' -> base), then
-- display name ('<Name> Plus'). Id wins because Upgrades' display-name
-- split means base and Plus can show unrelated display names (e.g. id
-- 'Shield Transform' displays 'Lifesteal Chamber'); the name fallback
-- covers Blessings, whose kebab-case ids ('blessed-bounty-plus') pair by
-- either rule.
local VARIANT_CATS = {Blessings = true, Upgrades = true}

local _byName = {}
local function byName(cat)
  if _byName[cat] == nil then
    local m = {}
    for _, r in ipairs(Core.load(cat)) do
      if r.name then m[r.name] = r end
    end
    _byName[cat] = m
  end
  return _byName[cat]
end

local function variantSections(cat, rec)
  if not VARIANT_CATS[cat] then return '' end
  local ids, names = Core.byId(cat), byName(cat)
  local id, name = rec.id or '', rec.name or ''
  local out = {}
  local base = nil
  if id:sub(-5) == ' Plus' then base = ids[id:sub(1, -6)] end
  if not base and id:sub(-5) == '-plus' then base = ids[id:sub(1, -6)] end
  if not base and name:sub(-5) == ' Plus' then base = names[name:sub(1, -6)] end
  if base and base ~= rec then
    out[#out + 1] = p.renderSection('Base variant', {base})
  elseif not (id:sub(-5) == ' Plus' or id:sub(-5) == '-plus'
              or name:sub(-5) == ' Plus') then
    local plus = ids[id .. ' Plus'] or ids[id .. '-plus'] or names[name .. ' Plus']
    if plus and plus ~= rec then
      out[#out + 1] = p.renderSection('Upgraded variant', {plus})
    end
  end
  return table.concat(out, '\n')
end

-- Full computed cross-ref block for one record (no frame needed).
function p.compute(cat, rec)
  if cat == 'Tags' then
    local TagIndex = require('Module:TagIndex')
    return TagIndex.sectionsFor(rec.id or rec.name)
  end
  return variantSections(cat, rec)
end

-- Frame entry used by the per-category shims.
function p.entry(cat, frame)
  local rec, missing = Core.resolveRec(cat, frame)
  if missing then return missing end
  if not rec then return '' end
  local body = p.compute(cat, rec)
  if body == '' then return '' end
  return mw.getCurrentFrame():preprocess(body)
end

return p