Expand IGDB data: collections, game modes, themes, perspectives, videos, websites, age ratings, similar games; add backfill command

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Mark
2026-06-01 00:24:05 +03:00
co-authored by Claude Sonnet 4.6
parent 0eab3e8dbf
commit 2a724c0ae2
21 changed files with 768 additions and 57 deletions
@@ -0,0 +1,95 @@
<?php
namespace App\Console\Commands;
use App\Models\Game;
use App\Services\IgdbService;
use Illuminate\Console\Command;
class BackfillExtendedDataCommand extends Command
{
protected $signature = 'igdb:backfill-extended
{--limit=100 : Max number of games to process}
{--offset=0 : Skip the first N games (ordered by id)}';
protected $description = 'Backfill extended IGDB data (collections, modes, themes, videos, etc.) for existing games';
public function handle(IgdbService $igdb): int
{
$limit = (int) $this->option('limit');
$offset = (int) $this->option('offset');
$games = Game::whereNotNull('igdb_id')
->orderBy('id')
->skip($offset)
->take($limit)
->get();
$total = $games->count();
if ($total === 0) {
$this->info('No games found to process.');
return self::SUCCESS;
}
$this->info("Processing {$total} games (offset: {$offset})…");
$this->newLine();
$n = 0;
foreach ($games->chunk(10) as $batch) {
$igdbIds = $batch->pluck('igdb_id')->all();
try {
$igdbResults = $igdb->getGamesByIds($igdbIds);
} catch (\Exception $e) {
$this->error('IGDB request failed: ' . $e->getMessage());
return self::FAILURE;
}
$indexed = collect($igdbResults)->keyBy('id');
foreach ($batch as $game) {
$n++;
$igdbData = $indexed->get($game->igdb_id);
if (!$igdbData) {
$this->warn(" [{$n}/{$total}] No IGDB data for: {$game->title} (igdb_id={$game->igdb_id})");
continue;
}
try {
$igdb->syncExtendedData($game, $igdbData);
$this->line(" [{$n}/{$total}] Processed: {$game->title}");
} catch (\Exception $e) {
$this->warn(" [{$n}/{$total}] Failed {$game->title}: " . $e->getMessage());
}
}
usleep(250000); // 250ms between IGDB batches
}
$this->newLine();
$this->info('Extended data sync complete. Resolving similar_games pivot…');
$this->resolveSimilarGames();
$this->info('Done.');
return self::SUCCESS;
}
private function resolveSimilarGames(): void
{
$resolved = 0;
Game::whereNotNull('similar_game_igdb_ids')->chunk(100, function ($games) use (&$resolved) {
foreach ($games as $game) {
$ids = $game->similar_game_igdb_ids;
if (empty($ids)) continue;
$similarIds = Game::whereIn('igdb_id', $ids)->pluck('id');
$game->similarGames()->sync($similarIds);
$resolved++;
}
});
$this->line(" Resolved similar_games for {$resolved} games.");
}
}
@@ -89,5 +89,7 @@ trait UpsertsGames
]);
}
}
$igdb->syncExtendedData($game, $data);
}
}
+11 -3
View File
@@ -47,7 +47,11 @@ class GameController extends Controller
public function show(Game $game)
{
$game->load(['genres', 'platforms', 'companies', 'screenshots', 'reviews.user']);
$game->load([
'genres', 'platforms', 'companies', 'screenshots', 'reviews.user',
'collection', 'gameModes', 'themes', 'playerPerspectives', 'videos', 'websites',
'similarGames' => fn($q) => $q->published()->with(['genres'])->limit(6),
]);
$userGame = null;
$userReview = null;
@@ -64,8 +68,10 @@ class GameController extends Controller
->limit(20)
->get();
$similarGames = collect();
if ($game->genres->isNotEmpty()) {
// Use IGDB similar_games if available, otherwise fall back to genre-based
if ($game->similarGames->isNotEmpty()) {
$similarGames = $game->similarGames;
} elseif ($game->genres->isNotEmpty()) {
$similarGames = Game::published()
->whereHas('genres', fn($q) => $q->whereIn('genres.id', $game->genres->pluck('id')))
->where('id', '!=', $game->id)
@@ -73,6 +79,8 @@ class GameController extends Controller
->orderByDesc('rating_avg')
->limit(6)
->get();
} else {
$similarGames = collect();
}
$response = view('games.show', compact('game', 'userGame', 'userReview', 'userLists', 'comments', 'similarGames'));
+16
View File
@@ -0,0 +1,16 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
class Collection extends Model
{
protected $fillable = ['igdb_id', 'name', 'slug'];
public function games(): HasMany
{
return $this->hasMany(Game::class);
}
}
+47 -4
View File
@@ -3,6 +3,7 @@
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Facades\Cache;
@@ -15,14 +16,16 @@ class Game extends Model
use HasSlug, Searchable;
protected $fillable = [
'igdb_id', 'slug', 'title', 'summary', 'cover_url', 'cover_thumb_url',
'igdb_id', 'slug', 'title', 'summary', 'storyline', 'cover_url', 'cover_thumb_url',
'release_date', 'release_year', 'rating_avg', 'rating_count', 'review_count',
'library_count', 'status',
'library_count', 'status', 'collection_id', 'age_rating_pegi', 'age_rating_esrb',
'aggregated_rating', 'aggregated_rating_count', 'similar_game_igdb_ids',
];
protected $casts = [
'release_date' => 'date',
'rating_avg' => 'decimal:2',
'release_date' => 'date',
'rating_avg' => 'decimal:2',
'similar_game_igdb_ids' => 'array',
];
public function getSlugOptions(): SlugOptions
@@ -32,6 +35,11 @@ class Game extends Model
public function getRouteKeyName(): string { return 'slug'; }
public function collection(): BelongsTo
{
return $this->belongsTo(Collection::class);
}
public function genres(): BelongsToMany
{
return $this->belongsToMany(Genre::class, 'game_genre');
@@ -67,6 +75,41 @@ class Game extends Model
return $this->hasMany(GameComment::class);
}
public function gameModes(): BelongsToMany
{
return $this->belongsToMany(GameMode::class, 'game_game_mode');
}
public function themes(): BelongsToMany
{
return $this->belongsToMany(Theme::class, 'game_theme');
}
public function playerPerspectives(): BelongsToMany
{
return $this->belongsToMany(PlayerPerspective::class, 'game_player_perspective');
}
public function videos(): HasMany
{
return $this->hasMany(GameVideo::class);
}
public function websites(): HasMany
{
return $this->hasMany(GameWebsite::class);
}
public function similarGames(): BelongsToMany
{
return $this->belongsToMany(
Game::class,
'similar_games',
'game_id',
'similar_game_id'
);
}
public function toSearchableArray(): array
{
return [
+16
View File
@@ -0,0 +1,16 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
class GameMode extends Model
{
protected $fillable = ['igdb_id', 'name', 'slug'];
public function games(): BelongsToMany
{
return $this->belongsToMany(Game::class, 'game_game_mode');
}
}
+26
View File
@@ -0,0 +1,26 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class GameVideo extends Model
{
protected $fillable = ['game_id', 'igdb_id', 'name', 'video_id'];
public function game(): BelongsTo
{
return $this->belongsTo(Game::class);
}
public function getYoutubeUrlAttribute(): string
{
return "https://www.youtube.com/watch?v={$this->video_id}";
}
public function getYoutubeEmbedUrlAttribute(): string
{
return "https://www.youtube.com/embed/{$this->video_id}";
}
}
+31
View File
@@ -0,0 +1,31 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class GameWebsite extends Model
{
protected $fillable = ['game_id', 'category', 'url'];
public const CATEGORY_LABELS = [
1 => 'Official',
2 => 'Wikia',
3 => 'Wikipedia',
9 => 'YouTube',
13 => 'Steam',
16 => 'Epic Games',
17 => 'GOG',
];
public function game(): BelongsTo
{
return $this->belongsTo(Game::class);
}
public function getLabelAttribute(): string
{
return self::CATEGORY_LABELS[$this->category] ?? 'Website';
}
}
+16
View File
@@ -0,0 +1,16 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
class PlayerPerspective extends Model
{
protected $fillable = ['igdb_id', 'name', 'slug'];
public function games(): BelongsToMany
{
return $this->belongsToMany(Game::class, 'game_player_perspective');
}
}
+16
View File
@@ -0,0 +1,16 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
class Theme extends Model
{
protected $fillable = ['igdb_id', 'name', 'slug'];
public function games(): BelongsToMany
{
return $this->belongsToMany(Game::class, 'game_theme');
}
}
+189 -48
View File
@@ -2,6 +2,14 @@
namespace App\Services;
use App\Models\Collection;
use App\Models\Company;
use App\Models\Game;
use App\Models\GameMode;
use App\Models\GameVideo;
use App\Models\GameWebsite;
use App\Models\PlayerPerspective;
use App\Models\Theme;
use GuzzleHttp\Client;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
@@ -13,6 +21,10 @@ class IgdbService
protected string $clientSecret;
protected string $baseUrl;
// IGDB age_rating category: 1=ESRB, 2=PEGI
private const ESRB_LABELS = [6 => 'RP', 7 => 'EC', 8 => 'E', 9 => 'E10+', 10 => 'T', 11 => 'M', 12 => 'AO'];
private const PEGI_LABELS = [1 => '3', 2 => '7', 3 => '12', 4 => '16', 5 => '18'];
public function __construct()
{
$this->client = new Client(['timeout' => 10]);
@@ -60,6 +72,27 @@ class IgdbService
}
}
private function extendedFields(): string
{
return 'id,name,slug,summary,storyline,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,involved_companies.porting,
screenshots.image_id,
rating,rating_count,
aggregated_rating,aggregated_rating_count,
collection.id,collection.name,collection.slug,
game_modes.id,game_modes.name,game_modes.slug,
themes.id,themes.name,themes.slug,
player_perspectives.id,player_perspectives.name,player_perspectives.slug,
videos.id,videos.name,videos.video_id,
websites.category,websites.url,
age_ratings.category,age_ratings.rating,
similar_games';
}
/**
* Search games by title.
*/
@@ -81,34 +114,33 @@ class IgdbService
public function getGame(int $igdbId): ?array
{
$results = $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;
fields {$this->extendedFields()};
where id = {$igdbId};
limit 1;
");
return $results[0] ?? null;
}
/**
* Fetch fresh extended data for a set of IGDB IDs (for backfill).
*/
public function getGamesByIds(array $igdbIds): array
{
$ids = implode(',', $igdbIds);
return $this->query('games', "
fields {$this->extendedFields()};
where id = ({$ids});
limit " . count($igdbIds) . ";
");
}
/**
* Get popular games (by rating and rating count).
*/
public function getPopularGames(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;
fields {$this->extendedFields()};
where rating != null & rating_count > 100 & cover != null;
sort rating_count desc;
limit {$limit};
@@ -122,14 +154,7 @@ class IgdbService
public function getPopularGamesForBackfill(int $limit = 500, int $offset = 0, int $minRatings = 5): 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;
fields {$this->extendedFields()};
where rating != null & rating_count > {$minRatings} & cover != null;
sort rating_count desc;
limit {$limit};
@@ -146,14 +171,7 @@ class IgdbService
$now = now()->timestamp;
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;
fields {$this->extendedFields()};
where first_release_date >= {$timestamp} & first_release_date <= {$now} & cover != null;
sort first_release_date desc;
limit {$limit};
@@ -169,14 +187,7 @@ class IgdbService
$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;
fields {$this->extendedFields()};
where first_release_date >= {$from} & first_release_date <= {$to} & cover != null;
sort rating_count desc;
limit {$limit};
@@ -190,14 +201,7 @@ class IgdbService
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;
fields {$this->extendedFields()};
where platforms = [{$platformIgdbId}] & cover != null & rating != null;
sort rating_count desc;
limit {$limit};
@@ -221,6 +225,143 @@ class IgdbService
");
}
/**
* Sync all extended IGDB data onto a Game model.
*/
public function syncExtendedData(Game $game, array $data): void
{
$updates = [];
// Collection
if (!empty($data['collection'])) {
$col = $data['collection'];
$collection = Collection::firstOrCreate(
['igdb_id' => $col['id']],
['name' => $col['name'], 'slug' => $col['slug'] ?? \Str::slug($col['name'])]
);
$updates['collection_id'] = $collection->id;
}
// Storyline
if (!empty($data['storyline'])) {
$updates['storyline'] = $data['storyline'];
}
// Aggregated rating (critics)
if (isset($data['aggregated_rating'])) {
$updates['aggregated_rating'] = round($data['aggregated_rating'], 2);
}
if (isset($data['aggregated_rating_count'])) {
$updates['aggregated_rating_count'] = $data['aggregated_rating_count'];
}
// Age ratings
if (!empty($data['age_ratings'])) {
foreach ($data['age_ratings'] as $ar) {
if (($ar['category'] ?? null) === 1 && isset($ar['rating'])) {
$updates['age_rating_esrb'] = self::ESRB_LABELS[$ar['rating']] ?? null;
}
if (($ar['category'] ?? null) === 2 && isset($ar['rating'])) {
$updates['age_rating_pegi'] = self::PEGI_LABELS[$ar['rating']] ?? null;
}
}
}
// Similar games — store IGDB IDs for later resolution
if (!empty($data['similar_games'])) {
$updates['similar_game_igdb_ids'] = array_values(
array_filter(array_map(fn($s) => is_array($s) ? ($s['id'] ?? null) : $s, $data['similar_games']))
);
}
if (!empty($updates)) {
$game->update($updates);
}
// Game modes
if (!empty($data['game_modes'])) {
$ids = [];
foreach ($data['game_modes'] as $gm) {
$model = GameMode::firstOrCreate(
['igdb_id' => $gm['id']],
['name' => $gm['name'], 'slug' => $gm['slug'] ?? \Str::slug($gm['name'])]
);
$ids[] = $model->id;
}
$game->gameModes()->sync($ids);
}
// Themes
if (!empty($data['themes'])) {
$ids = [];
foreach ($data['themes'] as $t) {
$model = Theme::firstOrCreate(
['igdb_id' => $t['id']],
['name' => $t['name'], 'slug' => $t['slug'] ?? \Str::slug($t['name'])]
);
$ids[] = $model->id;
}
$game->themes()->sync($ids);
}
// Player perspectives
if (!empty($data['player_perspectives'])) {
$ids = [];
foreach ($data['player_perspectives'] as $pp) {
$model = PlayerPerspective::firstOrCreate(
['igdb_id' => $pp['id']],
['name' => $pp['name'], 'slug' => $pp['slug'] ?? \Str::slug($pp['name'])]
);
$ids[] = $model->id;
}
$game->playerPerspectives()->sync($ids);
}
// Videos
if (isset($data['videos'])) {
GameVideo::where('game_id', $game->id)->delete();
foreach ($data['videos'] as $v) {
if (empty($v['video_id'])) continue;
GameVideo::create([
'game_id' => $game->id,
'igdb_id' => $v['id'] ?? null,
'name' => $v['name'] ?? null,
'video_id' => $v['video_id'],
]);
}
}
// Websites
if (isset($data['websites'])) {
GameWebsite::where('game_id', $game->id)->delete();
foreach ($data['websites'] as $w) {
if (empty($w['url'])) continue;
GameWebsite::create([
'game_id' => $game->id,
'category' => $w['category'],
'url' => $w['url'],
]);
}
}
// Involved companies (update existing game_company pivot with richer data)
if (!empty($data['involved_companies'])) {
$companyPivot = [];
foreach ($data['involved_companies'] as $ic) {
if (empty($ic['company']['name'])) continue;
$company = Company::firstOrCreate(
['igdb_id' => $ic['company']['id']],
['name' => $ic['company']['name'], 'slug' => \Str::slug($ic['company']['name'])]
);
$role = $ic['developer'] ? 'developer' : ($ic['publisher'] ? 'publisher' : 'other');
$companyPivot[$company->id] = ['role' => $role];
}
if (!empty($companyPivot)) {
$game->companies()->sync($companyPivot);
}
}
}
/**
* Build a full cover image URL from an IGDB image_id.
*/