From Arms of God Wiki

Documentation for this module may be created at Module:TagIndex/doc

-- Module:TagIndex — "everything that carries this tag".
--
-- WHAT IT DOES
--   Renders the member sections of a Tag page ('Weapons with this tag',
--   'Characters with this tag', ...). Membership is never stored: it
--   scans every tag-bearing category's records[].tags at render time
--   (via Module:Core's memoized membership map), so adding a tag key to
--   any entity's Data record updates its tag page on purge.
--
-- HOW TO INVOKE
--   {{#invoke:TagIndex|members|Melee}}      bare tag key
--   {{#invoke:TagIndex|members|Tag-Melee}}  page slug also accepted
--
-- SOURCE DATA IT READS (via Module:Core)
--   Data:Tags.json plus the tag-bearing categories: Data:Weapons.json,
--   Data:Characters.json, Data:Blessings.json, Data:Upgrades.json,
--   Data:Passives.json, Data:Enemies.json.
local Core = require('Module:Core')
local p = {}

-- Section order + labels (presentation config).
local SECTIONS = {
  {'Weapons', 'Weapons with this tag'},
  {'Characters', 'Characters with this tag'},
  {'Blessings', 'Blessings with this tag'},
  {'Upgrades', 'Upgrades with this tag'},
  {'Passives', 'Passives with this tag'},
  {'Enemies', 'Enemies with this tag'},
}

-- Computed '== <Cat> with this tag ==' bullet sections for one tag key.
function p.sectionsFor(key)
  if not key or key == '' then return '' end
  local m = Core.tagMembership()[key]
  if not m then return '' end
  local CrossRef = require('Module:CrossRef')
  local out = {}
  for _, sec in ipairs(SECTIONS) do
    local cat, label = sec[1], sec[2]
    local recs = m[cat]
    if recs and #recs > 0 then
      local sorted = {}
      for i, r in ipairs(recs) do sorted[i] = r end
      table.sort(sorted, function(a, b)
        return mw.ustring.lower(a.name or '') < mw.ustring.lower(b.name or '')
      end)
      out[#out + 1] = CrossRef.renderSection(label, sorted)
    end
  end
  return table.concat(out, '\n')
end

function p.members(frame)
  local key = (frame.args[1] or ''):gsub('^%s+', ''):gsub('%s+$', '')
  -- Accept either the bare tag key ('Melee') or the page slug ('Tag-Melee').
  if not Core.tagMembership()[key] and key:sub(1, 4) == 'Tag-' then
    local rec = Core.byId('Tags')[key]
    if rec then key = rec.id or rec.name end
  end
  local body = p.sectionsFor(key)
  if body == '' then
    return "''Nothing in the current game data carries this tag.''"
  end
  return mw.getCurrentFrame():preprocess(body)
end

return p