38676-vm/includes/status_helper.php
2026-02-26 01:39:06 +00:00

54 lines
2.1 KiB
PHP

<?php
/**
* Helper to calculate dynamic status based on rules.
*/
function get_dynamic_status($planet, $rules) {
// If no rules, return current status
if (empty($rules)) return $planet['status'];
foreach ($rules as $rule) {
// 1. Check if rule is active
if (!$rule['is_active']) continue;
// 2. Check if object type matches (NULL means global)
if ($rule['object_type_id'] !== null && $rule['object_type_id'] != $planet['type_id']) {
// Wait, planets table has 'type' (slug), not 'type_id'.
// I need to resolve the type_id or use slug.
// Let's assume we pass the planet with type_id or we resolve it here.
}
// 3. Check Condition
$match = false;
switch ($rule['condition_type']) {
case 'fixed':
$match = true;
break;
case 'orbital_control':
$val = (float)($planet['orbital_control'] ?? 0);
$min = $rule['min_control_value'] !== null ? (float)$rule['min_control_value'] : -1;
$max = $rule['max_control_value'] !== null ? (float)$rule['max_control_value'] : 101;
if ($val >= $min && $val <= $max) $match = true;
break;
case 'terrestrial_control':
$val = (float)($planet['terrestrial_control'] ?? 0);
$min = $rule['min_control_value'] !== null ? (float)$rule['min_control_value'] : -1;
$max = $rule['max_control_value'] !== null ? (float)$rule['max_control_value'] : 101;
if ($val >= $min && $val <= $max) $match = true;
break;
case 'uncontrolled':
$orb = (float)($planet['orbital_control'] ?? 0);
$terr = (float)($planet['terrestrial_control'] ?? 0);
if ($orb == 0 && $terr == 0) $match = true;
break;
}
if ($match) {
// Find status slug from id
// We should probably pass the status slug in the rule too or join it.
return $rule['status_slug'];
}
}
return $planet['status'];
}