Files
cleargame/app/Console/Commands/BuildGameLibrary.php
T
2026-02-22 20:02:34 +02:00

104 lines
3.5 KiB
PHP

<?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=2030 : 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");
$games = $igdbService->get($limit);
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()}");
dd($e);
$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;
}
}