Module:Test

From BTAWiki
Revision as of 02:24, 12 February 2022 by Turtrus (talk | contribs) (Reverted edits by Turtrus (talk) to last revision by Stencil)
Jump to navigation Jump to search

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

-- Module:Test is a blank canvas to try stuff out on without breaking anything that other pages are using right now. It will be invoked in [[Template:TemplateTest]]

local p = {}

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


--========================================================--
--                    Module:Factions
--========================================================--

p.factionTags = {
  AuriganDirectorate = 'Aurigan Directorate',
  AuriganMercenaries = 'Mercenaries',
  AuriganPirates = '[[Local Pirates|Pirates]]',
  AuriganRestoration = '[[Aurigan Coalition|Aurigan Restoration (Arano)]]',
  Chainelane = '[[Chainelane Isles]]',
  Circinus = '[[Circinus Federation]]',
  ClanDiamondShark = '[[Clan Diamond Shark]]',
  ClanGhostBear = '[[Clan Ghost Bear]]',
  ClanJadeFalcon = '[[Clan Jade Falcon]]',
  ClanNovaCat = '[[Clan Nova Cat]]',
  ClanWolf = '[[Clan Wolf]]',
  ComStar = '[[ComStar]]',
  Davion = '[[Federated Suns|Federated Suns (Davion)]]',
  DarkCaste = '[[Dark Caste]]',
  Delphi = '[[New Delphi Compact]]',
  Hanse = '[[Hanseatic League]]',
  Ives = '[[St. Ives Compact]]',
  JarnFolk = '[[JàrnFòlk]]',
  Kurita = '[[Draconis Combine|Draconis Combine (Kurita)]]',
  Liao = '[[Capellan Confederation|Capellan Confederation (Liao)]]',
  Locals = 'Local Government',
  MagistracyofCanopus = '[[Magistracy of Canopus]]',
  MagistracyOfCanopus = '[[Magistracy of Canopus]]',
  Marian = '[[Marian Hegemony]]',
  Marik = '[[Free Worlds League|Free Worlds League (Marik)]]',
  Outworld = '[[Outworlds Alliance]]',
  Rasalhague = '[[Free Rasalhague Republic]]',
  Rim = '[[Rim Collection]]',
  Steiner = '[[Lyran Commonwealth|Lyran Commonwealth (Steiner)]]',
  TaurianConcordat = '[[Taurian Concordat]]',
  Tortuga = '[[Tortuga Dominions]]',
  WordOfBlake = '[[Word of Blake]]'
}


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

  local faction = tpl_args[1]
  local where = string.format('Mech.MechTags HOLDS "%s"', faction)

  -- some mechs share the same name but have different tonnages and therefore
  -- different pages to be linked to
  local function MakeMechLink(name, variant, tonnage)
    if (name == "Blackjack" and tonnage ~= "45") or
       (name == "Centurion" and tonnage ~= "50") or
       (name == "Corsair" and tonnage ~= "95") or
       (name == "Stalker" and tonnage ~= "85") or
       (name == "Mad Cat MK II" and tonnage ~= "90") then
      return string.format('[[%s_(%sT)#%s|%s (%sT) %s]]', name, tonnage, variant, name, tonnage, variant)
    else
      return string.format('[[%s#%s|%s %s]]', name, variant, name, variant)
    end
  end

  -- when querying for mechs, set the limit to 2000. this is arbitrarily high
  -- (larger than the total number of mechs). without this, factions with lots
  -- of mechs would not show all of them.
  local factionMechData = mw.ext.cargo.query(
    'Mech,Chassis','Chassis.Name=Name,Chassis.VariantName=VariantName,Chassis.Tonnage=Tonnage',
    { join = 'Mech.ChassisID=Chassis.Id', where=where, limit=2000 }
  )

  local mechsList = mw.html.create('ul')
  mechsList:cssText('column-count: 3;-moz-column-count: 3;-webkit-column-count: 3')
  for _, mech in ipairs(factionMechData) do
    mechsList:tag('li'):wikitext(MakeMechLink(mech.Name, mech.VariantName, tostring(mech.Tonnage)))
  end
  return mechsList
end

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

  local mech = mechs.mech.byVariant(variant)

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

  local tags = {}

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

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

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


--========================================================--
--                    Module:Equipment
--========================================================--

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')

  -- some mechs share the same name but have different tonnages and therefore
  -- different pages to be linked to
  local function MakeMechLink(name, variant, tonnage)
    if (name == "Blackjack" and tonnage ~= "45") or
       (name == "Centurion" and tonnage ~= "50") or
       (name == "Corsair" and tonnage ~= "95") or
       (name == "Stalker" and tonnage ~= "85") then
      return string.format('[[%s_(%sT)#%s|%s (%sT) %s]]', name, tonnage, variant, name, tonnage, variant)
    else
      return string.format('[[%s#%s|%s %s]]', name, variant, name, variant)
    end
  end

  -- 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,Chassis.Tonnage=Tonnage',
      { where = string.format('Chassis.Id = "%s"', cid), limit=2000 }
    )

    for _, mech in ipairs(names) do
  
      local strMechLink = MakeMechLink(mech.Name, mech.VariantName, tostring(mech.Tonnage))

      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


--========================================================--
--             Random Test Stuff
--========================================================--

function p.findDuplicates(frame)

  -- this should create a table where the values are all the chassisdefs
  -- should include any duplicates
  local db = mw.ext.cargo.query(
    'Chassis','Chassis.Id=Id, Chassis._ID', { limit = 10000 } )

  local ids = {}
  local dupes = {}

  -- lets look at each one
  for k, v in pairs(db) do
    -- if we've already set an entry in the ids table for it ...
    if ids[v.Id] then
      -- ... then it is a duplicate
      dupes[v.Id] = true
    else
      ids[v.Id] = true
    end
  end

  -- try to grab the first dup
  local dupe = next(dupes)

  if not dupe then
    return mw.html.create('div').wikitext("''no duplicates were found''")
  end

--[===[
  local test = ""
  for k,v in pairs(dupes) do
    test = test .. "  " .. k
  end
  if test then
    return mw.html.create('div').wikitext(test)
  end
--]===]

  local where = string.format('where=Chassis.Id = "%s"', dupe)

  dupe = next(dupes, dupe)

  while dupe do
    where = string.format('%s OR Chassis.Id = "%s"', where, dupe)
    dupe = next(dupes, dupe)
  end

  local cq = string.format('{{#cargo_query:tables=Chassis|fields=Chassis.Id, Chassis._ID, Chassis._pageID, Chassis._pageName|%s}}', where)

  return mw.html.create('div').wikitext(cq)

end


return p