diff --git a/admin.php b/admin.php index ffe8fe7..22fa861 100644 --- a/admin.php +++ b/admin.php @@ -22,295 +22,1635 @@ $tab = isset($_GET['tab']) ? $_GET['tab'] : 'users'; // --- HANDLERS --- -// Status Profiles -if ($_SERVER["REQUEST_METHOD"] === "POST" && isset($_POST["action"]) && $_POST["action"] === "upsert_status_profile") { - $id = (int)$_POST["id"]; $name = $_POST["name"]; $slug = $_POST["slug"]; $enabled = isset($_POST["enabled"]) ? 1 : 0; $priority = (int)$_POST["priority"]; $scope_object_type = $_POST["scope_object_type"] === "" ? null : $_POST["scope_object_type"]; - $rules = []; if (isset($_POST['rule_status_id']) && is_array($_POST['rule_status_id'])) { foreach ($_POST['rule_status_id'] as $idx => $sid) { $rules[] = ['status_id' => (int)$sid, 'condition_type' => $_POST['rule_condition_type'][$idx], 'min_value' => $_POST['rule_min_value'][$idx] !== "" ? (float)$_POST['rule_min_value'][$idx] : null, 'max_value' => $_POST['rule_max_value'][$idx] !== "" ? (float)$_POST['rule_max_value'][$idx] : null]; } } - $config = json_encode(['rules' => $rules]); - if ($id > 0) { $stmt = $db->prepare("UPDATE celestial_object_status_profiles SET name = ?, slug = ?, enabled = ?, priority = ?, scope_object_type = ?, config = ? WHERE id = ?"); $stmt->execute([$name, $slug, $enabled, $priority, $scope_object_type, $config, $id]); } - else { $stmt = $db->prepare("INSERT INTO celestial_object_status_profiles (name, slug, enabled, priority, scope_object_type, config) VALUES (?, ?, ?, ?, ?, ?)"); $stmt->execute([$name, $slug, $enabled, $priority, $scope_object_type, $config]); } - header("Location: admin.php?tab=status_profiles&success=1"); exit; -} -if (isset($_GET["delete_status_profile"])) { $db->prepare("DELETE FROM celestial_object_status_profiles WHERE id = ?")->execute([(int)$_GET["delete_status_profile"]]); header("Location: admin.php?tab=status_profiles&success=1"); exit; } - -// User Roles +// Handle User Role Update if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'update_user_role') { - $target_user_id = (int)$_POST['target_user_id']; $new_role = $_POST['new_role']; - if (in_array($new_role, ['user', 'gm', 'admin'])) { $stmt = $db->prepare("UPDATE users SET role = ? WHERE id = ?"); $stmt->execute([$new_role, $target_user_id]); } - header("Location: admin.php?tab=users&success=1"); exit; + $target_user_id = (int)$_POST['target_user_id']; + $new_role = $_POST['new_role']; + if (in_array($new_role, ['user', 'gm', 'admin'])) { + $stmt = $db->prepare("UPDATE users SET role = ? WHERE id = ?"); + $stmt->execute([$new_role, $target_user_id]); + } + header("Location: admin.php?tab=users&success=1"); + exit; } -// Celestial Object Types +// Handle Celestial Object Type CRUD if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'upsert_object_type') { - $id = (int)$_POST['id']; $name = $_POST['name']; $slug = $_POST['slug']; $icon = $_POST['icon']; $description = $_POST['description']; $status_profile_id = !empty($_POST['status_profile_id']) ? (int)$_POST['status_profile_id'] : null; $modifier_ids = isset($_POST['modifiers']) ? $_POST['modifiers'] : []; - $image_url = null; if ($id > 0) { $stmt_img = $db->prepare("SELECT image_url FROM celestial_object_types WHERE id = ?"); $stmt_img->execute([$id]); $image_url = $stmt_img->fetchColumn(); } - if (isset($_FILES['image']) && $_FILES['image']['error'] === UPLOAD_ERR_OK) { $ext = pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION); $filename = $slug . "_" . time() . "." . $ext; $target = "assets/images/celestial/" . $filename; if (move_uploaded_file($_FILES['image']['tmp_name'], $target)) $image_url = $target; } - $orbital_enabled = isset($_POST["orbital_control_enabled"]) ? 1 : 0; $terrestrial_enabled = isset($_POST["terrestrial_control_enabled"]) ? 1 : 0; - if ($id > 0) { $stmt = $db->prepare("UPDATE celestial_object_types SET name = ?, slug = ?, icon = ?, description = ?, image_url = ?, orbital_control_enabled = ?, terrestrial_control_enabled = ?, status_profile_id = ? WHERE id = ?"); $stmt->execute([$name, $slug, $icon, $description, $image_url, $orbital_enabled, $terrestrial_enabled, $status_profile_id, $id]); } - else { $stmt = $db->prepare("INSERT INTO celestial_object_types (name, slug, icon, description, image_url, orbital_control_enabled, terrestrial_control_enabled, status_profile_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?)"); $stmt->execute([$name, $slug, $icon, $description, $image_url, $orbital_enabled, $terrestrial_enabled, $status_profile_id]); $id = $db->lastInsertId(); } + $id = (int)$_POST['id']; + $name = $_POST['name']; + $slug = $_POST['slug']; + $icon = $_POST['icon']; + $description = $_POST['description']; + $status_profile_id = !empty($_POST['status_profile_id']) ? (int)$_POST['status_profile_id'] : null; + $modifier_ids = isset($_POST['modifiers']) ? $_POST['modifiers'] : []; + + $image_url = null; + if ($id > 0) { + $stmt_img = $db->prepare("SELECT image_url FROM celestial_object_types WHERE id = ?"); + $stmt_img->execute([$id]); + $image_url = $stmt_img->fetchColumn(); + } + + if (isset($_FILES['image']) && $_FILES['image']['error'] === UPLOAD_ERR_OK) { + $ext = pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION); + $filename = $slug . "_" . time() . "." . $ext; + $target = "assets/images/celestial/" . $filename; + if (move_uploaded_file($_FILES['image']['tmp_name'], $target)) { + $image_url = $target; + } + } + + $orbital_enabled = isset($_POST["orbital_control_enabled"]) ? 1 : 0; + $terrestrial_enabled = isset($_POST["terrestrial_control_enabled"]) ? 1 : 0; + + if ($id > 0) { + $stmt = $db->prepare("UPDATE celestial_object_types SET name = ?, slug = ?, icon = ?, description = ?, image_url = ?, orbital_control_enabled = ?, terrestrial_control_enabled = ?, status_profile_id = ? WHERE id = ?"); + $stmt->execute([$name, $slug, $icon, $description, $image_url, $orbital_enabled, $terrestrial_enabled, $status_profile_id, $id]); + } else { + $stmt = $db->prepare("INSERT INTO celestial_object_types (name, slug, icon, description, image_url, orbital_control_enabled, terrestrial_control_enabled, status_profile_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?)"); + $stmt->execute([$name, $slug, $icon, $description, $image_url, $orbital_enabled, $terrestrial_enabled, $status_profile_id]); + $id = $db->lastInsertId(); + } + + // Sync modifiers $db->prepare("DELETE FROM celestial_object_type_modifiers WHERE celestial_object_type_id = ?")->execute([$id]); - if (!empty($modifier_ids)) { $ins = $db->prepare("INSERT INTO celestial_object_type_modifiers (celestial_object_type_id, modifier_id) VALUES (?, ?)"); foreach ($modifier_ids as $mid) $ins->execute([$id, (int)$mid]); } - header("Location: admin.php?tab=objects&success=1"); exit; -} -if (isset($_GET['delete_object'])) { $db->prepare("DELETE FROM celestial_object_types WHERE id = ?")->execute([(int)$_GET['delete_object']]); header("Location: admin.php?tab=objects&success=1"); exit; } + if (!empty($modifier_ids)) { + $ins = $db->prepare("INSERT INTO celestial_object_type_modifiers (celestial_object_type_id, modifier_id) VALUES (?, ?)"); + foreach ($modifier_ids as $mid) { + $ins->execute([$id, (int)$mid]); + } + } -// Statuses + header("Location: admin.php?tab=objects&success=1"); + exit; +} + +if (isset($_GET['delete_object'])) { + $id = (int)$_GET['delete_object']; + $db->prepare("DELETE FROM celestial_object_types WHERE id = ?")->execute([$id]); + header("Location: admin.php?tab=objects&success=1"); + exit; +} + +// Handle Status CRUD if ($_SERVER["REQUEST_METHOD"] === "POST" && isset($_POST["action"]) && $_POST["action"] === "upsert_status") { - $id = (int)$_POST["id"]; $name = $_POST["name"]; $slug = $_POST["slug"]; $color = $_POST["color"]; $description = $_POST["description"]; - if (isset($_POST["is_blinking"]) && $_POST["is_blinking"] === "on") { if (strpos($color, ";blink") === false) $color .= ";blink"; } else { $color = str_replace(";blink", "", $color); } - if ($id > 0) { $stmt = $db->prepare("UPDATE celestial_object_statuses SET name = ?, slug = ?, color = ?, description = ? WHERE id = ?"); $stmt->execute([$name, $slug, $color, $description, $id]); } - else { $stmt = $db->prepare("INSERT INTO celestial_object_statuses (name, slug, color, description) VALUES (?, ?, ?, ?)"); $stmt->execute([$name, $slug, $color, $description]); } - header("Location: admin.php?tab=statuses&success=1"); exit; -} -if (isset($_GET['delete_status'])) { $db->prepare("DELETE FROM celestial_object_statuses WHERE id = ?")->execute([(int)$_GET['delete_status']]); header("Location: admin.php?tab=statuses&success=1"); exit; } + $id = (int)$_POST["id"]; + $name = $_POST["name"]; + $slug = $_POST["slug"]; + $color = $_POST["color"]; + $description = $_POST["description"]; + + // Gérer le clignotement + if (isset($_POST["is_blinking"]) && $_POST["is_blinking"] === "on") { + if (strpos($color, ";blink") === false) { + $color .= ";blink"; + } + } else { + $color = str_replace(";blink", "", $color); + } -// Factions -if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'upsert_faction') { - $id = (int)$_POST['id']; $name = $_POST['name']; $slug = $_POST['slug']; $fa_icon = $_POST['fa_icon']; $color = $_POST['color']; $is_playable = isset($_POST['is_playable']) ? 1 : 0; - $image_url = null; if ($id > 0) { $stmt_img = $db->prepare("SELECT image_url FROM factions WHERE id = ?"); $stmt_img->execute([$id]); $image_url = $stmt_img->fetchColumn(); } - if (isset($_FILES['image']) && $_FILES['image']['error'] === UPLOAD_ERR_OK) { $ext = pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION); $filename = $slug . "_" . time() . "." . $ext; $target = "assets/images/factions/" . $filename; if (!is_dir("assets/images/factions")) mkdir("assets/images/factions", 0777, true); if (move_uploaded_file($_FILES['image']['tmp_name'], $target)) $image_url = $target; } - if ($id > 0) { $stmt = $db->prepare("UPDATE factions SET name = ?, slug = ?, fa_icon = ?, color = ?, image_url = ?, is_playable = ? WHERE id = ?"); $stmt->execute([$name, $slug, $fa_icon, $color, $image_url, $is_playable, $id]); } - else { $stmt = $db->prepare("INSERT INTO factions (name, slug, fa_icon, color, image_url, is_playable) VALUES (?, ?, ?, ?, ?, ?)"); $stmt->execute([$name, $slug, $fa_icon, $color, $image_url, $is_playable]); } - header("Location: admin.php?tab=factions&success=1"); exit; + if ($id > 0) { + $stmt = $db->prepare("UPDATE celestial_object_statuses SET name = ?, slug = ?, color = ?, description = ? WHERE id = ?"); + $stmt->execute([$name, $slug, $color, $description, $id]); + } else { + $stmt = $db->prepare("INSERT INTO celestial_object_statuses (name, slug, color, description) VALUES (?, ?, ?, ?)"); + $stmt->execute([$name, $slug, $color, $description]); + } + header("Location: admin.php?tab=statuses&success=1"); + exit; } -if (isset($_GET['delete_faction'])) { $db->prepare("DELETE FROM factions WHERE id = ?")->execute([(int)$_GET['delete_faction']]); header("Location: admin.php?tab=factions&success=1"); exit; } -// Resources -if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'upsert_resource') { - $id = (int)$_POST['id']; $name = $_POST['name']; $slug = $_POST['slug']; $icon = $_POST['icon']; $description = $_POST['description']; $show_in_header = isset($_POST["show_in_header"]) ? 1 : 0; - $image_url = null; if ($id > 0) { $stmt_img = $db->prepare("SELECT image_url FROM game_resources WHERE id = ?"); $stmt_img->execute([$id]); $image_url = $stmt_img->fetchColumn(); } - if (isset($_FILES['image']) && $_FILES['image']['error'] === UPLOAD_ERR_OK) { $ext = pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION); $filename = $slug . "_" . time() . "." . $ext; $target = "assets/images/resources/" . $filename; if (!is_dir("assets/images/resources")) mkdir("assets/images/resources", 0777, true); if (move_uploaded_file($_FILES['image']['tmp_name'], $target)) $image_url = $target; } - if ($id > 0) { $stmt = $db->prepare("UPDATE game_resources SET name = ?, slug = ?, icon = ?, description = ?, show_in_header = ?, image_url = ? WHERE id = ?"); $stmt->execute([$name, $slug, $icon, $description, $show_in_header, $image_url, $id]); } - else { $stmt = $db->prepare("INSERT INTO game_resources (name, slug, icon, description, show_in_header, image_url) VALUES (?, ?, ?, ?, ?, ?)"); $stmt->execute([$name, $slug, $icon, $description, $show_in_header, $image_url]); } - header("Location: admin.php?tab=resources&success=1"); exit; +if (isset($_GET['delete_status'])) { + $id = (int)$_GET['delete_status']; + $db->prepare("DELETE FROM celestial_object_statuses WHERE id = ?")->execute([$id]); + header("Location: admin.php?tab=statuses&success=1"); + exit; } -if (isset($_GET['delete_resource'])) { $db->prepare("DELETE FROM game_resources WHERE id = ?")->execute([(int)$_GET['delete_resource']]); header("Location: admin.php?tab=resources&success=1"); exit; } -// Modifiers -if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'upsert_modifier') { - $id = (int)$_POST['id']; $name = $_POST['name']; $type = $_POST['type']; $slug = $_POST['slug']; $description = $_POST['description']; $icon = $_POST['icon']; - if ($id > 0) { $stmt = $db->prepare("UPDATE modifiers SET name = ?, type = ?, slug = ?, description = ?, icon = ? WHERE id = ?"); $stmt->execute([$name, $type, $slug, $description, $icon, $id]); } - else { $stmt = $db->prepare("INSERT INTO modifiers (name, type, slug, description, icon) VALUES (?, ?, ?, ?, ?)"); $stmt->execute([$name, $type, $slug, $description, $icon]); } - header("Location: admin.php?tab=modifiers&success=1"); exit; +// Handle Status Profile CRUD +if ($_SERVER["REQUEST_METHOD"] === "POST" && isset($_POST["action"]) && $_POST["action"] === "upsert_status_profile") { + $id = (int)$_POST["id"]; + $name = $_POST["name"]; + $slug = $_POST["slug"]; + + if ($id > 0) { + $stmt = $db->prepare("UPDATE celestial_object_status_profiles SET name = ?, slug = ? WHERE id = ?"); + $stmt->execute([$name, $slug, $id]); + } else { + $stmt = $db->prepare("INSERT INTO celestial_object_status_profiles (name, slug) VALUES (?, ?)"); + $stmt->execute([$name, $slug]); + } + header("Location: admin.php?tab=statuses&success=1"); + exit; } -if (isset($_GET['delete_modifier'])) { $db->prepare("DELETE FROM modifiers WHERE id = ?")->execute([(int)$_GET['delete_modifier']]); header("Location: admin.php?tab=modifiers&success=1"); exit; } -// Settlement Types +if (isset($_GET["delete_status_profile"])) { + $id = (int)$_GET["delete_status_profile"]; + // Check if it is used + $count = $db->query("SELECT COUNT(*) FROM celestial_object_status_rules WHERE profile_id = $id")->fetchColumn(); + $count2 = $db->query("SELECT COUNT(*) FROM celestial_object_types WHERE status_profile_id = $id")->fetchColumn(); + if ($count == 0 && $count2 == 0) { + $db->prepare("DELETE FROM celestial_object_status_profiles WHERE id = ?")->execute([$id]); + header("Location: admin.php?tab=statuses&success=1"); + } else { + header("Location: admin.php?tab=statuses&error=profile_in_use"); + } + exit; +} + +// Handle Status Rule CRUD +if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'upsert_status_rule') { + $id = (int)$_POST['id']; + $name = $_POST['name']; + $status_id = (int)$_POST['status_id']; + $profile_id = (int)$_POST['profile_id']; + $priority = (int)$_POST['priority']; + + $orbital_count_op = $_POST['orbital_count_op'] ?: null; + $orbital_count_val = $_POST['orbital_count_val'] !== '' ? (int)$_POST['orbital_count_val'] : null; + $terrestrial_count_op = $_POST['terrestrial_count_op'] ?: null; + $terrestrial_count_val = $_POST['terrestrial_count_val'] !== '' ? (int)$_POST['terrestrial_count_val'] : null; + $orbital_dominance = $_POST['orbital_dominance'] ?: null; + $terrestrial_dominance = $_POST['terrestrial_dominance'] ?: null; + $is_empty_case = isset($_POST['is_empty_case']) ? 1 : 0; + + if ($id > 0) { + $stmt = $db->prepare("UPDATE celestial_object_status_rules SET name = ?, status_id = ?, profile_id = ?, priority = ?, orbital_count_op = ?, orbital_count_val = ?, terrestrial_count_op = ?, terrestrial_count_val = ?, orbital_dominance = ?, terrestrial_dominance = ?, is_empty_case = ? WHERE id = ?"); + $stmt->execute([$name, $status_id, $profile_id, $priority, $orbital_count_op, $orbital_count_val, $terrestrial_count_op, $terrestrial_count_val, $orbital_dominance, $terrestrial_dominance, $is_empty_case, $id]); + } else { + $stmt = $db->prepare("INSERT INTO celestial_object_status_rules (name, status_id, profile_id, priority, orbital_count_op, orbital_count_val, terrestrial_count_op, terrestrial_count_val, orbital_dominance, terrestrial_dominance, is_empty_case) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); + $stmt->execute([$name, $status_id, $profile_id, $priority, $orbital_count_op, $orbital_count_val, $terrestrial_count_op, $terrestrial_count_val, $orbital_dominance, $terrestrial_dominance, $is_empty_case]); + } + header("Location: admin.php?tab=statuses&success=1"); + exit; +} + +if (isset($_GET['delete_status_rule'])) { + $id = (int)$_GET['delete_status_rule']; + $db->prepare("DELETE FROM celestial_object_status_rules WHERE id = ?")->execute([$id]); + header("Location: admin.php?tab=statuses&success=1"); + exit; +} + +// Handle Settlement Type CRUD if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'upsert_settlement_type') { - $id = (int)$_POST['id']; $name = $_POST['name']; $slug = $_POST['slug']; $description = $_POST['description']; - if ($id > 0) { $stmt = $db->prepare("UPDATE settlement_types SET name = ?, slug = ?, description = ? WHERE id = ?"); $stmt->execute([$name, $slug, $description, $id]); } - else { $stmt = $db->prepare("INSERT INTO settlement_types (name, slug, description) VALUES (?, ?, ?)"); $stmt->execute([$name, $slug, $description]); } - header("Location: admin.php?tab=settlement_types&success=1"); exit; -} -if (isset($_GET['delete_settlement_type'])) { $db->prepare("DELETE FROM settlement_types WHERE id = ?")->execute([(int)$_GET['delete_settlement_type']]); header("Location: admin.php?tab=settlement_types&success=1"); exit; } + $id = (int)$_POST['id']; + $name = $_POST['name']; + $slug = $_POST['slug']; + $description = $_POST['description']; -// Lootboxes -if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'upsert_lootbox') { - $id = (int)$_POST['id']; $name = $_POST['name']; $slug = $_POST['slug']; $description = $_POST['description']; - if ($id > 0) { $db->prepare("UPDATE lootboxes SET name = ?, slug = ?, description = ? WHERE id = ?")->execute([$name, $slug, $description, $id]); } - else { $db->prepare("INSERT INTO lootboxes (name, slug, description) VALUES (?, ?, ?)")->execute([$name, $slug, $description]); } - header("Location: admin.php?tab=lootboxes&success=1"); exit; + if ($id > 0) { + $stmt = $db->prepare("UPDATE settlement_types SET name = ?, slug = ?, description = ? WHERE id = ?"); + $stmt->execute([$name, $slug, $description, $id]); + } else { + $stmt = $db->prepare("INSERT INTO settlement_types (name, slug, description) VALUES (?, ?, ?)"); + $stmt->execute([$name, $slug, $description]); + } + header("Location: admin.php?tab=settlement_types&success=1"); + exit; +} + +if (isset($_GET['delete_settlement_type'])) { + $id = (int)$_GET['delete_settlement_type']; + $db->prepare("DELETE FROM settlement_types WHERE id = ?")->execute([$id]); + header("Location: admin.php?tab=settlement_types&success=1"); + exit; +} + +// Handle Modifiers CRUD +if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'upsert_modifier') { + $id = (int)$_POST['id']; + $name = $_POST['name']; + $slug = $_POST['slug']; + $type = $_POST['type']; + $description = $_POST['description']; + + if ($id > 0) { + $stmt = $db->prepare("UPDATE modifiers SET name = ?, slug = ?, type = ?, description = ? WHERE id = ?"); + $stmt->execute([$name, $slug, $type, $description, $id]); + } else { + $stmt = $db->prepare("INSERT INTO modifiers (name, slug, type, description) VALUES (?, ?, ?, ?)"); + $stmt->execute([$name, $slug, $type, $description]); + } + header("Location: admin.php?tab=modifiers&success=1"); + exit; +} + +if (isset($_GET['delete_modifier'])) { + $id = (int)$_GET['delete_modifier']; + $db->prepare("DELETE FROM modifiers WHERE id = ?")->execute([$id]); + header("Location: admin.php?tab=modifiers&success=1"); + exit; +} + +// Handle Faction CRUD +if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'upsert_faction') { + $id = (int)$_POST['id']; + $name = $_POST['name']; + $slug = $_POST['slug']; + $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]); + $image_url = $stmt_img->fetchColumn(); + } + if (isset($_FILES['image']) && $_FILES['image']['error'] === UPLOAD_ERR_OK) { + $ext = pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION); + $filename = "faction_" . time() . "." . $ext; + $target = "assets/images/factions/" . $filename; + if (move_uploaded_file($_FILES['image']['tmp_name'], $target)) { + $image_url = $target; + } + } + if ($id > 0) { + $stmt = $db->prepare("UPDATE factions SET name = ?, slug = ?, image_url = ?, fa_icon = ?, color = ? WHERE id = ?"); + $stmt->execute([$name, $slug, $image_url, $fa_icon, $color, $id]); + } 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; +} + +if (isset($_GET['delete_faction'])) { + $id = (int)$_GET['delete_faction']; + $db->prepare("DELETE FROM factions WHERE id = ?")->execute([$id]); + header("Location: admin.php?tab=factions&success=1"); + exit; +} + +// Handle Resource CRUD +if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'upsert_resource') { + $id = (int)$_POST['id']; + $name = $_POST['name']; + $slug = $_POST['slug']; + $icon = $_POST['icon']; + $description = $_POST['description']; + $show_in_header = isset($_POST["show_in_header"]) ? 1 : 0; + + $image_url = null; + if ($id > 0) { + $stmt_img = $db->prepare("SELECT image_url FROM game_resources WHERE id = ?"); + $stmt_img->execute([$id]); + $image_url = $stmt_img->fetchColumn(); + } + + if (isset($_FILES['image']) && $_FILES['image']['error'] === UPLOAD_ERR_OK) { + $ext = pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION); + $filename = "res_" . $slug . "_" . time() . "." . $ext; + if (!is_dir("assets/images/resources")) { + mkdir("assets/images/resources", 0775, true); + } + $target = "assets/images/resources/" . $filename; + if (move_uploaded_file($_FILES['image']['tmp_name'], $target)) { + $image_url = $target; + } + } + + if ($id > 0) { + $stmt = $db->prepare("UPDATE game_resources SET name = ?, slug = ?, icon = ?, description = ?, image_url = ?, show_in_header = ? WHERE id = ?"); + $stmt->execute([$name, $slug, $icon, $description, $image_url, $show_in_header, $id]); + } else { + $stmt = $db->prepare("INSERT INTO game_resources (name, slug, icon, description, image_url, show_in_header) VALUES (?, ?, ?, ?, ?, ?)"); + $stmt->execute([$name, $slug, $icon, $description, $image_url, $show_in_header]); + } + + header("Location: admin.php?tab=resources&success=1"); + exit; +} + +if (isset($_GET['delete_resource'])) { + $id = (int)$_GET['delete_resource']; + $db->prepare("DELETE FROM game_resources WHERE id = ?")->execute([$id]); + header("Location: admin.php?tab=resources&success=1"); + exit; +} + +// Handle Lootbox CRUD +if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'upsert_lootbox') { + $id = (int)$_POST['id']; + $name = $_POST['name']; + $slug = $_POST['slug']; + $description = $_POST['description']; + + if ($id > 0) { + $stmt = $db->prepare("UPDATE lootboxes SET name = ?, slug = ?, description = ? WHERE id = ?"); + $stmt->execute([$name, $slug, $description, $id]); + } else { + $stmt = $db->prepare("INSERT INTO lootboxes (name, slug, description) VALUES (?, ?, ?)"); + $stmt->execute([$name, $slug, $description]); + $id = $db->lastInsertId(); + } + + // Handle Rolls + $db->prepare("DELETE FROM lootbox_rolls WHERE lootbox_id = ?")->execute([$id]); + if (isset($_POST['rolls_count']) && is_array($_POST['rolls_count'])) { + $ins_roll = $db->prepare("INSERT INTO lootbox_rolls (lootbox_id, roll_count, probability) VALUES (?, ?, ?)"); + foreach ($_POST['rolls_count'] as $idx => $rc) { + $prob = (float)$_POST['rolls_prob'][$idx]; + if ($prob > 0) { + $ins_roll->execute([$id, (int)$rc, $prob]); + } + } + } + + // Handle Items + $db->prepare("DELETE FROM lootbox_items WHERE lootbox_id = ?")->execute([$id]); + if (isset($_POST['item_slug']) && is_array($_POST['item_slug'])) { + $ins_item = $db->prepare("INSERT INTO lootbox_items (lootbox_id, resource_slug, probability, quantity_min, quantity_max, is_guaranteed) VALUES (?, ?, ?, ?, ?, ?)"); + foreach ($_POST['item_slug'] as $idx => $islug) { + $is_guaranteed = isset($_POST['item_is_guaranteed'][$idx]) ? (int)$_POST['item_is_guaranteed'][$idx] : 0; + $iprob = $is_guaranteed ? 100.00 : (float)$_POST['item_prob'][$idx]; + + if ($is_guaranteed || $iprob > 0) { + $qmin = (int)$_POST['item_qmin'][$idx]; + $qmax = (int)$_POST['item_qmax'][$idx]; + $ins_item->execute([$id, $islug ?: null, $iprob, $qmin, $qmax, $is_guaranteed]); + } + } + } + + header("Location: admin.php?tab=lootboxes&success=1"); + exit; +} + +if (isset($_GET['delete_lootbox'])) { + $id = (int)$_GET['delete_lootbox']; + $db->prepare("DELETE FROM lootboxes WHERE id = ?")->execute([$id]); + header("Location: admin.php?tab=lootboxes&success=1"); + exit; +} + + +// Handle Project Log CRUD +if ($_SERVER["REQUEST_METHOD"] === "POST" && isset($_POST["action"]) && $_POST["action"] === "upsert_project_log") { + $id = (int)$_POST["id"]; + $version = $_POST["version"]; + $title = $_POST["title"]; + $content_log = $_POST["content"]; + + if ($id > 0) { + $stmt = $db->prepare("UPDATE project_logs SET version = ?, title = ?, content = ? WHERE id = ?"); + $stmt->execute([$version, $title, $content_log, $id]); + } else { + $stmt = $db->prepare("INSERT INTO project_logs (version, title, content) VALUES (?, ?, ?)"); + $stmt->execute([$version, $title, $content_log]); + } + header("Location: admin.php?tab=project_logs&success=1"); + exit; +} + +if (isset($_GET["delete_project_log"])) { + $id = (int)$_GET["delete_project_log"]; + $db->prepare("DELETE FROM project_logs WHERE id = ?")->execute([$id]); + header("Location: admin.php?tab=project_logs&success=1"); + exit; } -if (isset($_GET['delete_lootbox'])) { $db->prepare("DELETE FROM lootboxes WHERE id = ?")->execute([(int)$_GET['delete_lootbox']]); header("Location: admin.php?tab=lootboxes&success=1"); exit; } // --- DATA FETCHING --- -$users_list = []; $objects_list = []; $statuses_list = []; $status_profiles_list = []; $modifiers_list = []; $factions_list = []; $resources_list = []; $settlement_types_list = []; $lootboxes_list = []; $project_logs_list = []; +$users_list = []; +$objects_list = []; +$statuses_list = []; +$status_rules_list = []; +$status_profiles_list = []; +$settlement_types_list = []; +$modifiers_list = []; +$factions_list = []; +$resources_list = []; +$lootboxes_list = []; +$project_logs_list = []; -if ($tab === 'users') { $users_list = $db->query("SELECT id, username, email, role FROM users ORDER BY username ASC")->fetchAll(); } -elseif ($tab === 'objects') { - $objects_list = $db->query("SELECT o.*, p.name as profile_name FROM celestial_object_types o LEFT JOIN celestial_object_status_profiles p ON o.status_profile_id = p.id ORDER BY o.name ASC")->fetchAll(); - foreach ($objects_list as &$obj) { $stmt = $db->prepare("SELECT modifier_id FROM celestial_object_type_modifiers WHERE celestial_object_type_id = ?"); $stmt->execute([$obj['id']]); $obj['modifier_ids'] = $stmt->fetchAll(PDO::FETCH_COLUMN); } unset($obj); - $status_profiles_list = $db->query("SELECT id, name FROM celestial_object_status_profiles WHERE enabled = 1 ORDER BY name ASC")->fetchAll(); +if ($tab === 'users') { + $users_list = $db->query("SELECT id, username, email, role FROM users ORDER BY username ASC")->fetchAll(); +} elseif ($tab === 'objects') { + $objects_list = $db->query("SELECT * FROM celestial_object_types ORDER BY name ASC")->fetchAll(); + foreach ($objects_list as &$obj) { + $stmt = $db->prepare("SELECT modifier_id FROM celestial_object_type_modifiers WHERE celestial_object_type_id = ?"); + $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(); } -elseif ($tab === 'status_profiles') { - $status_profiles_list = $db->query("SELECT * FROM celestial_object_status_profiles ORDER BY priority DESC, name ASC")->fetchAll(); - $statuses_list = $db->query("SELECT id, name FROM celestial_object_statuses ORDER BY name ASC")->fetchAll(); - $object_types_list = $db->query("SELECT id, name, slug FROM celestial_object_types ORDER BY name ASC")->fetchAll(); + $status_profiles_list = $db->query("SELECT * FROM celestial_object_status_profiles ORDER BY name ASC")->fetchAll(); +} elseif ($tab === 'statuses') { + $statuses_list = $db->query("SELECT * FROM celestial_object_statuses ORDER BY name ASC")->fetchAll(); + $status_rules_list = $db->query("SELECT r.*, s.name as status_name, p.name as profile_name FROM celestial_object_status_rules r JOIN celestial_object_statuses s ON r.status_id = s.id LEFT JOIN celestial_object_status_profiles p ON r.profile_id = p.id ORDER BY r.profile_id, r.priority DESC, r.name ASC")->fetchAll(); + $status_profiles_list = $db->query("SELECT * FROM celestial_object_status_profiles ORDER BY name ASC")->fetchAll(); + $factions_list = $db->query("SELECT * FROM factions ORDER BY name ASC")->fetchAll(); +} elseif ($tab === 'settlement_types') { + $settlement_types_list = $db->query("SELECT * FROM settlement_types ORDER BY name ASC")->fetchAll(); +} elseif ($tab === 'modifiers') { + $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 === 'project_logs') { + $project_logs_list = $db->query("SELECT * FROM project_logs ORDER BY created_at DESC")->fetchAll(); +} elseif ($tab === 'lootboxes') { + $lootboxes_list = $db->query("SELECT * FROM lootboxes ORDER BY name ASC")->fetchAll(); + $resources_list = $db->query("SELECT name, slug FROM game_resources ORDER BY name ASC")->fetchAll(); + foreach ($lootboxes_list as &$lb) { + $stmt_r = $db->prepare("SELECT * FROM lootbox_rolls WHERE lootbox_id = ?"); + $stmt_r->execute([$lb['id']]); + $lb['rolls'] = $stmt_r->fetchAll(); + + $stmt_i = $db->prepare("SELECT * FROM lootbox_items WHERE lootbox_id = ?"); + $stmt_i->execute([$lb['id']]); + $lb['items'] = $stmt_i->fetchAll(); + } + unset($lb); } -elseif ($tab === 'resources') { $resources_list = $db->query("SELECT * FROM game_resources ORDER BY name ASC")->fetchAll(); } -elseif ($tab === 'factions') { $factions_list = $db->query("SELECT * FROM factions ORDER BY name ASC")->fetchAll(); } -elseif ($tab === 'modifiers') { $modifiers_list = $db->query("SELECT * FROM modifiers ORDER BY type, name ASC")->fetchAll(); } -elseif ($tab === 'settlement_types') { $settlement_types_list = $db->query("SELECT * FROM settlement_types ORDER BY name ASC")->fetchAll(); } -elseif ($tab === 'lootboxes') { $lootboxes_list = $db->query("SELECT * FROM lootboxes ORDER BY name ASC")->fetchAll(); } -elseif ($tab === 'project_logs') { $project_logs_list = $db->query("SELECT * FROM project_logs ORDER BY created_at DESC")->fetchAll(); } ?> - Console Admin - Nexus + + Console Admin - Nexus -

