39315-vm/save_score.php
2026-03-25 15:17:29 +00:00

27 lines
806 B
PHP

<?php
declare(strict_types=1);
require_once __DIR__ . '/tetris_data.php';
if (($_SERVER['REQUEST_METHOD'] ?? 'GET') !== 'POST') {
header('Location: /');
exit;
}
try {
$scoreId = tetrisSaveScore([
'player_name' => $_POST['player_name'] ?? '',
'score' => $_POST['score'] ?? 0,
'lines_cleared' => $_POST['lines_cleared'] ?? 0,
'level_reached' => $_POST['level_reached'] ?? 1,
'duration_seconds' => $_POST['duration_seconds'] ?? 0,
]);
header('Location: /?saved=1&score_id=' . $scoreId . '#leaderboard');
exit;
} catch (Throwable $e) {
$message = rawurlencode($e instanceof InvalidArgumentException ? $e->getMessage() : 'Unable to save the score right now.');
header('Location: /?save_error=' . $message . '#save-score');
exit;
}