Module:VehicleHardpoint

local VehicleHardPoint = {}

local metatable = {} local methodtable = {}

metatable.__index = methodtable

--- Tries to fix hardpoints that have no item, but everything set on the 'child' key --- --- @param row table - API Data --- @return table - Fixed entry local function fixChild( row ) if row.item == nil and hasChildren( row ) and #row.children == 1 then local item = row.children[ 1 ]

local children = {}

if hasChildren( item ) then children = item.children if item.item ~= nil and item.item.children ~= nil then item.item.children = {} end end

row.name = item.name row.type = item.type row.sub_type = item.sub_type row.item = item.item if #children > 1 then row.children = children else row.children = { data = {} } end end

return row end

--- Sets all available hardpoints as sub-objects --- This is the main method called by others --- --- @param hardpoints table API Hardpoint data function methodtable.setHardPointObjects( self, hardpoints ) if type( hardpoints ) ~= 'table' or #hardpoints == 0 then error( 'Hardpoints need to be a table' ) end

local out = {}

local function addToOut( object, key ) if object == nil then return end

if type( out[ key ] ) ~= 'table' then if object ~= nil then out[ key ] = object out[ key ][ 'Quanity' ] = 1 end else out[ key ][ 'Quantity' ] = out[ key ][ 'Quantity' ] + 1

if type( out[ key ][ 'Magazine capacity' ] ) == 'number' then out[ key ][ 'Magazine capacity' ] = out[ key ][ 'Magazine capacity' ] + object[ 'Magazine capacity' ] end

for _, value in pairs( object[ 'Type' ] ) do				if value == 'Cargo grid' then out[ key ][ 'Quantity' ] = 1 if out[ key ][ 'SCU' ] ~= nil and object[ 'SCU' ] ~= nil then out[ key ][ 'SCU' ] = tonumber(out[ key ][ 'SCU' ]) + tonumber(object[ 'SCU' ]) end end end end end

local depth = 1

local function addHardpoints( hardpoints, parent, root ) for _, hardpoint in pairs( hardpoints ) do           hardpoint.name = string.lower( hardpoint.name ) hardpoint = fixChild( hardpoint )

if depth == 1 then root = hardpoint.name --mw.log(string.format('Root: %s', root)) end

hardpoint = VehicleHardPoint.fixTypes( hardpoint )

local hardpointData = self:getHardpointData( hardpoint.type or hardpoint.name )

if hardpointData ~= nil then local key = makeKey( hardpoint, hardpointData, parent, root )

local obj = self:makeObject( hardpoint, hardpointData, parent, root )

addToOut( obj, key )

if hasChildren( hardpoint ) then depth = depth + 1 addHardpoints( hardpoint.children, obj, root ) end end end

depth = depth - 1

if depth < 1 then depth = 1 root = nil end end

addHardpoints( hardpoints )

--mw.logObject(out)

for _, subobject in pairs( out ) do       mw.smw.subobject( subobject ) end end

--- New Instance --- --- @return table VehicleHardPoint function VehicleHardPoint.new( self, page ) local instance = { page = page or nil, spairs = require( 'Module:Common' ).spairs }

setmetatable( instance, metatable )

return instance end

return VehicleHardPoint