ADMIN

+
+
+

CONSOLE ADMIN

+ +
+
+ +
+
+
+ +
Opération effectuée avec succès.
+ +
- Utilisateurs - Objets Célestes - Profils de Statut - Statuts / États - Factions - Ressources - Bonus/Malus - Etablissements - Lootboxes - Journal + Utilisateurs + Objets Célestes + Bonus & Malus + Statuts / États + Types d'Établissements + Factions + Ressources + "> Journal de Bord + Lootboxes
- -
UserRoleNouveau Role
+

Gestion des Rôles

+ + + + + + + + + + + + + + +
UtilisateurEmailRôle ActuelNouveau Rôle
+ + + + +
+ + + + +
+
-

Ajouter / Modifier un Type

-
-
-
-
-
-
- -
-
- -
VisuelNomSlugProfilActions
X
+

Objets Célestes

+
+

Ajouter / Modifier un Objet

+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ +
+
+ +
+
+ + +
+
+ + +
+
+
+
+ + +
+
+ +
+
+ +
+ + + +
+
+
- -

Profil de Statut Automatique

- -
-
-
+
+ + +
+ +
- -
PrioNomSlugActions
X
- -

Statuts / États

-
-
-
- -
-
- -
CouleurNomSlugActions
X
- - -

Factions

