Files
game1.shadow-land.de/www/public/api/airports.php

143 lines
3.4 KiB
PHP

<?php
/**
* Airline Tycoon - Airports API
* Flughafen-bezogene API-Endpunkte
*/
require_once dirname(__DIR__) . '/src/bootstrap.php';
header('Content-Type: application/json; charset=utf-8');
$action = $_GET['action'] ?? $_POST['action'] ?? '';
switch ($action) {
case 'list':
handleList();
break;
case 'get':
handleGet();
break;
case 'search':
handleSearch();
break;
case 'distance':
handleDistance();
break;
default:
jsonResponse(['error' => 'Unbekannte Aktion'], 400);
}
function handleList() {
global $db;
// Airports aus CSV laden falls nötig
$airports = $db->getAllAirports();
if (empty($airports)) {
$config = require dirname(__DIR__) . '/src/config.php';
$count = $db->loadAirportsFromCsv($config['paths']['data'] . '/airports.csv');
$airports = $db->getAllAirports();
}
// Optional filtern
$continent = $_GET['continent'] ?? null;
$country = $_GET['country'] ?? null;
if ($continent) {
$airports = array_filter($airports, fn($a) => $a['continent'] === $continent);
}
if ($country) {
$airports = array_filter($airports, fn($a) => $a['country'] === $country);
}
jsonResponse([
'success' => true,
'airports' => array_values($airports),
'count' => count($airports)
]);
}
function handleGet() {
global $db;
$id = (int) ($_GET['id'] ?? 0);
$iata = $_GET['iata'] ?? '';
if ($id > 0) {
$airport = $db->selectOne("SELECT * FROM airports WHERE id = ?", [$id]);
} elseif ($iata) {
$airport = $db->getAirportByIata($iata);
} else {
jsonResponse(['error' => 'ID oder IATA-Code erforderlich'], 400);
}
if (!$airport) {
jsonResponse(['error' => 'Flughafen nicht gefunden'], 404);
}
jsonResponse([
'success' => true,
'airport' => $airport
]);
}
function handleSearch() {
global $db;
$query = trim($_GET['q'] ?? '');
if (strlen($query) < 2) {
jsonResponse(['error' => 'Suchbegriff zu kurz (min 2 Zeichen)'], 400);
}
$query = '%' . $query . '%';
$airports = $db->select(
"SELECT * FROM airports
WHERE name LIKE ? OR city LIKE ? OR iata_code LIKE ? OR country LIKE ?
ORDER BY name
LIMIT 20",
[$query, $query, $query, $query]
);
jsonResponse([
'success' => true,
'airports' => $airports,
'count' => count($airports)
]);
}
function handleDistance() {
global $db;
$from = (int) ($_GET['from'] ?? 0);
$to = (int) ($_GET['to'] ?? 0);
if ($from <= 0 || $to <= 0) {
jsonResponse(['error' => 'Gültige Flughafen-IDs erforderlich'], 400);
}
$fromAirport = $db->selectOne("SELECT * FROM airports WHERE id = ?", [$from]);
$toAirport = $db->selectOne("SELECT * FROM airports WHERE id = ?", [$to]);
if (!$fromAirport || !$toAirport) {
jsonResponse(['error' => 'Ein oder beide Flughäfen nicht gefunden'], 404);
}
$distance = $db->calculateDistance(
$fromAirport['latitude'], $fromAirport['longitude'],
$toAirport['latitude'], $toAirport['longitude']
);
jsonResponse([
'success' => true,
'distance' => $distance,
'from' => $fromAirport,
'to' => $toAirport
]);
}