first commit
This commit is contained in:
@@ -0,0 +1,107 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Services\IgdbService;
|
||||
use Illuminate\Console\Command;
|
||||
use MarcReichel\IGDBLaravel\Models\Game;
|
||||
|
||||
class BuildGameLibrary extends Command
|
||||
{
|
||||
protected $signature = 'igdb:build-library
|
||||
{--limit=100 : Number of games to fetch per batch}
|
||||
{--batches=10 : Number of batches to process}
|
||||
{--start-year=1980 : Start year for game releases}
|
||||
{--end-year=2024 : End year for game releases}';
|
||||
|
||||
protected $description = 'Build a comprehensive game library by fetching games in batches';
|
||||
|
||||
public function handle(IgdbService $igdbService): int
|
||||
{
|
||||
$limit = (int) $this->option('limit');
|
||||
$batches = (int) $this->option('batches');
|
||||
$startYear = (int) $this->option('start-year');
|
||||
$endYear = (int) $this->option('end-year');
|
||||
|
||||
$this->info("Starting to build game library...");
|
||||
$this->info("Will fetch {$limit} games per batch for {$batches} batches");
|
||||
$this->info("Covering games released between {$startYear} and {$endYear}");
|
||||
|
||||
$totalProcessed = 0;
|
||||
$totalAdded = 0;
|
||||
$totalSkipped = 0;
|
||||
|
||||
for ($batch = 1; $batch <= $batches; $batch++) {
|
||||
$this->newLine();
|
||||
$this->info("Processing batch {$batch} of {$batches}");
|
||||
|
||||
// Generate a random year within our range
|
||||
$year = rand($startYear, $endYear);
|
||||
$startTimestamp = strtotime("{$year}-01-01");
|
||||
$endTimestamp = strtotime("{$year}-12-31");
|
||||
|
||||
// Generate a random query to get diverse results
|
||||
$query = $this->generateRandomQuery();
|
||||
|
||||
$this->info("Searching for games from {$year} with query: {$query}");
|
||||
|
||||
$games = $igdbService->get();
|
||||
|
||||
if (empty($games)) {
|
||||
$this->warn("No games found for this batch. Moving to next batch...");
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->info(sprintf('Found %d games. Processing...', count($games)));
|
||||
|
||||
$bar = $this->output->createProgressBar(count($games));
|
||||
$bar->start();
|
||||
|
||||
/** @var Game $gameData */
|
||||
foreach ($games as $gameData) {
|
||||
$totalProcessed++;
|
||||
|
||||
try {
|
||||
$igdbService->processGameData($gameData);
|
||||
$totalAdded++;
|
||||
} catch (\Exception $e) {
|
||||
$this->warn("\nFailed to process game: {$e->getMessage()}");
|
||||
$totalSkipped++;
|
||||
}
|
||||
|
||||
$bar->advance();
|
||||
}
|
||||
|
||||
$bar->finish();
|
||||
|
||||
// Add a small delay between batches to avoid rate limiting
|
||||
if ($batch < $batches) {
|
||||
$this->info("\nWaiting 2 seconds before next batch...");
|
||||
sleep(2);
|
||||
}
|
||||
}
|
||||
|
||||
$this->newLine(2);
|
||||
$this->info("Library building complete!");
|
||||
$this->info("Total games processed: {$totalProcessed}");
|
||||
$this->info("New games added: {$totalAdded}");
|
||||
$this->info("Games skipped (invalid data): {$totalSkipped}");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
protected function generateRandomQuery(): string
|
||||
{
|
||||
$prefixes = ['', 'the ', 'super ', 'mega ', 'ultimate '];
|
||||
$suffixes = ['', ' game', ' edition', ' deluxe', ' special'];
|
||||
|
||||
$words = ['action', 'adventure', 'rpg', 'strategy', 'shooter', 'platformer',
|
||||
'racing', 'sports', 'fighting', 'puzzle', 'simulation', 'horror'];
|
||||
|
||||
$prefix = $prefixes[array_rand($prefixes)];
|
||||
$word = $words[array_rand($words)];
|
||||
$suffix = $suffixes[array_rand($suffixes)];
|
||||
|
||||
return $prefix . $word . $suffix;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Models\Game;
|
||||
use App\Services\IgdbService;
|
||||
use Illuminate\Console\Command;
|
||||
|
||||
class FetchIgdbGames extends Command
|
||||
{
|
||||
protected $signature = 'igdb:fetch {query?} {--limit=10}';
|
||||
protected $description = 'Fetch games from IGDB and store them in the database';
|
||||
|
||||
public function handle(IgdbService $igdbService): int
|
||||
{
|
||||
$query = $this->argument('query') ?? $this->ask('Enter search query');
|
||||
$limit = (int) $this->option('limit');
|
||||
|
||||
$this->info("Fetching games for query: {$query}");
|
||||
|
||||
$games = $igdbService->searchGames($query, $limit);
|
||||
|
||||
if (empty($games)) {
|
||||
$this->error('No games found or an error occurred.');
|
||||
return 1;
|
||||
}
|
||||
|
||||
$this->info(sprintf('Found %d games. Processing...', count($games)));
|
||||
|
||||
$bar = $this->output->createProgressBar(count($games));
|
||||
$bar->start();
|
||||
|
||||
foreach ($games as $gameData) {
|
||||
$this->processGame($gameData);
|
||||
$bar->advance();
|
||||
}
|
||||
|
||||
$bar->finish();
|
||||
$this->newLine();
|
||||
$this->info('Done!');
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
protected function processGame(array $gameData): void
|
||||
{
|
||||
$game = Game::updateOrCreate(
|
||||
['igdb_id' => $gameData['id']],
|
||||
[
|
||||
'name' => $gameData['name'],
|
||||
'summary' => $gameData['summary'] ?? null,
|
||||
'cover_url' => $gameData['cover']['url'] ?? null,
|
||||
'slug' => $gameData['slug'] ?? null,
|
||||
'genres' => array_column($gameData['genres'] ?? [], 'name'),
|
||||
'platforms' => array_column($gameData['platforms'] ?? [], 'name'),
|
||||
'first_release_date' => isset($gameData['first_release_date'])
|
||||
? date('Y-m-d', $gameData['first_release_date'])
|
||||
: null,
|
||||
'rating' => $gameData['rating'] ?? null,
|
||||
'rating_count' => $gameData['rating_count'] ?? null,
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user