mb_substr(trim(preg_replace('/\s+/u', ' ', $value) ?? ''), 0, 80); $cleanClass = static fn (string $value): ?string => in_array($value, AION_CLASSES, true) ? $value : null; $touch = static function (int $setId) use ($pdo): void { $pdo->prepare('UPDATE gear_sets SET updated_at = NOW() WHERE id = ?')->execute([$setId]); }; switch ($action) { case 'create': $name = $cleanName((string) ($_POST['name'] ?? '')); if ($name === '') { set_flash('error', 'Bitte gib dem Set einen Namen.'); redirect('/gear.php'); } $count = $pdo->prepare('SELECT COUNT(*) FROM gear_sets WHERE user_id = ?'); $count->execute([$userId]); if ((int) $count->fetchColumn() >= GEAR_MAX_SETS) { set_flash('error', 'Du hast schon ' . GEAR_MAX_SETS . ' Sets - lösche erst ein altes.'); redirect('/gear.php'); } // "– keine Klasse –" (leer) bleibt ohne Klasse, nur wenn das Feld ganz fehlt, gilt die aus dem Profil $class = isset($_POST['class_name']) ? $cleanClass((string) $_POST['class_name']) : ($me['class_name'] ?: null); $pdo->prepare( 'INSERT INTO gear_sets (user_id, name, class_name, sort_order, updated_at) SELECT ?, ?, ?, COALESCE(MAX(sort_order), 0) + 10, NOW() FROM gear_sets WHERE user_id = ?' )->execute([$userId, $name, $class, $userId]); redirect('/gear.php?set=' . (int) $pdo->lastInsertId()); case 'update': $set = $ownSet(); $name = $cleanName((string) ($_POST['name'] ?? '')); if ($name === '') { set_flash('error', 'Der Name darf nicht leer sein.'); redirect('/gear.php?set=' . (int) $set['id']); } $note = mb_substr(trim((string) ($_POST['note'] ?? '')), 0, 255); $class = $cleanClass((string) ($_POST['class_name'] ?? '')); [$buildUrl, $buildError] = gear_build_url_input((string) ($_POST['build_url'] ?? '')); if ($buildError !== null) { set_flash('error', $buildError); redirect('/gear.php?set=' . (int) $set['id']); } $pdo->prepare('UPDATE gear_sets SET name = ?, class_name = ?, note = ?, build_url = ?, show_on_profile = ?, updated_at = NOW() WHERE id = ?') ->execute([$name, $class, $note !== '' ? $note : null, $buildUrl, isset($_POST['show_on_profile']) ? 1 : 0, $set['id']]); set_flash('success', 'Set gespeichert.'); // Neue Klasse: Waffen, die sie nicht tragen kann, bleiben drin (im Raster rot markiert) - nur Bescheid geben if ($class !== null && $class !== $set['class_name']) { $setSlots = gear_slots_for_sets([(int) $set['id']])[(int) $set['id']] ?? []; $wrong = []; foreach (GEAR_SLOTS as $slotKey => [$slotLabel]) { $item = $setSlots[$slotKey]['item'] ?? null; if ($item && !gear_item_fits_class($item, $class)) { $wrong[] = $slotLabel . ' (' . $item['name'] . ')'; } } if ($wrong) { set_flash('info', 'Passt nicht zu ' . $class . ': ' . implode(', ', $wrong) . ' – neu wählen oder Slot leeren.'); } } redirect('/gear.php?set=' . (int) $set['id']); case 'duplicate': $set = $ownSet(); $count = $pdo->prepare('SELECT COUNT(*) FROM gear_sets WHERE user_id = ?'); $count->execute([$userId]); if ((int) $count->fetchColumn() >= GEAR_MAX_SETS) { set_flash('error', 'Du hast schon ' . GEAR_MAX_SETS . ' Sets - lösche erst ein altes.'); redirect('/gear.php?set=' . (int) $set['id']); } $pdo->beginTransaction(); $pdo->prepare( 'INSERT INTO gear_sets (user_id, name, class_name, note, build_url, show_on_profile, sort_order, updated_at) SELECT user_id, ?, class_name, note, build_url, 0, sort_order + 1, NOW() FROM gear_sets WHERE id = ?' )->execute([mb_substr($set['name'] . ' (Kopie)', 0, 80), $set['id']]); $newId = (int) $pdo->lastInsertId(); $pdo->prepare( 'INSERT INTO gear_set_slots (set_id, slot, item_id, enchant, exceed, soul_imprint, manastones, godstone) SELECT ?, slot, item_id, enchant, exceed, soul_imprint, manastones, godstone FROM gear_set_slots WHERE set_id = ?' )->execute([$newId, $set['id']]); $pdo->commit(); set_flash('success', 'Set kopiert - jetzt kannst du Varianten ausprobieren.'); redirect('/gear.php?set=' . $newId); case 'delete': $set = $ownSet(); $pdo->prepare('DELETE FROM gear_sets WHERE id = ?')->execute([$set['id']]); set_flash('success', 'Set „' . $set['name'] . '“ gelöscht.'); redirect('/gear.php'); case 'clear_slot': $set = $ownSet(); $slot = (string) ($_POST['slot'] ?? ''); $pdo->prepare('DELETE FROM gear_set_slots WHERE set_id = ? AND slot = ?')->execute([$set['id'], $slot]); $touch((int) $set['id']); redirect('/gear.php?set=' . (int) $set['id']); case 'save_slot': $set = $ownSet(); $slot = (string) ($_POST['slot'] ?? ''); $back = '/gear.php?set=' . (int) $set['id'] . '&slot=' . rawurlencode($slot); $fail = static function (string $message, string $to) use ($wantsJson, $json): void { if ($wantsJson) { $json(['ok' => false, 'error' => $message]); } set_flash('error', $message); redirect($to); }; if (!isset(GEAR_SLOTS[$slot])) { $fail('Unbekannter Slot.', '/gear.php?set=' . (int) $set['id']); } $item = gear_item((int) ($_POST['item_id'] ?? 0)); if (!$item) { $fail('Bitte einen Gegenstand aus der Liste wählen.', $back); } // Bei einem anderen Gegenstand passen Seelenpraegung und Manasteine nicht mehr - neu anfangen. // Der schon eingesetzte darf weiter bearbeitet werden, auch wenn er nicht mehr passt (z.B. nach // einem Klassenwechsel); neu einsetzen laesst sich nur, was in den Slot und zur Klasse passt. $current = $pdo->prepare('SELECT item_id FROM gear_set_slots WHERE set_id = ? AND slot = ?'); $current->execute([$set['id'], $slot]); $sameItem = (int) $current->fetchColumn() === (int) $item['id']; if (!$sameItem && !gear_item_fits_slot($item, $slot)) { $fail('„' . $item['name'] . '“ passt nicht in den Slot ' . GEAR_SLOTS[$slot][0] . '.', $back); } if (!$sameItem && !gear_item_fits_class($item, $set['class_name'])) { $fail('„' . $item['name'] . '“ ist keine Waffe für ' . $set['class_name'] . '.', $back); } $enchant = max(0, min((int) $item['enchant_max'], (int) ($_POST['enchant'] ?? 0))); $exceed = max(0, min((int) $item['exceed_max'], (int) ($_POST['exceed'] ?? 0))); $errors = []; $soul = []; if ($sameItem) { $ranges = []; foreach ($item['soul_imprint'] as $entry) { $ranges[$entry[0]] = $entry; } $stats = (array) ($_POST['soul_stat'] ?? []); $values = (array) ($_POST['soul_value'] ?? []); foreach ($stats as $i => $stat) { $stat = (string) $stat; $raw = str_replace(',', '.', trim((string) ($values[$i] ?? ''))); if ($stat === '' || $raw === '') { continue; } if (!isset($ranges[$stat])) { $errors[] = 'Seelenprägung „' . $stat . '“ gibt es bei diesem Gegenstand nicht.'; continue; } if (isset(array_column($soul, 1, 0)[$stat])) { $errors[] = 'Seelenprägung „' . $stat . '“ ist doppelt eingetragen.'; continue; } $value = (float) $raw; [, $min, $max, $unit] = $ranges[$stat]; if (!is_numeric($raw) || $value < $min || $value > $max) { $errors[] = $stat . ': ' . gear_format_value($value, $unit) . ' liegt nicht in der möglichen Spanne ' . gear_format_value((float) $min, $unit) . '–' . gear_format_value((float) $max, $unit) . '.'; continue; } $soul[] = [$stat, $value]; } if (count($soul) > (int) $item['soul_imprint_count']) { $errors[] = 'Höchstens ' . (int) $item['soul_imprint_count'] . ' Seelenprägungen.'; $soul = array_slice($soul, 0, (int) $item['soul_imprint_count']); } } $manastones = []; if ($sameItem) { $allowed = array_flip(gear_manastone_stats()); $stats = (array) ($_POST['mana_stat'] ?? []); $values = (array) ($_POST['mana_value'] ?? []); foreach ($stats as $i => $stat) { $stat = (string) $stat; $raw = trim((string) ($values[$i] ?? '')); if ($stat === '' || $raw === '') { continue; } if (!isset($allowed[$stat]) || !ctype_digit($raw) || (int) $raw < 1 || (int) $raw > 9999) { $errors[] = 'Manastein ' . ($i + 1) . ': bitte einen Wert aus der Liste und eine ganze Zahl eintragen.'; continue; } $manastones[] = [$stat, (int) $raw]; } $manastones = array_slice($manastones, 0, (int) $item['manastone_slots']); } $godstone = (int) $item['godstone_slots'] > 0 ? mb_substr(trim((string) ($_POST['godstone'] ?? '')), 0, 100) : ''; $pdo->prepare( 'INSERT INTO gear_set_slots (set_id, slot, item_id, enchant, exceed, soul_imprint, manastones, godstone) VALUES (?, ?, ?, ?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE item_id = VALUES(item_id), enchant = VALUES(enchant), exceed = VALUES(exceed), soul_imprint = VALUES(soul_imprint), manastones = VALUES(manastones), godstone = VALUES(godstone)' )->execute([ $set['id'], $slot, $item['id'], $enchant, $exceed, json_encode($soul, JSON_UNESCAPED_UNICODE), json_encode($manastones, JSON_UNESCAPED_UNICODE), $godstone !== '' ? $godstone : null, ]); $touch((int) $set['id']); if ($wantsJson) { // Neuer Gegenstand: die Seite laedt neu (andere Felder fuer Seelenpraegung und Manasteine) $json(['ok' => true, 'reload' => !$sameItem, 'errors' => $errors]); } if ($errors) { set_flash('error', 'Gespeichert, aber nicht alles übernommen: ' . implode(' ', $errors)); redirect($back); } if (!$sameItem && ((int) $item['soul_imprint_count'] > 0 || (int) $item['manastone_slots'] > 0)) { set_flash('success', '„' . $item['name'] . '“ eingesetzt - trag jetzt Seelenprägung und Manasteine ein.'); redirect($back); } set_flash('success', GEAR_SLOTS[$slot][0] . ' gespeichert.'); redirect('/gear.php?set=' . (int) $set['id']); } set_flash('error', 'Unbekannte Aktion.'); redirect('/gear.php');