From f479a7066cb15898dd4cb8782b074492ee8496ea Mon Sep 17 00:00:00 2001 From: drkthunder02 Date: Tue, 28 May 2019 22:50:43 -0500 Subject: [PATCH] assets and structure job modifications --- app/Console/Commands/GetAssets.php | 54 ++++++- app/Console/Commands/GetStructures.php | 46 +++--- app/Jobs/ProcessStructureJob.php | 204 +++++++++++++++--------- app/Models/Jobs/JobProcessStructure.php | 5 +- 4 files changed, 206 insertions(+), 103 deletions(-) diff --git a/app/Console/Commands/GetAssets.php b/app/Console/Commands/GetAssets.php index a755d81ed..ec141a0e5 100644 --- a/app/Console/Commands/GetAssets.php +++ b/app/Console/Commands/GetAssets.php @@ -53,6 +53,58 @@ class GetAssets extends Command */ public function handle() { - // + //Create the command helper container + $task = new CommandHelper('GetAssets'); + //Add the entry into the jobs table saying the job is starting + $task->SetStartStatus(); + + //Declare some variables + $charId = 93738849; + $corpId = 98287666; + + //ESI Scope Check + $esiHelper = new Esi(); + $assetScope = $esiHelper->HaveEsiScope(93738489, 'esi-assets.read_corporation_assets.v1'); + + if($assetScope == false) { + Log::critical("Scope check for esi failed."); + return null; + } + + //Setup the esi authentication container + $config = config('esi'); + //Get the refresh token from the database + $token = EsiToken::where(['character_id' => $charId])->get(['refresh_token']); + $authentication = new EsiAuthentication([ + 'client_id' => $config['client_id'], + 'secret' => $config['secret'], + 'refresh_token' => $token[0]->refresh_token, + ]); + + $esi = new Eseye($authentication); + + //Set the current page + $currentPage = 1; + //Set our default total pages, and we will refresh this later + $totalPages = 1; + + try { + $assets = $esi->page($currentPage) + ->invoke('get', '/corporations/{corporation_id}/assets/', [ + 'corporation_id' => $corpId, + ]); + } catch (RequestFailedException $e) { + // + } + + $totalPages = $assets->pages; + + for($i = 1; $i <= $totalPages; $i++) { + $job = new JobProcessAsset; + $job->charId = $charId; + $job->corpId = $corpId; + $job->page = $i; + ProcessAssetJob::dispatch($job)->onQueue('default'); + } } } diff --git a/app/Console/Commands/GetStructures.php b/app/Console/Commands/GetStructures.php index 783021d32..16f72683c 100644 --- a/app/Console/Commands/GetStructures.php +++ b/app/Console/Commands/GetStructures.php @@ -57,6 +57,10 @@ class GetStructures extends Command //Add the entry into the jobs table saying the job is starting $task->SetStartStatus(); + //Declare some variables + $charId = 93738849; + $corpId = 98287666; + //ESI Scope Check $esiHelper = new Esi(); $structureScope = $esiHelper->HaveEsiScope($charId, 'esi-universe.read_structures.v1'); @@ -84,32 +88,26 @@ class GetStructures extends Command //Set our default total pages, and we will refresh this later $totalPages = 1; - do { - try { - $structures = $esi->page($currentPage) - ->invoke('get', '/corporations/{corporation_id}/structures/', [ - 'corporation_id' => 98287666, - ]); - } catch (RequestFailedException $e) { - Log::critical("Failed to get structure list."); - return null; - } + try { + $structures = $esi->page($currentPage) + ->invoke('get', '/corporations/{corporation_id}/structures/', [ + 'corporation_id' => $corpId, + ]); + } catch (RequestFailedException $e) { + Log::critical("Failed to get structure list."); + return null; + } - //Set the total pages we need to cycle through - if($totalPages == 1) { - $totalPages = $structures->pages; - } + $totalPages = $structures->pages; - //Dispatch a job to get all of the structure information from ESI - foreach($structures as $structure) { - $job = new JobProcessStructure; - $job->charId = 93738489; - $job->corpId = 98287666; - $job->structure = $structure; - ProcessStructureJob::dispatch($job)->onQueue('default'); - } - - } while ($currentPage < $totalPages); + for($i = 1; $i <= $totalPages; $i++) { + $job = new JobProcessStructure; + $job->charId = $charId; + $job->corpId = $corpId; + $job->page = $i; + $job->esi = $esi; + ProcessStructureJob::dispatch($job)->onQueue('default'); + } //Mark the job as finished $task->SetStopStatus(); diff --git a/app/Jobs/ProcessStructureJob.php b/app/Jobs/ProcessStructureJob.php index f213b81ca..50b1ed9db 100644 --- a/app/Jobs/ProcessStructureJob.php +++ b/app/Jobs/ProcessStructureJob.php @@ -39,7 +39,7 @@ class ProcessStructureJob implements ShouldQueue */ private $charId; private $corpId; - private $structure; + private $page; /** @@ -52,6 +52,8 @@ class ProcessStructureJob implements ShouldQueue $this->charId = $jps->charId; $this->corpId = $jps->corpId; $this->structure = $jps->structure; + $this->page = $jps->page; + $this->esi = $jps->esi; //Set the connection for the job $this->connection = 'redis'; @@ -77,96 +79,146 @@ class ProcessStructureJob implements ShouldQueue return null; } - //Setup the Eseye container and authenticate it. - $config = config('esi'); - $token = EsiToken::where(['character_id' => 93738489])->get(['refresh_token']); - $authentication = new EsiAuthentication([ - 'client_id' => $config['client_id'], - 'secret' => $config['secret'], - 'refresh_token' => $token[0]->refresh_token, - ]); + //Get the page of structures + $structures = $this->GetListOfStructures(); - //Declare the esi variable - $esi = new Eseye($authentication); + foreach($structures as $structure) { + $info = $this->GetStructureInfo($structure['structure_id']); - try { - $info = $esi->invoke('get', '/universe/structures/{structure_id}/', [ - 'structure_id' => $this->structure['structure_id'], - ]); - } catch(RequestFailedException $e) { - return null; + $solarName = $this->GetSolarSystemName($info['solar_system_id']); + + + //Record the structure information into the database + //Find if the structure exists + $found = Structure::where(['structure_id' => $structure['structure_id']])->get(); + if($found) { + $this->UpdateExistingStructure($structure, $info, $solarName); + } else { + $this->StoreNewStructure($structure, $info, $solarName); + } + } + } + + private function UpdateExistingStructure($structure, $info, $solarName) { + //For each line see if it is part of the structure array, and attempt to modify each variable + //This will be implemented in the near future. + + if(isset($structures['services'])) { + foreach($structure['service'] as $service) { + //Search for the service, and if found, update it, else add it. + $serviceFound = Service::where([ + 'structure_id' => $structure['structure_id'], + 'name' => $service['name'], + ])->get(); + if($serviceFound) { + Service::where([ + 'structure_id' => $structure['structure_id'], + 'name' => $service['name'], + ])->update([ + 'state' => $service['state'], + ]); + } else { + $newService = new Service; + $newService->structure_id = $structure['structure_id']; + $newService->name = $service['name']; + $newService->state = $service['state']; + } + + } + } + } + + private function StoreNewStructure($structure, $info, $solarName) { + $structure = new Structure; + $structure->structure_id = $structure['structure_id']; + $structure->structure_name = $info['name']; + $structure->corporation_id = $info['owner_id']; + $structure->solar_system_id = $info['solar_system_id']; + $structure->solary_system_name = $solarName; + if(isset($info['type_id'])) { + $structure->type_id = $info['type_id']; + } + $structure->corporation_id = $structure['corporation_id']; + if(isset($structures['services'])) { + $structure->services = true; + } else { + $structure->services = false; + } + if(isset($structure['state_timer_start'])) { + $structure->state_timer_start = $this->DecodeDate($structure['state_timer_start']); + } + if(isset($structure['state_timer_end'])) { + $structure->state_timer_end = $this->DecodeDate($structure['state_timer_end']); + } + if(isset($structure['fuel_expires'])) { + $structure->fuel_expires = $structure['fuel_expires']; + } + $structure->profile_id = $structure['profile_id']; + $structure->position_x = $info['position']['x']; + $structure->position_y = $info['position']['y']; + $structure->position_z = $info['position']['z']; + if(isset($structure['next_reinforce_apply'])) { + $structure->next_reinforce_apply = $structure['next_reinforce_apply']; + } + if(isset($structure['next_reinforce_hour'])) { + $structure->next_reinforce_hour = $structure['next_reinforce_hour']; + } + if(isset($structure['next_reinforce_weekday'])) { + $structure->next_reinforce_weekday = $structure['next_reinforce_weekday']; + } + $structure->reinforce_hour = $structure['reinforce_hour']; + if(isset($structure['reinforce_weekday'])) { + $structure->reinforce_weekday = $structure['reinforce_weekday']; + } + if(isset($structure['unanchors_at'])) { + $structure->unanchors_at = $structure['unanchors_at']; + } + //If we set the structure services to true, let's save the services + if($structure->services == true) { + $this->StorestructureServices($structure['services'], $structure['structure_id']); } + //Save the database record + $structure->save(); + } + + private function GetSolarSystemName($systemId) { //Attempt to get the solar system name from ESI try { - $solarName = $esi->invoke('get', '/universe/systems/{system_id}/', [ - 'system_id' => $info['solary_system_id'], + $solarName = $this->esi->invoke('get', '/universe/systems/{system_id}/', [ + 'system_id' => $systemId, ]); } catch(RequestFailedException $e) { $solarName = null; } - //Record the structure information into the database - //Find if the structure exists - $found = Structure::where(['structure_id' => $this->structure['structure_id']])->get(); - if($found) { + return $solarName; + } - } else { - $structure = new Structure; - $structure->structure_id = $this->structure['structure_id']; - $structure->structure_name = $info['name']; - $structure->corporation_id = $info['owner_id']; - $structure->solar_system_id = $info['solar_system_id']; - $structure->solary_system_name = $solarName; - if(isset($info['type_id'])) { - $structure->type_id = $info['type_id']; - } - $structure->corporation_id = $this->structure['corporation_id']; - if(isset($this->structures['services'])) { - $structure->services = true; - } else { - $structure->services = false; - } - if(isset($this->structure['state_timer_start'])) { - $structure->state_timer_start = $this->DecodeDate($this->structure['state_timer_start']); - } - if(isset($this->structure['state_timer_end'])) { - $structure->state_timer_end = $this->DecodeDate($this->structure['state_timer_end']); - } - if(isset($this->structure['fuel_expires'])) { - $structure->fuel_expires = $this->structure['fuel_expires']; - } - $structure->profile_id = $this->structure['profile_id']; - $structure->position_x = $info['position']['x']; - $structure->position_y = $info['position']['y']; - $structure->position_z = $info['position']['z']; - if(isset($this->structure['next_reinforce_apply'])) { - $structure->next_reinforce_apply = $this->structure['next_reinforce_apply']; - } - if(isset($this->structure['next_reinforce_hour'])) { - $structure->next_reinforce_hour = $this->structure['next_reinforce_hour']; - } - if(isset($this->structure['next_reinforce_weekday'])) { - $structure->next_reinforce_weekday = $this->structure['next_reinforce_weekday']; - } - $structure->reinforce_hour = $this->structure['reinforce_hour']; - if(isset($this->structure['reinforce_weekday'])) { - $structure->reinforce_weekday = $this->structure['reinforce_weekday']; - } - if(isset($this->structure['unanchors_at'])) { - $structure->unanchors_at = $this->structure['unanchors_at']; - } - //If we set the structure services to true, let's save the services - if($structure->services == true) { - $this->StorestructureServices($this->structure['services'], $this->structure['structure_id']); - } - - //Save the database record - $structure->save(); + private function GetStructureInfo($structureId) { + try { + $info = $this->esi->invoke('get', '/universe/structures/{structure_id}/', [ + 'structure_id' => $structureId, + ]); + } catch(RequestFailedException $e) { + $info = null; } - //Record the structure's services information into the database + return $info; + } + private function GetListOfStructures() { + try { + $structures = $this->esi->page($this->page) + ->invoke('get', '/corporations/{corporation_id}/structures/', [ + 'corporation_id' => $this->corpId, + ]); + } catch (RequestFailedException $e) { + Log::critical("Failed to get structure list."); + $structures = null; + } + + return $structures; } private function StoreStructureServices($services, $structureId) { diff --git a/app/Models/Jobs/JobProcessStructure.php b/app/Models/Jobs/JobProcessStructure.php index 8996c6401..0c8f4cc51 100644 --- a/app/Models/Jobs/JobProcessStructure.php +++ b/app/Models/Jobs/JobProcessStructure.php @@ -19,7 +19,8 @@ class JobProcessStructure extends Model */ protected $fillable = [ 'charId', - 'corporationId', - 'structure', + 'corpId', + 'page', + 'esi', ]; }