Skip to content

Commit

Permalink
schedule: apply character scheduling rules to tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
recursivetree committed Aug 20, 2024
1 parent 0895a49 commit cb42103
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 3 deletions.
9 changes: 9 additions & 0 deletions src/Http/Controllers/Configuration/ScheduleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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!');
}
Expand All @@ -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!');
}
}
14 changes: 14 additions & 0 deletions src/Listeners/CharacterFilterDataUpdatedTokens.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Seat\Web\Listeners;

use Seat\Web\Events\CharacterFilterDataUpdate;
use Seat\Web\Models\CharacterSchedulingRule;

class CharacterFilterDataUpdatedTokens
{
public static function handle(CharacterFilterDataUpdate $update)
{
CharacterSchedulingRule::updateRefreshTokenSchedule($update->character->refresh_token);
}
}
48 changes: 46 additions & 2 deletions src/Models/CharacterSchedulingRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
}
3 changes: 2 additions & 1 deletion src/Observers/CharacterAffiliationObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
2 changes: 2 additions & 0 deletions src/WebServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit cb42103

Please sign in to comment.