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, Profiles, 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); $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); $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; 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); $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'] ?? ''; $status_profile_id = !empty($_POST['status_profile_id']) ? (int)$_POST['status_profile_id'] : null; $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; 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 ($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; } $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']]; } } 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

Succès !
SECTEUR