-
-
-
-
- -
-
- -
VisuelNomSlugActions
X
- - -

Ressources

-
-
-
-
- -
-
- -
IcôneNomSlugHeaderActions
X
+ + + + + + + + + + + + + + +
VisuelNomContrôlesRèglesBonus/MalusActions
+ + + + + +
+
+ + ORBITAL + + + TERRESTRE + +
+
+ Fixe"; + foreach($status_profiles_list as $p) if($p['id'] == $o['status_profile_id']) $p_name = htmlspecialchars($p['name']); + echo $p_name; + ?> + + prepare("SELECT m.name, m.type FROM modifiers m JOIN celestial_object_type_modifiers cotm ON m.id = cotm.modifier_id WHERE cotm.celestial_object_type_id = ?"); + $stmt->execute([$o['id']]); + $m_list = $stmt->fetchAll(); + foreach ($m_list as $ml): ?> + + + + + + + Suppr +
-

Bonus / Malus

-
-
-
- +

Gestion des Bonus & Malus

+
+

Ajouter / Modifier un Modificateur

+ + + +
+
+ + +
+
+ + +
+
+ + +
+
+
+ + +
+ +
- -
TypeNomSlugActions
X
+ + + + + + + + + + + + + + +
TypeNomDescriptionSlugActions
+ + + + + + Suppr +
+ + +

