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 or nil function methodtable.getApiDataForCurrentPage( self ) --- @param s string Query for API local function getApiJson( s ) if s == nil then return end local json = mw.text.jsonDecode( mw.ext.Apiunto.get_raw( 'v2/vehicles/' .. s, { locale = '', include = { 'hardpoints', 'components', 'shops', },	   } ) )	    if common.checkApiResponse( json, false, false ) then return json end end

local function getUuid( json ) if json ~= nil and json.data.uuid ~= nil then return json.data.uuid end end

local name = self.frameArgs[ 'name' ] or common.removeTypeSuffix(       mw.title.getCurrentTitle.rootText,        self.vehicleType    ) local jsonFromName = getApiJson( name )

local uuid = self.frameArgs[ 'uuid' ] or getUuid( jsonFromName ) local jsonFromUuid = getApiJson( uuid )

if jsonFromUuid ~= nil then self.apiData = jsonFromUuid.data elseif jsonFromName ~= nil then self.apiData = jsonFromName.data end

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