LS22 – PHP-Traktorleistungs-Rechner

<?php
declare(strict_types=1);

$geraete = [
    'Anhänger'         		=> [15, 25],
    'Ladewagen'                 => [15, 20],
    'Ballenpresse'              => [10, 15],
    'Scheibenegge'              => [10, 15],
    'Grubber'                   => [15, 20],
    'Pflug'                     => [15, 20],
    'Tiefenlockerer'  		=> [20, 25],
    'Mulcher'                   => [10, 15],
    'Sämaschine'                => [10, 15],
    'Einzelkornsämaschine'      => [10, 15],
    'Mähwerk'                   => [5, 10],
    'Wender'                    => [5, 10],
    'Schwader'                  => [5, 10],
    'Forstgeräte'               => [20, 30],
];

$ps = null;
$fehler = [];
$ergebnisse = [];

// Standardwerte für die Leistungsreserven
$reserven = $geraete;

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Traktorleistung validieren
    $eingabe = $_POST['ps'] ?? '';

    if (
        !is_scalar($eingabe)
        || !is_numeric((string) $eingabe)
        || (float) $eingabe <= 0
        || (float) $eingabe > 2000
    ) {
        $fehler[] = 'Bitte gib eine Leistung zwischen 1 und 2000 PS ein.';
    } else {
        $ps = (float) $eingabe;
    }

    // Individuelle Leistungsreserven übernehmen
    $eingabenReserven = $_POST['reserve'] ?? [];

    foreach ($geraete as $name => $standard) {
        $min = $eingabenReserven[$name]['min'] ?? $standard[0];
        $max = $eingabenReserven[$name]['max'] ?? $standard[1];

        if (
            !is_scalar($min)
            || !is_scalar($max)
            || !is_numeric((string) $min)
            || !is_numeric((string) $max)
        ) {
            $fehler[] = "Ungültige Leistungsreserve für $name.";
            continue;
        }

        $min = (float) $min;
        $max = (float) $max;

        if (
            $min < 0
            || $max < 0
            || $min > 100
            || $max > 100
            || $min > $max
        ) {
            $fehler[] = "Ungültige Leistungsreserve für $name.";
            continue;
        }

        $reserven[$name] = [$min, $max];
    }

    // Ergebnisse berechnen, sofern die Eingaben gültig sind
    if (empty($fehler) && $ps !== null) {
        foreach ($reserven as $name => [$reserveMin, $reserveMax]) {
            $leistungMin = $ps / (1 + $reserveMax / 100);
            $leistungMax = $ps / (1 + $reserveMin / 100);

            $ergebnisse[$name] = [
                'min' => $leistungMin,
                'max' => $leistungMax,
                'reserve_min' => $reserveMin,
                'reserve_max' => $reserveMax,
            ];
        }
    }
}

function h(mixed $wert): string
{
    return htmlspecialchars(
        (string) $wert,
        ENT_QUOTES | ENT_SUBSTITUTE,
        'UTF-8'
    );
}

function zahl(float $wert): string
{
    return number_format($wert, 1, ',', '.');
}
?>
<!DOCTYPE html>
<html lang="de">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <title>LS22 – Traktorleistungs-Rechner</title>

    <style>
        :root {
            color-scheme: dark;
            --bg: #121212;
            --surface: #1e1e1e;
            --border: #383838;
            --text: #eeeeee;
            --muted: #aaaaaa;
            --accent: #dd3333;
        }

        * {
            box-sizing: border-box;
        }

        body {
            margin: 0;
            padding: 30px 16px;
            background: var(--bg);
            color: var(--text);
            font-family: sans-serif;
            line-height: 1.5;
        }

        main {
            max-width: 1000px;
            margin: 0 auto;
        }

        h1, h2, h3 {
            line-height: 1.25;
        }

        .muted {
            color: var(--muted);
        }

        .panel {
            margin: 24px 0;
            padding: 20px;
            background: var(--surface);
            border: 1px solid var(--border);
            border-radius: 6px;
        }

        label {
            display: block;
            margin-bottom: 6px;
            font-weight: bold;
        }

        input {
            width: 100%;
            min-height: 40px;
            padding: 8px 10px;
            border: 1px solid var(--border);
            border-radius: 4px;
            background: var(--bg);
            color: var(--text);
            font: inherit;
        }

        button {
            min-height: 42px;
            padding: 10px 16px;
            border: 0;
            border-radius: 4px;
            background: var(--accent);
            color: white;
            font: inherit;
            font-weight: bold;
            cursor: pointer;
        }

        button:hover {
            background: #bb2222;
        }

        .tractor-input {
            max-width: 280px;
        }

        .grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
            gap: 14px;
        }

        .device {
            padding: 14px;
            border: 1px solid var(--border);
            border-radius: 4px;
        }

        .device h3 {
            margin: 0 0 12px;
        }

        .reserve-inputs {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 12px;
        }

        .reserve-inputs label {
            font-size: 0.9rem;
        }

        .actions {
            display: flex;
            flex-wrap: wrap;
            gap: 10px;
            margin-top: 20px;
        }

        .secondary {
            background: #444;
        }

        .error {
            margin: 16px 0;
            padding: 12px;
            border-left: 4px solid var(--accent);
            background: var(--surface);
        }

        .summary {
            padding: 16px;
            border-left: 4px solid var(--accent);
            background: var(--surface);
        }

        .table-wrapper {
            overflow-x: auto;
        }

        table {
            width: 100%;
            margin-top: 16px;
            border-collapse: collapse;
            white-space: nowrap;
        }

        th, td {
            padding: 12px;
            text-align: left;
            border-bottom: 1px solid var(--border);
        }

        th {
            background: var(--surface);
        }

        tbody tr:nth-child(even) {
            background: #191919;
        }

        footer {
            margin-top: 32px;
            padding-top: 16px;
            border-top: 1px solid var(--border);
            color: var(--muted);
            font-size: 0.85rem;
        }
    </style>
