This commit is contained in:
Lars Behrends
2025-11-01 22:00:30 +01:00
parent cd16867390
commit 7a7977d8b0
13 changed files with 2011 additions and 559 deletions

View File

@@ -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;
}

View File

@@ -0,0 +1,117 @@
<?php
namespace App\Services;
class PlayniteSyncService extends BaseSyncService
{
private PlayniteImportService $importService;
private array $importResults = [
'imported' => 0,
'updated' => 0,
'skipped' => 0,
'errors' => []
];
public function __construct(\PDO $pdo, array $source, ?int $existingSyncLogId = null)
{
parent::__construct($pdo, $source, $existingSyncLogId);
$this->importService = new PlayniteImportService($pdo);
}
/**
* Execute the sync process
*/
protected function executeSync(string $syncType): void
{
$this->logProgress("Starting Playnite import process");
try {
// Get the import data from session
if (!isset($_SESSION['playnite_import'])) {
$this->logProgress($_SESSION);
$this->logProgress("No Playnite import data found in session");
throw new \Exception('No Playnite import data found in session');
}
$importData = $_SESSION['playnite_import'];
$games = $importData['preview_data']['games'] ?? [];
$this->logProgress(sprintf('Found %d games to import', count($games)));
$this->totalItems = count($games);
// Import the games
$this->importResults = $this->importService->importGames(
$games,
true, // Always update existing
function($message) {
$this->logProgress($message);
}
);
$this->logProgress(sprintf(
'Import completed: %d imported, %d updated, %d skipped, %d errors',
$this->importResults['imported'] ?? 0,
$this->importResults['updated'] ?? 0,
$this->importResults['skipped'] ?? 0,
count($this->importResults['errors'] ?? [])
));
// Log any errors
foreach ($this->importResults['errors'] as $error) {
$this->logProgress("ERROR: $error");
}
} catch (\Exception $e) {
$this->logProgress("ERROR: " . $e->getMessage());
throw $e;
}
}
/**
* Get the number of processed items
*/
public function getProcessedCount(): int
{
return ($this->importResults['imported'] ?? 0) +
($this->importResults['updated'] ?? 0) +
($this->importResults['skipped'] ?? 0);
}
/**
* Get the number of new items
*/
public function getNewCount(): int
{
return $this->importResults['imported'] ?? 0;
}
/**
* Get the number of updated items
*/
public function getUpdatedCount(): int
{
return $this->importResults['updated'] ?? 0;
}
/**
* Get the number of deleted items
*/
public function getDeletedCount(): int
{
return 0; // Playnite import doesn't handle deletions
}
/**
* Get a completion message
*/
public function getCompletionMessage(): string
{
return sprintf(
'Playnite import completed: %d imported, %d updated, %d skipped, %d errors',
$this->importResults['imported'] ?? 0,
$this->importResults['updated'] ?? 0,
$this->importResults['skipped'] ?? 0,
count($this->importResults['errors'] ?? [])
);
}
}