This commit is contained in:
2019-05-30 02:16:09 -05:00
parent 765fc00aa2
commit d34d5040fb
2 changed files with 142 additions and 260 deletions

View File

@@ -62,18 +62,6 @@ class ProcessStructureJob implements ShouldQueue
$this->corpId = $jps->corpId;
$this->page = $jps->page;
//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,
]);
$this->esi = new Eseye($authentication);
//Set the connection for the job
$this->connection = 'redis';
}
@@ -91,195 +79,6 @@ class ProcessStructureJob implements ShouldQueue
{
$sHelper = new StructureHelper;
//Get the page of structures
$structures = $this->GetListOfStructures();
foreach($structures as $structure) {
$info = $this->GetStructureInfo($structure->structure_id);
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;
}
//Record the structure information into the database
//Find if the structure exists
$found = Structure::where(['structure_id' => $structure->structure_id])->get();
if(!$found) {
$sHelper->StoreNewStructure($structure, $info, $solarName);
} else {
$sHelper->UpdateExistingStructure($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($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;
$struct->structure_id = $structure->structure_id;
$struct->structure_name = $info->name;
$struct->corporation_id = $info->owner_id;
$struct->solar_system_id = $info->solar_system_id;
$struct->solary_system_name = $solarName;
if(isset($info->type_id)) {
$struct->type_id = $info->type_id;
}
$struct->corporation_id = $structure->corporation_id;
if(isset($structure->services)) {
$struct->services = true;
} else {
$struct->services = false;
}
if(isset($structure->state_timer_start)) {
$struct->state_timer_start = $this->DecodeDate($structure->state_timer_start);
}
if(isset($structure->state_timer_end)) {
$struct->state_timer_end = $this->DecodeDate($structure->state_timer_end);
}
if(isset($structure->fuel_expires)) {
$struct->fuel_expires = $structure->fuel_expires;
}
$struct->profile_id = $structure->profile_id;
$struct->position_x = $info->position->x;
$struct->position_y = $info->position->y;
$struct->position_z = $info->position->z;
if(isset($structure->next_reinforce_apply)) {
$struct->next_reinforce_apply = $structure->next_reinforce_apply;
}
if(isset($structure->next_reinforce_hour)) {
$struct->next_reinforce_hour = $structure->next_reinforce_hour;
}
if(isset($structure->next_reinforce_weekday)) {
$struct->next_reinforce_weekday = $structure->next_reinforce_weekday;
}
$struct->reinforce_hour = $structure->reinforce_hour;
if(isset($structure->reinforce_weekday)) {
$struct->reinforce_weekday = $structure->reinforce_weekday;
}
if(isset($structure->unanchors_at)) {
$struct->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
$struct->save();
}
private function GetSolarSystemName($systemId) {
//Attempt to get the solar system name from ESI
try {
$solar = $this->esi->invoke('get', '/universe/systems/{system_id}/', [
'system_id' => $systemId,
]);
} catch(RequestFailedException $e) {
$solar = null;
}
if($solar != null) {
return $solar->name;
} else {
return null;
}
}
private function GetStructureInfo($structureId) {
try {
$info = $this->esi->invoke('get', '/universe/structures/{structure_id}/', [
'structure_id' => $structureId,
]);
} catch(RequestFailedException $e) {
Log::warning("Failed to get structure information for structure with id " . $structureId);
Log::warning(var_dump($info));
Log::warning($e->getCode());
Log::warning($e->getMessage());
Log::warning($e->getEsiResponse());
$info = null;
}
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) {
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();
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,
]);
}
}
}
private function DecodeDate($date) {
$esiHelper = new Esi();
$dateTime = $esiHelper->DecodeDate($date);
return $dateTime;
$sHelper->Start($this->charId, $this->corpId, $this->page);
}
}

View File