</head>

<body>
<main>
    <h1>LS22 – Traktorleistungs-Rechner</h1>

    <p class="muted">
        Berechne die empfohlenen Leistungsbereiche für Arbeitsgeräte
        anhand der Motorleistung und deiner gewünschten Leistungsreserven.
    </p>

    <?php if ($fehler): ?>
        <div class="error">
            <strong>Eingabefehler:</strong>
            <ul>
                <?php foreach (array_unique($fehler) as $meldung): ?>
                    <li><?= h($meldung) ?></li>
                <?php endforeach; ?>
            </ul>
        </div>
    <?php endif; ?>

    <form method="post">
        <section class="panel">
            <h2>Traktor</h2>

            <div class="tractor-input">
                <label for="ps">Motorleistung in PS</label>

                <input
                    type="number"
                    id="ps"
                    name="ps"
                    min="1"
                    max="2000"
                    step="any"
                    value="<?= h(
                        $ps !== null
                            ? (string) $ps
                            : ($_POST['ps'] ?? '')
                    ) ?>"
                    placeholder="z. B. 180"
                    required
                >
            </div>
        </section>

        <section class="panel">
            <h2>Leistungsreserven</h2>

            <p class="muted">
                Lege für jede Geräteklasse eine minimale und maximale
                Leistungsreserve fest. Die Standardwerte kannst du
                jederzeit wiederherstellen.
            </p>

            <div class="grid">
                <?php foreach ($geraete as $name => $standard): ?>
                    <?php
                    $min = $reserven[$name][0];
                    $max = $reserven[$name][1];
                    ?>

                    <div class="device">
                        <h3><?= h($name) ?></h3>

                        <div class="reserve-inputs">
                            <div>
                                <label for="min-<?= h($name) ?>">
                                    Reserve min. (%)
                                </label>

                                <input
                                    type="number"
                                    id="min-<?= h($name) ?>"
                                    name="reserve[<?= h($name) ?>][min]"
                                    min="0"
                                    max="100"
                                    step="1"
                                    value="<?= h($min) ?>"
                                    required
                                >
                            </div>

                            <div>
                                <label for="max-<?= h($name) ?>">
                                    Reserve max. (%)
                                </label>

                                <input
                                    type="number"
                                    id="max-<?= h($name) ?>"
                                    name="reserve[<?= h($name) ?>][max]"
                                    min="0"
                                    max="100"
                                    step="1"
                                    value="<?= h($max) ?>"
                                    required
                                >
                            </div>
                        </div>
                    </div>
                <?php endforeach; ?>
            </div>

            <div class="actions">
                <button type="submit">Leistung berechnen</button>

                <button
                    type="submit"
                    name="reset"
                    value="1"
                    class="secondary"
                    formnovalidate
                >
                    Standardwerte laden
                </button>
            </div>
        </section>
    </form>

    <?php if ($ergebnisse): ?>
        <section>
            <h2>Ergebnisse</h2>

            <div class="summary">
                Traktorleistung:
                <strong><?= zahl($ps) ?> PS</strong>
            </div>

            <div class="table-wrapper">
                <table>
                    <thead>
                    <tr>
                        <th>Arbeitsgerät</th>
                        <th>Reserve</th>
                        <th>Empfohlener Leistungsbereich</th>
                        <th>Konservative Obergrenze</th>
                    </tr>
                    </thead>

                    <tbody>
                    <?php foreach ($ergebnisse as $name => $daten): ?>
                        <tr>
                            <td><?= h($name) ?></td>

                            <td>
                                <?= zahl($daten['reserve_min']) ?>–<?= zahl($daten['reserve_max']) ?> %
                            </td>

                            <td>
                                <?= zahl($daten['min']) ?>–<?= zahl($daten['max']) ?> PS
                            </td>

                            <td>
                                <?= zahl($daten['min']) ?> PS
                            </td>
                        </tr>
                    <?php endforeach; ?>
                    </tbody>
                </table>
            </div>

            <p class="muted">
                Die konservative Obergrenze berücksichtigt die jeweils
                größere Leistungsreserve. Sie ist ein Richtwert und keine
                Garantie für eine bestimmte Arbeitsgeschwindigkeit.
            </p>
        </section>
    <?php endif; ?>

    <section class="panel">
        <h2>Berechnungsmodell</h2>

        <p>
            Maximale Geräteleistung =
            Traktorleistung / (1 + Leistungsreserve / 100)
        </p>

        <p class="muted">
            Die Werte sind Richtwerte für den LS22. Die tatsächliche
            Zugfähigkeit hängt unter anderem vom Gewicht, den Reifen,
            dem Untergrund und der Spielmechanik ab.
        </p>
    </section>

    <footer>
        Landwirtschafts-Simulator 22 · Traktorleistungs-Rechner
    </footer>
</main>

<script>
    // Die Standardwerte werden ohne Serveranfrage wiederhergestellt.
    document.querySelector('button[name="reset"]')
        .addEventListener('click', function () {
            document.getElementById('ps').value = '';

            <?php foreach ($geraete as $name => $werte): ?>
                document.getElementById(
                    'min-<?= h($name) ?>'
                ).value = <?= (int) $werte[0] ?>;

                document.getElementById(
                    'max-<?= h($name) ?>'
                ).value = <?= (int) $werte[1] ?>;
            <?php endforeach; ?>
        });
</script>

</body>
</html>