Statuts / États

+
+ +
+
+

Ajouter / Modifier un Statut

+
+ + +
+
+ + +
+
+ + +
+
+ +
+ + +
+
+
+ + +
+
+
+ + +
+ + +
+
+ + + + + + + + + + + + + + +
CouleurNomSlugActions
+ + Suppr +
+
+ + +
+ +
+

PROFILS / GROUPES DE RÈGLES

+
+ + +
+ + +
+
+ + +
+ + +
+
+ +
+ () +
+ + +
+
+ +
+
+
+

CONFIGURATION DES RÈGLES

+
+ + + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ +
+
+
NOMBRE DE FACTIONS :
+
+ +
+ + +
+
+
+ +
+ + +
+
+
+ +
+
FACTION DOMINANTE :
+
+ + +
+
+ + +
+
+ +
+ + +
+
+ + + +
+
+ + + + + + + + + + + + + + +
ProfilePrioritéNom / ConditionStatutActions
+
+ + Toujours vrai'; + ?> + +
+ + Suppr +
+
+
-

Types d'Établissements

-
-
-
- +

Types d'Établissements

+
+

Ajouter / Modifier un Type d'Établissement

+ + + +
+
+ + +
+
+ + +
+
+
+ + +
+ +
- -
NomSlugActions
X
- -

