Skip to content

Commit

Permalink
Addition of CertificateSync Job
Browse files Browse the repository at this point in the history
  • Loading branch information
flyingferret committed Feb 3, 2020
1 parent 02a5bf1 commit 9f6d0be
Show file tree
Hide file tree
Showing 9 changed files with 168 additions and 142 deletions.
40 changes: 40 additions & 0 deletions src/Commands/CorporationCertificateSync.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/**
* Created by PhpStorm.
* User: Mutterschiff
* Date: 06.02.2018
* Time: 23:22.
*/

namespace FlyingFerret\Seat\WHTools\Commands;

use Seat\Eveapi\Models\Corporation\CorporationInfo;
use FlyingFerret\Seat\WHTools\Jobs\CertificatesSync;
use Illuminate\Console\Command;

class CorporationCertificateSync extends Command
{

protected $signature = 'seat-whtools:CorporationCertificates:sync {--corporation_ids= : The id list of characters in SeAT (using , as separator)}';

protected $description = 'Fire a job which attempts to update certificate ranks for all characters in the give corporation';

public function handle()
{

if(! is_null($this->option('corporation_ids'))) {
// transform the argument list in an array
$ids = explode(',', $this->option('corporation_ids'));
$corporation_ids = collect();

$corporations = CorporationInfo::whereIn('corporation_id', $ids)->get();

} else {
$corporations = CorporationInfo::all();
$this->info('A synchronization job has been queued in order to update all Corporation Member certificates.');
}
foreach ($corporations as $corp) {
dispatch(new CertificatesSync($corp));
}
}
}
2 changes: 1 addition & 1 deletion src/Config/whtools.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@

return [

'version' => '0.1.5',
'version' => '0.1.6',

];
22 changes: 13 additions & 9 deletions src/Http/Controllers/SkillCheckerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

use FlyingFerret\Seat\WHTools\Models\Certificate;
use FlyingFerret\Seat\WHTools\Models\CertificateSkill;
use FlyingFerret\Seat\WHTools\Models\CharacterCertificate;
use Seat\Web\Http\Controllers\Controller;
use FlyingFerret\Seat\WHTools\Models\Sde\DgmTypeAttributes;
use FlyingFerret\Seat\WHTools\Models\Sde\InvType;
Expand All @@ -31,6 +32,7 @@

use Seat\Eveapi\Models\Character\CharacterInfo;
use Seat\Eveapi\Models\Corporation\CorporationInfo;
use FlyingFerret\Seat\WHTools\Jobs\CertificatesSync;

/**
* Class HomeController
Expand Down Expand Up @@ -198,15 +200,17 @@ public function getCharacterCerts($characterID){
}

public function getCorporationCertificates($corporationID){
$corporationCertificates = [];
$characters = CorporationInfo::where('corporation_id',$corporationID)->firstOrFail()->characters()->get();
foreach ($characters as $character){
$data = [];
array_push($data,['Character'=> $character]);
array_push($data,['CharacterCerts'=> $this->getCharacterCerts($character->character_id)]);
array_push($corporationCertificates,['data'=>$data]);
$data = [];
$corp = CorporationInfo::findOrFail($corporationID);
$corpCerts = collect();
foreach($corp->characters()->get() as $character){
$corpCerts->push(CharacterCertificate::where('character_id',$character->character_id)->get());
}
return json_encode($corporationCertificates);
return json_encode($corpCerts);
}
public function test()
{
$corp = CorporationInfo::findOrFail('98560621');
dispatch(new CertificatesSync($corp));
return $corp->characters()->get();
}
}
7 changes: 6 additions & 1 deletion src/Http/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,5 +186,10 @@
'as' => 'whtools.getcharskills',
'uses' => 'SkillCheckerController@getCorporationCertificates',
'middleware' => 'bouncer:whtools.certview'
]);
]);
// Route::get('/test/',[
// 'as' => 'whtools.test',
// 'uses' => 'SkillCheckerController@test',
// 'middleware' => 'bouncer:whtools.certview'
// ]);
});
146 changes: 28 additions & 118 deletions src/Jobs/CertificatesSync.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@

namespace FlyingFerret\Seat\WHTools\Jobs;

use FlyingFerret\Seat\WHTools\Models\Certificate;
use FlyingFerret\Seat\WHTools\Models\CharacterCertificate;
use Seat\Eveapi\Models\Corporation\CorporationInfo;


class CertificatesSync extends WHToolsJobBase
{
Expand All @@ -12,143 +16,49 @@ class CertificatesSync extends WHToolsJobBase
protected $tags = ['sync'];

/**
* @var \Seat\Web\Models\Group
*/
private $group;

/**
* @var \Seat\Eveapi\Models\Character\CharacterInfo
* @var int
*/
private $main_character;
public $tries = 1;

/**
* @var int
* @var \Seat\Eveapi\Models\Corporation\CorporationInfo
*/
public $tries = 1;
private $corporation;