@@ -19,66 +19,46 @@ use App\Models\Structure\Service;
class StructureHelper {
public function StoreNewStructure($structure, $info, $solarName) {
$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->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;
} else {
$st->services = false;
}
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;
}
//Save the database record
//$st->save();
DB::table('AllianceStructures')->insert($st);
/*
//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 Start($charId, $corpId, $page) {
//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);
$structures = $this->GetListOfStructures($page, $corpId, $esi);
foreach($structures as $structure) {
$info = $this->GetStructureInfo($structure->structure_id, $esi);
if(isset($info->solar_system_id)) {
$solarName = $this->GetSolarSystemName($info->solar_system_id, $esi);
} else {
Log::critical("Couldn't get solar system name for structure " . $structure->structure_id);
Log::critical("Check access lists.");
$solarName = null;
}
//Record the structure information into the database
//Find if the structure exists
$found = Structure::where(['structure_id' => $structure->structure_id])->get();
if(!$found) {
$sHelper->StoreNewStructure($structure, $info, $solarName);
} else {
$sHelper->UpdateExistingStructure($structure, $info, $solarName);
}
}
}
public function UpdateExistingStructure($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.
@@ -107,7 +87,110 @@ class StructureHelper {
}
}
public function StoreStructureServices($services, $structureId) {
private function StoreNewStructure($structure, $info, $solarName) {
$struct = new Structure;
$struct->structure_id = $structure->structure_id;
$struct->structure_name = $info->name;
$struct->corporation_id = $info->owner_id;
$struct->solar_system_id = $info->solar_system_id;
$struct->solary_system_name = $solarName;
if(isset($info->type_id)) {
$struct->type_id = $info->type_id;
}
$struct->corporation_id = $structure->corporation_id;
if(isset($structure->services)) {
$struct->services = true;
} else {
$struct->services = false;
}
if(isset($structure->state_timer_start)) {
$struct->state_timer_start = $this->DecodeDate($structure->state_timer_start);
}
if(isset($structure->state_timer_end)) {
$struct->state_timer_end = $this->DecodeDate($structure->state_timer_end);
}
if(isset($structure->fuel_expires)) {
$struct->fuel_expires = $structure->fuel_expires;
}
$struct->profile_id = $structure->profile_id;
$struct->position_x = $info->position->x;
$struct->position_y = $info->position->y;
$struct->position_z = $info->position->z;
if(isset($structure->next_reinforce_apply)) {
$struct->next_reinforce_apply = $structure->next_reinforce_apply;
}
if(isset($structure->next_reinforce_hour)) {
$struct->next_reinforce_hour = $structure->next_reinforce_hour;
}
if(isset($structure->next_reinforce_weekday)) {
$struct->next_reinforce_weekday = $structure->next_reinforce_weekday;
}
$struct->reinforce_hour = $structure->reinforce_hour;
if(isset($structure->reinforce_weekday)) {
$struct->reinforce_weekday = $structure->reinforce_weekday;
}
if(isset($structure->unanchors_at)) {
$struct->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
$struct->save();
}
private function GetSolarSystemName($systemId, $esi) {
//Attempt to get the solar system name from ESI
try {
$solar = $esi->invoke('get', '/universe/systems/{system_id}/', [
'system_id' => $systemId,
]);
} catch(RequestFailedException $e) {
$solar = null;
}
if($solar != null) {
return $solar->name;
} else {
return null;
}
}
private function GetStructureInfo($structureId, $esi) {
try {
$info = $esi->invoke('get', '/universe/structures/{structure_id}/', [
'structure_id' => $structureId,
]);
} catch(RequestFailedException $e) {
Log::warning("Failed to get structure information for structure with id " . $structureId);
Log::warning(var_dump($info));
Log::warning($e->getCode());
Log::warning($e->getMessage());
Log::warning($e->getEsiResponse());
$info = null;
}
return $info;
}
private function GetListOfStructures($page, $corpId, $esi) {
try {
$structures = $esi->page($page)
->invoke('get', '/corporations/{corporation_id}/structures/', [
'corporation_id' => $corpId,
]);
} catch (RequestFailedException $e) {
Log::critical("Failed to get structure list.");
$structures = null;
}
return $structures;
}
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([