Difference between revisions of "Module:Equipment"
Jump to navigation
Jump to search
Line 13: | Line 13: | ||
local equipment = tpl_args[1] | local equipment = tpl_args[1] | ||
− | -- | + | -- Find all records in MechInventory where the component id matches |
− | local | + | -- 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 | |
− | for | + | -- 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=2000 } | ||
+ | ) | ||
+ | |||
+ | -- 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 | ||
+ | table.insert(ChassisIds, user.id) | ||
+ | elseif string.find(user.id, "mechdef", 1, true) then | ||
+ | table.insert(MechIds, user.id) | ||
+ | else | ||
+ | table.insert(UnknownIds, user.id) | ||
+ | end | ||
end | end | ||
− | + | -- convert mech ids to chassis ids | |
− | -- | + | for _, mid in ipairs(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 | ||
+ | table.insert(ChassisIds, c.id) | ||
+ | end | ||
+ | end | ||
− | + | -- sort the table to put the new chassis ids in the correct place | |
− | + | table.sort(ChassisIds) | |
− | -- | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
+ | -- create the display list | ||
local equipList = mw.html.create('ul') | local equipList = mw.html.create('ul') | ||
equipList:cssText('column-count: 3;-moz-column-count: 3;-webkit-column-count: 3') | equipList:cssText('column-count: 3;-moz-column-count: 3;-webkit-column-count: 3') | ||
− | for _, mech in ipairs( | + | |
− | equipList:tag('li'):wikitext(string.format("%s", tostring( | + | -- for every chassis id, query the name and variant and add to the display list |
+ | for _, cid in ipairs(ChassisIds) 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 | ||
+ | equipList:tag('li'):wikitext(string.format( | ||
+ | '[[%s#%s|%s %s]]', mech['Name'], mech['VariantName'], | ||
+ | mech['Name'], mech['VariantName'] | ||
+ | )) | ||
+ | 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("%s", tostring(unknown.id))) | ||
end | end | ||
Revision as of 13:35, 7 December 2021
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=2000 } ) -- 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 table.insert(ChassisIds, user.id) elseif string.find(user.id, "mechdef", 1, true) then table.insert(MechIds, user.id) else table.insert(UnknownIds, user.id) end end -- convert mech ids to chassis ids for _, mid in ipairs(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 table.insert(ChassisIds, c.id) end end -- sort the table to put the new chassis ids in the correct place table.sort(ChassisIds) -- 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(ChassisIds) 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 equipList:tag('li'):wikitext(string.format( '[[%s#%s|%s %s]]', mech['Name'], mech['VariantName'], mech['Name'], mech['VariantName'] )) 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("%s", tostring(unknown.id))) 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 equipmentMechData = mw.ext.cargo.query( 'MechInventory,Mech,Chassis','Chassis.Name=Name,Chassis.VariantName=VariantName', { join = 'MechInventory.MechID=Mech.Id,Mech.ChassisID=Chassis.Id', where=where, limit=2000 } ) local equipList = mw.html.create('ul') equipList:cssText('column-count: 3;-moz-column-count: 3;-webkit-column-count: 3') for _, mech in ipairs(equipmentMechData) do equipList:tag('li'):wikitext(string.format( '[[%s#%s|%s %s]]', mech['Name'], mech['VariantName'], mech['Name'], mech['VariantName'] )) 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