prepare("SELECT role FROM users WHERE id = ?"); $user_stmt->execute([$user_id]); $current_user = $user_stmt->fetch(); if (!$current_user || ($current_user['role'] !== 'admin' && $current_user['role'] !== 'gm')) { die("Accès refusé. Vous devez être un Maître du Jeu (MJ) pour accéder à cette console."); } $is_admin = ($current_user['role'] === 'admin'); // Fetch Dynamic Types, Statuses, Settlement Types, and Factions $object_types_db = $db->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); $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); $object_types_map = []; foreach($object_types_db as $ot) $object_types_map[$ot['slug']] = $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; // Handle Planet/Slot Update if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'update_slot') { $slot_id = (int)($_POST['slot_id'] ?? 0); $galaxy_id = (int)($_POST['galaxy_id'] ?? 1); $sector_id = (int)($_POST['sector_id'] ?? 1); $slot_num = (int)($_POST['slot_num'] ?? 0); $name = $_POST['name'] ?? 'Inconnu'; $type = $_POST['type'] ?? 'empty'; // Orbital control is now detailed by faction $orbital_controls = $_POST['orbital_controls'] ?? []; $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_empty'; $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) { $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 ($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_empty'; $faction_id = null; } 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; } // 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'] = []; } 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 $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']; } } foreach ($all_cities as $city) { $city['controls'] = $city_controls[$city['id']] ?? []; foreach ($grid as &$slot_data) { if ($slot_data && $slot_data['id'] == $city['planet_id']) { $slot_data['cities'][] = $city; } } } } $stmt = $db->prepare("SELECT name, status FROM sectors WHERE id = ?"); $stmt->execute([$sector_id]); $sector_info = $stmt->fetch(); } else { $stmt = $db->prepare("SELECT sector_id, slot, status, type FROM planets WHERE galaxy_id = ? ORDER BY sector_id, slot ASC"); $stmt->execute([$galaxy_id]); $all_planets = $stmt->fetchAll(); $sector_data = []; $active_sectors = []; foreach ($all_planets as $p) { $sector_data[$p['sector_id']][$p['slot']] = ['status' => $p['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)'; return $statuses_map[$status]['color'] ?? 'rgba(255,255,255,0.05)'; } ?> Console MJ - Nexus

CONSOLE MJ

Connecté en tant que MJ: @

Navigateur de Galaxie

SECTEUR
Retour

Secteur :