diff --git a/assets/pasted-20260226-155248-f13efd72.png b/assets/pasted-20260226-155248-f13efd72.png new file mode 100644 index 0000000..35d636d Binary files /dev/null and b/assets/pasted-20260226-155248-f13efd72.png differ diff --git a/index.php b/index.php index bc2153b..187e063 100644 --- a/index.php +++ b/index.php @@ -139,11 +139,10 @@ if ($view === 'sector') { $stmt->execute([$galaxy_id]); $all_planets = $stmt->fetchAll(PDO::FETCH_ASSOC); - // We need controls to calculate status even in galaxy view - // For performance in a real app, we'd cache this or use a simplified query $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), '?')); @@ -153,19 +152,37 @@ if ($view === 'sector') { $o_stmt->execute($planet_ids); while($r = $o_stmt->fetch()) $orb_controls[$r['planet_id']][$r['faction_id']] = $r['control_level']; - // Terrestrial (Simplified for Galaxy view: use aggregated if available or skip) - // Let's at least get city counts to know if it's empty + // 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 to know if it's empty $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); - $city_counts = []; while($r = $c_stmt->fetch()) $city_counts[$r['planet_id']] = $r['cnt']; + 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'] = []; // Simplified $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']]; @@ -188,9 +205,81 @@ function getStatusColor($status, $statuses_map) {