Difference between revisions of "Module:Equipment"

From BTAWiki
Jump to navigation Jump to search
(Fixed equipment update)
Line 23: Line 23:
  
 
   if next(UserIds) == nil then
 
   if next(UserIds) == nil then
     return mw.html.create('div').wikitext("\n''Not found on any 'Mechs''")
+
     return mw.html.create('div').wikitext("''Not found on any 'Mechs''")
 
   end
 
   end
  
Line 45: Line 45:
 
     )
 
     )
 
     for _, c in ipairs(cids) do
 
     for _, c in ipairs(cids) do
       ChassisIds[c.id] = true
+
       ChassisIds[c.id] = false  -- false to indicate that its not fixed to the chassis
 
     end
 
     end
 
   end
 
   end
Line 53: Line 53:
  
 
   -- keying by ID allowed us to remove duplicates, now give them an index so we can sort
 
   -- keying by ID allowed us to remove duplicates, now give them an index so we can sort
   local tempTable = {}
+
   local sortedChassis = {}
 
   for id,_ in pairs(ChassisIds) do
 
   for id,_ in pairs(ChassisIds) do
     table.insert(tempTable,id)
+
     table.insert(sortedChassis,id)
 
   end
 
   end
  ChassisIds = tempTable
 
  
 
   -- sort the table to put the new chassis ids in the correct place
 
   -- sort the table to put the new chassis ids in the correct place
   table.sort(ChassisIds,  
+
   table.sort(sortedChassis,  
 
     function(a,b)
 
     function(a,b)
 
       a = string.lower(a)
 
       a = string.lower(a)
Line 84: Line 83:
  
 
   -- for every chassis id, query the name and variant and add to the display list
 
   -- for every chassis id, query the name and variant and add to the display list
   for _, cid in ipairs(ChassisIds) do
+
   for _, cid in ipairs(sortedChassis) do
 
     local names = mw.ext.cargo.query(
 
     local names = mw.ext.cargo.query(
 
       'Chassis','Chassis.Name=Name,Chassis.VariantName=VariantName',
 
       'Chassis','Chassis.Name=Name,Chassis.VariantName=VariantName',
Line 91: Line 90:
  
 
     for _, mech in ipairs(names) do
 
     for _, mech in ipairs(names) do
       equipList:tag('li'):wikitext(string.format(
+
       local strMechLink = string.format('[[%s#%s|%s %s]]',  
        '[[%s#%s|%s %s]]', mech['Name'], mech['VariantName'],  
+
        mech.Name, mech.VariantName, mech.Name, mech.VariantName)
        mech['Name'], mech['VariantName']
+
 
       ))
+
      if ChassisIds[cid] then
 +
        equipList:tag('li'):wikitext(string.format("%s (Fixed Gear)", strMechLink))
 +
       else
 +
        equipList:tag('li'):wikitext(strMechLink)
 +
      end
 
     end
 
     end
 
   end
 
   end

Revision as of 11:04, 11 January 2022

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

-- Module:Equipment is my attempt to create a list of mechs that use a particular 
-- bit of equipment with the idea being you can see what mechs to hunt for particular bits of gear.

local p = {}

local mechs = require('Module:Mech').core
local getArgs = require('Module:Arguments').getArgs


function p.equipmentMechs(frame)
  local tpl_args = getArgs(frame, {parentFirst=true})

  local equipment = tpl_args[1]

  -- Find all records in MechInventory where the component id matches
  -- the item we are looking for. The MechID for each match will be
  -- saved in UserIds table. Note, if the equipment is fixed then the
  -- id will actually be a key to the Chassis table, not the Mech table.
  local UserIds = mw.ext.cargo.query(
    'MechInventory','MechInventory.MechID=id',
    { where = string.format('MechInventory.ComponentDefID = "%s"', equipment), limit=10000 }
  )

  if next(UserIds) == nil then
    return mw.html.create('div').wikitext("''Not found on any 'Mechs''")
  end

  -- Split the result sets by the type of id returned
  local ChassisIds, MechIds, UnknownIds = {}, {}, {}
  for _, user in pairs(UserIds) do
    if string.find(user.id, "chassisdef", 1, true) then
      ChassisIds[user.id] = true
    elseif string.find(user.id, "mechdef", 1, true) then
      MechIds[user.id] = true
    else
      table.insert(UnknownIds, user.id)
    end
  end

  -- convert mech ids to chassis ids
  for mid,_ in pairs(MechIds) do
    local cids = mw.ext.cargo.query(
      'Mech','Mech.ChassisID=id',
      { where = string.format('Mech.Id = "%s"', mid), limit=2000 }
    )
    for _, c in ipairs(cids) do
      ChassisIds[c.id] = false  -- false to indicate that its not fixed to the chassis
    end
  end

  -- this particular model should never be included
  ChassisIds["chassisdef_charger_CGR-FB"] = nil

  -- keying by ID allowed us to remove duplicates, now give them an index so we can sort
  local sortedChassis = {}
  for id,_ in pairs(ChassisIds) do
    table.insert(sortedChassis,id)
  end

  -- sort the table to put the new chassis ids in the correct place
  table.sort(sortedChassis, 
    function(a,b)
      a = string.lower(a)
      b = string.lower(b)

      local aprime = string.find(a, "prime", -5, true)
      if aprime and string.find(b, string.sub(a, 1, aprime-1), 1, true) then
        return true
      end

      local bprime = string.find(b, "prime", -5, true)
      if bprime and string.find(a, string.sub(b, 1, bprime-1), 1, true) then
        return false
      end
      
      return a < b
    end
  )

  -- create the display list
  local equipList = mw.html.create('ul')
  equipList:cssText('column-count: 3;-moz-column-count: 3;-webkit-column-count: 3')

  -- for every chassis id, query the name and variant and add to the display list
  for _, cid in ipairs(sortedChassis) do
    local names = mw.ext.cargo.query(
      'Chassis','Chassis.Name=Name,Chassis.VariantName=VariantName',
      { where = string.format('Chassis.Id = "%s"', cid), limit=2000 }
    )

    for _, mech in ipairs(names) do
      local strMechLink = string.format('[[%s#%s|%s %s]]', 
        mech.Name, mech.VariantName, mech.Name, mech.VariantName)

      if ChassisIds[cid] then
        equipList:tag('li'):wikitext(string.format("%s (Fixed Gear)", strMechLink))
      else
        equipList:tag('li'):wikitext(strMechLink)
      end
    end
  end

  -- if there were any items whose mech id wasn't a chassisdef or mechdef, 
  -- add them last as whatever
  for _, unknown in ipairs(UnknownIds) do
    equipList:tag('li'):wikitext(string.format("unknown %s", tostring(unknown.id)))
  end

  return equipList
end

function p.mechEquipment(frame)
  local tpl_args = getArgs(frame, {parentFirst=true})
  gearpiece = tpl_args[1]

  local mech = mechs.mech_inventory.componentDefID(gearpiece)

  if mech == nil then
    return mw.html.create('div').wikitext("''Mech not found''")
  end

  local equip = {}

  local mechList = mw.html.create('ul')

  for _, tag in ipairs(mech.equip) do
    if p.componentID[equip] ~= nil then
      table.insert(equip, p.componentID[tag])
    end
  end

  table.sort(tags)
  for _, tag in ipairs(equip) do
    factionList:tag('li'):wikitext(tag)
  end
  
  return mechList
end

return p