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) { $s['is_blinking'] = (strpos($s['color'], ';blink') !== false); $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'; $manual_status = $_POST['manual_status'] ?? ''; // 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; } } // Status is now 'sta_auto' by default, overridden only by manual MJ selection // Dynamic status is calculated on the fly in the helper $status = 'sta_auto'; $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); } // Manual status override if specified by MJ 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; } // 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 = []; $terr_controls = []; $city_counts = []; if (!empty($planet_ids)) { $placeholders = implode(',', array_fill(0, count($planet_ids), '?')); // Orbital $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']; // Terrestrial (Aggregated per planet) $t_stmt = $db->prepare("SELECT c.planet_id, cfc.faction_id, SUM(cfc.control_level) as total_lvl FROM city_faction_control cfc JOIN cities c ON cfc.city_id = c.id WHERE c.planet_id IN ($placeholders) GROUP BY c.planet_id, cfc.faction_id"); $t_stmt->execute($planet_ids); while($r = $t_stmt->fetch()) { $terr_controls[$r['planet_id']][$r['faction_id']] = $r['total_lvl']; } // City counts $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['cities'] = isset($city_counts[$p['id']]) ? array_fill(0, $city_counts[$p['id']], []) : []; $p['terrestrial_controls'] = []; if (!empty($p['cities'])) { $num_cities = count($p['cities']); if (isset($terr_controls[$p['id']])) { foreach ($terr_controls[$p['id']] as $fid => $total_lvl) { $p['terrestrial_controls'][$fid] = round($total_lvl / $num_cities); } } } $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); } ?> Console MJ - Nexus

CONSOLE MJ

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

Navigateur de Galaxie

" style="background-color: ;">
SECTEUR
Retour

Secteur :

" style="background: ;">