diff --git a/app/Console/Commands/GetStructures.php b/app/Console/Commands/GetStructures.php index 6c562a157..8706bf0ed 100644 --- a/app/Console/Commands/GetStructures.php +++ b/app/Console/Commands/GetStructures.php @@ -64,13 +64,14 @@ class GetStructuresCommand extends Command //Declare some variables $charId = 93738489; $corpId = 98287666; - + $sHelper = new StructureHelper($charId, $corpId); //ESI Scope Check $esiHelper = new Esi(); $structureScope = $esiHelper->HaveEsiScope($charId, 'esi-universe.read_structures.v1'); $corpStructureScope = $esiHelper->HaveEsiScope($charId, 'esi-corporations.read_structures.v1'); + //Check scopes if($structureScope == false || $corpStructureScope == false) { if($structureScope == false) { Log::critical("Scope check for esi-universe.read_structures.v1 has failed."); @@ -90,7 +91,7 @@ class GetStructuresCommand extends Command 'secret' => $config['secret'], 'refresh_token' => $token[0]->refresh_token, ]); - + //Setup the ESI variable $esi = new Eseye($authentication); //Set the current page @@ -98,23 +99,37 @@ class GetStructuresCommand extends Command //Set our default total pages, and we will refresh this later $totalPages = 1; - try { - $structures = $esi->invoke('get', '/corporations/{corporation_id}/structures/', [ - 'corporation_id' => $corpId, - ]); - } catch (RequestFailedException $e) { - Log::critical("Failed to get structure list."); - return null; - } - if(isset($structures)) { - for($i = 1; $i <= $structures->pages; $i++) { + //Get the list of structures, and send for processing + do { + //Try to get the ESI data + 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; + } + + //Update the total pages to go through + if($totalPages == 1) { + $totalPages = $structures->pages; + } + + //For each structure we retrieve dispatch a job to process it. + foreach($structures as $structure) { $job = new JobProcessStructure; - $job->charId = $charId; - $job->corpId = $corpId; - $job->page = $i; + $job->charId; + $job->corpId; + $job->structureId; ProcessStructureJob::dispatch($job)->onQueue('structures'); - } - } + } + + //Update the page counter + $currentPage++; + //Check to see if we need to grab more pages or not. + } while($currentPage <= $totalPages); //Mark the job as finished $task->SetStopStatus(); diff --git a/app/Jobs/ProcessStructureJob.php b/app/Jobs/ProcessStructureJob.php index 8b2843dae..c9cd1bc14 100644 --- a/app/Jobs/ProcessStructureJob.php +++ b/app/Jobs/ProcessStructureJob.php @@ -49,7 +49,7 @@ class ProcessStructureJob implements ShouldQueue { $this->charId = $jps->charId; $this->corpId = $jps->corpId; - $this->page = $jps->page; + $this->structure = $jps->structure; //Set the connection for the job $this->connection = 'redis'; @@ -66,8 +66,8 @@ class ProcessStructureJob implements ShouldQueue */ public function handle() { - $sHelper = new StructureHelper($this->charId, $this->corpId, $this->page); + $sHelper = new StructureHelper($this->charId, $this->corpId); - $sHelper->Start(); + $sHelper->ProcessStructure($this->structure); } } diff --git a/app/Library/Structures/StructureHelper.php b/app/Library/Structures/StructureHelper.php index 2026cfde2..9538a318b 100644 --- a/app/Library/Structures/StructureHelper.php +++ b/app/Library/Structures/StructureHelper.php @@ -37,171 +37,60 @@ class StructureHelper { private $corpId; private $page; - public function __construct($char, $corp, $pg) { + public function __construct($char, $corp) { $this->charId = $char; $this->corpId = $corp; - $this->page = $pg; } - public function Start() { + public function ProcessStructure($structure) { + //Setup the esi authentication container + $config = config('esi'); + //Get the refresh token from the database + $token = EsiToken::where(['character_id' => $this->charId])->get(['refresh_token']); + $authentication = new EsiAuthentication([ + 'client_id' => $config['client_id'], + 'secret' => $config['secret'], + 'refresh_token' => $token[0]->refresh_token, + ]); - $solarName = null; - $structure = null; - $info = null; + $esi = new Eseye($authentication); - $structures = $this->GetListOfStructures(); + $info = $this->GetStructureInfo($structure->structure_id); - foreach($structures as $structure) { - $info = $this->GetStructureInfo($structure->structure_id); - - //Record the structure information into the database - //Find if the structure exists - if(Structure::where(['structure_id' => $structure->structure_id])->count() == 0) { - if(isset($info->solar_system_id)) { - $solarName = $this->GetSolarSystemName($info->solar_system_id); - } else { - Log::critical("Couldn't get solar system name for structure " . $structure->structure_id); - Log::critical("Check access lists."); - $solarName = null; - } - - $st = new Structure; - $st->structure_id = $structure->structure_id; - $st->structure_name = $info->name; - $st->corporation_id = $info->owner_id; - $st->solar_system_id = $info->solar_system_id; - $st->solar_system_name = $solarName; - if(isset($info->type_id)) { - $st->type_id = $info->type_id; - } - $st->corporation_id = $structure->corporation_id; - if(isset($structure->services)) { - $st->services = true; - } else { - $st->services = false; - } - if(isset($structure->state)) { - $st->state = $structure->state; - } else { - $st->state = 'None'; - } - if(isset($structure->state_timer_start)) { - $st->state_timer_start = $this->DecodeDate($structure->state_timer_start); - } - if(isset($structure->state_timer_end)) { - $st->state_timer_end = $this->DecodeDate($structure->state_timer_end); - } - if(isset($structure->fuel_expires)) { - $st->fuel_expires = $this->DecodeDate($structure->fuel_expires); - } - $st->profile_id = $structure->profile_id; - $st->position_x = $info->position->x; - $st->position_y = $info->position->y; - $st->position_z = $info->position->z; - if(isset($structure->next_reinforce_apply)) { - $st->next_reinforce_apply = $this->DecodeDate($structure->next_reinforce_apply); - } - if(isset($structure->next_reinforce_hour)) { - $st->next_reinforce_hour = $structure->next_reinforce_hour; - } - if(isset($structure->next_reinforce_weekday)) { - $st->next_reinforce_weekday = $structure->next_reinforce_weekday; - } - $st->reinforce_hour = $structure->reinforce_hour; - if(isset($structure->reinforce_weekday)) { - $st->reinforce_weekday = $structure->reinforce_weekday; - } - if(isset($structure->unanchors_at)) { - $st->unanchors_at = $structure->unanchors_at; - } - - //Save the database record - $st->save(); - } - } - } - - 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($structure->services)) { - foreach($structure->services 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) { - $struct = new Structure; - $st->structure_id = $structure->structure_id; - $st->structure_name = $info->name; - $st->corporation_id = $info->owner_id; - $st->solar_system_id = $info->solar_system_id; - $st->solary_system_name = $solarName; - if(isset($info->type_id)) { - $st->type_id = $info->type_id; - } - $st->corporation_id = $structure->corporation_id; - if(isset($structure->services)) { - $st->services = true; + //Record the structure information into the database + //Find if the structure exists + if(Structure::where(['structure_id' => $structure->structure_id])->count() == 0) { + $this->SaveNewStructure($structure); } else { - $st->services = false; + $this->UpdateExistingStructure($structure); } - if(isset($structure->state_timer_start)) { - $st->state_timer_start = $this->DecodeDate($structure->state_timer_start); - } - if(isset($structure->state_timer_end)) { - $st->state_timer_end = $this->DecodeDate($structure->state_timer_end); - } - if(isset($structure->fuel_expires)) { - $st->fuel_expires = $structure->fuel_expires; - } - $st->profile_id = $structure->profile_id; - $st->position_x = $info->position->x; - $st->position_y = $info->position->y; - $st->position_z = $info->position->z; - if(isset($structure->next_reinforce_apply)) { - $st->next_reinforce_apply = $structure->next_reinforce_apply; - } - if(isset($structure->next_reinforce_hour)) { - $st->next_reinforce_hour = $structure->next_reinforce_hour; - } - if(isset($structure->next_reinforce_weekday)) { - $st->next_reinforce_weekday = $structure->next_reinforce_weekday; - } - $st->reinforce_hour = $structure->reinforce_hour; - if(isset($structure->reinforce_weekday)) { - $st->reinforce_weekday = $structure->reinforce_weekday; - } - if(isset($structure->unanchors_at)) { - $st->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); + + } + + public function GetListOfStructures() { + //Setup the esi authentication container + $config = config('esi'); + //Get the refresh token from the database + $token = EsiToken::where(['character_id' => $this->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); + + try { + $structures = $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; } - //Save the database record - $st->save(); + return $structures; } private function GetSolarSystemName($systemId) { @@ -261,53 +150,134 @@ class StructureHelper { return $info; } - private function GetListOfStructures() { - //Setup the esi authentication container - $config = config('esi'); - //Get the refresh token from the database - $token = EsiToken::where(['character_id' => $this->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); - - try { - $structures = $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; + private function UpdateExistingStructure($structure) { + $st = Structure::where(['structure_id' => $structure->structure_id])->first(); + $st->structure_id = $structure->structure_id; + $st->structure_name = $info->name; + if(isset($structure->services)) { + $st->services = true; + } else { + $st->services = false; } + if(isset($structure->state)) { + $st->state = $structure->state; + } else { + $st->state = 'None'; + } + if(isset($structure->state_timer_start)) { + $st->state_timer_start = $this->DecodeDate($structure->state_timer_start); + } + if(isset($structure->state_timer_end)) { + $st->state_timer_end = $this->DecodeDate($structure->state_timer_end); + } + if(isset($structure->fuel_expires)) { + $st->fuel_expires = $this->DecodeDate($structure->fuel_expires); + } + $st->profile_id = $structure->profile_id; + $st->position_x = $info->position->x; + $st->position_y = $info->position->y; + $st->position_z = $info->position->z; + if(isset($structure->next_reinforce_apply)) { + $st->next_reinforce_apply = $this->DecodeDate($structure->next_reinforce_apply); + } + if(isset($structure->next_reinforce_hour)) { + $st->next_reinforce_hour = $structure->next_reinforce_hour; + } + if(isset($structure->next_reinforce_weekday)) { + $st->next_reinforce_weekday = $structure->next_reinforce_weekday; + } + $st->reinforce_hour = $structure->reinforce_hour; + if(isset($structure->reinforce_weekday)) { + $st->reinforce_weekday = $structure->reinforce_weekday; + } + if(isset($structure->unanchors_at)) { + $st->unanchors_at = $structure->unanchors_at; + } + //Save the database record + $st->save(); - return $structures; + //Update the services for the structure as well + if($st->services == true) { + //Delete the existing services, then add the new services + Service::where(['structure_id' => $structure->structure_id, ])->delete(); + + foreach($structure->services as $service) { + $serv = new Service; + $serv->sructure_id = $structure->structure_id; + $serv->name = $service['name']; + $serv->state = $service['state']; + } + } } - private function StoreStructureServices($services, $structureId) { - foreach($services as $service) { - //Find the structure id and the name of the service to see if it exists - $found = Service::where([ - 'structure_id' => $structureId, - 'name' => $service->name, - ])->get(); + private function SaveNewStructure($structure) { + if(isset($info->solar_system_id)) { + $solarName = $this->GetSolarSystemName($info->solar_system_id); + } else { + Log::critical("Couldn't get solar system name for structure " . $structure->structure_id); + Log::critical("Check access lists."); + $solarName = null; + } - if(!$found) { - $new = new Service; - $new->structure_id = $structureId; - $new->name = $service->name; - $new->state = $service->state; - $new->save(); - } else { - Service::where([ - 'structure_id' => $structureId, - 'name' => $service->name, - ])->update([ - 'state' => $service->state, - ]); + $st = new Structure; + $st->structure_id = $structure->structure_id; + $st->structure_name = $info->name; + $st->corporation_id = $info->owner_id; + $st->solar_system_id = $info->solar_system_id; + $st->solar_system_name = $solarName; + if(isset($info->type_id)) { + $st->type_id = $info->type_id; + } + $st->corporation_id = $structure->corporation_id; + if(isset($structure->services)) { + $st->services = true; + } else { + $st->services = false; + } + if(isset($structure->state)) { + $st->state = $structure->state; + } else { + $st->state = 'None'; + } + if(isset($structure->state_timer_start)) { + $st->state_timer_start = $this->DecodeDate($structure->state_timer_start); + } + if(isset($structure->state_timer_end)) { + $st->state_timer_end = $this->DecodeDate($structure->state_timer_end); + } + if(isset($structure->fuel_expires)) { + $st->fuel_expires = $this->DecodeDate($structure->fuel_expires); + } + $st->profile_id = $structure->profile_id; + $st->position_x = $info->position->x; + $st->position_y = $info->position->y; + $st->position_z = $info->position->z; + if(isset($structure->next_reinforce_apply)) { + $st->next_reinforce_apply = $this->DecodeDate($structure->next_reinforce_apply); + } + if(isset($structure->next_reinforce_hour)) { + $st->next_reinforce_hour = $structure->next_reinforce_hour; + } + if(isset($structure->next_reinforce_weekday)) { + $st->next_reinforce_weekday = $structure->next_reinforce_weekday; + } + $st->reinforce_hour = $structure->reinforce_hour; + if(isset($structure->reinforce_weekday)) { + $st->reinforce_weekday = $structure->reinforce_weekday; + } + if(isset($structure->unanchors_at)) { + $st->unanchors_at = $structure->unanchors_at; + } + + //Save the database record + $st->save(); + + if($st->services == true) { + foreach($structure->services as $service) { + $serv = new Service; + $serv->sructure_id = $structure->structure_id; + $serv->name = $service['name']; + $serv->state = $service['state']; } } } diff --git a/app/Models/Jobs/JobProcessStructure.php b/app/Models/Jobs/JobProcessStructure.php index 1d0cfc326..40e4b4da8 100644 --- a/app/Models/Jobs/JobProcessStructure.php +++ b/app/Models/Jobs/JobProcessStructure.php @@ -20,6 +20,6 @@ class JobProcessStructure extends Model protected $fillable = [ 'charId', 'corpId', - 'page', + 'structure', ]; }