| Server IP : 202.61.199.114 / Your IP : 216.73.217.139 Web Server : nginx/1.22.1 System : Linux de.arni-solutions.de 6.1.0-49-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.174-1 (2026-05-26) x86_64 User : web20 ( 1018) PHP Version : 8.4.23 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /var/www/clients/client2/web31/web/slots/api/ |
Upload File : |
<?php
require_once __DIR__ . '/../includes/bootstrap.php';
header('Content-Type: application/json; charset=utf-8');
function dame_out($d) { echo json_encode($d, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); exit; }
function dame_fail($msg, $code = 400) { http_response_code($code); dame_out(['ok' => false, 'message' => $msg]); }
function dame_initial_board(): array {
$b = array_fill(0, 8, array_fill(0, 8, null));
for ($r = 0; $r < 8; $r++) for ($c = 0; $c < 8; $c++) {
if (($r + $c) % 2 !== 1) continue;
if ($r <= 2) $b[$r][$c] = ['c' => 2, 'k' => 0];
elseif ($r >= 5) $b[$r][$c] = ['c' => 1, 'k' => 0];
}
return $b;
}
function dame_state_row(array $m, ?string $seat = null): array {
$yourSeat = 0;
if ($seat && hash_equals($m['seat1_token'], $seat)) $yourSeat = 1;
elseif ($seat && $m['seat2_token'] && hash_equals($m['seat2_token'], $seat)) $yourSeat = 2;
return [
'ok' => true,
'token' => $m['token'],
'board' => json_decode($m['board'], true),
'turn' => (int)$m['turn'],
'status' => $m['status'],
'winner' => (int)$m['winner'],
'move_count'=> (int)$m['move_count'],
'names' => [1 => $m['seat1_name'], 2 => $m['seat2_name']],
'your_seat' => $yourSeat,
'joined' => $m['seat2_token'] ? true : false,
'last_move' => isset($m['last_move']) && $m['last_move'] !== null ? json_decode($m['last_move'], true) : null,
];
}
$payload = json_decode(file_get_contents('php://input'), true) ?: [];
$action = (string)($_GET['action'] ?? $payload['action'] ?? '');
$pdo = db();
$loadMatch = function (string $token) use ($pdo) {
$token = preg_replace('/[^a-f0-9]/', '', $token);
if ($token === '') return null;
$stmt = $pdo->prepare('SELECT * FROM dame_matches WHERE token = ? LIMIT 1');
$stmt->execute([$token]);
return $stmt->fetch() ?: null;
};
try {
if ($action === 'create') {
$user = current_user();
$name = trim((string)($payload['name'] ?? '')) ?: ($user['username'] ?? 'Spieler 1');
$name = mb_substr($name, 0, 40);
$token = bin2hex(random_bytes(8));
$seat1 = bin2hex(random_bytes(12));
$board = json_encode(dame_initial_board(), JSON_UNESCAPED_SLASHES);
$pdo->prepare('INSERT INTO dame_matches (token, board, turn, seat1_token, seat1_name, host_user_id, status, created_at, updated_at) VALUES (?,?,1,?,?,?,"waiting",NOW(),NOW())')
->execute([$token, $board, $seat1, $name, $user['id'] ?? null]);
dame_out(['ok' => true, 'token' => $token, 'seat' => $seat1, 'your_seat' => 1]);
}
if ($action === 'join') {
$m = $loadMatch((string)($payload['token'] ?? ''));
if (!$m) dame_fail('Partie nicht gefunden.', 404);
$seat = (string)($payload['seat'] ?? '');
// Rejoin mit bekanntem Sitz-Token
if ($seat && (hash_equals($m['seat1_token'], $seat) || ($m['seat2_token'] && hash_equals($m['seat2_token'], $seat)))) {
dame_out(array_merge(dame_state_row($m, $seat), ['seat' => $seat]));
}
if ($m['seat2_token']) dame_fail('Diese Partie ist bereits voll.', 409);
$user = current_user();
$name = trim((string)($payload['name'] ?? '')) ?: ($user['username'] ?? 'Spieler 2');
$name = mb_substr($name, 0, 40);
$seat2 = bin2hex(random_bytes(12));
$pdo->prepare('UPDATE dame_matches SET seat2_token=?, seat2_name=?, status="playing", updated_at=NOW() WHERE token=? AND seat2_token IS NULL')
->execute([$seat2, $name, $m['token']]);
$m = $loadMatch($m['token']);
dame_out(array_merge(dame_state_row($m, $seat2), ['seat' => $seat2]));
}
if ($action === 'state') {
$m = $loadMatch((string)($_GET['token'] ?? $payload['token'] ?? ''));
if (!$m) dame_fail('Partie nicht gefunden.', 404);
dame_out(dame_state_row($m, (string)($_GET['seat'] ?? $payload['seat'] ?? '')));
}
if ($action === 'move') {
$m = $loadMatch((string)($payload['token'] ?? ''));
if (!$m) dame_fail('Partie nicht gefunden.', 404);
if ($m['status'] === 'finished') dame_fail('Partie ist beendet.');
$seat = (string)($payload['seat'] ?? '');
$turn = (int)$m['turn'];
$expected = $turn === 1 ? $m['seat1_token'] : $m['seat2_token'];
if (!$expected || !hash_equals($expected, $seat)) dame_fail('Du bist nicht am Zug.', 403);
$board = $payload['board'] ?? null;
if (!is_array($board) || count($board) !== 8) dame_fail('Ungültiges Brett.');
$nextTurn = (int)($payload['turn'] ?? ($turn === 1 ? 2 : 1));
$winner = (int)($payload['winner'] ?? 0);
$status = $winner ? 'finished' : 'playing';
$lastMove = isset($payload['move']) ? json_encode($payload['move'], JSON_UNESCAPED_SLASHES) : null;
$pdo->prepare('UPDATE dame_matches SET board=?, turn=?, winner=?, status=?, last_move=?, move_count=move_count+1, updated_at=NOW() WHERE token=?')
->execute([json_encode($board, JSON_UNESCAPED_SLASHES), $nextTurn, $winner, $status, $lastMove, $m['token']]);
$m = $loadMatch($m['token']);
dame_out(dame_state_row($m, $seat));
}
dame_fail('Unbekannte Aktion.');
} catch (Throwable $e) {
dame_fail('Serverfehler: ' . $e->getMessage(), 500);
}