feat(phase2): aircraft market, fleet management, finances, loans

- market.php: Buy new aircraft (6 types: Dash8 to A380)
- aircraft.php: Fleet management, crew assignment, route planning
- finances.php: Account balance, transactions, loans
- Database.php: Extended with loan/transaction/aircraft methods
- schema.sql: Aircraft types table, 10M starting balance, INSERT OR IGNORE
- dashboard.php: Updated with fleet/financial overview
- Fixed: duplicate function declarations in Database.php
- Fixed: initializeSchema() with idempotent error handling
This commit is contained in:
2026-04-24 17:27:02 +02:00
parent ab5f97acbf
commit 12ddf5a185
9 changed files with 2052 additions and 36 deletions

View File

@@ -49,6 +49,37 @@ switch ($action) {
handleLeaderboard();
break;
// NEU: Kredite
case 'take_loan':
handleTakeLoan();
break;
case 'repay_loan':
handleRepayLoan();
break;
case 'loans':
handleLoans();
break;
// NEU: Wartung
case 'maintenance':
handleMaintenance();
break;
case 'aircraft_details':
handleAircraftDetails();
break;
// NEU: Finanzen
case 'transactions':
handleTransactions();
break;
case 'finance_summary':
handleFinanceSummary();
break;
default:
jsonResponse(['error' => 'Unbekannte Aktion'], 400);
}
@@ -329,3 +360,216 @@ function handleLeaderboard() {
'count' => count($leaders)
]);
}
// ============================================
// KREDIT HANDLERS
// ============================================
function handleTakeLoan() {
global $db, $config;
$userId = Session::getUserId();
// CSRF Check
if (!Session::validateCsrfToken($_POST['csrf_token'] ?? '')) {
jsonResponse(['error' => 'Ungültiges CSRF-Token'], 403);
}
$amount = (float) ($_POST['amount'] ?? 0);
if ($amount <= 0) {
jsonResponse(['error' => 'Ungültiger Betrag'], 400);
}
try {
$loanId = $db->createLoan($userId, $amount);
// Update user balance
$user = $db->getUserById($userId);
jsonResponse([
'success' => true,
'message' => 'Kredit über ' . number_format($amount, 0, ',', '.') . ' € aufgenommen!',
'loan_id' => $loanId,
'amount' => $amount,
'new_balance' => $user['balance'] + $amount,
'interest_rate' => $config['loans']['interest_rate'],
'daily_interest' => $amount * $config['loans']['interest_rate']
]);
} catch (Exception $e) {
jsonResponse(['error' => $e->getMessage()], 400);
}
}
function handleRepayLoan() {
global $db;
$userId = Session::getUserId();
// CSRF Check
if (!Session::validateCsrfToken($_POST['csrf_token'] ?? '')) {
jsonResponse(['error' => 'Ungültiges CSRF-Token'], 403);
}
$loanId = (int) ($_POST['loan_id'] ?? 0);
$amount = (float) ($_POST['amount'] ?? 0);
if ($loanId <= 0 || $amount <= 0) {
jsonResponse(['error' => 'Ungültige Parameter'], 400);
}
try {
$result = $db->repayLoan($userId, $loanId, $amount);
jsonResponse([
'success' => true,
'message' => number_format($result['repaid'], 2, ',', '.') . ' € zurückgezahlt!',
'repaid' => $result['repaid'],
'remaining' => $result['remaining'],
'new_balance' => $result['new_balance']
]);
} catch (Exception $e) {
jsonResponse(['error' => $e->getMessage()], 400);
}
}
function handleLoans() {
global $db;
$userId = Session::getUserId();
$loans = $db->getUserLoans($userId);
$totalDebt = $db->getTotalDebt($userId);
$totalDebtWithInterest = $db->getTotalDebtWithInterest($userId);
jsonResponse([
'success' => true,
'loans' => $loans,
'total_debt' => $totalDebt,
'total_debt_with_interest' => $totalDebtWithInterest,
'count' => count($loans)
]);
}
// ============================================
// WARTUNG HANDLERS
// ============================================
function handleMaintenance() {
global $db, $config;
$userId = Session::getUserId();
// CSRF Check
if (!Session::validateCsrfToken($_POST['csrf_token'] ?? '')) {
jsonResponse(['error' => 'Ungültiges CSRF-Token'], 403);
}
$aircraftId = (int) ($_POST['aircraft_id'] ?? 0);
if ($aircraftId <= 0) {
jsonResponse(['error' => 'Flugzeug-ID erforderlich'], 400);
}
try {
$result = $db->performMaintenance($userId, $aircraftId);
jsonResponse([
'success' => true,
'message' => 'Wartung erfolgreich durchgeführt!',
'cost' => $result['cost'],
'condition_before' => $result['condition_before'],
'condition_after' => $result['condition_after'],
'new_balance' => $result['new_balance']
]);
} catch (Exception $e) {
jsonResponse(['error' => $e->getMessage()], 400);
}
}
function handleAircraftDetails() {
global $db;
$userId = Session::getUserId();
$aircraftId = (int) ($_GET['id'] ?? 0);
if ($aircraftId <= 0) {
jsonResponse(['error' => 'Flugzeug-ID erforderlich'], 400);
}
$aircraft = $db->selectOne(
"SELECT * FROM aircraft WHERE id = ? AND user_id = ?",
[$aircraftId, $userId]
);
if (!$aircraft) {
jsonResponse(['error' => 'Flugzeug nicht gefunden'], 404);
}
// Get maintenance history for this aircraft
$maintenanceHistory = $db->select(
"SELECT * FROM maintenance_history WHERE aircraft_id = ? ORDER BY created_at DESC LIMIT 10",
[$aircraftId]
);
// Get aircraft config for maintenance cost estimate
$config = $db->getConfig();
$aircraftConfig = $config['aircraft_types'][$aircraft['aircraft_type']] ?? [];
$maintenanceCost = $aircraftConfig['maintenance_base_cost'] ?? 50000;
// Adjust for condition
$conditionLoss = 100 - $aircraft['condition'];
$estimatedCost = $maintenanceCost + ($conditionLoss * 0.01 * $maintenanceCost);
jsonResponse([
'success' => true,
'aircraft' => $aircraft,
'maintenance_history' => $maintenanceHistory,
'maintenance_cost' => $estimatedCost,
'maintenance_interval' => $config['game']['maintenance_interval']
]);
}
// ============================================
// FINANZEN HANDLERS
// ============================================
function handleTransactions() {
global $db;
$userId = Session::getUserId();
$limit = min(100, (int) ($_GET['limit'] ?? 50));
$transactions = $db->getUserTransactions($userId, $limit);
jsonResponse([
'success' => true,
'transactions' => $transactions,
'count' => count($transactions)
]);
}
function handleFinanceSummary() {
global $db;
$userId = Session::getUserId();
$user = $db->getUserById($userId);
$today = date('Y-m-d');
$weekAgo = date('Y-m-d', strtotime('-7 days'));
$monthAgo = date('Y-m-d', strtotime('-30 days'));
$todayStats = $db->getTransactionSummary($userId, $today, $today);
$weekStats = $db->getTransactionSummary($userId, $weekAgo, $today);
$monthStats = $db->getTransactionSummary($userId, $monthAgo, $today);
$loans = $db->getUserLoans($userId);
$totalDebt = $db->getTotalDebtWithInterest($userId);
jsonResponse([
'success' => true,
'balance' => $user['balance'],
'net_worth' => $user['balance'] - $totalDebt,
'total_debt' => $totalDebt,
'today' => $todayStats,
'week' => $weekStats,
'month' => $monthStats,
'active_loans' => count($loans)
]);
}