Module:Vehicle

local vehicle = {}

local metatable = {} local methodtable = {}

metatable.__index = methodtable

local common = require( 'Module:Common' ) local api = require( 'Module:Common/Api' )

--- Request Api Data --- Using current subpage name without vehicle type suffix --- @return table or nil function methodtable.getApiDataForCurrentPage( self ) local name = self.frameArgs.uuid or self.frameArgs[ 'name' ] or common.removeTypeSuffix(       mw.title.getCurrentTitle.rootText,        self.vehicleType    ) local json = mw.text.jsonDecode( mw.ext.Apiunto.get_raw( 'v2/vehicles/' .. name, { include = { 'hardpoints', --'components', 'shops', },   } ) )

if api.checkResponseStructure( json, true, false ) == false then return end

self.apiData = json[ 'data' ] self.apiData = api.makeAccessSafe( self.apiData )

return self.apiData end

--- Set Vehicle Semantic Properties function methodtable.setSemanticProperties( self ) if mw.title.getCurrentTitle.isSubpage then return end

common.checkSmwResult( self:setBaseSemanticProperties ) self:setExtraSemanticProperties end

--- Base Properties that are shared across all Vehicles --- @return table SMW Result function methodtable.setBaseSemanticProperties( self ) -- Api Error, don't set anything if self.apiData == nil then return end

local setData = { --- Identifier [ 'UUID' ] = self.apiData.uuid, [ 'Class name' ] = self.apiData.class_name, [ 'Pledge store name' ] = self.apiData.name, --- Stats [ 'Mass' ] = self.apiData.mass, --- Cargo [ 'Cargo capacity' ] = common.formatNum( self.apiData.cargo_capacity ), --- Speed [ 'SCM speed' ] = common.formatNum( self.apiData.speed.scm ), [ 'Maximum speed' ] = common.formatNum( self.apiData.speed.max ) }   --- Insurance if self.apiData.insurance ~= nil then setData[ 'Insurance claim time' ] = common.formatNum( self.apiData.insurance.claim_time or 0 ) setData[ 'Insurance expedite time' ] = common.formatNum( self.apiData.insurance.expedite_time or 0 ) setData[ 'Insurance expedite cost' ] = common.formatNum( self.apiData.insurance.expedite_cost or 0 ) end

local result = mw.smw.set( setData ) --- Components --- From game data if self.apiData.hardpoints ~= nil and type( self.apiData.hardpoints ) == 'table' and #self.apiData.hardpoints > 0 then local hardpoint = require( 'Module:VehicleHardpoint' ):new( self.frameArgs[ 'name' ] or mw.title.getCurrentTitle.fullText ) hardpoint:setHardPointObjects( self.apiData.hardpoints ) end

return result end

--- Extra Properties that are Vehicle (Type) Specific --- @param apiData table function methodtable.setExtraSemanticProperties( self, apiData )

end

--- Set the frame and load args --- @param frame table function methodtable.setFrame( self, frame ) self.currentFrame = frame self.frameArgs = require( 'Module:Arguments' ).getArgs( frame ) end

--- Save Api Data to SMW store function methodtable.saveApiData( self ) local data = self:getApiDataForCurrentPage

self:setSemanticProperties

return data end

--- New Instance --- @param type string Term used remove suffix from page title function vehicle.new( self, type ) if type == nil then error( 'Required argument "type" missing.' ) end

local instance = { vehicleType = type, categories = {} }

setmetatable( instance, metatable )

return instance end

return vehicle