From cb4210318a62bfdda2b1b0b69357f1a4d9f6e375 Mon Sep 17 00:00:00 2001 From: recursivetree <60423027+recursivetree@users.noreply.github.com> Date: Tue, 20 Aug 2024 20:27:05 +0200 Subject: [PATCH] schedule: apply character scheduling rules to tokens --- .../Configuration/ScheduleController.php | 9 ++++ .../CharacterFilterDataUpdatedTokens.php | 14 ++++++ src/Models/CharacterSchedulingRule.php | 48 ++++++++++++++++++- .../CharacterAffiliationObserver.php | 3 +- src/WebServiceProvider.php | 2 + 5 files changed, 73 insertions(+), 3 deletions(-) create mode 100644 src/Listeners/CharacterFilterDataUpdatedTokens.php diff --git a/src/Http/Controllers/Configuration/ScheduleController.php b/src/Http/Controllers/Configuration/ScheduleController.php index 3623fa008..304b74ac4 100644 --- a/src/Http/Controllers/Configuration/ScheduleController.php +++ b/src/Http/Controllers/Configuration/ScheduleController.php @@ -24,6 +24,7 @@ use Artisan; use Illuminate\Http\Request; +use Seat\Eveapi\Models\RefreshToken; use Seat\Services\Models\Schedule; use Seat\Web\Http\Controllers\Controller; use Seat\Web\Http\Validation\NewSchedule; @@ -117,6 +118,10 @@ public function createSchedulingRule(Request $request) $rule->filter = $request->filters; $rule->save(); + RefreshToken::all()->each(function ($token){ + CharacterSchedulingRule::updateRefreshTokenSchedule($token); + }); + return redirect()->back() ->with('success', 'Character Scheduling Rule added!'); } @@ -129,6 +134,10 @@ public function deleteSchedulingRule(Request $request) CharacterSchedulingRule::destroy($request->rule_id); + RefreshToken::all()->each(function ($token){ + CharacterSchedulingRule::updateRefreshTokenSchedule($token); + }); + return redirect()->back()->with('success','Successfully removed character scheduling rule!'); } } diff --git a/src/Listeners/CharacterFilterDataUpdatedTokens.php b/src/Listeners/CharacterFilterDataUpdatedTokens.php new file mode 100644 index 000000000..e27695e86 --- /dev/null +++ b/src/Listeners/CharacterFilterDataUpdatedTokens.php @@ -0,0 +1,14 @@ +character->refresh_token); + } +} \ No newline at end of file diff --git a/src/Models/CharacterSchedulingRule.php b/src/Models/CharacterSchedulingRule.php index 783a2b51e..7adafcc62 100644 --- a/src/Models/CharacterSchedulingRule.php +++ b/src/Models/CharacterSchedulingRule.php @@ -22,10 +22,10 @@ namespace Seat\Web\Models; -use Carbon\Carbon; +use Seat\Eveapi\Models\Character\CharacterInfo; use Seat\Eveapi\Models\RefreshToken; -use Seat\Eveapi\Models\RefreshTokenSchedule; use Seat\Services\Models\ExtensibleModel; +use stdClass; /** * @property int $id @@ -35,8 +35,52 @@ */ class CharacterSchedulingRule extends ExtensibleModel { + use Filterable; + /** * @var bool */ public $timestamps = false; + + /** + * The filters to use. + * + * @return \stdClass + */ + public function getFilters(): stdClass + { + return json_decode($this->filter); + } + + /** + * Recomputes the update interval of a character and saves it in the refresh_token_schedules table + * + * @param RefreshToken $token + * @return void + */ + public static function updateRefreshTokenSchedule(RefreshToken $token): void + { + $schedule = $token->token_schedule; + $schedule->update_interval = self::getCharacterSchedulingInterval($token->character); + $schedule->save(); + } + + /** + * Computes the scheduling interval from the character scheduling rules for a character + * + * @param CharacterInfo $character + * @return int + */ + private static function getCharacterSchedulingInterval(CharacterInfo $character): int + { + $scheduling_rules = CharacterSchedulingRule::orderBy('interval', 'asc')->get(); + + foreach ($scheduling_rules as $rule) { + if($rule->isEligible($character)) { + return $rule->interval; + } + } + + return 60*60; // 1 hour + } } \ No newline at end of file diff --git a/src/Observers/CharacterAffiliationObserver.php b/src/Observers/CharacterAffiliationObserver.php index 86297fcce..5586b52e6 100644 --- a/src/Observers/CharacterAffiliationObserver.php +++ b/src/Observers/CharacterAffiliationObserver.php @@ -58,6 +58,7 @@ public function updated(CharacterAffiliation $affiliation) */ protected function findRelatedCharacter(Model $fired_model): ?CharacterInfo { - return $fired_model->character; + // CharacterAffiliation links to UniverseName instead of CharacterInfo + return CharacterInfo::find($fired_model->character_id); } } diff --git a/src/WebServiceProvider.php b/src/WebServiceProvider.php index 0bb9c5dd5..18159406f 100644 --- a/src/WebServiceProvider.php +++ b/src/WebServiceProvider.php @@ -63,6 +63,7 @@ use Seat\Web\Http\Middleware\RegistrationAllowed; use Seat\Web\Http\Middleware\Requirements; use Seat\Web\Listeners\CharacterFilterDataUpdatedSquads; +use Seat\Web\Listeners\CharacterFilterDataUpdatedTokens; use Seat\Web\Models\Squads\SquadMember; use Seat\Web\Models\Squads\SquadRole; use Seat\Web\Observers\CharacterAffiliationObserver; @@ -302,6 +303,7 @@ private function add_events() // Custom Events Event::listen('security.log', SecLog::class); Event::listen(CharacterFilterDataUpdate::class, CharacterFilterDataUpdatedSquads::class); + Event::listen(CharacterFilterDataUpdate::class, CharacterFilterDataUpdatedTokens::class); // Characters / Corporations first auth - Jobs Events CharacterRole::observe(CharacterRoleObserver::class);