Tourismusregion: Karten-Editor reaktiviert (mit Lehrer-Guard)
editor.html + js/editor.js aus der Quelle uebernommen. save_map.php schreibt js/maps-custom.json (mit Auto-Backups) - jetzt abgesichert: nur eingeloggte Lehrpersonen duerfen speichern. Das Spiel laedt die Overrides beim Start (applyMapOverrides). Thomas fixt damit Routen- Punkte und Bauplaetze; die Datei wird danach ins Repo uebernommen. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
// Speichert Editor-Daten (Routen-Kurven + Bauslots) nach js/maps-custom.json.
|
||||
// Das Spiel lädt diese Datei beim Start und überschreibt damit die
|
||||
// eingebauten Karten-Daten. Nur lokal (XAMPP) gedacht.
|
||||
//
|
||||
// WICHTIG: Vor jedem Überschreiben wird die bisherige Datei automatisch als
|
||||
// Zeitstempel-Backup nach api/mapbackups/ gesichert – so geht nichts verloren.
|
||||
// Ein leeres {} wird abgelehnt, damit versehentliche Resets nicht speichern.
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
|
||||
// Plattform-Guard: Schreiben nur für eingeloggte Lehrpersonen.
|
||||
// Pfad funktioniert lokal (App/sims/...) UND auf Prod (Top-Level sims/...).
|
||||
require_once __DIR__ . '/../../../php/config/app.php';
|
||||
require_once __DIR__ . '/../../../php/lib/Database.php';
|
||||
require_once __DIR__ . '/../../../php/lib/Response.php';
|
||||
require_once __DIR__ . '/../../../php/lib/Session.php';
|
||||
Session::requireTeacher();
|
||||
|
||||
$raw = file_get_contents('php://input');
|
||||
$data = json_decode($raw, true);
|
||||
if (!is_array($data) || count($data) === 0) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['ok' => false, 'error' => 'invalid or empty json']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$target = __DIR__ . '/../js/maps-custom.json';
|
||||
$backupDir = __DIR__ . '/mapbackups';
|
||||
|
||||
// vorhandene, nicht-leere Datei zuerst sichern
|
||||
if (file_exists($target)) {
|
||||
$old = trim((string)file_get_contents($target));
|
||||
if ($old !== '' && $old !== '{}') {
|
||||
if (!is_dir($backupDir)) @mkdir($backupDir, 0777, true);
|
||||
@copy($target, $backupDir . '/maps-custom-' . date('Ymd-His') . '.json');
|
||||
@copy($target, $backupDir . '/maps-custom.prev.json'); // schneller Griff aufs letzte
|
||||
$files = glob($backupDir . '/maps-custom-*.json'); // max. 40 Backups halten
|
||||
if ($files && count($files) > 40) {
|
||||
sort($files);
|
||||
foreach (array_slice($files, 0, count($files) - 40) as $f) @unlink($f);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$ok = file_put_contents($target, json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
|
||||
echo json_encode(['ok' => (bool)$ok, 'file' => 'js/maps-custom.json', 'backed_up' => is_dir($backupDir)]);
|
||||
Reference in New Issue
Block a user