Lootboxes

-
-
-
- + + + + + + + + + + + + +
NomSlugDescriptionActions
+ + Suppr +
+ + +

Gestion des Factions

+
+

Ajouter / Modifier une Faction

+ + + +
+
+ + +
+
+ + +
+
+ +
+ + +
+
+
+ + +
+
+ + +
+
+ +
+ +
+ + + +
+
+ + +
- -
NomSlugActions
X
+ + + + + + + + + + + + + + + +
CouleurVisuelNomSlugAlliancesActions
+ + + + + + + + + + ' . htmlspecialchars($fl['name']) . ''; + break; + } + } + } + echo !empty($allies) ? implode(', ', $allies) : 'Aucune'; + ?> + + + + + Suppr + +
+ + +

Gestion des Ressources

+
+

Ajouter / Modifier une Ressource

+
+ + +
+
+ + +
+
+ + +
+
+
+
+ + +
+
+ + +
+
+
+ + +
+
+ +
+ + +
+
+ + + + + + + + + + + + + + +
VisuelNomSlugHeader?Actions
+ + + + + + + + + + + + + + + + Suppr +
- -
DateVersionTitre
+

Journal de Bord (Versions)

+
+

Ajouter / Modifier une Version

+
+ + +
+
+ + +
+
+ + +
+
+
+ + +
+ + +
+
+ + + + + + + + + + + + +
VersionTitreDateActions
v + + SUPPR +
+ + + +

Système de Lootboxes

+
+

Créer / Modifier une Lootbox

