mirror of
https://github.com/ceratic/MediaCollectorLibary.git
synced 2026-05-13 23:56:46 +02:00
Stuff i guess ?
This commit is contained in:
@@ -5,16 +5,164 @@ namespace App\Controllers;
|
||||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
use App\Models\Game;
|
||||
use App\Services\SteamGridDbService;
|
||||
use Slim\Views\Twig;
|
||||
use GuzzleHttp\Client;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
|
||||
class GameController extends Controller
|
||||
{
|
||||
private \PDO $pdo;
|
||||
private SteamGridDbService $steamGridDb;
|
||||
|
||||
public function __construct(\PDO $pdo, Twig $view)
|
||||
{
|
||||
parent::__construct($view);
|
||||
$this->pdo = $pdo;
|
||||
$this->steamGridDb = new SteamGridDbService();
|
||||
}
|
||||
|
||||
/**
|
||||
* Search for games on SteamGridDB
|
||||
*/
|
||||
public function searchSteamGridDb(Request $request, Response $response, array $args): Response
|
||||
{
|
||||
$query = $request->getQueryParams()['q'] ?? '';
|
||||
$results = [];
|
||||
|
||||
if (!empty($query)) {
|
||||
$results = $this->steamGridDb->searchGames($query);
|
||||
}
|
||||
|
||||
return $this->json($response, [
|
||||
'success' => true,
|
||||
'data' => $results
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get media from SteamGridDB
|
||||
*/
|
||||
public function getSteamGridDbMedia(Request $request, Response $response, array $args): Response
|
||||
{
|
||||
$gameId = $args['gameId'] ?? null;
|
||||
$type = $args['type'] ?? 'grids';
|
||||
$media = [];
|
||||
|
||||
if ($gameId) {
|
||||
switch ($type) {
|
||||
case 'grids':
|
||||
$media = $this->steamGridDb->getGrids($gameId, [
|
||||
'styles' => ['alternate', 'blurred', 'white_logo', 'material', 'no_logo'],
|
||||
'dimensions' => ['600x900', '920x430', '460x215', '920x430']
|
||||
]);
|
||||
break;
|
||||
case 'heroes':
|
||||
$media = $this->steamGridDb->getHeroes($gameId, [
|
||||
'dimensions' => ['1920x620', '3840x1240']
|
||||
]);
|
||||
break;
|
||||
case 'icons':
|
||||
$media = $this->steamGridDb->getIcons($gameId, [
|
||||
'dimensions' => ['32x32', '64x64', '128x128', '256x256', '512x512']
|
||||
]);
|
||||
break;
|
||||
case 'logos':
|
||||
$media = $this->steamGridDb->getLogos($gameId);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return $this->json($response, [
|
||||
'success' => true,
|
||||
'data' => $media
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Download and set media from SteamGridDB
|
||||
*/
|
||||
public function setSteamGridDbMedia(Request $request, Response $response, array $args): Response
|
||||
{
|
||||
$gameId = $args['id'] ?? null;
|
||||
$data = $request->getParsedBody();
|
||||
$type = $data['type'] ?? '';
|
||||
$url = $data['url'] ?? '';
|
||||
$field = '';
|
||||
|
||||
if (!$gameId || !$type || !$url) {
|
||||
return $this->json($response, [
|
||||
'success' => false,
|
||||
'message' => 'Missing required parameters'
|
||||
], 400);
|
||||
}
|
||||
|
||||
// Map media type to database field
|
||||
switch ($type) {
|
||||
case 'grid':
|
||||
$field = 'image_url';
|
||||
break;
|
||||
case 'hero':
|
||||
$field = 'banner_url';
|
||||
break;
|
||||
case 'icon':
|
||||
$field = 'icon';
|
||||
break;
|
||||
case 'logo':
|
||||
$field = 'logo_url';
|
||||
break;
|
||||
default:
|
||||
return $this->json($response, [
|
||||
'success' => false,
|
||||
'message' => 'Invalid media type'
|
||||
], 400);
|
||||
}
|
||||
|
||||
// Download the media file
|
||||
$tempFile = $this->steamGridDb->downloadMedia($url);
|
||||
if (!$tempFile) {
|
||||
return $this->json($response, [
|
||||
'success' => false,
|
||||
'message' => 'Failed to download media'
|
||||
], 500);
|
||||
}
|
||||
|
||||
// Move the file to the appropriate location
|
||||
$uploadDir = __DIR__ . '/../../public/uploads/games/' . $gameId;
|
||||
if (!is_dir($uploadDir)) {
|
||||
mkdir($uploadDir, 0755, true);
|
||||
}
|
||||
|
||||
$filename = $type . '_' . uniqid() . '.' . pathinfo($url, PATHINFO_EXTENSION);
|
||||
$filepath = $uploadDir . '/' . $filename;
|
||||
$publicPath = '/uploads/games/' . $gameId . '/' . $filename;
|
||||
|
||||
if (!rename($tempFile, $filepath)) {
|
||||
return $this->json($response, [
|
||||
'success' => false,
|
||||
'message' => 'Failed to save media file'
|
||||
], 500);
|
||||
}
|
||||
|
||||
// Update the game record
|
||||
$game = Game::find($this->pdo, $gameId);
|
||||
if (!$game) {
|
||||
return $this->json($response, [
|
||||
'success' => false,
|
||||
'message' => 'Game not found'
|
||||
], 404);
|
||||
}
|
||||
|
||||
$game->{$field} = $publicPath;
|
||||
$game->save($this->pdo);
|
||||
|
||||
return $this->json($response, [
|
||||
'success' => true,
|
||||
'data' => [
|
||||
'url' => $publicPath,
|
||||
'field' => $field
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
public function index(Request $request, Response $response, $args)
|
||||
@@ -44,8 +192,11 @@ class GameController extends Controller
|
||||
// Get view mode
|
||||
$viewMode = $queryParams['view'] ?? 'grid'; // grid, list, covers
|
||||
|
||||
// Get games with pagination and filters
|
||||
$games = Game::getGroupedGamesWithPagination($this->pdo, $page, $perPage, $search, $genres, $platforms);
|
||||
// Get sort parameter
|
||||
$sort = $queryParams['sort'] ?? 'title_asc';
|
||||
|
||||
// Get games with pagination, filters, and sorting
|
||||
$games = Game::getGroupedGamesWithPagination($this->pdo, $page, $perPage, $search, $genres, $platforms, $sort);
|
||||
|
||||
// Get total count for pagination
|
||||
$totalCount = Game::getTotalCount($this->pdo, $search, $genres, $platforms);
|
||||
@@ -82,6 +233,17 @@ class GameController extends Controller
|
||||
'available_filters' => [
|
||||
'genres' => $availableGenres,
|
||||
'platforms' => $availablePlatforms
|
||||
],
|
||||
'sort' => $sort,
|
||||
'sort_options' => [
|
||||
'title_asc' => 'Title (A-Z)',
|
||||
'title_desc' => 'Title (Z-A)',
|
||||
'year_asc' => 'Release Year (Oldest First)',
|
||||
'year_desc' => 'Release Year (Newest First)',
|
||||
'playtime_desc' => 'Most Played',
|
||||
'completion_desc' => 'Highest Completion',
|
||||
'added_desc' => 'Recently Added',
|
||||
'last_played_desc' => 'Last Played'
|
||||
]
|
||||
]);
|
||||
}
|
||||
@@ -92,9 +254,9 @@ class GameController extends Controller
|
||||
|
||||
// Find the main game entry (could be any platform version)
|
||||
$stmt = $this->pdo->prepare("
|
||||
SELECT g.*, s.display_name as source_name
|
||||
SELECT g.*, g.platform as source_name
|
||||
FROM games g
|
||||
JOIN sources s ON g.source_id = s.id
|
||||
|
||||
WHERE g.game_key = :game_key
|
||||
LIMIT 1
|
||||
");
|
||||
|
||||
Reference in New Issue
Block a user