updated some mining ledger functions, and removed duplicate functionality

This commit is contained in:
2021-01-24 21:15:46 +09:00
parent 314dd1c931
commit 416f4abd03
5 changed files with 113 additions and 168 deletions

View File

@@ -2,24 +2,74 @@
namespace App\Jobs;
//Internal Library
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Carbon\Carbon;
//App Library
use Seat\Eseye\Exceptions\RequestionFailedException;
use App\Library\Esi\Esi;
use App\Library\Lookups\LookupHelper;
//App Models
use App\Models\MiningTax\Observer;
use App\Models\MiningTax\Ledger;
class FetchMiningTaxesLedgersJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Timeout in seconds
*
* @var int
*/
public $timeout = 3600;
/**
* Number of job retries
*
* @var int
*/
public $tries = 3;
/**
* Job Variables
*/
private $charId;
private $corpId;
private $observerId;
private $refreshToken;
private $esi;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct()
public function __construct($corpId, $observerId)
{
//
//Set the connection for the job
$this->connection = 'redis';
//Import the variables from the calling function
$this->charId = $charId;
$this->corpId = $corpId;
$this->observerId = $observerId;
$this->$esi = new Esi;
//Setup the private esi variables
if(!$this->esi->haveEsiScope($this->charId, 'esi-industry.read_corporation_mining.v1')) {
Log::critical('Character: ' . $this->charId . ' did not have the correct esi scope in FetchMiningTaxesLedgersJob.');
return null;
}
$this->refreshToken = $this->esi->GetRefreshToken($this->charId);
$this->esi = $this->esi->SetupEsiAuthentication($this->refreshToken);
}
/**
@@ -29,6 +79,50 @@ class FetchMiningTaxesLedgersJob implements ShouldQueue
*/
public function handle()
{
//
//Declare variables
$lookup = new LookupHelper;
$final = array();
$items = array();
$notSorted = array();
//Get the ledger from ESI
try {
$ledgers = $this->esi->invoke('get', '/corporation/{corporation_id}/mining/observers/{observer_id}/', [
'corporation_id' => $this->corpId,
'observer_id' => $this->observerId,
]);
} catch(RequestFailedException $e) {
Log::warning('Failed to get the mining ledger in FetchMiningTaxesLedgersJob for observer id: ' . $this->observerId);
return null;
}
//Sort through the array, and create the variables needed for database entries
foreach($ledgers as $ledger) {
//Get some basic information we need to work with
$charName = $lookup->CharacterIdToName($ledger->character_id);
$typeName = $lookup->ItemIdToName($ledger->ore);
$updated = $this->esi->DecodeDate($ledger->last_updated);
//Insert or update the entry in the database
$item = Ledger::updateOrCreate([
'character_id' => $ledger->character_id,
'character_name' => $charName,
'last_updated' => $updated,
'type_id' => $ledger->type_id,
'ore_name' => $typeName,
'quantity' => $ledger->quantity,
], [
'character_id' => $ledger->character_id,
'character_name' => $charName,
'last_updated' => $updated,
'type_id' => $ledger->type_id,
'ore_name' => $typeName,
'quantity' => $ledger->quantity,
]);
}
//Clean up old data
Ledger::where(['updated_at', '<', Carbon::now()->subDays(120)])->delete();
}
}

View File

@@ -64,37 +64,25 @@ class AssetHelper {
* Store a new asset record in the database
*/
public function StoreNewAsset($asset) {
//See if we find any assets which already exist
$found = Asset::where([
Asset::updateOrCreate([
'item_id' => $asset->item_id,
])->count();
//If nothing is found
if($found == 0) {
$item = new Asset;
if(isset($asset->is_blueprint_copy)) {
$item->is_blueprint_copy = $asset->is_blueprint_copy;
}
$item->is_singleton = $asset->is_singleton;
$item->item_id = $asset->item_id;
$item->location_flag = $asset->location_flag;
$item->location_id = $asset->location_id;
$item->location_type = $asset->location_type;
$item->quantity = $asset->quantity;
$item->type_id = $asset->type_id;
$item->save();
} else {
$this->UpdateAsset($asset);
}
], [
'is_blueprint_copy' => $asset->is_blueprint_copy,
'is_singleton' => $asset->is_singleton,
'item_id' => $asset->item_id,
'location_flag' => $asset->location_flag,
'location_id' => $asset->location_id,
'location_type' => $asset->location_type,
'quantity' => $asset->quantity,
'type_id' => $asset->type_id,
]);
}
/**
* Purge old data, so we don't run into data issues
*/
public function PurgeStaleData() {
$date = Carbon::now()->subDay(1);
Asset::where('updated_at', '<', $date)->delete();
Asset::where('updated_at', '<', Carbon::now()->subDay())->delete();
}
/**
@@ -120,24 +108,6 @@ class AssetHelper {
return $asset['quantity'];
}
}
/**
* Update an existing asset based off the esi pull
*/
private function UpdateAsset($asset) {
Asset::where([
'item_id' => $asset->item_id,
])->update([
'is_singleton' => $asset->is_singleton,
'location_flag' => $asset->location_flag,
'location_id' => $asset->location_id,
'location_type' => $asset->location_type,
'quantity' => $asset->quantity,
'type_id' => $asset->type_id,
'updated_at' => Carbon::now(),
]);
}
}
?>

