add games

This commit is contained in:
2025-12-02 14:55:55 +01:00
parent 975b704553
commit 46ea9d10d3
10 changed files with 634 additions and 76 deletions
+153 -67
View File
@@ -79,6 +79,101 @@ function getDb(): PDO {
return $pdo;
}
// Shared game helpers (usable for both GET and POST actions)
function saveGameStatus(int $igdbId, string $name, ?string $originalName, int $status, ?string $note = null, ?string $coverUrl = null): array {
$pdo = getDb();
static $hasCover = null;
if ($hasCover === null) {
try {
$pdo->query("SHOW COLUMNS FROM game LIKE 'cover_url'");
$hasCover = true;
} catch (Throwable $e) {
$hasCover = false;
}
}
$sql = $hasCover
? '
INSERT INTO game (igdb_id, name, original_name, status, note, cover_url)
VALUES (:igdb_id, :name, :original_name, :status, :note, :cover_url)
ON DUPLICATE KEY UPDATE
name = VALUES(name),
original_name = VALUES(original_name),
status = VALUES(status),
note = VALUES(note),
cover_url = VALUES(cover_url),
updated_at = CURRENT_TIMESTAMP
'
: '
INSERT INTO game (igdb_id, name, original_name, status, note)
VALUES (:igdb_id, :name, :original_name, :status, :note)
ON DUPLICATE KEY UPDATE
name = VALUES(name),
original_name = VALUES(original_name),
status = VALUES(status),
note = VALUES(note),
updated_at = CURRENT_TIMESTAMP
';
$stmt = $pdo->prepare($sql);
$params = [
':igdb_id' => $igdbId,
':name' => $name,
':original_name' => $originalName,
':status' => $status,
':note' => $note,
];
if ($hasCover) {
$params[':cover_url'] = $coverUrl;
}
$stmt->execute($params);
$stmt = $pdo->prepare('SELECT * FROM game WHERE igdb_id = :id LIMIT 1');
$stmt->execute([':id' => $igdbId]);
return $stmt->fetch() ?: [];
}
function getGameByIgdbId(int $igdbId): ?array {
$pdo = getDb();
$stmt = $pdo->prepare('SELECT * FROM game WHERE igdb_id = :id LIMIT 1');
$stmt->execute([':id' => $igdbId]);
return $stmt->fetch() ?: null;
}
function getLocalization(int $igdbId, string $lang = 'de'): ?array {
$pdo = getDb();
$stmt = $pdo->prepare('SELECT * FROM igdb_localizations WHERE igdb_id = :id AND lang = :lang LIMIT 1');
$stmt->execute([
':id' => $igdbId,
':lang' => $lang,
]);
$row = $stmt->fetch();
return $row ?: null;
}
function saveLocalization(int $igdbId, string $lang, ?string $title, ?string $summary, ?string $storyline, ?int $userId): array {
$pdo = getDb();
$sql = '
INSERT INTO igdb_localizations (igdb_id, lang, title, summary, storyline, user_id)
VALUES (:id, :lang, :title, :summary, :storyline, :user_id)
ON DUPLICATE KEY UPDATE
title = VALUES(title),
summary = VALUES(summary),
storyline = VALUES(storyline),
user_id = VALUES(user_id),
updated_at = CURRENT_TIMESTAMP
';
$stmt = $pdo->prepare($sql);
$stmt->execute([
':id' => $igdbId,
':lang' => $lang,
':title' => $title,
':summary' => $summary,
':storyline' => $storyline,
':user_id' => $userId,
]);
return getLocalization($igdbId, $lang);
}
// =====================================================
// IGDB + Wikipedia (GET-basiert)
// =====================================================
@@ -176,70 +271,6 @@ if (isset($_GET['action'])) {
return $data;
}
function getLocalization(int $igdbId, string $lang = 'de'): ?array {
$pdo = getDb();
$stmt = $pdo->prepare('SELECT * FROM igdb_localizations WHERE igdb_id = :id AND lang = :lang LIMIT 1');
$stmt->execute([':id' => $igdbId, ':lang' => $lang]);
$row = $stmt->fetch();
return $row ?: null;
}
function saveLocalization(int $igdbId, string $lang, ?string $title, ?string $summary, ?string $storyline, ?int $userId): array {
$pdo = getDb();
$sql = '
INSERT INTO igdb_localizations (igdb_id, lang, title, summary, storyline, user_id)
VALUES (:id, :lang, :title, :summary, :storyline, :user_id)
ON DUPLICATE KEY UPDATE
title = VALUES(title),
summary = VALUES(summary),
storyline = VALUES(storyline),
user_id = VALUES(user_id),
updated_at = CURRENT_TIMESTAMP
';
$stmt = $pdo->prepare($sql);
$stmt->execute([
':id' => $igdbId,
':lang' => $lang,
':title' => $title,
':summary' => $summary,
':storyline' => $storyline,
':user_id' => $userId,
]);
return getLocalization($igdbId, $lang);
}
function saveGameStatus(int $igdbId, string $name, ?string $originalName, int $status, ?string $note = null): array {
$pdo = getDb();
$sql = '
INSERT INTO games (igdb_id, name, original_name, status, note)
VALUES (:igdb_id, :name, :original_name, :status, :note)
ON DUPLICATE KEY UPDATE
name = VALUES(name),
original_name = VALUES(original_name),
status = VALUES(status),
note = VALUES(note),
updated_at = CURRENT_TIMESTAMP
';
$stmt = $pdo->prepare($sql);
$stmt->execute([
':igdb_id' => $igdbId,
':name' => $name,
':original_name' => $originalName,
':status' => $status,
':note' => $note,
]);
$stmt = $pdo->prepare('SELECT * FROM games WHERE igdb_id = :id LIMIT 1');
$stmt->execute([':id' => $igdbId]);
return $stmt->fetch() ?: [];
}
function getGameByIgdbId(int $igdbId): ?array {
$pdo = getDb();
$stmt = $pdo->prepare('SELECT * FROM games WHERE igdb_id = :id LIMIT 1');
$stmt->execute([':id' => $igdbId]);
return $stmt->fetch() ?: null;
}
function wikiApiSearchWithExtract(string $searchQuery): ?array {
$params = [
'action' => 'query',
@@ -340,12 +371,13 @@ if (isset($_GET['action'])) {
$offset = ($page - 1) * $limit;
$safeQuery = str_replace('"', '\\"', $query);
$body = sprintf(
'fields id,name,summary,first_release_date,cover.image_id; ' .
'limit %d; offset %d; search "%s";',
$limit,
$offset,
addslashes($query)
$safeQuery
);
$results = igdbRequest('/games', $body);
@@ -356,7 +388,7 @@ if (isset($_GET['action'])) {
? igdbImageUrl($g['cover']['image_id'], 't_cover_big')
: null;
// eigener Status aus games
// eigener Status aus game-Tabelle
$localGame = getGameByIgdbId($g['id']);
$g['my_status'] = $localGame['status'] ?? null;
@@ -530,10 +562,11 @@ if (isset($_GET['action'])) {
$orig = isset($data['original_name']) ? trim($data['original_name']) : '';
$status = (int)($data['status'] ?? 0);
$note = isset($data['note']) ? trim($data['note']) : '';
$coverUrl = isset($data['cover_url']) ? trim($data['cover_url']) : null;
if ($igdbId <= 0) error_response('Invalid \"igdb_id\"', 400);
if ($name === '') error_response('Missing \"name\"', 400);
if ($status < 0 || $status > 2) error_response('Invalid \"status\" (0, 1, 2 allowed)', 400);
$game = saveGameStatus($igdbId, $name, $orig !== '' ? $orig : null, $status, $note !== '' ? $note : null);
$game = saveGameStatus($igdbId, $name, $orig !== '' ? $orig : null, $status, $note !== '' ? $note : null, $coverUrl ?: null);
respond(['success' => true, 'game' => $game]);
break;
@@ -891,6 +924,7 @@ try {
case 'get_list': {
$type=$in['type'] ?? 'movie';
$status=$in['status'] ?? null;
$lang=$in['lang'] ?? 'de';
$q=$in['q'] ?? '';
$limit=max(1,(int)($in['limit']??50));
$offset=max(0,(int)($in['offset']??0));
@@ -967,6 +1001,30 @@ try {
$stmt=$pdo->prepare($sql); $i=1; foreach($params as $p){$stmt->bindValue($i++,$p,is_int($p)?PDO::PARAM_INT:PDO::PARAM_STR);} $stmt->execute();
resp(['ok'=>true,'items'=>$stmt->fetchAll()]);
}
if ($type==='game') {
$statusVal = null;
if ($status) {
$s = strtolower($status);
if ($s==='init') $statusVal = 0; elseif ($s==='progress') $statusVal = 1; elseif ($s==='done') $statusVal = 2;
}
$sql = "SELECT g.*,
CASE g.status WHEN 1 THEN 'Progress' WHEN 2 THEN 'Done' ELSE 'Init' END AS status,
loc.title AS loc_title,
loc.summary AS loc_summary,
loc.storyline AS loc_storyline
FROM game g
LEFT JOIN igdb_localizations loc
ON loc.igdb_id = g.igdb_id AND loc.lang = ?
WHERE 1=1";
$params = [$lang];
if ($statusVal !== null) { $sql .= " AND g.status=?"; $params[] = $statusVal; }
if ($q) { $sql .= " AND g.name LIKE ?"; $params[] = '%'.$q.'%'; }
$sql .= " ORDER BY g.name ASC LIMIT ?,?"; $params[] = $offset; $params[] = $limit;
$stmt = $pdo->prepare($sql);
$i = 1; foreach ($params as $p) { $stmt->bindValue($i++, $p, is_int($p)?PDO::PARAM_INT:PDO::PARAM_STR); }
$stmt->execute();
resp(['ok'=>true,'items'=>$stmt->fetchAll()]);
}
fail('unsupported type');
}
@@ -1208,6 +1266,34 @@ try {
]);
}
case 'set_game_status': {
$igdbId = (int)($in['igdb_id'] ?? 0);
$name = isset($in['name']) ? trim($in['name']) : '';
$orig = isset($in['original_name']) ? trim($in['original_name']) : '';
$status = (int)($in['status'] ?? 0);
$note = isset($in['note']) ? trim($in['note']) : '';
$lang = isset($in['lang']) && $in['lang'] !== '' ? $in['lang'] : 'de';
$locTitle = isset($in['title']) ? trim($in['title']) : '';
$locSummary = isset($in['summary']) ? trim($in['summary']) : '';
$locStory = isset($in['storyline']) ? trim($in['storyline']) : '';
$coverUrl = isset($in['cover_url']) ? trim($in['cover_url']) : null;
if ($igdbId <= 0) fail('Invalid "igdb_id"');
if ($name === '') fail('Missing "name"');
if ($status < 0 || $status > 2) fail('Invalid "status" (0,1,2)');
$game = saveGameStatus($igdbId, $name, $orig !== '' ? $orig : null, $status, $note !== '' ? $note : null, $coverUrl ?: null);
if ($locTitle !== '' || $locSummary !== '' || $locStory !== '') {
saveLocalization($igdbId, $lang, $locTitle ?: null, $locSummary ?: null, $locStory ?: null, null);
}
resp(['ok' => true, 'game' => $game]);
}
case 'get_game': {
$igdbId = (int)($in['igdb_id'] ?? 0);
if ($igdbId <= 0) fail('Invalid "igdb_id"');
$game = getGameByIgdbId($igdbId);
resp(['ok' => true, 'found' => (bool)$game, 'game' => $game]);
}
default: fail('unknown action');
}
} catch (Throwable $e) {