Module:Vehicle

local vehicle = {}

local metatable = {} local methodtable = {}

metatable.__index = methodtable

local common = require( 'Module:Common' )

--- Request Api Data --- Using current subpage name without vehicle type suffix --- @return table function methodtable.getApiDataForCurrentPage( self ) local name = self.frameArgs[ 'name' ] or common.removeTypeSuffix(       mw.title.getCurrentTitle.rootText,        self.vehicleType    ) local api = mw.ext.Apiunto; --- Retrieve UUID and call API again --- Sometimes there can be multiple results for the same UUID, but getting --- it through UUID is always canonical local uuid = self.frameArgs[ 'uuid' ] or mw.text.jsonDecode( api.get_raw( 'v2/vehicles/' .. name, { locale = '' } ) )[ 'data' ].uuid

local json = mw.text.jsonDecode( api.get_raw( 'v2/vehicles/' .. uuid, { locale = '', include = { 'hardpoints', 'components', 'shops', },   } ) )

common.checkApiResponse( json, false, false )

self.apiData = json[ 'data' ]

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 can have lingering whitespaces --- TODO: Remove trim when it is fixed upstream [ 'Pledge store name' ] = mw.text.trim( 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 )

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