diff --git a/app/Console/Commands/ImportGamesCommand.php b/app/Console/Commands/ImportGamesCommand.php index d2094e3..2ec30a1 100644 --- a/app/Console/Commands/ImportGamesCommand.php +++ b/app/Console/Commands/ImportGamesCommand.php @@ -12,22 +12,36 @@ use Illuminate\Console\Command; class ImportGamesCommand extends Command { - protected $signature = 'games:import {--limit=100} {--offset=0} {--popular} {--new}'; + protected $signature = 'games:import {--limit=100} {--offset=0} {--popular} {--new} {--year=} {--platform=}'; protected $description = 'Import games from IGDB into the local database'; public function handle(IgdbService $igdb): int { - $limit = (int) $this->option('limit'); - $offset = (int) $this->option('offset'); + $limit = (int) $this->option('limit'); + $offset = (int) $this->option('offset'); + $year = $this->option('year') ? (int) $this->option('year') : null; + $platform = $this->option('platform') ? (int) $this->option('platform') : null; - $this->info("Fetching {$limit} games from IGDB (offset: {$offset})…"); + if ($year && $platform) { + $this->error('Use --year or --platform, not both.'); + return self::FAILURE; + } + + $label = match (true) { + $year !== null => "year {$year}", + $platform !== null => "platform IGDB ID {$platform}", + $this->option('new') => 'new releases', + default => "popular (offset: {$offset})", + }; + $this->info("Fetching {$limit} games from IGDB ({$label})…"); try { - if ($this->option('new')) { - $results = $igdb->getNewReleases($limit); - } else { - $results = $igdb->getPopularGames($limit, $offset); - } + $results = match (true) { + $year !== null => $igdb->getGamesByYear($year, $limit, $offset), + $platform !== null => $igdb->getGamesByPlatform($platform, $limit, $offset), + $this->option('new') => $igdb->getNewReleases($limit), + default => $igdb->getPopularGames($limit, $offset), + }; } catch (\Exception $e) { $this->error('IGDB request failed: ' . $e->getMessage()); return self::FAILURE; diff --git a/app/Http/Controllers/GameController.php b/app/Http/Controllers/GameController.php index 051f526..6e60866 100644 --- a/app/Http/Controllers/GameController.php +++ b/app/Http/Controllers/GameController.php @@ -52,7 +52,7 @@ class GameController extends Controller if (auth()->check()) { $userGame = $game->userGames()->where('user_id', auth()->id())->first(); $userReview = $game->reviews()->where('user_id', auth()->id())->first(); - $userLists = auth()->user()->lists()->get(['id', 'title']); + $userLists = auth()->user()->lists()->get(['id', 'slug', 'title']); } $comments = $game->comments()->whereNull('parent_id') diff --git a/app/Services/IgdbService.php b/app/Services/IgdbService.php index 6b5758e..5b534d1 100644 --- a/app/Services/IgdbService.php +++ b/app/Services/IgdbService.php @@ -139,6 +139,51 @@ class IgdbService "); } + /** + * Get games released in a given year. + */ + public function getGamesByYear(int $year, int $limit = 20, int $offset = 0): array + { + $from = mktime(0, 0, 0, 1, 1, $year); + $to = mktime(23, 59, 59, 12, 31, $year); + + return $this->query('games', " + fields id,name,slug,summary,cover.image_id,first_release_date, + genres.id,genres.name,genres.slug, + platforms.id,platforms.name,platforms.slug,platforms.abbreviation, + involved_companies.company.id,involved_companies.company.name, + involved_companies.company.slug,involved_companies.developer, + involved_companies.publisher, + screenshots.image_id, + rating,rating_count; + where first_release_date >= {$from} & first_release_date <= {$to} & cover != null; + sort rating_count desc; + limit {$limit}; + offset {$offset}; + "); + } + + /** + * Get games for a given platform IGDB ID. + */ + public function getGamesByPlatform(int $platformIgdbId, int $limit = 20, int $offset = 0): array + { + return $this->query('games', " + fields id,name,slug,summary,cover.image_id,first_release_date, + genres.id,genres.name,genres.slug, + platforms.id,platforms.name,platforms.slug,platforms.abbreviation, + involved_companies.company.id,involved_companies.company.name, + involved_companies.company.slug,involved_companies.developer, + involved_companies.publisher, + screenshots.image_id, + rating,rating_count; + where platforms = [{$platformIgdbId}] & cover != null & rating != null; + sort rating_count desc; + limit {$limit}; + offset {$offset}; + "); + } + /** * Get games by genre. */ diff --git a/resources/views/games/show.blade.php b/resources/views/games/show.blade.php index 1a39daf..05fd6b8 100644 --- a/resources/views/games/show.blade.php +++ b/resources/views/games/show.blade.php @@ -196,27 +196,28 @@ @else @@ -799,11 +800,11 @@ function addToListWidget(gameId, lists) { open: false, added: {}, - async toggleList(listId) { - const isAdded = this.added[listId]; + async toggleList(listSlug) { + const isAdded = this.added[listSlug]; const url = isAdded - ? `/lists/${listId}/games/${this.gameId}` - : `/lists/${listId}/games`; + ? `/lists/${listSlug}/games/${this.gameId}` + : `/lists/${listSlug}/games`; const method = isAdded ? 'DELETE' : 'POST'; const res = await fetch(url, { @@ -817,7 +818,7 @@ function addToListWidget(gameId, lists) { }); if (res.ok) { - this.added[listId] = !isAdded; + this.added[listSlug] = !isAdded; } }, }; diff --git a/resources/views/lists/show.blade.php b/resources/views/lists/show.blade.php index 72a733b..4c53ea4 100644 --- a/resources/views/lists/show.blade.php +++ b/resources/views/lists/show.blade.php @@ -56,7 +56,7 @@ @auth @if(auth()->id() === $list->user_id) - @@ -71,9 +71,9 @@ @push('scripts')