39030-vm/save_world.php
Flatlogic Bot 6489bf1f14 ai
2026-03-07 04:07:33 +00:00

47 lines
1.3 KiB
PHP

<?php
declare(strict_types=1);
header('Content-Type: application/json');
require_once __DIR__ . '/db/config.php';
require_once __DIR__ . '/includes/worlds.php';
try {
ensureWorldsTable();
} catch (Throwable $e) {
echo json_encode(['success' => false, 'error' => 'Database unavailable.']);
exit;
}
$payload = json_decode(file_get_contents('php://input'), true);
if (!is_array($payload)) {
echo json_encode(['success' => false, 'error' => 'Invalid payload.']);
exit;
}
$name = trim((string)($payload['name'] ?? ''));
$world = $payload['world'] ?? null;
$size = (int)($payload['size'] ?? 0);
if ($name === '' || mb_strlen($name) < 2 || mb_strlen($name) > 80) {
echo json_encode(['success' => false, 'error' => 'World name must be 2-80 characters.']);
exit;
}
if (!is_array($world) || $size <= 0 || $size > 32) {
echo json_encode(['success' => false, 'error' => 'World data is invalid.']);
exit;
}
$worldJson = json_encode($world, JSON_UNESCAPED_UNICODE);
if ($worldJson === false || strlen($worldJson) > 200000) {
echo json_encode(['success' => false, 'error' => 'World data too large.']);
exit;
}
try {
$id = saveWorld($name, $worldJson, $size);
echo json_encode(['success' => true, 'id' => $id]);
} catch (Throwable $e) {
echo json_encode(['success' => false, 'error' => 'Unable to save world right now.']);
}