+
+ + +
+
+ + +
+
+ + +
+
+
+ + +
+ +
+ +
+ + +
+
Nb Total
+
Chance (%)
+
+
+ +
+ +
+ +
+ + +
+ + +
+
Type
+
Ressource / Objet
+
Chance (%)
+
Qté Min
+
Qté Max
+
+
+ +
+ +
+ +
+
+ +
+ + +
+
+
+ + + + + + + + + + + + + + + +
NomSlugObjets Directs (100%)Nb Total (%)Pool Aléatoire (%)Actions
+ + $i['is_guaranteed']); + foreach ($directs as $d): ?> +
+ + (Qté: -) +
+ Aucun"; ?> +
+
+ + +
+ objet(s): + % +
+ +
+
+ + !$i['is_guaranteed']); + foreach ($pool as $i): ?> +
+ (Rien)'; ?>: + % + (Qté: -) +
+ +
+
+ + Suppr +
+ - + + diff --git a/gm_console.php b/gm_console.php index 01a52dd..f7a37f9 100644 --- a/gm_console.php +++ b/gm_console.php @@ -1,5 +1,6 @@ query("SELECT * FROM celestial_object_types ORDER BY name ASC")->fetchAll(PDO::FETCH_ASSOC); $statuses_db = $db->query("SELECT * FROM celestial_object_statuses ORDER BY id ASC")->fetchAll(PDO::FETCH_ASSOC); -$status_profiles_db = $db->query("SELECT * FROM celestial_object_status_profiles WHERE enabled = 1 ORDER BY priority DESC, name ASC")->fetchAll(PDO::FETCH_ASSOC); $settlement_types_db = $db->query("SELECT * FROM settlement_types ORDER BY name ASC")->fetchAll(PDO::FETCH_ASSOC); $factions_db = $db->query("SELECT * FROM factions ORDER BY name ASC")->fetchAll(PDO::FETCH_ASSOC); @@ -31,36 +31,6 @@ $object_types_map = []; foreach($object_types_db as $ot) $object_types_map[$ot[' $statuses_map = []; foreach($statuses_db as $s) $statuses_map[$s['slug']] = $s; $factions_map = []; foreach($factions_db as $f) $factions_map[$f['id']] = $f; -function resolvePlanetStatus($planet, $profiles, $statuses_db, $object_types_map) { - // 1. Determine which profile to use (Individual first, then Type default) - $profile_id = $planet['status_profile_id'] ?: ($object_types_map[$planet['type']]['status_profile_id'] ?? null); - - if (!empty($profile_id)) { - foreach ($profiles as $prof) { - if ($prof['id'] == $profile_id) { - $config = json_decode($prof['config'], true); - if (isset($config['rules']) && is_array($config['rules'])) { - foreach ($config['rules'] as $rule) { - $match = false; $cond = $rule['condition_type']; - if ($cond === 'fixed') $match = true; - elseif ($cond === 'orbital_control') { $val = (float)($planet['orbital_control'] ?? 0); if ($val >= ($rule['min_value'] ?? 0) && $val <= ($rule['max_value'] ?? 100)) $match = true; } - elseif ($cond === 'terrestrial_control') { $val = (float)($planet['terrestrial_control'] ?? 0); if ($val >= ($rule['min_value'] ?? 0) && $val <= ($rule['max_value'] ?? 100)) $match = true; } - elseif ($cond === 'uncontrolled') { if ((float)($planet['orbital_control'] ?? 0) == 0 && (float)($planet['terrestrial_control'] ?? 0) == 0) $match = true; } - - if ($match) { - foreach ($statuses_db as $s) { - if ($s['id'] == $rule['status_id']) return $s['slug']; - } - } - } - } - break; - } - } - } - return $planet['status']; -} - // Handle Planet/Slot Update if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'update_slot') { $slot_id = (int)($_POST['slot_id'] ?? 0); @@ -70,212 +40,706 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST[' $name = $_POST['name'] ?? 'Inconnu'; $type = $_POST['type'] ?? 'empty'; $manual_status = $_POST['manual_status'] ?? ''; - $status_profile_id = !empty($_POST['status_profile_id']) ? (int)$_POST['status_profile_id'] : null; + + // Orbital control is now detailed by faction $orbital_controls = $_POST['orbital_controls'] ?? []; - $dominant_orbital_val = 0; foreach($orbital_controls as $fid => $val) { if ((int)$val > $dominant_orbital_val && (int)$fid != 1) $dominant_orbital_val = (int)$val; } - $status = 'sta_inhabited'; $faction_id = null; $total_non_aucun = 0; $active_factions = []; $num_cities = 0; $avg_terrestrial_control = 0; + $dominant_orbital_val = 0; + $dominant_orbital_faction = null; + foreach($orbital_controls as $fid => $val) { + if ((int)$val > $dominant_orbital_val && (int)$fid != 1) { // Not "Aucune" + $dominant_orbital_val = (int)$val; + $dominant_orbital_faction = (int)$fid; + } + } + + // Derive Status and Faction from Settlements + $status = 'sta_inhabited'; + $faction_id = null; + $total_non_aucun = 0; + $active_factions = []; + $num_cities = 0; + $avg_terrestrial_control = 0; + if (isset($_POST['cities']) && is_array($_POST['cities'])) { foreach ($_POST['cities'] as $city_data) { - if (empty($city_data['name'])) continue; $num_cities++; - if (isset($city_data['controls']) && is_array($city_data['controls'])) { foreach ($city_data['controls'] as $f_id => $lvl) { if ((int)$lvl > 0 && $f_id != 1) { $total_non_aucun += (int)$lvl; $active_factions[$f_id] = ($active_factions[$f_id] ?? 0) + (int)$lvl; $avg_terrestrial_control += (int)$lvl; } } } - } - } - if ($num_cities > 0) $avg_terrestrial_control = round($avg_terrestrial_control / $num_cities); - if ($num_cities > 0 && $total_non_aucun > 0) { - arsort($active_factions); $faction_id = (int)key($active_factions); - if (count($active_factions) > 1) { $status = 'sta_hostile'; } else { $status = ($total_non_aucun >= ($num_cities * 100)) ? 'sta_controlled' : 'sta_contested'; } - } else if ($type !== 'empty') { $status = 'sta_inhabited'; } - if (!empty($manual_status)) $status = $manual_status; - if ($type === 'empty') { if ($slot_id > 0) { $db->prepare("DELETE FROM cities WHERE planet_id = ?")->execute([$slot_id]); $db->prepare("DELETE FROM planet_faction_control WHERE planet_id = ?")->execute([$slot_id]); $db->prepare("DELETE FROM planets WHERE id = ?")->execute([$slot_id]); } } - else { - if ($slot_id > 0) { $db->prepare("UPDATE planets SET name = ?, type = ?, status = ?, faction_id = ?, orbital_control = ?, terrestrial_control = ?, status_profile_id = ? WHERE id = ?")->execute([$name, $type, $status, $faction_id, $dominant_orbital_val, $avg_terrestrial_control, $status_profile_id, $slot_id]); $planet_id = $slot_id; } - else { $db->prepare("INSERT INTO planets (galaxy_id, sector_id, slot, name, type, status, faction_id, orbital_control, terrestrial_control, status_profile_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")->execute([$galaxy_id, $sector_id, $slot_num, $name, $type, $status, $faction_id, $dominant_orbital_val, $avg_terrestrial_control, $status_profile_id]); $planet_id = $db->lastInsertId(); } - $db->prepare("DELETE FROM planet_faction_control WHERE planet_id = ?")->execute([$planet_id]); - foreach($orbital_controls as $fid => $lvl) { if ((int)$lvl > 0) $db->prepare("INSERT INTO planet_faction_control (planet_id, faction_id, control_level) VALUES (?, ?, ?)")->execute([$planet_id, (int)$fid, (int)$lvl]); } - if (isset($_POST['cities']) && is_array($_POST['cities'])) { - $sent_city_ids = []; - foreach ($_POST['cities'] as $city_data) { - if (empty($city_data['name'])) continue; $c_id = (int)($city_data['id'] ?? 0); - if ($c_id > 0) { $db->prepare("UPDATE cities SET name = ?, settlement_type_id = ? WHERE id = ?")->execute([$city_data['name'], !empty($city_data['type_id'])?(int)$city_data['type_id']:null, $c_id]); $city_id = $c_id; } - else { $db->prepare("INSERT INTO cities (planet_id, name, settlement_type_id) VALUES (?, ?, ?)")->execute([$planet_id, $city_data['name'], !empty($city_data['type_id'])?(int)$city_data['type_id']:null]); $city_id = $db->lastInsertId(); } - $sent_city_ids[] = $city_id; $db->prepare("DELETE FROM city_faction_control WHERE city_id = ?")->execute([$city_id]); - if (isset($city_data['controls']) && is_array($city_data['controls'])) { foreach ($city_data['controls'] as $f_id => $l) { if ((int)$l > 0) $db->prepare("INSERT INTO city_faction_control (city_id, faction_id, control_level) VALUES (?, ?, ?)")->execute([$city_id, (int)$f_id, (int)$l]); } } + if (empty($city_data['name'])) continue; + $num_cities++; + if (isset($city_data['controls']) && is_array($city_data['controls'])) { + foreach ($city_data['controls'] as $f_id => $lvl) { + $lvl = (int)$lvl; + if ($lvl > 0 && $f_id != 1) { // 1 is "Aucune" + $total_non_aucun += $lvl; + $active_factions[$f_id] = ($active_factions[$f_id] ?? 0) + $lvl; + $avg_terrestrial_control += $lvl; + } + } } - if ($planet_id > 0) { if (empty($sent_city_ids)) { $db->prepare("DELETE FROM cities WHERE planet_id = ?")->execute([$planet_id]); } else { $placeholders = implode(',', array_fill(0, count($sent_city_ids), '?')); $db->prepare("DELETE FROM cities WHERE planet_id = ? AND id NOT IN ($placeholders)")->execute(array_merge([$planet_id], $sent_city_ids)); } } } } - header("Location: gm_console.php?view=sector&galaxy_id=$galaxy_id§or_id=$sector_id&success=1"); exit; + + if ($num_cities > 0) { + $avg_terrestrial_control = round($avg_terrestrial_control / $num_cities); + } + + if ($num_cities > 0 && $total_non_aucun > 0) { + arsort($active_factions); + $faction_id = (int)key($active_factions); + + if (count($active_factions) > 1) { + $status = 'sta_hostile'; + } else { + if ($total_non_aucun >= ($num_cities * 100)) { + $status = 'sta_controlled'; + } else { + $status = 'sta_contested'; + } + } + } else if ($type !== 'empty') { + $status = 'sta_inhabited'; + $faction_id = null; + } + + // Manual status override + if (!empty($manual_status)) { + $status = $manual_status; + } + + if ($type === 'empty') { + if ($slot_id > 0) { + $db->prepare("DELETE FROM cities WHERE planet_id = ?")->execute([$slot_id]); + $db->prepare("DELETE FROM planet_faction_control WHERE planet_id = ?")->execute([$slot_id]); + $db->prepare("DELETE FROM planets WHERE id = ?")->execute([$slot_id]); + } + } else { + if ($slot_id > 0) { + $stmt = $db->prepare("UPDATE planets SET name = ?, type = ?, status = ?, faction_id = ?, orbital_control = ?, terrestrial_control = ? WHERE id = ?"); + $stmt->execute([$name, $type, $status, $faction_id, $dominant_orbital_val, $avg_terrestrial_control, $slot_id]); + $planet_id = $slot_id; + } else { + $stmt = $db->prepare("INSERT INTO planets (galaxy_id, sector_id, slot, name, type, status, faction_id, orbital_control, terrestrial_control) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"); + $stmt->execute([$galaxy_id, $sector_id, $slot_num, $name, $type, $status, $faction_id, $dominant_orbital_val, $avg_terrestrial_control]); + $planet_id = $db->lastInsertId(); + } + + // Handle Orbital Faction Control + $db->prepare("DELETE FROM planet_faction_control WHERE planet_id = ?")->execute([$planet_id]); + foreach($orbital_controls as $fid => $lvl) { + if ((int)$lvl > 0) { + $db->prepare("INSERT INTO planet_faction_control (planet_id, faction_id, control_level) VALUES (?, ?, ?)")->execute([$planet_id, (int)$fid, (int)$lvl]); + } + } + + // Handle Multiple Settlements + $sent_city_ids = []; + if (isset($_POST['cities']) && is_array($_POST['cities'])) { + foreach ($_POST['cities'] as $city_data) { + if (empty($city_data['name'])) continue; + + $c_id = (int)($city_data['id'] ?? 0); + $c_name = $city_data['name']; + $c_type_id = !empty($city_data['type_id']) ? (int)$city_data['type_id'] : null; + + if ($c_id > 0) { + $stmt = $db->prepare("UPDATE cities SET name = ?, settlement_type_id = ? WHERE id = ? AND planet_id = ?"); + $stmt->execute([$c_name, $c_type_id, $c_id, $planet_id]); + $city_id = $c_id; + } else { + $stmt = $db->prepare("INSERT INTO cities (planet_id, name, settlement_type_id) VALUES (?, ?, ?)"); + $stmt->execute([$planet_id, $c_name, $c_type_id]); + $city_id = $db->lastInsertId(); + } + $sent_city_ids[] = $city_id; + + // Handle Faction Control + $db->prepare("DELETE FROM city_faction_control WHERE city_id = ?")->execute([$city_id]); + if (isset($city_data['controls']) && is_array($city_data['controls'])) { + foreach ($city_data['controls'] as $fac_id => $control_lvl) { + $control_lvl = (int)$control_lvl; + if ($control_lvl > 0) { + $stmt = $db->prepare("INSERT INTO city_faction_control (city_id, faction_id, control_level) VALUES (?, ?, ?)"); + $stmt->execute([$city_id, (int)$fac_id, $control_lvl]); + } + } + } + } + } + if ($planet_id > 0) { + if (empty($sent_city_ids)) { + $db->prepare("DELETE FROM cities WHERE planet_id = ?")->execute([$planet_id]); + } else { + $placeholders = implode(',', array_fill(0, count($sent_city_ids), '?')); + $stmt = $db->prepare("DELETE FROM cities WHERE planet_id = ? AND id NOT IN ($placeholders)"); + $params = array_merge([$planet_id], $sent_city_ids); + $stmt->execute($params); + } + } + } + header("Location: gm_console.php?view=sector&galaxy_id=$galaxy_id§or_id=$sector_id&success=1"); + exit; } -$view = isset($_GET['view']) ? $_GET['view'] : 'galaxy'; $galaxy_id = (int)($_GET['galaxy_id'] ?? 1); $sector_id = (int)($_GET['sector_id'] ?? 1); $grid_size = 36; -if ($view === 'sector') { - $stmt = $db->prepare("SELECT * FROM planets WHERE galaxy_id = ? AND sector_id = ? AND slot BETWEEN 1 AND ?"); $stmt->execute([$galaxy_id, $sector_id, $grid_size]); - $grid = array_fill(1, $grid_size, null); $planet_ids = []; - foreach ($stmt->fetchAll() as $obj) { $obj['status'] = resolvePlanetStatus($obj, $status_profiles_db, $statuses_db, $object_types_map); $grid[$obj['slot']] = $obj; $planet_ids[] = $obj['id']; $grid[$obj['slot']]['cities'] = []; $grid[$obj['slot']]['orbital_controls'] = []; } - if (!empty($planet_ids)) { - $p_list = implode(',', array_fill(0, count($planet_ids), '?')); - $stmt = $db->prepare("SELECT * FROM planet_faction_control WHERE planet_id IN ($p_list)"); $stmt->execute($planet_ids); - foreach ($stmt->fetchAll() as $ocr) { foreach ($grid as &$s) { if ($s && $s['id'] == $ocr['planet_id']) $s['orbital_controls'][$ocr['faction_id']] = $ocr['control_level']; } } - unset($s); - $stmt = $db->prepare("SELECT * FROM cities WHERE planet_id IN ($p_list)"); $stmt->execute($planet_ids); - $cities = $stmt->fetchAll(); $c_ids = array_column($cities, 'id'); - if (!empty($c_ids)) { - $c_list = implode(',', array_fill(0, count($c_ids), '?')); - $c_stmt = $db->prepare("SELECT * FROM city_faction_control WHERE city_id IN ($c_list)"); $c_stmt->execute($c_ids); - $c_ctrls = []; foreach ($c_stmt->fetchAll() as $cr) $c_ctrls[$cr['city_id']][$cr['faction_id']] = $cr['control_level']; - foreach ($cities as $c) { $c['controls'] = $c_ctrls[$c['id']] ?? []; foreach ($grid as &$s) { if ($s && $s['id'] == $c['planet_id']) $s['cities'][] = $c; } } - unset($s); - } - } - $stmt = $db->prepare("SELECT name, status FROM sectors WHERE id = ?"); $stmt->execute([$sector_id]); $sector_info = $stmt->fetch(); -} else { - $stmt = $db->prepare("SELECT id, sector_id, slot, status, type, orbital_control, terrestrial_control, status_profile_id FROM planets WHERE galaxy_id = ? ORDER BY sector_id, slot ASC"); $stmt->execute([$galaxy_id]); - $sector_data = []; foreach ($stmt->fetchAll() as $p) { $p['status'] = resolvePlanetStatus($p, $status_profiles_db, $statuses_db, $object_types_map); $sector_data[$p['sector_id']][$p['slot']] = ['status' => $p['status'], 'type' => $p['type']]; } +// Handle Sector Update +if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'update_sector') { + $sector_id = (int)$_POST['sector_id']; + $galaxy_id = (int)$_POST['galaxy_id']; + $s_name = $_POST['sector_name'] ?? "Secteur $sector_id"; + $s_status = $_POST['sector_status'] ?? 'unexplored'; + + $stmt = $db->prepare("INSERT INTO sectors (id, galaxy_id, name, status) VALUES (?, ?, ?, ?) ON DUPLICATE KEY UPDATE name = ?, status = ?"); + $stmt->execute([$sector_id, $galaxy_id, $s_name, $s_status, $s_name, $s_status]); + + header("Location: gm_console.php?view=sector&galaxy_id=$galaxy_id§or_id=$sector_id&success=1"); + exit; +} + +$view = isset($_GET['view']) ? $_GET['view'] : 'galaxy'; +$galaxy_id = isset($_GET['galaxy_id']) ? (int)$_GET['galaxy_id'] : 1; +$sector_id = isset($_GET['sector_id']) ? (int)$_GET['sector_id'] : 1; +$grid_size = 36; + +if ($view === 'sector') { + $stmt = $db->prepare("SELECT * FROM planets WHERE galaxy_id = ? AND sector_id = ? AND slot BETWEEN 1 AND ?"); + $stmt->execute([$galaxy_id, $sector_id, $grid_size]); + $objects_raw = $stmt->fetchAll(); + + $grid = array_fill(1, $grid_size, null); + $planet_ids = []; + foreach ($objects_raw as $obj) { + $grid[$obj['slot']] = $obj; + $planet_ids[] = $obj['id']; + $grid[$obj['slot']]['cities'] = []; + $grid[$obj['slot']]['orbital_controls'] = []; + $grid[$obj['slot']]['terrestrial_controls'] = []; + } + + if (!empty($planet_ids)) { + // Fetch Orbital Controls + $placeholders = implode(',', array_fill(0, count($planet_ids), '?')); + $stmt = $db->prepare("SELECT * FROM planet_faction_control WHERE planet_id IN ($placeholders)"); + $stmt->execute($planet_ids); + $orb_controls_raw = $stmt->fetchAll(PDO::FETCH_ASSOC); + foreach ($orb_controls_raw as $ocr) { + foreach ($grid as &$slot_data) { + if ($slot_data && $slot_data['id'] == $ocr['planet_id']) { + $slot_data['orbital_controls'][$ocr['faction_id']] = $ocr['control_level']; + } + } + } + + // 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); + + $city_ids = array_column($all_cities, 'id'); + $city_controls = []; + if (!empty($city_ids)) { + $c_placeholders = implode(',', array_fill(0, count($city_ids), '?')); + $c_stmt = $db->prepare("SELECT * FROM city_faction_control WHERE city_id IN ($c_placeholders)"); + $c_stmt->execute($city_ids); + $controls_raw = $c_stmt->fetchAll(PDO::FETCH_ASSOC); + foreach ($controls_raw as $cr) { + $city_controls[$cr['city_id']][$cr['faction_id']] = $cr['control_level']; + } + } + + $planet_terrestrial_agg = []; + foreach ($all_cities as $city) { + $pid = $city['planet_id']; + $city['controls'] = $city_controls[$city['id']] ?? []; + foreach ($city['controls'] as $fid => $lvl) { + $planet_terrestrial_agg[$pid][$fid] = ($planet_terrestrial_agg[$pid][$fid] ?? 0) + $lvl; + } + foreach ($grid as &$slot_data) { + if ($slot_data && $slot_data['id'] == $city['planet_id']) { + $slot_data['cities'][] = $city; + } + } + } + + // Aggregate terrestrial controls + foreach ($grid as &$slot_data) { + if ($slot_data && !empty($slot_data['cities'])) { + $num_cities = count($slot_data['cities']); + if (isset($planet_terrestrial_agg[$slot_data['id']])) { + foreach ($planet_terrestrial_agg[$slot_data['id']] as $fid => $total) { + $slot_data['terrestrial_controls'][$fid] = round($total / $num_cities); + } + } + } + } + unset($slot_data); + + // Apply dynamic status + foreach ($grid as &$slot_data) { + if ($slot_data) { + $slot_data['status'] = calculateCelestialStatus($slot_data, $db, $statuses_map); + } + } + unset($slot_data); + } + $stmt = $db->prepare("SELECT name, status FROM sectors WHERE id = ?"); + $stmt->execute([$sector_id]); + $sector_info = $stmt->fetch(); +} else { // Galaxy view + $stmt = $db->prepare("SELECT * FROM planets WHERE galaxy_id = ? ORDER BY sector_id, slot ASC"); + $stmt->execute([$galaxy_id]); + $all_planets = $stmt->fetchAll(PDO::FETCH_ASSOC); + + $planet_ids = array_column($all_planets, 'id'); + $orb_controls = []; + $city_counts = []; + + if (!empty($planet_ids)) { + $placeholders = implode(',', array_fill(0, count($planet_ids), '?')); + $o_stmt = $db->prepare("SELECT * FROM planet_faction_control WHERE planet_id IN ($placeholders)"); + $o_stmt->execute($planet_ids); + while($r = $o_stmt->fetch()) $orb_controls[$r['planet_id']][$r['faction_id']] = $r['control_level']; + + $c_stmt = $db->prepare("SELECT planet_id, COUNT(*) as cnt FROM cities WHERE planet_id IN ($placeholders) GROUP BY planet_id"); + $c_stmt->execute($planet_ids); + while($r = $c_stmt->fetch()) $city_counts[$r['planet_id']] = $r['cnt']; + } + + $sector_data = []; + $active_sectors = []; + foreach ($all_planets as $p) { + $p['orbital_controls'] = $orb_controls[$p['id']] ?? []; + $p['terrestrial_controls'] = []; + $p['cities'] = isset($city_counts[$p['id']]) ? array_fill(0, $city_counts[$p['id']], []) : []; + + $dynamic_status = calculateCelestialStatus($p, $db, $statuses_map); + $sector_data[$p['sector_id']][$p['slot']] = ['status' => $dynamic_status, 'type' => $p['type']]; + if (!in_array($p['sector_id'], $active_sectors)) { $active_sectors[] = (int)$p['sector_id']; } + } +} + +function getStatusColor($status, $type, $statuses_map, $object_types_map) { + if ($type === 'empty') return 'rgba(255,255,255,0.05)'; + $c = $statuses_map[$status]['color'] ?? 'rgba(255,255,255,0.05)'; return str_replace(';blink', '', $c); } -function getStatusColor($status, $type, $statuses_map) { if ($type === 'empty') return 'rgba(255,255,255,0.05)'; $c = $statuses_map[$status]['color'] ?? 'rgba(255,255,255,0.05)'; return str_replace(';blink', '', $c); } ?> - Console MJ - Nexus + + Console MJ - Nexus
- +
+

CONSOLE MJ

+ +
+
Connecté en tant que MJ: @
-
Succès !
+ +
+ Modifications enregistrées avec succès ! +
+ +

Navigateur de Galaxie

+ +
+
+ Retour +

Secteur :

+
+ +
+
-
- - -
- -
- - - -
- +
+ + +
+ +
+ + + + + +
+ -
- -
+ +
+ +
+ +
+ +
+ + + + + + +
+ + +
+ +
-
+ +