Skip to content

Commit

Permalink
Merge pull request #240 from P3D-Legacy/develop
Browse files Browse the repository at this point in the history
  • Loading branch information
dsbilling authored Jan 24, 2023
2 parents 63281fa + 4525636 commit 976fea0
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 41 deletions.
85 changes: 53 additions & 32 deletions app/Console/Commands/UpdateGamejoltAccountTrophies.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class UpdateGamejoltAccountTrophies extends Command
*
* @var string
*/
protected $signature = 'gj:update-trophies';
protected $signature = 'gj:update-trophies {gamejolt_user_id}';

/**
* The console command description.
Expand All @@ -35,48 +35,69 @@ public function __construct()
parent::__construct();
}

private function handleUser($gamejolt_user_id, $api): void
{
try {
$account = GamejoltAccount::firstWhere('id', $gamejolt_user_id);
$trophies = $api->trophies()->fetch($account->username, $account->token);
if (filter_var($trophies['response']['success'], FILTER_VALIDATE_BOOLEAN) === false) {
$this->error("No success for {$account->username}");

return;
}
$trophies = $trophies['response']['trophies'];
$trophy_count = count($trophies);
$this->info("Found {$trophy_count} for {$account->username}");
foreach ($trophies as $trophy) {
GamejoltAccountTrophy::updateOrCreate(
[
'gamejolt_account_id' => $account->id,
'id' => $trophy['id'],
],
[
'title' => $trophy['title'],
'difficulty' => $trophy['difficulty'],
'description' => $trophy['description'],
'image_url' => $trophy['image_url'],
'achieved' => filter_var($trophy['achieved'], FILTER_VALIDATE_BOOLEAN),
]
);
}
$this->info("Sync done for {$account->username}");
} catch (TimeOutException $e) {
$this->error("Timeout for {$account->username}");
} catch (\Exception $e) {
$this->error("Unknown Error: {$e->getMessage()}");
}
}

/**
* Execute the console command.
*
* @return int
*/
public function handle(): int
{
$gamejolt_user_id = $this->argument('gamejolt_user_id');
if ($gamejolt_user_id != 'all') {
if (! is_numeric($gamejolt_user_id) || $gamejolt_user_id < 1) {
$this->error('GameJolt user ID must be numeric.');

return Command::FAILURE;
}
}

$api = new GamejoltApi(new GamejoltConfig(env('GAMEJOLT_GAME_ID'), env('GAMEJOLT_GAME_PRIVATE_KEY')));
$accounts = GamejoltAccount::all();
foreach ($accounts as $account) {
try {
$trophies = $api->trophies()->fetch($account->username, $account->token);
if (filter_var($trophies['response']['success'], FILTER_VALIDATE_BOOLEAN) === false) {
$this->error("No success for {$account->username}");

continue;
}
$trophies = $trophies['response']['trophies'];
$trophy_count = count($trophies);
$this->info("Found {$trophy_count} for {$account->username}");
foreach ($trophies as $trophy) {
GamejoltAccountTrophy::updateOrCreate(
[
'gamejolt_account_id' => $account->id,
'id' => $trophy['id'],
],
[
'title' => $trophy['title'],
'difficulty' => $trophy['difficulty'],
'description' => $trophy['description'],
'image_url' => $trophy['image_url'],
'achieved' => filter_var($trophy['achieved'], FILTER_VALIDATE_BOOLEAN),
]
);
}
$this->info("Sync done for {$account->username}");
} catch (TimeOutException $e) {
$this->error("Timeout for {$account->username}");
} catch (\Exception $e) {
$this->error("Unknown Error: {$e->getMessage()}");
if ($gamejolt_user_id == 'all') {
$gamejolt_accounts = GamejoltAccount::all();
foreach ($gamejolt_accounts as $gamejolt_account) {
$this->handleUser($gamejolt_account->id, $api);
}
} else {
$this->handleUser($gamejolt_user_id, $api);
}

$this->info('Done.');

return 0;
Expand Down
8 changes: 0 additions & 8 deletions app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@
use App\Console\Commands\NotifyGameUpdate;
use App\Console\Commands\PingAllServers;
use App\Console\Commands\SkinUserUpdate;
use App\Console\Commands\SyncGameSave;
use App\Console\Commands\SyncGameSaveGamejoltAccountTrophies;
use App\Console\Commands\SyncGameVersion;
use App\Console\Commands\UpdateGamejoltAccountTrophies;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use Spatie\Activitylog\CleanActivitylogCommand;
Expand All @@ -27,7 +24,6 @@ protected function schedule(Schedule $schedule)
{
// Often commands
$schedule->command(PingAllServers::class)->hourly();
$schedule->command(UpdateGamejoltAccountTrophies::class)->hourly();
$schedule->command(SkinUserUpdate::class)->hourlyAt(10);
// Daily commands
$schedule->command(DiscordRoleSync::class)->dailyAt('12:00');
Expand All @@ -36,10 +32,6 @@ protected function schedule(Schedule $schedule)
$schedule->command(SyncGameVersion::class)->dailyAt('00:00');
$schedule->command(NotifyGameUpdate::class)->dailyAt('00:30');
$schedule->command(CleanActivitylogCommand::class)->dailyAt('01:00');
// $schedule->command(SyncGameSave::class, ['gamejolt_user_id' => 'all'])->dailyAt('02:00');
// $schedule->command(SyncGameSaveGamejoltAccountTrophies::class, ['gamejolt_user_id' => 'all'])->dailyAt('02:15');
$schedule->command(SyncGameSave::class, ['gamejolt_user_id' => 'all'])->hourly();
$schedule->command(SyncGameSaveGamejoltAccountTrophies::class, ['gamejolt_user_id' => 'all'])->hourlyAt(5);
}

/**
Expand Down
51 changes: 51 additions & 0 deletions app/Listeners/Auth/UpdateUserGameJoltData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace App\Listeners\Auth;

use Artisan;
use Illuminate\Auth\Events\Login;
use Illuminate\Support\Facades\Auth;

class UpdateUserGameJoltData
{
/**
* Handle the event.
*
* @param Login $event
* @return void
*/
public function handle(Login $event)
{
$user = null;

/**
* If the event is Login, we get the user from the web guard.
*/
if ($event instanceof Login) {
$user = Auth::user();
}

/**
* If no user is found, we just return. Nothing to do here.
*/
if (is_null($user)) {
return;
}

// Check if user has gamejolt account
if (! $user->gamejolt) {
return;
}

// Run commands to update user data
Artisan::call('gj:update-trophies', [
'gamejolt_user_id' => $user->gamejolt->id,
]);
Artisan::call('sync:gamesave', [
'gamejolt_user_id' => $user->gamejolt->id,
]);
Artisan::call('sync:game-save-gamejolt-account-trophies', [
'gamejolt_user_id' => $user->gamejolt->id,
]);
}
}
5 changes: 4 additions & 1 deletion app/Providers/EventServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ class EventServiceProvider extends ServiceProvider
\SocialiteProviders\Manager\SocialiteWasCalled::class => [
\SocialiteProviders\Twitch\TwitchExtendSocialite::class.'@handle',
],
'Illuminate\Auth\Events\Login' => ['App\Listeners\Auth\UpdateUserTimezone'],
'Illuminate\Auth\Events\Login' => [
'App\Listeners\Auth\UpdateUserTimezone',
'App\Listeners\Auth\UpdateUserGameJoltData',
],
];

/**
Expand Down

0 comments on commit 976fea0

Please sign in to comment.