/**
* ConversationOrchestrator constructor.
*
* @param \Seat\Web\Models\Group $group
* @param \Seat\Eveapi\Models\Corporation\CorporationInfo $corp
*/
public function __construct(Group $group)
public function __construct(CorporationInfo $corp)
{
$this->group = $group;
$this->main_character = $group->main_character;
if (is_null($group->main_character)) {
logger()->warning('Group has no main character set. Attempt to make assignation based on first attached character.', [
'group_id' => $group->id,
]);
$this->main_character = $group->users->first()->character;
}

// avoid the construct to throw an exception if no character has been set
if (!is_null($this->main_character)) {
logger()->debug('Initialising SeAT Group sync for ' . $this->main_character->name);

array_push($this->tags, sprintf('users: %s',
$this->group->users->map(function ($user) {
return $user->name;
})->implode(', ')));
}

$this->corporation = $corp;
}

public function handle()
{
// in case no main character has been set, throw an exception and abort the process
if (is_null($this->main_character))
throw new MissingMainCharacterException($this->group);

Redis::funnel('seat-groups:jobs.group_sync_' . $this->group->id)->limit(1)->then(function () {
$this->beforeStart();

try {
$roles = collect();
$group = $this->group;

//Catch Superuser
foreach ($group->roles as $role) {
if ($role->title === 'Superuser') {
$roles->push($role->id);
$characters = $this->corporation->characters()->get();
$allCerts = Certificate::get();

foreach($characters as $character){
foreach($allCerts as $cert){

$certSkills = $cert->skills()->get();
$certRank = 5;
foreach ($certSkills as $certSkill){
$charSkill = $character->skills()->where('skill_id',$certSkill->skillID)->first();
if(isset($charSkill) and $charSkill->trained_skill_level < $certSkill->requiredLvl){
$certRank = $certSkill->certRank - 1;
}
}

Seatgroup::all()->each(function ($seat_group) use ($roles, $group) {

if ($seat_group->isQualified($group)) {
switch ($seat_group->type) {
case 'auto':
foreach ($seat_group->role as $role) {
$roles->push($role->id);
}
if (!in_array($group->id, $seat_group->group->pluck('id')->toArray())) {
// add user_group to seat_group as member if no member yet.
$seat_group->member()->attach($group->id);
}
break;
case 'open':
case 'managed':
case 'hidden':
// check if user is in the group
if ($seat_group->isMember($group)) {
foreach ($seat_group->role as $role) {
$roles->push($role->id);
}
}
break;
}
} elseif (in_array($group->id, $seat_group->group->pluck('id')->toArray())) {
$seat_group->member()->detach($group->id);
}
});

$group->roles()->sync($roles->unique());

$this->onFinish();

logger()->debug('Group has beend synced for ' . $this->main_character->name);

} catch (\Throwable $exception) {

$this->onFail($exception);

}

}, function () {
logger()->warning('A GroupSync job is already running for ' . $this->main_character->name . ' Removing the job from the queue.');

$this->delete();
});

}

public function beforeStart()
{

foreach ($this->group->users as $user) {

// If a RefreshToken is missing
if (is_null($user->refresh_token)) {
// take away all roles
$this->group->roles()->sync([]);
Seatgroup::all()->each(function ($seatgroup) {
$seatgroup->member()->detach($this->group->id);
});

SeatgroupLog::create([
'event' => 'warning',
'message' => sprintf('The RefreshToken of %s is missing, therefore user group of %s (%s) loses all permissions.',
$user->name, $this->main_character->name, $this->group->users->map(function ($user) {
return $user->name;
})->implode(', ')),

]);

// throw exception
throw new MissingRefreshTokenException();
CharacterCertificate::updateOrCreate(
['character_id'=>$character->character_id,'certID'=>$cert->name],
['character_name'=>$character->name,'cert_name'=>$cert->name,'rank'=>$certRank]
);
}
}

}

public function onFail($exception)
Expand Down
22 changes: 22 additions & 0 deletions src/Models/CharacterCertificate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php


namespace FlyingFerret\Seat\WHTools\Models;

use Illuminate\Database\Eloquent\Model;

class CharacterCertificate extends Model
{
public $timestamps = true;

protected $table = 'whtools-characterCertificates';

protected $fillable = ['character_id','character_name','certID','cert_name','rank'];

public function certificate(){
return $this->hasOne('FlyingFerret\Seat\WHTools\Models\Certificate','certID','certID');
}
public function character(){
return $this->hasOne(Seat\Eveapi\Models\Character\CharacterInfo::class ,'character_id','character_id');
}
}
7 changes: 6 additions & 1 deletion src/WHToolsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ public function boot()

// Add the views for WHTools
$this->add_views();

//add commands for WHTools
$this->addCommands();

// Add the migrations for WHTools
$this->add_migrations();
Expand Down Expand Up @@ -72,7 +75,9 @@ public function register()

private function addCommands()
{

$this->commands([
Commands\CorporationCertificateSync::class,
]);
}

private function add_migrations()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateCharacterCertificatesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('whtools-characterCertificates', function (Blueprint $table) {
$table->bigInteger('character_id');
$table->string('character_name');
$table->integer('certID');
$table->string('cert_name');
$table->smallInteger('rank');
$table->timestamps();
});

}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('whtools-characterCertificates');
}
}
Loading

0 comments on commit 9f6d0be

Please sign in to comment.