View File

@@ -78,73 +78,6 @@ class Esi {
return $corporation;
}
public function GetCharacterName($charId) {
$esi = $this->SetupEsiAuthentication;
try {
$character = $esi->invoke('get', '/characters/{character_id}/', [
'character_id' => $charId,
]);
} catch(RequestFailedException $e) {
return null;
}
return $character->name;
}
public function FindCharacterId($name) {
$config = config('esi');
$esi = $this->SetupEsiAuthentication();
try {
$character = $esi->setBody(array(
$name,
))->invoke('post', '/universe/ids/');
} catch(RequestFailedException $e) {
return null;
}
if(isset($character->characters[0]->id)) {
return $character->characters[0]->id;
} else {
return null;
}
}
public function FindCorporationId($charId) {
$esi = $this->SetupEsiAuthentication();
try {
$character = $esi->invoke('get', '/characters/{character_id}/', [
'character_id' => $charId,
]);
} catch(RequestFailedException $e) {
return null;
}
return $character->corporation_id;
}
public function FindCorporationName($charId) {
$esi = $this->SetupEsiAuthentication();
try {
$character = $esi->invoke('get', '/characters/{character_id}/', [
'character_id' => $charId,
]);
$corporation = $esi->invoke('get', '/corporations/{corporation_id}/', [
'corporation_id' => $character->corporation_id,
]);
} catch(RequestFailedException $e) {
return null;
}
return $corporation->name;
}
public function DecodeDate($date) {
//Find the end of the date
$dateEnd = strpos($date, "T");

View File

@@ -80,6 +80,7 @@ class MiningLedgerHelper {
public function GetMiningStructureInfo($observerId) {
//Declare variables
$esiHelper = new Esi;
$lookup = new LookupHelper;
//Check for ESI scopes
if(!$esiHelper->HaveEsiScope($this->charId, 'esi-universe.read_structures.v1')) {
@@ -98,7 +99,7 @@ class MiningLedgerHelper {
return null;
}
$system = $this->GetSolarSystemName($info->solar_system_id);
$system = $lookup->SystemIdToName($info->solar_system_id);
return [
'name' => $info->name,
@@ -161,7 +162,7 @@ class MiningLedgerHelper {
//Sort through the array and replace character id with name and item id with name
foreach($items as $item) {
$charName = $lookup->CharacterIdToName($item->character_id);
$typeName = $this->GetItemName($item->type_id);
$typeName = $lookup->ItemIdToName($item->type_id);
$corpName = $lookup->CorporationIdToName($item->recorded_corporation_id);
if(isset($final[$charName])) {
@@ -183,59 +184,5 @@ class MiningLedgerHelper {
return $final;
}
/**
* Get the type id and return the name of the ore
*
* @var typeId
* @return string
*/
private function GetItemName($typeId) {
//Setup the esi helper variable
$esiHelper = new Esi;
//Setup the authentication container for ESI
$esi = $esiHelper->SetupEsiAuthentication();
try {
$item = $esi->invoke('get', '/universe/types/{type_id}/', [
'type_id' => $typeId,
]);
} catch(RequestFailedException $e) {
return null;
}
return $item->name;
}
/**
* Get the solar system name
*
* @var systemId
* @return string
*/
private function GetSolarSystemName($systemId) {
//Setup the esi helper variable
$esiHelper = new Esi;
//Setup the authentication container for ESI
$esi = $esiHelper->SetupEsiAuthentication();
//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;
}
}
}
?>

View File

@@ -42,6 +42,7 @@ class CreateMiningTaxTables extends Migration
Schema::create('alliance_mining_tax_ledgers', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('character_id');
$table->string('character_name');
$table->dateTime('last_updated');
$table->unsignedBigInteger('type_id');
$table->string('ore_name');