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
+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.
*/