This commit is contained in:
2025-04-10 00:44:55 +03:00
parent 909d514552
commit 8d230e8e67
4 changed files with 497 additions and 48 deletions
+130 -48
View File
@@ -223,20 +223,23 @@ class IgdbService
// Process platforms
$platforms = [];
if (isset($gameData->platforms) && is_array($gameData->platforms)) {
foreach ($gameData->platforms as $platformId) {
if ($platformDetails = $this->getPlatformDetails($platformId)) {
$platform = Platform::updateOrCreate(
['igdb_id' => $platformDetails['id']],
[
'name' => $platformDetails['name'],
'slug' => $platformDetails['slug'],
'abbreviation' => $platformDetails['abbreviation'],
]
);
$platforms[] = $platform->id;
}
$platformData = IgdbPlatform::select(['id', 'name', 'slug', 'abbreviation'])
->whereIn('id', $gameData->platforms)
->get();
foreach ($platformData as $platformDetails) {
$platform = Platform::updateOrCreate(
['igdb_id' => $platformDetails->id],
[
'name' => $platformDetails->name,
'slug' => $platformDetails->slug,
'abbreviation' => $platformDetails->abbreviation,
]
);
$platforms[] = $platform->id;
}
}
// Create or update the game
$game = Game::updateOrCreate(
['igdb_id' => $gameData->id],
@@ -252,55 +255,101 @@ class IgdbService
);
// Process artworks
if (isset($gameData->artworks) && is_array($gameData->artworks)) {
foreach ($gameData->artworks as $artwork) {
try {
$artworks = \MarcReichel\IGDBLaravel\Models\Artwork::select([
'id',
'url',
'width',
'height',
'image_id'
])
->where('game', '=', $gameData->id)
->get();
foreach ($artworks as $artworkData) {
Artwork::updateOrCreate(
['igdb_id' => $artwork->id],
['igdb_id' => $artworkData->id],
[
'game_id' => $game->id,
'url' => $artwork->url,
'width' => $artwork->width ?? null,
'height' => $artwork->height ?? null,
'image_id' => $artwork->image_id ?? null,
'url' => $artworkData->url,
'width' => $artworkData->width ?? null,
'height' => $artworkData->height ?? null,
'image_id' => $artworkData->image_id ?? null,
]
);
}
} catch (\Exception $e) {
Log::error('Failed to get artworks for game', [
'game_id' => $game->id,
'error' => $e->getMessage()
]);
}
// Process platforms relationship
if (!empty($platforms)) {
$game->platforms()->sync($platforms);
}
// Process screenshots
if (isset($gameData->screenshots) && is_array($gameData->screenshots)) {
foreach ($gameData->screenshots as $screenshot) {
try {
$screenshots = \MarcReichel\IGDBLaravel\Models\Screenshot::select([
'id',
'url',
'width',
'height',
'image_id'
])
->where('game', '=', $gameData->id)
->get();
foreach ($screenshots as $screenshotData) {
Screenshot::updateOrCreate(
['igdb_id' => $screenshot->id],
['igdb_id' => $screenshotData->id],
[
'game_id' => $game->id,
'url' => $screenshot->url,
'width' => $screenshot->width ?? null,
'height' => $screenshot->height ?? null,
'image_id' => $screenshot->image_id ?? null,
'url' => $screenshotData->url,
'width' => $screenshotData->width ?? null,
'height' => $screenshotData->height ?? null,
'image_id' => $screenshotData->image_id ?? null,
]
);
}
} catch (\Exception $e) {
Log::error('Failed to get screenshots for game', [
'game_id' => $game->id,
'error' => $e->getMessage()
]);
}
// Process videos
if (isset($gameData->videos) && is_array($gameData->videos)) {
foreach ($gameData->videos as $video) {
try {
$videos = \MarcReichel\IGDBLaravel\Models\GameVideo::select([
'id',
'name',
'video_id',
'url',
'checksum'
])
->where('game', '=', $gameData->id)
->get();
foreach ($videos as $videoData) {
Video::updateOrCreate(
['igdb_id' => $video->id],
['igdb_id' => $videoData->id],
[
'game_id' => $game->id,
'name' => $video->name,
'video_id' => $video->video_id,
'url' => $video->url,
'checksum' => $video->checksum ?? null,
'name' => $videoData->name,
'video_id' => $videoData->video_id,
'url' => $videoData->url,
'checksum' => $videoData->checksum ?? null,
]
);
}
} catch (\Exception $e) {
Log::error('Failed to get videos for game', [
'game_id' => $game->id,
'error' => $e->getMessage()
]);
}
// Process relationships
@@ -316,24 +365,18 @@ class IgdbService
throw new \InvalidArgumentException('Game data must be an instance of IgdbGame');
}
// Fetch related games data using IGDB API
$relationships = [
'bundles' => 'bundles',
'collections' => 'collections',
'dlcs' => 'dlcs',
'expanded_games' => 'expansions',
'franchises' => 'franchises',
'ports' => 'ports',
'remakes' => 'remakes',
'similar_games' => 'similarGames',
'bundles' => IgdbGame::select(['id'])->where('id', 'in', $gameData->bundles ?? [])->get()->pluck('id')->toArray(),
'collections' => IgdbGame::select(['id'])->where('id', 'in', $gameData->collections ?? [])->get()->pluck('id')->toArray(),
'dlcs' => IgdbGame::select(['id'])->where('id', 'in', $gameData->dlcs ?? [])->get()->pluck('id')->toArray(),
'expanded_games' => IgdbGame::select(['id'])->where('id', 'in', $gameData->expanded_games ?? [])->get()->pluck('id')->toArray(),
'franchises' => IgdbGame::select(['id'])->where('id', 'in', $gameData->franchises ?? [])->get()->pluck('id')->toArray(),
'ports' => IgdbGame::select(['id'])->where('id', 'in', $gameData->ports ?? [])->get()->pluck('id')->toArray(),
'remakes' => IgdbGame::select(['id'])->where('id', 'in', $gameData->remakes ?? [])->get()->pluck('id')->toArray(),
'similar_games' => IgdbGame::select(['id'])->where('id', 'in', $gameData->similar_games ?? [])->get()->pluck('id')->toArray(),
];
$collections = Collection::where('games', $gameData->identifier)->first();
dd(
$gameData,
$collections
);
foreach ($relationships as $igdbKey => $relationship) {
$relatedGames = [];
if (isset($gameData->$igdbKey) && !empty($gameData->$igdbKey)) {
@@ -343,4 +386,43 @@ class IgdbService
$game->$relationship()->sync($relatedGames);
}
}
public function processGenreData(array $genreData): void
{
try {
Genre::updateOrCreate(
['igdb_id' => $genreData['id']],
[
'name' => $genreData['name'],
'slug' => $genreData['slug'],
]
);
} catch (\Exception $e) {
Log::error('Failed to process genre data', [
'genre_id' => $genreData['id'],
'error' => $e->getMessage()
]);
throw $e;
}
}
public function processPlatformData(array $platformData): void
{
try {
Platform::updateOrCreate(
['igdb_id' => $platformData['id']],
[
'name' => $platformData['name'],
'slug' => $platformData['slug'],
'abbreviation' => $platformData['abbreviation'] ?? null,
]
);
} catch (\Exception $e) {
Log::error('Failed to process platform data', [
'platform_id' => $platformData['id'],
'error' => $e->getMessage()
]);
throw $e;
}
}
}