mirror of
https://github.com/ceratic/MediaCollectorLibary.git
synced 2026-05-13 23:56:46 +02:00
yay
This commit is contained in:
@@ -304,8 +304,13 @@ class PlayniteImportService
|
||||
|
||||
/**
|
||||
* Import games to database
|
||||
*
|
||||
* @param array $games Array of games to import
|
||||
* @param bool $updateExisting Whether to update existing games
|
||||
* @param callable|null $logCallback Optional callback for logging progress
|
||||
* @return array Results of the import
|
||||
*/
|
||||
public function importGames(array $games, bool $updateExisting = true): array
|
||||
public function importGames(array $games, bool $updateExisting = true, ?callable $logCallback = null): array
|
||||
{
|
||||
$results = [
|
||||
'imported' => 0,
|
||||
@@ -314,24 +319,56 @@ class PlayniteImportService
|
||||
'errors' => []
|
||||
];
|
||||
|
||||
foreach ($games as $gameData) {
|
||||
$totalGames = count($games);
|
||||
$log = function(string $message) use ($logCallback) {
|
||||
if (is_callable($logCallback)) {
|
||||
$logCallback($message);
|
||||
}
|
||||
};
|
||||
|
||||
$log(sprintf("Starting import of %d games", $totalGames));
|
||||
|
||||
foreach ($games as $index => $gameData) {
|
||||
$gameTitle = $gameData['title'] ?? 'Untitled';
|
||||
$log(sprintf("Processing game %d/%d: %s", $index + 1, $totalGames, $gameTitle));
|
||||
|
||||
try {
|
||||
$existingGame = $this->findExistingGame($gameData);
|
||||
|
||||
if ($existingGame && $updateExisting) {
|
||||
$log(sprintf("Updating existing game: %s (ID: %d)", $gameTitle, $existingGame['id']));
|
||||
$this->updateGame($existingGame['id'], $gameData);
|
||||
$results['updated']++;
|
||||
$log(sprintf("Successfully updated game: %s", $gameTitle));
|
||||
} elseif (!$existingGame) {
|
||||
$log(sprintf("Importing new game: %s", $gameTitle));
|
||||
$this->insertGame($gameData);
|
||||
$results['imported']++;
|
||||
$log(sprintf("Successfully imported game: %s", $gameTitle));
|
||||
} else {
|
||||
$log(sprintf("Skipping unchanged game: %s", $gameTitle));
|
||||
$results['skipped']++;
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
$results['errors'][] = "Failed to import {$gameData['title']}: " . $e->getMessage();
|
||||
$errorMsg = sprintf("Failed to import %s: %s", $gameTitle, $e->getMessage());
|
||||
$results['errors'][] = $errorMsg;
|
||||
$log("ERROR: " . $errorMsg);
|
||||
}
|
||||
|
||||
// Log progress every 10 games or if it's the last game
|
||||
if (($index + 1) % 10 === 0 || ($index + 1) === $totalGames) {
|
||||
$log(sprintf("Progress: %d/%d games processed", $index + 1, $totalGames));
|
||||
}
|
||||
}
|
||||
|
||||
$log(sprintf(
|
||||
"Import completed: %d imported, %d updated, %d skipped, %d errors",
|
||||
$results['imported'],
|
||||
$results['updated'],
|
||||
$results['skipped'],
|
||||
count($results['errors'])
|
||||
));
|
||||
|
||||
return $results;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user