diff --git a/admin.php b/admin.php index d2201b1..16faec9 100644 --- a/admin.php +++ b/admin.php @@ -173,6 +173,8 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST[' $fa_icon = $_POST['fa_icon']; $color = $_POST['color']; $image_url = null; + $alliance_ids = isset($_POST['alliances']) ? $_POST['alliances'] : []; + if ($id > 0) { $stmt_img = $db->prepare("SELECT image_url FROM factions WHERE id = ?"); $stmt_img->execute([$id]); @@ -192,7 +194,18 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST[' } else { $stmt = $db->prepare("INSERT INTO factions (name, slug, image_url, fa_icon, color) VALUES (?, ?, ?, ?, ?)"); $stmt->execute([$name, $slug, $image_url, $fa_icon, $color]); + $id = $db->lastInsertId(); } + + // Handle Alliances (Reciprocal) + $db->prepare("DELETE FROM faction_alliances WHERE faction_id_1 = ? OR faction_id_2 = ?")->execute([$id, $id]); + foreach ($alliance_ids as $ally_id) { + $f1 = min((int)$id, (int)$ally_id); + $f2 = max((int)$id, (int)$ally_id); + if ($f1 === $f2) continue; + $db->prepare("INSERT IGNORE INTO faction_alliances (faction_id_1, faction_id_2) VALUES (?, ?)")->execute([$f1, $f2]); + } + header("Location: admin.php?tab=factions&success=1"); exit; } @@ -325,6 +338,7 @@ if ($tab === 'users') { $stmt->execute([$obj['id']]); $obj['modifier_ids'] = $stmt->fetchAll(PDO::FETCH_COLUMN); } + unset($obj); $modifiers_list = $db->query("SELECT * FROM modifiers ORDER BY type, name ASC")->fetchAll(); } elseif ($tab === 'statuses') { $statuses_list = $db->query("SELECT * FROM celestial_object_statuses ORDER BY name ASC")->fetchAll(); @@ -334,6 +348,12 @@ if ($tab === 'users') { $modifiers_list = $db->query("SELECT * FROM modifiers ORDER BY type, name ASC")->fetchAll(); } elseif ($tab === 'factions') { $factions_list = $db->query("SELECT * FROM factions ORDER BY name ASC")->fetchAll(); + foreach ($factions_list as &$f) { + $stmt = $db->prepare("SELECT faction_id_1 as ally_id FROM faction_alliances WHERE faction_id_2 = ? UNION SELECT faction_id_2 as ally_id FROM faction_alliances WHERE faction_id_1 = ?"); + $stmt->execute([$f['id'], $f['id']]); + $f['alliance_ids'] = $stmt->fetchAll(PDO::FETCH_COLUMN); + } + unset($f); } elseif ($tab === 'resources') { $resources_list = $db->query("SELECT * FROM game_resources ORDER BY name ASC")->fetchAll(); } elseif ($tab === 'lootboxes') { @@ -348,6 +368,7 @@ if ($tab === 'users') { $stmt_i->execute([$lb['id']]); $lb['items'] = $stmt_i->fetchAll(); } + unset($lb); } ?> @@ -738,13 +759,28 @@ if ($tab === 'users') { + +
| Couleur | Visuel | Nom | Slug | Actions | |
|---|---|---|---|---|---|
| Couleur | Visuel | Nom | Slug | Alliances | Actions |
|
+ + + ' . htmlspecialchars($fl['name']) . ''; + break; + } + } + } + echo !empty($allies) ? implode(', ', $allies) : 'Aucune'; + ?> + + | @@ -1035,6 +1087,21 @@ if ($tab === 'users') { document.getElementById('fac_color_picker').value = color.startsWith('#') ? color : '#808080'; } document.getElementById('fac_fa_icon').value = data.fa_icon || ''; + + // Handle Alliances + document.querySelectorAll('.alliance-checkbox').forEach(cb => cb.checked = false); + document.querySelectorAll('.alliance-label').forEach(lbl => lbl.style.display = 'flex'); + + // Hide current faction from alliance list + const currentLabel = document.querySelector(`.alliance-label[data-id="${data.id}"]`); + if (currentLabel) currentLabel.style.display = 'none'; + + if (data.alliance_ids) { + data.alliance_ids.forEach(aid => { + const cb = document.querySelector(`.alliance-checkbox[value="${aid}"]`); + if (cb) cb.checked = true; + }); + } window.scrollTo(0,0); } function resetFactionForm() { @@ -1042,6 +1109,8 @@ if ($tab === 'users') { document.getElementById('fac_id').value = 0; document.getElementById('fac_color').value = '#808080'; document.getElementById('fac_color_picker').value = '#808080'; + document.querySelectorAll('.alliance-checkbox').forEach(cb => cb.checked = false); + document.querySelectorAll('.alliance-label').forEach(lbl => lbl.style.display = 'flex'); } function editResource(data) { diff --git a/db/migrate_faction_alliances.php b/db/migrate_faction_alliances.php new file mode 100644 index 0000000..190f298 --- /dev/null +++ b/db/migrate_faction_alliances.php @@ -0,0 +1,20 @@ +exec("CREATE TABLE IF NOT EXISTS faction_alliances ( + id INT AUTO_INCREMENT PRIMARY KEY, + faction_id_1 INT NOT NULL, + faction_id_2 INT NOT NULL, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + UNIQUE KEY unique_alliance (faction_id_1, faction_id_2), + FOREIGN KEY (faction_id_1) REFERENCES factions(id) ON DELETE CASCADE, + FOREIGN KEY (faction_id_2) REFERENCES factions(id) ON DELETE CASCADE + )"); + echo "Table 'faction_alliances' created or already exists.\n"; +} catch (PDOException $e) { + die("Migration failed: " . $e->getMessage()); +} + diff --git a/gm_console.php b/gm_console.php index 2244020..34ec132 100644 --- a/gm_console.php +++ b/gm_console.php @@ -219,6 +219,7 @@ if ($view === 'sector') { } // Fetch Cities + unset($slot_data); $stmt = $db->prepare("SELECT * FROM cities WHERE planet_id IN ($placeholders)"); $stmt->execute($planet_ids); $all_cities = $stmt->fetchAll(PDO::FETCH_ASSOC); @@ -243,6 +244,7 @@ if ($view === 'sector') { } } } + unset($slot_data); } $stmt = $db->prepare("SELECT name, status FROM sectors WHERE id = ?"); $stmt->execute([$sector_id]); diff --git a/index.php b/index.php index 43683e5..4d6046b 100644 --- a/index.php +++ b/index.php @@ -69,6 +69,7 @@ if ($view === 'sector') { } // Fetch Cities + unset($slot_data); $stmt = $db->prepare("SELECT c.*, st.name as type_name FROM cities c LEFT JOIN settlement_types st ON c.settlement_type_id = st.id @@ -97,6 +98,7 @@ if ($view === 'sector') { } } } + unset($slot_data); $stmt = $db->prepare("SELECT name FROM sectors WHERE id = ?"); $stmt->execute([$sector_id]); |